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

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

fix constant types, remove unnecessary string copying, work on regression testing, fix several memory leaks

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