|
| 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 | + |
| 4 | +using System.Linq; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 8 | + |
| 9 | +namespace System.ServiceModel.BuildTools |
| 10 | +{ |
| 11 | + internal static class MethodSymbolExtensions |
| 12 | + { |
| 13 | + public static bool? IsGeneratedCode(this IMethodSymbol methodSymbol) |
| 14 | + => methodSymbol.Locations.FirstOrDefault()?.SourceTree?.FilePath.EndsWith(".g.cs"); |
| 15 | + |
| 16 | + public static bool IsMatchingUserProvidedMethod(this IMethodSymbol methodSymbol, IMethodSymbol userProvidedMethodSymbol, INamedTypeSymbol? coreWCFInjectedAttribute, INamedTypeSymbol? fromServicesAttribute) |
| 17 | + { |
| 18 | + int parameterFound = 0; |
| 19 | + if (methodSymbol.Name != userProvidedMethodSymbol.Name) |
| 20 | + { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + var parameters = methodSymbol.Parameters; |
| 25 | + |
| 26 | + for (int i = 0,j = 0; i < userProvidedMethodSymbol.Parameters.Length; i++) |
| 27 | + { |
| 28 | + IParameterSymbol parameterSymbol = userProvidedMethodSymbol.Parameters[i]; |
| 29 | + if (parameterSymbol.GetOneAttributeOf(coreWCFInjectedAttribute, fromServicesAttribute) is not null) |
| 30 | + { |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + if (parameterSymbol.IsMatchingParameter(parameters[j])) |
| 35 | + { |
| 36 | + j++; |
| 37 | + parameterFound++; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return parameterFound == parameters.Length; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + internal static class ParameterSymbolExtensions |
| 46 | + { |
| 47 | + public static bool IsMatchingParameter(this IParameterSymbol symbol, IParameterSymbol parameterSymbol) |
| 48 | + => SymbolEqualityComparer.Default.Equals(symbol.Type, parameterSymbol.Type); |
| 49 | + } |
| 50 | + |
| 51 | + internal static class SymbolExtensions |
| 52 | + { |
| 53 | + public static AttributeData? GetOneAttributeOf(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols) |
| 54 | + { |
| 55 | + if (attributeTypeSymbols.Length == 0) |
| 56 | + { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + foreach (var attribute in symbol.GetAttributes()) |
| 61 | + { |
| 62 | + foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null)) |
| 63 | + { |
| 64 | + if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, namedTypeSymbol)) |
| 65 | + { |
| 66 | + return attribute; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + public static bool HasOneAttributeInheritFrom(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols) |
| 75 | + { |
| 76 | + if (attributeTypeSymbols.Length == 0) |
| 77 | + { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + foreach (var attribute in symbol.GetAttributes()) |
| 82 | + { |
| 83 | + foreach (var @interface in attribute.AttributeClass!.AllInterfaces) |
| 84 | + { |
| 85 | + foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null)) |
| 86 | + { |
| 87 | + if (SymbolEqualityComparer.Default.Equals(@interface, namedTypeSymbol)) |
| 88 | + { |
| 89 | + return true; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + internal static class NamedTypeSymbolExtensions |
| 100 | + { |
| 101 | + public static bool IsPartial(this INamedTypeSymbol namedTypeSymbol, out INamedTypeSymbol parentType) |
| 102 | + { |
| 103 | + bool result = namedTypeSymbol.DeclaringSyntaxReferences.Select(static s => s.GetSyntax()).OfType<ClassDeclarationSyntax>().All(static c => c.Modifiers.Any(static m => m.IsKind(SyntaxKind.PartialKeyword))); |
| 104 | + if (result && namedTypeSymbol.ContainingType != null) |
| 105 | + { |
| 106 | + return namedTypeSymbol.ContainingType.IsPartial(out parentType); |
| 107 | + } |
| 108 | + parentType = namedTypeSymbol; |
| 109 | + return result; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + internal static class TypedConstantExtensions |
| 114 | + { |
| 115 | + public static string ToSafeCSharpString(this TypedConstant typedConstant) |
| 116 | + { |
| 117 | + if (typedConstant.Kind == TypedConstantKind.Array) |
| 118 | + { |
| 119 | + return $"new [] {typedConstant.ToCSharpString()}"; |
| 120 | + } |
| 121 | + |
| 122 | + return typedConstant.ToCSharpString(); |
| 123 | + |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments