source: src/Parser/ParseNode.h @ 1d4580a

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

more refactoring of parser code

  • Property mode set to 100644
File size: 18.5 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 08:09:54 2016
13// Update Count     : 469
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( const std::string *name );
339        StatementNode( Type t, ExpressionNode *control = 0, StatementNode *block = 0 );
340        StatementNode( Type t, std::string *target );
341        StatementNode( DeclarationNode *decl );
342
343        ~StatementNode();
344
345        StatementNode *set_block( StatementNode *b ) { block = b; return this; }
346        StatementNode *get_block() const { return block; }
347
348        void set_control( ExpressionNode *c ) { control = c; }
349        ExpressionNode *get_control() const { return control; }
350
351        StatementNode::Type get_type() const { return type; }
352
353        virtual StatementNode *add_label( const std::string * );
354        virtual std::list<std::string> get_labels() const { return labels; }
355
356        void addDeclaration( DeclarationNode *newDecl ) { decl = newDecl; }
357
358        void setCatchRest( bool newVal ) { catchAny = newVal; }
359
360        // StatementNode *add_controlexp( ExpressionNode * );
361        StatementNode *append_block( StatementNode * );
362        virtual StatementNode *append_last_case( StatementNode * );
363
364        void print( std::ostream &os, int indent = 0) const;
365        virtual StatementNode *clone() const;
366        virtual Statement *build() const;
367  public:
368        static const char *StType[];
369        Type type;
370        ExpressionNode *control;
371        StatementNode *block;
372        std::list<std::string> labels;
373        std::string *target;                                                            // target label for jump statements
374        DeclarationNode *decl;
375        bool catchAny;
376}; // StatementNode
377
378class StatementNode2 : public StatementNode {
379  public:
380        StatementNode2() { stmt = nullptr; }
381        StatementNode2( Statement *stmt ) : stmt( stmt ) {}
382        StatementNode2( DeclarationNode *decl );
383        virtual ~StatementNode2() {}
384
385        virtual StatementNode2 *clone() const { assert( false ); return nullptr; }
386        virtual Statement *build() const { return stmt; }
387
388        virtual StatementNode2 *add_label( const std::string * name ) {
389                stmt->get_labels().emplace_back( *name );
390                return this;
391        }
392        virtual std::list<std::string> get_labels() const { assert( false ); return StatementNode::get_labels(); }
393
394        virtual StatementNode *append_last_case( StatementNode * );
395
396        virtual void print( std::ostream &os, int indent = 0 ) {}
397        virtual void printList( std::ostream &os, int indent = 0 ) {}
398  private:
399        Statement *stmt;
400}; // StatementNode
401
402Statement *build_expr( ExpressionNode *ctl );
403
404struct ForCtl {
405        ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
406                init( new StatementNode2( build_expr( expr ) ) ), condition( condition ), change( change ) {}
407        ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
408                init( new StatementNode2( decl ) ), condition( condition ), change( change ) {}
409
410        StatementNode *init;
411        ExpressionNode *condition;
412        ExpressionNode *change;
413};
414
415Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
416Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
417Statement *build_case( ExpressionNode *ctl );
418Statement *build_default();
419Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
420Statement *build_for( ForCtl *forctl, StatementNode *stmt );
421Statement *build_branch( std::string identifier, BranchStmt::Type kind );
422Statement *build_computedgoto( ExpressionNode *ctl );
423Statement *build_return( ExpressionNode *ctl );
424Statement *build_throw( ExpressionNode *ctl );
425Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt );
426Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny = false );
427Statement *build_finally( StatementNode *stmt );
428
429//##############################################################################
430
431class CompoundStmtNode : public StatementNode {
432  public:
433        CompoundStmtNode();
434        CompoundStmtNode( StatementNode * );
435        ~CompoundStmtNode();
436
437        void add_statement( StatementNode * );
438
439        void print( std::ostream &os, int indent = 0 ) const;
440        virtual Statement *build() const;
441  private:
442        StatementNode *first, *last;
443};
444
445//##############################################################################
446
447class AsmStmtNode : public StatementNode {
448  public:
449        AsmStmtNode( Type, bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
450        ~AsmStmtNode();
451
452        void print( std::ostream &os, int indent = 0 ) const;
453        Statement *build() const;
454  private:
455        bool voltile;
456        ConstantExpr *instruction;
457        ExpressionNode *output, *input;
458        ExpressionNode *clobber;
459        std::list< Label > gotolabels;
460};
461
462//##############################################################################
463
464template< typename SynTreeType, typename NodeType >
465void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
466        SemanticError errors;
467        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
468        const NodeType *cur = firstNode;
469
470        while ( cur ) {
471                try {
472//                      SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::result_of<decltype(&NodeType::build)(NodeType)>::type>( cur ) );
473                        SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::pointer_traits<decltype(cur->build())>::element_type>( cur ) );
474                        if ( result ) {
475                                *out++ = result;
476                        } else {
477                        } // if
478                } catch( SemanticError &e ) {
479                        errors.append( e );
480                } // try
481                cur = dynamic_cast< NodeType *>( cur->get_next() );
482        } // while
483        if ( ! errors.isEmpty() ) {
484                throw errors;
485        } // if
486}
487
488// in DeclarationNode.cc
489void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
490void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
491void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
492
493#endif // PARSENODE_H
494
495// Local Variables: //
496// tab-width: 4 //
497// mode: c++ //
498// compile-command: "make install" //
499// End: //
Note: See TracBrowser for help on using the repository browser.