source: src/Parser/ParseNode.h @ bd85400

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 bd85400 was 5721a6d, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

correctly set type for complex constants, consolidate function name tables, add offsetof, refactor printing complex constants to use basic types

  • Property mode set to 100644
File size: 18.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 : Mon Feb  1 13:32:32 2016
13// Update Count     : 184
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 ) : 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, Context };
332        enum TypeClass { Type, Dtype, Ftype };
333
334        static const char *storageName[]; 
335        static const char *qualifierName[];
336        static const char *basicTypeName[];
337        static const char *modifierName[];
338        static const char *aggregateName[];
339        static const char *typeClassName[];
340
341        static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
342        static DeclarationNode *newQualifier( Qualifier );
343        static DeclarationNode *newStorageClass( StorageClass );
344        static DeclarationNode *newBasicType( BasicType );
345        static DeclarationNode *newModifier( Modifier );
346        static DeclarationNode *newForall( DeclarationNode *);
347        static DeclarationNode *newFromTypedef( std::string *);
348        static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields );
349        static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
350        static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
351        static DeclarationNode *newName( std::string *);
352        static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
353        static DeclarationNode *newTypeParam( TypeClass, std::string *);
354        static DeclarationNode *newContext( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
355        static DeclarationNode *newContextUse( std::string *name, ExpressionNode *params );
356        static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams );
357        static DeclarationNode *newPointer( DeclarationNode *qualifiers );
358        static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic );
359        static DeclarationNode *newVarArray( DeclarationNode *qualifiers );
360        static DeclarationNode *newBitfield( ExpressionNode *size );
361        static DeclarationNode *newTuple( DeclarationNode *members );
362        static DeclarationNode *newTypeof( ExpressionNode *expr );
363        static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
364        static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
365
366        DeclarationNode *addQualifiers( DeclarationNode *);
367        DeclarationNode *copyStorageClasses( DeclarationNode *);
368        DeclarationNode *addType( DeclarationNode *);
369        DeclarationNode *addTypedef();
370        DeclarationNode *addAssertions( DeclarationNode *);
371        DeclarationNode *addName( std::string *);
372        DeclarationNode *addBitfield( ExpressionNode *size );
373        DeclarationNode *addVarArgs();
374        DeclarationNode *addFunctionBody( StatementNode *body );
375        DeclarationNode *addOldDeclList( DeclarationNode *list );
376        DeclarationNode *addPointer( DeclarationNode *qualifiers );
377        DeclarationNode *addArray( DeclarationNode *array );
378        DeclarationNode *addNewPointer( DeclarationNode *pointer );
379        DeclarationNode *addNewArray( DeclarationNode *array );
380        DeclarationNode *addParamList( DeclarationNode *list );
381        DeclarationNode *addIdList( DeclarationNode *list );       // old-style functions
382        DeclarationNode *addInitializer( InitializerNode *init );
383
384        DeclarationNode *cloneType( std::string *newName );
385        DeclarationNode *cloneType( DeclarationNode *existing );
386        DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); }
387        DeclarationNode *cloneBaseType( std::string *newName );
388        DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
389
390        DeclarationNode *appendList( DeclarationNode * );
391
392        DeclarationNode *clone() const;
393        void print( std::ostream &, int indent = 0 ) const;
394        void printList( std::ostream &, int indent = 0 ) const;
395
396        Declaration *build() const;
397        ::Type *buildType() const;
398
399        bool get_hasEllipsis() const;
400        const std::string &get_name() const { return name; }
401        LinkageSpec::Type get_linkage() const { return linkage; }
402        DeclarationNode *extractAggregate() const;
403
404        DeclarationNode();
405        ~DeclarationNode();
406  private:
407        StorageClass buildStorageClass() const;
408        bool buildFuncSpecifier( StorageClass key ) const;
409
410        TypeData *type;
411        std::string name;
412        std::list< StorageClass > storageClasses;
413        std::list< std::string > attributes;
414        ExpressionNode *bitfieldWidth;
415        InitializerNode *initializer;
416        bool hasEllipsis;
417        LinkageSpec::Type linkage;
418
419        static UniqueName anonymous;
420}; // DeclarationNode
421
422class StatementNode : public ParseNode {
423  public:
424        enum Type { Exp,   If,        Switch,  Case,    Default,  Choose,   Fallthru, 
425                                While, Do,        For,
426                                Goto,  Continue,  Break,   Return,  Throw,
427                                Try,   Catch,     Finally, Asm,
428                                Decl
429        };
430
431        StatementNode();
432        StatementNode( const std::string *name );
433        StatementNode( Type t, ExpressionNode *control = 0, StatementNode *block = 0 );
434        StatementNode( Type t, std::string *target );
435        StatementNode( DeclarationNode *decl );
436
437        ~StatementNode();
438
439        static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false );
440
441        StatementNode *set_block( StatementNode *b ) {  block = b; return this; }
442        StatementNode *get_block() const { return block; }
443
444        void set_control( ExpressionNode *c ) { control = c; }
445        ExpressionNode *get_control() const { return control; }
446
447        StatementNode::Type get_type() const { return type; }
448
449        StatementNode *add_label( const std::string * );
450        const std::list<std::string> &get_labels() const { return labels; }
451
452        void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; }
453        void setCatchRest( bool newVal ) { isCatchRest = newVal; }
454
455        std::string get_target() const;
456
457        StatementNode *add_controlexp( ExpressionNode * );
458        StatementNode *append_block( StatementNode * );
459        StatementNode *append_last_case( StatementNode * );
460
461        void print( std::ostream &, int indent = 0) const;
462        virtual StatementNode *clone() const;
463        virtual Statement *build() const;
464  private:
465        static const char *StType[];
466        Type type;
467        ExpressionNode *control;
468        StatementNode *block;
469        std::list<std::string> labels;
470        std::string *target;                            // target label for jump statements
471        DeclarationNode *decl;
472        bool isCatchRest;
473}; // StatementNode
474
475class CompoundStmtNode : public StatementNode {
476  public:
477        CompoundStmtNode();
478        CompoundStmtNode( const std::string * );
479        CompoundStmtNode( StatementNode * );
480        ~CompoundStmtNode();
481
482        void add_statement( StatementNode * );
483
484        void print( std::ostream &, int indent = 0 ) const;
485        virtual Statement *build() const;
486  private:
487        StatementNode *first, *last;
488};
489
490class AsmStmtNode : public StatementNode {
491  public:
492        AsmStmtNode( Type, bool voltile, ConstantNode *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ConstantNode *clobber = 0, LabelNode *gotolabels = 0 );
493        ~AsmStmtNode();
494
495        void print( std::ostream &, int indent = 0 ) const;
496        Statement *build() const;
497  private:
498        bool voltile;
499        ConstantNode *instruction;
500        ExpressionNode *output, *input;
501        ConstantNode *clobber;
502        std::list<std::string> gotolabels;
503};
504
505class NullStmtNode : public CompoundStmtNode {
506  public:
507        Statement *build() const;
508        void print( std::ostream &, int indent = 0 ) const;
509};
510
511class InitializerNode : public ParseNode {
512  public:
513        InitializerNode( ExpressionNode *, bool aggrp = false,  ExpressionNode *des = 0 );
514        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
515        ~InitializerNode();
516
517        ExpressionNode *get_expression() const { return expr; }
518
519        InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
520        ExpressionNode *get_designators() const { return designator; }
521
522        InitializerNode *next_init() const { return kids; }
523
524        void print( std::ostream &, int indent = 0 ) const;
525        void printOneLine( std::ostream & ) const;
526
527        virtual Initializer *build() const;
528  private:
529        ExpressionNode *expr;
530        bool aggregate;
531        ExpressionNode *designator; // may be list
532        InitializerNode *kids;
533};
534
535template< typename SynTreeType, typename NodeType >
536void buildList( const NodeType *firstNode, std::list< SynTreeType *> &outputList ) {
537        SemanticError errors;
538        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
539        const NodeType *cur = firstNode;
540
541        while ( cur ) {
542                try {
543                        SynTreeType *result = dynamic_cast< SynTreeType *>( cur->build() );
544                        if ( result ) {
545                                *out++ = result;
546                        } else {
547                        } // if
548                } catch( SemanticError &e ) {
549                        errors.append( e );
550                } // try
551                cur = dynamic_cast< NodeType *>( cur->get_link() );
552        } // while
553        if ( ! errors.isEmpty() ) {
554                throw errors;
555        } // if
556}
557
558// in DeclarationNode.cc
559void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
560void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
561void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
562
563// in ExpressionNode.cc
564ExpressionNode *flattenCommas( ExpressionNode *list );
565ExpressionNode *tupleContents( ExpressionNode *tuple );
566
567#endif // PARSENODE_H
568
569// Local Variables: //
570// tab-width: 4 //
571// mode: c++ //
572// compile-command: "make install" //
573// End: //
Note: See TracBrowser for help on using the repository browser.