|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.ComponentModel; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.Cci.Extensions.CSharp; |
| 9 | + |
| 10 | +namespace Microsoft.Cci.Extensions |
| 11 | +{ |
| 12 | + public static class MemberExtensions |
| 13 | + { |
| 14 | + public static bool IsVisibleOutsideAssembly(this ITypeDefinitionMember member) |
| 15 | + { |
| 16 | + return MemberHelper.IsVisibleOutsideAssembly(member); |
| 17 | + } |
| 18 | + |
| 19 | + public static bool IsVisibleToFriendAssemblies(this ITypeDefinitionMember member) |
| 20 | + { |
| 21 | + // MemberHelper in CCI doesn't have a method like IsVisibleOutsideAssembly(...) to use here. This method |
| 22 | + // has similar behavior except that it returns true for all internal and internal protected members as well |
| 23 | + // as non-sealed private protected members. |
| 24 | + switch (member.Visibility) |
| 25 | + { |
| 26 | + case TypeMemberVisibility.Assembly: |
| 27 | + case TypeMemberVisibility.FamilyOrAssembly: |
| 28 | + case TypeMemberVisibility.Public: |
| 29 | + return true; |
| 30 | + |
| 31 | + case TypeMemberVisibility.Family: |
| 32 | + case TypeMemberVisibility.FamilyAndAssembly: |
| 33 | + return !member.ContainingTypeDefinition.IsSealed; |
| 34 | + } |
| 35 | + |
| 36 | + var containingType = member.ContainingTypeDefinition; |
| 37 | + return member switch |
| 38 | + { |
| 39 | + IMethodDefinition methodDefinition => |
| 40 | + IsExplicitImplementationVisible(methodDefinition, containingType), |
| 41 | + IPropertyDefinition propertyDefinition => |
| 42 | + IsExplicitImplementationVisible(propertyDefinition.Getter, containingType) || |
| 43 | + IsExplicitImplementationVisible(propertyDefinition.Setter, containingType), |
| 44 | + IEventDefinition eventDefinition => |
| 45 | + IsExplicitImplementationVisible(eventDefinition.Adder, containingType) || |
| 46 | + IsExplicitImplementationVisible(eventDefinition.Remover, containingType), |
| 47 | + _ => false, |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + public static bool IsGenericInstance(this IMethodReference method) |
| 52 | + { |
| 53 | + return method is IGenericMethodInstanceReference; |
| 54 | + } |
| 55 | + |
| 56 | + public static bool IsWindowsRuntimeMember(this ITypeMemberReference member) |
| 57 | + { |
| 58 | + var assemblyRef = member.GetAssemblyReference(); |
| 59 | + |
| 60 | + return assemblyRef.IsWindowsRuntimeAssembly(); |
| 61 | + } |
| 62 | + |
| 63 | + public static string FullName(this ICustomAttribute attribute) |
| 64 | + { |
| 65 | + if (attribute is FakeCustomAttribute fca) |
| 66 | + { |
| 67 | + return fca.FullTypeName; |
| 68 | + } |
| 69 | + |
| 70 | + return attribute.Type.FullName(); |
| 71 | + } |
| 72 | + |
| 73 | + public static string GetMethodSignature(this IMethodDefinition method) |
| 74 | + { |
| 75 | + return MemberHelper.GetMethodSignature(method, NameFormattingOptions.Signature); |
| 76 | + } |
| 77 | + |
| 78 | + public static T UnWrapMember<T>(this T member) |
| 79 | + where T : ITypeMemberReference |
| 80 | + { |
| 81 | + return member switch |
| 82 | + { |
| 83 | + IGenericMethodInstanceReference genericMethod => (T)genericMethod.GenericMethod.UnWrapMember(), |
| 84 | + ISpecializedNestedTypeReference type => (T)type.UnspecializedVersion.UnWrapMember(), |
| 85 | + ISpecializedMethodReference method => (T)method.UnspecializedVersion.UnWrapMember(), |
| 86 | + ISpecializedFieldReference field => (T)field.UnspecializedVersion.UnWrapMember(), |
| 87 | + ISpecializedPropertyDefinition property => (T)property.UnspecializedVersion.UnWrapMember(), |
| 88 | + ISpecializedEventDefinition evnt => (T)evnt.UnspecializedVersion.UnWrapMember(), |
| 89 | + _ => member |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + public static bool IsPropertyOrEventAccessor(this IMethodDefinition method) |
| 94 | + { |
| 95 | + return method.GetAccessorType() != AccessorType.None; |
| 96 | + } |
| 97 | + |
| 98 | + public static AccessorType GetAccessorType(this IMethodDefinition methodDefinition) |
| 99 | + { |
| 100 | + if (!methodDefinition.IsSpecialName) |
| 101 | + { |
| 102 | + return AccessorType.None; |
| 103 | + } |
| 104 | + |
| 105 | + // Cannot use MemberHelper.IsAdder(...) and similar due to their TypeMemberVisibility.Public restriction. |
| 106 | + var name = methodDefinition.GetNameWithoutExplicitType(); |
| 107 | + if (name.StartsWith("get_")) |
| 108 | + { |
| 109 | + return AccessorType.EventAdder; |
| 110 | + } |
| 111 | + else if (name.StartsWith("set_")) |
| 112 | + { |
| 113 | + return AccessorType.PropertyGetter; |
| 114 | + } |
| 115 | + else if (name.StartsWith("add_")) |
| 116 | + { |
| 117 | + return AccessorType.EventRemover; |
| 118 | + } |
| 119 | + else if (name.StartsWith("remove_")) |
| 120 | + { |
| 121 | + return AccessorType.PropertySetter; |
| 122 | + } |
| 123 | + |
| 124 | + return AccessorType.None; |
| 125 | + } |
| 126 | + |
| 127 | + public static bool IsEditorBrowseableStateNever(this ICustomAttribute attribute) |
| 128 | + { |
| 129 | + if (attribute.Type.FullName() != typeof(EditorBrowsableAttribute).FullName) |
| 130 | + { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + if (attribute.Arguments == null || attribute.Arguments.Count() != 1) |
| 135 | + { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + var singleArgument = attribute.Arguments.Single() as IMetadataConstant; |
| 140 | + if (singleArgument == null || !(singleArgument.Value is int)) |
| 141 | + { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + if (EditorBrowsableState.Never != (EditorBrowsableState)singleArgument.Value) |
| 146 | + { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + public static bool IsObsoleteWithUsageTreatedAsCompilationError(this ICustomAttribute attribute) |
| 154 | + { |
| 155 | + if (attribute.Type.FullName() != typeof(ObsoleteAttribute).FullName) |
| 156 | + { |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + if (attribute.Arguments == null || attribute.Arguments.Count() != 2) |
| 161 | + { |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + var messageArgument = attribute.Arguments.ElementAt(0) as IMetadataConstant; |
| 166 | + var errorArgument = attribute.Arguments.ElementAt(1) as IMetadataConstant; |
| 167 | + if (messageArgument == null || errorArgument == null) |
| 168 | + { |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + if (!(messageArgument.Value is string && errorArgument.Value is bool)) |
| 173 | + { |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + return (bool)errorArgument.Value; |
| 178 | + } |
| 179 | + |
| 180 | + public static ApiKind GetApiKind(this ITypeDefinitionMember member) |
| 181 | + { |
| 182 | + if (member.ContainingTypeDefinition.IsDelegate) |
| 183 | + return ApiKind.DelegateMember; |
| 184 | + |
| 185 | + switch (member) |
| 186 | + { |
| 187 | + case IFieldDefinition field: |
| 188 | + if (member.ContainingTypeDefinition.IsEnum && field.IsSpecialName) |
| 189 | + { |
| 190 | + return ApiKind.EnumField; |
| 191 | + } |
| 192 | + |
| 193 | + return ApiKind.Field; |
| 194 | + |
| 195 | + case IPropertyDefinition _: |
| 196 | + return ApiKind.Property; |
| 197 | + |
| 198 | + case IEventDefinition _: |
| 199 | + return ApiKind.Event; |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | + var method = (IMethodDefinition)member; |
| 204 | + if (method.IsConstructor || method.IsStaticConstructor) |
| 205 | + { |
| 206 | + return ApiKind.Constructor; |
| 207 | + } |
| 208 | + |
| 209 | + var accessorType = method.GetAccessorType(); |
| 210 | + switch (accessorType) |
| 211 | + { |
| 212 | + case AccessorType.PropertyGetter: |
| 213 | + case AccessorType.PropertySetter: |
| 214 | + return ApiKind.PropertyAccessor; |
| 215 | + |
| 216 | + case AccessorType.EventAdder: |
| 217 | + case AccessorType.EventRemover: |
| 218 | + return ApiKind.EventAccessor; |
| 219 | + |
| 220 | + default: |
| 221 | + return ApiKind.Method; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + // A rewrite of MemberHelper.IsExplicitImplementationVisible(...) with looser visibility checks. |
| 226 | + private static bool IsExplicitImplementationVisible(IMethodReference method, ITypeDefinition containingType) |
| 227 | + { |
| 228 | + if (method == null) |
| 229 | + { |
| 230 | + return false; |
| 231 | + } |
| 232 | + |
| 233 | + using var enumerator = containingType.ExplicitImplementationOverrides.GetEnumerator(); |
| 234 | + while (enumerator.MoveNext()) |
| 235 | + { |
| 236 | + var current = enumerator.Current; |
| 237 | + if (current.ImplementingMethod.InternedKey == method.InternedKey) |
| 238 | + { |
| 239 | + var resolvedMethod = current.ImplementedMethod.ResolvedMethod; |
| 240 | + if (resolvedMethod is Dummy) |
| 241 | + { |
| 242 | + return true; |
| 243 | + } |
| 244 | + |
| 245 | + // Reviewers: Recursive call that mimics MemberHelper methods. But, why is this safe? (I'm undoing |
| 246 | + // my InternalsAndPublicCciFilter.SimpleInclude(...) failsafe but double-checking.) |
| 247 | + if (IsVisibleToFriendAssemblies(resolvedMethod)) |
| 248 | + { |
| 249 | + return true; |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return false; |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments