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 16 21:32:18 2016 |
---|
13 | // Update Count : 538 |
---|
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 "Parser/LinkageSpec.h" |
---|
25 | #include "SynTree/Type.h" |
---|
26 | #include "SynTree/Expression.h" |
---|
27 | #include "SynTree/Statement.h" |
---|
28 | #include "SynTree/Label.h" |
---|
29 | #include "Common/utility.h" |
---|
30 | #include "Common/UniqueName.h" |
---|
31 | |
---|
32 | class StatementNode; |
---|
33 | class CompoundStmtNode; |
---|
34 | class DeclarationNode; |
---|
35 | class ExpressionNode; |
---|
36 | class InitializerNode; |
---|
37 | |
---|
38 | //############################################################################## |
---|
39 | |
---|
40 | class ParseNode { |
---|
41 | public: |
---|
42 | ParseNode(); |
---|
43 | ParseNode( const std::string * ); |
---|
44 | ParseNode( const std::string & ); // for copy constructing subclasses |
---|
45 | virtual ~ParseNode(); |
---|
46 | virtual ParseNode *clone() const = 0; |
---|
47 | |
---|
48 | ParseNode *get_next() const { return next; } |
---|
49 | ParseNode *set_next( ParseNode *newlink ) { next = newlink; return this; } |
---|
50 | ParseNode *get_last(); |
---|
51 | ParseNode *set_last( ParseNode *newlast ); |
---|
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 | private: |
---|
59 | static int indent_by; |
---|
60 | |
---|
61 | ParseNode *next = nullptr; |
---|
62 | std::string name; |
---|
63 | }; // ParseNode |
---|
64 | |
---|
65 | //############################################################################## |
---|
66 | |
---|
67 | class InitializerNode : public ParseNode { |
---|
68 | public: |
---|
69 | InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode *des = 0 ); |
---|
70 | InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 ); |
---|
71 | ~InitializerNode(); |
---|
72 | virtual InitializerNode *clone() const { assert( false ); return nullptr; } |
---|
73 | |
---|
74 | ExpressionNode *get_expression() const { return expr; } |
---|
75 | |
---|
76 | InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; } |
---|
77 | ExpressionNode *get_designators() const { return designator; } |
---|
78 | |
---|
79 | InitializerNode *set_maybeConstructed( bool value ) { maybeConstructed = value; return this; } |
---|
80 | bool get_maybeConstructed() const { return maybeConstructed; } |
---|
81 | |
---|
82 | InitializerNode *next_init() const { return kids; } |
---|
83 | |
---|
84 | void print( std::ostream &os, int indent = 0 ) const; |
---|
85 | void printOneLine( std::ostream & ) const; |
---|
86 | |
---|
87 | virtual Initializer *build() const; |
---|
88 | private: |
---|
89 | ExpressionNode *expr; |
---|
90 | bool aggregate; |
---|
91 | ExpressionNode *designator; // may be list |
---|
92 | InitializerNode *kids; |
---|
93 | bool maybeConstructed; |
---|
94 | }; |
---|
95 | |
---|
96 | //############################################################################## |
---|
97 | |
---|
98 | class ExpressionNode : public ParseNode { |
---|
99 | public: |
---|
100 | ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {} |
---|
101 | ExpressionNode( Expression * expr, const std::string *name ) : ParseNode( name ), expr( expr ) {} |
---|
102 | ExpressionNode( const ExpressionNode &other ); |
---|
103 | virtual ~ExpressionNode() {} |
---|
104 | virtual ExpressionNode *clone() const { assert( false ); return nullptr; } |
---|
105 | |
---|
106 | bool get_extension() const { return extension; } |
---|
107 | ExpressionNode *set_extension( bool exten ) { extension = exten; return this; } |
---|
108 | |
---|
109 | virtual void print( std::ostream &os, int indent = 0 ) const {} |
---|
110 | virtual void printOneLine( std::ostream &os, int indent = 0 ) const {} |
---|
111 | |
---|
112 | virtual Expression *build() const { return expr; } |
---|
113 | private: |
---|
114 | bool extension = false; |
---|
115 | Expression *expr; |
---|
116 | }; |
---|
117 | |
---|
118 | template< typename T > |
---|
119 | struct maybeBuild_t< Expression, T > { |
---|
120 | static inline Expression * doit( const T *orig ) { |
---|
121 | if ( orig ) { |
---|
122 | Expression *p = orig->build(); |
---|
123 | p->set_extension( orig->get_extension() ); |
---|
124 | return p; |
---|
125 | } else { |
---|
126 | return nullptr; |
---|
127 | } // if |
---|
128 | } |
---|
129 | }; |
---|
130 | |
---|
131 | enum class OperKinds { |
---|
132 | // diadic |
---|
133 | SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And, |
---|
134 | BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq, |
---|
135 | Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn, |
---|
136 | Index, Range, |
---|
137 | // monadic |
---|
138 | UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress, |
---|
139 | Ctor, Dtor, |
---|
140 | }; |
---|
141 | |
---|
142 | struct LabelNode { |
---|
143 | std::list< Label > labels; |
---|
144 | }; |
---|
145 | |
---|
146 | Expression *build_constantInteger( std::string &str ); |
---|
147 | Expression *build_constantFloat( std::string &str ); |
---|
148 | Expression *build_constantChar( std::string &str ); |
---|
149 | ConstantExpr *build_constantStr( std::string &str ); |
---|
150 | |
---|
151 | NameExpr *build_varref( const std::string *name, bool labelp = false ); |
---|
152 | Expression *build_typevalue( DeclarationNode *decl ); |
---|
153 | |
---|
154 | Expression *build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node ); |
---|
155 | Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member ); |
---|
156 | Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member ); |
---|
157 | Expression *build_addressOf( ExpressionNode *expr_node ); |
---|
158 | Expression *build_sizeOfexpr( ExpressionNode *expr_node ); |
---|
159 | Expression *build_sizeOftype( DeclarationNode *decl_node ); |
---|
160 | Expression *build_alignOfexpr( ExpressionNode *expr_node ); |
---|
161 | Expression *build_alignOftype( DeclarationNode *decl_node ); |
---|
162 | Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member ); |
---|
163 | Expression *build_and( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ); |
---|
164 | Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind ); |
---|
165 | Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node ); |
---|
166 | Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node ); |
---|
167 | Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ); |
---|
168 | Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 ); |
---|
169 | Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 ); |
---|
170 | Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 ); |
---|
171 | Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node ); |
---|
172 | Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node ); |
---|
173 | Expression *build_tuple( ExpressionNode * expr_node = 0 ); |
---|
174 | Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node ); |
---|
175 | Expression *build_range( ExpressionNode * low, ExpressionNode *high ); |
---|
176 | Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand ); |
---|
177 | Expression *build_valexpr( StatementNode *s ); |
---|
178 | Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids ); |
---|
179 | |
---|
180 | //############################################################################## |
---|
181 | |
---|
182 | class TypeData; |
---|
183 | |
---|
184 | class DeclarationNode : public ParseNode { |
---|
185 | public: |
---|
186 | enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic }; |
---|
187 | enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, }; |
---|
188 | enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary }; |
---|
189 | enum Modifier { Signed, Unsigned, Short, Long }; |
---|
190 | enum Aggregate { Struct, Union, Trait }; |
---|
191 | enum TypeClass { Type, Dtype, Ftype }; |
---|
192 | enum BuiltinType { Valist }; |
---|
193 | |
---|
194 | static const char *storageName[]; |
---|
195 | static const char *qualifierName[]; |
---|
196 | static const char *basicTypeName[]; |
---|
197 | static const char *modifierName[]; |
---|
198 | static const char *aggregateName[]; |
---|
199 | static const char *typeClassName[]; |
---|
200 | static const char *builtinTypeName[]; |
---|
201 | |
---|
202 | static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false ); |
---|
203 | static DeclarationNode *newQualifier( Qualifier ); |
---|
204 | static DeclarationNode *newStorageClass( StorageClass ); |
---|
205 | static DeclarationNode *newBasicType( BasicType ); |
---|
206 | static DeclarationNode *newModifier( Modifier ); |
---|
207 | static DeclarationNode *newForall( DeclarationNode *); |
---|
208 | static DeclarationNode *newFromTypedef( std::string *); |
---|
209 | static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ); |
---|
210 | static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants ); |
---|
211 | static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant ); |
---|
212 | static DeclarationNode *newName( std::string *); |
---|
213 | static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params ); |
---|
214 | static DeclarationNode *newTypeParam( TypeClass, std::string *); |
---|
215 | static DeclarationNode *newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ); |
---|
216 | static DeclarationNode *newTraitUse( std::string *name, ExpressionNode *params ); |
---|
217 | static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams ); |
---|
218 | static DeclarationNode *newPointer( DeclarationNode *qualifiers ); |
---|
219 | static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ); |
---|
220 | static DeclarationNode *newVarArray( DeclarationNode *qualifiers ); |
---|
221 | static DeclarationNode *newBitfield( ExpressionNode *size ); |
---|
222 | static DeclarationNode *newTuple( DeclarationNode *members ); |
---|
223 | static DeclarationNode *newTypeof( ExpressionNode *expr ); |
---|
224 | static DeclarationNode *newAttr( std::string *, ExpressionNode *expr ); |
---|
225 | static DeclarationNode *newAttr( std::string *, DeclarationNode *type ); |
---|
226 | static DeclarationNode *newBuiltinType( BuiltinType ); |
---|
227 | |
---|
228 | DeclarationNode(); |
---|
229 | ~DeclarationNode(); |
---|
230 | DeclarationNode *clone() const; |
---|
231 | |
---|
232 | DeclarationNode *addQualifiers( DeclarationNode *); |
---|
233 | DeclarationNode *copyStorageClasses( DeclarationNode *); |
---|
234 | DeclarationNode *addType( DeclarationNode *); |
---|
235 | DeclarationNode *addTypedef(); |
---|
236 | DeclarationNode *addAssertions( DeclarationNode *); |
---|
237 | DeclarationNode *addName( std::string *); |
---|
238 | DeclarationNode *addBitfield( ExpressionNode *size ); |
---|
239 | DeclarationNode *addVarArgs(); |
---|
240 | DeclarationNode *addFunctionBody( StatementNode *body ); |
---|
241 | DeclarationNode *addOldDeclList( DeclarationNode *list ); |
---|
242 | DeclarationNode *addPointer( DeclarationNode *qualifiers ); |
---|
243 | DeclarationNode *addArray( DeclarationNode *array ); |
---|
244 | DeclarationNode *addNewPointer( DeclarationNode *pointer ); |
---|
245 | DeclarationNode *addNewArray( DeclarationNode *array ); |
---|
246 | DeclarationNode *addParamList( DeclarationNode *list ); |
---|
247 | DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions |
---|
248 | DeclarationNode *addInitializer( InitializerNode *init ); |
---|
249 | |
---|
250 | DeclarationNode *cloneType( std::string *newName ); |
---|
251 | DeclarationNode *cloneType( DeclarationNode *existing ); |
---|
252 | DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); } |
---|
253 | DeclarationNode *cloneBaseType( std::string *newName ); |
---|
254 | DeclarationNode *cloneBaseType( DeclarationNode *newdecl ); |
---|
255 | |
---|
256 | DeclarationNode *appendList( DeclarationNode * ); |
---|
257 | |
---|
258 | void print( std::ostream &os, int indent = 0 ) const; |
---|
259 | void printList( std::ostream &os, int indent = 0 ) const; |
---|
260 | |
---|
261 | Declaration *build() const; |
---|
262 | ::Type *buildType() const; |
---|
263 | |
---|
264 | bool get_hasEllipsis() const; |
---|
265 | const std::string &get_name() const { return name; } |
---|
266 | LinkageSpec::Type get_linkage() const { return linkage; } |
---|
267 | DeclarationNode *extractAggregate() const; |
---|
268 | ExpressionNode *get_enumeratorValue() const { return enumeratorValue; } |
---|
269 | |
---|
270 | bool get_extension() const { return extension; } |
---|
271 | DeclarationNode *set_extension( bool exten ) { extension = exten; return this; } |
---|
272 | private: |
---|
273 | // StorageClass buildStorageClass() const; |
---|
274 | // bool buildFuncSpecifier( StorageClass key ) const; |
---|
275 | |
---|
276 | TypeData *type; |
---|
277 | std::string name; |
---|
278 | // std::list< StorageClass > storageClasses; |
---|
279 | StorageClass storageClass; |
---|
280 | bool isInline, isNoreturn; |
---|
281 | std::list< std::string > attributes; |
---|
282 | ExpressionNode *bitfieldWidth; |
---|
283 | ExpressionNode *enumeratorValue; |
---|
284 | InitializerNode *initializer; |
---|
285 | bool hasEllipsis; |
---|
286 | LinkageSpec::Type linkage; |
---|
287 | bool extension = false; |
---|
288 | std::string error; |
---|
289 | |
---|
290 | static UniqueName anonymous; |
---|
291 | }; // DeclarationNode |
---|
292 | |
---|
293 | Type *buildType( TypeData *type ); |
---|
294 | |
---|
295 | //############################################################################## |
---|
296 | |
---|
297 | class StatementNode : public ParseNode { |
---|
298 | public: |
---|
299 | StatementNode() { stmt = nullptr; } |
---|
300 | StatementNode( Statement *stmt ) : stmt( stmt ) {} |
---|
301 | StatementNode( DeclarationNode *decl ); |
---|
302 | virtual ~StatementNode() {} |
---|
303 | |
---|
304 | virtual StatementNode *clone() const { assert( false ); return nullptr; } |
---|
305 | virtual Statement *build() const { return stmt; } |
---|
306 | |
---|
307 | virtual StatementNode *add_label( const std::string * name ) { |
---|
308 | stmt->get_labels().emplace_back( *name ); |
---|
309 | return this; |
---|
310 | } |
---|
311 | |
---|
312 | virtual StatementNode *append_last_case( StatementNode * ); |
---|
313 | |
---|
314 | virtual void print( std::ostream &os, int indent = 0 ) {} |
---|
315 | virtual void printList( std::ostream &os, int indent = 0 ) {} |
---|
316 | private: |
---|
317 | Statement *stmt; |
---|
318 | }; // StatementNode |
---|
319 | |
---|
320 | Statement *build_expr( ExpressionNode *ctl ); |
---|
321 | |
---|
322 | struct ForCtl { |
---|
323 | ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) : |
---|
324 | init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {} |
---|
325 | ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) : |
---|
326 | init( new StatementNode( decl ) ), condition( condition ), change( change ) {} |
---|
327 | |
---|
328 | StatementNode *init; |
---|
329 | ExpressionNode *condition; |
---|
330 | ExpressionNode *change; |
---|
331 | }; |
---|
332 | |
---|
333 | Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ); |
---|
334 | Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ); |
---|
335 | Statement *build_case( ExpressionNode *ctl ); |
---|
336 | Statement *build_default(); |
---|
337 | Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false ); |
---|
338 | Statement *build_for( ForCtl *forctl, StatementNode *stmt ); |
---|
339 | Statement *build_branch( std::string identifier, BranchStmt::Type kind ); |
---|
340 | Statement *build_computedgoto( ExpressionNode *ctl ); |
---|
341 | Statement *build_return( ExpressionNode *ctl ); |
---|
342 | Statement *build_throw( ExpressionNode *ctl ); |
---|
343 | Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ); |
---|
344 | Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false ); |
---|
345 | Statement *build_finally( StatementNode *stmt ); |
---|
346 | Statement *build_compound( StatementNode *first ); |
---|
347 | Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 ); |
---|
348 | |
---|
349 | //############################################################################## |
---|
350 | |
---|
351 | template< typename SynTreeType, typename NodeType > |
---|
352 | void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) { |
---|
353 | SemanticError errors; |
---|
354 | std::back_insert_iterator< std::list< SynTreeType * > > out( outputList ); |
---|
355 | const NodeType *cur = firstNode; |
---|
356 | |
---|
357 | while ( cur ) { |
---|
358 | try { |
---|
359 | // SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) ); |
---|
360 | SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) ); |
---|
361 | if ( result ) { |
---|
362 | *out++ = result; |
---|
363 | } else { |
---|
364 | } // if |
---|
365 | } catch( SemanticError &e ) { |
---|
366 | errors.append( e ); |
---|
367 | } // try |
---|
368 | cur = dynamic_cast< NodeType * >( cur->get_next() ); |
---|
369 | } // while |
---|
370 | if ( ! errors.isEmpty() ) { |
---|
371 | throw errors; |
---|
372 | } // if |
---|
373 | } |
---|
374 | |
---|
375 | // in DeclarationNode.cc |
---|
376 | void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ); |
---|
377 | void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ); |
---|
378 | void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ); |
---|
379 | |
---|
380 | #endif // PARSENODE_H |
---|
381 | |
---|
382 | // Local Variables: // |
---|
383 | // tab-width: 4 // |
---|
384 | // mode: c++ // |
---|
385 | // compile-command: "make install" // |
---|
386 | // End: // |
---|