source: src/Parser/ParseNode.h @ 8b7ee09

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

rename type LinkageSpec::Type to LinkageSpec::Spec, which affects many files

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