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 | VariableExpr::VariableExpr( DeclarationWithType *_var ) : Expression(), var( _var ) { |
---|
86 | assert( var ); |
---|
87 | assert( var->get_type() ); |
---|
88 | Type * type = var->get_type()->clone(); |
---|
89 | type->set_lvalue( true ); |
---|
90 | set_result( type ); |
---|
91 | } |
---|
92 | |
---|
93 | VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) { |
---|
94 | } |
---|
95 | |
---|
96 | VariableExpr::~VariableExpr() { |
---|
97 | // don't delete the declaration, since it points somewhere else in the tree |
---|
98 | } |
---|
99 | |
---|
100 | VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) { |
---|
101 | VariableExpr * funcExpr = new VariableExpr( func ); |
---|
102 | funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) ); |
---|
103 | return funcExpr; |
---|
104 | } |
---|
105 | |
---|
106 | void VariableExpr::print( std::ostream &os, Indenter indent ) const { |
---|
107 | os << "Variable Expression: "; |
---|
108 | var->printShort(os, indent); |
---|
109 | Expression::print( os, indent ); |
---|
110 | } |
---|
111 | |
---|
112 | SizeofExpr::SizeofExpr( Expression *expr_ ) : |
---|
113 | Expression(), expr(expr_), type(0), isType(false) { |
---|
114 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
115 | } |
---|
116 | |
---|
117 | SizeofExpr::SizeofExpr( Type *type_ ) : |
---|
118 | Expression(), expr(0), type(type_), isType(true) { |
---|
119 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
120 | } |
---|
121 | |
---|
122 | SizeofExpr::SizeofExpr( const SizeofExpr &other ) : |
---|
123 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
124 | } |
---|
125 | |
---|
126 | SizeofExpr::~SizeofExpr() { |
---|
127 | delete expr; |
---|
128 | delete type; |
---|
129 | } |
---|
130 | |
---|
131 | void SizeofExpr::print( std::ostream &os, Indenter indent) const { |
---|
132 | os << "Sizeof Expression on: "; |
---|
133 | if (isType) type->print(os, indent+1); |
---|
134 | else expr->print(os, indent+1); |
---|
135 | Expression::print( os, indent ); |
---|
136 | } |
---|
137 | |
---|
138 | AlignofExpr::AlignofExpr( Expression *expr_ ) : |
---|
139 | Expression(), expr(expr_), type(0), isType(false) { |
---|
140 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
141 | } |
---|
142 | |
---|
143 | AlignofExpr::AlignofExpr( Type *type_ ) : |
---|
144 | Expression(), expr(0), type(type_), isType(true) { |
---|
145 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
146 | } |
---|
147 | |
---|
148 | AlignofExpr::AlignofExpr( const AlignofExpr &other ) : |
---|
149 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
150 | } |
---|
151 | |
---|
152 | AlignofExpr::~AlignofExpr() { |
---|
153 | delete expr; |
---|
154 | delete type; |
---|
155 | } |
---|
156 | |
---|
157 | void AlignofExpr::print( std::ostream &os, Indenter indent) const { |
---|
158 | os << "Alignof 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 | UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type, const std::string &member ) : |
---|
165 | Expression(), type(type), member(member) { |
---|
166 | assert( type ); |
---|
167 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
168 | } |
---|
169 | |
---|
170 | UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) : |
---|
171 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {} |
---|
172 | |
---|
173 | UntypedOffsetofExpr::~UntypedOffsetofExpr() { |
---|
174 | delete type; |
---|
175 | } |
---|
176 | |
---|
177 | void UntypedOffsetofExpr::print( std::ostream &os, Indenter indent) const { |
---|
178 | os << "Untyped Offsetof Expression on member " << member << " of "; |
---|
179 | type->print(os, indent+1); |
---|
180 | Expression::print( os, indent ); |
---|
181 | } |
---|
182 | |
---|
183 | OffsetofExpr::OffsetofExpr( Type *type, DeclarationWithType *member ) : |
---|
184 | Expression(), type(type), member(member) { |
---|
185 | assert( member ); |
---|
186 | assert( type ); |
---|
187 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
188 | } |
---|
189 | |
---|
190 | OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) : |
---|
191 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {} |
---|
192 | |
---|
193 | OffsetofExpr::~OffsetofExpr() { |
---|
194 | delete type; |
---|
195 | } |
---|
196 | |
---|
197 | void OffsetofExpr::print( std::ostream &os, Indenter indent) const { |
---|
198 | os << "Offsetof Expression on member " << member->name << " of "; |
---|
199 | type->print(os, indent+1); |
---|
200 | Expression::print( os, indent ); |
---|
201 | } |
---|
202 | |
---|
203 | OffsetPackExpr::OffsetPackExpr( StructInstType *type ) : Expression(), type( type ) { |
---|
204 | assert( type ); |
---|
205 | set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) ); |
---|
206 | } |
---|
207 | |
---|
208 | OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {} |
---|
209 | |
---|
210 | OffsetPackExpr::~OffsetPackExpr() { delete type; } |
---|
211 | |
---|
212 | void OffsetPackExpr::print( std::ostream &os, Indenter indent ) const { |
---|
213 | os << "Offset pack expression on "; |
---|
214 | type->print(os, indent+1); |
---|
215 | Expression::print( os, indent ); |
---|
216 | } |
---|
217 | |
---|
218 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_ ) : |
---|
219 | Expression(), attr( attr ), expr(expr_), type(0), isType(false) { |
---|
220 | } |
---|
221 | |
---|
222 | AttrExpr::AttrExpr( Expression *attr, Type *type_ ) : |
---|
223 | Expression(), attr( attr ), expr(0), type(type_), isType(true) { |
---|
224 | } |
---|
225 | |
---|
226 | AttrExpr::AttrExpr( const AttrExpr &other ) : |
---|
227 | Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
228 | } |
---|
229 | |
---|
230 | AttrExpr::~AttrExpr() { |
---|
231 | delete attr; |
---|
232 | delete expr; |
---|
233 | delete type; |
---|
234 | } |
---|
235 | |
---|
236 | void AttrExpr::print( std::ostream &os, Indenter indent) const { |
---|
237 | os << "Attr "; |
---|
238 | attr->print( os, indent+1); |
---|
239 | if ( isType || expr ) { |
---|
240 | os << "applied to: "; |
---|
241 | if (isType) type->print(os, indent+1); |
---|
242 | else expr->print(os, indent+1); |
---|
243 | } // if |
---|
244 | Expression::print( os, indent ); |
---|
245 | } |
---|
246 | |
---|
247 | CastExpr::CastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { |
---|
248 | set_result(toType); |
---|
249 | } |
---|
250 | |
---|
251 | CastExpr::CastExpr( Expression *arg_ ) : Expression(), arg(arg_) { |
---|
252 | set_result( new VoidType( Type::Qualifiers() ) ); |
---|
253 | } |
---|
254 | |
---|
255 | CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
256 | } |
---|
257 | |
---|
258 | CastExpr::~CastExpr() { |
---|
259 | delete arg; |
---|
260 | } |
---|
261 | |
---|
262 | void CastExpr::print( std::ostream &os, Indenter indent ) const { |
---|
263 | os << "Cast of:" << std::endl << indent+1; |
---|
264 | arg->print(os, indent+1); |
---|
265 | os << std::endl << indent << "... to:"; |
---|
266 | if ( result->isVoid() ) { |
---|
267 | os << " nothing"; |
---|
268 | } else { |
---|
269 | os << std::endl << indent+1; |
---|
270 | result->print( os, indent+1 ); |
---|
271 | } // if |
---|
272 | Expression::print( os, indent ); |
---|
273 | } |
---|
274 | |
---|
275 | VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { |
---|
276 | set_result(toType); |
---|
277 | } |
---|
278 | |
---|
279 | VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
280 | } |
---|
281 | |
---|
282 | VirtualCastExpr::~VirtualCastExpr() { |
---|
283 | delete arg; |
---|
284 | } |
---|
285 | |
---|
286 | void VirtualCastExpr::print( std::ostream &os, Indenter indent ) const { |
---|
287 | os << "Virtual Cast of:" << std::endl << indent+1; |
---|
288 | arg->print(os, indent+1); |
---|
289 | os << std::endl << indent << "... to:"; |
---|
290 | if ( ! result ) { |
---|
291 | os << " unknown"; |
---|
292 | } else { |
---|
293 | os << std::endl << indent+1; |
---|
294 | result->print( os, indent+1 ); |
---|
295 | } // if |
---|
296 | Expression::print( os, indent ); |
---|
297 | } |
---|
298 | |
---|
299 | UntypedMemberExpr::UntypedMemberExpr( Expression * member, Expression *aggregate ) : |
---|
300 | Expression(), member(member), aggregate(aggregate) { |
---|
301 | assert( aggregate ); |
---|
302 | } |
---|
303 | |
---|
304 | UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) : |
---|
305 | Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) { |
---|
306 | } |
---|
307 | |
---|
308 | UntypedMemberExpr::~UntypedMemberExpr() { |
---|
309 | delete aggregate; |
---|
310 | delete member; |
---|
311 | } |
---|
312 | |
---|
313 | void UntypedMemberExpr::print( std::ostream &os, Indenter indent ) const { |
---|
314 | os << "Untyped Member Expression, with field: " << std::endl << indent+1; |
---|
315 | member->print(os, indent+1 ); |
---|
316 | os << indent << "... from aggregate: " << std::endl << indent+1; |
---|
317 | aggregate->print(os, indent+1); |
---|
318 | Expression::print( os, indent ); |
---|
319 | } |
---|
320 | |
---|
321 | namespace { |
---|
322 | TypeSubstitution makeSub( Type * t ) { |
---|
323 | if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( t ) ) { |
---|
324 | return makeSub( refType->get_base() ); |
---|
325 | } else if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) { |
---|
326 | return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() ); |
---|
327 | } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) { |
---|
328 | return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() ); |
---|
329 | } else { |
---|
330 | assertf( false, "makeSub expects struct or union type for aggregate, but got: %s", toString( t ).c_str() ); |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) : |
---|
337 | Expression(), member(member), aggregate(aggregate) { |
---|
338 | assert( member ); |
---|
339 | assert( aggregate ); |
---|
340 | |
---|
341 | TypeSubstitution sub( makeSub( aggregate->get_result() ) ); |
---|
342 | Type * res = member->get_type()->clone(); |
---|
343 | sub.apply( res ); |
---|
344 | set_result( res ); |
---|
345 | get_result()->set_lvalue( true ); |
---|
346 | } |
---|
347 | |
---|
348 | MemberExpr::MemberExpr( const MemberExpr &other ) : |
---|
349 | Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) { |
---|
350 | } |
---|
351 | |
---|
352 | MemberExpr::~MemberExpr() { |
---|
353 | // don't delete the member declaration, since it points somewhere else in the tree |
---|
354 | delete aggregate; |
---|
355 | } |
---|
356 | |
---|
357 | void MemberExpr::print( std::ostream &os, Indenter indent ) const { |
---|
358 | os << "Member Expression, with field: " << std::endl; |
---|
359 | os << indent+1; |
---|
360 | member->print( os, indent+1 ); |
---|
361 | os << std::endl << indent << "... from aggregate: " << std::endl << indent+1; |
---|
362 | aggregate->print(os, indent + 1); |
---|
363 | Expression::print( os, indent ); |
---|
364 | } |
---|
365 | |
---|
366 | UntypedExpr::UntypedExpr( Expression *function, const std::list<Expression *> &args ) : |
---|
367 | Expression(), function(function), args(args) {} |
---|
368 | |
---|
369 | UntypedExpr::UntypedExpr( const UntypedExpr &other ) : |
---|
370 | Expression( other ), function( maybeClone( other.function ) ) { |
---|
371 | cloneAll( other.args, args ); |
---|
372 | } |
---|
373 | |
---|
374 | UntypedExpr::~UntypedExpr() { |
---|
375 | delete function; |
---|
376 | deleteAll( args ); |
---|
377 | } |
---|
378 | |
---|
379 | UntypedExpr * UntypedExpr::createDeref( Expression * expr ) { |
---|
380 | UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } ); |
---|
381 | if ( Type * type = expr->get_result() ) { |
---|
382 | Type * base = InitTweak::getPointerBase( type ); |
---|
383 | assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() ); |
---|
384 | ret->set_result( base->clone() ); |
---|
385 | if ( GenPoly::referencesPermissable() ) { |
---|
386 | // if references are still allowed in the AST, dereference returns a reference |
---|
387 | ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) ); |
---|
388 | } else { |
---|
389 | // references have been removed, in which case dereference returns an lvalue of the base type. |
---|
390 | ret->get_result()->set_lvalue( true ); |
---|
391 | } |
---|
392 | } |
---|
393 | return ret; |
---|
394 | } |
---|
395 | |
---|
396 | UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) { |
---|
397 | assert( arg1 && arg2 ); |
---|
398 | UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } ); |
---|
399 | if ( arg1->get_result() && arg2->get_result() ) { |
---|
400 | // if both expressions are typed, assumes that this assignment is a C bitwise assignment, |
---|
401 | // so the result is the type of the RHS |
---|
402 | ret->set_result( arg2->get_result()->clone() ); |
---|
403 | } |
---|
404 | return ret; |
---|
405 | } |
---|
406 | |
---|
407 | |
---|
408 | void UntypedExpr::print( std::ostream &os, Indenter indent ) const { |
---|
409 | os << "Applying untyped: " << std::endl; |
---|
410 | os << indent+1; |
---|
411 | function->print(os, indent+1); |
---|
412 | os << std::endl << indent << "...to: " << std::endl; |
---|
413 | printAll(args, os, indent+1); |
---|
414 | Expression::print( os, indent ); |
---|
415 | } |
---|
416 | |
---|
417 | NameExpr::NameExpr( std::string name ) : Expression(), name(name) { |
---|
418 | assertf(name != "0", "Zero is not a valid name"); |
---|
419 | assertf(name != "1", "One is not a valid name"); |
---|
420 | } |
---|
421 | |
---|
422 | NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) { |
---|
423 | } |
---|
424 | |
---|
425 | NameExpr::~NameExpr() {} |
---|
426 | |
---|
427 | void NameExpr::print( std::ostream &os, Indenter indent ) const { |
---|
428 | os << "Name: " << get_name(); |
---|
429 | Expression::print( os, indent ); |
---|
430 | } |
---|
431 | |
---|
432 | LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp ) : |
---|
433 | Expression(), arg1(arg1_), arg2(arg2_), isAnd(andp) { |
---|
434 | set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
435 | } |
---|
436 | |
---|
437 | LogicalExpr::LogicalExpr( const LogicalExpr &other ) : |
---|
438 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) { |
---|
439 | } |
---|
440 | |
---|
441 | LogicalExpr::~LogicalExpr() { |
---|
442 | delete arg1; |
---|
443 | delete arg2; |
---|
444 | } |
---|
445 | |
---|
446 | void LogicalExpr::print( std::ostream &os, Indenter indent )const { |
---|
447 | os << "Short-circuited operation (" << (isAnd ? "and" : "or") << ") on: "; |
---|
448 | arg1->print(os); |
---|
449 | os << " and "; |
---|
450 | arg2->print(os); |
---|
451 | Expression::print( os, indent ); |
---|
452 | } |
---|
453 | |
---|
454 | ConditionalExpr::ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 ) : |
---|
455 | Expression(), arg1(arg1), arg2(arg2), arg3(arg3) {} |
---|
456 | |
---|
457 | ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) : |
---|
458 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) { |
---|
459 | } |
---|
460 | |
---|
461 | ConditionalExpr::~ConditionalExpr() { |
---|
462 | delete arg1; |
---|
463 | delete arg2; |
---|
464 | delete arg3; |
---|
465 | } |
---|
466 | |
---|
467 | void ConditionalExpr::print( std::ostream &os, Indenter indent ) const { |
---|
468 | os << "Conditional expression on: " << std::endl << indent+1; |
---|
469 | arg1->print( os, indent+1 ); |
---|
470 | os << indent << "First alternative:" << std::endl << indent+1; |
---|
471 | arg2->print( os, indent+1 ); |
---|
472 | os << indent << "Second alternative:" << std::endl << indent+1; |
---|
473 | arg3->print( os, indent+1 ); |
---|
474 | Expression::print( os, indent ); |
---|
475 | } |
---|
476 | |
---|
477 | AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {} |
---|
478 | |
---|
479 | |
---|
480 | void AsmExpr::print( std::ostream &os, Indenter indent ) const { |
---|
481 | os << "Asm Expression: " << std::endl; |
---|
482 | if ( inout ) inout->print( os, indent+1 ); |
---|
483 | if ( constraint ) constraint->print( os, indent+1 ); |
---|
484 | if ( operand ) operand->print( os, indent+1 ); |
---|
485 | } |
---|
486 | |
---|
487 | |
---|
488 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) { |
---|
489 | assert( callExpr ); |
---|
490 | assert( callExpr->result ); |
---|
491 | set_result( callExpr->get_result()->clone() ); |
---|
492 | } |
---|
493 | |
---|
494 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) { |
---|
495 | cloneAll( other.tempDecls, tempDecls ); |
---|
496 | cloneAll( other.returnDecls, returnDecls ); |
---|
497 | cloneAll( other.dtors, dtors ); |
---|
498 | } |
---|
499 | |
---|
500 | ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() { |
---|
501 | set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment |
---|
502 | delete callExpr; |
---|
503 | deleteAll( tempDecls ); |
---|
504 | deleteAll( returnDecls ); |
---|
505 | deleteAll( dtors ); |
---|
506 | } |
---|
507 | |
---|
508 | void ImplicitCopyCtorExpr::print( std::ostream &os, Indenter indent ) const { |
---|
509 | os << "Implicit Copy Constructor Expression: " << std::endl << indent+1; |
---|
510 | callExpr->print( os, indent+1 ); |
---|
511 | os << std::endl << indent << "... with temporaries:" << std::endl; |
---|
512 | printAll( tempDecls, os, indent+1 ); |
---|
513 | os << std::endl << indent << "... with return temporaries:" << std::endl; |
---|
514 | printAll( returnDecls, os, indent+1 ); |
---|
515 | Expression::print( os, indent ); |
---|
516 | } |
---|
517 | |
---|
518 | |
---|
519 | ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) { |
---|
520 | // allow resolver to type a constructor used as an expression as if it has the same type as its first argument |
---|
521 | assert( callExpr ); |
---|
522 | Expression * arg = InitTweak::getCallArg( callExpr, 0 ); |
---|
523 | assert( arg ); |
---|
524 | set_result( maybeClone( arg->result ) ); |
---|
525 | } |
---|
526 | |
---|
527 | ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) { |
---|
528 | } |
---|
529 | |
---|
530 | ConstructorExpr::~ConstructorExpr() { |
---|
531 | delete callExpr; |
---|
532 | } |
---|
533 | |
---|
534 | void ConstructorExpr::print( std::ostream &os, Indenter indent ) const { |
---|
535 | os << "Constructor Expression: " << std::endl << indent+1; |
---|
536 | callExpr->print( os, indent + 2 ); |
---|
537 | Expression::print( os, indent ); |
---|
538 | } |
---|
539 | |
---|
540 | |
---|
541 | CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) { |
---|
542 | assert( type && initializer ); |
---|
543 | type->set_lvalue( true ); |
---|
544 | set_result( type ); |
---|
545 | } |
---|
546 | |
---|
547 | CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {} |
---|
548 | |
---|
549 | CompoundLiteralExpr::~CompoundLiteralExpr() { |
---|
550 | delete initializer; |
---|
551 | } |
---|
552 | |
---|
553 | void CompoundLiteralExpr::print( std::ostream &os, Indenter indent ) const { |
---|
554 | os << "Compound Literal Expression: " << std::endl << indent+1; |
---|
555 | result->print( os, indent+1 ); |
---|
556 | os << indent+1; |
---|
557 | initializer->print( os, indent+1 ); |
---|
558 | Expression::print( os, indent ); |
---|
559 | } |
---|
560 | |
---|
561 | RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {} |
---|
562 | RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {} |
---|
563 | void RangeExpr::print( std::ostream &os, Indenter indent ) const { |
---|
564 | os << "Range Expression: "; |
---|
565 | low->print( os, indent ); |
---|
566 | os << " ... "; |
---|
567 | high->print( os, indent ); |
---|
568 | Expression::print( os, indent ); |
---|
569 | } |
---|
570 | |
---|
571 | StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) { |
---|
572 | assert( statements ); |
---|
573 | std::list< Statement * > & body = statements->get_kids(); |
---|
574 | if ( ! body.empty() ) { |
---|
575 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) { |
---|
576 | set_result( maybeClone( exprStmt->get_expr()->get_result() ) ); |
---|
577 | } |
---|
578 | } |
---|
579 | // ensure that StmtExpr has a result type |
---|
580 | if ( ! result ) { |
---|
581 | set_result( new VoidType( Type::Qualifiers() ) ); |
---|
582 | } |
---|
583 | } |
---|
584 | StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) { |
---|
585 | cloneAll( other.returnDecls, returnDecls ); |
---|
586 | cloneAll( other.dtors, dtors ); |
---|
587 | } |
---|
588 | StmtExpr::~StmtExpr() { |
---|
589 | delete statements; |
---|
590 | deleteAll( dtors ); |
---|
591 | deleteAll( returnDecls ); |
---|
592 | } |
---|
593 | void StmtExpr::print( std::ostream &os, Indenter indent ) const { |
---|
594 | os << "Statement Expression: " << std::endl << indent+1; |
---|
595 | statements->print( os, indent+1 ); |
---|
596 | if ( ! returnDecls.empty() ) { |
---|
597 | os << indent+1 << "... with returnDecls: "; |
---|
598 | printAll( returnDecls, os, indent+1 ); |
---|
599 | } |
---|
600 | if ( ! dtors.empty() ) { |
---|
601 | os << indent+1 << "... with dtors: "; |
---|
602 | printAll( dtors, os, indent+1 ); |
---|
603 | } |
---|
604 | Expression::print( os, indent ); |
---|
605 | } |
---|
606 | |
---|
607 | |
---|
608 | long long UniqueExpr::count = 0; |
---|
609 | UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) { |
---|
610 | assert( expr ); |
---|
611 | assert( count != -1 ); |
---|
612 | if ( id == -1 ) id = count++; |
---|
613 | if ( expr->get_result() ) { |
---|
614 | set_result( expr->get_result()->clone() ); |
---|
615 | } |
---|
616 | } |
---|
617 | UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) { |
---|
618 | } |
---|
619 | UniqueExpr::~UniqueExpr() { |
---|
620 | delete expr; |
---|
621 | delete object; |
---|
622 | delete var; |
---|
623 | } |
---|
624 | void UniqueExpr::print( std::ostream &os, Indenter indent ) const { |
---|
625 | os << "Unique Expression with id:" << id << std::endl << indent+1; |
---|
626 | expr->print( os, indent+1 ); |
---|
627 | if ( object ) { |
---|
628 | os << indent << "... with decl: "; |
---|
629 | get_object()->printShort( os, indent+1 ); |
---|
630 | } |
---|
631 | Expression::print( os, indent ); |
---|
632 | } |
---|
633 | |
---|
634 | InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {} |
---|
635 | InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {} |
---|
636 | InitAlternative::~InitAlternative() { |
---|
637 | delete type; |
---|
638 | delete designation; |
---|
639 | } |
---|
640 | |
---|
641 | UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {} |
---|
642 | UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {} |
---|
643 | UntypedInitExpr::~UntypedInitExpr() { |
---|
644 | delete expr; |
---|
645 | } |
---|
646 | |
---|
647 | void UntypedInitExpr::print( std::ostream & os, Indenter indent ) const { |
---|
648 | os << "Untyped Init Expression" << std::endl << indent+1; |
---|
649 | expr->print( os, indent+1 ); |
---|
650 | if ( ! initAlts.empty() ) { |
---|
651 | for ( const InitAlternative & alt : initAlts ) { |
---|
652 | os << indent+1 << "InitAlternative: "; |
---|
653 | alt.type->print( os, indent+1 ); |
---|
654 | alt.designation->print( os, indent+1 ); |
---|
655 | } |
---|
656 | } |
---|
657 | } |
---|
658 | |
---|
659 | InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) { |
---|
660 | set_result( expr->get_result()->clone() ); |
---|
661 | } |
---|
662 | InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {} |
---|
663 | InitExpr::~InitExpr() { |
---|
664 | delete expr; |
---|
665 | delete designation; |
---|
666 | } |
---|
667 | |
---|
668 | void InitExpr::print( std::ostream & os, Indenter indent ) const { |
---|
669 | os << "Init Expression" << std::endl << indent+1; |
---|
670 | expr->print( os, indent+1 ); |
---|
671 | os << indent+1 << "... with designation: "; |
---|
672 | designation->print( os, indent+1 ); |
---|
673 | } |
---|
674 | |
---|
675 | // Local Variables: // |
---|
676 | // tab-width: 4 // |
---|
677 | // mode: c++ // |
---|
678 | // compile-command: "make install" // |
---|
679 | // End: // |
---|