source: src/Parser/ParseNode.h @ 0ada2f0

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

designator parsing and AST building

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