|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | + |
| 6 | +namespace Stunts.CodeAnalysis; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// The members of a type that its stunt intercepts, for the checks of their signatures. |
| 10 | +/// </summary> |
| 11 | +static class InterceptedMembers |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// The <c>Invoke</c> method of a delegate, or the overridable and interface members of a |
| 15 | + /// class or interface, with the accessible constructors of the class when |
| 16 | + /// <paramref name="constructors"/> is true: a compile-time stunt intercepts them, and a |
| 17 | + /// run-time stunt does not. |
| 18 | + /// </summary> |
| 19 | + /// <param name="type">The type the stunt inherits or implements.</param> |
| 20 | + /// <param name="assembly">The assembly that contains the stunt.</param> |
| 21 | + /// <param name="constructors">Whether the stunt intercepts the constructors.</param> |
| 22 | + public static IEnumerable<ISymbol> Of(INamedTypeSymbol type, IAssemblySymbol assembly, bool constructors) |
| 23 | + { |
| 24 | + if (type.TypeKind == TypeKind.Delegate) |
| 25 | + { |
| 26 | + if (type.DelegateInvokeMethod is IMethodSymbol invoke) |
| 27 | + yield return invoke; |
| 28 | + |
| 29 | + yield break; |
| 30 | + } |
| 31 | + |
| 32 | + if (type.TypeKind == TypeKind.Interface) |
| 33 | + { |
| 34 | + foreach (var current in new[] { type }.Concat(type.AllInterfaces)) |
| 35 | + { |
| 36 | + foreach (var member in current.GetMembers()) |
| 37 | + { |
| 38 | + if (IsInterfaceMember(member)) |
| 39 | + yield return member; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + yield break; |
| 44 | + } |
| 45 | + |
| 46 | + var overridden = new HashSet<ISymbol>(SymbolEqualityComparer.Default); |
| 47 | + for (var current = type; current != null && current.SpecialType != SpecialType.System_Object; current = current.BaseType) |
| 48 | + { |
| 49 | + foreach (var member in current.GetMembers()) |
| 50 | + { |
| 51 | + if (Overridden(member) is ISymbol previous) |
| 52 | + overridden.Add(previous); |
| 53 | + if (overridden.Contains(member)) |
| 54 | + continue; |
| 55 | + if (IsOverridable(member, assembly)) |
| 56 | + yield return member; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (constructors) |
| 61 | + { |
| 62 | + foreach (var constructor in type.InstanceConstructors) |
| 63 | + { |
| 64 | + if (Accessible(constructor, assembly)) |
| 65 | + yield return constructor; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + foreach (var iface in type.AllInterfaces) |
| 70 | + { |
| 71 | + foreach (var member in iface.GetMembers()) |
| 72 | + { |
| 73 | + if (!IsInterfaceMember(member)) |
| 74 | + continue; |
| 75 | + |
| 76 | + var implementation = type.FindImplementationForInterfaceMember(member); |
| 77 | + if (implementation == null || implementation.ContainingType.TypeKind == TypeKind.Interface) |
| 78 | + yield return member; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Whether a type of the signature of the member (its return, property or event type, or |
| 85 | + /// one of its parameters) matches. |
| 86 | + /// </summary> |
| 87 | + public static bool Uses(ISymbol member, Func<ITypeSymbol, bool> matches) => member switch |
| 88 | + { |
| 89 | + IMethodSymbol method => matches(method.ReturnType) || method.Parameters.Any(parameter => matches(parameter.Type)), |
| 90 | + IPropertySymbol property => matches(property.Type) || property.Parameters.Any(parameter => matches(parameter.Type)), |
| 91 | + IEventSymbol symbol => matches(symbol.Type), |
| 92 | + _ => false, |
| 93 | + }; |
| 94 | + |
| 95 | + static bool IsOverridable(ISymbol member, IAssemblySymbol assembly) |
| 96 | + => IsCallable(member) && |
| 97 | + !member.IsSealed && |
| 98 | + (member.IsAbstract || member.IsVirtual || member.IsOverride) && |
| 99 | + Accessible(member, assembly); |
| 100 | + |
| 101 | + static bool IsInterfaceMember(ISymbol member) |
| 102 | + => IsCallable(member) && !IsExplicit(member); |
| 103 | + |
| 104 | + static bool IsCallable(ISymbol member) |
| 105 | + { |
| 106 | + if (member.IsStatic || member.IsImplicitlyDeclared) |
| 107 | + return false; |
| 108 | + |
| 109 | + if (member is IMethodSymbol method) |
| 110 | + return method.MethodKind == MethodKind.Ordinary && method.CanBeReferencedByName; |
| 111 | + |
| 112 | + return member is IPropertySymbol or IEventSymbol; |
| 113 | + } |
| 114 | + |
| 115 | + static bool IsExplicit(ISymbol member) => member switch |
| 116 | + { |
| 117 | + IMethodSymbol method => method.ExplicitInterfaceImplementations.Length > 0, |
| 118 | + IPropertySymbol property => property.ExplicitInterfaceImplementations.Length > 0, |
| 119 | + IEventSymbol symbol => symbol.ExplicitInterfaceImplementations.Length > 0, |
| 120 | + _ => false, |
| 121 | + }; |
| 122 | + |
| 123 | + static bool Accessible(ISymbol member, IAssemblySymbol assembly) |
| 124 | + { |
| 125 | + switch (member.DeclaredAccessibility) |
| 126 | + { |
| 127 | + case Accessibility.Public: |
| 128 | + case Accessibility.Protected: |
| 129 | + case Accessibility.ProtectedOrInternal: |
| 130 | + return true; |
| 131 | + case Accessibility.Internal: |
| 132 | + return member.ContainingAssembly.GivesAccessTo(assembly); |
| 133 | + case Accessibility.ProtectedAndInternal: |
| 134 | + return SymbolEqualityComparer.Default.Equals(member.ContainingAssembly, assembly); |
| 135 | + default: |
| 136 | + return false; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + static ISymbol? Overridden(ISymbol member) => member switch |
| 141 | + { |
| 142 | + IMethodSymbol method => method.OverriddenMethod, |
| 143 | + IPropertySymbol property => property.OverriddenProperty, |
| 144 | + IEventSymbol symbol => symbol.OverriddenEvent, |
| 145 | + _ => null, |
| 146 | + }; |
| 147 | +} |
0 commit comments