source: src/Parser/ParseNode.h@ 18997b9

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 18997b9 was 893256d, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into ctor

Conflicts:

src/Parser/ParseNode.h

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