source: src/Parser/ParseNode.h @ a5f0529

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 a5f0529 was a5f0529, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Virtual casts have been added. They still require a lot of hand coded support to work but for simple cases it should be enough.

  • Property mode set to 100644
File size: 18.9 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 : Andrew Beach
12// Last Modified On : Tus Jul 25 10:09:00 2017
13// Update Count     : 787
14//
15
16#pragma once
17
18#include <string>
19#include <list>
20#include <iterator>
21#include <memory>
22
23#include "Parser/LinkageSpec.h"
24#include "SynTree/Type.h"
25#include "SynTree/Expression.h"
26#include "SynTree/Statement.h"
27#include "SynTree/Label.h"
28#include "Common/utility.h"
29#include "Common/UniqueName.h"
30
31class StatementNode;
32class CompoundStmtNode;
33class DeclarationNode;
34class ExpressionNode;
35class InitializerNode;
36class Attribute;
37
38//##############################################################################
39
40extern char * yyfilename;
41extern int yylineno;
42
43class ParseNode {
44  public:
45        ParseNode() {};
46        virtual ~ParseNode() { delete next; delete name; };
47        virtual ParseNode * clone() const = 0;
48
49        ParseNode * get_next() const { return next; }
50        ParseNode * set_next( ParseNode * newlink ) { next = newlink; return this; }
51
52        ParseNode * get_last() {
53                ParseNode * current;
54                for ( current = this; current->get_next() != nullptr; current = current->get_next() );
55                return current;
56        }
57        ParseNode * set_last( ParseNode * newlast ) {
58                if ( newlast != nullptr ) get_last()->set_next( newlast );
59                return this;
60        }
61
62        virtual void print( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const {}
63        virtual void printList( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const {}
64
65        static int indent_by;
66
67        ParseNode * next = nullptr;
68        std::string * name = nullptr;
69        CodeLocation location = { yyfilename, yylineno };
70}; // ParseNode
71
72//##############################################################################
73
74class InitializerNode : public ParseNode {
75  public:
76        InitializerNode( ExpressionNode *, bool aggrp = false,  ExpressionNode * des = nullptr );
77        InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode * des = nullptr );
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        virtual ~ExpressionNode() {}
109        virtual ExpressionNode * clone() const override { return expr ? static_cast<ExpressionNode*>((new ExpressionNode( expr->clone() ))->set_next( maybeClone( get_next() ) )) : nullptr; }
110
111        bool get_extension() const { return extension; }
112        ExpressionNode * set_extension( bool exten ) { extension = exten; return this; }
113
114        virtual void print( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const override {}
115        void printOneLine( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const {}
116
117        template<typename T>
118        bool isExpressionType() const {
119                return nullptr != dynamic_cast<T>(expr.get());
120        }
121
122        Expression * build() const { return const_cast<ExpressionNode *>(this)->expr.release(); }
123  private:
124        bool extension = false;
125        std::unique_ptr<Expression> expr;
126}; // ExpressionNode
127
128template< typename T >
129struct maybeBuild_t< Expression, T > {
130        static inline Expression * doit( const T * orig ) {
131                if ( orig ) {
132                        Expression * p = orig->build();
133                        p->set_extension( orig->get_extension() );
134                        p->location = orig->location;
135                        return p;
136                } else {
137                        return nullptr;
138                } // if
139        }
140};
141
142// Must harmonize with OperName.
143enum class OperKinds {
144        // diadic
145        SizeOf, AlignOf, OffsetOf, Plus, Minus, Exp, Mul, Div, Mod, Or, And,
146        BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
147        Assign, AtAssn, ExpAssn, 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 );
161Expression * build_constantZeroOne( const std::string &str );
162ConstantExpr * build_constantStr( const std::string &str );
163Expression * build_field_name_FLOATINGconstant( const std::string & str );
164Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts );
165Expression * build_field_name_REALFRACTIONconstant( const std::string & str );
166Expression * build_field_name_REALDECIMALconstant( const std::string & str );
167
168NameExpr * build_varref( const std::string * name );
169Expression * build_typevalue( DeclarationNode * decl );
170
171Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node );
172Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node );
173Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member );
174Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member );
175Expression * build_addressOf( ExpressionNode * expr_node );
176Expression * build_sizeOfexpr( ExpressionNode * expr_node );
177Expression * build_sizeOftype( DeclarationNode * decl_node );
178Expression * build_alignOfexpr( ExpressionNode * expr_node );
179Expression * build_alignOftype( DeclarationNode * decl_node );
180Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member );
181Expression * build_and( ExpressionNode * expr_node1, ExpressionNode * expr_node2 );
182Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind );
183Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node );
184Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node );
185Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 );
186Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 );
187Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 );
188Expression * build_comma( ExpressionNode * expr_node1, ExpressionNode * expr_node2 );
189Expression * build_attrexpr( NameExpr * var, ExpressionNode * expr_node );
190Expression * build_attrtype( NameExpr * var, DeclarationNode * decl_node );
191Expression * build_tuple( ExpressionNode * expr_node = nullptr );
192Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node );
193Expression * build_range( ExpressionNode * low, ExpressionNode * high );
194Expression * build_asmexpr( ExpressionNode * inout, ConstantExpr * constraint, ExpressionNode * operand );
195Expression * build_valexpr( StatementNode * s );
196Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids );
197
198//##############################################################################
199
200struct TypeData;
201
202class DeclarationNode : public ParseNode {
203  public:
204        enum BasicType { Void, Bool, Char, Int, Float, Double, LongDouble, NoBasicType };
205        enum ComplexType { Complex, Imaginary, NoComplexType };
206        enum Signedness { Signed, Unsigned, NoSignedness };
207        enum Length { Short, Long, LongLong, NoLength };
208        enum Aggregate { Struct, Union, Trait, Coroutine, Monitor, Thread, NoAggregate };
209        enum TypeClass { Otype, Dtype, Ftype, Ttype, NoTypeClass };
210        enum BuiltinType { Valist, Zero, One, NoBuiltinType };
211
212        static const char * basicTypeNames[];
213        static const char * complexTypeNames[];
214        static const char * signednessNames[];
215        static const char * lengthNames[];
216        static const char * aggregateNames[];
217        static const char * typeClassNames[];
218        static const char * builtinTypeNames[];
219
220        static DeclarationNode * newStorageClass( Type::StorageClasses );
221        static DeclarationNode * newFuncSpecifier( Type::FuncSpecifiers );
222        static DeclarationNode * newTypeQualifier( Type::Qualifiers );
223        static DeclarationNode * newBasicType( BasicType );
224        static DeclarationNode * newComplexType( ComplexType );
225        static DeclarationNode * newSignedNess( Signedness );
226        static DeclarationNode * newLength( Length );
227        static DeclarationNode * newBuiltinType( BuiltinType );
228        static DeclarationNode * newForall( DeclarationNode * );
229        static DeclarationNode * newFromTypedef( std::string * );
230        static DeclarationNode * newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle = false );
231        static DeclarationNode * newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
232        static DeclarationNode * newEnum( std::string * name, DeclarationNode * constants, bool body );
233        static DeclarationNode * newEnumConstant( std::string * name, ExpressionNode * constant );
234        static DeclarationNode * newName( std::string * );
235        static DeclarationNode * newFromTypeGen( std::string *, ExpressionNode * params );
236        static DeclarationNode * newTypeParam( TypeClass, std::string * );
237        static DeclarationNode * newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts );
238        static DeclarationNode * newTraitUse( const std::string * name, ExpressionNode * params );
239        static DeclarationNode * newTypeDecl( std::string * name, DeclarationNode * typeParams );
240        static DeclarationNode * newPointer( DeclarationNode * qualifiers );
241        static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic );
242        static DeclarationNode * newVarArray( DeclarationNode * qualifiers );
243        static DeclarationNode * newBitfield( ExpressionNode * size );
244        static DeclarationNode * newTuple( DeclarationNode * members );
245        static DeclarationNode * newTypeof( ExpressionNode * expr );
246        static DeclarationNode * newAttr( std::string *, ExpressionNode * expr ); // @ attributes
247        static DeclarationNode * newAttr( std::string *, DeclarationNode * type ); // @ attributes
248        static DeclarationNode * newAttribute( std::string *, ExpressionNode * expr = nullptr ); // gcc attributes
249        static DeclarationNode * newAsmStmt( StatementNode * stmt ); // gcc external asm statement
250
251        // Perhaps this would best fold into newAggragate.
252        static DeclarationNode * newTreeStruct( Aggregate kind, const std::string * name, const std::string * parent, ExpressionNode * actuals, DeclarationNode * fields, bool body );
253
254        DeclarationNode();
255        ~DeclarationNode();
256        DeclarationNode * clone() const override;
257
258        DeclarationNode * addQualifiers( DeclarationNode * );
259        void checkQualifiers( const TypeData *, const TypeData * );
260        void checkSpecifiers( DeclarationNode * );
261        DeclarationNode * copySpecifiers( DeclarationNode * );
262        DeclarationNode * addType( DeclarationNode * );
263        DeclarationNode * addTypedef();
264        DeclarationNode * addAssertions( DeclarationNode * );
265        DeclarationNode * addName( std::string * );
266        DeclarationNode * addAsmName( DeclarationNode * );
267        DeclarationNode * addBitfield( ExpressionNode * size );
268        DeclarationNode * addVarArgs();
269        DeclarationNode * addFunctionBody( StatementNode * body );
270        DeclarationNode * addOldDeclList( DeclarationNode * list );
271        DeclarationNode * setBase( TypeData * newType );
272        DeclarationNode * copyAttribute( DeclarationNode * attr );
273        DeclarationNode * addPointer( DeclarationNode * qualifiers );
274        DeclarationNode * addArray( DeclarationNode * array );
275        DeclarationNode * addNewPointer( DeclarationNode * pointer );
276        DeclarationNode * addNewArray( DeclarationNode * array );
277        DeclarationNode * addParamList( DeclarationNode * list );
278        DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions
279        DeclarationNode * addInitializer( InitializerNode * init );
280        DeclarationNode * addTypeInitializer( DeclarationNode * init );
281
282        DeclarationNode * cloneType( std::string * newName );
283        DeclarationNode * cloneBaseType( DeclarationNode * newdecl );
284
285        DeclarationNode * appendList( DeclarationNode * node ) {
286                return (DeclarationNode *)set_last( node );
287        }
288
289        virtual void print( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const override;
290        virtual void printList( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const override;
291
292        Declaration * build() const;
293        Type * buildType() const;
294
295        bool get_hasEllipsis() const;
296        LinkageSpec::Spec get_linkage() const { return linkage; }
297        DeclarationNode * extractAggregate() const;
298        bool has_enumeratorValue() const { return (bool)enumeratorValue; }
299        ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode *>(this)->enumeratorValue.release(); }
300
301        bool get_extension() const { return extension; }
302        DeclarationNode * set_extension( bool exten ) { extension = exten; return this; }
303  public:
304        struct Variable_t {
305//              const std::string * name;
306                DeclarationNode::TypeClass tyClass;
307                DeclarationNode * assertions;
308                DeclarationNode * initializer;
309        };
310        Variable_t variable;
311
312        struct Attr_t {
313//              const std::string * name;
314                ExpressionNode * expr;
315                DeclarationNode * type;
316        };
317        Attr_t attr;
318
319        BuiltinType builtin;
320
321        TypeData * type;
322
323        Type::FuncSpecifiers funcSpecs;
324        Type::StorageClasses storageClasses;
325
326        ExpressionNode * bitfieldWidth;
327        std::unique_ptr<ExpressionNode> enumeratorValue;
328        bool hasEllipsis;
329        LinkageSpec::Spec linkage;
330        ConstantExpr *asmName;
331        std::list< Attribute * > attributes;
332        InitializerNode * initializer;
333        bool extension = false;
334        std::string error;
335        StatementNode * asmStmt;
336
337        static UniqueName anonymous;
338
339        // Temp to test TreeStruct
340        const std::string * parent_name;
341}; // DeclarationNode
342
343Type * buildType( TypeData * type );
344
345static inline Type * maybeMoveBuildType( const DeclarationNode * orig ) {
346        Type * ret = orig ? orig->buildType() : nullptr;
347        delete orig;
348        return ret;
349}
350
351//##############################################################################
352
353class StatementNode final : public ParseNode {
354  public:
355        StatementNode() { stmt = nullptr; }
356        StatementNode( Statement * stmt ) : stmt( stmt ) {}
357        StatementNode( DeclarationNode * decl );
358        virtual ~StatementNode() {}
359
360        virtual StatementNode * clone() const final { assert( false ); return nullptr; }
361        Statement * build() const { return const_cast<StatementNode *>(this)->stmt.release(); }
362
363        virtual StatementNode * add_label( const std::string * name, DeclarationNode * attr = nullptr ) {
364                stmt->get_labels().emplace_back( * name, nullptr, attr ? std::move( attr->attributes ) : std::list< Attribute * > {} );
365                delete attr;
366                delete name;
367                return this;
368        }
369
370        virtual StatementNode * append_last_case( StatementNode * );
371
372        virtual void print( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const override {}
373        virtual void printList( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const override {}
374  private:
375        std::unique_ptr<Statement> stmt;
376}; // StatementNode
377
378Statement * build_expr( ExpressionNode * ctl );
379
380struct ForCtl {
381        ForCtl( ExpressionNode * expr, ExpressionNode * condition, ExpressionNode * change ) :
382                init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
383        ForCtl( DeclarationNode * decl, ExpressionNode * condition, ExpressionNode * change ) :
384                init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
385
386        StatementNode * init;
387        ExpressionNode * condition;
388        ExpressionNode * change;
389};
390
391Statement * build_if( ExpressionNode * ctl, StatementNode * then_stmt, StatementNode * else_stmt );
392Statement * build_switch( ExpressionNode * ctl, StatementNode * stmt );
393Statement * build_case( ExpressionNode * ctl );
394Statement * build_default();
395Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind = false );
396Statement * build_for( ForCtl * forctl, StatementNode * stmt );
397Statement * build_branch( BranchStmt::Type kind );
398Statement * build_branch( std::string * identifier, BranchStmt::Type kind );
399Statement * build_computedgoto( ExpressionNode * ctl );
400Statement * build_return( ExpressionNode * ctl );
401Statement * build_throw( ExpressionNode * ctl );
402Statement * build_resume( ExpressionNode * ctl );
403Statement * build_resume_at( ExpressionNode * ctl , ExpressionNode * target );
404Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt );
405Statement * build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body );
406Statement * build_finally( StatementNode * stmt );
407Statement * build_compound( StatementNode * first );
408Statement * build_asmstmt( bool voltile, ConstantExpr * instruction, ExpressionNode * output = nullptr, ExpressionNode * input = nullptr, ExpressionNode * clobber = nullptr, LabelNode * gotolabels = nullptr );
409
410//##############################################################################
411
412template< typename SynTreeType, typename NodeType, template< typename, typename...> class Container, typename... Args >
413void buildList( const NodeType * firstNode, Container< SynTreeType *, Args... > &outputList ) {
414        SemanticError errors;
415        std::back_insert_iterator< Container< SynTreeType *, Args... > > out( outputList );
416        const NodeType * cur = firstNode;
417
418        while ( cur ) {
419                try {
420                        SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
421                        if ( result ) {
422                                result->location = cur->location;
423                                * out++ = result;
424                        } else {
425                                assertf(false, "buildList unknown type");
426                        } // if
427                } catch( SemanticError &e ) {
428                        e.set_location( cur->location );
429                        errors.append( e );
430                } // try
431                cur = dynamic_cast< NodeType * >( cur->get_next() );
432        } // while
433        if ( ! errors.isEmpty() ) {
434                throw errors;
435        } // if
436}
437
438// in DeclarationNode.cc
439void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList );
440void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList );
441void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList );
442
443template< typename SynTreeType, typename NodeType >
444void buildMoveList( const NodeType * firstNode, std::list< SynTreeType * > &outputList ) {
445        buildList( firstNode, outputList );
446        delete firstNode;
447}
448
449// in ParseNode.cc
450std::ostream & operator<<( std::ostream & out, const ParseNode * node );
451
452// Local Variables: //
453// tab-width: 4 //
454// mode: c++ //
455// compile-command: "make install" //
456// End: //
Note: See TracBrowser for help on using the repository browser.