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