source: src/SynTree/Expression.cc@ b117e0c

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 b117e0c was 546e712, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fix for 1 bug of N

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