source: src/Parser/ParseNode.h @ 7880579

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

more refactoring of parser code

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