source: src/SynTree/Expression.cc @ ff29f08

new-envwith_gc
Last change on this file since ff29f08 was 42107b4, checked in by Aaron Moss <a3moss@…>, 6 years ago

Leftover cleanup from merge

  • Property mode set to 100644
File size: 22.8 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
[c0bf94e]240CastExpr::CastExpr( Expression *arg, Type *toType, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) {
[906e24d]241        set_result(toType);
[51b7345]242}
243
[c0bf94e]244CastExpr::CastExpr( Expression *arg, bool isGenerated ) : Expression(), arg(arg), isGenerated( isGenerated ) {
[906e24d]245        set_result( new VoidType( Type::Qualifiers() ) );
[51b7345]246}
247
[c0bf94e]248CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), isGenerated( other.isGenerated ) {
[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
[9a705dc8]264KeywordCastExpr::KeywordCastExpr( Expression *arg, Target target ) : Expression(), arg(arg), target( target ) {
265}
266
267KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) {
268}
269
270const std::string & KeywordCastExpr::targetString() const {
271        static const std::string targetStrs[] = {
272                "coroutine", "thread", "monitor"
273        };
274        static_assert(
275                (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS),
276                "Each KeywordCastExpr::Target should have a corresponding string representation"
277        );
278        return targetStrs[(unsigned long)target];
279}
280
281void KeywordCastExpr::print( std::ostream &os, Indenter indent ) const {
282        os << "Keyword Cast of:" << std::endl << indent+1;
283        arg->print(os, indent+1);
284        os << std::endl << indent << "... to: ";
285        os << targetString();
286        Expression::print( os, indent );
[0dd3a2f]287}
288
[a5f0529]289VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
290        set_result(toType);
291}
292
293VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
294}
295
[50377a4]296void VirtualCastExpr::print( std::ostream &os, Indenter indent ) const {
297        os << "Virtual Cast of:" << std::endl << indent+1;
298        arg->print(os, indent+1);
299        os << std::endl << indent << "... to:";
[a5f0529]300        if ( ! result ) {
[50377a4]301                os << " unknown";
[a5f0529]302        } else {
[50377a4]303                os << std::endl << indent+1;
304                result->print( os, indent+1 );
[a5f0529]305        } // if
306        Expression::print( os, indent );
307}
308
[bf4b4cf]309UntypedMemberExpr::UntypedMemberExpr( Expression * member, Expression *aggregate ) :
310                Expression(), member(member), aggregate(aggregate) {
[50377a4]311        assert( aggregate );
312}
[51b7345]313
[0dd3a2f]314UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
[3b58d91]315                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
[51b7345]316}
317
[50377a4]318void UntypedMemberExpr::print( std::ostream &os, Indenter indent ) const {
319        os << "Untyped Member Expression, with field: " << std::endl << indent+1;
320        member->print(os, indent+1 );
321        os << indent << "... from aggregate: " << std::endl << indent+1;
322        aggregate->print(os, indent+1);
[0dd3a2f]323        Expression::print( os, indent );
[51b7345]324}
325
[bf4b4cf]326MemberExpr::MemberExpr( DeclarationWithType *member, Expression *aggregate ) :
327                Expression(), member(member), aggregate(aggregate) {
[50377a4]328        assert( member );
329        assert( aggregate );
[9bfc9da]330        assert( aggregate->result );
[d9fa60a]331
[9bfc9da]332        TypeSubstitution sub = aggregate->result->genericSubstitution();
[d9fa60a]333        Type * res = member->get_type()->clone();
334        sub.apply( res );
[487845d7]335        result = res;
336        result->set_lvalue( true );
337        result->get_qualifiers() |= aggregate->result->get_qualifiers();
[51b7345]338}
339
[0dd3a2f]340MemberExpr::MemberExpr( const MemberExpr &other ) :
[39f84a4]341                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
[51b7345]342}
343
[50377a4]344void MemberExpr::print( std::ostream &os, Indenter indent ) const {
[89231bc]345        os << "Member Expression, with field: " << std::endl;
[50377a4]346        os << indent+1;
347        member->print( os, indent+1 );
348        os << std::endl << indent << "... from aggregate: " << std::endl << indent+1;
349        aggregate->print(os, indent + 1);
[0dd3a2f]350        Expression::print( os, indent );
[51b7345]351}
352
[bf4b4cf]353UntypedExpr::UntypedExpr( Expression *function, const std::list<Expression *> &args ) :
354                Expression(), function(function), args(args) {}
[51b7345]355
[0dd3a2f]356UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
357                Expression( other ), function( maybeClone( other.function ) ) {
358        cloneAll( other.args, args );
[51b7345]359}
360
[b3b2077]361UntypedExpr * UntypedExpr::createDeref( Expression * expr ) {
362        UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } );
363        if ( Type * type = expr->get_result() ) {
364                Type * base = InitTweak::getPointerBase( type );
[0698aa1]365                assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() );
[b0440b7]366                ret->set_result( base->clone() );
367                if ( GenPoly::referencesPermissable() ) {
368                        // if references are still allowed in the AST, dereference returns a reference
369                        ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) );
370                } else {
371                        // references have been removed, in which case dereference returns an lvalue of the base type.
[0690350]372                        ret->result->set_lvalue( true );
[b0440b7]373                }
[b3b2077]374        }
375        return ret;
376}
377
378UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) {
379        assert( arg1 && arg2 );
380        UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } );
381        if ( arg1->get_result() && arg2->get_result() ) {
382                // if both expressions are typed, assumes that this assignment is a C bitwise assignment,
383                // so the result is the type of the RHS
384                ret->set_result( arg2->get_result()->clone() );
385        }
386        return ret;
387}
388
389
[50377a4]390void UntypedExpr::print( std::ostream &os, Indenter indent ) const {
[89231bc]391        os << "Applying untyped: " << std::endl;
[50377a4]392        os << indent+1;
393        function->print(os, indent+1);
394        os << std::endl << indent << "...to: " << std::endl;
395        printAll(args, os, indent+1);
[0dd3a2f]396        Expression::print( os, indent );
[51b7345]397}
398
[bf4b4cf]399NameExpr::NameExpr( std::string name ) : Expression(), name(name) {
[50377a4]400        assertf(name != "0", "Zero is not a valid name");
401        assertf(name != "1", "One is not a valid name");
[4cb935e]402}
[51b7345]403
[0dd3a2f]404NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
[51b7345]405}
406
[50377a4]407void NameExpr::print( std::ostream &os, Indenter indent ) const {
408        os << "Name: " << get_name();
[0dd3a2f]409        Expression::print( os, indent );
[51b7345]410}
411
[bf4b4cf]412LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp ) :
413                Expression(), arg1(arg1_), arg2(arg2_), isAnd(andp) {
[906e24d]414        set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[51b7345]415}
416
[0dd3a2f]417LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
418                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
[51b7345]419}
420
[50377a4]421void LogicalExpr::print( std::ostream &os, Indenter indent )const {
422        os << "Short-circuited operation (" << (isAnd ? "and" : "or") << ") on: ";
[0dd3a2f]423        arg1->print(os);
424        os << " and ";
425        arg2->print(os);
426        Expression::print( os, indent );
[51b7345]427}
428
[bf4b4cf]429ConditionalExpr::ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 ) :
430                Expression(), arg1(arg1), arg2(arg2), arg3(arg3) {}
[51b7345]431
[0dd3a2f]432ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
433                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
434}
[51b7345]435
[50377a4]436void ConditionalExpr::print( std::ostream &os, Indenter indent ) const {
437        os << "Conditional expression on: " << std::endl << indent+1;
438        arg1->print( os, indent+1 );
439        os << indent << "First alternative:" << std::endl << indent+1;
440        arg2->print( os, indent+1 );
441        os << indent << "Second alternative:" << std::endl << indent+1;
442        arg3->print( os, indent+1 );
[0dd3a2f]443        Expression::print( os, indent );
444}
445
[e6cee92]446AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
[3be261a]447
448
[50377a4]449void AsmExpr::print( std::ostream &os, Indenter indent ) const {
[7f5566b]450        os << "Asm Expression: " << std::endl;
[50377a4]451        if ( inout ) inout->print( os, indent+1 );
452        if ( constraint ) constraint->print( os, indent+1 );
453        if ( operand ) operand->print( os, indent+1 );
[7f5566b]454}
455
[db4ecc5]456
457ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
458        assert( callExpr );
[50377a4]459        assert( callExpr->result );
[906e24d]460        set_result( callExpr->get_result()->clone() );
[db4ecc5]461}
462
463ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
464        cloneAll( other.tempDecls, tempDecls );
465        cloneAll( other.returnDecls, returnDecls );
466        cloneAll( other.dtors, dtors );
467}
468
469ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
[d5556a3]470        set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment
[db4ecc5]471}
472
[50377a4]473void ImplicitCopyCtorExpr::print( std::ostream &os, Indenter indent ) const {
474        os <<  "Implicit Copy Constructor Expression: " << std::endl << indent+1;
475        callExpr->print( os, indent+1 );
476        os << std::endl << indent << "... with temporaries:" << std::endl;
477        printAll( tempDecls, os, indent+1 );
478        os << std::endl << indent << "... with return temporaries:" << std::endl;
479        printAll( returnDecls, os, indent+1 );
[db4ecc5]480        Expression::print( os, indent );
481}
482
[3be261a]483
[b6fe7e6]484ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) {
485        // allow resolver to type a constructor used as an expression as if it has the same type as its first argument
486        assert( callExpr );
487        Expression * arg = InitTweak::getCallArg( callExpr, 0 );
488        assert( arg );
[50377a4]489        set_result( maybeClone( arg->result ) );
[b6fe7e6]490}
[3be261a]491
[b6fe7e6]492ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
493}
494
[50377a4]495void ConstructorExpr::print( std::ostream &os, Indenter indent ) const {
496        os <<  "Constructor Expression: " << std::endl << indent+1;
[b6fe7e6]497        callExpr->print( os, indent + 2 );
498        Expression::print( os, indent );
[0dd3a2f]499}
500
[3be261a]501
[fbcde64]502CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) {
[3c13c03]503        assert( type && initializer );
[5ccb10d]504        type->set_lvalue( true );
[fbcde64]505        set_result( type );
[630a82a]506}
507
[fbcde64]508CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {}
[630a82a]509
[50377a4]510void CompoundLiteralExpr::print( std::ostream &os, Indenter indent ) const {
511        os << "Compound Literal Expression: " << std::endl << indent+1;
512        result->print( os, indent+1 );
513        os << indent+1;
514        initializer->print( os, indent+1 );
[d5556a3]515        Expression::print( os, indent );
[630a82a]516}
517
[d9e2280]518RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
[3c13c03]519RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
[50377a4]520void RangeExpr::print( std::ostream &os, Indenter indent ) const {
[3c13c03]521        os << "Range Expression: ";
[8688ce1]522        low->print( os, indent );
523        os << " ... ";
524        high->print( os, indent );
[d5556a3]525        Expression::print( os, indent );
[8688ce1]526}
[3be261a]527
[6eb8948]528StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) {
[5e2c348]529        computeResult();
530}
531StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {
532        cloneAll( other.returnDecls, returnDecls );
533        cloneAll( other.dtors, dtors );
534}
535void StmtExpr::computeResult() {
[6eb8948]536        assert( statements );
[5e2c348]537        std::list< Statement * > & body = statements->kids;
538        result = nullptr;
[0992849]539        if ( ! returnDecls.empty() ) {
540                // prioritize return decl for result type, since if a return decl exists, then
541                // the StmtExpr is currently in an intermediate state where the body will always
542                // give a void result type.
543                result = returnDecls.front()->get_type()->clone();
544        } else if ( ! body.empty() ) {
[6eb8948]545                if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
[9fe33947]546                        result = maybeClone( exprStmt->expr->result );
[6eb8948]547                }
548        }
[07516b56]549        // ensure that StmtExpr has a result type
550        if ( ! result ) {
[9fe33947]551                result = new VoidType( Type::Qualifiers() );
[07516b56]552        }
[6eb8948]553}
[50377a4]554void StmtExpr::print( std::ostream &os, Indenter indent ) const {
555        os << "Statement Expression: " << std::endl << indent+1;
556        statements->print( os, indent+1 );
[d5556a3]557        if ( ! returnDecls.empty() ) {
[50377a4]558                os << indent+1 << "... with returnDecls: ";
559                printAll( returnDecls, os, indent+1 );
[d5556a3]560        }
561        if ( ! dtors.empty() ) {
[50377a4]562                os << indent+1 << "... with dtors: ";
563                printAll( dtors, os, indent+1 );
[d5556a3]564        }
565        Expression::print( os, indent );
[6eb8948]566}
567
[3c13c03]568
[bf32bb8]569long long UniqueExpr::count = 0;
[141b786]570UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) {
571        assert( expr );
[bf32bb8]572        assert( count != -1 );
573        if ( id == -1 ) id = count++;
574        if ( expr->get_result() ) {
575                set_result( expr->get_result()->clone() );
576        }
[3c13c03]577}
[141b786]578UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) {
[3c13c03]579}
[68f9c43]580
[50377a4]581void UniqueExpr::print( std::ostream &os, Indenter indent ) const {
582        os << "Unique Expression with id:" << id << std::endl << indent+1;
583        expr->print( os, indent+1 );
584        if ( object ) {
585                os << indent << "... with decl: ";
586                get_object()->printShort( os, indent+1 );
[77971f6]587        }
[d5556a3]588        Expression::print( os, indent );
[3c13c03]589}
590
[e4d829b]591InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {}
592InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {}
593
594UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {}
595UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {}
596
[50377a4]597void UntypedInitExpr::print( std::ostream & os, Indenter indent ) const {
598        os << "Untyped Init Expression" << std::endl << indent+1;
599        expr->print( os, indent+1 );
[e4d829b]600        if ( ! initAlts.empty() ) {
601                for ( const InitAlternative & alt : initAlts ) {
[50377a4]602                        os << indent+1 <<  "InitAlternative: ";
603                        alt.type->print( os, indent+1 );
604                        alt.designation->print( os, indent+1 );
[e4d829b]605                }
606        }
607}
608
[62423350]609InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) {
610        set_result( expr->get_result()->clone() );
[e4d829b]611}
612InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
613
[50377a4]614void InitExpr::print( std::ostream & os, Indenter indent ) const {
615        os << "Init Expression" << std::endl << indent+1;
616        expr->print( os, indent+1 );
617        os << indent+1 << "... with designation: ";
618        designation->print( os, indent+1 );
[e4d829b]619}
620
[44b4114]621DeletedExpr::DeletedExpr( Expression * expr, BaseSyntaxNode * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) {
622        assert( expr->result );
623        result = expr->result->clone();
624}
625DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {}
626
627void DeletedExpr::print( std::ostream & os, Indenter indent ) const {
628        os << "Deleted Expression" << std::endl << indent+1;
629        expr->print( os, indent+1 );
630        os << std::endl << indent+1 << "... deleted by: ";
631        deleteStmt->print( os, indent+1 );
632}
633
634
[0dd3a2f]635// Local Variables: //
636// tab-width: 4 //
637// mode: c++ //
638// compile-command: "make install" //
639// End: //
Note: See TracBrowser for help on using the repository browser.