source: src/Parser/ParseNode.h@ d88f256a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since d88f256a was b6424d9, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

refactor copyStorageClasses

  • Property mode set to 100644
File size: 17.2 KB
RevLine 
[b87a5ed]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//
[974906e2]7// ParseNode.h --
[b87a5ed]8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 13:28:16 2015
[e04ef3a]11// Last Modified By : Peter A. Buhr
[b6424d9]12// Last Modified On : Sat Sep 10 17:14:37 2016
13// Update Count : 589
[b87a5ed]14//
15
[51b73452]16#ifndef PARSENODE_H
17#define PARSENODE_H
18
19#include <string>
20#include <list>
21#include <iterator>
[e04ef3a]22#include <memory>
[51b73452]23
[68cd1ce]24#include "Parser/LinkageSpec.h"
[59db689]25#include "SynTree/Type.h"
[e04ef3a]26#include "SynTree/Expression.h"
[2f22cc4]27#include "SynTree/Statement.h"
[0f8e4ac]28#include "SynTree/Label.h"
[7880579]29#include "Common/utility.h"
30#include "Common/UniqueName.h"
[51b73452]31
32class StatementNode;
33class CompoundStmtNode;
34class DeclarationNode;
[d1625f8]35class ExpressionNode;
[51b73452]36class InitializerNode;
37
[7880579]38//##############################################################################
39
[51b73452]40class ParseNode {
[bdd516a]41 public:
[99cad3aa]42 ParseNode() {};
[b6424d9]43 ParseNode( const std::string * name ) : name( * name ) { assert( false ); delete name; }
[99cad3aa]44 ParseNode( const std::string &name ) : name( name ) { assert( false ); }
45 virtual ~ParseNode() { delete next; };
[b6424d9]46 virtual ParseNode * clone() const = 0;
[51b73452]47
[b6424d9]48 ParseNode * get_next() const { return next; }
49 ParseNode * set_next( ParseNode * newlink ) { next = newlink; return this; }
50 ParseNode * get_last() {
51 ParseNode * current;
[99cad3aa]52 for ( current = this; current->get_next() != 0; current = current->get_next() );
53 return current;
54 }
[b6424d9]55 ParseNode * set_last( ParseNode * newlast ) {
[99cad3aa]56 if ( newlast != 0 ) get_last()->set_next( newlast );
57 return this;
58 }
[51b73452]59
[e869d663]60 const std::string &get_name() const { return name; }
61 void set_name( const std::string &newValue ) { name = newValue; }
62
[7880579]63 virtual void print( std::ostream &os, int indent = 0 ) const {}
[0da3e2c]64 virtual void printList( std::ostream &os, int indent = 0 ) const {}
[7880579]65 private:
[b87a5ed]66 static int indent_by;
[7880579]67
[b6424d9]68 ParseNode * next = nullptr;
[7880579]69 std::string name;
70}; // ParseNode
[51b73452]71
[7bf7fb9]72//##############################################################################
73
[d1625f8]74class InitializerNode : public ParseNode {
75 public:
[b6424d9]76 InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode * des = 0 );
77 InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode * des = 0 );
[d1625f8]78 ~InitializerNode();
[b6424d9]79 virtual InitializerNode * clone() const { assert( false ); return nullptr; }
[d1625f8]80
[b6424d9]81 ExpressionNode * get_expression() const { return expr; }
[d1625f8]82
[b6424d9]83 InitializerNode * set_designators( ExpressionNode * des ) { designator = des; return this; }
84 ExpressionNode * get_designators() const { return designator; }
[d1625f8]85
[b6424d9]86 InitializerNode * set_maybeConstructed( bool value ) { maybeConstructed = value; return this; }
[d1625f8]87 bool get_maybeConstructed() const { return maybeConstructed; }
88
[b6424d9]89 InitializerNode * next_init() const { return kids; }
[d1625f8]90
91 void print( std::ostream &os, int indent = 0 ) const;
92 void printOneLine( std::ostream & ) const;
93
[b6424d9]94 virtual Initializer * build() const;
[d1625f8]95 private:
[b6424d9]96 ExpressionNode * expr;
[d1625f8]97 bool aggregate;
[b6424d9]98 ExpressionNode * designator; // may be list
99 InitializerNode * kids;
[d1625f8]100 bool maybeConstructed;
[c1c1112]101}; // InitializerNode
[d1625f8]102
103//##############################################################################
104
[ac71a86]105class ExpressionNode final : public ParseNode {
[bdd516a]106 public:
[d1625f8]107 ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
[b6424d9]108 ExpressionNode( Expression * expr, const std::string * name ) : ParseNode( name ), expr( expr ) {}
[b87a5ed]109 ExpressionNode( const ExpressionNode &other );
[d1625f8]110 virtual ~ExpressionNode() {}
[b6424d9]111 virtual ExpressionNode * clone() const { return expr ? new ExpressionNode( expr->clone() ) : nullptr; }
[51b73452]112
[e04ef3a]113 bool get_extension() const { return extension; }
[b6424d9]114 ExpressionNode * set_extension( bool exten ) { extension = exten; return this; }
[51b73452]115
[ac71a86]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 }
[51b73452]123
[b6424d9]124 Expression * build() const { return const_cast<ExpressionNode*>(this)->expr.release(); }
[bdd516a]125 private:
[e04ef3a]126 bool extension = false;
[ac71a86]127 std::unique_ptr<Expression> expr;
[c1c1112]128}; // ExpressionNode
[e04ef3a]129
130template< typename T >
[7880579]131struct maybeBuild_t< Expression, T > {
[b6424d9]132 static inline Expression * doit( const T * orig ) {
[e04ef3a]133 if ( orig ) {
[b6424d9]134 Expression * p = orig->build();
[e04ef3a]135 p->set_extension( orig->get_extension() );
136 return p;
137 } else {
[7880579]138 return nullptr;
[e04ef3a]139 } // if
140 }
[51b73452]141};
142
[d9e2280]143enum class OperKinds {
144 // diadic
[7bf7fb9]145 SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
[d9e2280]146 BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
[a839867]147 Assign, AtAssn, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
[d9e2280]148 Index, Range,
149 // monadic
150 UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
151 Ctor, Dtor,
[c1c1112]152}; // OperKinds
[51b73452]153
[e82aa9df]154struct LabelNode {
155 std::list< Label > labels;
156};
157
[b6424d9]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, NameExpr * member );
168Expression * build_pfieldSel( ExpressionNode * expr_node, NameExpr * 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 );
[51b73452]191
[7bf7fb9]192//##############################################################################
193
[51b73452]194class TypeData;
195
[bdd516a]196class DeclarationNode : public ParseNode {
197 public:
[c1c1112]198 enum Qualifier { Const, Restrict, Volatile, Lvalue, Atomic, NoOfQualifier };
[68cd1ce]199 enum StorageClass { Extern, Static, Auto, Register, Inline, Fortran, Noreturn, Threadlocal, NoStorageClass, };
[b87a5ed]200 enum BasicType { Char, Int, Float, Double, Void, Bool, Complex, Imaginary };
[68cd1ce]201 enum Modifier { Signed, Unsigned, Short, Long };
[4040425]202 enum Aggregate { Struct, Union, Trait };
[b87a5ed]203 enum TypeClass { Type, Dtype, Ftype };
[90c3b1c]204 enum BuiltinType { Valist };
[b87a5ed]205
[b6424d9]206 static const char * qualifierName[];
207 static const char * storageName[];
208 static const char * basicTypeName[];
209 static const char * modifierName[];
210 static const char * aggregateName[];
211 static const char * typeClassName[];
212 static const char * builtinTypeName[];
213
214 static DeclarationNode * newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle = false );
215 static DeclarationNode * newQualifier( Qualifier );
216 static DeclarationNode * newForall( DeclarationNode *);
217 static DeclarationNode * newStorageClass( StorageClass );
218 static DeclarationNode * newBasicType( BasicType );
219 static DeclarationNode * newModifier( Modifier );
220 static DeclarationNode * newBuiltinType( BuiltinType );
221 static DeclarationNode * newFromTypedef( std::string *);
222 static DeclarationNode * newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
223 static DeclarationNode * newEnum( std::string * name, DeclarationNode * constants );
224 static DeclarationNode * newEnumConstant( std::string * name, ExpressionNode * constant );
225 static DeclarationNode * newName( std::string *);
226 static DeclarationNode * newFromTypeGen( std::string *, ExpressionNode * params );
227 static DeclarationNode * newTypeParam( TypeClass, std::string *);
228 static DeclarationNode * newTrait( std::string * name, DeclarationNode * params, DeclarationNode * asserts );
229 static DeclarationNode * newTraitUse( std::string * name, ExpressionNode * params );
230 static DeclarationNode * newTypeDecl( std::string * name, DeclarationNode * typeParams );
231 static DeclarationNode * newPointer( DeclarationNode * qualifiers );
232 static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic );
233 static DeclarationNode * newVarArray( DeclarationNode * qualifiers );
234 static DeclarationNode * newBitfield( ExpressionNode * size );
235 static DeclarationNode * newTuple( DeclarationNode * members );
236 static DeclarationNode * newTypeof( ExpressionNode * expr );
237 static DeclarationNode * newAttr( std::string *, ExpressionNode * expr );
238 static DeclarationNode * newAttr( std::string *, DeclarationNode * type );
[b87a5ed]239
[7880579]240 DeclarationNode();
241 ~DeclarationNode();
[b6424d9]242 DeclarationNode * clone() const;
[7880579]243
[b6424d9]244 DeclarationNode * addQualifiers( DeclarationNode *);
[413ad05]245 void checkQualifiers( const TypeData *, const TypeData * );
[b6424d9]246 void checkStorageClasses( DeclarationNode *q );
247 DeclarationNode * copyStorageClasses( DeclarationNode *);
248 DeclarationNode * addType( DeclarationNode *);
249 DeclarationNode * addTypedef();
250 DeclarationNode * addAssertions( DeclarationNode *);
251 DeclarationNode * addName( std::string *);
252 DeclarationNode * addBitfield( ExpressionNode * size );
253 DeclarationNode * addVarArgs();
254 DeclarationNode * addFunctionBody( StatementNode * body );
255 DeclarationNode * addOldDeclList( DeclarationNode * list );
256 DeclarationNode * addPointer( DeclarationNode * qualifiers );
257 DeclarationNode * addArray( DeclarationNode * array );
258 DeclarationNode * addNewPointer( DeclarationNode * pointer );
259 DeclarationNode * addNewArray( DeclarationNode * array );
260 DeclarationNode * addParamList( DeclarationNode * list );
261 DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions
262 DeclarationNode * addInitializer( InitializerNode * init );
263
264 DeclarationNode * cloneType( std::string * newName );
265 DeclarationNode * cloneType( DeclarationNode * existing );
266 DeclarationNode * cloneType( int ) { return cloneType( ( std::string *)0 ); }
267 DeclarationNode * cloneBaseType( std::string * newName );
268 DeclarationNode * cloneBaseType( DeclarationNode * newdecl );
269
270 DeclarationNode * appendList( DeclarationNode * node ) {
[99cad3aa]271 return (DeclarationNode *)set_last( node );
272 }
[b87a5ed]273
[7bf7fb9]274 void print( std::ostream &os, int indent = 0 ) const;
275 void printList( std::ostream &os, int indent = 0 ) const;
[b87a5ed]276
[b6424d9]277 Declaration * build() const;
278 ::Type * buildType() const;
[b87a5ed]279
280 bool get_hasEllipsis() const;
[5f2f2d7]281 const std::string &get_name() const { return name; }
[8b7ee09]282 LinkageSpec::Spec get_linkage() const { return linkage; }
[b6424d9]283 DeclarationNode * extractAggregate() const;
[4f147cc]284 bool has_enumeratorValue() const { return (bool)enumeratorValue; }
[b6424d9]285 ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode*>(this)->enumeratorValue.release(); }
[b87a5ed]286
[7305915]287 bool get_extension() const { return extension; }
[b6424d9]288 DeclarationNode * set_extension( bool exten ) { extension = exten; return this; }
[c1c1112]289 public:
[13e3b50]290 // StorageClass buildStorageClass() const;
291 // bool buildFuncSpecifier( StorageClass key ) const;
[b87a5ed]292
[28307be]293 struct Variable_t {
294 DeclarationNode::TypeClass tyClass;
295 std::string name;
296 DeclarationNode * assertions;
297 };
298 Variable_t variable;
299
300 struct Attr_t {
301 std::string name;
302 ExpressionNode * expr;
303 DeclarationNode * type;
304 };
305 Attr_t attr;
306
[8f6f47d7]307 BuiltinType builtin;
308
[b6424d9]309 TypeData * type;
[b87a5ed]310 std::string name;
[13e3b50]311 // std::list< StorageClass > storageClasses;
312 StorageClass storageClass;
313 bool isInline, isNoreturn;
[1db21619]314 std::list< std::string > attributes;
[b6424d9]315 ExpressionNode * bitfieldWidth;
[4f147cc]316 std::unique_ptr<ExpressionNode> enumeratorValue;
[b6424d9]317 InitializerNode * initializer;
[b87a5ed]318 bool hasEllipsis;
[8b7ee09]319 LinkageSpec::Spec linkage;
[7305915]320 bool extension = false;
[13e3b50]321 std::string error;
[b87a5ed]322
323 static UniqueName anonymous;
[1db21619]324}; // DeclarationNode
[51b73452]325
[b6424d9]326Type * buildType( TypeData * type );
[413ad05]327//Type::Qualifiers buildQualifiers( const TypeData::Qualifiers & qualifiers );
[d1625f8]328
[b6424d9]329static inline Type * maybeMoveBuildType( const DeclarationNode * orig ) {
[4f147cc]330 Type* ret = orig ? orig->buildType() : nullptr;
331 delete orig;
332 return ret;
333}
334
[7bf7fb9]335//##############################################################################
336
[ac71a86]337class StatementNode final : public ParseNode {
[bdd516a]338 public:
[e82aa9df]339 StatementNode() { stmt = nullptr; }
[b6424d9]340 StatementNode( Statement * stmt ) : stmt( stmt ) {}
341 StatementNode( DeclarationNode * decl );
[e82aa9df]342 virtual ~StatementNode() {}
[51b73452]343
[b6424d9]344 virtual StatementNode * clone() const final { assert( false ); return nullptr; }
345 Statement * build() const { return const_cast<StatementNode*>(this)->stmt.release(); }
[2f22cc4]346
[b6424d9]347 virtual StatementNode * add_label( const std::string * name ) {
348 stmt->get_labels().emplace_back( * name );
[ac71a86]349 delete name;
[2f22cc4]350 return this;
351 }
352
[b6424d9]353 virtual StatementNode * append_last_case( StatementNode * );
[1d4580a]354
[2f22cc4]355 virtual void print( std::ostream &os, int indent = 0 ) {}
356 virtual void printList( std::ostream &os, int indent = 0 ) {}
357 private:
[ac71a86]358 std::unique_ptr<Statement> stmt;
[2f22cc4]359}; // StatementNode
360
[b6424d9]361Statement * build_expr( ExpressionNode * ctl );
[1d4580a]362
[2f22cc4]363struct ForCtl {
[b6424d9]364 ForCtl( ExpressionNode * expr, ExpressionNode * condition, ExpressionNode * change ) :
[e82aa9df]365 init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
[b6424d9]366 ForCtl( DeclarationNode * decl, ExpressionNode * condition, ExpressionNode * change ) :
[e82aa9df]367 init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
[2f22cc4]368
[b6424d9]369 StatementNode * init;
370 ExpressionNode * condition;
371 ExpressionNode * change;
[2f22cc4]372};
373
[b6424d9]374Statement * build_if( ExpressionNode * ctl, StatementNode * then_stmt, StatementNode * else_stmt );
375Statement * build_switch( ExpressionNode * ctl, StatementNode * stmt );
376Statement * build_case( ExpressionNode * ctl );
377Statement * build_default();
378Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind = false );
379Statement * build_for( ForCtl * forctl, StatementNode * stmt );
380Statement * build_branch( BranchStmt::Type kind );
381Statement * build_branch( std::string * identifier, BranchStmt::Type kind );
382Statement * build_computedgoto( ExpressionNode * ctl );
383Statement * build_return( ExpressionNode * ctl );
384Statement * build_throw( ExpressionNode * ctl );
385Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt );
386Statement * build_catch( DeclarationNode * decl, StatementNode * stmt, bool catchAny = false );
387Statement * build_finally( StatementNode * stmt );
388Statement * build_compound( StatementNode * first );
389Statement * build_asmstmt( bool voltile, ConstantExpr * instruction, ExpressionNode * output = 0, ExpressionNode * input = 0, ExpressionNode * clobber = 0, LabelNode * gotolabels = 0 );
[7f5566b]390
[7bf7fb9]391//##############################################################################
392
[51b73452]393template< typename SynTreeType, typename NodeType >
[b6424d9]394void buildList( const NodeType * firstNode, std::list< SynTreeType * > &outputList ) {
[b87a5ed]395 SemanticError errors;
[7880579]396 std::back_insert_iterator< std::list< SynTreeType * > > out( outputList );
[b6424d9]397 const NodeType * cur = firstNode;
[b87a5ed]398
399 while ( cur ) {
400 try {
[b6424d9]401// SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::result_of< decltype(&NodeType::build)(NodeType)>::type >( cur ) );
402 SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
[b87a5ed]403 if ( result ) {
[b6424d9]404 * out++ = result;
[b87a5ed]405 } // if
406 } catch( SemanticError &e ) {
407 errors.append( e );
408 } // try
[7880579]409 cur = dynamic_cast< NodeType * >( cur->get_next() );
[b87a5ed]410 } // while
[a32b204]411 if ( ! errors.isEmpty() ) {
[b87a5ed]412 throw errors;
413 } // if
[51b73452]414}
415
416// in DeclarationNode.cc
[b6424d9]417void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList );
418void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList );
419void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList );
[51b73452]420
[7ecbb7e]421template< typename SynTreeType, typename NodeType >
[b6424d9]422void buildMoveList( const NodeType * firstNode, std::list< SynTreeType * > &outputList ) {
[7ecbb7e]423 buildList(firstNode, outputList);
424 delete firstNode;
425}
426
[c8dfcd3]427// in ParseNode.cc
428std::ostream & operator<<( std::ostream & out, const ParseNode * node );
[7ecbb7e]429
[bdd516a]430#endif // PARSENODE_H
[51b73452]431
432// Local Variables: //
[b87a5ed]433// tab-width: 4 //
434// mode: c++ //
435// compile-command: "make install" //
[51b73452]436// End: //
Note: See TracBrowser for help on using the repository browser.