1 /**
2    Deals with D-specific translation, such as avoiding keywords
3  */
4 module dpp.translation.dlang;
5 
6 import dpp.from;
7 
8 
9 string maybeRename(in from!"clang".Cursor cursor,
10                    in from!"dpp.runtime.context".Context context)
11     @safe pure nothrow
12 {
13     return nameClashes(cursor, context) ? rename(cursor.spelling, context) : cursor.spelling;
14 }
15 
16 string maybePragma(in from!"clang".Cursor cursor,
17                    in from!"dpp.runtime.context".Context context)
18     @safe pure nothrow
19 {
20     import dpp.runtime.context: Language;
21     import std.algorithm: startsWith;
22     const isCpp = context.language == Language.Cpp;
23     if(isCpp) return pragmaMangle(cursor.mangling);
24     return nameClashes(cursor, context) ? pragmaMangle(cursor.mangling) : "";
25 }
26 
27 string rename(string spelling,
28               in from!"dpp.runtime.context".Context context)
29     @safe pure nothrow
30 {
31     do
32         spelling ~= "_";
33     while(spelling in context.aggregateDeclarations || isKeyword(spelling));
34 
35     return spelling;
36 }
37 
38 string pragmaMangle(in string mangling) @safe pure nothrow {
39     return mangling == "" ? "" : `pragma(mangle, "` ~ mangling ~ `") `;
40 }
41 
42 private bool nameClashes(in from!"clang".Cursor cursor,
43                          in from!"dpp.runtime.context".Context context)
44     @safe pure nothrow
45 {
46     return
47         cursor.spelling.isKeyword ||
48         cursor.spelling in context.aggregateDeclarations;
49 }
50 
51 bool isKeyword(string str) @safe @nogc pure nothrow {
52     switch (str) {
53         default: return false;
54         case "abstract":
55         case "alias":
56         case "align":
57         case "asm":
58         case "assert":
59         case "auto":
60 
61         case "body":
62         case "bool":
63         case "break":
64         case "byte":
65 
66         case "case":
67         case "cast":
68         case "catch":
69         case "cdouble":
70         case "cent":
71         case "cfloat":
72         case "char":
73         case "class":
74         case "const":
75         case "continue":
76         case "creal":
77 
78         case "dchar":
79         case "debug":
80         case "default":
81         case "delegate":
82         case "delete":
83         case "deprecated":
84         case "do":
85         case "double":
86 
87         case "else":
88         case "enum":
89         case "export":
90         case "extern":
91 
92         case "false":
93         case "final":
94         case "finally":
95         case "float":
96         case "for":
97         case "foreach":
98         case "foreach_reverse":
99         case "function":
100 
101         case "goto":
102 
103         case "idouble":
104         case "if":
105         case "ifloat":
106         case "import":
107         case "in":
108         case "inout":
109         case "int":
110         case "interface":
111         case "invariant":
112         case "ireal":
113         case "is":
114 
115         case "lazy":
116         case "long":
117 
118         case "macro":
119         case "mixin":
120         case "module":
121 
122         case "new":
123         case "nothrow":
124         case "null":
125 
126         case "out":
127         case "override":
128 
129         case "package":
130         case "pragma":
131         case "private":
132         case "protected":
133         case "public":
134         case "pure":
135 
136         case "real":
137         case "ref":
138         case "return":
139 
140         case "scope":
141         case "shared":
142         case "short":
143         case "static":
144         case "struct":
145         case "super":
146         case "switch":
147         case "synchronized":
148 
149         case "template":
150         case "this":
151         case "throw":
152         case "true":
153         case "try":
154         case "typedef":
155         case "typeid":
156         case "typeof":
157 
158         case "ubyte":
159         case "ucent":
160         case "uint":
161         case "ulong":
162         case "union":
163         case "unittest":
164         case "ushort":
165 
166         case "version":
167         case "void":
168         case "volatile":
169 
170         case "wchar":
171         case "while":
172         case "with":
173         case "immutable":
174         case "__gshared":
175         case "__thread":
176         case "__traits":
177 
178         case "__EOF__":
179         case "__FILE__":
180         case "__LINE__":
181         case "__DATE__":
182         case "__TIME__":
183         case "__TIMESTAMP__":
184         case "__VENDOR__":
185         case "__VERSION__":
186             return true;
187     }
188 
189     assert(0);
190 }