source: src/Parser/ParseNode.h @ 777bfcf

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

more refactoring of parser code

  • Property mode set to 100644
File size: 17.9 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 Aug 14 16:29:20 2016
13// Update Count     : 483
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
67ParseNode *mkList( ParseNode & );
68
69//##############################################################################
70
71class InitializerNode : public ParseNode {
72  public:
73        InitializerNode( ExpressionNode *, bool aggrp = false,  ExpressionNode *des = 0 );
74        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode *des = 0 );
75        ~InitializerNode();
76
77        ExpressionNode *get_expression() const { return expr; }
78
79        InitializerNode *set_designators( ExpressionNode *des ) { designator = des; return this; }
80        ExpressionNode *get_designators() const { return designator; }
81
82        InitializerNode *set_maybeConstructed( bool value ) { maybeConstructed = value; return this; }
83        bool get_maybeConstructed() const { return maybeConstructed; }
84
85        InitializerNode *next_init() const { return kids; }
86
87        void print( std::ostream &os, int indent = 0 ) const;
88        void printOneLine( std::ostream & ) const;
89
90        virtual Initializer *build() const;
91  private:
92        ExpressionNode *expr;
93        bool aggregate;
94        ExpressionNode *designator;                                                     // may be list
95        InitializerNode *kids;
96        bool maybeConstructed;
97};
98
99//##############################################################################
100
101class ExpressionNode : public ParseNode {
102  public:
103        ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
104        ExpressionNode( Expression * expr, const std::string *name ) : ParseNode( name ), expr( expr ) {}
105        ExpressionNode( const ExpressionNode &other );
106        virtual ~ExpressionNode() {}
107
108        virtual ExpressionNode *clone() const { return 0; }
109
110        bool get_extension() const { return extension; }
111        ExpressionNode *set_extension( bool exten ) { extension = exten; return this; }
112
113        virtual void print( std::ostream &os, int indent = 0 ) const {}
114        virtual void printOneLine( std::ostream &os, int indent = 0 ) const {}
115
116        virtual Expression *build() const { return expr; }
117  private:
118        bool extension = false;
119        Expression *expr;
120};
121
122template< typename T >
123struct maybeBuild_t<Expression, T> {
124        static inline Expression * doit( const T *orig ) {
125                if ( orig ) {
126                        Expression *p = orig->build();
127                        p->set_extension( orig->get_extension() );
128                        return p;
129                } else {
130                        return 0;
131                } // if
132        }
133};
134
135//##############################################################################
136
137Expression *build_constantInteger( std::string &str );
138Expression *build_constantFloat( std::string &str );
139Expression *build_constantChar( std::string &str );
140ConstantExpr *build_constantStr( std::string &str );
141
142//##############################################################################
143
144NameExpr *build_varref( const std::string *name, bool labelp = false );
145
146//##############################################################################
147
148Expression *build_typevalue( DeclarationNode *decl );
149
150//##############################################################################
151
152enum class OperKinds {
153        // diadic
154        SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
155        BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
156        Assign, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
157        Index, Range,
158        // monadic
159        UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
160        Ctor, Dtor,
161};
162
163Expression *build_cast( DeclarationNode * decl_node, ExpressionNode *expr_node );
164Expression *build_fieldSel( ExpressionNode *expr_node, NameExpr *member );
165Expression *build_pfieldSel( ExpressionNode *expr_node, NameExpr *member );
166Expression *build_addressOf( ExpressionNode *expr_node );
167Expression *build_sizeOfexpr( ExpressionNode *expr_node );
168Expression *build_sizeOftype( DeclarationNode *decl_node );
169Expression *build_alignOfexpr( ExpressionNode *expr_node );
170Expression *build_alignOftype( DeclarationNode *decl_node );
171Expression *build_offsetOf( DeclarationNode *decl_node, NameExpr *member );
172Expression *build_and( ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
173Expression *build_and_or( ExpressionNode *expr_node1, ExpressionNode *expr_node2, bool kind );
174Expression *build_unary_val( OperKinds op, ExpressionNode *expr_node );
175Expression *build_unary_ptr( OperKinds op, ExpressionNode *expr_node );
176Expression *build_binary_val( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
177Expression *build_binary_ptr( OperKinds op, ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
178Expression *build_cond( ExpressionNode *expr_node1, ExpressionNode *expr_node2, ExpressionNode *expr_node3 );
179Expression *build_comma( ExpressionNode *expr_node1, ExpressionNode *expr_node2 );
180Expression *build_attrexpr( NameExpr *var, ExpressionNode * expr_node );
181Expression *build_attrtype( NameExpr *var, DeclarationNode * decl_node );
182Expression *build_tuple( ExpressionNode * expr_node = 0 );
183Expression *build_func( ExpressionNode * function, ExpressionNode * expr_node );
184Expression *build_range( ExpressionNode * low, ExpressionNode *high );
185
186//##############################################################################
187
188Expression *build_asm( ExpressionNode *inout, ConstantExpr *constraint, ExpressionNode *operand );
189
190//##############################################################################
191
192class LabelNode : public ExpressionNode {
193  public:
194        virtual Expression *build() const { return NULL; }
195        virtual LabelNode *clone() const { assert( false ); return new LabelNode( *this ); }
196
197        virtual void print( std::ostream &os, int indent = 0) const;
198        virtual void printOneLine( std::ostream &os, int indent = 0) const;
199
200        const std::list< Label > &get_labels() const { return labels; };
201        void append_label( std::string * label ) { labels.push_back( *label ); delete label; }
202  private:
203        std::list< Label > labels;
204};
205
206//##############################################################################
207
208Expression *build_valexpr( StatementNode *s );
209
210//##############################################################################
211
212Expression *build_compoundLiteral( DeclarationNode *decl_node, InitializerNode *kids );
213
214//##############################################################################
215
216class TypeData;
217
218class DeclarationNode : public ParseNode {
219  public:
220        enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic };
221        enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
222        enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
223        enum Modifier  { Signed, Unsigned, Short, Long };
224        enum Aggregate { Struct, Union, Trait };
225        enum TypeClass { Type, Dtype, Ftype };
226        enum BuiltinType { Valist };
227
228        static const char *storageName[];
229        static const char *qualifierName[];
230        static const char *basicTypeName[];
231        static const char *modifierName[];
232        static const char *aggregateName[];
233        static const char *typeClassName[];
234        static const char *builtinTypeName[];
235
236        static DeclarationNode *newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle = false );
237        static DeclarationNode *newQualifier( Qualifier );
238        static DeclarationNode *newStorageClass( StorageClass );
239        static DeclarationNode *newBasicType( BasicType );
240        static DeclarationNode *newModifier( Modifier );
241        static DeclarationNode *newForall( DeclarationNode *);
242        static DeclarationNode *newFromTypedef( std::string *);
243        static DeclarationNode *newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body );
244        static DeclarationNode *newEnum( std::string *name, DeclarationNode *constants );
245        static DeclarationNode *newEnumConstant( std::string *name, ExpressionNode *constant );
246        static DeclarationNode *newName( std::string *);
247        static DeclarationNode *newFromTypeGen( std::string *, ExpressionNode *params );
248        static DeclarationNode *newTypeParam( TypeClass, std::string *);
249        static DeclarationNode *newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts );
250        static DeclarationNode *newTraitUse( std::string *name, ExpressionNode *params );
251        static DeclarationNode *newTypeDecl( std::string *name, DeclarationNode *typeParams );
252        static DeclarationNode *newPointer( DeclarationNode *qualifiers );
253        static DeclarationNode *newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic );
254        static DeclarationNode *newVarArray( DeclarationNode *qualifiers );
255        static DeclarationNode *newBitfield( ExpressionNode *size );
256        static DeclarationNode *newTuple( DeclarationNode *members );
257        static DeclarationNode *newTypeof( ExpressionNode *expr );
258        static DeclarationNode *newAttr( std::string *, ExpressionNode *expr );
259        static DeclarationNode *newAttr( std::string *, DeclarationNode *type );
260        static DeclarationNode *newBuiltinType( BuiltinType );
261
262        DeclarationNode *addQualifiers( DeclarationNode *);
263        DeclarationNode *copyStorageClasses( DeclarationNode *);
264        DeclarationNode *addType( DeclarationNode *);
265        DeclarationNode *addTypedef();
266        DeclarationNode *addAssertions( DeclarationNode *);
267        DeclarationNode *addName( std::string *);
268        DeclarationNode *addBitfield( ExpressionNode *size );
269        DeclarationNode *addVarArgs();
270        DeclarationNode *addFunctionBody( StatementNode *body );
271        DeclarationNode *addOldDeclList( DeclarationNode *list );
272        DeclarationNode *addPointer( DeclarationNode *qualifiers );
273        DeclarationNode *addArray( DeclarationNode *array );
274        DeclarationNode *addNewPointer( DeclarationNode *pointer );
275        DeclarationNode *addNewArray( DeclarationNode *array );
276        DeclarationNode *addParamList( DeclarationNode *list );
277        DeclarationNode *addIdList( DeclarationNode *list );       // old-style functions
278        DeclarationNode *addInitializer( InitializerNode *init );
279
280        DeclarationNode *cloneType( std::string *newName );
281        DeclarationNode *cloneType( DeclarationNode *existing );
282        DeclarationNode *cloneType( int ) { return cloneType( ( std::string *)0 ); }
283        DeclarationNode *cloneBaseType( std::string *newName );
284        DeclarationNode *cloneBaseType( DeclarationNode *newdecl );
285
286        DeclarationNode *appendList( DeclarationNode * );
287
288        DeclarationNode *clone() const;
289        void print( std::ostream &os, int indent = 0 ) const;
290        void printList( std::ostream &os, int indent = 0 ) const;
291
292        Declaration *build() const;
293        ::Type *buildType() const;
294
295        bool get_hasEllipsis() const;
296        const std::string &get_name() const { return name; }
297        LinkageSpec::Type get_linkage() const { return linkage; }
298        DeclarationNode *extractAggregate() const;
299        ExpressionNode *get_enumeratorValue() const { return enumeratorValue; }
300
301        bool get_extension() const { return extension; }
302        DeclarationNode *set_extension( bool exten ) { extension = exten; return this; }
303
304        DeclarationNode();
305        ~DeclarationNode();
306  private:
307        StorageClass buildStorageClass() const;
308        bool buildFuncSpecifier( StorageClass key ) const;
309
310        TypeData *type;
311        std::string name;
312        std::list< StorageClass > storageClasses;
313        std::list< std::string > attributes;
314        ExpressionNode *bitfieldWidth;
315        ExpressionNode *enumeratorValue;
316        InitializerNode *initializer;
317        bool hasEllipsis;
318        LinkageSpec::Type linkage;
319        bool extension = false;
320
321        static UniqueName anonymous;
322}; // DeclarationNode
323
324Type *buildType( TypeData *type );
325
326//##############################################################################
327
328class StatementNode : public ParseNode {
329  public:
330        enum Type { Exp,   If,        Switch,  Case,    Default,  Choose,   Fallthru,
331                                While, Do,        For,
332                                Goto,  Continue,  Break,   Return,  Throw,
333                                Try,   Catch,     Finally, Asm,
334                                Decl
335        };
336
337        StatementNode();
338        StatementNode( DeclarationNode *decl );
339        ~StatementNode();
340
341        StatementNode::Type get_type() const { return type; }
342
343        virtual StatementNode *add_label( const std::string * );
344        virtual std::list<std::string> get_labels() const { return labels; }
345
346        void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; }
347
348        virtual StatementNode *append_last_case( StatementNode * );
349
350        virtual void print( std::ostream &os, int indent = 0) const {}
351        virtual StatementNode *clone() const;
352        virtual Statement *build() const;
353  public:
354        static const char *StType[];
355        Type type;
356        std::list<std::string> labels;
357        DeclarationNode *decl;
358}; // StatementNode
359
360class StatementNode2 : public StatementNode {
361  public:
362        StatementNode2() { stmt = nullptr; }
363        StatementNode2( Statement *stmt ) : stmt( stmt ) {}
364        StatementNode2( DeclarationNode *decl );
365        virtual ~StatementNode2() {}
366
367        virtual StatementNode2 *clone() const { assert( false ); return nullptr; }
368        virtual Statement *build() const { return stmt; }
369
370        virtual StatementNode2 *add_label( const std::string * name ) {
371                stmt->get_labels().emplace_back( *name );
372                return this;
373        }
374        virtual std::list<std::string> get_labels() const { assert( false ); return StatementNode::get_labels(); }
375
376        virtual StatementNode *append_last_case( StatementNode * );
377
378        virtual void print( std::ostream &os, int indent = 0 ) {}
379        virtual void printList( std::ostream &os, int indent = 0 ) {}
380  private:
381        Statement *stmt;
382}; // StatementNode
383
384Statement *build_expr( ExpressionNode *ctl );
385
386struct ForCtl {
387        ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
388                init( new StatementNode2( build_expr( expr ) ) ), condition( condition ), change( change ) {}
389        ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
390                init( new StatementNode2( decl ) ), condition( condition ), change( change ) {}
391
392        StatementNode *init;
393        ExpressionNode *condition;
394        ExpressionNode *change;
395};
396
397Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
398Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
399Statement *build_case( ExpressionNode *ctl );
400Statement *build_default();
401Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
402Statement *build_for( ForCtl *forctl, StatementNode *stmt );
403Statement *build_branch( std::string identifier, BranchStmt::Type kind );
404Statement *build_computedgoto( ExpressionNode *ctl );
405Statement *build_return( ExpressionNode *ctl );
406Statement *build_throw( ExpressionNode *ctl );
407Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt );
408Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false );
409Statement *build_finally( StatementNode *stmt );
410
411//##############################################################################
412
413class CompoundStmtNode : public StatementNode {
414  public:
415        CompoundStmtNode();
416        CompoundStmtNode( StatementNode * );
417        ~CompoundStmtNode();
418
419        void add_statement( StatementNode * );
420
421        void print( std::ostream &os, int indent = 0 ) const;
422        virtual Statement *build() const;
423  private:
424        StatementNode *first, *last;
425};
426
427//##############################################################################
428
429class AsmStmtNode : public StatementNode {
430  public:
431        AsmStmtNode( bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
432        ~AsmStmtNode();
433
434        void print( std::ostream &os, int indent = 0 ) const {}
435        Statement *build() const;
436  private:
437        bool voltile;
438        ConstantExpr *instruction;
439        ExpressionNode *output, *input;
440        ExpressionNode *clobber;
441        std::list< Label > gotolabels;
442};
443
444//##############################################################################
445
446template< typename SynTreeType, typename NodeType >
447void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
448        SemanticError errors;
449        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
450        const NodeType *cur = firstNode;
451
452        while ( cur ) {
453                try {
454//                      SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::result_of<decltype(&NodeType::build)(NodeType)>::type>( cur ) );
455                        SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::pointer_traits<decltype(cur->build())>::element_type>( cur ) );
456                        if ( result ) {
457                                *out++ = result;
458                        } else {
459                        } // if
460                } catch( SemanticError &e ) {
461                        errors.append( e );
462                } // try
463                cur = dynamic_cast< NodeType *>( cur->get_next() );
464        } // while
465        if ( ! errors.isEmpty() ) {
466                throw errors;
467        } // if
468}
469
470// in DeclarationNode.cc
471void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
472void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
473void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
474
475#endif // PARSENODE_H
476
477// Local Variables: //
478// tab-width: 4 //
479// mode: c++ //
480// compile-command: "make install" //
481// End: //
Note: See TracBrowser for help on using the repository browser.