source: src/SynTree/Expression.cc@ 158b026

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 158b026 was 5d00425, checked in by Andrew Beach <ajbeach@…>, 6 years ago

StmtExpr also does not have the lvalue I originally expected.

  • Property mode set to 100644
File size: 26.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
[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...
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
[5170d95]35void printInferParams( const InferredParams & inferParams, std::ostream & os, Indenter indent, int level ) {
[68195a6]36 if ( ! inferParams.empty() ) {
37 os << indent << "with inferred parameters " << level << ":" << std::endl;
38 for ( InferredParams::const_iterator i = inferParams.begin(); i != inferParams.end(); ++i ) {
39 os << indent+1;
[aaeacf4]40 assert(i->second.declptr);
41 i->second.declptr->printShort( os, indent+1 );
[68195a6]42 os << std::endl;
[0b00df0]43 printInferParams( i->second.expr->inferParams, os, indent+1, level+1 );
[68195a6]44 } // for
45 } // if
46}
47
[bf4b4cf]48Expression::Expression() : result( 0 ), env( 0 ) {}
[0dd3a2f]49
[5170d95]50Expression::Expression( const Expression & other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), extension( other.extension ), inferParams( other.inferParams ), resnSlots( other.resnSlots ) {}
[0dd3a2f]51
[cdb990a]52void Expression::spliceInferParams( Expression * other ) {
53 if ( ! other ) return;
54 for ( auto p : other->inferParams ) {
55 inferParams[p.first] = std::move( p.second );
56 }
[0b00df0]57 resnSlots.insert( resnSlots.end(), other->resnSlots.begin(), other->resnSlots.end() );
[cdb990a]58}
59
[0dd3a2f]60Expression::~Expression() {
61 delete env;
[906e24d]62 delete result;
[51b73452]63}
64
[2d80111]65bool Expression::get_lvalue() const {
[14388c1]66 assert( !result->get_lvalue() );
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();
[615a096]117 type->set_lvalue( true );
[3de176d]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;
[0690350]125 // long long int value;
126 // if ( decl->valueOf( var, value ) ) {
127 // type->set_lvalue( false );
[3de176d]128 // }
129 // }
130
[906e24d]131 set_result( type );
[51b73452]132}
133
[5170d95]134VariableExpr::VariableExpr( const VariableExpr & other ) : Expression( other ), var( other.var ) {
[51b73452]135}
136
[0dd3a2f]137VariableExpr::~VariableExpr() {
138 // don't delete the declaration, since it points somewhere else in the tree
[51b73452]139}
140
[14388c1]141bool VariableExpr::get_lvalue() const {
142 return result->get_lvalue();
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 {
279 return result->get_lvalue();
280}
281
[5170d95]282void CastExpr::print( std::ostream & os, Indenter indent ) const {
283 os << (isGenerated ? "Generated " : "Explicit ") << "Cast of:" << std::endl << indent+1;
[50377a4]284 arg->print(os, indent+1);
285 os << std::endl << indent << "... to:";
[906e24d]286 if ( result->isVoid() ) {
[50377a4]287 os << " nothing";
[0dd3a2f]288 } else {
[50377a4]289 os << std::endl << indent+1;
290 result->print( os, indent+1 );
[0dd3a2f]291 } // if
292 Expression::print( os, indent );
293}
294
[5170d95]295KeywordCastExpr::KeywordCastExpr( Expression * arg, Target target ) : Expression(), arg(arg), target( target ) {
[9a705dc8]296}
297
[5170d95]298KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr & other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) {
[9a705dc8]299}
300
301KeywordCastExpr::~KeywordCastExpr() {
302 delete arg;
303}
304
305const std::string & KeywordCastExpr::targetString() const {
306 static const std::string targetStrs[] = {
307 "coroutine", "thread", "monitor"
308 };
309 static_assert(
310 (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS),
311 "Each KeywordCastExpr::Target should have a corresponding string representation"
312 );
313 return targetStrs[(unsigned long)target];
314}
315
[5170d95]316void KeywordCastExpr::print( std::ostream & os, Indenter indent ) const {
[9a705dc8]317 os << "Keyword Cast of:" << std::endl << indent+1;
318 arg->print(os, indent+1);
319 os << std::endl << indent << "... to: ";
320 os << targetString();
321 Expression::print( os, indent );
322}
323
[5170d95]324VirtualCastExpr::VirtualCastExpr( Expression * arg_, Type * toType ) : Expression(), arg(arg_) {
[a5f0529]325 set_result(toType);
326}
327
[5170d95]328VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr & other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
[a5f0529]329}
330
331VirtualCastExpr::~VirtualCastExpr() {
332 delete arg;
333}
334
[5170d95]335void VirtualCastExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]336 os << "Virtual Cast of:" << std::endl << indent+1;
337 arg->print(os, indent+1);
338 os << std::endl << indent << "... to:";
[a5f0529]339 if ( ! result ) {
[50377a4]340 os << " unknown";
[a5f0529]341 } else {
[50377a4]342 os << std::endl << indent+1;
343 result->print( os, indent+1 );
[a5f0529]344 } // if
345 Expression::print( os, indent );
346}
347
[5170d95]348UntypedMemberExpr::UntypedMemberExpr( Expression * member, Expression * aggregate ) :
[bf4b4cf]349 Expression(), member(member), aggregate(aggregate) {
[50377a4]350 assert( aggregate );
351}
[51b73452]352
[5170d95]353UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr & other ) :
[3b58d91]354 Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
[51b73452]355}
356
357UntypedMemberExpr::~UntypedMemberExpr() {
[0dd3a2f]358 delete aggregate;
[3b58d91]359 delete member;
[51b73452]360}
361
[5170d95]362void UntypedMemberExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]363 os << "Untyped Member Expression, with field: " << std::endl << indent+1;
364 member->print(os, indent+1 );
[08222c7]365 os << indent << "... from aggregate:" << std::endl << indent+1;
[50377a4]366 aggregate->print(os, indent+1);
[0dd3a2f]367 Expression::print( os, indent );
[51b73452]368}
369
[5170d95]370MemberExpr::MemberExpr( DeclarationWithType * member, Expression * aggregate ) :
[bf4b4cf]371 Expression(), member(member), aggregate(aggregate) {
[50377a4]372 assert( member );
373 assert( aggregate );
[9bfc9da]374 assert( aggregate->result );
[d9fa60a]375
[9bfc9da]376 TypeSubstitution sub = aggregate->result->genericSubstitution();
[d9fa60a]377 Type * res = member->get_type()->clone();
378 sub.apply( res );
[487845d7]379 result = res;
380 result->set_lvalue( true );
381 result->get_qualifiers() |= aggregate->result->get_qualifiers();
[51b73452]382}
383
[5170d95]384MemberExpr::MemberExpr( const MemberExpr & other ) :
[39f84a4]385 Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
[51b73452]386}
387
388MemberExpr::~MemberExpr() {
[4551a6e]389 // don't delete the member declaration, since it points somewhere else in the tree
[0dd3a2f]390 delete aggregate;
[51b73452]391}
392
[14388c1]393bool MemberExpr::get_lvalue() const {
394 assert( result->get_lvalue() );
395 return true;
396}
397
[5170d95]398void MemberExpr::print( std::ostream & os, Indenter indent ) const {
[08222c7]399 os << "Member Expression, with field:" << std::endl;
[50377a4]400 os << indent+1;
401 member->print( os, indent+1 );
[08222c7]402 os << std::endl << indent << "... from aggregate:" << std::endl << indent+1;
[50377a4]403 aggregate->print(os, indent + 1);
[0dd3a2f]404 Expression::print( os, indent );
[51b73452]405}
406
[5170d95]407UntypedExpr::UntypedExpr( Expression * function, const std::list<Expression *> & args ) :
[bf4b4cf]408 Expression(), function(function), args(args) {}
[51b73452]409
[5170d95]410UntypedExpr::UntypedExpr( const UntypedExpr & other ) :
[0dd3a2f]411 Expression( other ), function( maybeClone( other.function ) ) {
412 cloneAll( other.args, args );
[51b73452]413}
414
[ac71a86]415UntypedExpr::~UntypedExpr() {
416 delete function;
[03b812d2]417 deleteAll( args );
[ac71a86]418}
[51b73452]419
[b3b2077]420UntypedExpr * UntypedExpr::createDeref( Expression * expr ) {
421 UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } );
422 if ( Type * type = expr->get_result() ) {
423 Type * base = InitTweak::getPointerBase( type );
[0698aa1]424 assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() );
[b0440b7]425 ret->set_result( base->clone() );
426 if ( GenPoly::referencesPermissable() ) {
427 // if references are still allowed in the AST, dereference returns a reference
428 ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) );
429 } else {
430 // references have been removed, in which case dereference returns an lvalue of the base type.
[0690350]431 ret->result->set_lvalue( true );
[b0440b7]432 }
[b3b2077]433 }
434 return ret;
435}
436
437UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) {
438 assert( arg1 && arg2 );
439 UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } );
440 if ( arg1->get_result() && arg2->get_result() ) {
441 // if both expressions are typed, assumes that this assignment is a C bitwise assignment,
442 // so the result is the type of the RHS
443 ret->set_result( arg2->get_result()->clone() );
444 }
445 return ret;
446}
447
[14388c1]448bool UntypedExpr::get_lvalue() const {
449 return result->get_lvalue();
450}
[b3b2077]451
[5170d95]452void UntypedExpr::print( std::ostream & os, Indenter indent ) const {
[07ec1a2]453 os << "Applying untyped:" << std::endl;
[50377a4]454 os << indent+1;
455 function->print(os, indent+1);
[07ec1a2]456 os << std::endl << indent << "...to:" << std::endl;
[50377a4]457 printAll(args, os, indent+1);
[0dd3a2f]458 Expression::print( os, indent );
[51b73452]459}
460
[bf4b4cf]461NameExpr::NameExpr( std::string name ) : Expression(), name(name) {
[50377a4]462 assertf(name != "0", "Zero is not a valid name");
463 assertf(name != "1", "One is not a valid name");
[4cb935e]464}
[51b73452]465
[5170d95]466NameExpr::NameExpr( const NameExpr & other ) : Expression( other ), name( other.name ) {
[51b73452]467}
468
469NameExpr::~NameExpr() {}
470
[5170d95]471void NameExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]472 os << "Name: " << get_name();
[0dd3a2f]473 Expression::print( os, indent );
[51b73452]474}
475
[5170d95]476LogicalExpr::LogicalExpr( Expression * arg1_, Expression * arg2_, bool andp ) :
[bf4b4cf]477 Expression(), arg1(arg1_), arg2(arg2_), isAnd(andp) {
[906e24d]478 set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[51b73452]479}
480
[5170d95]481LogicalExpr::LogicalExpr( const LogicalExpr & other ) :
[0dd3a2f]482 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
[51b73452]483}
484
[a08ba92]485LogicalExpr::~LogicalExpr() {
[0dd3a2f]486 delete arg1;
487 delete arg2;
[51b73452]488}
489
[5170d95]490void LogicalExpr::print( std::ostream & os, Indenter indent )const {
[50377a4]491 os << "Short-circuited operation (" << (isAnd ? "and" : "or") << ") on: ";
[0dd3a2f]492 arg1->print(os);
493 os << " and ";
494 arg2->print(os);
495 Expression::print( os, indent );
[51b73452]496}
497
[bf4b4cf]498ConditionalExpr::ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 ) :
499 Expression(), arg1(arg1), arg2(arg2), arg3(arg3) {}
[51b73452]500
[5170d95]501ConditionalExpr::ConditionalExpr( const ConditionalExpr & other ) :
[0dd3a2f]502 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
503}
[51b73452]504
505ConditionalExpr::~ConditionalExpr() {
[0dd3a2f]506 delete arg1;
507 delete arg2;
508 delete arg3;
[51b73452]509}
510
[14388c1]511bool ConditionalExpr::get_lvalue() const {
512 return result->get_lvalue();
513}
514
[5170d95]515void ConditionalExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]516 os << "Conditional expression on: " << std::endl << indent+1;
517 arg1->print( os, indent+1 );
518 os << indent << "First alternative:" << std::endl << indent+1;
519 arg2->print( os, indent+1 );
520 os << indent << "Second alternative:" << std::endl << indent+1;
521 arg3->print( os, indent+1 );
[0dd3a2f]522 Expression::print( os, indent );
523}
524
[e6cee92]525AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
[3be261a]526
527
[5170d95]528void AsmExpr::print( std::ostream & os, Indenter indent ) const {
[7f5566b]529 os << "Asm Expression: " << std::endl;
[50377a4]530 if ( inout ) inout->print( os, indent+1 );
531 if ( constraint ) constraint->print( os, indent+1 );
532 if ( operand ) operand->print( os, indent+1 );
[7f5566b]533}
534
[db4ecc5]535
536ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
537 assert( callExpr );
[50377a4]538 assert( callExpr->result );
[2f86ddf]539 set_result( callExpr->result->clone() );
[db4ecc5]540}
541
542ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
543}
544
545ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
[d5556a3]546 set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment
[db4ecc5]547 delete callExpr;
548}
549
[5170d95]550void ImplicitCopyCtorExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]551 os << "Implicit Copy Constructor Expression: " << std::endl << indent+1;
552 callExpr->print( os, indent+1 );
[db4ecc5]553}
554
[3be261a]555
[b6fe7e6]556ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) {
557 // allow resolver to type a constructor used as an expression as if it has the same type as its first argument
558 assert( callExpr );
559 Expression * arg = InitTweak::getCallArg( callExpr, 0 );
560 assert( arg );
[50377a4]561 set_result( maybeClone( arg->result ) );
[b6fe7e6]562}
[3be261a]563
[b6fe7e6]564ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
565}
566
567ConstructorExpr::~ConstructorExpr() {
568 delete callExpr;
569}
570
[14388c1]571bool ConstructorExpr::get_lvalue() const {
572 return result->get_lvalue();
573}
574
[5170d95]575void ConstructorExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]576 os << "Constructor Expression: " << std::endl << indent+1;
[b6fe7e6]577 callExpr->print( os, indent + 2 );
578 Expression::print( os, indent );
[0dd3a2f]579}
580
[3be261a]581
[fbcde64]582CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) {
[3c13c03]583 assert( type && initializer );
[5ccb10d]584 type->set_lvalue( true );
[fbcde64]585 set_result( type );
[630a82a]586}
587
[5170d95]588CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr & other ) : Expression( other ), initializer( other.initializer->clone() ) {}
[630a82a]589
590CompoundLiteralExpr::~CompoundLiteralExpr() {
591 delete initializer;
592}
593
[14388c1]594bool CompoundLiteralExpr::get_lvalue() const {
595 assert( result->get_lvalue() );
596 return true;
597}
598
[5170d95]599void CompoundLiteralExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]600 os << "Compound Literal Expression: " << std::endl << indent+1;
601 result->print( os, indent+1 );
602 os << indent+1;
603 initializer->print( os, indent+1 );
[d5556a3]604 Expression::print( os, indent );
[630a82a]605}
606
[5170d95]607RangeExpr::RangeExpr( Expression * low, Expression * high ) : low( low ), high( high ) {}
608RangeExpr::RangeExpr( const RangeExpr & other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
609void RangeExpr::print( std::ostream & os, Indenter indent ) const {
[3c13c03]610 os << "Range Expression: ";
[8688ce1]611 low->print( os, indent );
612 os << " ... ";
613 high->print( os, indent );
[d5556a3]614 Expression::print( os, indent );
[8688ce1]615}
[3be261a]616
[5170d95]617StmtExpr::StmtExpr( CompoundStmt * statements ) : statements( statements ) {
[5e2c348]618 computeResult();
619}
[0e315a5]620StmtExpr::StmtExpr( const StmtExpr & other ) : Expression( other ), statements( other.statements->clone() ), resultExpr( other.resultExpr ) {
[5e2c348]621 cloneAll( other.returnDecls, returnDecls );
622 cloneAll( other.dtors, dtors );
623}
624StmtExpr::~StmtExpr() {
625 delete statements;
626 deleteAll( dtors );
627 deleteAll( returnDecls );
628}
629void StmtExpr::computeResult() {
[6eb8948]630 assert( statements );
[5e2c348]631 std::list< Statement * > & body = statements->kids;
632 delete result;
633 result = nullptr;
[0992849]634 if ( ! returnDecls.empty() ) {
635 // prioritize return decl for result type, since if a return decl exists, then
636 // the StmtExpr is currently in an intermediate state where the body will always
637 // give a void result type.
638 result = returnDecls.front()->get_type()->clone();
639 } else if ( ! body.empty() ) {
[6eb8948]640 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
[9fe33947]641 result = maybeClone( exprStmt->expr->result );
[6eb8948]642 }
643 }
[07516b56]644 // ensure that StmtExpr has a result type
645 if ( ! result ) {
[9fe33947]646 result = new VoidType( Type::Qualifiers() );
[07516b56]647 }
[6eb8948]648}
[5d00425]649bool StmtExpr::get_lvalue() const {
650 return result->get_lvalue();
651}
[5170d95]652void StmtExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]653 os << "Statement Expression: " << std::endl << indent+1;
654 statements->print( os, indent+1 );
[d5556a3]655 if ( ! returnDecls.empty() ) {
[50377a4]656 os << indent+1 << "... with returnDecls: ";
657 printAll( returnDecls, os, indent+1 );
[d5556a3]658 }
659 if ( ! dtors.empty() ) {
[50377a4]660 os << indent+1 << "... with dtors: ";
661 printAll( dtors, os, indent+1 );
[d5556a3]662 }
663 Expression::print( os, indent );
[6eb8948]664}
665
[3c13c03]666
[bf32bb8]667long long UniqueExpr::count = 0;
[5170d95]668UniqueExpr::UniqueExpr( Expression * expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) {
[141b786]669 assert( expr );
[bf32bb8]670 assert( count != -1 );
671 if ( id == -1 ) id = count++;
672 if ( expr->get_result() ) {
673 set_result( expr->get_result()->clone() );
674 }
[3c13c03]675}
[5170d95]676UniqueExpr::UniqueExpr( const UniqueExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) {
[3c13c03]677}
678UniqueExpr::~UniqueExpr() {
[141b786]679 delete expr;
680 delete object;
681 delete var;
[3c13c03]682}
[5170d95]683void UniqueExpr::print( std::ostream & os, Indenter indent ) const {
[50377a4]684 os << "Unique Expression with id:" << id << std::endl << indent+1;
685 expr->print( os, indent+1 );
686 if ( object ) {
687 os << indent << "... with decl: ";
688 get_object()->printShort( os, indent+1 );
[77971f6]689 }
[d5556a3]690 Expression::print( os, indent );
[3c13c03]691}
692
[e4d829b]693InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {}
694InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {}
695InitAlternative::~InitAlternative() {
696 delete type;
697 delete designation;
698}
699
700UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {}
701UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {}
702UntypedInitExpr::~UntypedInitExpr() {
703 delete expr;
704}
705
[50377a4]706void UntypedInitExpr::print( std::ostream & os, Indenter indent ) const {
707 os << "Untyped Init Expression" << std::endl << indent+1;
708 expr->print( os, indent+1 );
[e4d829b]709 if ( ! initAlts.empty() ) {
710 for ( const InitAlternative & alt : initAlts ) {
[50377a4]711 os << indent+1 << "InitAlternative: ";
712 alt.type->print( os, indent+1 );
713 alt.designation->print( os, indent+1 );
[e4d829b]714 }
715 }
716}
717
[62423350]718InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) {
719 set_result( expr->get_result()->clone() );
[e4d829b]720}
721InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
722InitExpr::~InitExpr() {
723 delete expr;
724 delete designation;
725}
726
[50377a4]727void InitExpr::print( std::ostream & os, Indenter indent ) const {
728 os << "Init Expression" << std::endl << indent+1;
729 expr->print( os, indent+1 );
730 os << indent+1 << "... with designation: ";
731 designation->print( os, indent+1 );
[e4d829b]732}
733
[e67991f]734DeletedExpr::DeletedExpr( Expression * expr, Declaration * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) {
[44b4114]735 assert( expr->result );
736 result = expr->result->clone();
737}
738DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {}
739DeletedExpr::~DeletedExpr() {
740 delete expr;
741}
742
743void DeletedExpr::print( std::ostream & os, Indenter indent ) const {
744 os << "Deleted Expression" << std::endl << indent+1;
745 expr->print( os, indent+1 );
746 os << std::endl << indent+1 << "... deleted by: ";
747 deleteStmt->print( os, indent+1 );
748}
749
[0f79853]750
751DefaultArgExpr::DefaultArgExpr( Expression * expr ) : expr( expr ) {
752 assert( expr->result );
753 result = expr->result->clone();
754}
755DefaultArgExpr::DefaultArgExpr( const DefaultArgExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ) {}
756DefaultArgExpr::~DefaultArgExpr() {
757 delete expr;
758}
759
760void DefaultArgExpr::print( std::ostream & os, Indenter indent ) const {
761 os << "Default Argument Expression" << std::endl << indent+1;
762 expr->print( os, indent+1 );
763}
764
[d807ca28]765GenericExpr::Association::Association( Type * type, Expression * expr ) : type( type ), expr( expr ), isDefault( false ) {}
766GenericExpr::Association::Association( Expression * expr ) : type( nullptr ), expr( expr ), isDefault( true ) {}
767GenericExpr::Association::Association( const Association & other ) : type( maybeClone( other.type ) ), expr( maybeClone( other.expr ) ), isDefault( other.isDefault ) {}
768GenericExpr::Association::~Association() {
769 delete type;
770 delete expr;
771}
772
773GenericExpr::GenericExpr( Expression * control, const std::list<Association> & assoc ) : Expression(), control( control ), associations( assoc ) {}
774GenericExpr::GenericExpr( const GenericExpr & other ) : Expression(other), control( maybeClone( other.control ) ), associations( other.associations ) {
775}
776GenericExpr::~GenericExpr() {
777 delete control;
778}
779
780void GenericExpr::print( std::ostream & os, Indenter indent ) const {
781 os << "C11 _Generic Expression" << std::endl << indent+1;
782 control->print( os, indent+1 );
783 os << std::endl << indent+1 << "... with associations: " << std::endl;
784 for ( const Association & assoc : associations ) {
785 os << indent+1;
786 if (assoc.isDefault) {
787 os << "... default: ";
788 assoc.expr->print( os, indent+1 );
789 } else {
790 os << "... type: ";
791 assoc.type->print( os, indent+1 );
792 os << std::endl << indent+1 << "... expression: ";
793 assoc.expr->print( os, indent+1 );
794 os << std::endl;
795 }
796 os << std::endl;
797 }
798}
[44b4114]799
[0dd3a2f]800// Local Variables: //
801// tab-width: 4 //
802// mode: c++ //
803// compile-command: "make install" //
804// End: //
Note: See TracBrowser for help on using the repository browser.