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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 8c49c0e was 12bc63a, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into tuples

Conflicts:

src/Parser/parser.cc

  • Property mode set to 100644
File size: 17.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 : Mon Sep 12 08:00:05 2016
13// Update Count     : 603
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}; // InitializerNode
102
103//##############################################################################
104
105class ExpressionNode final : 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 { return expr ? new ExpressionNode( expr->clone() ) : nullptr; }
112
113        bool get_extension() const { return extension; }
114        ExpressionNode * set_extension( bool exten ) { extension = exten; return this; }
115
116        void print( std::ostream &os, int indent = 0 ) const {}
117        void printOneLine( std::ostream &os, int indent = 0 ) const {}
118
119        template<typename T>
120        bool isExpressionType() const {
121                return nullptr != dynamic_cast<T>(expr.get());
122        }
123
124        Expression * build() const { return const_cast<ExpressionNode*>(this)->expr.release(); }
125  private:
126        bool extension = false;
127        std::unique_ptr<Expression> expr;
128}; // ExpressionNode
129
130template< typename T >
131struct maybeBuild_t< Expression, T > {
132        static inline Expression * doit( const T * orig ) {
133                if ( orig ) {
134                        Expression * p = orig->build();
135                        p->set_extension( orig->get_extension() );
136                        return p;
137                } else {
138                        return nullptr;
139                } // if
140        }
141};
142
143enum class OperKinds {
144        // diadic
145        SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
146        BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
147        Assign, AtAssn, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
148        Index, Range,
149        // monadic
150        UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
151        Ctor, Dtor,
152}; // OperKinds
153
154struct LabelNode {
155        std::list< Label > labels;
156};
157
158Expression * build_constantInteger( const std::string &str );
159Expression * build_constantFloat( const std::string &str );
160Expression * build_constantChar( const std::string &str );
161ConstantExpr * build_constantStr( const std::string &str );
162
163NameExpr * build_varref( const std::string * name, bool labelp = false );
164Expression * build_typevalue( DeclarationNode * decl );
165
166Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node );
167Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member );
168Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member );
169Expression * build_addressOf( ExpressionNode * expr_node );
170Expression * build_sizeOfexpr( ExpressionNode * expr_node );
171Expression * build_sizeOftype( DeclarationNode * decl_node );
172Expression * build_alignOfexpr( ExpressionNode * expr_node );
173Expression * build_alignOftype( DeclarationNode * decl_node );
174Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member );
175Expression * build_and( ExpressionNode * expr_node1, ExpressionNode * expr_node2 );
176Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind );
177Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node );
178Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node );
179Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 );
180Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 );
181Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 );
182Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 );
183Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node );
184Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node );
185Expression * build_tuple( ExpressionNode * expr_node = 0 );
186Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node );
187Expression * build_range( ExpressionNode * low, ExpressionNode * high );
188Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand );
189Expression * build_valexpr( StatementNode * s );
190Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids );
191
192//##############################################################################
193
194class TypeData;
195
196class DeclarationNode : public ParseNode {
197  public:
198        // These must remain in the same order as the corresponding DeclarationNode names.
199        enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
200        enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic, NoQualifier };
201        enum BasicType { Void, Bool, Char, Int, Float, Double, LongDouble, NoBasicType };
202        enum ComplexType { Complex, Imaginary, NoComplexType };
203        enum Signedness { Signed, Unsigned, NoSignedness };
204        enum Length { Short, Long, LongLong, NoLength };
205        enum Aggregate { Struct, Union, Trait };
206        enum TypeClass { Otype, Dtype, Ftype };
207        enum BuiltinType { Valist };
208
209        static const char * storageName[];
210        static const char * qualifierName[];
211        static const char * basicTypeName[];
212        static const char * complexTypeName[];
213        static const char * signednessName[];
214        static const char * lengthName[];
215        static const char * aggregateName[];
216        static const char * typeClassName[];
217        static const char * builtinTypeName[];
218
219        static DeclarationNode * newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle = false );
220        static DeclarationNode * newQualifier( Qualifier );
221        static DeclarationNode * newForall( DeclarationNode *);
222        static DeclarationNode * newStorageClass( StorageClass );
223        static DeclarationNode * newBasicType( BasicType );
224        static DeclarationNode * newComplexType( ComplexType );
225        static DeclarationNode * newSignedNess( Signedness sn );
226        static DeclarationNode * newLength( Length lnth );
227        static DeclarationNode * newBuiltinType( BuiltinType );
228        static DeclarationNode * newFromTypedef( std::string *);
229        static DeclarationNode * newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
230        static DeclarationNode * newEnum( std::string * name, DeclarationNode * constants );
231        static DeclarationNode * newEnumConstant( std::string * name, ExpressionNode * constant );
232        static DeclarationNode * newName( std::string *);
233        static DeclarationNode * newFromTypeGen( std::string *, ExpressionNode * params );
234        static DeclarationNode * newTypeParam( TypeClass, std::string *);
235        static DeclarationNode * newTrait( std::string * name, DeclarationNode * params, DeclarationNode * asserts );
236        static DeclarationNode * newTraitUse( std::string * name, ExpressionNode * params );
237        static DeclarationNode * newTypeDecl( std::string * name, DeclarationNode * typeParams );
238        static DeclarationNode * newPointer( DeclarationNode * qualifiers );
239        static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic );
240        static DeclarationNode * newVarArray( DeclarationNode * qualifiers );
241        static DeclarationNode * newBitfield( ExpressionNode * size );
242        static DeclarationNode * newTuple( DeclarationNode * members );
243        static DeclarationNode * newTypeof( ExpressionNode * expr );
244        static DeclarationNode * newAttr( std::string *, ExpressionNode * expr );
245        static DeclarationNode * newAttr( std::string *, DeclarationNode * type );
246
247        DeclarationNode();
248        ~DeclarationNode();
249        DeclarationNode * clone() const;
250
251        DeclarationNode * addQualifiers( DeclarationNode *);
252        void checkQualifiers( const TypeData *, const TypeData * );
253        void checkStorageClasses( DeclarationNode *q );
254        DeclarationNode * copyStorageClasses( DeclarationNode *);
255        DeclarationNode * addType( DeclarationNode *);
256        DeclarationNode * addTypedef();
257        DeclarationNode * addAssertions( DeclarationNode *);
258        DeclarationNode * addName( std::string *);
259        DeclarationNode * addBitfield( ExpressionNode * size );
260        DeclarationNode * addVarArgs();
261        DeclarationNode * addFunctionBody( StatementNode * body );
262        DeclarationNode * addOldDeclList( DeclarationNode * list );
263        DeclarationNode * addPointer( DeclarationNode * qualifiers );
264        DeclarationNode * addArray( DeclarationNode * array );
265        DeclarationNode * addNewPointer( DeclarationNode * pointer );
266        DeclarationNode * addNewArray( DeclarationNode * array );
267        DeclarationNode * addParamList( DeclarationNode * list );
268        DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions
269        DeclarationNode * addInitializer( InitializerNode * init );
270
271        DeclarationNode * cloneType( std::string * newName );
272        DeclarationNode * cloneType( DeclarationNode * existing );
273        DeclarationNode * cloneType( int ) { return cloneType( ( std::string *)0 ); }
274        DeclarationNode * cloneBaseType( std::string * newName );
275        DeclarationNode * cloneBaseType( DeclarationNode * newdecl );
276
277        DeclarationNode * appendList( DeclarationNode * node ) {
278                return (DeclarationNode *)set_last( node );
279        }
280
281        void print( std::ostream &os, int indent = 0 ) const;
282        void printList( std::ostream &os, int indent = 0 ) const;
283
284        Declaration * build() const;
285        ::Type * buildType() const;
286
287        bool get_hasEllipsis() const;
288        const std::string &get_name() const { return name; }
289        LinkageSpec::Spec get_linkage() const { return linkage; }
290        DeclarationNode * extractAggregate() const;
291        bool has_enumeratorValue() const { return (bool)enumeratorValue; }
292        ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode*>(this)->enumeratorValue.release(); }
293
294        bool get_extension() const { return extension; }
295        DeclarationNode * set_extension( bool exten ) { extension = exten; return this; }
296  public:
297        // StorageClass buildStorageClass() const;
298        // bool buildFuncSpecifier( StorageClass key ) const;
299
300        struct Variable_t {
301                DeclarationNode::TypeClass tyClass;
302                std::string name;
303                DeclarationNode * assertions;
304        };
305        Variable_t variable;
306
307        struct Attr_t {
308                std::string name;
309                ExpressionNode * expr;
310                DeclarationNode * type;
311        };
312        Attr_t attr;
313
314        BuiltinType builtin;
315
316        TypeData * type;
317        std::string name;
318        StorageClass storageClass;
319        bool isInline, isNoreturn;
320        std::list< std::string > attributes;
321        ExpressionNode * bitfieldWidth;
322        std::unique_ptr<ExpressionNode> enumeratorValue;
323        InitializerNode * initializer;
324        bool hasEllipsis;
325        LinkageSpec::Spec linkage;
326        bool extension = false;
327        std::string error;
328
329        static UniqueName anonymous;
330}; // DeclarationNode
331
332Type * buildType( TypeData * type );
333//Type::Qualifiers buildQualifiers( const TypeData::Qualifiers & qualifiers );
334
335static inline Type * maybeMoveBuildType( const DeclarationNode * orig ) {
336        Type* ret = orig ? orig->buildType() : nullptr;
337        delete orig;
338        return ret;
339}
340
341//##############################################################################
342
343class StatementNode final : public ParseNode {
344  public:
345        StatementNode() { stmt = nullptr; }
346        StatementNode( Statement * stmt ) : stmt( stmt ) {}
347        StatementNode( DeclarationNode * decl );
348        virtual ~StatementNode() {}
349
350        virtual StatementNode * clone() const final { assert( false ); return nullptr; }
351        Statement * build() const { return const_cast<StatementNode*>(this)->stmt.release(); }
352
353        virtual StatementNode * add_label( const std::string * name ) {
354                stmt->get_labels().emplace_back( * name );
355                delete name;
356                return this;
357        }
358
359        virtual StatementNode * append_last_case( StatementNode * );
360
361        virtual void print( std::ostream &os, int indent = 0 ) {}
362        virtual void printList( std::ostream &os, int indent = 0 ) {}
363  private:
364        std::unique_ptr<Statement> stmt;
365}; // StatementNode
366
367Statement * build_expr( ExpressionNode * ctl );
368
369struct ForCtl {
370        ForCtl( ExpressionNode * expr, ExpressionNode * condition, ExpressionNode * change ) :
371                init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
372        ForCtl( DeclarationNode * decl, ExpressionNode * condition, ExpressionNode * change ) :
373                init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
374
375        StatementNode * init;
376        ExpressionNode * condition;
377        ExpressionNode * change;
378};
379
380Statement * build_if( ExpressionNode * ctl, StatementNode * then_stmt, StatementNode * else_stmt );
381Statement * build_switch( ExpressionNode * ctl, StatementNode * stmt );
382Statement * build_case( ExpressionNode * ctl );
383Statement * build_default();
384Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind = false );
385Statement * build_for( ForCtl * forctl, StatementNode * stmt );
386Statement * build_branch( BranchStmt::Type kind );
387Statement * build_branch( std::string * identifier, BranchStmt::Type kind );
388Statement * build_computedgoto( ExpressionNode * ctl );
389Statement * build_return( ExpressionNode * ctl );
390Statement * build_throw( ExpressionNode * ctl );
391Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt );
392Statement * build_catch( DeclarationNode * decl, StatementNode * stmt, bool catchAny = false );
393Statement * build_finally( StatementNode * stmt );
394Statement * build_compound( StatementNode * first );
395Statement * build_asmstmt( bool voltile, ConstantExpr * instruction, ExpressionNode * output = 0, ExpressionNode * input = 0, ExpressionNode * clobber = 0, LabelNode * gotolabels = 0 );
396
397//##############################################################################
398
399template< typename SynTreeType, typename NodeType >
400void buildList( const NodeType * firstNode, std::list< SynTreeType * > &outputList ) {
401        SemanticError errors;
402        std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );
403        const NodeType * cur = firstNode;
404
405        while ( cur ) {
406                try {
407//                      SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) );
408                        SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
409                        if ( result ) {
410                                * out++ = result;
411                        } // if
412                } catch( SemanticError &e ) {
413                        errors.append( e );
414                } // try
415                cur = dynamic_cast< NodeType * >( cur->get_next() );
416        } // while
417        if ( ! errors.isEmpty() ) {
418                throw errors;
419        } // if
420}
421
422// in DeclarationNode.cc
423void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList );
424void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList );
425void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList );
426
427template< typename SynTreeType, typename NodeType >
428void buildMoveList( const NodeType * firstNode, std::list< SynTreeType * > &outputList ) {
429        buildList(firstNode, outputList);
430        delete firstNode;
431}
432
433// in ParseNode.cc
434std::ostream & operator<<( std::ostream & out, const ParseNode * node );
435
436#endif // PARSENODE_H
437
438// Local Variables: //
439// tab-width: 4 //
440// mode: c++ //
441// compile-command: "make install" //
442// End: //
Note: See TracBrowser for help on using the repository browser.