source: src/Parser/ParseNode.h @ 59db689

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

constant types, first attempt

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