1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // ParseNode.h -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Sat May 16 13:28:16 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Aug 9 12:28:12 2016 |
---|
13 | // Update Count : 389 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef PARSENODE_H |
---|
17 | #define PARSENODE_H |
---|
18 | |
---|
19 | #include <string> |
---|
20 | #include <list> |
---|
21 | #include <iterator> |
---|
22 | #include <memory> |
---|
23 | |
---|
24 | #include "Common/utility.h" |
---|
25 | #include "Parser/LinkageSpec.h" |
---|
26 | #include "SynTree/Type.h" |
---|
27 | #include "SynTree/Expression.h" |
---|
28 | //#include "SynTree/Declaration.h" |
---|
29 | #include "Common/UniqueName.h" |
---|
30 | #include "SynTree/Label.h" |
---|
31 | |
---|
32 | class StatementNode; |
---|
33 | class CompoundStmtNode; |
---|
34 | class DeclarationNode; |
---|
35 | class ExpressionNode; |
---|
36 | class InitializerNode; |
---|
37 | |
---|
38 | // Builder |
---|
39 | class ParseNode { |
---|
40 | public: |
---|
41 | ParseNode(); |
---|
42 | ParseNode( const std::string * ); |
---|
43 | ParseNode( const std::string & ); // for copy constructing subclasses |
---|
44 | virtual ~ParseNode(); |
---|
45 | |
---|
46 | ParseNode *get_link() const { return next; } |
---|
47 | ParseNode *get_last(); |
---|
48 | ParseNode *set_link( ParseNode * ); |
---|
49 | void set_next( ParseNode *newlink ) { next = newlink; } |
---|
50 | |
---|
51 | virtual ParseNode *clone() const { return 0; }; |
---|
52 | |
---|
53 | const std::string &get_name() const { return name; } |
---|
54 | void set_name( const std::string &newValue ) { name = newValue; } |
---|
55 | |
---|
56 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
57 | virtual void printList( std::ostream &os, int indent = 0 ) const; |
---|
58 | |
---|
59 | ParseNode &operator,( ParseNode &); |
---|
60 | protected: |
---|
61 | std::string name; |
---|
62 | static int indent_by; |
---|
63 | ParseNode *next; |
---|
64 | }; |
---|
65 | |
---|
66 | ParseNode *mkList( ParseNode & ); |
---|
67 | |
---|
68 | //############################################################################## |
---|
69 | |
---|
70 | class InitializerNode : public ParseNode { |
---|
71 | public: |
---|
72 | InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode *des = 0 ); |
---|
73 | InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 ); |
---|
74 | ~InitializerNode(); |
---|
75 | |
---|
76 | ExpressionNode *get_expression() const { return expr; } |
---|
77 | |
---|
78 | InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; } |
---|
79 | ExpressionNode *get_designators() const { return designator; } |
---|
80 | |
---|
81 | InitializerNode *set_maybeConstructed( bool value ) { maybeConstructed = value; return this; } |
---|
82 | bool get_maybeConstructed() const { return maybeConstructed; } |
---|
83 | |
---|
84 | InitializerNode *next_init() const { return kids; } |
---|
85 | |
---|
86 | void print( std::ostream &os, int indent = 0 ) const; |
---|
87 | void printOneLine( std::ostream & ) const; |
---|
88 | |
---|
89 | virtual Initializer *build() const; |
---|
90 | private: |
---|
91 | ExpressionNode *expr; |
---|
92 | bool aggregate; |
---|
93 | ExpressionNode *designator; // may be list |
---|
94 | InitializerNode *kids; |
---|
95 | bool maybeConstructed; |
---|
96 | }; |
---|
97 | |
---|
98 | //############################################################################## |
---|
99 | |
---|
100 | class ExpressionNode : public ParseNode { |
---|
101 | public: |
---|
102 | ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {} |
---|
103 | ExpressionNode( Expression * expr, const std::string *name ) : ParseNode( name ), expr( expr ) {} |
---|
104 | ExpressionNode( const ExpressionNode &other ); |
---|
105 | virtual ~ExpressionNode() {} |
---|
106 | |
---|
107 | virtual ExpressionNode *clone() const { return 0; } |
---|
108 | |
---|
109 | bool get_extension() const { return extension; } |
---|
110 | ExpressionNode *set_extension( bool exten ) { extension = exten; return this; } |
---|
111 | |
---|
112 | virtual void print( std::ostream &os, int indent = 0 ) const {} |
---|
113 | virtual void printOneLine( std::ostream &os, int indent = 0 ) const {} |
---|
114 | |
---|
115 | virtual Expression *build() const { return expr; } |
---|
116 | private: |
---|
117 | bool extension = false; |
---|
118 | Expression *expr; |
---|
119 | }; |
---|
120 | |
---|
121 | template< typename T > |
---|
122 | struct maybeBuild_t<Expression, T> { |
---|
123 | static inline Expression * doit( const T *orig ) { |
---|
124 | if ( orig ) { |
---|
125 | Expression *p = orig->build(); |
---|
126 | p->set_extension( orig->get_extension() ); |
---|
127 | return p; |
---|
128 | } else { |
---|
129 | return 0; |
---|
130 | } // if |
---|
131 | } |
---|
132 | }; |
---|
133 | |
---|
134 | //############################################################################## |
---|
135 | |
---|
136 | // NullExprNode is used in tuples as a place-holder where a tuple component is omitted e.g., [ 2, , 3 ] |
---|
137 | // class NullExprNode : public ExpressionNode { |
---|
138 | // public: |
---|
139 | // NullExprNode() {} |
---|
140 | |
---|
141 | // virtual NullExprNode *clone() const { assert( false ); } |
---|
142 | |
---|
143 | // virtual void print( std::ostream &os, int indent = 0) const {} |
---|
144 | // virtual void printOneLine( std::ostream &os, int indent = 0) const {} |
---|
145 | |
---|
146 | // virtual Expression *build() const { assert( false ); return 0; } |
---|
147 | // }; |
---|
148 | |
---|
149 | //############################################################################## |
---|
150 | |
---|
151 | // class ConstantNode : public ExpressionNode { |
---|
152 | // public: |
---|
153 | // ConstantNode( ConstantExpr *expr ) : expr( expr ) {} |
---|
154 | // ConstantNode( const ConstantNode &other ) : expr( other.expr->clone() ) {} |
---|
155 | // virtual ~ConstantNode() {} |
---|
156 | |
---|
157 | // virtual ConstantNode *clone() const { assert( false ); return new ConstantNode( *this ); } |
---|
158 | |
---|
159 | // ConstantExpr *get_expr() const { return expr; } |
---|
160 | |
---|
161 | // virtual void print( std::ostream &os, int indent = 0 ) const {} |
---|
162 | // virtual void printOneLine( std::ostream &os, int indent = 0 ) const {} |
---|
163 | |
---|
164 | // Expression *build() const { return expr; } |
---|
165 | // private: |
---|
166 | // ConstantExpr *expr; |
---|
167 | // }; |
---|
168 | |
---|
169 | Expression *build_constantInteger( std::string &str ); |
---|
170 | Expression *build_constantFloat( std::string &str ); |
---|
171 | Expression *build_constantChar( std::string &str ); |
---|
172 | ConstantExpr *build_constantStr( std::string &str ); |
---|
173 | |
---|
174 | //############################################################################## |
---|
175 | |
---|
176 | // class VarRefNode : public ExpressionNode { |
---|
177 | // public: |
---|
178 | // VarRefNode( const std::string *, bool isLabel = false ); |
---|
179 | // VarRefNode( const VarRefNode &other ); |
---|
180 | |
---|
181 | // virtual Expression *build() const ; |
---|
182 | |
---|
183 | // virtual VarRefNode *clone() const { return new VarRefNode( *this ); } |
---|
184 | |
---|
185 | // virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
186 | // virtual void printOneLine( std::ostream &os, int indent = 0 ) const; |
---|
187 | // private: |
---|
188 | // bool isLabel; |
---|
189 | // }; |
---|
190 | |
---|
191 | NameExpr *build_varref( const std::string *name, bool labelp = false ); |
---|
192 | |
---|
193 | //############################################################################## |
---|
194 | |
---|
195 | // class DesignatorNode : public ExpressionNode { |
---|
196 | // public: |
---|
197 | // DesignatorNode( ExpressionNode *expr, bool isArrayIndex = false ); |
---|
198 | // DesignatorNode( const DesignatorNode &other ); |
---|
199 | |
---|
200 | // virtual Expression *build() const ; |
---|
201 | // virtual DesignatorNode *clone() const { return new DesignatorNode( *this ); } |
---|
202 | |
---|
203 | // virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
204 | // virtual void printOneLine( std::ostream &os, int indent = 0 ) const; |
---|
205 | // private: |
---|
206 | // bool isArrayIndex; |
---|
207 | // }; |
---|
208 | |
---|
209 | //############################################################################## |
---|
210 | |
---|
211 | // class TypeValueNode : public ExpressionNode { |
---|
212 | // public: |
---|
213 | // TypeValueNode( DeclarationNode * ); |
---|
214 | // TypeValueNode( const TypeValueNode &other ); |
---|
215 | |
---|
216 | // DeclarationNode *get_decl() const { return decl; } |
---|
217 | |
---|
218 | // virtual Expression *build() const ; |
---|
219 | |
---|
220 | // virtual TypeValueNode *clone() const { return new TypeValueNode( *this ); } |
---|
221 | |
---|
222 | // virtual void print( std::ostream &os, int indent = 0) const; |
---|
223 | // virtual void printOneLine( std::ostream &os, int indent = 0) const; |
---|
224 | // private: |
---|
225 | // DeclarationNode *decl; |
---|
226 | // }; |
---|
227 | |
---|
228 | Expression *build_typevalue( DeclarationNode *decl ); |
---|
229 | |
---|
230 | //############################################################################## |
---|
231 | |
---|
232 | // class CompositeExprNode : public ExpressionNode { |
---|
233 | // public: |
---|
234 | // CompositeExprNode( Expression *expr ) : expr( expr ) {} |
---|
235 | // CompositeExprNode( const CompositeExprNode &other ) : expr( other.expr->clone() ) {} |
---|
236 | // virtual ~CompositeExprNode() {} |
---|
237 | |
---|
238 | // CompositeExprNode *clone() const { assert( false ); return new CompositeExprNode( *this ); } |
---|
239 | |
---|
240 | // Expression *build() const { return expr; } |
---|
241 | |
---|
242 | // void print( std::ostream &os, int indent = 0 ) const {} |
---|
243 | // void printOneLine( std::ostream &os, int indent = 0 ) const {} |
---|
244 | // private: |
---|
245 | // Expression *expr; |
---|
246 | // }; |
---|
247 | |
---|
248 | enum class OperKinds { |
---|
249 | // diadic |
---|
250 | SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And, |
---|
251 | BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq, |
---|
252 | Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn, |
---|
253 | Index, Range, |
---|
254 | // monadic |
---|
255 | UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress, |
---|
256 | Ctor, Dtor, |
---|
257 | }; |
---|
258 | |
---|
259 | Expression *build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node ); |
---|
260 | Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member ); |
---|
261 | Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member ); |
---|
262 | Expression *build_addressOf( ExpressionNode *expr_node ); |
---|
263 | Expression *build_sizeOfexpr( ExpressionNode *expr_node ); |
---|
264 | Expression *build_sizeOftype( DeclarationNode *decl_node ); |
---|
265 | Expression *build_alignOfexpr( ExpressionNode *expr_node ); |
---|
266 | Expression *build_alignOftype( DeclarationNode *decl_node ); |
---|
267 | Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ); |
---|
268 | Expression *build_and( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ); |
---|
269 | Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ); |
---|
270 | Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ); |
---|
271 | Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ); |
---|
272 | Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ); |
---|
273 | Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ); |
---|
274 | Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ); |
---|
275 | Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ); |
---|
276 | Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ); |
---|
277 | Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ); |
---|
278 | Expression *build_tuple( ExpressionNode * expr_node = 0 ); |
---|
279 | Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ); |
---|
280 | Expression *build_range( ExpressionNode * low, ExpressionNode *high ); |
---|
281 | |
---|
282 | //############################################################################## |
---|
283 | |
---|
284 | // class AsmExprNode : public ExpressionNode { |
---|
285 | // public: |
---|
286 | // AsmExprNode(); |
---|
287 | // AsmExprNode( ExpressionNode *inout, ConstantNode *constraint, ExpressionNode *operand ) : inout( inout ), constraint( constraint ), operand( operand ) {} |
---|
288 | // virtual ~AsmExprNode() { delete inout; delete constraint; delete operand; } |
---|
289 | |
---|
290 | // virtual AsmExprNode *clone() const { return new AsmExprNode( *this ); } |
---|
291 | // virtual Expression *build() const; |
---|
292 | |
---|
293 | // virtual void print( std::ostream &os, int indent = 0) const; |
---|
294 | // virtual void printOneLine( std::ostream &os, int indent = 0) const; |
---|
295 | |
---|
296 | // ExpressionNode *get_inout() const { return inout; }; |
---|
297 | // void set_inout( ExpressionNode *newValue ) { inout = newValue; } |
---|
298 | |
---|
299 | // ConstantNode *get_constraint() const { return constraint; }; |
---|
300 | // void set_constraint( ConstantNode *newValue ) { constraint = newValue; } |
---|
301 | |
---|
302 | // ExpressionNode *get_operand() const { return operand; }; |
---|
303 | // void set_operand( ExpressionNode *newValue ) { operand = newValue; } |
---|
304 | // private: |
---|
305 | // ExpressionNode *inout; |
---|
306 | // ConstantNode *constraint; |
---|
307 | // ExpressionNode *operand; |
---|
308 | // }; |
---|
309 | |
---|
310 | Expression *build_asm( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ); |
---|
311 | |
---|
312 | //############################################################################## |
---|
313 | |
---|
314 | class LabelNode : public ExpressionNode { |
---|
315 | public: |
---|
316 | virtual Expression *build() const { return NULL; } |
---|
317 | virtual LabelNode *clone() const { assert( false ); return new LabelNode( *this ); } |
---|
318 | |
---|
319 | virtual void print( std::ostream &os, int indent = 0) const; |
---|
320 | virtual void printOneLine( std::ostream &os, int indent = 0) const; |
---|
321 | |
---|
322 | const std::list< Label > &get_labels() const { return labels; }; |
---|
323 | void append_label( std::string * label ) { labels.push_back( *label ); delete label; } |
---|
324 | private: |
---|
325 | std::list< Label > labels; |
---|
326 | }; |
---|
327 | |
---|
328 | //############################################################################## |
---|
329 | |
---|
330 | class ForCtlExprNode : public ExpressionNode { |
---|
331 | public: |
---|
332 | ForCtlExprNode( ParseNode *, ExpressionNode *, ExpressionNode * ) throw ( SemanticError ); |
---|
333 | ForCtlExprNode( const ForCtlExprNode &other ); |
---|
334 | ~ForCtlExprNode(); |
---|
335 | |
---|
336 | StatementNode *get_init() const { return init; } |
---|
337 | ExpressionNode *get_condition() const { return condition; } |
---|
338 | ExpressionNode *get_change() const { return change; } |
---|
339 | |
---|
340 | virtual ForCtlExprNode *clone() const { return new ForCtlExprNode( *this ); } |
---|
341 | virtual Expression *build() const; |
---|
342 | |
---|
343 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
344 | virtual void printOneLine( std::ostream &os, int indent = 0 ) const; |
---|
345 | private: |
---|
346 | StatementNode *init; |
---|
347 | ExpressionNode *condition; |
---|
348 | ExpressionNode *change; |
---|
349 | }; |
---|
350 | |
---|
351 | //############################################################################## |
---|
352 | |
---|
353 | // class ValofExprNode : public ExpressionNode { |
---|
354 | // public: |
---|
355 | // ValofExprNode(); |
---|
356 | // ValofExprNode( StatementNode *s = 0 ); |
---|
357 | // ValofExprNode( const ValofExprNode &other ); |
---|
358 | // ~ValofExprNode(); |
---|
359 | |
---|
360 | // virtual ValofExprNode *clone() const { return new ValofExprNode( *this ); } |
---|
361 | |
---|
362 | // StatementNode *get_body() const { return body; } |
---|
363 | // void print( std::ostream &os, int indent = 0 ) const; |
---|
364 | // void printOneLine( std::ostream &os, int indent = 0 ) const; |
---|
365 | // Expression *build() const; |
---|
366 | |
---|
367 | // private: |
---|
368 | // StatementNode *body; |
---|
369 | // }; |
---|
370 | |
---|
371 | Expression *build_valexpr( StatementNode *s ); |
---|
372 | |
---|
373 | //############################################################################## |
---|
374 | |
---|
375 | // class CompoundLiteralNode : public ExpressionNode { |
---|
376 | // public: |
---|
377 | // CompoundLiteralNode( DeclarationNode *type, InitializerNode *kids ); |
---|
378 | // CompoundLiteralNode( const CompoundLiteralNode &type ); |
---|
379 | // ~CompoundLiteralNode(); |
---|
380 | |
---|
381 | // virtual CompoundLiteralNode *clone() const; |
---|
382 | |
---|
383 | // DeclarationNode *get_type() const { return type; } |
---|
384 | // CompoundLiteralNode *set_type( DeclarationNode *t ) { type = t; return this; } |
---|
385 | |
---|
386 | // InitializerNode *get_initializer() const { return kids; } |
---|
387 | // CompoundLiteralNode *set_initializer( InitializerNode *k ) { kids = k; return this; } |
---|
388 | |
---|
389 | // void print( std::ostream &os, int indent = 0 ) const; |
---|
390 | // void printOneLine( std::ostream &os, int indent = 0 ) const; |
---|
391 | |
---|
392 | // virtual Expression *build() const; |
---|
393 | // private: |
---|
394 | // DeclarationNode *type; |
---|
395 | // InitializerNode *kids; |
---|
396 | // }; |
---|
397 | |
---|
398 | Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ); |
---|
399 | |
---|
400 | //############################################################################## |
---|
401 | |
---|
402 | class TypeData; |
---|
403 | |
---|
404 | class DeclarationNode : public ParseNode { |
---|
405 | public: |
---|
406 | enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic }; |
---|
407 | enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, }; |
---|
408 | enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary }; |
---|
409 | enum Modifier { Signed, Unsigned, Short, Long }; |
---|
410 | enum Aggregate { Struct, Union, Trait }; |
---|
411 | enum TypeClass { Type, Dtype, Ftype }; |
---|
412 | enum BuiltinType { Valist }; |
---|
413 | |
---|
414 | static const char *storageName[]; |
---|
415 | static const char *qualifierName[]; |
---|
416 | static const char *basicTypeName[]; |
---|
417 | static const char *modifierName[]; |
---|
418 | static const char *aggregateName[]; |
---|
419 | static const char *typeClassName[]; |
---|
420 | static const char *builtinTypeName[]; |
---|
421 | |
---|
422 | static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false ); |
---|
423 | static DeclarationNode *newQualifier( Qualifier ); |
---|
424 | static DeclarationNode *newStorageClass( StorageClass ); |
---|
425 | static DeclarationNode *newBasicType( BasicType ); |
---|
426 | static DeclarationNode *newModifier( Modifier ); |
---|
427 | static DeclarationNode *newForall( DeclarationNode *); |
---|
428 | static DeclarationNode *newFromTypedef( std::string *); |
---|
429 | static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ); |
---|
430 | static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants ); |
---|
431 | static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant ); |
---|
432 | static DeclarationNode *newName( std::string *); |
---|
433 | static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params ); |
---|
434 | static DeclarationNode *newTypeParam( TypeClass, std::string *); |
---|
435 | static DeclarationNode *newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ); |
---|
436 | static DeclarationNode *newTraitUse( std::string *name, ExpressionNode *params ); |
---|
437 | static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams ); |
---|
438 | static DeclarationNode *newPointer( DeclarationNode *qualifiers ); |
---|
439 | static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ); |
---|
440 | static DeclarationNode *newVarArray( DeclarationNode *qualifiers ); |
---|
441 | static DeclarationNode *newBitfield( ExpressionNode *size ); |
---|
442 | static DeclarationNode *newTuple( DeclarationNode *members ); |
---|
443 | static DeclarationNode *newTypeof( ExpressionNode *expr ); |
---|
444 | static DeclarationNode *newAttr( std::string *, ExpressionNode *expr ); |
---|
445 | static DeclarationNode *newAttr( std::string *, DeclarationNode *type ); |
---|
446 | static DeclarationNode *newBuiltinType( BuiltinType ); |
---|
447 | |
---|
448 | DeclarationNode *addQualifiers( DeclarationNode *); |
---|
449 | DeclarationNode *copyStorageClasses( DeclarationNode *); |
---|
450 | DeclarationNode *addType( DeclarationNode *); |
---|
451 | DeclarationNode *addTypedef(); |
---|
452 | DeclarationNode *addAssertions( DeclarationNode *); |
---|
453 | DeclarationNode *addName( std::string *); |
---|
454 | DeclarationNode *addBitfield( ExpressionNode *size ); |
---|
455 | DeclarationNode *addVarArgs(); |
---|
456 | DeclarationNode *addFunctionBody( StatementNode *body ); |
---|
457 | DeclarationNode *addOldDeclList( DeclarationNode *list ); |
---|
458 | DeclarationNode *addPointer( DeclarationNode *qualifiers ); |
---|
459 | DeclarationNode *addArray( DeclarationNode *array ); |
---|
460 | DeclarationNode *addNewPointer( DeclarationNode *pointer ); |
---|
461 | DeclarationNode *addNewArray( DeclarationNode *array ); |
---|
462 | DeclarationNode *addParamList( DeclarationNode *list ); |
---|
463 | DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions |
---|
464 | DeclarationNode *addInitializer( InitializerNode *init ); |
---|
465 | |
---|
466 | DeclarationNode *cloneType( std::string *newName ); |
---|
467 | DeclarationNode *cloneType( DeclarationNode *existing ); |
---|
468 | DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); } |
---|
469 | DeclarationNode *cloneBaseType( std::string *newName ); |
---|
470 | DeclarationNode *cloneBaseType( DeclarationNode *newdecl ); |
---|
471 | |
---|
472 | DeclarationNode *appendList( DeclarationNode * ); |
---|
473 | |
---|
474 | DeclarationNode *clone() const; |
---|
475 | void print( std::ostream &os, int indent = 0 ) const; |
---|
476 | void printList( std::ostream &os, int indent = 0 ) const; |
---|
477 | |
---|
478 | Declaration *build() const; |
---|
479 | ::Type *buildType() const; |
---|
480 | |
---|
481 | bool get_hasEllipsis() const; |
---|
482 | const std::string &get_name() const { return name; } |
---|
483 | LinkageSpec::Type get_linkage() const { return linkage; } |
---|
484 | DeclarationNode *extractAggregate() const; |
---|
485 | ExpressionNode *get_enumeratorValue() const { return enumeratorValue; } |
---|
486 | |
---|
487 | bool get_extension() const { return extension; } |
---|
488 | DeclarationNode *set_extension( bool exten ) { extension = exten; return this; } |
---|
489 | |
---|
490 | DeclarationNode(); |
---|
491 | ~DeclarationNode(); |
---|
492 | private: |
---|
493 | StorageClass buildStorageClass() const; |
---|
494 | bool buildFuncSpecifier( StorageClass key ) const; |
---|
495 | |
---|
496 | TypeData *type; |
---|
497 | std::string name; |
---|
498 | std::list< StorageClass > storageClasses; |
---|
499 | std::list< std::string > attributes; |
---|
500 | ExpressionNode *bitfieldWidth; |
---|
501 | ExpressionNode *enumeratorValue; |
---|
502 | InitializerNode *initializer; |
---|
503 | bool hasEllipsis; |
---|
504 | LinkageSpec::Type linkage; |
---|
505 | bool extension = false; |
---|
506 | |
---|
507 | static UniqueName anonymous; |
---|
508 | }; // DeclarationNode |
---|
509 | |
---|
510 | Type *buildType( TypeData *type ); |
---|
511 | |
---|
512 | //############################################################################## |
---|
513 | |
---|
514 | class StatementNode : public ParseNode { |
---|
515 | public: |
---|
516 | enum Type { Exp, If, Switch, Case, Default, Choose, Fallthru, |
---|
517 | While, Do, For, |
---|
518 | Goto, Continue, Break, Return, Throw, |
---|
519 | Try, Catch, Finally, Asm, |
---|
520 | Decl |
---|
521 | }; |
---|
522 | |
---|
523 | StatementNode(); |
---|
524 | StatementNode( const std::string *name ); |
---|
525 | StatementNode( Type t, ExpressionNode *control = 0, StatementNode *block = 0 ); |
---|
526 | StatementNode( Type t, std::string *target ); |
---|
527 | StatementNode( DeclarationNode *decl ); |
---|
528 | |
---|
529 | ~StatementNode(); |
---|
530 | |
---|
531 | static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false ); |
---|
532 | |
---|
533 | StatementNode *set_block( StatementNode *b ) { block = b; return this; } |
---|
534 | StatementNode *get_block() const { return block; } |
---|
535 | |
---|
536 | void set_control( ExpressionNode *c ) { control = c; } |
---|
537 | ExpressionNode *get_control() const { return control; } |
---|
538 | |
---|
539 | StatementNode::Type get_type() const { return type; } |
---|
540 | |
---|
541 | StatementNode *add_label( const std::string * ); |
---|
542 | const std::list<std::string> &get_labels() const { return labels; } |
---|
543 | |
---|
544 | void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; } |
---|
545 | void setCatchRest( bool newVal ) { isCatchRest = newVal; } |
---|
546 | |
---|
547 | std::string get_target() const; |
---|
548 | |
---|
549 | // StatementNode *add_controlexp( ExpressionNode * ); |
---|
550 | StatementNode *append_block( StatementNode * ); |
---|
551 | StatementNode *append_last_case( StatementNode * ); |
---|
552 | |
---|
553 | void print( std::ostream &os, int indent = 0) const; |
---|
554 | virtual StatementNode *clone() const; |
---|
555 | virtual Statement *build() const; |
---|
556 | private: |
---|
557 | static const char *StType[]; |
---|
558 | Type type; |
---|
559 | ExpressionNode *control; |
---|
560 | StatementNode *block; |
---|
561 | std::list<std::string> labels; |
---|
562 | std::string *target; // target label for jump statements |
---|
563 | DeclarationNode *decl; |
---|
564 | bool isCatchRest; |
---|
565 | }; // StatementNode |
---|
566 | |
---|
567 | //############################################################################## |
---|
568 | |
---|
569 | class CompoundStmtNode : public StatementNode { |
---|
570 | public: |
---|
571 | CompoundStmtNode(); |
---|
572 | CompoundStmtNode( const std::string * ); |
---|
573 | CompoundStmtNode( StatementNode * ); |
---|
574 | ~CompoundStmtNode(); |
---|
575 | |
---|
576 | void add_statement( StatementNode * ); |
---|
577 | |
---|
578 | void print( std::ostream &os, int indent = 0 ) const; |
---|
579 | virtual Statement *build() const; |
---|
580 | private: |
---|
581 | StatementNode *first, *last; |
---|
582 | }; |
---|
583 | |
---|
584 | //############################################################################## |
---|
585 | |
---|
586 | class AsmStmtNode : public StatementNode { |
---|
587 | public: |
---|
588 | AsmStmtNode( Type, bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 ); |
---|
589 | ~AsmStmtNode(); |
---|
590 | |
---|
591 | void print( std::ostream &os, int indent = 0 ) const; |
---|
592 | Statement *build() const; |
---|
593 | private: |
---|
594 | bool voltile; |
---|
595 | ConstantExpr *instruction; |
---|
596 | ExpressionNode *output, *input; |
---|
597 | ExpressionNode *clobber; |
---|
598 | std::list< Label > gotolabels; |
---|
599 | }; |
---|
600 | |
---|
601 | //############################################################################## |
---|
602 | |
---|
603 | template< typename SynTreeType, typename NodeType > |
---|
604 | void buildList( const NodeType *firstNode, std::list< SynTreeType *> &outputList ) { |
---|
605 | SemanticError errors; |
---|
606 | std::back_insert_iterator< std::list< SynTreeType *> > out( outputList ); |
---|
607 | const NodeType *cur = firstNode; |
---|
608 | |
---|
609 | while ( cur ) { |
---|
610 | try { |
---|
611 | // SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::result_of<decltype(&NodeType::build)(NodeType)>::type>( cur ) ); |
---|
612 | SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::pointer_traits<decltype(cur->build())>::element_type>( cur ) ); |
---|
613 | if ( result ) { |
---|
614 | *out++ = result; |
---|
615 | } else { |
---|
616 | } // if |
---|
617 | } catch( SemanticError &e ) { |
---|
618 | errors.append( e ); |
---|
619 | } // try |
---|
620 | cur = dynamic_cast< NodeType *>( cur->get_link() ); |
---|
621 | } // while |
---|
622 | if ( ! errors.isEmpty() ) { |
---|
623 | throw errors; |
---|
624 | } // if |
---|
625 | } |
---|
626 | |
---|
627 | // in DeclarationNode.cc |
---|
628 | void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ); |
---|
629 | void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList ); |
---|
630 | void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ); |
---|
631 | |
---|
632 | #endif // PARSENODE_H |
---|
633 | |
---|
634 | // Local Variables: // |
---|
635 | // tab-width: 4 // |
---|
636 | // mode: c++ // |
---|
637 | // compile-command: "make install" // |
---|
638 | // End: // |
---|