source: src/Parser/ParseNode.h @ b1848a0

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

more refactoring of parser code

  • Property mode set to 100644
File size: 15.0 KB
RevLine 
[b87a5ed]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//
[974906e2]7// ParseNode.h --
[b87a5ed]8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 13:28:16 2015
[e04ef3a]11// Last Modified By : Peter A. Buhr
[e82aa9df]12// Last Modified On : Mon Aug 15 14:52:12 2016
13// Update Count     : 512
[b87a5ed]14//
15
[51b7345]16#ifndef PARSENODE_H
17#define PARSENODE_H
18
19#include <string>
20#include <list>
21#include <iterator>
[e04ef3a]22#include <memory>
[51b7345]23
[d3b7937]24#include "Common/utility.h"
[68cd1ce]25#include "Parser/LinkageSpec.h"
[59db689]26#include "SynTree/Type.h"
[e04ef3a]27#include "SynTree/Expression.h"
[2f22cc4]28#include "SynTree/Statement.h"
[68cd1ce]29//#include "SynTree/Declaration.h"
[d3b7937]30#include "Common/UniqueName.h"
[0f8e4ac]31#include "SynTree/Label.h"
[51b7345]32
33class StatementNode;
34class CompoundStmtNode;
35class DeclarationNode;
[d1625f8]36class ExpressionNode;
[51b7345]37class InitializerNode;
38
39// Builder
40class ParseNode {
[bdd516a]41  public:
[59db689]42        ParseNode();
43        ParseNode( const std::string * );
[8e9cbb2]44        ParseNode( const std::string & );                                       // for copy constructing subclasses
[59db689]45        virtual ~ParseNode();
[51b7345]46
[1d4580a]47        ParseNode *get_next() const { return next; }
48        ParseNode *set_next( ParseNode *newlink ) { next = newlink; return this; }
[59db689]49        ParseNode *get_last();
[1d4580a]50        ParseNode *set_last( ParseNode * );
[51b7345]51
[b87a5ed]52        virtual ParseNode *clone() const { return 0; };
[51b7345]53
[e869d663]54        const std::string &get_name() const { return name; }
55        void set_name( const std::string &newValue ) { name = newValue; }
56
[7bf7fb9]57        virtual void print( std::ostream &os, int indent = 0 ) const;
58        virtual void printList( std::ostream &os, int indent = 0 ) const;
[51b7345]59
[2f22cc4]60        ParseNode &operator,( ParseNode & );
[bdd516a]61  protected:
[e869d663]62        std::string name;
[b87a5ed]63        static int indent_by;
[8e9cbb2]64        ParseNode *next;
[51b7345]65};
66
[7bf7fb9]67//##############################################################################
68
[d1625f8]69class InitializerNode : public ParseNode {
70  public:
71        InitializerNode( ExpressionNode *, bool aggrp = false,  ExpressionNode *des = 0 );
72        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
73        ~InitializerNode();
74
75        ExpressionNode *get_expression() const { return expr; }
76
77        InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
78        ExpressionNode *get_designators() const { return designator; }
79
80        InitializerNode *set_maybeConstructed( bool value ) { maybeConstructed = value; return this; }
81        bool get_maybeConstructed() const { return maybeConstructed; }
82
83        InitializerNode *next_init() const { return kids; }
84
85        void print( std::ostream &os, int indent = 0 ) const;
86        void printOneLine( std::ostream & ) const;
87
88        virtual Initializer *build() const;
89  private:
90        ExpressionNode *expr;
91        bool aggregate;
[777bfcf]92        ExpressionNode *designator;                                                     // may be list
[d1625f8]93        InitializerNode *kids;
94        bool maybeConstructed;
95};
96
97//##############################################################################
98
[51b7345]99class ExpressionNode : public ParseNode {
[bdd516a]100  public:
[d1625f8]101        ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
102        ExpressionNode( Expression * expr, const std::string *name ) : ParseNode( name ), expr( expr ) {}
[b87a5ed]103        ExpressionNode( const ExpressionNode &other );
[d1625f8]104        virtual ~ExpressionNode() {}
[51b7345]105
[d1625f8]106        virtual ExpressionNode *clone() const { return 0; }
[51b7345]107
[e04ef3a]108        bool get_extension() const { return extension; }
109        ExpressionNode *set_extension( bool exten ) { extension = exten; return this; }
[51b7345]110
[d1625f8]111        virtual void print( std::ostream &os, int indent = 0 ) const {}
112        virtual void printOneLine( std::ostream &os, int indent = 0 ) const {}
[51b7345]113
[d1625f8]114        virtual Expression *build() const { return expr; }
[bdd516a]115  private:
[e04ef3a]116        bool extension = false;
[d1625f8]117        Expression *expr;
[e04ef3a]118};
119
120template< typename T >
121struct maybeBuild_t<Expression, T> {
122        static inline Expression * doit( const T *orig ) {
123                if ( orig ) {
124                        Expression *p = orig->build();
125                        p->set_extension( orig->get_extension() );
126                        return p;
127                } else {
128                        return 0;
129                } // if
130        }
[51b7345]131};
132
[d9e2280]133enum class OperKinds {
134        // diadic
[7bf7fb9]135        SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
[d9e2280]136        BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
137        Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
138        Index, Range,
139        // monadic
140        UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
141        Ctor, Dtor,
[51b7345]142};
143
[e82aa9df]144struct LabelNode {
145        std::list< Label > labels;
146};
147
148Expression *build_constantInteger( std::string &str );
149Expression *build_constantFloat( std::string &str );
150Expression *build_constantChar( std::string &str );
151ConstantExpr *build_constantStr( std::string &str );
152
153NameExpr *build_varref( const std::string *name, bool labelp = false );
154Expression *build_typevalue( DeclarationNode *decl );
155
[d1625f8]156Expression *build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node );
157Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member );
158Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member );
[064e3ff]159Expression *build_addressOf( ExpressionNode *expr_node );
[d1625f8]160Expression *build_sizeOfexpr( ExpressionNode *expr_node );
161Expression *build_sizeOftype( DeclarationNode *decl_node );
162Expression *build_alignOfexpr( ExpressionNode *expr_node );
163Expression *build_alignOftype( DeclarationNode *decl_node );
164Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member );
[51e076e]165Expression *build_and( ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
166Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind );
[d9e2280]167Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node );
168Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node );
169Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
170Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
[51e076e]171Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 );
172Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
[d1625f8]173Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node );
174Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node );
175Expression *build_tuple( ExpressionNode * expr_node = 0 );
176Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node );
[d9e2280]177Expression *build_range( ExpressionNode * low, ExpressionNode *high );
[e82aa9df]178Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand );
[d1625f8]179Expression *build_valexpr( StatementNode *s );
180Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids );
[51b7345]181
[7bf7fb9]182//##############################################################################
183
[51b7345]184class TypeData;
185
[bdd516a]186class DeclarationNode : public ParseNode {
187  public:
[1db21619]188        enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
[68cd1ce]189        enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
[b87a5ed]190        enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
[68cd1ce]191        enum Modifier  { Signed, Unsigned, Short, Long };
[4040425]192        enum Aggregate { Struct, Union, Trait };
[b87a5ed]193        enum TypeClass { Type, Dtype, Ftype };
[90c3b1c]194        enum BuiltinType { Valist };
[b87a5ed]195
[974906e2]196        static const char *storageName[];
[b87a5ed]197        static const char *qualifierName[];
198        static const char *basicTypeName[];
199        static const char *modifierName[];
[68cd1ce]200        static const char *aggregateName[];
[b87a5ed]201        static const char *typeClassName[];
[90c3b1c]202        static const char *builtinTypeName[];
[b87a5ed]203
[7f5566b]204        static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
[b87a5ed]205        static DeclarationNode *newQualifier( Qualifier );
206        static DeclarationNode *newStorageClass( StorageClass );
207        static DeclarationNode *newBasicType( BasicType );
208        static DeclarationNode *newModifier( Modifier );
209        static DeclarationNode *newForall( DeclarationNode *);
210        static DeclarationNode *newFromTypedef( std::string *);
[5d125e4]211        static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body );
[b87a5ed]212        static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
213        static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
214        static DeclarationNode *newName( std::string *);
[59db689]215        static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
[b87a5ed]216        static DeclarationNode *newTypeParam( TypeClass, std::string *);
[4040425]217        static DeclarationNode *newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
218        static DeclarationNode *newTraitUse( std::string *name, ExpressionNode *params );
[b87a5ed]219        static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams );
220        static DeclarationNode *newPointer( DeclarationNode *qualifiers );
221        static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic );
222        static DeclarationNode *newVarArray( DeclarationNode *qualifiers );
223        static DeclarationNode *newBitfield( ExpressionNode *size );
224        static DeclarationNode *newTuple( DeclarationNode *members );
225        static DeclarationNode *newTypeof( ExpressionNode *expr );
[59db689]226        static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
227        static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
[90c3b1c]228        static DeclarationNode *newBuiltinType( BuiltinType );
[b87a5ed]229
230        DeclarationNode *addQualifiers( DeclarationNode *);
231        DeclarationNode *copyStorageClasses( DeclarationNode *);
232        DeclarationNode *addType( DeclarationNode *);
233        DeclarationNode *addTypedef();
234        DeclarationNode *addAssertions( DeclarationNode *);
235        DeclarationNode *addName( std::string *);
236        DeclarationNode *addBitfield( ExpressionNode *size );
237        DeclarationNode *addVarArgs();
238        DeclarationNode *addFunctionBody( StatementNode *body );
239        DeclarationNode *addOldDeclList( DeclarationNode *list );
240        DeclarationNode *addPointer( DeclarationNode *qualifiers );
241        DeclarationNode *addArray( DeclarationNode *array );
242        DeclarationNode *addNewPointer( DeclarationNode *pointer );
243        DeclarationNode *addNewArray( DeclarationNode *array );
244        DeclarationNode *addParamList( DeclarationNode *list );
[e82aa9df]245        DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions
[b87a5ed]246        DeclarationNode *addInitializer( InitializerNode *init );
247
248        DeclarationNode *cloneType( std::string *newName );
249        DeclarationNode *cloneType( DeclarationNode *existing );
250        DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); }
251        DeclarationNode *cloneBaseType( std::string *newName );
252        DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
253
[de62360d]254        DeclarationNode *appendList( DeclarationNode * );
[b87a5ed]255
256        DeclarationNode *clone() const;
[7bf7fb9]257        void print( std::ostream &os, int indent = 0 ) const;
258        void printList( std::ostream &os, int indent = 0 ) const;
[b87a5ed]259
260        Declaration *build() const;
261        ::Type *buildType() const;
262
263        bool get_hasEllipsis() const;
[5f2f2d7]264        const std::string &get_name() const { return name; }
[b87a5ed]265        LinkageSpec::Type get_linkage() const { return linkage; }
266        DeclarationNode *extractAggregate() const;
[90c3b1c]267        ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
[b87a5ed]268
[7305915]269        bool get_extension() const { return extension; }
270        DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
271
[b87a5ed]272        DeclarationNode();
273        ~DeclarationNode();
[bdd516a]274  private:
[68cd1ce]275        StorageClass buildStorageClass() const;
[de62360d]276        bool buildFuncSpecifier( StorageClass key ) const;
[b87a5ed]277
278        TypeData *type;
279        std::string name;
280        std::list< StorageClass > storageClasses;
[1db21619]281        std::list< std::string > attributes;
[b87a5ed]282        ExpressionNode *bitfieldWidth;
[90c3b1c]283        ExpressionNode *enumeratorValue;
[b87a5ed]284        InitializerNode *initializer;
285        bool hasEllipsis;
286        LinkageSpec::Type linkage;
[7305915]287        bool extension = false;
[b87a5ed]288
289        static UniqueName anonymous;
[1db21619]290}; // DeclarationNode
[51b7345]291
[d1625f8]292Type *buildType( TypeData *type );
293
[7bf7fb9]294//##############################################################################
295
[51b7345]296class StatementNode : public ParseNode {
[bdd516a]297  public:
[e82aa9df]298        StatementNode() { stmt = nullptr; }
299        StatementNode( Statement *stmt ) : stmt( stmt ) {}
[b87a5ed]300        StatementNode( DeclarationNode *decl );
[e82aa9df]301        virtual ~StatementNode() {}
[51b7345]302
[e82aa9df]303        virtual StatementNode *clone() const { assert( false ); return nullptr; }
[2f22cc4]304        virtual Statement *build() const { return stmt; }
305
[e82aa9df]306        virtual StatementNode *add_label( const std::string * name ) {
[2f22cc4]307                stmt->get_labels().emplace_back( *name );
308                return this;
309        }
310
[1d4580a]311        virtual StatementNode *append_last_case( StatementNode * );
312
[2f22cc4]313        virtual void print( std::ostream &os, int indent = 0 ) {}
314        virtual void printList( std::ostream &os, int indent = 0 ) {}
315  private:
316        Statement *stmt;
317}; // StatementNode
318
[1d4580a]319Statement *build_expr( ExpressionNode *ctl );
320
[2f22cc4]321struct ForCtl {
322        ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
[e82aa9df]323                init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
[2f22cc4]324        ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
[e82aa9df]325                init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
[2f22cc4]326
327        StatementNode *init;
328        ExpressionNode *condition;
329        ExpressionNode *change;
330};
331
332Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
333Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
[8cc5cb0]334Statement *build_case( ExpressionNode *ctl );
335Statement *build_default();
[2f22cc4]336Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
337Statement *build_for( ForCtl *forctl, StatementNode *stmt );
[321f55d]338Statement *build_branch( std::string identifier, BranchStmt::Type kind );
[8cc5cb0]339Statement *build_computedgoto( ExpressionNode *ctl );
340Statement *build_return( ExpressionNode *ctl );
341Statement *build_throw( ExpressionNode *ctl );
[1d4580a]342Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt );
343Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false );
344Statement *build_finally( StatementNode *stmt );
[e82aa9df]345Statement *build_compound( StatementNode *first );
346Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
[7f5566b]347
[7bf7fb9]348//##############################################################################
349
[51b7345]350template< typename SynTreeType, typename NodeType >
[2f22cc4]351void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
[b87a5ed]352        SemanticError errors;
353        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
354        const NodeType *cur = firstNode;
355
356        while ( cur ) {
357                try {
[e04ef3a]358//                      SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::result_of<decltype(&NodeType::build)(NodeType)>::type>( cur ) );
359                        SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::pointer_traits<decltype(cur->build())>::element_type>( cur ) );
[b87a5ed]360                        if ( result ) {
361                                *out++ = result;
362                        } else {
363                        } // if
364                } catch( SemanticError &e ) {
365                        errors.append( e );
366                } // try
[1d4580a]367                cur = dynamic_cast< NodeType *>( cur->get_next() );
[b87a5ed]368        } // while
[a32b204]369        if ( ! errors.isEmpty() ) {
[b87a5ed]370                throw errors;
371        } // if
[51b7345]372}
373
374// in DeclarationNode.cc
[59db689]375void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
[bdd516a]376void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
[59db689]377void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
[51b7345]378
[bdd516a]379#endif // PARSENODE_H
[51b7345]380
381// Local Variables: //
[b87a5ed]382// tab-width: 4 //
383// mode: c++ //
384// compile-command: "make install" //
[51b7345]385// End: //
Note: See TracBrowser for help on using the repository browser.