18 public void Execute(GeneratorExecutionContext context)
20 const string attrSource =
26 namespace Lilac.Annotations
28 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Enum | AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)]
29 internal class ExportAttribute : Attribute
35 context.AddSource(
"ExportAttribute.g.cs", SourceText.From(attrSource, Encoding.UTF8));
37 var syntax = CSharpSyntaxTree.ParseText(
39 (CSharpParseOptions)context.Compilation.SyntaxTrees.First().Options);
40 var compilation = context.Compilation.AddSyntaxTrees(syntax);
42 using var swSource =
new StringWriter();
43 using var swHeader =
new StringWriter();
44 using var swManaged =
new StringWriter();
46 var source =
new IndentedTextWriter(swSource,
" ");
47 var header =
new IndentedTextWriter(swHeader,
" ");
48 var managed =
new IndentedTextWriter(swManaged,
" ");
50 foreach (var symbol
in compilation.GetSymbolsWithName(_ =>
true).Where(TypeHelper.IsExported))
52 if (symbol is not INamedTypeSymbol namedT)
54 var method = (IMethodSymbol)symbol;
56 if (!method.ContainingType.IsExported())
58 context.ReportDiagnostic(
60 Diagnostics.Type_should_be_exported_to_export_its_member,
61 method.Locations.First(),
62 method.ContainingType.ToDisplayString(),
63 method.ToDisplayString()
73 exporter.GenerateManagedCode(managed);
78 Console.WriteLine(swHeader.ToString());
79 Console.WriteLine(
"-------------------------------");
80 Console.WriteLine(swSource.ToString());
81 Console.WriteLine(
"-------------------------------");
82 Console.WriteLine(swManaged.ToString());