MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
NamespaceWriter.cs
Go to the documentation of this file.
1using Microsoft.CodeAnalysis;
3
5
6public readonly struct NamespaceWriter(INamespaceSymbol symbol) : ICodeWriter
7{
8 private static string Stub;
9
10 static NamespaceWriter()
11 {
12 Stub = Embedded.Resource("BridgeStub.h").GetAwaiter().GetResult();
13 }
14
15 public IEnumerable<WorkspaceDiagnostic> WriteTo(CodeWriter w, CodeContext ctx)
16 {
17 if (!symbol.IsTarget())
18 yield break;
19
20 if (symbol.IsGlobalNamespace)
21 {
22 w.WriteLine(Stub);
23 }
24 else
25 {
26 w.WriteLine($"namespace {symbol.Name} {{");
27 w.Indent++;
28 }
29
30 foreach (var ns in symbol.GetNamespaceMembers())
31 foreach (var diag in new NamespaceWriter(ns).WriteTo(w, ctx))
32 yield return diag;
33
34 foreach (var t in symbol.GetTypeMembers().Where(SymbolExtension.IsTarget))
35 foreach (var diag in new TypeDeclWriter(t).WriteTo(w, ctx))
36 yield return diag;
37
38 if (!symbol.IsGlobalNamespace)
39 {
40 w.Indent--;
41 w.WriteLine("}");
42 }
43 }
44}