1using Microsoft.CodeAnalysis;
5public static class TypeHelper
7 public static bool IsExported(
this ISymbol symbol)
11 .Any(attr => attr.AttributeClass?.ToDisplayString() ==
"Lilac.Annotations.ExportAttribute");
14 public static string GetFullName(
this ISymbol symbol)
16 return symbol.ContainingNamespace is
null
18 : $
"{symbol.ContainingNamespace.ToDisplayString().Replace(".
", "::
")}::{symbol.Name}";
21 public static string GetManagedFullName(
this ISymbol symbol)
23 return $
"{symbol.ContainingNamespace.ToDisplayString()}.{symbol.Name}";
26 public static string GetNativeName(
this ISymbol symbol)
28 var dict =
new Dictionary<string, string>
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"
49 var fullname = symbol.GetFullName();
50 return dict.GetValueOrDefault(fullname, fullname);
53 public static string GetUnmanagedPackedName(
this ISymbol symbol,
string @
default =
"void*")
55 var dict =
new Dictionary<string, string>
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*"
76 var fullname = symbol.GetFullName();
77 return dict.GetValueOrDefault(fullname, @
default);
80 private static readonly
string[] Primitives =
101 public static bool IsPrimitives(
this ISymbol symbol)
103 return Primitives.Contains(symbol.GetManagedFullName());
106 public static string GetManagedPackedName(
this ISymbol symbol)
108 var fullname = symbol.GetManagedFullName();
109 return fullname ==
"System.String" ?
"unsafe char*" : fullname;
112 public static string GetUnmanagedMethodName(
this IMethodSymbol symbol)
114 return symbol.Name
switch
116 ".ctor" => symbol.ContainingType.Name,
121 public static string GetAssemblyQualifiedName(
this INamedTypeSymbol symbol)
123 var fullName = GetFullNameInternal(symbol);
124 var assemblyName = symbol.ContainingAssembly?.ToDisplayString() ??
string.Empty;
125 return $
"{fullName}, {assemblyName}";
127 static string GetFullNameInternal(INamedTypeSymbol symbol)
129 if (symbol.ContainingType !=
null)
130 return $
"{GetFullNameInternal(symbol.ContainingType)}+{symbol.MetadataName}";
132 if (!
string.IsNullOrEmpty(symbol.ContainingNamespace?.ToDisplayString()))
133 return $
"{symbol.ContainingNamespace.ToDisplayString()}.{symbol.MetadataName}";
135 return symbol.MetadataName;