|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Reflection.Metadata; |
| 8 | +using ILCompiler.Logging; |
| 9 | +using ILLink.Shared; |
4 | 10 | using Internal.TypeSystem;
|
5 | 11 | using Internal.TypeSystem.Ecma;
|
6 | 12 |
|
@@ -44,40 +50,147 @@ internal static string GetGenericParameterDeclaringMemberDisplayName(GenericPara
|
44 | 50 | return ((TypeDesc)parent).GetDisplayName();
|
45 | 51 | }
|
46 | 52 |
|
47 |
| - internal static string GetRequiresAttributeMessage(MethodDesc method, string requiresAttributeName) |
| 53 | + internal static bool TryGetRequiresAttribute(TypeSystemEntity member, string requiresAttributeName, [NotNullWhen(returnValue: true)] out CustomAttributeValue<TypeDesc>? attribute) |
48 | 54 | {
|
49 |
| - var ecmaMethod = method.GetTypicalMethodDefinition() as EcmaMethod; |
50 |
| - if (ecmaMethod == null) |
51 |
| - return null; |
| 55 | + attribute = default; |
| 56 | + CustomAttributeValue<TypeDesc>? decoded = default; |
| 57 | + switch (member) |
| 58 | + { |
| 59 | + case MethodDesc method: |
| 60 | + var ecmaMethod = method.GetTypicalMethodDefinition() as EcmaMethod; |
| 61 | + if (ecmaMethod == null) |
| 62 | + return false; |
| 63 | + decoded = ecmaMethod.GetDecodedCustomAttribute("System.Diagnostics.CodeAnalysis", requiresAttributeName); |
| 64 | + break; |
| 65 | + case MetadataType type: |
| 66 | + var ecmaType = type as EcmaType; |
| 67 | + if (ecmaType == null) |
| 68 | + return false; |
| 69 | + decoded = ecmaType.GetDecodedCustomAttribute("System.Diagnostics.CodeAnalysis", requiresAttributeName); |
| 70 | + break; |
| 71 | + case PropertyPseudoDesc property: |
| 72 | + decoded = property.GetDecodedCustomAttribute("System.Diagnostics.CodeAnalysis", requiresAttributeName); |
| 73 | + break; |
| 74 | + default: |
| 75 | + Debug.Fail("Trying to operate with unsupported TypeSystemEntity " + member.GetType().ToString()); |
| 76 | + break; |
| 77 | + } |
| 78 | + if (!decoded.HasValue) |
| 79 | + return false; |
| 80 | + |
| 81 | + attribute = decoded.Value; |
| 82 | + return true; |
| 83 | + } |
52 | 84 |
|
53 |
| - var decoded = ecmaMethod.GetDecodedCustomAttribute("System.Diagnostics.CodeAnalysis", requiresAttributeName); |
54 |
| - if (decoded == null) |
| 85 | + public static CustomAttributeValue<TypeDesc>? GetDecodedCustomAttribute(this PropertyPseudoDesc prop, string attributeNamespace, string attributeName) |
| 86 | + { |
| 87 | + var ecmaType = prop.OwningType as EcmaType; |
| 88 | + var metadataReader = ecmaType.MetadataReader; |
| 89 | + |
| 90 | + var attributeHandle = metadataReader.GetCustomAttributeHandle(prop.GetCustomAttributes, |
| 91 | + attributeNamespace, attributeName); |
| 92 | + |
| 93 | + if (attributeHandle.IsNil) |
55 | 94 | return null;
|
56 | 95 |
|
57 |
| - var decodedValue = decoded.Value; |
| 96 | + return metadataReader.GetCustomAttribute(attributeHandle).DecodeValue(new CustomAttributeTypeProvider(ecmaType.EcmaModule)); |
| 97 | + } |
58 | 98 |
|
59 |
| - if (decodedValue.FixedArguments.Length != 0) |
60 |
| - return (string)decodedValue.FixedArguments[0].Value; |
| 99 | + internal static string GetRequiresAttributeMessage(CustomAttributeValue<TypeDesc> attribute) |
| 100 | + { |
| 101 | + if (attribute.FixedArguments.Length != 0) |
| 102 | + return (string)attribute.FixedArguments[0].Value; |
61 | 103 |
|
62 | 104 | return null;
|
63 | 105 | }
|
64 | 106 |
|
65 |
| - internal static string GetRequiresAttributeUrl(MethodDesc method, string requiresAttributeName) |
| 107 | + internal static string GetRequiresAttributeUrl(CustomAttributeValue<TypeDesc> attribute) |
66 | 108 | {
|
67 |
| - var ecmaMethod = method.GetTypicalMethodDefinition() as EcmaMethod; |
68 |
| - if (ecmaMethod == null) |
69 |
| - return null; |
| 109 | + if (attribute.NamedArguments.Length != 0 && attribute.NamedArguments[0].Name == "Url") |
| 110 | + return (string)attribute.NamedArguments[0].Value; |
70 | 111 |
|
71 |
| - var decoded = ecmaMethod.GetDecodedCustomAttribute("System.Diagnostics.CodeAnalysis", requiresAttributeName); |
72 |
| - if (decoded == null) |
73 |
| - return null; |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Determines if method is within a declared Requires scope - this typically means that trim analysis |
| 117 | + /// warnings should be suppressed in such a method. |
| 118 | + /// </summary> |
| 119 | + /// <remarks>Unlike <see cref="DoesMemberRequire(TypeSystemEntity, string, out CustomAttributeValue{TypeDesc}?)"/> |
| 120 | + /// if a declaring type has Requires, all methods in that type are considered "in scope" of that Requires. So this includes also |
| 121 | + /// instance methods (not just statics and .ctors).</remarks> |
| 122 | + internal static bool IsInRequiresScope(this MethodDesc method, string requiresAttribute) => |
| 123 | + method.IsInRequiresScope(requiresAttribute, true); |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// True if member of a call is considered to be annotated with the Requires... attribute. |
| 127 | + /// Doesn't check the associated symbol for overrides and virtual methods because we should warn on mismatched between the property AND the accessors |
| 128 | + /// </summary> |
| 129 | + /// <param name="method"> |
| 130 | + /// MethodDesc that is either an overriding member or an overriden/virtual member |
| 131 | + /// </param> |
| 132 | + internal static bool IsOverrideInRequiresScope(this MethodDesc method, string requiresAttribute) => |
| 133 | + method.IsInRequiresScope(requiresAttribute, false); |
| 134 | + |
| 135 | + private static bool IsInRequiresScope(this MethodDesc method, string requiresAttribute, bool checkAssociatedSymbol) |
| 136 | + { |
| 137 | + if (method.HasCustomAttribute("System.Diagnostics.CodeAnalysis", requiresAttribute) && !method.IsStaticConstructor) |
| 138 | + return true; |
74 | 139 |
|
75 |
| - var decodedValue = decoded.Value; |
| 140 | + if (method.OwningType is TypeDesc type && TryGetRequiresAttribute(type, requiresAttribute, out _)) |
| 141 | + return true; |
76 | 142 |
|
77 |
| - if (decodedValue.NamedArguments.Length != 0 && decodedValue.NamedArguments[0].Name == "Url") |
78 |
| - return (string)decodedValue.NamedArguments[0].Value; |
| 143 | + if (checkAssociatedSymbol && method.GetPropertyForAccessor() is PropertyPseudoDesc property && TryGetRequiresAttribute(property, requiresAttribute, out _)) |
| 144 | + return true; |
79 | 145 |
|
80 |
| - return null; |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + internal static bool DoesMethodRequire(this MethodDesc method, string requiresAttribute, [NotNullWhen(returnValue: true)] out CustomAttributeValue<TypeDesc>? attribute) |
| 150 | + { |
| 151 | + attribute = null; |
| 152 | + if (method.IsStaticConstructor) |
| 153 | + return false; |
| 154 | + |
| 155 | + if (TryGetRequiresAttribute(method, requiresAttribute, out attribute)) |
| 156 | + return true; |
| 157 | + |
| 158 | + if ((method.Signature.IsStatic || method.IsConstructor) && method.OwningType is TypeDesc owningType && |
| 159 | + !owningType.IsArray && TryGetRequiresAttribute(owningType, requiresAttribute, out attribute)) |
| 160 | + return true; |
| 161 | + |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + internal static bool DoesFieldRequire(this FieldDesc field, string requiresAttribute, [NotNullWhen(returnValue: true)] out CustomAttributeValue<TypeDesc>? attribute) |
| 166 | + { |
| 167 | + if (!field.IsStatic || field.OwningType is not TypeDesc owningType || owningType.IsArray) |
| 168 | + { |
| 169 | + attribute = null; |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + return TryGetRequiresAttribute(field.OwningType, requiresAttribute, out attribute); |
| 174 | + } |
| 175 | + |
| 176 | + internal static bool DoesPropertyRequire(this PropertyPseudoDesc property, string requiresAttribute, [NotNullWhen(returnValue: true)] out CustomAttributeValue<TypeDesc>? attribute) => |
| 177 | + TryGetRequiresAttribute(property, requiresAttribute, out attribute); |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// Determines if member requires (and thus any usage of such method should be warned about). |
| 181 | + /// </summary> |
| 182 | + /// <remarks>Unlike <see cref="IsInRequiresScope(MethodDesc, string)"/> only static methods |
| 183 | + /// and .ctors are reported as requires when the declaring type has Requires on it.</remarks> |
| 184 | + internal static bool DoesMemberRequire(this TypeSystemEntity member, string requiresAttribute, [NotNullWhen(returnValue: true)] out CustomAttributeValue<TypeDesc>? attribute) |
| 185 | + { |
| 186 | + attribute = null; |
| 187 | + return member switch |
| 188 | + { |
| 189 | + MethodDesc method => DoesMethodRequire(method, requiresAttribute, out attribute), |
| 190 | + FieldDesc field => DoesFieldRequire(field, requiresAttribute, out attribute), |
| 191 | + PropertyPseudoDesc property => DoesPropertyRequire(property, requiresAttribute, out attribute), |
| 192 | + _ => false |
| 193 | + }; |
81 | 194 | }
|
82 | 195 | }
|
83 | 196 | }
|
0 commit comments