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 | // Expression.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Jul 25 14:15:47 2017 |
---|
13 | // Update Count : 54 |
---|
14 | // |
---|
15 | |
---|
16 | #include "SynTree/Expression.h" |
---|
17 | |
---|
18 | #include <cassert> // for assert, assertf |
---|
19 | #include <iostream> // for ostream, operator<<, basic_ostream |
---|
20 | #include <list> // for list, _List_iterator, list<>::co... |
---|
21 | |
---|
22 | #include "Common/utility.h" // for maybeClone, cloneAll, deleteAll |
---|
23 | #include "Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
24 | #include "Expression.h" // for Expression, ImplicitCopyCtorExpr |
---|
25 | #include "InitTweak/InitTweak.h" // for getCallArg, getPointerBase |
---|
26 | #include "Initializer.h" // for Designation, Initializer |
---|
27 | #include "Statement.h" // for CompoundStmt, ExprStmt, Statement |
---|
28 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode |
---|
29 | #include "SynTree/Constant.h" // for Constant |
---|
30 | #include "Type.h" // for Type, BasicType, Type::Qualifiers |
---|
31 | #include "TypeSubstitution.h" // for TypeSubstitution |
---|
32 | |
---|
33 | #include "GenPoly/Lvalue.h" |
---|
34 | |
---|
35 | void printInferParams( const InferredParams & inferParams, std::ostream &os, Indenter indent, int level ) { |
---|
36 | if ( ! inferParams.empty() ) { |
---|
37 | os << indent << "with inferred parameters " << level << ":" << std::endl; |
---|
38 | for ( InferredParams::const_iterator i = inferParams.begin(); i != inferParams.end(); ++i ) { |
---|
39 | os << indent+1; |
---|
40 | Declaration::declFromId( i->second.decl )->printShort( os, indent+1 ); |
---|
41 | os << std::endl; |
---|
42 | printInferParams( *i->second.inferParams, os, indent+1, level+1 ); |
---|
43 | } // for |
---|
44 | } // if |
---|
45 | } |
---|
46 | |
---|
47 | Expression::Expression() : result( 0 ), env( 0 ) {} |
---|
48 | |
---|
49 | Expression::Expression( const Expression &other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), extension( other.extension ), inferParams( other.inferParams ) { |
---|
50 | } |
---|
51 | |
---|
52 | Expression::~Expression() { |
---|
53 | delete env; |
---|
54 | delete result; |
---|
55 | } |
---|
56 | |
---|
57 | void Expression::print( std::ostream &os, Indenter indent ) const { |
---|
58 | printInferParams( inferParams, os, indent+1, 0 ); |
---|
59 | |
---|
60 | if ( env ) { |
---|
61 | os << std::endl << indent << "... with environment:" << std::endl; |
---|
62 | env->print( os, indent+1 ); |
---|
63 | } // if |
---|
64 | |
---|
65 | if ( extension ) { |
---|
66 | os << std::endl << indent << "... with extension:"; |
---|
67 | } // if |
---|
68 | } |
---|
69 | |
---|
70 | ConstantExpr::ConstantExpr( Constant _c ) : Expression(), constant( _c ) { |
---|
71 | set_result( constant.get_type()->clone() ); |
---|
72 | } |
---|
73 | |
---|
74 | ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) { |
---|
75 | } |
---|
76 | |
---|
77 | ConstantExpr::~ConstantExpr() {} |
---|
78 | |
---|
79 | void ConstantExpr::print( std::ostream &os, Indenter indent ) const { |
---|
80 | os << "constant expression " ; |
---|
81 | constant.print( os ); |
---|
82 | Expression::print( os, indent ); |
---|
83 | } |
---|
84 | |
---|
85 | long long int ConstantExpr::intValue() const { |
---|
86 | if ( BasicType * basicType = dynamic_cast< BasicType * >( result ) ) { |
---|
87 | if ( basicType->isInteger() ) { |
---|
88 | return get_constant()->get_ival(); |
---|
89 | } |
---|
90 | } else if ( dynamic_cast< OneType * >( result ) ) { |
---|
91 | return 1; |
---|
92 | } else if ( dynamic_cast< ZeroType * >( result ) ) { |
---|
93 | return 0; |
---|
94 | } |
---|
95 | SemanticError( this, "Constant expression of non-integral type " ); |
---|
96 | } |
---|
97 | |
---|
98 | VariableExpr::VariableExpr( DeclarationWithType *_var ) : Expression(), var( _var ) { |
---|
99 | assert( var ); |
---|
100 | assert( var->get_type() ); |
---|
101 | Type * type = var->get_type()->clone(); |
---|
102 | type->set_lvalue( true ); |
---|
103 | |
---|
104 | // xxx - doesn't quite work yet - get different alternatives with the same cost |
---|
105 | |
---|
106 | // // enumerators are not lvalues |
---|
107 | // if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( var->get_type() ) ) { |
---|
108 | // assert( inst->baseEnum ); |
---|
109 | // EnumDecl * decl = inst->baseEnum; |
---|
110 | // long long int value; |
---|
111 | // if ( decl->valueOf( var, value ) ) { |
---|
112 | // type->set_lvalue( false ); |
---|
113 | // } |
---|
114 | // } |
---|
115 | |
---|
116 | set_result( type ); |
---|
117 | } |
---|
118 | |
---|
119 | VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) { |
---|
120 | } |
---|
121 | |
---|
122 | VariableExpr::~VariableExpr() { |
---|
123 | // don't delete the declaration, since it points somewhere else in the tree |
---|
124 | } |
---|
125 | |
---|
126 | VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) { |
---|
127 | VariableExpr * funcExpr = new VariableExpr( func ); |
---|
128 | funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) ); |
---|
129 | return funcExpr; |
---|
130 | } |
---|
131 | |
---|
132 | void VariableExpr::print( std::ostream &os, Indenter indent ) const { |
---|
133 | os << "Variable Expression: "; |
---|
134 | var->printShort(os, indent); |
---|
135 | Expression::print( os, indent ); |
---|
136 | } |
---|
137 | |
---|
138 | SizeofExpr::SizeofExpr( Expression *expr_ ) : |
---|
139 | Expression(), expr(expr_), type(0), isType(false) { |
---|
140 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
141 | } |
---|
142 | |
---|
143 | SizeofExpr::SizeofExpr( Type *type_ ) : |
---|
144 | Expression(), expr(0), type(type_), isType(true) { |
---|
145 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
146 | } |
---|
147 | |
---|
148 | SizeofExpr::SizeofExpr( const SizeofExpr &other ) : |
---|
149 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
150 | } |
---|
151 | |
---|
152 | SizeofExpr::~SizeofExpr() { |
---|
153 | delete expr; |
---|
154 | delete type; |
---|
155 | } |
---|
156 | |
---|
157 | void SizeofExpr::print( std::ostream &os, Indenter indent) const { |
---|
158 | os << "Sizeof Expression on: "; |
---|
159 | if (isType) type->print(os, indent+1); |
---|
160 | else expr->print(os, indent+1); |
---|
161 | Expression::print( os, indent ); |
---|
162 | } |
---|
163 | |
---|
164 | AlignofExpr::AlignofExpr( Expression *expr_ ) : |
---|
165 | Expression(), expr(expr_), type(0), isType(false) { |
---|
166 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
167 | } |
---|
168 | |
---|
169 | AlignofExpr::AlignofExpr( Type *type_ ) : |
---|
170 | Expression(), expr(0), type(type_), isType(true) { |
---|
171 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
172 | } |
---|
173 | |
---|
174 | AlignofExpr::AlignofExpr( const AlignofExpr &other ) : |
---|
175 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
176 | } |
---|
177 | |
---|
178 | AlignofExpr::~AlignofExpr() { |
---|
179 | delete expr; |
---|
180 | delete type; |
---|
181 | } |
---|
182 | |
---|
183 | void AlignofExpr::print( std::ostream &os, Indenter indent) const { |
---|
184 | os << "Alignof Expression on: "; |
---|
185 | if (isType) type->print(os, indent+1); |
---|
186 | else expr->print(os, indent+1); |
---|
187 | Expression::print( os, indent ); |
---|
188 | } |
---|
189 | |
---|
190 | UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type, const std::string &member ) : |
---|
191 | Expression(), type(type), member(member) { |
---|
192 | assert( type ); |
---|
193 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
194 | } |
---|
195 | |
---|
196 | UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) : |
---|
197 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {} |
---|
198 | |
---|
199 | UntypedOffsetofExpr::~UntypedOffsetofExpr() { |
---|
200 | delete type; |
---|
201 | } |
---|
202 | |
---|
203 | void UntypedOffsetofExpr::print( std::ostream &os, Indenter indent) const { |
---|
204 | os << "Untyped Offsetof Expression on member " << member << " of "; |
---|
205 | type->print(os, indent+1); |
---|
206 | Expression::print( os, indent ); |
---|
207 | } |
---|
208 | |
---|
209 | OffsetofExpr::OffsetofExpr( Type *type, DeclarationWithType *member ) : |
---|
210 | Expression(), type(type), member(member) { |
---|
211 | assert( member ); |
---|
212 | assert( type ); |
---|
213 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
214 | } |
---|
215 | |
---|
216 | OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) : |
---|
217 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {} |
---|
218 | |
---|
219 | OffsetofExpr::~OffsetofExpr() { |
---|
220 | delete type; |
---|
221 | } |
---|
222 | |
---|
223 | void OffsetofExpr::print( std::ostream &os, Indenter indent) const { |
---|
224 | os << "Offsetof Expression on member " << member->name << " of "; |
---|
225 | type->print(os, indent+1); |
---|
226 | Expression::print( os, indent ); |
---|
227 | } |
---|
228 | |
---|
229 | OffsetPackExpr::OffsetPackExpr( StructInstType *type ) : Expression(), type( type ) { |
---|
230 | assert( type ); |
---|
231 | set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) ); |
---|
232 | } |
---|
233 | |
---|
234 | OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {} |
---|
235 | |
---|
236 | OffsetPackExpr::~OffsetPackExpr() { delete type; } |
---|
237 | |
---|
238 | void OffsetPackExpr::print( std::ostream &os, Indenter indent ) const { |
---|
239 | os << "Offset pack expression on "; |
---|
240 | type->print(os, indent+1); |
---|
241 | Expression::print( os, indent ); |
---|
242 | } |
---|
243 | |
---|
244 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_ ) : |
---|
245 | Expression(), attr( attr ), expr(expr_), type(0), isType(false) { |
---|
246 | } |
---|
247 | |
---|
248 | AttrExpr::AttrExpr( Expression *attr, Type *type_ ) : |
---|
249 | Expression(), attr( attr ), expr(0), type(type_), isType(true) { |
---|
250 | } |
---|
251 | |
---|
252 | AttrExpr::AttrExpr( const AttrExpr &other ) : |
---|
253 | Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
254 | } |
---|
255 | |
---|
256 | AttrExpr::~AttrExpr() { |
---|
257 | delete attr; |
---|
258 | delete expr; |
---|
259 | delete type; |
---|
260 | } |
---|
261 | |
---|
262 | void AttrExpr::print( std::ostream &os, Indenter indent) const { |
---|
263 | os << "Attr "; |
---|
264 | attr->print( os, indent+1); |
---|
265 | if ( isType || expr ) { |
---|
266 | os << "applied to: "; |
---|
267 | if (isType) type->print(os, indent+1); |
---|
268 | else expr->print(os, indent+1); |
---|
269 | } // if |
---|
270 | Expression::print( os, indent ); |
---|
271 | } |
---|
272 | |
---|
273 | CastExpr::CastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { |
---|
274 | set_result(toType); |
---|
275 | } |
---|
276 | |
---|
277 | CastExpr::CastExpr( Expression *arg_ ) : Expression(), arg(arg_) { |
---|
278 | set_result( new VoidType( Type::Qualifiers() ) ); |
---|
279 | } |
---|
280 | |
---|
281 | CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
282 | } |
---|
283 | |
---|
284 | CastExpr::~CastExpr() { |
---|
285 | delete arg; |
---|
286 | } |
---|
287 | |
---|
288 | void CastExpr::print( std::ostream &os, Indenter indent ) const { |
---|
289 | os << "Cast of:" << std::endl << indent+1; |
---|
290 | arg->print(os, indent+1); |
---|
291 | os << std::endl << indent << "... to:"; |
---|
292 | if ( result->isVoid() ) { |
---|
293 | os << " nothing"; |
---|
294 | } else { |
---|
295 | os << std::endl << indent+1; |
---|
296 | result->print( os, indent+1 ); |
---|
297 | } // if |
---|
298 | Expression::print( os, indent ); |
---|
299 | } |
---|
300 | |
---|
301 | VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { |
---|
302 | set_result(toType); |
---|
303 | } |
---|
304 | |
---|
305 | VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
306 | } |
---|
307 | |
---|
308 | VirtualCastExpr::~VirtualCastExpr() { |
---|
309 | delete arg; |
---|
310 | } |
---|
311 | |
---|
312 | void VirtualCastExpr::print( std::ostream &os, Indenter indent ) const { |
---|
313 | os << "Virtual Cast of:" << std::endl << indent+1; |
---|
314 | arg->print(os, indent+1); |
---|
315 | os << std::endl << indent << "... to:"; |
---|
316 | if ( ! result ) { |
---|
317 | os << " unknown"; |
---|
318 | } else { |
---|
319 | os << std::endl << indent+1; |
---|
320 | result->print( os, indent+1 ); |
---|
321 | } // if |
---|
322 | Expression::print( os, indent ); |
---|
323 | } |
---|
324 | |
---|
325 | UntypedMemberExpr::UntypedMemberExpr( Expression * member, Expression *aggregate ) : |
---|
326 | Expression(), member(member), aggregate(aggregate) { |
---|
327 | assert( aggregate ); |
---|
328 | } |
---|
329 | |
---|
330 | UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) : |
---|
331 | Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) { |
---|
332 | } |
---|
333 | |
---|
334 | UntypedMemberExpr::~UntypedMemberExpr() { |
---|
335 | delete aggregate; |
---|
336 | delete member; |
---|
337 | } |
---|
338 | |
---|
339 | void UntypedMemberExpr::print( std::ostream &os, Indenter indent ) const { |
---|
340 | os << "Untyped Member Expression, with field: " << std::endl << indent+1; |
---|
341 | member->print(os, indent+1 ); |
---|
342 | os << indent << "... from aggregate: " << std::endl << indent+1; |
---|
343 | aggregate->print(os, indent+1); |
---|
344 | Expression::print( os, indent ); |
---|
345 | } |
---|
346 | |
---|
347 | MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) : |
---|
348 | Expression(), member(member), aggregate(aggregate) { |
---|
349 | assert( member ); |
---|
350 | assert( aggregate ); |
---|
351 | assert( aggregate->result ); |
---|
352 | |
---|
353 | TypeSubstitution sub = aggregate->result->genericSubstitution(); |
---|
354 | Type * res = member->get_type()->clone(); |
---|
355 | sub.apply( res ); |
---|
356 | result = res; |
---|
357 | result->set_lvalue( true ); |
---|
358 | result->get_qualifiers() |= aggregate->result->get_qualifiers(); |
---|
359 | } |
---|
360 | |
---|
361 | MemberExpr::MemberExpr( const MemberExpr &other ) : |
---|
362 | Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) { |
---|
363 | } |
---|
364 | |
---|
365 | MemberExpr::~MemberExpr() { |
---|
366 | // don't delete the member declaration, since it points somewhere else in the tree |
---|
367 | delete aggregate; |
---|
368 | } |
---|
369 | |
---|
370 | void MemberExpr::print( std::ostream &os, Indenter indent ) const { |
---|
371 | os << "Member Expression, with field: " << std::endl; |
---|
372 | os << indent+1; |
---|
373 | member->print( os, indent+1 ); |
---|
374 | os << std::endl << indent << "... from aggregate: " << std::endl << indent+1; |
---|
375 | aggregate->print(os, indent + 1); |
---|
376 | Expression::print( os, indent ); |
---|
377 | } |
---|
378 | |
---|
379 | UntypedExpr::UntypedExpr( Expression *function, const std::list<Expression *> &args ) : |
---|
380 | Expression(), function(function), args(args) {} |
---|
381 | |
---|
382 | UntypedExpr::UntypedExpr( const UntypedExpr &other ) : |
---|
383 | Expression( other ), function( maybeClone( other.function ) ) { |
---|
384 | cloneAll( other.args, args ); |
---|
385 | } |
---|
386 | |
---|
387 | UntypedExpr::~UntypedExpr() { |
---|
388 | delete function; |
---|
389 | deleteAll( args ); |
---|
390 | } |
---|
391 | |
---|
392 | UntypedExpr * UntypedExpr::createDeref( Expression * expr ) { |
---|
393 | UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } ); |
---|
394 | if ( Type * type = expr->get_result() ) { |
---|
395 | Type * base = InitTweak::getPointerBase( type ); |
---|
396 | assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() ); |
---|
397 | ret->set_result( base->clone() ); |
---|
398 | if ( GenPoly::referencesPermissable() ) { |
---|
399 | // if references are still allowed in the AST, dereference returns a reference |
---|
400 | ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) ); |
---|
401 | } else { |
---|
402 | // references have been removed, in which case dereference returns an lvalue of the base type. |
---|
403 | ret->result->set_lvalue( true ); |
---|
404 | } |
---|
405 | } |
---|
406 | return ret; |
---|
407 | } |
---|
408 | |
---|
409 | UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) { |
---|
410 | assert( arg1 && arg2 ); |
---|
411 | UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } ); |
---|
412 | if ( arg1->get_result() && arg2->get_result() ) { |
---|
413 | // if both expressions are typed, assumes that this assignment is a C bitwise assignment, |
---|
414 | // so the result is the type of the RHS |
---|
415 | ret->set_result( arg2->get_result()->clone() ); |
---|
416 | } |
---|
417 | return ret; |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | void UntypedExpr::print( std::ostream &os, Indenter indent ) const { |
---|
422 | os << "Applying untyped: " << std::endl; |
---|
423 | os << indent+1; |
---|
424 | function->print(os, indent+1); |
---|
425 | os << std::endl << indent << "...to: " << std::endl; |
---|
426 | printAll(args, os, indent+1); |
---|
427 | Expression::print( os, indent ); |
---|
428 | } |
---|
429 | |
---|
430 | NameExpr::NameExpr( std::string name ) : Expression(), name(name) { |
---|
431 | assertf(name != "0", "Zero is not a valid name"); |
---|
432 | assertf(name != "1", "One is not a valid name"); |
---|
433 | } |
---|
434 | |
---|
435 | NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) { |
---|
436 | } |
---|
437 | |
---|
438 | NameExpr::~NameExpr() {} |
---|
439 | |
---|
440 | void NameExpr::print( std::ostream &os, Indenter indent ) const { |
---|
441 | os << "Name: " << get_name(); |
---|
442 | Expression::print( os, indent ); |
---|
443 | } |
---|
444 | |
---|
445 | LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp ) : |
---|
446 | Expression(), arg1(arg1_), arg2(arg2_), isAnd(andp) { |
---|
447 | set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
448 | } |
---|
449 | |
---|
450 | LogicalExpr::LogicalExpr( const LogicalExpr &other ) : |
---|
451 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) { |
---|
452 | } |
---|
453 | |
---|
454 | LogicalExpr::~LogicalExpr() { |
---|
455 | delete arg1; |
---|
456 | delete arg2; |
---|
457 | } |
---|
458 | |
---|
459 | void LogicalExpr::print( std::ostream &os, Indenter indent )const { |
---|
460 | os << "Short-circuited operation (" << (isAnd ? "and" : "or") << ") on: "; |
---|
461 | arg1->print(os); |
---|
462 | os << " and "; |
---|
463 | arg2->print(os); |
---|
464 | Expression::print( os, indent ); |
---|
465 | } |
---|
466 | |
---|
467 | ConditionalExpr::ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 ) : |
---|
468 | Expression(), arg1(arg1), arg2(arg2), arg3(arg3) {} |
---|
469 | |
---|
470 | ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) : |
---|
471 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) { |
---|
472 | } |
---|
473 | |
---|
474 | ConditionalExpr::~ConditionalExpr() { |
---|
475 | delete arg1; |
---|
476 | delete arg2; |
---|
477 | delete arg3; |
---|
478 | } |
---|
479 | |
---|
480 | void ConditionalExpr::print( std::ostream &os, Indenter indent ) const { |
---|
481 | os << "Conditional expression on: " << std::endl << indent+1; |
---|
482 | arg1->print( os, indent+1 ); |
---|
483 | os << indent << "First alternative:" << std::endl << indent+1; |
---|
484 | arg2->print( os, indent+1 ); |
---|
485 | os << indent << "Second alternative:" << std::endl << indent+1; |
---|
486 | arg3->print( os, indent+1 ); |
---|
487 | Expression::print( os, indent ); |
---|
488 | } |
---|
489 | |
---|
490 | AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {} |
---|
491 | |
---|
492 | |
---|
493 | void AsmExpr::print( std::ostream &os, Indenter indent ) const { |
---|
494 | os << "Asm Expression: " << std::endl; |
---|
495 | if ( inout ) inout->print( os, indent+1 ); |
---|
496 | if ( constraint ) constraint->print( os, indent+1 ); |
---|
497 | if ( operand ) operand->print( os, indent+1 ); |
---|
498 | } |
---|
499 | |
---|
500 | |
---|
501 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) { |
---|
502 | assert( callExpr ); |
---|
503 | assert( callExpr->result ); |
---|
504 | set_result( callExpr->get_result()->clone() ); |
---|
505 | } |
---|
506 | |
---|
507 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) { |
---|
508 | cloneAll( other.tempDecls, tempDecls ); |
---|
509 | cloneAll( other.returnDecls, returnDecls ); |
---|
510 | cloneAll( other.dtors, dtors ); |
---|
511 | } |
---|
512 | |
---|
513 | ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() { |
---|
514 | set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment |
---|
515 | delete callExpr; |
---|
516 | deleteAll( tempDecls ); |
---|
517 | deleteAll( returnDecls ); |
---|
518 | deleteAll( dtors ); |
---|
519 | } |
---|
520 | |
---|
521 | void ImplicitCopyCtorExpr::print( std::ostream &os, Indenter indent ) const { |
---|
522 | os << "Implicit Copy Constructor Expression: " << std::endl << indent+1; |
---|
523 | callExpr->print( os, indent+1 ); |
---|
524 | os << std::endl << indent << "... with temporaries:" << std::endl; |
---|
525 | printAll( tempDecls, os, indent+1 ); |
---|
526 | os << std::endl << indent << "... with return temporaries:" << std::endl; |
---|
527 | printAll( returnDecls, os, indent+1 ); |
---|
528 | Expression::print( os, indent ); |
---|
529 | } |
---|
530 | |
---|
531 | |
---|
532 | ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) { |
---|
533 | // allow resolver to type a constructor used as an expression as if it has the same type as its first argument |
---|
534 | assert( callExpr ); |
---|
535 | Expression * arg = InitTweak::getCallArg( callExpr, 0 ); |
---|
536 | assert( arg ); |
---|
537 | set_result( maybeClone( arg->result ) ); |
---|
538 | } |
---|
539 | |
---|
540 | ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) { |
---|
541 | } |
---|
542 | |
---|
543 | ConstructorExpr::~ConstructorExpr() { |
---|
544 | delete callExpr; |
---|
545 | } |
---|
546 | |
---|
547 | void ConstructorExpr::print( std::ostream &os, Indenter indent ) const { |
---|
548 | os << "Constructor Expression: " << std::endl << indent+1; |
---|
549 | callExpr->print( os, indent + 2 ); |
---|
550 | Expression::print( os, indent ); |
---|
551 | } |
---|
552 | |
---|
553 | |
---|
554 | CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) { |
---|
555 | assert( type && initializer ); |
---|
556 | type->set_lvalue( true ); |
---|
557 | set_result( type ); |
---|
558 | } |
---|
559 | |
---|
560 | CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {} |
---|
561 | |
---|
562 | CompoundLiteralExpr::~CompoundLiteralExpr() { |
---|
563 | delete initializer; |
---|
564 | } |
---|
565 | |
---|
566 | void CompoundLiteralExpr::print( std::ostream &os, Indenter indent ) const { |
---|
567 | os << "Compound Literal Expression: " << std::endl << indent+1; |
---|
568 | result->print( os, indent+1 ); |
---|
569 | os << indent+1; |
---|
570 | initializer->print( os, indent+1 ); |
---|
571 | Expression::print( os, indent ); |
---|
572 | } |
---|
573 | |
---|
574 | RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {} |
---|
575 | RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {} |
---|
576 | void RangeExpr::print( std::ostream &os, Indenter indent ) const { |
---|
577 | os << "Range Expression: "; |
---|
578 | low->print( os, indent ); |
---|
579 | os << " ... "; |
---|
580 | high->print( os, indent ); |
---|
581 | Expression::print( os, indent ); |
---|
582 | } |
---|
583 | |
---|
584 | StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) { |
---|
585 | computeResult(); |
---|
586 | } |
---|
587 | StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) { |
---|
588 | cloneAll( other.returnDecls, returnDecls ); |
---|
589 | cloneAll( other.dtors, dtors ); |
---|
590 | } |
---|
591 | StmtExpr::~StmtExpr() { |
---|
592 | delete statements; |
---|
593 | deleteAll( dtors ); |
---|
594 | deleteAll( returnDecls ); |
---|
595 | } |
---|
596 | void StmtExpr::computeResult() { |
---|
597 | assert( statements ); |
---|
598 | std::list< Statement * > & body = statements->kids; |
---|
599 | delete result; |
---|
600 | result = nullptr; |
---|
601 | if ( ! returnDecls.empty() ) { |
---|
602 | // prioritize return decl for result type, since if a return decl exists, then |
---|
603 | // the StmtExpr is currently in an intermediate state where the body will always |
---|
604 | // give a void result type. |
---|
605 | result = returnDecls.front()->get_type()->clone(); |
---|
606 | } else if ( ! body.empty() ) { |
---|
607 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) { |
---|
608 | result = maybeClone( exprStmt->expr->result ); |
---|
609 | } |
---|
610 | } |
---|
611 | // ensure that StmtExpr has a result type |
---|
612 | if ( ! result ) { |
---|
613 | result = new VoidType( Type::Qualifiers() ); |
---|
614 | } |
---|
615 | } |
---|
616 | void StmtExpr::print( std::ostream &os, Indenter indent ) const { |
---|
617 | os << "Statement Expression: " << std::endl << indent+1; |
---|
618 | statements->print( os, indent+1 ); |
---|
619 | if ( ! returnDecls.empty() ) { |
---|
620 | os << indent+1 << "... with returnDecls: "; |
---|
621 | printAll( returnDecls, os, indent+1 ); |
---|
622 | } |
---|
623 | if ( ! dtors.empty() ) { |
---|
624 | os << indent+1 << "... with dtors: "; |
---|
625 | printAll( dtors, os, indent+1 ); |
---|
626 | } |
---|
627 | Expression::print( os, indent ); |
---|
628 | } |
---|
629 | |
---|
630 | |
---|
631 | long long UniqueExpr::count = 0; |
---|
632 | UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) { |
---|
633 | assert( expr ); |
---|
634 | assert( count != -1 ); |
---|
635 | if ( id == -1 ) id = count++; |
---|
636 | if ( expr->get_result() ) { |
---|
637 | set_result( expr->get_result()->clone() ); |
---|
638 | } |
---|
639 | } |
---|
640 | UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) { |
---|
641 | } |
---|
642 | UniqueExpr::~UniqueExpr() { |
---|
643 | delete expr; |
---|
644 | delete object; |
---|
645 | delete var; |
---|
646 | } |
---|
647 | void UniqueExpr::print( std::ostream &os, Indenter indent ) const { |
---|
648 | os << "Unique Expression with id:" << id << std::endl << indent+1; |
---|
649 | expr->print( os, indent+1 ); |
---|
650 | if ( object ) { |
---|
651 | os << indent << "... with decl: "; |
---|
652 | get_object()->printShort( os, indent+1 ); |
---|
653 | } |
---|
654 | Expression::print( os, indent ); |
---|
655 | } |
---|
656 | |
---|
657 | InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {} |
---|
658 | InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {} |
---|
659 | InitAlternative::~InitAlternative() { |
---|
660 | delete type; |
---|
661 | delete designation; |
---|
662 | } |
---|
663 | |
---|
664 | UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {} |
---|
665 | UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {} |
---|
666 | UntypedInitExpr::~UntypedInitExpr() { |
---|
667 | delete expr; |
---|
668 | } |
---|
669 | |
---|
670 | void UntypedInitExpr::print( std::ostream & os, Indenter indent ) const { |
---|
671 | os << "Untyped Init Expression" << std::endl << indent+1; |
---|
672 | expr->print( os, indent+1 ); |
---|
673 | if ( ! initAlts.empty() ) { |
---|
674 | for ( const InitAlternative & alt : initAlts ) { |
---|
675 | os << indent+1 << "InitAlternative: "; |
---|
676 | alt.type->print( os, indent+1 ); |
---|
677 | alt.designation->print( os, indent+1 ); |
---|
678 | } |
---|
679 | } |
---|
680 | } |
---|
681 | |
---|
682 | InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) { |
---|
683 | set_result( expr->get_result()->clone() ); |
---|
684 | } |
---|
685 | InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {} |
---|
686 | InitExpr::~InitExpr() { |
---|
687 | delete expr; |
---|
688 | delete designation; |
---|
689 | } |
---|
690 | |
---|
691 | void InitExpr::print( std::ostream & os, Indenter indent ) const { |
---|
692 | os << "Init Expression" << std::endl << indent+1; |
---|
693 | expr->print( os, indent+1 ); |
---|
694 | os << indent+1 << "... with designation: "; |
---|
695 | designation->print( os, indent+1 ); |
---|
696 | } |
---|
697 | |
---|
698 | DeletedExpr::DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) { |
---|
699 | assert( expr->result ); |
---|
700 | result = expr->result->clone(); |
---|
701 | } |
---|
702 | DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {} |
---|
703 | DeletedExpr::~DeletedExpr() { |
---|
704 | delete expr; |
---|
705 | } |
---|
706 | |
---|
707 | void DeletedExpr::print( std::ostream & os, Indenter indent ) const { |
---|
708 | os << "Deleted Expression" << std::endl << indent+1; |
---|
709 | expr->print( os, indent+1 ); |
---|
710 | os << std::endl << indent+1 << "... deleted by: "; |
---|
711 | deleteStmt->print( os, indent+1 ); |
---|
712 | } |
---|
713 | |
---|
714 | |
---|
715 | // Local Variables: // |
---|
716 | // tab-width: 4 // |
---|
717 | // mode: c++ // |
---|
718 | // compile-command: "make install" // |
---|
719 | // End: // |
---|