source: src/SynTree/Expression.cc@ 90ce35aa

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 90ce35aa was 849720f, checked in by Andrew Beach <ajbeach@…>, 6 years ago

lvalue should now always come directly from the expression.

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