source: src/SynTree/Expression.cc @ 34dcc474

new-envwith_gc
Last change on this file since 34dcc474 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 6 years ago

First pass at delete removal

  • Property mode set to 100644
File size: 21.9 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
[a5f0529]12// Last Modified On : Tue Jul 25 14:15:47 2017
13// Update Count     : 54
[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
[51b7345]32
[b0440b7]33#include "GenPoly/Lvalue.h"
[51b7345]34
[68195a6]35void printInferParams( const InferredParams & inferParams, std::ostream &os, Indenter indent, int level ) {
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;
42                        printInferParams( *i->second.inferParams, os, indent+1, level+1 );
43                } // for
44        } // if
45}
46
[bf4b4cf]47Expression::Expression() : result( 0 ), env( 0 ) {}
[0dd3a2f]48
[df626eb]49Expression::Expression( const Expression &other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), extension( other.extension ), inferParams( other.inferParams ) {
[0dd3a2f]50}
51
52Expression::~Expression() {
53        delete env;
[51b7345]54}
55
[50377a4]56void Expression::print( std::ostream &os, Indenter indent ) const {
[68195a6]57        printInferParams( inferParams, os, indent+1, 0 );
58
[0dd3a2f]59        if ( env ) {
[50377a4]60                os << std::endl << indent << "... with environment:" << std::endl;
61                env->print( os, indent+1 );
[0dd3a2f]62        } // if
[e04ef3a]63
64        if ( extension ) {
[50377a4]65                os << std::endl << indent << "... with extension:";
[e04ef3a]66        } // if
[51b7345]67}
68
[bf4b4cf]69ConstantExpr::ConstantExpr( Constant _c ) : Expression(), constant( _c ) {
[906e24d]70        set_result( constant.get_type()->clone() );
[51b7345]71}
72
[0dd3a2f]73ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
74}
[51b7345]75
[50377a4]76void ConstantExpr::print( std::ostream &os, Indenter indent ) const {
[60089f4]77        os << "constant expression " ;
[cf0941d]78        constant.print( os );
[0dd3a2f]79        Expression::print( os, indent );
[51b7345]80}
81
[ddb80bd]82long long int ConstantExpr::intValue() const {
83        if ( BasicType * basicType = dynamic_cast< BasicType * >( result ) ) {
84                if ( basicType->isInteger() ) {
85                        return get_constant()->get_ival();
86                }
87        } else if ( dynamic_cast< OneType * >( result ) ) {
88                return 1;
89        } else if ( dynamic_cast< ZeroType * >( result ) ) {
90                return 0;
91        }
[a16764a6]92        SemanticError( this, "Constant expression of non-integral type " );
[ddb80bd]93}
94
[bf4b4cf]95VariableExpr::VariableExpr( DeclarationWithType *_var ) : Expression(), var( _var ) {
[db4ecc5]96        assert( var );
97        assert( var->get_type() );
[906e24d]98        Type * type = var->get_type()->clone();
[615a096]99        type->set_lvalue( true );
[3de176d]100
101        // xxx - doesn't quite work yet - get different alternatives with the same cost
102
103        // // enumerators are not lvalues
104        // if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( var->get_type() ) ) {
105        //      assert( inst->baseEnum );
106        //      EnumDecl * decl = inst->baseEnum;
[0690350]107        //      long long int value;
108        //      if ( decl->valueOf( var, value ) ) {
109        //              type->set_lvalue( false );
[3de176d]110        //      }
111        // }
112
[906e24d]113        set_result( type );
[51b7345]114}
115
[0dd3a2f]116VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
[51b7345]117}
118
[8a6cf7e]119VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) {
120        VariableExpr * funcExpr = new VariableExpr( func );
121        funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) );
122        return funcExpr;
123}
124
[50377a4]125void VariableExpr::print( std::ostream &os, Indenter indent ) const {
[89231bc]126        os << "Variable Expression: ";
[50377a4]127        var->printShort(os, indent);
[0dd3a2f]128        Expression::print( os, indent );
[51b7345]129}
130
[bf4b4cf]131SizeofExpr::SizeofExpr( Expression *expr_ ) :
132                Expression(), expr(expr_), type(0), isType(false) {
[906e24d]133        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[51b7345]134}
135
[bf4b4cf]136SizeofExpr::SizeofExpr( Type *type_ ) :
137                Expression(), expr(0), type(type_), isType(true) {
[906e24d]138        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[51b7345]139}
140
[0dd3a2f]141SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
142        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
[51b7345]143}
144
[50377a4]145void SizeofExpr::print( std::ostream &os, Indenter indent) const {
[89231bc]146        os << "Sizeof Expression on: ";
[50377a4]147        if (isType) type->print(os, indent+1);
148        else expr->print(os, indent+1);
[47534159]149        Expression::print( os, indent );
150}
151
[bf4b4cf]152AlignofExpr::AlignofExpr( Expression *expr_ ) :
153                Expression(), expr(expr_), type(0), isType(false) {
[906e24d]154        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[47534159]155}
156
[bf4b4cf]157AlignofExpr::AlignofExpr( Type *type_ ) :
158                Expression(), expr(0), type(type_), isType(true) {
[906e24d]159        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[47534159]160}
161
162AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
163        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
164}
165
[50377a4]166void AlignofExpr::print( std::ostream &os, Indenter indent) const {
[f19339e]167        os << "Alignof Expression on: ";
[50377a4]168        if (isType) type->print(os, indent+1);
169        else expr->print(os, indent+1);
[0dd3a2f]170        Expression::print( os, indent );
[51b7345]171}
172
[bf4b4cf]173UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type, const std::string &member ) :
174                Expression(), type(type), member(member) {
[50377a4]175        assert( type );
[906e24d]176        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[2a4b088]177}
178
179UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
180        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
181
[50377a4]182void UntypedOffsetofExpr::print( std::ostream &os, Indenter indent) const {
183        os << "Untyped Offsetof Expression on member " << member << " of ";
184        type->print(os, indent+1);
[2a4b088]185        Expression::print( os, indent );
186}
187
[bf4b4cf]188OffsetofExpr::OffsetofExpr( Type *type, DeclarationWithType *member ) :
189                Expression(), type(type), member(member) {
[50377a4]190        assert( member );
191        assert( type );
[906e24d]192        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[25a054f]193}
194
195OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
[79970ed]196        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
[25a054f]197
[50377a4]198void OffsetofExpr::print( std::ostream &os, Indenter indent) const {
199        os << "Offsetof Expression on member " << member->name << " of ";
200        type->print(os, indent+1);
[25a054f]201        Expression::print( os, indent );
202}
203
[bf4b4cf]204OffsetPackExpr::OffsetPackExpr( StructInstType *type ) : Expression(), type( type ) {
[50377a4]205        assert( type );
[906e24d]206        set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
[afc1045]207}
208
209OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
210
[50377a4]211void OffsetPackExpr::print( std::ostream &os, Indenter indent ) const {
212        os << "Offset pack expression on ";
213        type->print(os, indent+1);
[25a054f]214        Expression::print( os, indent );
215}
216
[bf4b4cf]217AttrExpr::AttrExpr( Expression *attr, Expression *expr_ ) :
218                Expression(), attr( attr ), expr(expr_), type(0), isType(false) {
[51b7345]219}
220
[bf4b4cf]221AttrExpr::AttrExpr( Expression *attr, Type *type_ ) :
222                Expression(), attr( attr ), expr(0), type(type_), isType(true) {
[51b7345]223}
224
[0dd3a2f]225AttrExpr::AttrExpr( const AttrExpr &other ) :
226                Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
[51b7345]227}
228
[50377a4]229void AttrExpr::print( std::ostream &os, Indenter indent) const {
[f19339e]230        os << "Attr ";
[50377a4]231        attr->print( os, indent+1);
[0dd3a2f]232        if ( isType || expr ) {
233                os << "applied to: ";
[50377a4]234                if (isType) type->print(os, indent+1);
235                else expr->print(os, indent+1);
[0dd3a2f]236        } // if
237        Expression::print( os, indent );
[51b7345]238}
239
[bf4b4cf]240CastExpr::CastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
[906e24d]241        set_result(toType);
[51b7345]242}
243
[bf4b4cf]244CastExpr::CastExpr( Expression *arg_ ) : Expression(), arg(arg_) {
[906e24d]245        set_result( new VoidType( Type::Qualifiers() ) );
[51b7345]246}
247
[0dd3a2f]248CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
[51b7345]249}
250
[50377a4]251void CastExpr::print( std::ostream &os, Indenter indent ) const {
252        os << "Cast of:" << std::endl << indent+1;
253        arg->print(os, indent+1);
254        os << std::endl << indent << "... to:";
[906e24d]255        if ( result->isVoid() ) {
[50377a4]256                os << " nothing";
[0dd3a2f]257        } else {
[50377a4]258                os << std::endl << indent+1;
259                result->print( os, indent+1 );
[0dd3a2f]260        } // if
261        Expression::print( os, indent );
262}
263
[a5f0529]264VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
265        set_result(toType);
266}
267
268VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
269}
270
[50377a4]271void VirtualCastExpr::print( std::ostream &os, Indenter indent ) const {
272        os << "Virtual Cast of:" << std::endl << indent+1;
273        arg->print(os, indent+1);
274        os << std::endl << indent << "... to:";
[a5f0529]275        if ( ! result ) {
[50377a4]276                os << " unknown";
[a5f0529]277        } else {
[50377a4]278                os << std::endl << indent+1;
279                result->print( os, indent+1 );
[a5f0529]280        } // if
281        Expression::print( os, indent );
282}
283
[bf4b4cf]284UntypedMemberExpr::UntypedMemberExpr( Expression * member, Expression *aggregate ) :
285                Expression(), member(member), aggregate(aggregate) {
[50377a4]286        assert( aggregate );
287}
[51b7345]288
[0dd3a2f]289UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
[3b58d91]290                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
[51b7345]291}
292
[50377a4]293void UntypedMemberExpr::print( std::ostream &os, Indenter indent ) const {
294        os << "Untyped Member Expression, with field: " << std::endl << indent+1;
295        member->print(os, indent+1 );
296        os << indent << "... from aggregate: " << std::endl << indent+1;
297        aggregate->print(os, indent+1);
[0dd3a2f]298        Expression::print( os, indent );
[51b7345]299}
300
[bf4b4cf]301MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) :
302                Expression(), member(member), aggregate(aggregate) {
[50377a4]303        assert( member );
304        assert( aggregate );
[9bfc9da]305        assert( aggregate->result );
[d9fa60a]306
[9bfc9da]307        TypeSubstitution sub = aggregate->result->genericSubstitution();
[d9fa60a]308        Type * res = member->get_type()->clone();
309        sub.apply( res );
[487845d7]310        result = res;
311        result->set_lvalue( true );
312        result->get_qualifiers() |= aggregate->result->get_qualifiers();
[51b7345]313}
314
[0dd3a2f]315MemberExpr::MemberExpr( const MemberExpr &other ) :
[39f84a4]316                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
[51b7345]317}
318
[50377a4]319void MemberExpr::print( std::ostream &os, Indenter indent ) const {
[89231bc]320        os << "Member Expression, with field: " << std::endl;
[50377a4]321        os << indent+1;
322        member->print( os, indent+1 );
323        os << std::endl << indent << "... from aggregate: " << std::endl << indent+1;
324        aggregate->print(os, indent + 1);
[0dd3a2f]325        Expression::print( os, indent );
[51b7345]326}
327
[bf4b4cf]328UntypedExpr::UntypedExpr( Expression *function, const std::list<Expression *> &args ) :
329                Expression(), function(function), args(args) {}
[51b7345]330
[0dd3a2f]331UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
332                Expression( other ), function( maybeClone( other.function ) ) {
333        cloneAll( other.args, args );
[51b7345]334}
335
[b3b2077]336UntypedExpr * UntypedExpr::createDeref( Expression * expr ) {
337        UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } );
338        if ( Type * type = expr->get_result() ) {
339                Type * base = InitTweak::getPointerBase( type );
[0698aa1]340                assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() );
[b0440b7]341                ret->set_result( base->clone() );
342                if ( GenPoly::referencesPermissable() ) {
343                        // if references are still allowed in the AST, dereference returns a reference
344                        ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) );
345                } else {
346                        // references have been removed, in which case dereference returns an lvalue of the base type.
[0690350]347                        ret->result->set_lvalue( true );
[b0440b7]348                }
[b3b2077]349        }
350        return ret;
351}
352
353UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) {
354        assert( arg1 && arg2 );
355        UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } );
356        if ( arg1->get_result() && arg2->get_result() ) {
357                // if both expressions are typed, assumes that this assignment is a C bitwise assignment,
358                // so the result is the type of the RHS
359                ret->set_result( arg2->get_result()->clone() );
360        }
361        return ret;
362}
363
364
[50377a4]365void UntypedExpr::print( std::ostream &os, Indenter indent ) const {
[89231bc]366        os << "Applying untyped: " << std::endl;
[50377a4]367        os << indent+1;
368        function->print(os, indent+1);
369        os << std::endl << indent << "...to: " << std::endl;
370        printAll(args, os, indent+1);
[0dd3a2f]371        Expression::print( os, indent );
[51b7345]372}
373
[bf4b4cf]374NameExpr::NameExpr( std::string name ) : Expression(), name(name) {
[50377a4]375        assertf(name != "0", "Zero is not a valid name");
376        assertf(name != "1", "One is not a valid name");
[4cb935e]377}
[51b7345]378
[0dd3a2f]379NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
[51b7345]380}
381
[50377a4]382void NameExpr::print( std::ostream &os, Indenter indent ) const {
383        os << "Name: " << get_name();
[0dd3a2f]384        Expression::print( os, indent );
[51b7345]385}
386
[bf4b4cf]387LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp ) :
388                Expression(), arg1(arg1_), arg2(arg2_), isAnd(andp) {
[906e24d]389        set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[51b7345]390}
391
[0dd3a2f]392LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
393                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
[51b7345]394}
395
[50377a4]396void LogicalExpr::print( std::ostream &os, Indenter indent )const {
397        os << "Short-circuited operation (" << (isAnd ? "and" : "or") << ") on: ";
[0dd3a2f]398        arg1->print(os);
399        os << " and ";
400        arg2->print(os);
401        Expression::print( os, indent );
[51b7345]402}
403
[bf4b4cf]404ConditionalExpr::ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 ) :
405                Expression(), arg1(arg1), arg2(arg2), arg3(arg3) {}
[51b7345]406
[0dd3a2f]407ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
408                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
409}
[51b7345]410
[50377a4]411void ConditionalExpr::print( std::ostream &os, Indenter indent ) const {
412        os << "Conditional expression on: " << std::endl << indent+1;
413        arg1->print( os, indent+1 );
414        os << indent << "First alternative:" << std::endl << indent+1;
415        arg2->print( os, indent+1 );
416        os << indent << "Second alternative:" << std::endl << indent+1;
417        arg3->print( os, indent+1 );
[0dd3a2f]418        Expression::print( os, indent );
419}
420
[e6cee92]421AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
[3be261a]422
423
[50377a4]424void AsmExpr::print( std::ostream &os, Indenter indent ) const {
[7f5566b]425        os << "Asm Expression: " << std::endl;
[50377a4]426        if ( inout ) inout->print( os, indent+1 );
427        if ( constraint ) constraint->print( os, indent+1 );
428        if ( operand ) operand->print( os, indent+1 );
[7f5566b]429}
430
[db4ecc5]431
432ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
433        assert( callExpr );
[50377a4]434        assert( callExpr->result );
[906e24d]435        set_result( callExpr->get_result()->clone() );
[db4ecc5]436}
437
438ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
439        cloneAll( other.tempDecls, tempDecls );
440        cloneAll( other.returnDecls, returnDecls );
441        cloneAll( other.dtors, dtors );
442}
443
444ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
[d5556a3]445        set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment
[db4ecc5]446}
447
[50377a4]448void ImplicitCopyCtorExpr::print( std::ostream &os, Indenter indent ) const {
449        os <<  "Implicit Copy Constructor Expression: " << std::endl << indent+1;
450        callExpr->print( os, indent+1 );
451        os << std::endl << indent << "... with temporaries:" << std::endl;
452        printAll( tempDecls, os, indent+1 );
453        os << std::endl << indent << "... with return temporaries:" << std::endl;
454        printAll( returnDecls, os, indent+1 );
[db4ecc5]455        Expression::print( os, indent );
456}
457
[3be261a]458
[b6fe7e6]459ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) {
460        // allow resolver to type a constructor used as an expression as if it has the same type as its first argument
461        assert( callExpr );
462        Expression * arg = InitTweak::getCallArg( callExpr, 0 );
463        assert( arg );
[50377a4]464        set_result( maybeClone( arg->result ) );
[b6fe7e6]465}
[3be261a]466
[b6fe7e6]467ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
468}
469
[50377a4]470void ConstructorExpr::print( std::ostream &os, Indenter indent ) const {
471        os <<  "Constructor Expression: " << std::endl << indent+1;
[b6fe7e6]472        callExpr->print( os, indent + 2 );
473        Expression::print( os, indent );
[0dd3a2f]474}
475
[3be261a]476
[fbcde64]477CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) {
[3c13c03]478        assert( type && initializer );
[5ccb10d]479        type->set_lvalue( true );
[fbcde64]480        set_result( type );
[630a82a]481}
482
[fbcde64]483CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {}
[630a82a]484
[50377a4]485void CompoundLiteralExpr::print( std::ostream &os, Indenter indent ) const {
486        os << "Compound Literal Expression: " << std::endl << indent+1;
487        result->print( os, indent+1 );
488        os << indent+1;
489        initializer->print( os, indent+1 );
[d5556a3]490        Expression::print( os, indent );
[630a82a]491}
492
[d9e2280]493RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
[3c13c03]494RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
[50377a4]495void RangeExpr::print( std::ostream &os, Indenter indent ) const {
[3c13c03]496        os << "Range Expression: ";
[8688ce1]497        low->print( os, indent );
498        os << " ... ";
499        high->print( os, indent );
[d5556a3]500        Expression::print( os, indent );
[8688ce1]501}
[3be261a]502
[6eb8948]503StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) {
[5e2c348]504        computeResult();
505}
506StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {
507        cloneAll( other.returnDecls, returnDecls );
508        cloneAll( other.dtors, dtors );
509}
510void StmtExpr::computeResult() {
[6eb8948]511        assert( statements );
[5e2c348]512        std::list< Statement * > & body = statements->kids;
513        result = nullptr;
[0992849]514        if ( ! returnDecls.empty() ) {
515                // prioritize return decl for result type, since if a return decl exists, then
516                // the StmtExpr is currently in an intermediate state where the body will always
517                // give a void result type.
518                result = returnDecls.front()->get_type()->clone();
519        } else if ( ! body.empty() ) {
[6eb8948]520                if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
[9fe33947]521                        result = maybeClone( exprStmt->expr->result );
[6eb8948]522                }
523        }
[07516b56]524        // ensure that StmtExpr has a result type
525        if ( ! result ) {
[9fe33947]526                result = new VoidType( Type::Qualifiers() );
[07516b56]527        }
[6eb8948]528}
[50377a4]529void StmtExpr::print( std::ostream &os, Indenter indent ) const {
530        os << "Statement Expression: " << std::endl << indent+1;
531        statements->print( os, indent+1 );
[d5556a3]532        if ( ! returnDecls.empty() ) {
[50377a4]533                os << indent+1 << "... with returnDecls: ";
534                printAll( returnDecls, os, indent+1 );
[d5556a3]535        }
536        if ( ! dtors.empty() ) {
[50377a4]537                os << indent+1 << "... with dtors: ";
538                printAll( dtors, os, indent+1 );
[d5556a3]539        }
540        Expression::print( os, indent );
[6eb8948]541}
542
[3c13c03]543
[bf32bb8]544long long UniqueExpr::count = 0;
[141b786]545UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) {
546        assert( expr );
[bf32bb8]547        assert( count != -1 );
548        if ( id == -1 ) id = count++;
549        if ( expr->get_result() ) {
550                set_result( expr->get_result()->clone() );
551        }
[3c13c03]552}
[141b786]553UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) {
[3c13c03]554}
[68f9c43]555
[50377a4]556void UniqueExpr::print( std::ostream &os, Indenter indent ) const {
557        os << "Unique Expression with id:" << id << std::endl << indent+1;
558        expr->print( os, indent+1 );
559        if ( object ) {
560                os << indent << "... with decl: ";
561                get_object()->printShort( os, indent+1 );
[77971f6]562        }
[d5556a3]563        Expression::print( os, indent );
[3c13c03]564}
565
[e4d829b]566InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {}
567InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {}
568
569UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {}
570UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {}
571
[50377a4]572void UntypedInitExpr::print( std::ostream & os, Indenter indent ) const {
573        os << "Untyped Init Expression" << std::endl << indent+1;
574        expr->print( os, indent+1 );
[e4d829b]575        if ( ! initAlts.empty() ) {
576                for ( const InitAlternative & alt : initAlts ) {
[50377a4]577                        os << indent+1 <<  "InitAlternative: ";
578                        alt.type->print( os, indent+1 );
579                        alt.designation->print( os, indent+1 );
[e4d829b]580                }
581        }
582}
583
[62423350]584InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) {
585        set_result( expr->get_result()->clone() );
[e4d829b]586}
587InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
588
[50377a4]589void InitExpr::print( std::ostream & os, Indenter indent ) const {
590        os << "Init Expression" << std::endl << indent+1;
591        expr->print( os, indent+1 );
592        os << indent+1 << "... with designation: ";
593        designation->print( os, indent+1 );
[e4d829b]594}
595
[44b4114]596DeletedExpr::DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) {
597        assert( expr->result );
598        result = expr->result->clone();
599}
600DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {}
601
602void DeletedExpr::print( std::ostream & os, Indenter indent ) const {
603        os << "Deleted Expression" << std::endl << indent+1;
604        expr->print( os, indent+1 );
605        os << std::endl << indent+1 << "... deleted by: ";
606        deleteStmt->print( os, indent+1 );
607}
608
609
[0dd3a2f]610// Local Variables: //
611// tab-width: 4 //
612// mode: c++ //
613// compile-command: "make install" //
614// End: //
Note: See TracBrowser for help on using the repository browser.