source: src/SynTree/Expression.cc@ ef46abb

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 ef46abb was 2d80111, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Lvalue is checked through Expression::get_lvalue. Only three other places use Type::get_lvalue directly now: CodeGen/GenType, ResolvExpr/ConversionCost & SynTree/TopLvalue.

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