LILAC
Language to language Interop LAyer Compiler
Loading...
Searching...
No Matches
TypeHelper.cs
Go to the documentation of this file.
1using Microsoft.CodeAnalysis;
2
3namespace Lilac.Generator;
4
5public static class TypeHelper
6{
7 public static bool IsExported(this ISymbol symbol)
8 {
9 return symbol
10 .GetAttributes()
11 .Any(attr => attr.AttributeClass?.ToDisplayString() == "Lilac.Annotations.ExportAttribute");
12 }
13
14 public static string GetFullName(this ISymbol symbol)
15 {
16 return symbol.ContainingNamespace is null
17 ? symbol.Name
18 : $"{symbol.ContainingNamespace.ToDisplayString().Replace(".", "::")}::{symbol.Name}";
19 }
20
21 public static string GetManagedFullName(this ISymbol symbol)
22 {
23 return $"{symbol.ContainingNamespace.ToDisplayString()}.{symbol.Name}";
24 }
25
26 public static string GetNativeName(this ISymbol symbol)
27 {
28 var dict = new Dictionary<string, string>
29 {
30 ["System::Void"] = "void",
31 ["System::Boolean"] = "bool",
32 ["System::SByte"] = "std::int8_t",
33 ["System::Int16"] = "std::int16_t",
34 ["System::Int32"] = "std::int32_t",
35 ["System::Int64"] = "std::int64_t",
36 ["System::IntPtr"] = "std::intptr_t",
37 ["System::Byte"] = "std::uint8_t",
38 ["System::UInt16"] = "std::uint16_t",
39 ["System::UInt32"] = "std::uint32_t",
40 ["System::UInt64"] = "std::uint64_t",
41 ["System::UIntPtr"] = "std::uintptr_t",
42 ["System::Char"] = "std::char16_t",
43 ["System::Double"] = "std::float64_t",
44 ["System::Single"] = "std::float32_t",
45 ["System::Half"] = "std::float16_t",
46 ["System::String"] = "clr::string"
47 };
48
49 var fullname = symbol.GetFullName();
50 return dict.GetValueOrDefault(fullname, fullname);
51 }
52
53 public static string GetUnmanagedPackedName(this ISymbol symbol, string @default = "void*")
54 {
55 var dict = new Dictionary<string, string>
56 {
57 ["System::Void"] = "void",
58 ["System::Boolean"] = "bool",
59 ["System::SByte"] = "std::int8_t",
60 ["System::Int16"] = "std::int16_t",
61 ["System::Int32"] = "std::int32_t",
62 ["System::Int64"] = "std::int64_t",
63 ["System::IntPtr"] = "std::intptr_t",
64 ["System::Byte"] = "std::uint8_t",
65 ["System::UInt16"] = "std::uint16_t",
66 ["System::UInt32"] = "std::uint32_t",
67 ["System::UInt64"] = "std::uint64_t",
68 ["System::UIntPtr"] = "std::uintptr_t",
69 ["System::Char"] = "std::char16_t",
70 ["System::Double"] = "std::float64_t",
71 ["System::Single"] = "std::float32_t",
72 ["System::Half"] = "std::float16_t",
73 ["System::String"] = "const char*"
74 };
75
76 var fullname = symbol.GetFullName();
77 return dict.GetValueOrDefault(fullname, @default);
78 }
79
80 private static readonly string[] Primitives =
81 [
82 "System.Void",
83 "System.Boolean",
84 "System.SByte",
85 "System.Int16",
86 "System.Int32",
87 "System.Int64",
88 "System.IntPtr",
89 "System.Byte",
90 "System.UInt16",
91 "System.UInt32",
92 "System.UInt64",
93 "System.UIntPtr",
94 "System.Char",
95 "System.Double",
96 "System.Single",
97 "System.Half",
98 "System.String"
99 ];
100
101 public static bool IsPrimitives(this ISymbol symbol)
102 {
103 return Primitives.Contains(symbol.GetManagedFullName());
104 }
105
106 public static string GetManagedPackedName(this ISymbol symbol)
107 {
108 var fullname = symbol.GetManagedFullName();
109 return fullname == "System.String" ? "unsafe char*" : fullname;
110 }
111
112 public static string GetUnmanagedMethodName(this IMethodSymbol symbol)
113 {
114 return symbol.Name switch
115 {
116 ".ctor" => symbol.ContainingType.Name,
117 _ => symbol.Name
118 };
119 }
120
121 public static string GetAssemblyQualifiedName(this INamedTypeSymbol symbol)
122 {
123 var fullName = GetFullNameInternal(symbol);
124 var assemblyName = symbol.ContainingAssembly?.ToDisplayString() ?? string.Empty;
125 return $"{fullName}, {assemblyName}";
126
127 static string GetFullNameInternal(INamedTypeSymbol symbol)
128 {
129 if (symbol.ContainingType != null)
130 return $"{GetFullNameInternal(symbol.ContainingType)}+{symbol.MetadataName}";
131
132 if (!string.IsNullOrEmpty(symbol.ContainingNamespace?.ToDisplayString()))
133 return $"{symbol.ContainingNamespace.ToDisplayString()}.{symbol.MetadataName}";
134
135 return symbol.MetadataName;
136 }
137 }
138}