source: src/Parser/ParseNode.h @ 37218fc

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 37218fc was 37218fc, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

Conflicts:

src/Parser/ParseNode.h

  • Property mode set to 100644
File size: 19.7 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 : Mon Apr 11 11:50:52 2016
13// Update Count     : 205
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 ) : type( other.type ), btype( other.btype), 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        };
188
189        OperatorNode( Type t );
190        OperatorNode( const OperatorNode &other );
191        virtual ~OperatorNode();
192
193        virtual OperatorNode *clone() const { return new OperatorNode( *this ); }
194
195        Type get_type() const;
196        const char *get_typename() const;
197
198        virtual void print( std::ostream &, int indent = 0) const;
199        virtual void printOneLine( std::ostream &, int indent = 0) const;
200
201        virtual Expression *build() const { return 0; }
202  private:
203        Type type;
204};
205
206class CompositeExprNode : public ExpressionNode {
207  public:
208        CompositeExprNode();
209        CompositeExprNode( const std::string * );
210        CompositeExprNode( ExpressionNode *f, ExpressionNode *args = 0 );
211        CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2 );
212        CompositeExprNode( const CompositeExprNode &other );
213        virtual ~CompositeExprNode();
214
215        virtual CompositeExprNode *clone() const { return new CompositeExprNode( *this ); }
216        virtual Expression *build() const;
217
218        virtual void print( std::ostream &, int indent = 0) const;
219        virtual void printOneLine( std::ostream &, int indent = 0) const;
220
221        void set_function( ExpressionNode * );
222        void set_args( ExpressionNode * );
223
224        void add_arg( ExpressionNode * );
225
226        ExpressionNode *get_function() const;
227        ExpressionNode *get_args() const;
228  private:
229        ExpressionNode *function;
230        ExpressionNode *arguments;
231};
232
233class AsmExprNode : public ExpressionNode {
234  public:
235        AsmExprNode();
236        AsmExprNode( ExpressionNode *inout, ConstantNode *constraint, ExpressionNode *operand ) : inout( inout ), constraint( constraint ), operand( operand ) {}
237        virtual ~AsmExprNode() { delete inout; delete constraint; delete operand; }
238
239        virtual AsmExprNode *clone() const { return new AsmExprNode( *this ); }
240        virtual Expression *build() const;
241
242        virtual void print( std::ostream &, int indent = 0) const;
243        virtual void printOneLine( std::ostream &, int indent = 0) const;
244
245        ExpressionNode *get_inout() const { return inout; };
246        void set_inout( ExpressionNode *newValue ) { inout = newValue; }
247
248        ConstantNode *get_constraint() const { return constraint; };
249        void set_constraint( ConstantNode *newValue ) { constraint = newValue; }
250
251        ExpressionNode *get_operand() const { return operand; };
252        void set_operand( ExpressionNode *newValue ) { operand = newValue; }
253  private:
254        ExpressionNode *inout;
255        ConstantNode *constraint;
256        ExpressionNode *operand;
257};
258
259class LabelNode : public ExpressionNode {
260  public:
261        virtual Expression *build() const { return NULL; }
262        virtual LabelNode *clone() const { return new LabelNode( *this ); }
263
264        virtual void print( std::ostream &, int indent = 0) const;
265        virtual void printOneLine( std::ostream &, int indent = 0) const;
266
267        const std::list< std::string > &get_labels() const { return labels; };
268        void append_label( std::string *label ) { labels.push_back( *label ); delete label; }
269  private:
270        std::list< std::string > labels;
271};
272
273class CommaExprNode : public CompositeExprNode {
274  public:
275        CommaExprNode();
276        CommaExprNode( ExpressionNode * );
277        CommaExprNode( ExpressionNode *, ExpressionNode * );
278        CommaExprNode( const CommaExprNode &other );
279
280        virtual CommaExprNode *add_to_list( ExpressionNode * );
281        virtual CommaExprNode *clone() const { return new CommaExprNode( *this ); }
282};
283
284class ForCtlExprNode : public ExpressionNode {
285  public:
286        ForCtlExprNode( ParseNode *, ExpressionNode *, ExpressionNode * ) throw ( SemanticError );
287        ForCtlExprNode( const ForCtlExprNode &other );
288        ~ForCtlExprNode();
289
290        StatementNode *get_init() const { return init; }
291        ExpressionNode *get_condition() const { return condition; }
292        ExpressionNode *get_change() const { return change; }
293
294        virtual ForCtlExprNode *clone() const { return new ForCtlExprNode( *this ); }
295        virtual Expression *build() const;
296
297        virtual void print( std::ostream &, int indent = 0 ) const;
298        virtual void printOneLine( std::ostream &, int indent = 0 ) const;
299  private:
300        StatementNode *init;
301        ExpressionNode *condition;
302        ExpressionNode *change;
303};
304
305class ValofExprNode : public ExpressionNode {
306  public:
307        ValofExprNode();
308        ValofExprNode( StatementNode *s = 0 );
309        ValofExprNode( const ValofExprNode &other );
310        ~ValofExprNode();
311
312        virtual ValofExprNode *clone() const { return new ValofExprNode( *this ); }
313
314        StatementNode *get_body() const { return body; }
315        void print( std::ostream &, int indent = 0 ) const;
316        void printOneLine( std::ostream &, int indent = 0 ) const;
317        Expression *build() const;
318
319  private:
320        StatementNode *body;
321};
322
323class TypeData;
324
325class DeclarationNode : public ParseNode {
326  public:
327        enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
328        enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
329        enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
330        enum Modifier  { Signed, Unsigned, Short, Long };
331        enum Aggregate { Struct, Union, Trait };
332        enum TypeClass { Type, Dtype, Ftype };
333        enum BuiltinType { Valist };
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        static const char *builtinTypeName[];
342
343        static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
344        static DeclarationNode *newQualifier( Qualifier );
345        static DeclarationNode *newStorageClass( StorageClass );
346        static DeclarationNode *newBasicType( BasicType );
347        static DeclarationNode *newModifier( Modifier );
348        static DeclarationNode *newForall( DeclarationNode *);
349        static DeclarationNode *newFromTypedef( std::string *);
350        static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields );
351        static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
352        static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
353        static DeclarationNode *newName( std::string *);
354        static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
355        static DeclarationNode *newTypeParam( TypeClass, std::string *);
356        static DeclarationNode *newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
357        static DeclarationNode *newTraitUse( std::string *name, ExpressionNode *params );
358        static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams );
359        static DeclarationNode *newPointer( DeclarationNode *qualifiers );
360        static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic );
361        static DeclarationNode *newVarArray( DeclarationNode *qualifiers );
362        static DeclarationNode *newBitfield( ExpressionNode *size );
363        static DeclarationNode *newTuple( DeclarationNode *members );
364        static DeclarationNode *newTypeof( ExpressionNode *expr );
365        static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
366        static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
367        static DeclarationNode *newBuiltinType( BuiltinType );
368
369        DeclarationNode *addQualifiers( DeclarationNode *);
370        DeclarationNode *copyStorageClasses( DeclarationNode *);
371        DeclarationNode *addType( DeclarationNode *);
372        DeclarationNode *addTypedef();
373        DeclarationNode *addAssertions( DeclarationNode *);
374        DeclarationNode *addName( std::string *);
375        DeclarationNode *addBitfield( ExpressionNode *size );
376        DeclarationNode *addVarArgs();
377        DeclarationNode *addFunctionBody( StatementNode *body );
378        DeclarationNode *addOldDeclList( DeclarationNode *list );
379        DeclarationNode *addPointer( DeclarationNode *qualifiers );
380        DeclarationNode *addArray( DeclarationNode *array );
381        DeclarationNode *addNewPointer( DeclarationNode *pointer );
382        DeclarationNode *addNewArray( DeclarationNode *array );
383        DeclarationNode *addParamList( DeclarationNode *list );
384        DeclarationNode *addIdList( DeclarationNode *list );       // old-style functions
385        DeclarationNode *addInitializer( InitializerNode *init );
386
387        DeclarationNode *cloneType( std::string *newName );
388        DeclarationNode *cloneType( DeclarationNode *existing );
389        DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); }
390        DeclarationNode *cloneBaseType( std::string *newName );
391        DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
392
393        DeclarationNode *appendList( DeclarationNode * );
394
395        DeclarationNode *clone() const;
396        void print( std::ostream &, int indent = 0 ) const;
397        void printList( std::ostream &, int indent = 0 ) const;
398
399        Declaration *build() const;
400        ::Type *buildType() const;
401
402        bool get_hasEllipsis() const;
403        const std::string &get_name() const { return name; }
404        LinkageSpec::Type get_linkage() const { return linkage; }
405        DeclarationNode *extractAggregate() const;
406        ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
407
408        DeclarationNode();
409        ~DeclarationNode();
410  private:
411        StorageClass buildStorageClass() const;
412        bool buildFuncSpecifier( StorageClass key ) const;
413
414        TypeData *type;
415        std::string name;
416        std::list< StorageClass > storageClasses;
417        std::list< std::string > attributes;
418        ExpressionNode *bitfieldWidth;
419        ExpressionNode *enumeratorValue;
420        InitializerNode *initializer;
421        bool hasEllipsis;
422        LinkageSpec::Type linkage;
423
424        static UniqueName anonymous;
425}; // DeclarationNode
426
427class StatementNode : public ParseNode {
428  public:
429        enum Type { Exp,   If,        Switch,  Case,    Default,  Choose,   Fallthru,
430                                While, Do,        For,
431                                Goto,  Continue,  Break,   Return,  Throw,
432                                Try,   Catch,     Finally, Asm,
433                                Decl
434        };
435
436        StatementNode();
437        StatementNode( const std::string *name );
438        StatementNode( Type t, ExpressionNode *control = 0, StatementNode *block = 0 );
439        StatementNode( Type t, std::string *target );
440        StatementNode( DeclarationNode *decl );
441
442        ~StatementNode();
443
444        static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false );
445
446        StatementNode *set_block( StatementNode *b ) {  block = b; return this; }
447        StatementNode *get_block() const { return block; }
448
449        void set_control( ExpressionNode *c ) { control = c; }
450        ExpressionNode *get_control() const { return control; }
451
452        StatementNode::Type get_type() const { return type; }
453
454        StatementNode *add_label( const std::string * );
455        const std::list<std::string> &get_labels() const { return labels; }
456
457        void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; }
458        void setCatchRest( bool newVal ) { isCatchRest = newVal; }
459
460        std::string get_target() const;
461
462        StatementNode *add_controlexp( ExpressionNode * );
463        StatementNode *append_block( StatementNode * );
464        StatementNode *append_last_case( StatementNode * );
465
466        void print( std::ostream &, int indent = 0) const;
467        virtual StatementNode *clone() const;
468        virtual Statement *build() const;
469  private:
470        static const char *StType[];
471        Type type;
472        ExpressionNode *control;
473        StatementNode *block;
474        std::list<std::string> labels;
475        std::string *target;                            // target label for jump statements
476        DeclarationNode *decl;
477        bool isCatchRest;
478}; // StatementNode
479
480class CompoundStmtNode : public StatementNode {
481  public:
482        CompoundStmtNode();
483        CompoundStmtNode( const std::string * );
484        CompoundStmtNode( StatementNode * );
485        ~CompoundStmtNode();
486
487        void add_statement( StatementNode * );
488
489        void print( std::ostream &, int indent = 0 ) const;
490        virtual Statement *build() const;
491  private:
492        StatementNode *first, *last;
493};
494
495class AsmStmtNode : public StatementNode {
496  public:
497        AsmStmtNode( Type, bool voltile, ConstantNode *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ConstantNode *clobber = 0, LabelNode *gotolabels = 0 );
498        ~AsmStmtNode();
499
500        void print( std::ostream &, int indent = 0 ) const;
501        Statement *build() const;
502  private:
503        bool voltile;
504        ConstantNode *instruction;
505        ExpressionNode *output, *input;
506        ConstantNode *clobber;
507        std::list<std::string> gotolabels;
508};
509
510class NullStmtNode : public CompoundStmtNode {
511  public:
512        Statement *build() const;
513        void print( std::ostream &, int indent = 0 ) const;
514};
515
516class InitializerNode : public ParseNode {
517  public:
518        InitializerNode( ExpressionNode *, bool aggrp = false,  ExpressionNode *des = 0 );
519        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
520        ~InitializerNode();
521
522        ExpressionNode *get_expression() const { return expr; }
523
524        InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
525        ExpressionNode *get_designators() const { return designator; }
526
527        InitializerNode *next_init() const { return kids; }
528
529        void print( std::ostream &, int indent = 0 ) const;
530        void printOneLine( std::ostream & ) const;
531
532        virtual Initializer *build() const;
533  private:
534        ExpressionNode *expr;
535        bool aggregate;
536        ExpressionNode *designator; // may be list
537        InitializerNode *kids;
538};
539
540class CompoundLiteralNode : public ExpressionNode {
541  public:
542        CompoundLiteralNode( DeclarationNode *type, InitializerNode *kids );
543        CompoundLiteralNode( const CompoundLiteralNode &type );
544        ~CompoundLiteralNode();
545
546        virtual CompoundLiteralNode *clone() const;
547
548        DeclarationNode *get_type() const { return type; }
549        CompoundLiteralNode *set_type( DeclarationNode *t ) { type = t; return this; }
550
551        InitializerNode *get_initializer() const { return kids; }
552        CompoundLiteralNode *set_initializer( InitializerNode *k ) { kids = k; return this; }
553
554        void print( std::ostream &, int indent = 0 ) const;
555        void printOneLine( std::ostream &, int indent = 0 ) const;
556
557        virtual Expression *build() const;
558  private:
559        DeclarationNode *type;
560        InitializerNode *kids;
561};
562
563template< typename SynTreeType, typename NodeType >
564void buildList( const NodeType *firstNode, std::list< SynTreeType *> &outputList ) {
565        SemanticError errors;
566        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
567        const NodeType *cur = firstNode;
568
569        while ( cur ) {
570                try {
571                        SynTreeType *result = dynamic_cast< SynTreeType *>( cur->build() );
572                        if ( result ) {
573                                *out++ = result;
574                        } else {
575                        } // if
576                } catch( SemanticError &e ) {
577                        errors.append( e );
578                } // try
579                cur = dynamic_cast< NodeType *>( cur->get_link() );
580        } // while
581        if ( ! errors.isEmpty() ) {
582                throw errors;
583        } // if
584}
585
586// in DeclarationNode.cc
587void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
588void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
589void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
590
591// in ExpressionNode.cc
592ExpressionNode *flattenCommas( ExpressionNode *list );
593ExpressionNode *tupleContents( ExpressionNode *tuple );
594
595#endif // PARSENODE_H
596
597// Local Variables: //
598// tab-width: 4 //
599// mode: c++ //
600// compile-command: "make install" //
601// End: //
Note: See TracBrowser for help on using the repository browser.