source: src/Parser/ParseNode.h @ 73a28e2

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 73a28e2 was 7ee14bb7, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

formatting, add missing copy constructor for ConstantNode?, support gcc D (double), LD (long double) and iI (imaginary) constant suffixes, delete old examples

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