source: src/SynTree/Expression.cc@ be9036d

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since be9036d was 07516b56, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Set result type of statement expressions to void when body is empty or last statement is untyped [fixes #32]

  • Property mode set to 100644
File size: 24.4 KB
RevLine 
[0dd3a2f]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//
[3be261a]7// Expression.cc --
[0dd3a2f]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[e04ef3a]11// Last Modified By : Peter A. Buhr
[a5f0529]12// Last Modified On : Tue Jul 25 14:15:47 2017
13// Update Count : 54
[0dd3a2f]14//
15
[ea6332d]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
[51b73452]32
[b0440b7]33#include "GenPoly/Lvalue.h"
[51b73452]34
[906e24d]35Expression::Expression( Expression *_aname ) : result( 0 ), env( 0 ), argName( _aname ) {}
[0dd3a2f]36
[64ac636]37Expression::Expression( const Expression &other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ), extension( other.extension ) {
[0dd3a2f]38}
39
40Expression::~Expression() {
41 delete env;
[7f5566b]42 delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix
[906e24d]43 delete result;
[51b73452]44}
45
[59db689]46void Expression::print( std::ostream &os, int indent ) const {
[0dd3a2f]47 if ( env ) {
[cf0941d]48 os << std::string( indent, ' ' ) << "with environment:" << std::endl;
[0dd3a2f]49 env->print( os, indent+2 );
50 } // if
[51b73452]51
[0dd3a2f]52 if ( argName ) {
[cf0941d]53 os << std::string( indent, ' ' ) << "with designator:";
[0dd3a2f]54 argName->print( os, indent+2 );
55 } // if
[e04ef3a]56
57 if ( extension ) {
58 os << std::string( indent, ' ' ) << "with extension:";
59 } // if
[51b73452]60}
61
[0dd3a2f]62ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) {
[906e24d]63 set_result( constant.get_type()->clone() );
[51b73452]64}
65
[0dd3a2f]66ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
67}
[51b73452]68
69ConstantExpr::~ConstantExpr() {}
70
[0dd3a2f]71void ConstantExpr::print( std::ostream &os, int indent ) const {
[60089f4]72 os << "constant expression " ;
[cf0941d]73 constant.print( os );
[0dd3a2f]74 Expression::print( os, indent );
[51b73452]75}
76
[0dd3a2f]77VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) {
[db4ecc5]78 assert( var );
79 assert( var->get_type() );
[906e24d]80 Type * type = var->get_type()->clone();
[615a096]81 type->set_lvalue( true );
[906e24d]82 set_result( type );
[51b73452]83}
84
[0dd3a2f]85VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
[51b73452]86}
87
[0dd3a2f]88VariableExpr::~VariableExpr() {
89 // don't delete the declaration, since it points somewhere else in the tree
[51b73452]90}
91
[8a6cf7e]92VariableExpr * 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
[0dd3a2f]98void VariableExpr::print( std::ostream &os, int indent ) const {
[89231bc]99 os << "Variable Expression: ";
[51b73452]100
[0dd3a2f]101 Declaration *decl = get_var();
102 if ( decl != 0) decl->printShort(os, indent + 2);
103 os << std::endl;
104 Expression::print( os, indent );
[51b73452]105}
106
107SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
[0dd3a2f]108 Expression( _aname ), expr(expr_), type(0), isType(false) {
[906e24d]109 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[51b73452]110}
111
112SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
[0dd3a2f]113 Expression( _aname ), expr(0), type(type_), isType(true) {
[906e24d]114 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[51b73452]115}
116
[0dd3a2f]117SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
118 Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
[51b73452]119}
120
[0dd3a2f]121SizeofExpr::~SizeofExpr() {
122 delete expr;
123 delete type;
[51b73452]124}
125
126void SizeofExpr::print( std::ostream &os, int indent) const {
[89231bc]127 os << "Sizeof Expression on: ";
[51b73452]128
[47534159]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
138AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
139 Expression( _aname ), expr(expr_), type(0), isType(false) {
[906e24d]140 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[47534159]141}
142
143AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
144 Expression( _aname ), expr(0), type(type_), isType(true) {
[906e24d]145 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[47534159]146}
147
148AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
149 Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
150}
151
152AlignofExpr::~AlignofExpr() {
153 delete expr;
154 delete type;
155}
156
157void AlignofExpr::print( std::ostream &os, int indent) const {
[f19339e]158 os << "Alignof Expression on: ";
[47534159]159
[0dd3a2f]160 if (isType)
161 type->print(os, indent + 2);
162 else
163 expr->print(os, indent + 2);
[51b73452]164
[0dd3a2f]165 os << std::endl;
166 Expression::print( os, indent );
[51b73452]167}
168
[2a4b088]169UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
170 Expression( _aname ), type(type_), member(member_) {
[906e24d]171 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[2a4b088]172}
173
174UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
175 Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
176
177UntypedOffsetofExpr::~UntypedOffsetofExpr() {
178 delete type;
179}
180
181void 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
[25a054f]194OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
195 Expression( _aname ), type(type_), member(member_) {
[906e24d]196 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[25a054f]197}
198
199OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
[79970ed]200 Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
[25a054f]201
202OffsetofExpr::~OffsetofExpr() {
203 delete type;
204}
205
206void 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
[afc1045]227OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
[906e24d]228 set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
[afc1045]229}
230
231OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
232
233OffsetPackExpr::~OffsetPackExpr() { delete type; }
234
235void 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
[25a054f]244 os << std::endl;
245 Expression::print( os, indent );
246}
247
[51b73452]248AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
[0dd3a2f]249 Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
[51b73452]250}
251
252AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
[0dd3a2f]253 Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
[51b73452]254}
255
[0dd3a2f]256AttrExpr::AttrExpr( const AttrExpr &other ) :
257 Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
[51b73452]258}
259
[0dd3a2f]260AttrExpr::~AttrExpr() {
261 delete attr;
262 delete expr;
263 delete type;
[51b73452]264}
265
266void AttrExpr::print( std::ostream &os, int indent) const {
[f19339e]267 os << "Attr ";
[0dd3a2f]268 attr->print( os, indent + 2 );
269 if ( isType || expr ) {
270 os << "applied to: ";
[51b73452]271
[0dd3a2f]272 if (isType)
273 type->print(os, indent + 2);
274 else
275 expr->print(os, indent + 2);
276 } // if
[51b73452]277
[0dd3a2f]278 os << std::endl;
279 Expression::print( os, indent );
[51b73452]280}
281
[0dd3a2f]282CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
[906e24d]283 set_result(toType);
[51b73452]284}
285
286CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
[906e24d]287 set_result( new VoidType( Type::Qualifiers() ) );
[51b73452]288}
289
[0dd3a2f]290CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
[51b73452]291}
292
293CastExpr::~CastExpr() {
[0dd3a2f]294 delete arg;
[51b73452]295}
296
297void CastExpr::print( std::ostream &os, int indent ) const {
[89231bc]298 os << "Cast of:" << std::endl << std::string( indent+2, ' ' );
[0dd3a2f]299 arg->print(os, indent+2);
[44b5ca0]300 os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
[906e24d]301 os << std::string( indent+2, ' ' );
302 if ( result->isVoid() ) {
303 os << "nothing";
[0dd3a2f]304 } else {
[906e24d]305 result->print( os, indent+2 );
[0dd3a2f]306 } // if
[906e24d]307 os << std::endl;
[0dd3a2f]308 Expression::print( os, indent );
309}
310
[a5f0529]311VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
312 set_result(toType);
313}
314
315VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
316}
317
318VirtualCastExpr::~VirtualCastExpr() {
319 delete arg;
320}
321
322void 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
[3b58d91]336UntypedMemberExpr::UntypedMemberExpr( Expression * _member, Expression *_aggregate, Expression *_aname ) :
[0dd3a2f]337 Expression( _aname ), member(_member), aggregate(_aggregate) {}
[51b73452]338
[0dd3a2f]339UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
[3b58d91]340 Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
[51b73452]341}
342
343UntypedMemberExpr::~UntypedMemberExpr() {
[0dd3a2f]344 delete aggregate;
[3b58d91]345 delete member;
[51b73452]346}
347
348void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
[3b58d91]349 os << "Untyped Member Expression, with field: " << std::endl;
[8c49c0e]350 os << std::string( indent+2, ' ' );
[3b58d91]351 get_member()->print(os, indent+4);
352 os << std::string( indent+2, ' ' );
[51b73452]353
[0dd3a2f]354 Expression *agg = get_aggregate();
[3b58d91]355 os << "from aggregate: " << std::endl;
[89231bc]356 if (agg != 0) {
[3b58d91]357 os << std::string( indent + 4, ' ' );
358 agg->print(os, indent + 4);
[89231bc]359 }
360 os << std::string( indent+2, ' ' );
[0dd3a2f]361 Expression::print( os, indent );
[51b73452]362}
363
[d9fa60a]364namespace {
365 TypeSubstitution makeSub( Type * t ) {
[0698aa1]366 if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( t ) ) {
367 return makeSub( refType->get_base() );
368 } else if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) {
[d9fa60a]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 {
[4b0f997]373 assertf( false, "makeSub expects struct or union type for aggregate, but got: %s", toString( t ).c_str() );
[d9fa60a]374 }
375 }
376}
377
[51b73452]378
379MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
[0dd3a2f]380 Expression( _aname ), member(_member), aggregate(_aggregate) {
[d9fa60a]381
382 TypeSubstitution sub( makeSub( aggregate->get_result() ) );
383 Type * res = member->get_type()->clone();
384 sub.apply( res );
385 set_result( res );
[615a096]386 get_result()->set_lvalue( true );
[51b73452]387}
388
[0dd3a2f]389MemberExpr::MemberExpr( const MemberExpr &other ) :
[39f84a4]390 Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
[51b73452]391}
392
393MemberExpr::~MemberExpr() {
[4551a6e]394 // don't delete the member declaration, since it points somewhere else in the tree
[0dd3a2f]395 delete aggregate;
[51b73452]396}
397
398void MemberExpr::print( std::ostream &os, int indent ) const {
[89231bc]399 os << "Member Expression, with field: " << std::endl;
[51b73452]400
[0dd3a2f]401 assert( member );
[44b5ca0]402 os << std::string( indent + 2, ' ' );
[0dd3a2f]403 member->print( os, indent + 2 );
404 os << std::endl;
[51b73452]405
[0dd3a2f]406 Expression *agg = get_aggregate();
[44b5ca0]407 os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
[89231bc]408 if (agg != 0) {
[bb8ea30]409 os << std::string( indent + 2, ' ' );
[89231bc]410 agg->print(os, indent + 2);
411 }
412 os << std::string( indent+2, ' ' );
[0dd3a2f]413 Expression::print( os, indent );
[51b73452]414}
415
[6eb8948]416UntypedExpr::UntypedExpr( Expression *_function, const std::list<Expression *> &_args, Expression *_aname ) :
417 Expression( _aname ), function(_function), args(_args) {}
[51b73452]418
[0dd3a2f]419UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
420 Expression( other ), function( maybeClone( other.function ) ) {
421 cloneAll( other.args, args );
[51b73452]422}
423
[ac71a86]424UntypedExpr::~UntypedExpr() {
425 delete function;
[03b812d2]426 deleteAll( args );
[ac71a86]427}
[51b73452]428
[b3b2077]429UntypedExpr * 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 );
[0698aa1]433 assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() );
[b0440b7]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 }
[b3b2077]442 }
443 return ret;
444}
445
446UntypedExpr * 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
[51b73452]458void UntypedExpr::print( std::ostream &os, int indent ) const {
[89231bc]459 os << "Applying untyped: " << std::endl;
[bb8ea30]460 os << std::string( indent+2, ' ' );
461 function->print(os, indent + 2);
[44b5ca0]462 os << std::string( indent, ' ' ) << "...to: " << std::endl;
[bb8ea30]463 printAll(args, os, indent + 2);
[0dd3a2f]464 Expression::print( os, indent );
[51b73452]465}
466
[0dd3a2f]467void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
468 std::list<Expression *>::const_iterator i;
[60089f4]469 for (i = args.begin(); i != args.end(); i++) {
470 os << std::string(indent, ' ' );
[0dd3a2f]471 (*i)->print(os, indent);
[60089f4]472 }
[51b73452]473}
474
[4cb935e]475NameExpr::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}
[51b73452]479
[0dd3a2f]480NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
[51b73452]481}
482
483NameExpr::~NameExpr() {}
484
485void NameExpr::print( std::ostream &os, int indent ) const {
[89231bc]486 os << "Name: " << get_name() << std::endl;
[0dd3a2f]487 Expression::print( os, indent );
[51b73452]488}
489
490LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
[0dd3a2f]491 Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
[906e24d]492 set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[51b73452]493}
494
[0dd3a2f]495LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
496 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
[51b73452]497}
498
[a08ba92]499LogicalExpr::~LogicalExpr() {
[0dd3a2f]500 delete arg1;
501 delete arg2;
[51b73452]502}
503
504void LogicalExpr::print( std::ostream &os, int indent )const {
[f19339e]505 os << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
[0dd3a2f]506 arg1->print(os);
507 os << " and ";
508 arg2->print(os);
509 os << std::endl;
510 Expression::print( os, indent );
[51b73452]511}
512
513ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
[0dd3a2f]514 Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
[51b73452]515
[0dd3a2f]516ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
517 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
518}
[51b73452]519
520ConditionalExpr::~ConditionalExpr() {
[0dd3a2f]521 delete arg1;
522 delete arg2;
523 delete arg3;
[51b73452]524}
525
526void ConditionalExpr::print( std::ostream &os, int indent ) const {
[b3b2077]527 os << "Conditional expression on: " << std::endl;
528 os << std::string( indent+2, ' ' );
[0dd3a2f]529 arg1->print( os, indent+2 );
[44b5ca0]530 os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
[b3b2077]531 os << std::string( indent+2, ' ' );
[0dd3a2f]532 arg2->print( os, indent+2 );
[44b5ca0]533 os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
[b3b2077]534 os << std::string( indent+2, ' ' );
[0dd3a2f]535 arg3->print( os, indent+2 );
536 os << std::endl;
537 Expression::print( os, indent );
538}
539
[e6cee92]540AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
[3be261a]541
542
[7f5566b]543void 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
[db4ecc5]550
551ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
552 assert( callExpr );
[906e24d]553 assert( callExpr->has_result() );
554 set_result( callExpr->get_result()->clone() );
[db4ecc5]555}
556
557ImplicitCopyCtorExpr::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
563ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
[d5556a3]564 set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment
[db4ecc5]565 delete callExpr;
566 deleteAll( tempDecls );
567 deleteAll( returnDecls );
568 deleteAll( dtors );
569}
570
571void ImplicitCopyCtorExpr::print( std::ostream &os, int indent ) const {
[b6fe7e6]572 os << "Implicit Copy Constructor Expression: " << std::endl;
[db4ecc5]573 assert( callExpr );
[d5556a3]574 os << std::string( indent+2, ' ' );
[db4ecc5]575 callExpr->print( os, indent + 2 );
576 os << std::endl << std::string( indent, ' ' ) << "with temporaries:" << std::endl;
577 printAll(tempDecls, os, indent+2);
[dc2e7e0]578 os << std::endl << std::string( indent, ' ' ) << "with return temporaries:" << std::endl;
579 printAll(returnDecls, os, indent+2);
[db4ecc5]580 Expression::print( os, indent );
581}
582
[3be261a]583
[b6fe7e6]584ConstructorExpr::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 );
[906e24d]589 set_result( maybeClone( arg->get_result() ) );
[b6fe7e6]590}
[3be261a]591
[b6fe7e6]592ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
593}
594
595ConstructorExpr::~ConstructorExpr() {
596 delete callExpr;
597}
598
599void 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 );
[0dd3a2f]605}
606
[3be261a]607
[fbcde64]608CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) {
[3c13c03]609 assert( type && initializer );
[5ccb10d]610 type->set_lvalue( true );
[fbcde64]611 set_result( type );
[630a82a]612}
613
[fbcde64]614CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {}
[630a82a]615
616CompoundLiteralExpr::~CompoundLiteralExpr() {
617 delete initializer;
618}
619
620void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
621 os << "Compound Literal Expression: " << std::endl;
[3c13c03]622 os << std::string( indent+2, ' ' );
[fbcde64]623 get_result()->print( os, indent + 2 );
[3c13c03]624 os << std::string( indent+2, ' ' );
625 initializer->print( os, indent + 2 );
[d5556a3]626 Expression::print( os, indent );
[630a82a]627}
628
[d9e2280]629RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
[3c13c03]630RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
[8688ce1]631void RangeExpr::print( std::ostream &os, int indent ) const {
[3c13c03]632 os << "Range Expression: ";
[8688ce1]633 low->print( os, indent );
634 os << " ... ";
635 high->print( os, indent );
[d5556a3]636 Expression::print( os, indent );
[8688ce1]637}
[3be261a]638
[6eb8948]639StmtExpr::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() ) ) {
[aa8f9df]644 set_result( maybeClone( exprStmt->get_expr()->get_result() ) );
[6eb8948]645 }
646 }
[07516b56]647 // ensure that StmtExpr has a result type
648 if ( ! result ) {
649 set_result( new VoidType( Type::Qualifiers() ) );
650 }
[6eb8948]651}
[d5556a3]652StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {
653 cloneAll( other.returnDecls, returnDecls );
654 cloneAll( other.dtors, dtors );
655}
[6eb8948]656StmtExpr::~StmtExpr() {
657 delete statements;
[d5556a3]658 deleteAll( dtors );
659 deleteAll( returnDecls );
[6eb8948]660}
661void StmtExpr::print( std::ostream &os, int indent ) const {
[3c13c03]662 os << "Statement Expression: " << std::endl << std::string( indent, ' ' );
[6eb8948]663 statements->print( os, indent+2 );
[d5556a3]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 );
[6eb8948]673}
674
[3c13c03]675
[bf32bb8]676long long UniqueExpr::count = 0;
[141b786]677UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) {
678 assert( expr );
[bf32bb8]679 assert( count != -1 );
680 if ( id == -1 ) id = count++;
681 if ( expr->get_result() ) {
682 set_result( expr->get_result()->clone() );
683 }
[3c13c03]684}
[141b786]685UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) {
[3c13c03]686}
687UniqueExpr::~UniqueExpr() {
[141b786]688 delete expr;
689 delete object;
690 delete var;
[3c13c03]691}
692void UniqueExpr::print( std::ostream &os, int indent ) const {
[bf32bb8]693 os << "Unique Expression with id:" << id << std::endl << std::string( indent+2, ' ' );
[3c13c03]694 get_expr()->print( os, indent+2 );
[77971f6]695 if ( get_object() ) {
[d5556a3]696 os << std::string( indent+2, ' ' ) << "with decl: ";
[77971f6]697 get_object()->printShort( os, indent+2 );
698 }
[d5556a3]699 Expression::print( os, indent );
[3c13c03]700}
701
[e4d829b]702InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {}
703InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {}
704InitAlternative::~InitAlternative() {
705 delete type;
706 delete designation;
707}
708
709UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {}
710UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {}
711UntypedInitExpr::~UntypedInitExpr() {
712 delete expr;
713}
714
715void 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
[62423350]727InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) {
728 set_result( expr->get_result()->clone() );
[e4d829b]729}
730InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
731InitExpr::~InitExpr() {
732 delete expr;
733 delete designation;
734}
735
736void 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
[3906301]744std::ostream & operator<<( std::ostream & out, const Expression * expr ) {
[8bf784a]745 if ( expr ) {
746 expr->print( out );
747 } else {
748 out << "nullptr";
749 }
[baf7fee]750 return out;
751}
752
[0dd3a2f]753// Local Variables: //
754// tab-width: 4 //
755// mode: c++ //
756// compile-command: "make install" //
757// End: //
Note: See TracBrowser for help on using the repository browser.