1 /// Author: Aziz Köksal
2 /// License: GPL3
3 /// $(Maturity very high)
4 module dil.ast.NodesEnum;
5 
6 /// Enumerates the categories of a node.
7 enum NodeCategory : ubyte
8 {
9   Undefined,
10   Declaration,
11   Statement,
12   Expression,
13   Type,
14   Parameter,
15 }
16 
17 /// A list of all class names that inherit from Node.
18 enum string[] NodeClassNames = [
19   // Declarations:
20   "IllegalDecl",
21   "CompoundDecl",
22   "ColonBlockDecl",
23   "EmptyDecl",
24   "ModuleDecl",
25   "ImportDecl",
26   "AliasDecl",
27   "AliasesDecl",
28   "AliasThisDecl",
29   "TypedefDecl",
30   "EnumDecl",
31   "EnumMemberDecl",
32   "TemplateDecl",
33   "ClassDecl",
34   "InterfaceDecl",
35   "StructDecl",
36   "UnionDecl",
37   "ConstructorDecl",
38   "StaticCtorDecl",
39   "DestructorDecl",
40   "StaticDtorDecl",
41   "FunctionDecl",
42   "VariablesDecl",
43   "InvariantDecl",
44   "UnittestDecl",
45   "DebugDecl",
46   "VersionDecl",
47   "StaticIfDecl",
48   "StaticAssertDecl",
49   "NewDecl",
50   "DeleteDecl",
51   "ProtectionDecl",
52   "StorageClassDecl",
53   "LinkageDecl",
54   "AlignDecl",
55   "PragmaDecl",
56   "MixinDecl",
57 
58   // Statements:
59   "IllegalStmt",
60   "CompoundStmt",
61   "EmptyStmt",
62   "FuncBodyStmt",
63   "ScopeStmt",
64   "LabeledStmt",
65   "ExpressionStmt",
66   "DeclarationStmt",
67   "IfStmt",
68   "WhileStmt",
69   "DoWhileStmt",
70   "ForStmt",
71   "ForeachStmt",
72   "SwitchStmt",
73   "CaseStmt",
74   "CaseRangeStmt",
75   "DefaultStmt",
76   "ContinueStmt",
77   "BreakStmt",
78   "ReturnStmt",
79   "GotoStmt",
80   "WithStmt",
81   "SynchronizedStmt",
82   "TryStmt",
83   "CatchStmt",
84   "FinallyStmt",
85   "ScopeGuardStmt",
86   "ThrowStmt",
87   "VolatileStmt",
88   "AsmBlockStmt",
89   "AsmStmt",
90   "AsmAlignStmt",
91   "IllegalAsmStmt",
92   "PragmaStmt",
93   "MixinStmt",
94   "StaticIfStmt",
95   "StaticAssertStmt",
96   "DebugStmt",
97   "VersionStmt",
98 
99   // Expressions:
100   "IllegalExpr",
101   "CondExpr",
102   "CommaExpr",
103   "OrOrExpr",
104   "AndAndExpr",
105   "OrExpr",
106   "XorExpr",
107   "AndExpr",
108   "EqualExpr",
109   "IdentityExpr",
110   "RelExpr",
111   "InExpr",
112   "LShiftExpr",
113   "RShiftExpr",
114   "URShiftExpr",
115   "PlusExpr",
116   "MinusExpr",
117   "CatExpr",
118   "MulExpr",
119   "DivExpr",
120   "ModExpr",
121   "PowExpr", // D2
122   "RangeExpr",
123   "AssignExpr",
124   "LShiftAssignExpr",
125   "RShiftAssignExpr",
126   "URShiftAssignExpr",
127   "OrAssignExpr",
128   "AndAssignExpr",
129   "PlusAssignExpr",
130   "MinusAssignExpr",
131   "DivAssignExpr",
132   "MulAssignExpr",
133   "ModAssignExpr",
134   "XorAssignExpr",
135   "CatAssignExpr",
136   "PowAssignExpr", // D2
137   "AddressExpr",
138   "PreIncrExpr",
139   "PreDecrExpr",
140   "PostIncrExpr",
141   "PostDecrExpr",
142   "DerefExpr",
143   "SignExpr",
144   "NotExpr",
145   "CompExpr",
146   "CallExpr",
147   "NewExpr",
148   "NewClassExpr",
149   "DeleteExpr",
150   "CastExpr",
151   "IndexExpr",
152   "SliceExpr",
153   "ModuleScopeExpr",
154   "IdentifierExpr",
155   "SpecialTokenExpr",
156   "TmplInstanceExpr",
157   "ThisExpr",
158   "SuperExpr",
159   "NullExpr",
160   "DollarExpr",
161   "BoolExpr",
162   "IntExpr",
163   "FloatExpr",
164   "ComplexExpr",
165   "CharExpr",
166   "StringExpr",
167   "ArrayLiteralExpr",
168   "AArrayLiteralExpr",
169   "AssertExpr",
170   "MixinExpr",
171   "ImportExpr",
172   "TypeExpr",
173   "TypeofExpr",
174   "TypeDotIdExpr",
175   "TypeidExpr",
176   "IsExpr",
177   "ParenExpr",
178   "FuncLiteralExpr",
179   "LambdaExpr", // D2.0
180   "TraitsExpr", // D2.0
181   "VoidInitExpr",
182   "ArrayInitExpr",
183   "StructInitExpr",
184   "AsmTypeExpr",
185   "AsmOffsetExpr",
186   "AsmSegExpr",
187   "AsmPostBracketExpr",
188   "AsmBracketExpr",
189   "AsmLocalSizeExpr",
190   "AsmRegisterExpr",
191 
192   // Types:
193   "IllegalType",
194   "IntegralType",
195   "ModuleScopeType",
196   "IdentifierType",
197   "TypeofType",
198   "TmplInstanceType",
199   "PointerType",
200   "ArrayType",
201   "FunctionType",
202   "DelegateType",
203   "BaseClassType",
204   "ModifierType", // D2.0
205 
206   // Parameters:
207   "Parameter",
208   "Parameters",
209   "TemplateAliasParam",
210   "TemplateTypeParam",
211   "TemplateThisParam", // D2.0
212   "TemplateValueParam",
213   "TemplateTupleParam",
214   "TemplateParameters",
215   "TemplateArguments",
216 ];
217 
218 /// Generates the members of enum NodeKind.
219 char[] generateNodeKindMembers()
220 {
221   char[] text;
222   foreach (className; NodeClassNames)
223     text ~= className ~ ",";
224   return text;
225 }
226 // pragma(msg, generateNodeKindMembers());
227 
228 version(DDoc)
229   /// The node kind identifies every class that inherits from Node.
230   enum NodeKind : ushort;
231 else
232 mixin(
233   "enum NodeKind : ushort"
234   "{"
235     ~ generateNodeKindMembers ~
236   "}"
237 );
238 
239 bool isDeclaration(NodeKind k)
240 {
241   return NodeKind.CompoundDecl <= k && k <= NodeKind.MixinDecl;
242 }
243 
244 bool isStatement(NodeKind k)
245 {
246   return NodeKind.CompoundStmt <= k && k <= NodeKind.VersionStmt;
247 }
248 
249 bool isExpression(NodeKind k)
250 {
251   return NodeKind.IllegalExpr <= k && k <= NodeKind.AsmRegisterExpr;
252 }
253 
254 bool isType(NodeKind k)
255 {
256   return NodeKind.IllegalType <= k && k <= NodeKind.ModifierType;
257 }
258 
259 bool isParameter(NodeKind k)
260 {
261   return NodeKind.Parameter <= k && k <= NodeKind.TemplateArguments;
262 }
263 
264 /// Returns the category of a node kind.
265 NodeCategory category(NodeKind k)
266 {
267   if (k.isDeclaration)
268     return NodeCategory.Declaration;
269   if (k.isStatement)
270     return NodeCategory.Statement;
271   if (k.isExpression)
272     return NodeCategory.Expression;
273   if (k.isType)
274     return NodeCategory.Type;
275   if (k.isParameter)
276     return NodeCategory.Parameter;
277   return NodeCategory.Undefined;
278 }