source: src/SynTree/Expression.cc @ b834e98

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since b834e98 was ddb80bd, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add intValue helper function to ConstantExpr?

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