source: src/SynTree/Expression.cc@ e8c52cf

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since e8c52cf was 665f432, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fixed trac #149 where operand names in asm statements where incorrectly resolved (i.e., should not have been resolved)

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