source: src/Parser/ParseNode.h@ cd99ef1

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

second attempt add exponential operator

  • 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 : Peter A. Buhr
12// Last Modified On : Sat Jul 15 16:00:48 2017
13// Update Count : 785
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;
37class Attribute;
38
39//##############################################################################
40
41extern char * yyfilename;
42extern int yylineno;
43
44class ParseNode {
45 public:
46 ParseNode() {};
47 virtual ~ParseNode() { delete next; delete name; };
48 virtual ParseNode * clone() const = 0;
49
50 ParseNode * get_next() const { return next; }
51 ParseNode * set_next( ParseNode * newlink ) { next = newlink; return this; }
52
53 ParseNode * get_last() {
54 ParseNode * current;
55 for ( current = this; current->get_next() != nullptr; current = current->get_next() );
56 return current;
57 }
58 ParseNode * set_last( ParseNode * newlast ) {
59 if ( newlast != nullptr ) get_last()->set_next( newlast );
60 return this;
61 }
62
63 virtual void print( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const {}
64 virtual void printList( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const {}
65
66 static int indent_by;
67
68 ParseNode * next = nullptr;
69 std::string * name = nullptr;
70 CodeLocation location = { yyfilename, yylineno };
71}; // ParseNode
72
73//##############################################################################
74
75class InitializerNode : public ParseNode {
76 public:
77 InitializerNode( ExpressionNode *, bool aggrp = false, ExpressionNode * des = nullptr );
78 InitializerNode( InitializerNode *, bool aggrp = false, ExpressionNode * des = nullptr );
79 ~InitializerNode();
80 virtual InitializerNode * clone() const { assert( false ); return nullptr; }
81
82 ExpressionNode * get_expression() const { return expr; }
83
84 InitializerNode * set_designators( ExpressionNode * des ) { designator = des; return this; }
85 ExpressionNode * get_designators() const { return designator; }
86
87 InitializerNode * set_maybeConstructed( bool value ) { maybeConstructed = value; return this; }
88 bool get_maybeConstructed() const { return maybeConstructed; }
89
90 InitializerNode * next_init() const { return kids; }
91
92 void print( std::ostream &os, int indent = 0 ) const;
93 void printOneLine( std::ostream & ) const;
94
95 virtual Initializer * build() const;
96 private:
97 ExpressionNode * expr;
98 bool aggregate;
99 ExpressionNode * designator; // may be list
100 InitializerNode * kids;
101 bool maybeConstructed;
102}; // InitializerNode
103
104//##############################################################################
105
106class ExpressionNode final : public ParseNode {
107 public:
108 ExpressionNode( Expression * expr = nullptr ) : expr( expr ) {}
109 virtual ~ExpressionNode() {}
110 virtual ExpressionNode * clone() const override { return expr ? static_cast<ExpressionNode*>((new ExpressionNode( expr->clone() ))->set_next( maybeClone( get_next() ) )) : nullptr; }
111
112 bool get_extension() const { return extension; }
113 ExpressionNode * set_extension( bool exten ) { extension = exten; return this; }
114
115 virtual void print( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const override {}
116 void printOneLine( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent = 0 ) const {}
117
118 template<typename T>
119 bool isExpressionType() const {
120 return nullptr != dynamic_cast<T>(expr.get());
121 }
122
123 Expression * build() const { return const_cast<ExpressionNode *>(this)->expr.release(); }
124 private:
125 bool extension = false;
126 std::unique_ptr<Expression> expr;
127}; // ExpressionNode
128
129template< typename T >
130struct maybeBuild_t< Expression, T > {
131 static inline Expression * doit( const T * orig ) {
132 if ( orig ) {
133 Expression * p = orig->build();
134 p->set_extension( orig->get_extension() );
135 p->location = orig->location;
136 return p;
137 } else {
138 return nullptr;
139 } // if
140 }
141};
142
143// Must harmonize with OperName.
144enum class OperKinds {
145 // diadic
146 SizeOf, AlignOf, OffsetOf, Plus, Minus, Exp, Mul, Div, Mod, Or, And,
147 BitOr, BitAnd, Xor, Cast, LShift, RShift, LThan, GThan, LEThan, GEThan, Eq, Neq,
148 Assign, AtAssn, ExpAssn, 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 );
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 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#endif // PARSENODE_H
453
454// Local Variables: //
455// tab-width: 4 //
456// mode: c++ //
457// compile-command: "make install" //
458// End: //
Note: See TracBrowser for help on using the repository browser.