source: src/SynTree/Expression.cc@ 9e0a360

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 9e0a360 was aaeacf4, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Removed global look-up table from UniqueId to Decl

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