source: src/Parser/ParseNode.h @ 473d8c82

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

more refactoring of parser code

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