MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
TypeDefWriter.cs
Go to the documentation of this file.
1using Microsoft.CodeAnalysis;
4
6
7public readonly struct TypeDefWriter(INamedTypeSymbol symbol) : ICodeWriter
8{
9 public IEnumerable<WorkspaceDiagnostic> WriteTo(CodeWriter w, CodeContext ctx)
10 {
11 if (!symbol.IsTarget())
12 yield break;
13
14 if (!symbol.IsStatic)
15 {
16 var internals = symbol
17 .ContainingAssembly
18 .GlobalNamespace
19 .GetAllTypes()
20 .FirstOrDefault(type => type.GetFullName() == "MLLIF.Internal");
21 var freeDelegate = symbol
22 .ContainingAssembly
23 .GlobalNamespace
24 .GetAllTypes()
25 .FirstOrDefault(type => type.GetFullName() == "MLLIF.Internal.FreeDelegate");
26 if (internals is null || freeDelegate is null)
27 {
28 yield return BindingDiagnostic.Error(ctx.Project, symbol, "couldn't find embedded internals; is source generator added to project?");
29 }
30 else
31 {
32 w.WriteLine($"{symbol.GetFullName("::")}::~{symbol.Name}() {{");
33 w.Indent++;
34 w.WriteLine(
35 $$"""
36 static void (*__proxy)(void*) = nullptr;
37 if (!__proxy) {
38 ::s_get_function_pointer("{{internals.GetFullyQualifiedName()}}", "{{internals.MetadataName}}", "{{freeDelegate.GetFullyQualifiedName()}}", nullptr, nullptr, reinterpret_cast<void**>(&__proxy));
39 }
40 __proxy(_handle);
41 """);
42 w.Indent--;
43 w.WriteLine("}");
44 w.WriteLine();
45 }
46 }
47
48 var methods = symbol
49 .GetMembers()
50 .OfType<IMethodSymbol>()
51 .Where(SymbolExtension.IsTarget)
52 .ToArray();
53 foreach (var method in methods)
54 foreach (var diag in new MethodDefWriter(method).WriteTo(w, ctx))
55 yield return diag;
56 }
57}
class BindingDiagnostic(WorkspaceDiagnosticKind kind, string message, Project project, Document? document)