source: src/Parser/ParseNode.h@ 6152c81

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 6152c81 was a7c90d4, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

change StorageClass to bitset, support _Thread_local as separate storage-class

  • Property mode set to 100644
File size: 18.8 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 : Tue Mar 7 08:10:53 2017
13// Update Count : 726
14//
15
16#ifndef PARSENODE_H
17#define PARSENODE_H
18
19#include <string>
20#include <list>
21#include <bitset>
22#include <iterator>
23#include <memory>
24
25#include "Parser/LinkageSpec.h"
26#include "SynTree/Type.h"
27#include "SynTree/Expression.h"
28#include "SynTree/Statement.h"
29#include "SynTree/Label.h"
30#include "Common/utility.h"
31#include "Common/UniqueName.h"
32
33class StatementNode;
34class CompoundStmtNode;
35class DeclarationNode;
36class ExpressionNode;
37class InitializerNode;
38class Attribute;
39
40//##############################################################################
41
42extern char * yyfilename;
43extern int yylineno;
44
45class ParseNode {
46 public:
47 ParseNode() {};
48 virtual ~ParseNode() { delete next; delete name; };
49 virtual ParseNode * clone() const = 0;
50
51 ParseNode * get_next() const { return next; }
52 ParseNode * set_next( ParseNode * newlink ) { next = newlink; return this; }
53
54 ParseNode * get_last() {
55 ParseNode * current;
56 for ( current = this; current->get_next() != nullptr; current = current->get_next() );
57 return current;
58 }
59 ParseNode * set_last( ParseNode * newlast ) {
60 if ( newlast != nullptr ) get_last()->set_next( newlast );
61 return this;
62 }
63
64 virtual void print( std::ostream &os, int indent = 0 ) const {}
65 virtual void printList( std::ostream &os, int indent = 0 ) const {}
66
67 static int indent_by;
68
69 ParseNode * next = nullptr;
70 std::string * name = nullptr;
71 CodeLocation location = { yyfilename, yylineno };
72}; // ParseNode
73
74//##############################################################################
75
76class InitializerNode : public ParseNode {
77 public:
78 InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode * des = nullptr );
79 InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode * des = nullptr );
80 ~InitializerNode();
81 virtual InitializerNode * clone() const { assert( false ); return nullptr; }
82
83 ExpressionNode * get_expression() const { return expr; }
84
85 InitializerNode * set_designators( ExpressionNode * des ) { designator = des; return this; }
86 ExpressionNode * get_designators() const { return designator; }
87
88 InitializerNode * set_maybeConstructed( bool value ) { maybeConstructed = value; return this; }
89 bool get_maybeConstructed() const { return maybeConstructed; }
90
91 InitializerNode * next_init() const { return kids; }
92
93 void print( std::ostream &os, int indent = 0 ) const;
94 void printOneLine( std::ostream & ) const;
95
96 virtual Initializer * build() const;
97 private:
98 ExpressionNode * expr;
99 bool aggregate;
100 ExpressionNode * designator; // may be list
101 InitializerNode * kids;
102 bool maybeConstructed;
103}; // InitializerNode
104
105//##############################################################################
106
107class ExpressionNode final : public ParseNode {
108 public:
109 ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
110 ExpressionNode( const ExpressionNode &other );
111 virtual ~ExpressionNode() {}
112 virtual ExpressionNode * clone() const override { return expr ? new ExpressionNode( expr->clone() ) : nullptr; }
113
114 bool get_extension() const { return extension; }
115 ExpressionNode * set_extension( bool exten ) { extension = exten; return this; }
116
117 virtual void print( std::ostream &os, int indent = 0 ) const override {}
118 void printOneLine( std::ostream &os, int indent = 0 ) const {}
119
120 template<typename T>
121 bool isExpressionType() const {
122 return nullptr != dynamic_cast<T>(expr.get());
123 }
124
125 Expression * build() const { return const_cast<ExpressionNode *>(this)->expr.release(); }
126 private:
127 bool extension = false;
128 std::unique_ptr<Expression> expr;
129}; // ExpressionNode
130
131template< typename T >
132struct maybeBuild_t< Expression, T > {
133 static inline Expression * doit( const T * orig ) {
134 if ( orig ) {
135 Expression * p = orig->build();
136 p->set_extension( orig->get_extension() );
137 return p;
138 } else {
139 return nullptr;
140 } // if
141 }
142};
143
144enum class OperKinds {
145 // diadic
146 SizeOf, AlignOf, OffsetOf, Plus, Minus, Mul, Div, Mod, Or, And,
147 BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
148 Assign, AtAssn, MulAssn, DivAssn, ModAssn, PlusAssn, MinusAssn, LSAssn, RSAssn, AndAssn, ERAssn, OrAssn,
149 Index, Range,
150 // monadic
151 UnPlus, UnMinus, AddressOf, PointTo, Neg, BitNeg, Incr, IncrPost, Decr, DecrPost, LabelAddress,
152 Ctor, Dtor,
153}; // OperKinds
154
155struct LabelNode {
156 std::list< Label > labels;
157};
158
159Expression * build_constantInteger( const std::string &str );
160Expression * build_constantFloat( const std::string &str );
161Expression * build_constantChar( const std::string &str );
162Expression * build_constantZeroOne( const std::string &str );
163ConstantExpr * build_constantStr( const std::string &str );
164Expression * build_field_name_FLOATINGconstant( const std::string & str );
165Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts );
166Expression * build_field_name_REALFRACTIONconstant( const std::string & str );
167Expression * build_field_name_REALDECIMALconstant( const std::string & str );
168
169NameExpr * build_varref( const std::string * name, bool labelp = false );
170Expression * build_typevalue( DeclarationNode * decl );
171
172Expression * build_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 // These must remain in the same order as the corresponding DeclarationNode names.
205
206 enum StorageClass { Extern, Static, Auto, Register, Threadlocal, NoStorageClass,
207 ExternClass = 1 << Extern, StaticClass = 1 << Static, AutoClass = 1 << Auto, RegisterClass = 1 << Register, ThreadlocalClass = 1 << Threadlocal };
208 enum FuncSpecifier { Inline, Noreturn, Fortran, NoFuncSpecifier,
209 InlineSpec = 1 << Inline, NoreturnSpec = 1 << Noreturn, FortranSpec = 1 << Fortran };
210 enum TypeQualifier { Const, Restrict, Volatile, Lvalue, Mutex, Atomic, NoTypeQualifier };
211 enum BasicType { Void, Bool, Char, Int, Float, Double, LongDouble, NoBasicType };
212 enum ComplexType { Complex, Imaginary, NoComplexType };
213 enum Signedness { Signed, Unsigned, NoSignedness };
214 enum Length { Short, Long, LongLong, NoLength };
215 enum Aggregate { Struct, Union, Trait, NoAggregate };
216 enum TypeClass { Otype, Dtype, Ftype, Ttype, NoTypeClass };
217 enum BuiltinType { Valist, Zero, One, NoBuiltinType };
218
219 static const char * storageClassNames[];
220 static const char * funcSpecifierNames[];
221 static const char * typeQualifierNames[];
222 static const char * basicTypeNames[];
223 static const char * complexTypeNames[];
224 static const char * signednessNames[];
225 static const char * lengthNames[];
226 static const char * aggregateNames[];
227 static const char * typeClassNames[];
228 static const char * builtinTypeNames[];
229
230 static DeclarationNode * newStorageClass( StorageClass );
231 static DeclarationNode * newFuncSpecifier( FuncSpecifier );
232 static DeclarationNode * newTypeQualifier( TypeQualifier );
233 static DeclarationNode * newBasicType( BasicType );
234 static DeclarationNode * newComplexType( ComplexType );
235 static DeclarationNode * newSignedNess( Signedness );
236 static DeclarationNode * newLength( Length );
237 static DeclarationNode * newBuiltinType( BuiltinType );
238 static DeclarationNode * newForall( DeclarationNode * );
239 static DeclarationNode * newFromTypedef( std::string * );
240 static DeclarationNode * newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle = false );
241 static DeclarationNode * newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
242 static DeclarationNode * newEnum( std::string * name, DeclarationNode * constants, bool body );
243 static DeclarationNode * newEnumConstant( std::string * name, ExpressionNode * constant );
244 static DeclarationNode * newName( std::string * );
245 static DeclarationNode * newFromTypeGen( std::string *, ExpressionNode * params );
246 static DeclarationNode * newTypeParam( TypeClass, std::string * );
247 static DeclarationNode * newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts );
248 static DeclarationNode * newTraitUse( const std::string * name, ExpressionNode * params );
249 static DeclarationNode * newTypeDecl( std::string * name, DeclarationNode * typeParams );
250 static DeclarationNode * newPointer( DeclarationNode * qualifiers );
251 static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic );
252 static DeclarationNode * newVarArray( DeclarationNode * qualifiers );
253 static DeclarationNode * newBitfield( ExpressionNode * size );
254 static DeclarationNode * newTuple( DeclarationNode * members );
255 static DeclarationNode * newTypeof( ExpressionNode * expr );
256 static DeclarationNode * newAttr( std::string *, ExpressionNode * expr ); // @ attributes
257 static DeclarationNode * newAttr( std::string *, DeclarationNode * type ); // @ attributes
258 static DeclarationNode * newAttribute( std::string *, ExpressionNode * expr = nullptr ); // gcc attributes
259 static DeclarationNode * newAsmStmt( StatementNode * stmt ); // gcc external asm statement
260
261 DeclarationNode();
262 ~DeclarationNode();
263 DeclarationNode * clone() const override;
264
265 DeclarationNode * addQualifiers( DeclarationNode * );
266 void checkQualifiers( const TypeData *, const TypeData * );
267 void checkSpecifiers( DeclarationNode * );
268 DeclarationNode * copySpecifiers( DeclarationNode * );
269 DeclarationNode * addType( DeclarationNode * );
270 DeclarationNode * addTypedef();
271 DeclarationNode * addAssertions( DeclarationNode * );
272 DeclarationNode * addName( std::string * );
273 DeclarationNode * addAsmName( DeclarationNode * );
274 DeclarationNode * addBitfield( ExpressionNode * size );
275 DeclarationNode * addVarArgs();
276 DeclarationNode * addFunctionBody( StatementNode * body );
277 DeclarationNode * addOldDeclList( DeclarationNode * list );
278 DeclarationNode * setBase( TypeData * newType );
279 DeclarationNode * copyAttribute( DeclarationNode * attr );
280 DeclarationNode * addPointer( DeclarationNode * qualifiers );
281 DeclarationNode * addArray( DeclarationNode * array );
282 DeclarationNode * addNewPointer( DeclarationNode * pointer );
283 DeclarationNode * addNewArray( DeclarationNode * array );
284 DeclarationNode * addParamList( DeclarationNode * list );
285 DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions
286 DeclarationNode * addInitializer( InitializerNode * init );
287
288 DeclarationNode * cloneType( std::string * newName );
289 DeclarationNode * cloneBaseType( DeclarationNode * newdecl );
290
291 DeclarationNode * appendList( DeclarationNode * node ) {
292 return (DeclarationNode *)set_last( node );
293 }
294
295 virtual void print( std::ostream &os, int indent = 0 ) const override;
296 virtual void printList( std::ostream &os, int indent = 0 ) const override;
297
298 Declaration * build() const;
299 Type * buildType() const;
300
301 bool get_hasEllipsis() const;
302 LinkageSpec::Spec get_linkage() const { return linkage; }
303 DeclarationNode * extractAggregate() const;
304 bool has_enumeratorValue() const { return (bool)enumeratorValue; }
305 ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode *>(this)->enumeratorValue.release(); }
306
307 bool get_extension() const { return extension; }
308 DeclarationNode * set_extension( bool exten ) { extension = exten; return this; }
309 public:
310 struct Variable_t {
311// const std::string * name;
312 DeclarationNode::TypeClass tyClass;
313 DeclarationNode * assertions;
314 };
315 Variable_t variable;
316
317 struct Attr_t {
318// const std::string * name;
319 ExpressionNode * expr;
320 DeclarationNode * type;
321 };
322 Attr_t attr;
323
324 BuiltinType builtin;
325
326 TypeData * type;
327
328 typedef std::bitset< DeclarationNode::NoStorageClass > StorageClasses;
329 StorageClasses storageClasses;
330 static void print_StorageClass( std::ostream & output, StorageClasses storageClasses );
331
332 typedef std::bitset< DeclarationNode::NoFuncSpecifier > FuncSpecifiers;
333 FuncSpecifiers funcSpecs;
334 static void print_FuncSpec( std::ostream & output, FuncSpecifiers funcSpecs );
335
336 ExpressionNode * bitfieldWidth;
337 std::unique_ptr<ExpressionNode> enumeratorValue;
338 bool hasEllipsis;
339 LinkageSpec::Spec linkage;
340 ConstantExpr *asmName;
341 std::list< Attribute * > attributes;
342 InitializerNode * initializer;
343 bool extension = false;
344 std::string error;
345 StatementNode * asmStmt;
346
347 static UniqueName anonymous;
348}; // DeclarationNode
349
350Type * buildType( TypeData * type );
351
352static inline Type * maybeMoveBuildType( const DeclarationNode * orig ) {
353 Type * ret = orig ? orig->buildType() : nullptr;
354 delete orig;
355 return ret;
356}
357
358//##############################################################################
359
360class StatementNode final : public ParseNode {
361 public:
362 StatementNode() { stmt = nullptr; }
363 StatementNode( Statement * stmt ) : stmt( stmt ) {}
364 StatementNode( DeclarationNode * decl );
365 virtual ~StatementNode() {}
366
367 virtual StatementNode * clone() const final { assert( false ); return nullptr; }
368 Statement * build() const { return const_cast<StatementNode *>(this)->stmt.release(); }
369
370 virtual StatementNode * add_label( const std::string * name, DeclarationNode * attr = nullptr ) {
371 stmt->get_labels().emplace_back( * name, nullptr, attr ? std::move( attr->attributes ) : std::list< Attribute * > {} );
372 delete attr;
373 delete name;
374 return this;
375 }
376
377 virtual StatementNode * append_last_case( StatementNode * );
378
379 virtual void print( std::ostream &os, int indent = 0 ) const override {}
380 virtual void printList( std::ostream &os, int indent = 0 ) const override {}
381 private:
382 std::unique_ptr<Statement> stmt;
383}; // StatementNode
384
385Statement * build_expr( ExpressionNode * ctl );
386
387struct ForCtl {
388 ForCtl( ExpressionNode * expr, ExpressionNode * condition, ExpressionNode * change ) :
389 init( new StatementNode( build_expr( expr ) ) ), condition( condition ), change( change ) {}
390 ForCtl( DeclarationNode * decl, ExpressionNode * condition, ExpressionNode * change ) :
391 init( new StatementNode( decl ) ), condition( condition ), change( change ) {}
392
393 StatementNode * init;
394 ExpressionNode * condition;
395 ExpressionNode * change;
396};
397
398Statement * build_if( ExpressionNode * ctl, StatementNode * then_stmt, StatementNode * else_stmt );
399Statement * build_switch( ExpressionNode * ctl, StatementNode * stmt );
400Statement * build_case( ExpressionNode * ctl );
401Statement * build_default();
402Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind = false );
403Statement * build_for( ForCtl * forctl, StatementNode * stmt );
404Statement * build_branch( BranchStmt::Type kind );
405Statement * build_branch( std::string * identifier, BranchStmt::Type kind );
406Statement * build_computedgoto( ExpressionNode * ctl );
407Statement * build_return( ExpressionNode * ctl );
408Statement * build_throw( ExpressionNode * ctl );
409Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt );
410Statement * build_catch( DeclarationNode * decl, StatementNode * stmt, bool catchAny = false );
411Statement * build_finally( StatementNode * stmt );
412Statement * build_compound( StatementNode * first );
413Statement * build_asmstmt( bool voltile, ConstantExpr * instruction, ExpressionNode * output = nullptr, ExpressionNode * input = nullptr, ExpressionNode * clobber = nullptr, LabelNode * gotolabels = nullptr );
414
415//##############################################################################
416
417template< typename SynTreeType, typename NodeType, template< typename, typename...> class Container, typename... Args >
418void buildList( const NodeType * firstNode, Container< SynTreeType *, Args... > &outputList ) {
419 SemanticError errors;
420 std::back_insert_iterator< Container< SynTreeType *, Args... > > out( outputList );
421 const NodeType * cur = firstNode;
422
423 while ( cur ) {
424 try {
425 SynTreeType * result = dynamic_cast< SynTreeType * >( maybeBuild< typename std::pointer_traits< decltype(cur->build())>::element_type >( cur ) );
426 if ( result ) {
427 result->location = cur->location;
428 * out++ = result;
429 } // if
430 } catch( SemanticError &e ) {
431 e.set_location( cur->location );
432 errors.append( e );
433 } // try
434 cur = dynamic_cast< NodeType * >( cur->get_next() );
435 } // while
436 if ( ! errors.isEmpty() ) {
437 throw errors;
438 } // if
439}
440
441// in DeclarationNode.cc
442void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList );
443void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList );
444void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList );
445
446template< typename SynTreeType, typename NodeType >
447void buildMoveList( const NodeType * firstNode, std::list< SynTreeType * > &outputList ) {
448 buildList( firstNode, outputList );
449 delete firstNode;
450}
451
452// in ParseNode.cc
453std::ostream & operator<<( std::ostream & out, const ParseNode * node );
454
455#endif // PARSENODE_H
456
457// Local Variables: //
458// tab-width: 4 //
459// mode: c++ //
460// compile-command: "make install" //
461// End: //
Note: See TracBrowser for help on using the repository browser.