Debian Codegraph @

modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/ExtensionMethods.cs

raw symbols SCIP
1 using System.Collections.Generic;
2 using System.Linq;
3 using Microsoft.CodeAnalysis;
4 using Microsoft.CodeAnalysis.CSharp;
5 using Microsoft.CodeAnalysis.CSharp.Syntax;
6
7 namespace Godot.SourceGenerators.Internal;
8
9 internal static class ExtensionMethods
10 {
11 public static AttributeData? GetGenerateUnmanagedCallbacksAttribute(this INamedTypeSymbol symbol)
12 => symbol.GetAttributes()
13 .FirstOrDefault(a => a.AttributeClass?.IsGenerateUnmanagedCallbacksAttribute() ?? false);
14
15 private static bool HasGenerateUnmanagedCallbacksAttribute(
16 this ClassDeclarationSyntax cds, Compilation compilation,
17 out INamedTypeSymbol? symbol
18 )
19 {
20 var sm = compilation.GetSemanticModel(cds.SyntaxTree);
21
22 var classTypeSymbol = sm.GetDeclaredSymbol(cds);
23 if (classTypeSymbol == null)
24 {
25 symbol = null;
26 return false;
27 }
28
29 if (!classTypeSymbol.GetAttributes()
30 .Any(a => a.AttributeClass?.IsGenerateUnmanagedCallbacksAttribute() ?? false))
31 {
32 symbol = null;
33 return false;
34 }
35
36 symbol = classTypeSymbol;
37 return true;
38 }
39
40 private static bool IsGenerateUnmanagedCallbacksAttribute(this INamedTypeSymbol symbol)
41 => symbol.FullQualifiedNameOmitGlobal() == GeneratorClasses.GenerateUnmanagedCallbacksAttr;
42
43 public static IEnumerable<(ClassDeclarationSyntax cds, INamedTypeSymbol symbol)> SelectUnmanagedCallbacksClasses(
44 this IEnumerable<ClassDeclarationSyntax> source,
45 Compilation compilation
46 )
47 {
48 foreach (var cds in source)
49 {
50 if (cds.HasGenerateUnmanagedCallbacksAttribute(compilation, out var symbol))
51 yield return (cds, symbol!);
52 }
53 }
54
55 public static bool IsNested(this TypeDeclarationSyntax cds)
56 => cds.Parent is TypeDeclarationSyntax;
57
58 public static bool IsPartial(this TypeDeclarationSyntax cds)
59 => cds.Modifiers.Any(SyntaxKind.PartialKeyword);
60
61 public static bool AreAllOuterTypesPartial(
62 this TypeDeclarationSyntax cds,
63 out TypeDeclarationSyntax? typeMissingPartial
64 )
65 {
66 SyntaxNode? outerSyntaxNode = cds.Parent;
67
68 while (outerSyntaxNode is TypeDeclarationSyntax outerTypeDeclSyntax)
69 {
70 if (!outerTypeDeclSyntax.IsPartial())
71 {
72 typeMissingPartial = outerTypeDeclSyntax;
73 return false;
74 }
75
76 outerSyntaxNode = outerSyntaxNode.Parent;
77 }
78
79 typeMissingPartial = null;
80 return true;
81 }
82
83 public static string GetDeclarationKeyword(this INamedTypeSymbol namedTypeSymbol)
84 {
85 string? keyword = namedTypeSymbol.DeclaringSyntaxReferences
86 .OfType<TypeDeclarationSyntax>().FirstOrDefault()?
87 .Keyword.Text;
88
89 return keyword ?? namedTypeSymbol.TypeKind switch
90 {
91 TypeKind.Interface => "interface",
92 TypeKind.Struct => "struct",
93 _ => "class"
94 };
95 }
96
97 private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } =
98 SymbolDisplayFormat.FullyQualifiedFormat
99 .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
100
101 private static SymbolDisplayFormat FullyQualifiedFormatIncludeGlobal { get; } =
102 SymbolDisplayFormat.FullyQualifiedFormat
103 .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Included);
104
105 public static string FullQualifiedNameOmitGlobal(this ITypeSymbol symbol)
106 => symbol.ToDisplayString(NullableFlowState.NotNull, FullyQualifiedFormatOmitGlobal);
107
108 public static string FullQualifiedNameOmitGlobal(this INamespaceSymbol namespaceSymbol)
109 => namespaceSymbol.ToDisplayString(FullyQualifiedFormatOmitGlobal);
110
111 public static string FullQualifiedNameIncludeGlobal(this ITypeSymbol symbol)
112 => symbol.ToDisplayString(NullableFlowState.NotNull, FullyQualifiedFormatIncludeGlobal);
113
114 public static string FullQualifiedNameIncludeGlobal(this INamespaceSymbol namespaceSymbol)
115 => namespaceSymbol.ToDisplayString(FullyQualifiedFormatIncludeGlobal);
116
117 public static string SanitizeQualifiedNameForUniqueHint(this string qualifiedName)
118 => qualifiedName
119 // AddSource() doesn't support @ prefix
120 .Replace("@", "")
121 // AddSource() doesn't support angle brackets
122 .Replace("<", "(Of ")
123 .Replace(">", ")");
124 }