1 module bamboo.codegen.frontend;
2 
3 import bamboo.codegen;
4 
5 /// Generates D source code from a given module.  It will optionally declare a D module.
6 /// This utility function also imports codegen helpers and privately aliases dclass primitives.
7 /// This function is suitable for creating a self-contained file.
8 /// See_Also: $(D generateModule).
9 string generateFile(Module file, string moduleName = "",
10         string distributedObjectModule = "libastrond", string baseType = "",
11         bool generateStubs = true)
12 {
13     string format;
14 
15     if (moduleName.length == 0)
16     {
17         moduleName = file.symbol;
18     }
19 
20     format ~= "// This code was generated by a tool.\n";
21     if (moduleName.length > 0)
22     {
23         format ~= "module ";
24         format ~= moduleName;
25         format ~= ";";
26     }
27     format ~= "import std.exception;";
28     format ~= "import bamboo.codegen;";
29     format ~= "import " ~ distributedObjectModule ~ ";";
30 
31     foreach (imprt; file.importDeclarations)
32     {
33         if (imprt.packageName == moduleName)
34         {
35             continue;
36         }
37         format ~= "import " ~ imprt.packageName ~ " : ";
38         foreach (type; imprt.symbols[0 .. $ - 1])
39         {
40             format ~= type ~ ", ";
41         }
42         format ~= imprt.symbols[$ - 1] ~ ";";
43     }
44 
45     format ~= "private { mixin(Primitives); }";
46     format ~= generateModule(file, baseType, generateStubs);
47 
48     return format;
49 }
50 
51 /// Ditto.
52 string generateFile(string dcFile, string moduleName = "",
53         string distributedObjectModule = "libastrond", string baseType = "",
54         bool generateStubs = true)
55 {
56     import bamboo.astgen : parseModule;
57 
58     Module file = parseModule(dcFile);
59     return generateFile(file, moduleName, distributedObjectModule, baseType, generateStubs);
60 }
61 
62 /// Generates D source code from a given module. This function is 
63 /// suitable for mixing into a larger D source file.
64 /// See_Also: $(D generateFile).
65 string generateModule(Module file, string baseType = "", bool generateStubs = true)
66 {
67     string format;
68 
69     format ~= "import std.typecons;";
70 
71     if (file.keywords.length)
72     {
73         format ~= " enum {";
74         foreach (id, keyword; file.keywords)
75         {
76             format ~= q{%1$s = 1 << %2$s,}.format(keyword.symbol, id + 1);
77         }
78         format ~= "} ";
79     }
80 
81     foreach (type; file.typesById)
82     {
83         if (auto cls = cast(ClassDeclaration) type)
84         {
85             format ~= generateClass(cls, baseType, generateStubs);
86         }
87         else if (auto strct = cast(StructDeclaration) type)
88         {
89             format ~= generateStruct(strct);
90         }
91     }
92 
93     return format;
94 }
95 
96 /// Ditto.
97 string generateModule(string dcFile, string baseType = "", bool generateStubs = true)
98 {
99     import bamboo.astgen : parseModule;
100 
101     Module file = parseModule(dcFile);
102     return generateModule(file, baseType, generateStubs);
103 }