|
| 1 | +using dotnetCampus.Ipc.CodeAnalysis.Models; |
| 2 | +using dotnetCampus.Ipc.Generators.Compiling; |
| 3 | + |
| 4 | +namespace dotnetCampus.Ipc.Analyzers.Compiling; |
| 5 | + |
| 6 | +internal static class IpcAttributeHelper |
| 7 | +{ |
| 8 | + public static IEnumerable<(AttributeSyntax attributeNode, IpcPublicAttributeNamedValues namedValues)> TryFindIpcPublicAttributes( |
| 9 | + SemanticModel semanticModel, InterfaceDeclarationSyntax typeDeclarationNode) |
| 10 | + { |
| 11 | + var typeSymbol = semanticModel.GetDeclaredSymbol(typeDeclarationNode); |
| 12 | + if (typeSymbol is null) |
| 13 | + { |
| 14 | + yield break; |
| 15 | + } |
| 16 | + |
| 17 | + if (typeDeclarationNode.AttributeLists.SelectMany(x => x.Attributes).FirstOrDefault(x => |
| 18 | + { |
| 19 | + string? attributeName = x.Name switch |
| 20 | + { |
| 21 | + IdentifierNameSyntax identifierName => identifierName.ToString(), |
| 22 | + QualifiedNameSyntax qualifiedName => qualifiedName.ChildNodes().OfType<IdentifierNameSyntax>().LastOrDefault()?.ToString(), |
| 23 | + _ => null, |
| 24 | + }; |
| 25 | + return attributeName is not null && |
| 26 | + (attributeName.Equals(nameof(IpcPublicAttribute), StringComparison.Ordinal) |
| 27 | + || attributeName.Equals(GetAttributeName(nameof(IpcPublicAttribute)), StringComparison.Ordinal)); |
| 28 | + }) is { } attributeNode) |
| 29 | + { |
| 30 | + var namedValues = typeSymbol.GetIpcPublicNamedValues(); |
| 31 | + yield return (attributeNode, namedValues); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static IEnumerable<(AttributeSyntax attributeNode, IpcShapeAttributeNamedValues namedValues)> TryFindIpcShapeAttributes( |
| 36 | + SemanticModel semanticModel, ClassDeclarationSyntax typeDeclarationNode) |
| 37 | + { |
| 38 | + var typeSymbol = semanticModel.GetDeclaredSymbol(typeDeclarationNode); |
| 39 | + if (typeSymbol is null) |
| 40 | + { |
| 41 | + yield break; |
| 42 | + } |
| 43 | + |
| 44 | + if (typeDeclarationNode.AttributeLists.SelectMany(x => x.Attributes).FirstOrDefault(x => |
| 45 | + { |
| 46 | + string? attributeName = x.Name switch |
| 47 | + { |
| 48 | + IdentifierNameSyntax identifierName => identifierName.ToString(), |
| 49 | + QualifiedNameSyntax qualifiedName => qualifiedName.ChildNodes().OfType<IdentifierNameSyntax>().LastOrDefault()?.ToString(), |
| 50 | + _ => null, |
| 51 | + }; |
| 52 | + return attributeName is not null && |
| 53 | + (attributeName.Equals(nameof(IpcShapeAttribute), StringComparison.Ordinal) |
| 54 | + || attributeName.Equals(GetAttributeName(nameof(IpcShapeAttribute)), StringComparison.Ordinal)); |
| 55 | + }) is { } attributeNode) |
| 56 | + { |
| 57 | + var namedValues = typeSymbol.GetIpcShapeNamedValues(); |
| 58 | + yield return (attributeNode, namedValues); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static IEnumerable<(AttributeSyntax? attributeNode, IpcPublicAttributeNamedValues namedValues)> TryFindMemberAttributes( |
| 63 | + SemanticModel semanticModel, TypeDeclarationSyntax typeDeclarationNode) |
| 64 | + { |
| 65 | + if (TryFindIpcPublicType(semanticModel, typeDeclarationNode, out var compilation)) |
| 66 | + { |
| 67 | + foreach (var (contractType, member) in compilation.EnumerateMembers()) |
| 68 | + { |
| 69 | + var namedValues = member switch |
| 70 | + { |
| 71 | + IPropertySymbol propertySymbol => propertySymbol.GetIpcNamedValues(contractType), |
| 72 | + IMethodSymbol methodSymbol => methodSymbol.GetIpcNamedValues(null, contractType), |
| 73 | + _ => null, |
| 74 | + }; |
| 75 | + if (namedValues is null) |
| 76 | + { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + var memberNode = typeDeclarationNode.DescendantNodes() |
| 81 | + .Where(x => x is PropertyDeclarationSyntax or MethodDeclarationSyntax) |
| 82 | + .FirstOrDefault(x => x switch |
| 83 | + { |
| 84 | + PropertyDeclarationSyntax propertyDeclarationSyntax => SymbolEqualityComparer.Default.Equals(member, semanticModel.GetDeclaredSymbol(propertyDeclarationSyntax)), |
| 85 | + MethodDeclarationSyntax methodDeclarationSyntax => SymbolEqualityComparer.Default.Equals(member, semanticModel.GetDeclaredSymbol(methodDeclarationSyntax)), |
| 86 | + _ => false, |
| 87 | + }) as MemberDeclarationSyntax; |
| 88 | + if (memberNode is not null && memberNode.AttributeLists.SelectMany(x => x.Attributes).FirstOrDefault(x => |
| 89 | + { |
| 90 | + string? attributeName = x.Name switch |
| 91 | + { |
| 92 | + IdentifierNameSyntax identifierName => identifierName.ToString(), |
| 93 | + QualifiedNameSyntax qualifiedName => qualifiedName.ChildNodes().OfType<IdentifierNameSyntax>().LastOrDefault()?.ToString(), |
| 94 | + _ => null, |
| 95 | + }; |
| 96 | + return attributeName is not null && |
| 97 | + (attributeName.Equals(nameof(IpcPropertyAttribute), StringComparison.Ordinal) |
| 98 | + || attributeName.Equals(GetAttributeName(nameof(IpcPropertyAttribute)), StringComparison.Ordinal) |
| 99 | + || attributeName.Equals(nameof(IpcMethodAttribute), StringComparison.Ordinal) |
| 100 | + || attributeName.Equals(GetAttributeName(nameof(IpcMethodAttribute)), StringComparison.Ordinal)); |
| 101 | + }) is { } attributeNode) |
| 102 | + { |
| 103 | + yield return (attributeNode, namedValues); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + yield return (null, namedValues); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static bool TryFindIpcPublicType(SemanticModel semanticModel, TypeDeclarationSyntax typeDeclarationNode, |
| 114 | + [NotNullWhen(true)] out IpcPublicCompilation? publicIpcObjectCompilation) |
| 115 | + { |
| 116 | + if (semanticModel.GetDeclaredSymbol(typeDeclarationNode) is { } typeDeclarationSymbol |
| 117 | + && typeDeclarationSymbol.GetAttributes().FirstOrDefault(x => string.Equals( |
| 118 | + x.AttributeClass?.ToString(), |
| 119 | + typeof(IpcPublicAttribute).FullName, |
| 120 | + StringComparison.Ordinal)) is { } ipcPublicAttribute) |
| 121 | + { |
| 122 | + publicIpcObjectCompilation = new(typeDeclarationNode.SyntaxTree, semanticModel, typeDeclarationSymbol); |
| 123 | + return true; |
| 124 | + } |
| 125 | + publicIpcObjectCompilation = null; |
| 126 | + return false; |
| 127 | + } |
| 128 | +} |
0 commit comments