source: translator/Parser/ParseNode.h @ bdd516a

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

fixed sizeof type variable, find lowest cost alternative for sizeof expression, removed unused classes, added compiler flag, remove temporary file for -CFA, formatting

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