source: src/Parser/ParseNode.h @ d58ebf3

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 d58ebf3 was 51b1202, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

move constant/string to unary_expression, temporarily separate asm and local label, introduce DesignatorNode?

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