source: src/Parser/ParseNode.h@ 5b40f30

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 with_gc
Last change on this file since 5b40f30 was d63eeb0, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/GenPoly/Box.cc
src/Makefile.in
src/Parser/ParseNode.h
src/Parser/parser.cc
src/Parser/parser.yy
src/SymTab/Validate.cc
src/SynTree/Initializer.h
src/SynTree/ObjectDecl.cc
src/SynTree/Visitor.h
src/main.cc

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