source: src/Parser/ParseNode.h @ 658fafe4

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 658fafe4 was 658fafe4, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Remove more dead code in the parser

  • Property mode set to 100644
File size: 20.3 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 : Sun Jul 24 02:17:00 2016
13// Update Count     : 269
14//
15
16#ifndef PARSENODE_H
17#define PARSENODE_H
18
19#include <string>
20#include <list>
21#include <iterator>
22#include <memory>
23
24#include "Common/utility.h"
25#include "Parser/LinkageSpec.h"
26#include "SynTree/Type.h"
27#include "SynTree/Expression.h"
28//#include "SynTree/Declaration.h"
29#include "Common/UniqueName.h"
30#include "SynTree/Label.h"
31
32class ExpressionNode;
33class CompositeExprNode;
34class CommaExprNode;
35class StatementNode;
36class CompoundStmtNode;
37class DeclarationNode;
38class InitializerNode;
39
40// Builder
41class ParseNode {
42  public:
43        ParseNode();
44        ParseNode( const std::string * );
45        ParseNode( const std::string & );                                       // for copy constructing subclasses
46        virtual ~ParseNode();
47
48        ParseNode *get_link() const { return next; }
49        ParseNode *get_last();
50        ParseNode *set_link( ParseNode * );
51        void set_next( ParseNode *newlink ) { next = newlink; }
52
53        virtual ParseNode *clone() const { return 0; };
54
55        const std::string &get_name() const { return name; }
56        void set_name( const std::string &newValue ) { name = newValue; }
57
58        virtual void print( std::ostream &, int indent = 0 ) const;
59        virtual void printList( std::ostream &, int indent = 0 ) const;
60
61        ParseNode &operator,( ParseNode &);
62  protected:
63        std::string name;
64        static int indent_by;
65        ParseNode *next;
66};
67
68ParseNode *mkList( ParseNode & );
69
70class ExpressionNode : public ParseNode {
71  public:
72        ExpressionNode();
73        ExpressionNode( const std::string * );
74        ExpressionNode( const ExpressionNode &other );
75        virtual ~ExpressionNode() { delete argName; } // cannot delete argName because it might be referenced elsewhere
76
77        virtual ExpressionNode *clone() const = 0;
78
79        // virtual CommaExprNode *add_to_list( ExpressionNode * );
80
81        ExpressionNode *get_argName() const { return argName; }
82        ExpressionNode *set_argName( const std::string *aName );
83        ExpressionNode *set_argName( ExpressionNode *aDesignator );
84        bool get_extension() const { return extension; }
85        ExpressionNode *set_extension( bool exten ) { extension = exten; return this; }
86
87        virtual void print( std::ostream &, int indent = 0) const = 0;
88        virtual void printOneLine( std::ostream &, int indent = 0) const = 0;
89
90        virtual Expression *build() const = 0;
91  protected:
92        void printDesignation ( std::ostream &, int indent = 0) const;
93  private:
94        ExpressionNode *argName = 0;
95        bool extension = false;
96};
97
98template< typename T >
99struct maybeBuild_t<Expression, T> {
100        static inline Expression * doit( const T *orig ) {
101                if ( orig ) {
102                        Expression *p = orig->build();
103                        p->set_extension( orig->get_extension() );
104                        return p;
105                } else {
106                        return 0;
107                } // if
108        }
109};
110
111// NullExprNode is used in tuples as a place-holder where a tuple component is omitted e.g., [ 2, , 3 ]
112class NullExprNode : public ExpressionNode {
113  public:
114        NullExprNode();
115
116        virtual NullExprNode *clone() const;
117
118        virtual void print( std::ostream &, int indent = 0) const;
119        virtual void printOneLine( std::ostream &, int indent = 0) const;
120
121        virtual Expression *build() const;
122};
123
124class ConstantNode : public ExpressionNode {
125  public:
126        enum Type { Integer, Float, Character, String };
127
128        ConstantNode( ConstantExpr * );
129        ConstantNode( const ConstantNode &other ) : expr( other.expr->clone() ) {};
130        ~ConstantNode() { delete expr; }
131
132        virtual ConstantNode *clone() const { return new ConstantNode( *this ); }
133        virtual void print( std::ostream &, int indent = 0) const;
134        virtual void printOneLine( std::ostream &, int indent = 0) const;
135
136        ConstantNode *appendstr( const std::string *newValue );
137
138        Expression *build() const;
139  private:
140        ConstantExpr *expr;
141};
142
143ConstantNode *makeConstantInteger( std::string & );
144ConstantNode *makeConstantFloat( std::string & );
145ConstantNode *makeConstantChar( std::string & );
146ConstantNode *makeConstantStr( std::string & );
147
148class VarRefNode : public ExpressionNode {
149  public:
150        VarRefNode();
151        VarRefNode( const std::string *, bool isLabel = false );
152        VarRefNode( const VarRefNode &other );
153
154        virtual Expression *build() const ;
155
156        virtual VarRefNode *clone() const { return new VarRefNode( *this ); }
157
158        virtual void print( std::ostream &, int indent = 0 ) const;
159        virtual void printOneLine( std::ostream &, int indent = 0 ) const;
160  private:
161        bool isLabel;
162};
163
164class DesignatorNode : public ExpressionNode {
165  public:
166        DesignatorNode( ExpressionNode *expr, bool isArrayIndex = false );
167        DesignatorNode( const DesignatorNode &other );
168
169        virtual Expression *build() const ;
170        virtual DesignatorNode *clone() const { return new DesignatorNode( *this ); }
171
172        virtual void print( std::ostream &, int indent = 0 ) const;
173        virtual void printOneLine( std::ostream &, int indent = 0 ) const;
174  private:
175        bool isArrayIndex;
176};
177
178class TypeValueNode : public ExpressionNode {
179  public:
180        TypeValueNode( DeclarationNode * );
181        TypeValueNode( const TypeValueNode &other );
182
183        DeclarationNode *get_decl() const { return decl; }
184
185        virtual Expression *build() const ;
186
187        virtual TypeValueNode *clone() const { return new TypeValueNode( *this ); }
188
189        virtual void print( std::ostream &, int indent = 0) const;
190        virtual void printOneLine( std::ostream &, int indent = 0) const;
191  private:
192        DeclarationNode *decl;
193};
194
195class OperatorNode : public ExpressionNode {
196  public:
197        enum Type { TupleC, Comma, TupleFieldSel, // n-adic
198                                // triadic
199                                Cond, NCond,
200                                // diadic
201                                SizeOf, AlignOf, OffsetOf, Attr, Plus, Minus, Mul, Div, Mod, Or, And,
202                                BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
203                                Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
204                                Index, FieldSel, PFieldSel, Range,
205                                // monadic
206                                UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
207                                Ctor, Dtor,
208        };
209
210        OperatorNode( Type t );
211        OperatorNode( const OperatorNode &other );
212        virtual ~OperatorNode();
213
214        virtual OperatorNode *clone() const { return new OperatorNode( *this ); }
215
216        Type get_type() const;
217        const char *get_typename() const;
218
219        virtual void print( std::ostream &, int indent = 0) const;
220        virtual void printOneLine( std::ostream &, int indent = 0) const;
221
222        virtual Expression *build() const { return 0; }
223  private:
224        Type type;
225};
226
227class CompositeExprNode : public ExpressionNode {
228  public:
229        CompositeExprNode();
230        CompositeExprNode( const std::string * );
231        CompositeExprNode( ExpressionNode *f, ExpressionNode *args = 0 );
232        CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2 );
233        CompositeExprNode( const CompositeExprNode &other );
234        virtual ~CompositeExprNode();
235
236        virtual CompositeExprNode *clone() const { return new CompositeExprNode( *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        void set_function( ExpressionNode * );
243        void set_args( ExpressionNode * );
244
245        void add_arg( ExpressionNode * );
246
247        ExpressionNode *get_function() const;
248        ExpressionNode *get_args() const;
249  private:
250        ExpressionNode *function;
251        ExpressionNode *arguments;
252};
253
254class AsmExprNode : public ExpressionNode {
255  public:
256        AsmExprNode();
257        AsmExprNode( ExpressionNode *inout, ConstantNode *constraint, ExpressionNode *operand ) : inout( inout ), constraint( constraint ), operand( operand ) {}
258        virtual ~AsmExprNode() { delete inout; delete constraint; delete operand; }
259
260        virtual AsmExprNode *clone() const { return new AsmExprNode( *this ); }
261        virtual Expression *build() const;
262
263        virtual void print( std::ostream &, int indent = 0) const;
264        virtual void printOneLine( std::ostream &, int indent = 0) const;
265
266        ExpressionNode *get_inout() const { return inout; };
267        void set_inout( ExpressionNode *newValue ) { inout = newValue; }
268
269        ConstantNode *get_constraint() const { return constraint; };
270        void set_constraint( ConstantNode *newValue ) { constraint = newValue; }
271
272        ExpressionNode *get_operand() const { return operand; };
273        void set_operand( ExpressionNode *newValue ) { operand = newValue; }
274  private:
275        ExpressionNode *inout;
276        ConstantNode *constraint;
277        ExpressionNode *operand;
278};
279
280class LabelNode : public ExpressionNode {
281  public:
282        virtual Expression *build() const { return NULL; }
283        virtual LabelNode *clone() const { return new LabelNode( *this ); }
284
285        virtual void print( std::ostream &, int indent = 0) const;
286        virtual void printOneLine( std::ostream &, int indent = 0) const;
287
288        const std::list< Label > &get_labels() const { return labels; };
289        void append_label( std::string *label ) { labels.push_back( *label ); delete label; }
290  private:
291        std::list< Label > labels;
292};
293
294class ForCtlExprNode : public ExpressionNode {
295  public:
296        ForCtlExprNode( ParseNode *, ExpressionNode *, ExpressionNode * ) throw ( SemanticError );
297        ForCtlExprNode( const ForCtlExprNode &other );
298        ~ForCtlExprNode();
299
300        StatementNode *get_init() const { return init; }
301        ExpressionNode *get_condition() const { return condition; }
302        ExpressionNode *get_change() const { return change; }
303
304        virtual ForCtlExprNode *clone() const { return new ForCtlExprNode( *this ); }
305        virtual Expression *build() const;
306
307        virtual void print( std::ostream &, int indent = 0 ) const;
308        virtual void printOneLine( std::ostream &, int indent = 0 ) const;
309  private:
310        StatementNode *init;
311        ExpressionNode *condition;
312        ExpressionNode *change;
313};
314
315class ValofExprNode : public ExpressionNode {
316  public:
317        ValofExprNode();
318        ValofExprNode( StatementNode *s = 0 );
319        ValofExprNode( const ValofExprNode &other );
320        ~ValofExprNode();
321
322        virtual ValofExprNode *clone() const { return new ValofExprNode( *this ); }
323
324        StatementNode *get_body() const { return body; }
325        void print( std::ostream &, int indent = 0 ) const;
326        void printOneLine( std::ostream &, int indent = 0 ) const;
327        Expression *build() const;
328
329  private:
330        StatementNode *body;
331};
332
333class TypeData;
334
335class DeclarationNode : public ParseNode {
336  public:
337        enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
338        enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
339        enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
340        enum Modifier  { Signed, Unsigned, Short, Long };
341        enum Aggregate { Struct, Union, Trait };
342        enum TypeClass { Type, Dtype, Ftype };
343        enum BuiltinType { Valist };
344
345        static const char *storageName[];
346        static const char *qualifierName[];
347        static const char *basicTypeName[];
348        static const char *modifierName[];
349        static const char *aggregateName[];
350        static const char *typeClassName[];
351        static const char *builtinTypeName[];
352
353        static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
354        static DeclarationNode *newQualifier( Qualifier );
355        static DeclarationNode *newStorageClass( StorageClass );
356        static DeclarationNode *newBasicType( BasicType );
357        static DeclarationNode *newModifier( Modifier );
358        static DeclarationNode *newForall( DeclarationNode *);
359        static DeclarationNode *newFromTypedef( std::string *);
360        static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body );
361        static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
362        static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
363        static DeclarationNode *newName( std::string *);
364        static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
365        static DeclarationNode *newTypeParam( TypeClass, std::string *);
366        static DeclarationNode *newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
367        static DeclarationNode *newTraitUse( std::string *name, ExpressionNode *params );
368        static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams );
369        static DeclarationNode *newPointer( DeclarationNode *qualifiers );
370        static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic );
371        static DeclarationNode *newVarArray( DeclarationNode *qualifiers );
372        static DeclarationNode *newBitfield( ExpressionNode *size );
373        static DeclarationNode *newTuple( DeclarationNode *members );
374        static DeclarationNode *newTypeof( ExpressionNode *expr );
375        static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
376        static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
377        static DeclarationNode *newBuiltinType( BuiltinType );
378
379        DeclarationNode *addQualifiers( DeclarationNode *);
380        DeclarationNode *copyStorageClasses( DeclarationNode *);
381        DeclarationNode *addType( DeclarationNode *);
382        DeclarationNode *addTypedef();
383        DeclarationNode *addAssertions( DeclarationNode *);
384        DeclarationNode *addName( std::string *);
385        DeclarationNode *addBitfield( ExpressionNode *size );
386        DeclarationNode *addVarArgs();
387        DeclarationNode *addFunctionBody( StatementNode *body );
388        DeclarationNode *addOldDeclList( DeclarationNode *list );
389        DeclarationNode *addPointer( DeclarationNode *qualifiers );
390        DeclarationNode *addArray( DeclarationNode *array );
391        DeclarationNode *addNewPointer( DeclarationNode *pointer );
392        DeclarationNode *addNewArray( DeclarationNode *array );
393        DeclarationNode *addParamList( DeclarationNode *list );
394        DeclarationNode *addIdList( DeclarationNode *list );       // old-style functions
395        DeclarationNode *addInitializer( InitializerNode *init );
396
397        DeclarationNode *cloneType( std::string *newName );
398        DeclarationNode *cloneType( DeclarationNode *existing );
399        DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); }
400        DeclarationNode *cloneBaseType( std::string *newName );
401        DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
402
403        DeclarationNode *appendList( DeclarationNode * );
404
405        DeclarationNode *clone() const;
406        void print( std::ostream &, int indent = 0 ) const;
407        void printList( std::ostream &, int indent = 0 ) const;
408
409        Declaration *build() const;
410        ::Type *buildType() const;
411
412        bool get_hasEllipsis() const;
413        const std::string &get_name() const { return name; }
414        LinkageSpec::Type get_linkage() const { return linkage; }
415        DeclarationNode *extractAggregate() const;
416        ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
417
418        bool get_extension() const { return extension; }
419        DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
420
421        DeclarationNode();
422        ~DeclarationNode();
423  private:
424        StorageClass buildStorageClass() const;
425        bool buildFuncSpecifier( StorageClass key ) const;
426
427        TypeData *type;
428        std::string name;
429        std::list< StorageClass > storageClasses;
430        std::list< std::string > attributes;
431        ExpressionNode *bitfieldWidth;
432        ExpressionNode *enumeratorValue;
433        InitializerNode *initializer;
434        bool hasEllipsis;
435        LinkageSpec::Type linkage;
436        bool extension = false;
437
438        static UniqueName anonymous;
439}; // DeclarationNode
440
441class StatementNode : public ParseNode {
442  public:
443        enum Type { Exp,   If,        Switch,  Case,    Default,  Choose,   Fallthru,
444                                While, Do,        For,
445                                Goto,  Continue,  Break,   Return,  Throw,
446                                Try,   Catch,     Finally, Asm,
447                                Decl
448        };
449
450        StatementNode();
451        StatementNode( const std::string *name );
452        StatementNode( Type t, ExpressionNode *control = 0, StatementNode *block = 0 );
453        StatementNode( Type t, std::string *target );
454        StatementNode( DeclarationNode *decl );
455
456        ~StatementNode();
457
458        static StatementNode *newCatchStmt( DeclarationNode *d = 0, StatementNode *s = 0, bool catchRestP = false );
459
460        StatementNode *set_block( StatementNode *b ) {  block = b; return this; }
461        StatementNode *get_block() const { return block; }
462
463        void set_control( ExpressionNode *c ) { control = c; }
464        ExpressionNode *get_control() const { return control; }
465
466        StatementNode::Type get_type() const { return type; }
467
468        StatementNode *add_label( const std::string * );
469        const std::list<std::string> &get_labels() const { return labels; }
470
471        void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; }
472        void setCatchRest( bool newVal ) { isCatchRest = newVal; }
473
474        std::string get_target() const;
475
476        // StatementNode *add_controlexp( ExpressionNode * );
477        StatementNode *append_block( StatementNode * );
478        StatementNode *append_last_case( StatementNode * );
479
480        void print( std::ostream &, int indent = 0) const;
481        virtual StatementNode *clone() const;
482        virtual Statement *build() const;
483  private:
484        static const char *StType[];
485        Type type;
486        ExpressionNode *control;
487        StatementNode *block;
488        std::list<std::string> labels;
489        std::string *target;                            // target label for jump statements
490        DeclarationNode *decl;
491        bool isCatchRest;
492}; // StatementNode
493
494class CompoundStmtNode : public StatementNode {
495  public:
496        CompoundStmtNode();
497        CompoundStmtNode( const std::string * );
498        CompoundStmtNode( StatementNode * );
499        ~CompoundStmtNode();
500
501        void add_statement( StatementNode * );
502
503        void print( std::ostream &, int indent = 0 ) const;
504        virtual Statement *build() const;
505  private:
506        StatementNode *first, *last;
507};
508
509class AsmStmtNode : public StatementNode {
510  public:
511        AsmStmtNode( Type, bool voltile, ConstantNode *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ConstantNode *clobber = 0, LabelNode *gotolabels = 0 );
512        ~AsmStmtNode();
513
514        void print( std::ostream &, int indent = 0 ) const;
515        Statement *build() const;
516  private:
517        bool voltile;
518        ConstantNode *instruction;
519        ExpressionNode *output, *input;
520        ConstantNode *clobber;
521        std::list< Label > gotolabels;
522};
523
524class InitializerNode : public ParseNode {
525  public:
526        InitializerNode( ExpressionNode *, bool aggrp = false,  ExpressionNode *des = 0 );
527        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
528        ~InitializerNode();
529
530        ExpressionNode *get_expression() const { return expr; }
531
532        InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
533        ExpressionNode *get_designators() const { return designator; }
534
535        InitializerNode *set_maybeConstructed( bool value ) { maybeConstructed = value; return this; }
536        bool get_maybeConstructed() const { return maybeConstructed; }
537
538        InitializerNode *next_init() const { return kids; }
539
540        void print( std::ostream &, int indent = 0 ) const;
541        void printOneLine( std::ostream & ) const;
542
543        virtual Initializer *build() const;
544  private:
545        ExpressionNode *expr;
546        bool aggregate;
547        ExpressionNode *designator; // may be list
548        InitializerNode *kids;
549        bool maybeConstructed;
550};
551
552class CompoundLiteralNode : public ExpressionNode {
553  public:
554        CompoundLiteralNode( DeclarationNode *type, InitializerNode *kids );
555        CompoundLiteralNode( const CompoundLiteralNode &type );
556        ~CompoundLiteralNode();
557
558        virtual CompoundLiteralNode *clone() const;
559
560        DeclarationNode *get_type() const { return type; }
561        CompoundLiteralNode *set_type( DeclarationNode *t ) { type = t; return this; }
562
563        InitializerNode *get_initializer() const { return kids; }
564        CompoundLiteralNode *set_initializer( InitializerNode *k ) { kids = k; return this; }
565
566        void print( std::ostream &, int indent = 0 ) const;
567        void printOneLine( std::ostream &, int indent = 0 ) const;
568
569        virtual Expression *build() const;
570  private:
571        DeclarationNode *type;
572        InitializerNode *kids;
573};
574
575template< typename SynTreeType, typename NodeType >
576void buildList( const NodeType *firstNode, std::list< SynTreeType *> &outputList ) {
577        SemanticError errors;
578        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
579        const NodeType *cur = firstNode;
580
581        while ( cur ) {
582                try {
583//                      SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::result_of<decltype(&NodeType::build)(NodeType)>::type>( cur ) );
584                        SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::pointer_traits<decltype(cur->build())>::element_type>( cur ) );
585                        if ( result ) {
586                                *out++ = result;
587                        } else {
588                        } // if
589                } catch( SemanticError &e ) {
590                        errors.append( e );
591                } // try
592                cur = dynamic_cast< NodeType *>( cur->get_link() );
593        } // while
594        if ( ! errors.isEmpty() ) {
595                throw errors;
596        } // if
597}
598
599// in DeclarationNode.cc
600void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
601void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
602void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
603
604// in ExpressionNode.cc
605ExpressionNode *flattenCommas( ExpressionNode *list );
606ExpressionNode *tupleContents( ExpressionNode *tuple );
607
608#endif // PARSENODE_H
609
610// Local Variables: //
611// tab-width: 4 //
612// mode: c++ //
613// compile-command: "make install" //
614// End: //
Note: See TracBrowser for help on using the repository browser.