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