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