source: src/Parser/ParseNode.h@ 51b986f

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 51b986f was 1db21619, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

add CFA flag, remove -p from cc1, typedef on functions become function declarations, move isInline/isNoreturn to Declaration, cleaned up label code

  • Property mode set to 100644
File size: 16.0 KB
Line 
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 Jul 14 14:58:25 2015
13// Update Count : 91
14//
15
16#ifndef PARSENODE_H
17#define PARSENODE_H
18
19#include <string>
20#include <list>
21#include <iterator>
22
23#include "utility.h"
24#include "Parser/LinkageSpec.h"
25#include "SynTree/Type.h"
26//#include "SynTree/Declaration.h"
27#include "UniqueName.h"
28
29class ExpressionNode;
30class CompositeExprNode;
31class CommaExprNode;
32class StatementNode;
33class CompoundStmtNode;
34class DeclarationNode;
35class InitializerNode;
36
37// Builder
38class ParseNode {
39 public:
40 ParseNode();
41 ParseNode( const std::string * );
42 virtual ~ParseNode();
43
44 ParseNode *get_link() const;
45 ParseNode *get_last();
46 ParseNode *set_link( ParseNode * );
47 void set_next( ParseNode *newlink ) { next = newlink; }
48
49 virtual ParseNode *clone() const { return 0; };
50
51 const std::string &get_name() const { return *name; }
52 virtual void print( std::ostream &, int indent = 0 ) const;
53 virtual void printList( std::ostream &, int indent = 0 ) const;
54
55 ParseNode &operator,( ParseNode &);
56 protected:
57 const std::string *name;
58 ParseNode *next;
59 static int indent_by;
60};
61
62ParseNode *mkList( ParseNode & );
63
64class ExpressionNode : public ParseNode {
65 public:
66 ExpressionNode();
67 ExpressionNode( const std::string * );
68 ExpressionNode( const ExpressionNode &other );
69 virtual ~ExpressionNode() {} // cannot delete asArgName because it might be referenced elsewhere
70
71 virtual ExpressionNode *clone() const = 0;
72
73 virtual CommaExprNode *add_to_list( ExpressionNode * );
74
75 ExpressionNode *get_argName() const { return argName; }
76 ExpressionNode *set_asArgName( const std::string *aName );
77 ExpressionNode *set_asArgName( ExpressionNode *aDesignator );
78
79 virtual void print( std::ostream &, int indent = 0) const = 0;
80 virtual void printOneLine( std::ostream &, int indent = 0) const = 0;
81
82 virtual Expression *build() const = 0;
83 protected:
84 void printDesignation ( std::ostream &, int indent = 0) const;
85 private:
86 ExpressionNode *argName;
87};
88
89// NullExprNode is used in tuples as a place-holder where a tuple component is omitted e.g., [ 2, , 3 ]
90class NullExprNode : public ExpressionNode {
91 public:
92 NullExprNode();
93
94 virtual NullExprNode *clone() const;
95
96 virtual void print( std::ostream &, int indent = 0) const;
97 virtual void printOneLine( std::ostream &, int indent = 0) const;
98
99 virtual Expression *build() const;
100};
101
102class ConstantNode : public ExpressionNode {
103 public:
104 enum Type { Integer, Float, Character, String };
105
106 ConstantNode( Type, std::string * );
107
108 virtual ConstantNode *clone() const { return new ConstantNode( *this ); }
109 Type get_type( void ) const { return type; }
110 virtual void print( std::ostream &, int indent = 0) const;
111 virtual void printOneLine( std::ostream &, int indent = 0) const;
112
113 const std::string &get_value() const { return value; }
114 ConstantNode *appendstr( const std::string *newValue );
115
116 Expression *build() const;
117 private:
118 Type type;
119 BasicType::Kind btype;
120 std::string &value;
121};
122
123class VarRefNode : public ExpressionNode {
124 public:
125 VarRefNode();
126 VarRefNode( const std::string *, bool isLabel = false );
127 VarRefNode( const VarRefNode &other );
128
129 virtual Expression *build() const ;
130
131 virtual VarRefNode *clone() const { return new VarRefNode( *this ); }
132
133 virtual void print( std::ostream &, int indent = 0 ) const;
134 virtual void printOneLine( std::ostream &, int indent = 0 ) const;
135 private:
136 bool isLabel;
137};
138
139class TypeValueNode : public ExpressionNode {
140 public:
141 TypeValueNode( DeclarationNode * );
142 TypeValueNode( const TypeValueNode &other );
143
144 DeclarationNode *get_decl() const { return decl; }
145
146 virtual Expression *build() const ;
147
148 virtual TypeValueNode *clone() const { return new TypeValueNode( *this ); }
149
150 virtual void print( std::ostream &, int indent = 0) const;
151 virtual void printOneLine( std::ostream &, int indent = 0) const;
152 private:
153 DeclarationNode *decl;
154};
155
156class OperatorNode : public ExpressionNode {
157 public:
158 enum Type { TupleC, Comma, TupleFieldSel,
159 Cond, NCond,
160 SizeOf, AlignOf, Attr, CompLit, Plus, Minus, Mul, Div, Mod, Or, And,
161 BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
162 Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn,
163 ERAssn, OrAssn, Index, FieldSel, PFieldSel, Range,
164 UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress
165 };
166
167 OperatorNode( Type t );
168 OperatorNode( const OperatorNode &other );
169 virtual ~OperatorNode();
170
171 virtual OperatorNode *clone() const { return new OperatorNode( *this ); }
172
173 Type get_type() const;
174 const char *get_typename() const;
175
176 virtual void print( std::ostream &, int indent = 0) const;
177 virtual void printOneLine( std::ostream &, int indent = 0) const;
178
179 virtual Expression *build() const { return 0; }
180 private:
181 Type type;
182 static const char *OpName[];
183};
184
185
186class CompositeExprNode : public ExpressionNode {
187 public:
188 CompositeExprNode();
189 CompositeExprNode( const std::string * );
190 CompositeExprNode( ExpressionNode *f, ExpressionNode *args = 0 );
191 CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2 );
192 CompositeExprNode( const CompositeExprNode &other );
193 virtual ~CompositeExprNode();
194
195 virtual CompositeExprNode *clone() const { return new CompositeExprNode( *this ); }
196 virtual Expression *build() const;
197
198 virtual void print( std::ostream &, int indent = 0) const;
199 virtual void printOneLine( std::ostream &, int indent = 0) const;
200
201 void set_function( ExpressionNode * );
202 void set_args( ExpressionNode * );
203
204 void add_arg( ExpressionNode * );
205
206 ExpressionNode *get_function() const;
207 ExpressionNode *get_args() const;
208 private:
209 ExpressionNode *function;
210 ExpressionNode *arguments;
211};
212
213class CommaExprNode : public CompositeExprNode {
214 public:
215 CommaExprNode();
216 CommaExprNode( ExpressionNode * );
217 CommaExprNode( ExpressionNode *, ExpressionNode * );
218 CommaExprNode( const CommaExprNode &other );
219
220 virtual CommaExprNode *add_to_list( ExpressionNode * );
221 virtual CommaExprNode *clone() const { return new CommaExprNode( *this ); }
222};
223
224class ForCtlExprNode : public ExpressionNode {
225 public:
226 ForCtlExprNode( ParseNode *, ExpressionNode *, ExpressionNode * ) throw ( SemanticError );
227 ForCtlExprNode( const ForCtlExprNode &other );
228 ~ForCtlExprNode();
229
230 StatementNode *get_init() const { return init; }
231 ExpressionNode *get_condition() const { return condition; }
232 ExpressionNode *get_change() const { return change; }
233
234 virtual ForCtlExprNode *clone() const { return new ForCtlExprNode( *this ); }
235 virtual Expression *build() const;
236
237 virtual void print( std::ostream &, int indent = 0 ) const;
238 virtual void printOneLine( std::ostream &, int indent = 0 ) const;
239 private:
240 StatementNode *init;
241 ExpressionNode *condition;
242 ExpressionNode *change;
243};
244
245class ValofExprNode : public ExpressionNode {
246 public:
247 ValofExprNode();
248 ValofExprNode( StatementNode *s = 0 );
249 ValofExprNode( const ValofExprNode &other );
250 ~ValofExprNode();
251
252 virtual ValofExprNode *clone() const { return new ValofExprNode( *this ); }
253
254 StatementNode *get_body() const { return body; }
255 void print( std::ostream &, int indent = 0 ) const;
256 void printOneLine( std::ostream &, int indent = 0 ) const;
257 Expression *build() const;
258
259 private:
260 StatementNode *body;
261};
262
263class TypeData;
264
265class DeclarationNode : public ParseNode {
266 public:
267 enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
268 enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
269 enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
270 enum Modifier { Signed, Unsigned, Short, Long };
271 enum Aggregate { Struct, Union, Context };
272 enum TypeClass { Type, Dtype, Ftype };
273
274 static const char *storageName[];
275 static const char *qualifierName[];
276 static const char *basicTypeName[];
277 static const char *modifierName[];
278 static const char *aggregateName[];
279 static const char *typeClassName[];
280
281 static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param,
282 StatementNode *body, bool newStyle = false );
283 static DeclarationNode *newQualifier( Qualifier );
284 static DeclarationNode *newStorageClass( StorageClass );
285 static DeclarationNode *newBasicType( BasicType );
286 static DeclarationNode *newModifier( Modifier );
287 static DeclarationNode *newForall( DeclarationNode *);
288 static DeclarationNode *newFromTypedef( std::string *);
289 static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields );
290 static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
291 static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
292 static DeclarationNode *newName( std::string *);
293 static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
294 static DeclarationNode *newTypeParam( TypeClass, std::string *);
295 static DeclarationNode *newContext( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
296 static DeclarationNode *newContextUse( std::string *name, ExpressionNode *params );
297 static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams );
298 static DeclarationNode *newPointer( DeclarationNode *qualifiers );
299 static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic );
300 static DeclarationNode *newVarArray( DeclarationNode *qualifiers );
301 static DeclarationNode *newBitfield( ExpressionNode *size );
302 static DeclarationNode *newTuple( DeclarationNode *members );
303 static DeclarationNode *newTypeof( ExpressionNode *expr );
304 static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
305 static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
306
307 DeclarationNode *addQualifiers( DeclarationNode *);
308 DeclarationNode *copyStorageClasses( DeclarationNode *);
309 DeclarationNode *addType( DeclarationNode *);
310 DeclarationNode *addTypedef();
311 DeclarationNode *addAssertions( DeclarationNode *);
312 DeclarationNode *addName( std::string *);
313 DeclarationNode *addBitfield( ExpressionNode *size );
314 DeclarationNode *addVarArgs();
315 DeclarationNode *addFunctionBody( StatementNode *body );
316 DeclarationNode *addOldDeclList( DeclarationNode *list );
317 DeclarationNode *addPointer( DeclarationNode *qualifiers );
318 DeclarationNode *addArray( DeclarationNode *array );
319 DeclarationNode *addNewPointer( DeclarationNode *pointer );
320 DeclarationNode *addNewArray( DeclarationNode *array );
321 DeclarationNode *addParamList( DeclarationNode *list );
322 DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions
323 DeclarationNode *addInitializer( InitializerNode *init );
324
325 DeclarationNode *cloneType( std::string *newName );
326 DeclarationNode *cloneType( DeclarationNode *existing );
327 DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); }
328 DeclarationNode *cloneBaseType( std::string *newName );
329 DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
330
331 DeclarationNode *appendList( DeclarationNode * );
332
333 DeclarationNode *clone() const;
334 void print( std::ostream &, int indent = 0 ) const;
335 void printList( std::ostream &, int indent = 0 ) const;
336
337 Declaration *build() const;
338 ::Type *buildType() const;
339
340 bool get_hasEllipsis() const;
341 const std::string &get_name() const { return name; }
342 LinkageSpec::Type get_linkage() const { return linkage; }
343 DeclarationNode *extractAggregate() const;
344
345 DeclarationNode();
346 ~DeclarationNode();
347 private:
348 StorageClass buildStorageClass() const;
349 bool buildFuncSpecifier( StorageClass key ) const;
350
351 TypeData *type;
352 std::string name;
353 std::list< StorageClass > storageClasses;
354 std::list< std::string > attributes;
355 ExpressionNode *bitfieldWidth;
356 InitializerNode *initializer;
357 bool hasEllipsis;
358 LinkageSpec::Type linkage;
359
360 static UniqueName anonymous;
361}; // DeclarationNode
362
363class StatementNode : public ParseNode {
364 public:
365 enum Type { Exp, If, Switch, Case, Default, Choose, Fallthru,
366 While, Do, For,
367 Goto, Continue, Break, Return, Throw,
368 Try, Catch, Finally, Asm,
369 Decl
370 };
371
372 StatementNode();
373 StatementNode( const std::string * );
374 StatementNode( Type, ExpressionNode *e = 0, StatementNode *s = 0 );
375 StatementNode( Type, std::string *target );
376 StatementNode( DeclarationNode *decl );
377
378
379 ~StatementNode();
380
381 static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false );
382
383 StatementNode *set_block( StatementNode *b ) { block = b; return this; }
384 StatementNode *get_block() const { return block; }
385
386 void set_control( ExpressionNode *c ) { control = c; }
387 ExpressionNode *get_control() const { return control; }
388
389 StatementNode::Type get_type() const { return type; }
390
391 StatementNode *add_label( const std::string * );
392 const std::list<std::string> &get_labels() const { return labels; }
393
394 void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; }
395 void setCatchRest( bool newVal ) { isCatchRest = newVal; }
396
397 std::string get_target() const;
398
399 StatementNode *add_controlexp( ExpressionNode * );
400 StatementNode *append_block( StatementNode * );
401 StatementNode *append_last_case( StatementNode * );
402
403 void print( std::ostream &, int indent = 0) const;
404
405 virtual StatementNode *clone() const;
406
407 virtual Statement *build() const;
408 private:
409 static const char *StType[];
410 Type type;
411 ExpressionNode *control;
412 StatementNode *block;
413 std::list<std::string> labels;
414 std::string *target; // target label for jump statements
415 DeclarationNode *decl;
416
417 bool isCatchRest;
418}; // StatementNode
419
420class CompoundStmtNode : public StatementNode {
421 public:
422 CompoundStmtNode();
423 CompoundStmtNode( const std::string * );
424 CompoundStmtNode( StatementNode * );
425 ~CompoundStmtNode();
426
427 void add_statement( StatementNode * );
428
429 void print( std::ostream &, int indent = 0 ) const;
430
431 virtual Statement *build() const;
432 private:
433 StatementNode *first, *last;
434};
435
436class NullStmtNode : public CompoundStmtNode {
437 public:
438 Statement *build() const;
439 void print( std::ostream &, int indent = 0) const;
440};
441
442class InitializerNode : public ParseNode {
443 public:
444 InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode *des = 0 );
445 InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
446 ~InitializerNode();
447
448 ExpressionNode *get_expression() const { return expr; }
449
450 InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
451 ExpressionNode *get_designators() const { return designator; }
452
453 InitializerNode *next_init() const { return kids; }
454
455 void print( std::ostream &, int indent = 0 ) const;
456 void printOneLine( std::ostream & ) const;
457
458 virtual Initializer *build() const;
459 private:
460 ExpressionNode *expr;
461 bool aggregate;
462 ExpressionNode *designator; // may be list
463 InitializerNode *kids;
464};
465
466
467
468template< typename SynTreeType, typename NodeType >
469void buildList( const NodeType *firstNode, std::list< SynTreeType *> &outputList ) {
470 SemanticError errors;
471 std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
472 const NodeType *cur = firstNode;
473
474 while ( cur ) {
475 try {
476 SynTreeType *result = dynamic_cast< SynTreeType *>( cur->build() );
477 if ( result ) {
478 *out++ = result;
479 } else {
480 } // if
481 } catch( SemanticError &e ) {
482 errors.append( e );
483 } // try
484 cur = dynamic_cast< NodeType *>( cur->get_link() );
485 } // while
486 if ( ! errors.isEmpty() ) {
487 throw errors;
488 } // if
489}
490
491// in DeclarationNode.cc
492void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
493void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
494void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
495
496// in ExpressionNode.cc
497ExpressionNode *flattenCommas( ExpressionNode *list );
498ExpressionNode *tupleContents( ExpressionNode *tuple );
499
500#endif // PARSENODE_H
501
502// Local Variables: //
503// tab-width: 4 //
504// mode: c++ //
505// compile-command: "make install" //
506// End: //
Note: See TracBrowser for help on using the repository browser.