source: src/SynTree/Expression.cc @ 8a6cf7e

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

Fix various reference features.

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