|
| 1 | +// SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +using System; |
| 4 | +#if NULL_STATE_STATIC_ANALYSIS_ATTRIBUTES |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +#endif |
| 7 | +using System.Linq; |
| 8 | +using System.Reflection; |
| 9 | + |
| 10 | +namespace Smdn.Reflection; |
| 11 | + |
| 12 | +internal static class TypeGenericParameterExtensions { |
| 13 | + private static void ValidateGenericParameterArgument(Type param, string paramName) |
| 14 | + { |
| 15 | + if (param is null) |
| 16 | + throw new ArgumentNullException(paramName); |
| 17 | + if (!param.IsGenericParameter) |
| 18 | + throw new InvalidOperationException($"{paramName} must be a generic parameter or generic argument"); |
| 19 | + } |
| 20 | + |
| 21 | + // ref: https://github.com/dotnet/roslyn/blob/main/docs/features/nullable-metadata.md#type-parameters |
| 22 | + public static bool HasGenericParameterNotNullConstraint(this Type genericParameter) |
| 23 | + { |
| 24 | + ValidateGenericParameterArgument(genericParameter, nameof(genericParameter)); |
| 25 | + |
| 26 | + var attrNullable = genericParameter.CustomAttributes.FirstOrDefault(IsNullableAttribute); |
| 27 | + var attrNullableContext = FindNullableContextAttribute(genericParameter); |
| 28 | + |
| 29 | + const byte notAnnotated = 1; |
| 30 | + |
| 31 | + if (attrNullableContext is not null && notAnnotated.Equals(attrNullableContext.ConstructorArguments[0].Value)) |
| 32 | + // `#nullable enable` context |
| 33 | + return attrNullable is null; |
| 34 | + else |
| 35 | + // `#nullable disable` context |
| 36 | + return attrNullable is not null && notAnnotated.Equals(attrNullable.ConstructorArguments[0].Value); |
| 37 | + } |
| 38 | + |
| 39 | + private static bool IsNullableAttribute(CustomAttributeData attr) |
| 40 | + => "System.Runtime.CompilerServices.NullableAttribute".Equals(attr.AttributeType.FullName, StringComparison.Ordinal); |
| 41 | + |
| 42 | + private static bool IsNullableContextAttribute(CustomAttributeData attr) |
| 43 | + => "System.Runtime.CompilerServices.NullableContextAttribute".Equals(attr.AttributeType.FullName, StringComparison.Ordinal); |
| 44 | + |
| 45 | + private static bool TryGetNullableContextAttribute( |
| 46 | + MemberInfo member, |
| 47 | +#if NULL_STATE_STATIC_ANALYSIS_ATTRIBUTES |
| 48 | + [NotNullWhen(true)] |
| 49 | +#endif |
| 50 | + out CustomAttributeData? attrNullableContext |
| 51 | + ) |
| 52 | + { |
| 53 | + attrNullableContext = member.CustomAttributes.FirstOrDefault(IsNullableContextAttribute); |
| 54 | + |
| 55 | + return attrNullableContext is not null; |
| 56 | + } |
| 57 | + |
| 58 | + private static CustomAttributeData? FindNullableContextAttribute(Type genericParameter) |
| 59 | + { |
| 60 | + if ( |
| 61 | + genericParameter.IsGenericMethodParameter && |
| 62 | + genericParameter.DeclaringMethod is not null && |
| 63 | + TryGetNullableContextAttribute(genericParameter.DeclaringMethod, out var methodAttr) |
| 64 | + ) { |
| 65 | + return methodAttr; |
| 66 | + } |
| 67 | + |
| 68 | + Type? t = genericParameter; |
| 69 | + |
| 70 | + for (; ; ) { |
| 71 | + if ((t = t!.DeclaringType) is null) |
| 72 | + return null; |
| 73 | + if (TryGetNullableContextAttribute(t, out var typeAttr)) |
| 74 | + return typeAttr; |
| 75 | + |
| 76 | + // retry against to the outer type |
| 77 | + continue; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments