source: src/SynTree/Expression.cc @ 0992849

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 0992849 was 0992849, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Assign a result type to StmtExpr? based on returnDecl in FixInit?

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