source: src/Parser/ParseNode.h @ 321f55d

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

more refactoring of parser code

  • Property mode set to 100644
File size: 18.2 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 : Wed Aug 10 21:51:49 2016
13// Update Count     : 437
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        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 std::list<std::string> get_labels() const { assert( false ); return StatementNode::get_labels(); }
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
402struct ForCtl {
403        ForCtl( ExpressionNode *expr, ExpressionNode *condition, ExpressionNode *change ) :
404                init( new StatementNode( StatementNode::Exp, expr ) ), condition( condition ), change( change ) {}
405        ForCtl( DeclarationNode *decl, ExpressionNode *condition, ExpressionNode *change ) :
406                init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
407
408        StatementNode *init;
409        ExpressionNode *condition;
410        ExpressionNode *change;
411};
412
413Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt );
414Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt );
415Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind = false );
416Statement *build_for( ForCtl *forctl, StatementNode *stmt );
417Statement *build_branch( std::string identifier, BranchStmt::Type kind );
418Statement *build_case( ExpressionNode *ctl );
419Statement *build_default();
420
421//##############################################################################
422
423class CompoundStmtNode : public StatementNode {
424  public:
425        CompoundStmtNode();
426        CompoundStmtNode( const std::string * );
427        CompoundStmtNode( StatementNode * );
428        ~CompoundStmtNode();
429
430        void add_statement( StatementNode * );
431
432        void print( std::ostream &os, int indent = 0 ) const;
433        virtual Statement *build() const;
434  private:
435        StatementNode *first, *last;
436};
437
438//##############################################################################
439
440class AsmStmtNode : public StatementNode {
441  public:
442        AsmStmtNode( Type, bool voltile, ConstantExpr *instruction, ExpressionNode *output = 0, ExpressionNode *input = 0, ExpressionNode *clobber = 0, LabelNode *gotolabels = 0 );
443        ~AsmStmtNode();
444
445        void print( std::ostream &os, int indent = 0 ) const;
446        Statement *build() const;
447  private:
448        bool voltile;
449        ConstantExpr *instruction;
450        ExpressionNode *output, *input;
451        ExpressionNode *clobber;
452        std::list< Label > gotolabels;
453};
454
455//##############################################################################
456
457template< typename SynTreeType, typename NodeType >
458void buildList( const NodeType *firstNode, std::list< SynTreeType * > &outputList ) {
459        SemanticError errors;
460        std::back_insert_iterator< std::list< SynTreeType *> > out( outputList );
461        const NodeType *cur = firstNode;
462
463        while ( cur ) {
464                try {
465//                      SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::result_of<decltype(&NodeType::build)(NodeType)>::type>( cur ) );
466                        SynTreeType *result = dynamic_cast< SynTreeType *>( maybeBuild<typename std::pointer_traits<decltype(cur->build())>::element_type>( cur ) );
467                        if ( result ) {
468                                *out++ = result;
469                        } else {
470                        } // if
471                } catch( SemanticError &e ) {
472                        errors.append( e );
473                } // try
474                cur = dynamic_cast< NodeType *>( cur->get_link() );
475        } // while
476        if ( ! errors.isEmpty() ) {
477                throw errors;
478        } // if
479}
480
481// in DeclarationNode.cc
482void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList );
483void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType *> &outputList );
484void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList );
485
486#endif // PARSENODE_H
487
488// Local Variables: //
489// tab-width: 4 //
490// mode: c++ //
491// compile-command: "make install" //
492// End: //
Note: See TracBrowser for help on using the repository browser.