source: src/SynTree/Expression.cc@ 0d92e5c

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 0d92e5c was 033ff37, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

remove attribute expression '@'name mechanism

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