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
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 : Mon Aug 15 14:52:12 2016
13// Update Count     : 512
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/Statement.h"
29//#include "SynTree/Declaration.h"
30#include "Common/UniqueName.h"
31#include "SynTree/Label.h"
32
33class StatementNode;
34class CompoundStmtNode;
35class DeclarationNode;
36class ExpressionNode;
37class InitializerNode;
38
39// Builder
40class ParseNode {
41  public:
42        ParseNode();
43        ParseNode( const std::string * );
44        ParseNode( const std::string & );                                       // for copy constructing subclasses
45        virtual ~ParseNode();
46
47        ParseNode *get_next() const { return next; }
48        ParseNode *set_next( ParseNode *newlink ) { next = newlink; return this; }
49        ParseNode *get_last();
50        ParseNode *set_last( ParseNode * );
51
52        virtual ParseNode *clone() const { return 0; };
53
54        const std::string &get_name() const { return name; }
55        void set_name( const std::string &newValue ) { name = newValue; }
56
57        virtual void print( std::ostream &os, int indent = 0 ) const;
58        virtual void printList( std::ostream &os, int indent = 0 ) const;
59
60        ParseNode &operator,( ParseNode & );
61  protected:
62        std::string name;
63        static int indent_by;
64        ParseNode *next;
65};
66
67//##############################################################################
68
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;
92        ExpressionNode *designator;                                                     // may be list
93        InitializerNode *kids;
94        bool maybeConstructed;
95};
96
97//##############################################################################
98
99class ExpressionNode : public ParseNode {
100  public:
101        ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
102        ExpressionNode( Expression * expr, const std::string *name ) : ParseNode( name ), expr( expr ) {}
103        ExpressionNode( const ExpressionNode &other );
104        virtual ~ExpressionNode() {}
105
106        virtual ExpressionNode *clone() const { return 0; }
107
108        bool get_extension() const { return extension; }
109        ExpressionNode *set_extension( bool exten ) { extension = exten; return this; }
110
111        virtual void print( std::ostream &os, int indent = 0 ) const {}
112        virtual void printOneLine( std::ostream &os, int indent = 0 ) const {}
113
114        virtual Expression *build() const { return expr; }
115  private:
116        bool extension = false;
117        Expression *expr;
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        }
131};
132
133enum class OperKinds {
134        // diadic
135        SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
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,
142};
143
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
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 );
159Expression *build_addressOf( ExpressionNode *expr_node );
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 );
165Expression *build_and( ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
166Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind );
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 );
171Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 );
172Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
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 );
177Expression *build_range( ExpressionNode * low, ExpressionNode *high );
178Expression *build_asmexpr( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand );
179Expression *build_valexpr( StatementNode *s );
180Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids );
181
182//##############################################################################
183
184class TypeData;
185
186class DeclarationNode : public ParseNode {
187  public:
188        enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
189        enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
190        enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
191        enum Modifier  { Signed, Unsigned, Short, Long };
192        enum Aggregate { Struct, Union, Trait };
193        enum TypeClass { Type, Dtype, Ftype };
194        enum BuiltinType { Valist };
195
196        static const char *storageName[];
197        static const char *qualifierName[];
198        static const char *basicTypeName[];
199        static const char *modifierName[];
200        static const char *aggregateName[];
201        static const char *typeClassName[];
202        static const char *builtinTypeName[];
203
204        static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
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 *);
211        static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body );
212        static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
213        static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
214        static DeclarationNode *newName( std::string *);
215        static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
216        static DeclarationNode *newTypeParam( TypeClass, std::string *);
217        static DeclarationNode *newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
218        static DeclarationNode *newTraitUse( std::string *name, ExpressionNode *params );
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 );
226        static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
227        static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
228        static DeclarationNode *newBuiltinType( BuiltinType );
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 );
245        DeclarationNode *addIdList( DeclarationNode *list ); // old-style functions
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
254        DeclarationNode *appendList( DeclarationNode * );
255
256        DeclarationNode *clone() const;
257        void print( std::ostream &os, int indent = 0 ) const;
258        void printList( std::ostream &os, int indent = 0 ) const;
259
260        Declaration *build() const;
261        ::Type *buildType() const;
262
263        bool get_hasEllipsis() const;
264        const std::string &get_name() const { return name; }
265        LinkageSpec::Type get_linkage() const { return linkage; }
266        DeclarationNode *extractAggregate() const;
267        ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
268
269        bool get_extension() const { return extension; }
270        DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
271
272        DeclarationNode();
273        ~DeclarationNode();
274  private:
275        StorageClass buildStorageClass() const;
276        bool buildFuncSpecifier( StorageClass key ) const;
277
278        TypeData *type;
279        std::string name;
280        std::list< StorageClass > storageClasses;
281        std::list< std::string > attributes;
282        ExpressionNode *bitfieldWidth;
283        ExpressionNode *enumeratorValue;
284        InitializerNode *initializer;
285        bool hasEllipsis;
286        LinkageSpec::Type linkage;
287        bool extension = false;
288
289        static UniqueName anonymous;
290}; // DeclarationNode
291
292Type *buildType( TypeData *type );
293
294//##############################################################################
295
296class StatementNode : public ParseNode {
297  public:
298        StatementNode() { stmt = nullptr; }
299        StatementNode( Statement *stmt ) : stmt( stmt ) {}
300        StatementNode( DeclarationNode *decl );
301        virtual ~StatementNode() {}
302
303        virtual StatementNode *clone() const { assert( false ); return nullptr; }
304        virtual Statement *build() const { return stmt; }
305
306        virtual StatementNode *add_label( const std::string * name ) {
307                stmt->get_labels().emplace_back( *name );
308                return this;
309        }
310
311        virtual StatementNode *append_last_case( StatementNode * );
312
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
319Statement *build_expr( ExpressionNode *ctl );
320
321struct ForCtl {
322        ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
323                init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
324        ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
325                init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
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 );
334Statement *build_case( ExpressionNode *ctl );
335Statement *build_default();
336Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
337Statement *build_for( ForCtl *forctl, StatementNode *stmt );
338Statement *build_branch( std::string identifier, BranchStmt::Type kind );
339Statement *build_computedgoto( ExpressionNode *ctl );
340Statement *build_return( ExpressionNode *ctl );
341Statement *build_throw( ExpressionNode *ctl );
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 );
345Statement *build_compound( StatementNode *first );
346Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
347
348//##############################################################################
349
350template< typename SynTreeType, typename NodeType >
351void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
352        SemanticError errors;
353        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
354        const NodeType *cur = firstNode;
355
356        while ( cur ) {
357                try {
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 ) );
360                        if ( result ) {
361                                *out++ = result;
362                        } else {
363                        } // if
364                } catch( SemanticError &e ) {
365                        errors.append( e );
366                } // try
367                cur = dynamic_cast< NodeType *>( cur->get_next() );
368        } // while
369        if ( ! errors.isEmpty() ) {
370                throw errors;
371        } // if
372}
373
374// in DeclarationNode.cc
375void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
376void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
377void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
378
379#endif // PARSENODE_H
380
381// Local Variables: //
382// tab-width: 4 //
383// mode: c++ //
384// compile-command: "make install" //
385// End: //
Note: See TracBrowser for help on using the repository browser.