LILAC
Language to language Interop LAyer Compiler
Loading...
Searching...
No Matches
TypeExporter.cs
Go to the documentation of this file.
1using System.CodeDom.Compiler;
2using System.Xml.Linq;
3using Microsoft.CodeAnalysis;
4
6
7public class TypeExporter(INamedTypeSymbol symbol) : IExporter
8{
9 public void GenerateUnmanagedCode(IndentedTextWriter source, IndentedTextWriter header)
10 {
11 // TODO : Handle pinned-memory leak
12 // TODO : Impl implicit conversion from void*
13
14 header.WriteLine($"namespace {symbol.ContainingNamespace.GetFullName()}");
15 header.WriteLine("{");
16 header.Indent++;
17
18 header.WriteLine($"class {symbol.Name}");
19 header.WriteLine("{");
20 header.Indent++;
21
22 header.WriteLine("void* __this;");
23 header.WriteLine();
24
25 header.Indent--;
26 header.WriteLine("public:");
27 header.Indent++;
28
29 var methods = symbol.GetMembers().OfType<IMethodSymbol>().Where(method => method.IsExported()).ToArray();
30 for (var i = 0; i < methods.Length; i++)
31 {
32 var method = methods[i];
33 if (i > 0)
34 {
35 source.WriteLine();
36 header.WriteLine();
37 header.WriteLine();
38 }
39
40 new MethodExporter(method).GenerateUnmanagedCode(source, header);
41 }
42
43 header.Indent--;
44 header.WriteLine("};");
45
46 header.Indent--;
47 header.WriteLine("}");
48 }
49
50 public void GenerateManagedCode(IndentedTextWriter builder)
51 {
52 builder.WriteLine($"namespace {symbol.ContainingNamespace.GetManagedFullName()}");
53 builder.WriteLine("{");
54 builder.Indent++;
55
56 var access = symbol.DeclaredAccessibility switch
57 {
58 Accessibility.Private => "private",
59 Accessibility.ProtectedAndInternal => "private protected",
60 Accessibility.Protected => "protected",
61 Accessibility.Internal => "internal",
62 Accessibility.ProtectedOrInternal => "protected internal",
63 Accessibility.Public => "public",
64 _ => throw new ArgumentOutOfRangeException()
65 };
66
67 builder.WriteLine($"{access} partial class {symbol.Name}");
68 builder.WriteLine("{");
69 builder.Indent++;
70
71 var methods = symbol.GetMembers().OfType<IMethodSymbol>().Where(method => method.IsExported()).ToArray();
72 for (var i = 0; i < methods.Length; i++)
73 {
74 var method = methods[i];
75 if (i > 0)
76 {
77 builder.WriteLine();
78 builder.WriteLine();
79 }
80 IExporter.Create(method).GenerateManagedCode(builder);
81 }
82
83 builder.Indent--;
84 builder.WriteLine("}");
85
86 builder.Indent--;
87 builder.WriteLine("}");
88 }
89}