source: src/Parser/ParseNode.h @ 3b8e52c

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

more refactoring of parser code

  • Property mode set to 100644
File size: 15.2 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
[cbaee0d]12// Last Modified On : Tue Aug 16 21:32:18 2016
13// Update Count     : 538
[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();
[0da3e2c]46        virtual ParseNode *clone() const = 0;
[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();
[cbaee0d]51        ParseNode *set_last( ParseNode *newlast );
[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 {}
[0da3e2c]57        virtual void printList( std::ostream &os, int indent = 0 ) const {}
[7880579]58  private:
[b87a5ed]59        static int indent_by;
[7880579]60
[0da3e2c]61        ParseNode *next = nullptr;
[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();
[0da3e2c]72        virtual InitializerNode *clone() const { assert( false ); return nullptr; }
[d1625f8]73
74        ExpressionNode *get_expression() const { return expr; }
75
76        InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
77        ExpressionNode *get_designators() const { return designator; }
78
79        InitializerNode *set_maybeConstructed( bool value ) { maybeConstructed = value; return this; }
80        bool get_maybeConstructed() const { return maybeConstructed; }
81
82        InitializerNode *next_init() const { return kids; }
83
84        void print( std::ostream &os, int indent = 0 ) const;
85        void printOneLine( std::ostream & ) const;
86
87        virtual Initializer *build() const;
88  private:
89        ExpressionNode *expr;
90        bool aggregate;
[777bfcf]91        ExpressionNode *designator;                                                     // may be list
[d1625f8]92        InitializerNode *kids;
93        bool maybeConstructed;
94};
95
96//##############################################################################
97
[51b7345]98class ExpressionNode : public ParseNode {
[bdd516a]99  public:
[d1625f8]100        ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
101        ExpressionNode( Expression * expr, const std::string *name ) : ParseNode( name ), expr( expr ) {}
[b87a5ed]102        ExpressionNode( const ExpressionNode &other );
[d1625f8]103        virtual ~ExpressionNode() {}
[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:
[13e3b50]273        // StorageClass buildStorageClass() const;
274        // bool buildFuncSpecifier( StorageClass key ) const;
[b87a5ed]275
276        TypeData *type;
277        std::string name;
[13e3b50]278        // std::list< StorageClass > storageClasses;
279        StorageClass storageClass;
280        bool isInline, isNoreturn;
[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;
[13e3b50]288        std::string error;
[b87a5ed]289
290        static UniqueName anonymous;
[1db21619]291}; // DeclarationNode
[51b7345]292
[d1625f8]293Type *buildType( TypeData *type );
294
[7bf7fb9]295//##############################################################################
296
[51b7345]297class StatementNode : public ParseNode {
[bdd516a]298  public:
[e82aa9df]299        StatementNode() { stmt = nullptr; }
300        StatementNode( Statement *stmt ) : stmt( stmt ) {}
[b87a5ed]301        StatementNode( DeclarationNode *decl );
[e82aa9df]302        virtual ~StatementNode() {}
[51b7345]303
[e82aa9df]304        virtual StatementNode *clone() const { assert( false ); return nullptr; }
[2f22cc4]305        virtual Statement *build() const { return stmt; }
306
[e82aa9df]307        virtual StatementNode *add_label( const std::string * name ) {
[2f22cc4]308                stmt->get_labels().emplace_back( *name );
309                return this;
310        }
311
[1d4580a]312        virtual StatementNode *append_last_case( StatementNode * );
313
[2f22cc4]314        virtual void print( std::ostream &os, int indent = 0 ) {}
315        virtual void printList( std::ostream &os, int indent = 0 ) {}
316  private:
317        Statement *stmt;
318}; // StatementNode
319
[1d4580a]320Statement *build_expr( ExpressionNode *ctl );
321
[2f22cc4]322struct ForCtl {
323        ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
[e82aa9df]324                init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
[2f22cc4]325        ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
[e82aa9df]326                init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
[2f22cc4]327
328        StatementNode *init;
329        ExpressionNode *condition;
330        ExpressionNode *change;
331};
332
333Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
334Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
[8cc5cb0]335Statement *build_case( ExpressionNode *ctl );
336Statement *build_default();
[2f22cc4]337Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
338Statement *build_for( ForCtl *forctl, StatementNode *stmt );
[321f55d]339Statement *build_branch( std::string identifier, BranchStmt::Type kind );
[8cc5cb0]340Statement *build_computedgoto( ExpressionNode *ctl );
341Statement *build_return( ExpressionNode *ctl );
342Statement *build_throw( ExpressionNode *ctl );
[1d4580a]343Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt );
344Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false );
345Statement *build_finally( StatementNode *stmt );
[e82aa9df]346Statement *build_compound( StatementNode *first );
347Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
[7f5566b]348
[7bf7fb9]349//##############################################################################
350
[51b7345]351template< typename SynTreeType, typename NodeType >
[2f22cc4]352void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
[b87a5ed]353        SemanticError errors;
[7880579]354        std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );
[b87a5ed]355        const NodeType *cur = firstNode;
356
357        while ( cur ) {
358                try {
[7880579]359//                      SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) );
360                        SynTreeType *result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
[b87a5ed]361                        if ( result ) {
362                                *out++ = result;
363                        } else {
364                        } // if
365                } catch( SemanticError &e ) {
366                        errors.append( e );
367                } // try
[7880579]368                cur = dynamic_cast< NodeType * >( cur->get_next() );
[b87a5ed]369        } // while
[a32b204]370        if ( ! errors.isEmpty() ) {
[b87a5ed]371                throw errors;
372        } // if
[51b7345]373}
374
375// in DeclarationNode.cc
[59db689]376void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
[7880579]377void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList );
[59db689]378void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
[51b7345]379
[bdd516a]380#endif // PARSENODE_H
[51b7345]381
382// Local Variables: //
[b87a5ed]383// tab-width: 4 //
384// mode: c++ //
385// compile-command: "make install" //
[51b7345]386// End: //
Note: See TracBrowser for help on using the repository browser.