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