source: src/SynTree/Expression.cc @ e6cfa8ff

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e6cfa8ff was 312029a, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

move enum Aggregate from DeclarationNode? to AggregateDecl?, add control-keyword field-dereference to replace control-keyword cast

  • Property mode set to 100644
File size: 26.2 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 : Wed Dec 11 07:55:15 2019
13// Update Count     : 70
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#include <set>                       // for set
22
23#include "Common/utility.h"          // for maybeClone, cloneAll, deleteAll
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                        assert(i->second.declptr);
41                        i->second.declptr->printShort( os, indent+1 );
42                        os << std::endl;
43                        printInferParams( i->second.expr->inferParams, os, indent+1, level+1 );
44                } // for
45        } // if
46}
47
48Expression::Expression() : result( 0 ), env( 0 ) {}
49
50Expression::Expression( const Expression & other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), extension( other.extension ), inferParams( other.inferParams ), resnSlots( other.resnSlots ) {}
51
52void Expression::spliceInferParams( Expression * other ) {
53        if ( ! other ) return;
54        for ( auto p : other->inferParams ) {
55                inferParams[p.first] = std::move( p.second );
56        }
57        resnSlots.insert( resnSlots.end(), other->resnSlots.begin(), other->resnSlots.end() );
58}
59
60Expression::~Expression() {
61        delete env;
62        delete result;
63}
64
65bool Expression::get_lvalue() const {
66        return false;
67}
68
69void Expression::print( std::ostream & os, Indenter indent ) const {
70        printInferParams( inferParams, os, indent+1, 0 );
71
72        if ( env ) {
73                os << std::endl << indent << "... with environment:" << std::endl;
74                env->print( os, indent+1 );
75        } // if
76
77        if ( extension ) {
78                os << std::endl << indent << "... with extension:";
79        } // if
80}
81
82ConstantExpr::ConstantExpr( Constant _c ) : Expression(), constant( _c ) {
83        set_result( constant.get_type()->clone() );
84}
85
86ConstantExpr::ConstantExpr( const ConstantExpr & other) : Expression( other ), constant( other.constant ) {
87}
88
89ConstantExpr::~ConstantExpr() {}
90
91void ConstantExpr::print( std::ostream & os, Indenter indent ) const {
92        os << "constant expression " ;
93        constant.print( os );
94        Expression::print( os, indent );
95}
96
97long long int ConstantExpr::intValue() const {
98        if ( BasicType * basicType = dynamic_cast< BasicType * >( result ) ) {
99                if ( basicType->isInteger() ) {
100                        return get_constant()->get_ival();
101                }
102        } else if ( dynamic_cast< OneType * >( result ) ) {
103                return 1;
104        } else if ( dynamic_cast< ZeroType * >( result ) ) {
105                return 0;
106        }
107        SemanticError( this, "Constant expression of non-integral type " );
108}
109
110VariableExpr::VariableExpr() : Expression(), var( nullptr ) {}
111
112VariableExpr::VariableExpr( DeclarationWithType *_var ) : Expression(), var( _var ) {
113        assert( var );
114        assert( var->get_type() );
115        Type * type = var->get_type()->clone();
116
117        // xxx - doesn't quite work yet - get different alternatives with the same cost
118
119        // // enumerators are not lvalues
120        // if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( var->get_type() ) ) {
121        //      assert( inst->baseEnum );
122        //      EnumDecl * decl = inst->baseEnum;
123        //      long long int value;
124        //      if ( decl->valueOf( var, value ) ) {
125        //              type->set_lvalue( false ); // Would have to move to get_lvalue.
126        //      }
127        // }
128
129        set_result( type );
130}
131
132VariableExpr::VariableExpr( const VariableExpr & other ) : Expression( other ), var( other.var ) {
133}
134
135VariableExpr::~VariableExpr() {
136        // don't delete the declaration, since it points somewhere else in the tree
137}
138
139bool VariableExpr::get_lvalue() const {
140        // It isn't always an lvalue, but it is never an rvalue.
141        return true;
142}
143
144VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) {
145        VariableExpr * funcExpr = new VariableExpr( func );
146        funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) );
147        return funcExpr;
148}
149
150void VariableExpr::print( std::ostream & os, Indenter indent ) const {
151        os << "Variable Expression: ";
152        var->printShort(os, indent);
153        Expression::print( os, indent );
154}
155
156SizeofExpr::SizeofExpr( Expression * expr_ ) :
157                Expression(), expr(expr_), type(0), isType(false) {
158        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
159}
160
161SizeofExpr::SizeofExpr( Type * type_ ) :
162                Expression(), expr(0), type(type_), isType(true) {
163        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
164}
165
166SizeofExpr::SizeofExpr( const SizeofExpr & other ) :
167        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
168}
169
170SizeofExpr::~SizeofExpr() {
171        delete expr;
172        delete type;
173}
174
175void SizeofExpr::print( std::ostream & os, Indenter indent) const {
176        os << "Sizeof Expression on: ";
177        if (isType) type->print(os, indent+1);
178        else expr->print(os, indent+1);
179        Expression::print( os, indent );
180}
181
182AlignofExpr::AlignofExpr( Expression * expr_ ) :
183                Expression(), expr(expr_), type(0), isType(false) {
184        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
185}
186
187AlignofExpr::AlignofExpr( Type * type_ ) :
188                Expression(), expr(0), type(type_), isType(true) {
189        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
190}
191
192AlignofExpr::AlignofExpr( const AlignofExpr & other ) :
193        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
194}
195
196AlignofExpr::~AlignofExpr() {
197        delete expr;
198        delete type;
199}
200
201void AlignofExpr::print( std::ostream & os, Indenter indent) const {
202        os << "Alignof Expression on: ";
203        if (isType) type->print(os, indent+1);
204        else expr->print(os, indent+1);
205        Expression::print( os, indent );
206}
207
208UntypedOffsetofExpr::UntypedOffsetofExpr( Type * type, const std::string & member ) :
209                Expression(), type(type), member(member) {
210        assert( type );
211        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
212}
213
214UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr & other ) :
215        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
216
217UntypedOffsetofExpr::~UntypedOffsetofExpr() {
218        delete type;
219}
220
221void UntypedOffsetofExpr::print( std::ostream & os, Indenter indent) const {
222        os << "Untyped Offsetof Expression on member " << member << " of ";
223        type->print(os, indent+1);
224        Expression::print( os, indent );
225}
226
227OffsetofExpr::OffsetofExpr( Type * type, DeclarationWithType * member ) :
228                Expression(), type(type), member(member) {
229        assert( member );
230        assert( type );
231        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
232}
233
234OffsetofExpr::OffsetofExpr( const OffsetofExpr & other ) :
235        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
236
237OffsetofExpr::~OffsetofExpr() {
238        delete type;
239}
240
241void OffsetofExpr::print( std::ostream & os, Indenter indent) const {
242        os << "Offsetof Expression on member " << member->name << " of ";
243        type->print(os, indent+1);
244        Expression::print( os, indent );
245}
246
247OffsetPackExpr::OffsetPackExpr( StructInstType * type ) : Expression(), type( type ) {
248        assert( type );
249        set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
250}
251
252OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr & other ) : Expression( other ), type( maybeClone( other.type ) ) {}
253
254OffsetPackExpr::~OffsetPackExpr() { delete type; }
255
256void OffsetPackExpr::print( std::ostream & os, Indenter indent ) const {
257        os << "Offset pack expression on ";
258        type->print(os, indent+1);
259        Expression::print( os, indent );
260}
261
262CastExpr::CastExpr( Expression * arg, Type * toType, bool isGenerated ) : arg(arg), isGenerated( isGenerated ) {
263        set_result(toType);
264}
265
266CastExpr::CastExpr( Expression * arg, bool isGenerated ) : arg(arg), isGenerated( isGenerated ) {
267        set_result( new VoidType( Type::Qualifiers() ) );
268}
269
270CastExpr::CastExpr( const CastExpr & other ) : Expression( other ), arg( maybeClone( other.arg ) ), isGenerated( other.isGenerated ) {
271}
272
273CastExpr::~CastExpr() {
274        delete arg;
275}
276
277bool CastExpr::get_lvalue() const {
278        // This is actually wrong by C, but it works with our current set-up.
279        return arg->get_lvalue();
280}
281
282void CastExpr::print( std::ostream & os, Indenter indent ) const {
283        os << (isGenerated ? "Generated " : "Explicit ") << "Cast of:" << std::endl << indent+1;
284        arg->print(os, indent+1);
285        os << std::endl << indent << "... to:";
286        if ( result->isVoid() ) {
287                os << " nothing";
288        } else {
289                os << std::endl << indent+1;
290                result->print( os, indent+1 );
291        } // if
292        Expression::print( os, indent );
293}
294
295KeywordCastExpr::KeywordCastExpr( Expression * arg, AggregateDecl::Aggregate target ) : Expression(), arg(arg), target( target ) {
296}
297
298KeywordCastExpr::KeywordCastExpr( const KeywordCastExpr & other ) : Expression( other ), arg( maybeClone( other.arg ) ), target( other.target ) {
299}
300
301KeywordCastExpr::~KeywordCastExpr() {
302        delete arg;
303}
304
305const char * KeywordCastExpr::targetString() const {
306        return AggregateDecl::aggrString( target );
307}
308
309void KeywordCastExpr::print( std::ostream & os, Indenter indent ) const {
310        os << "Keyword Cast of:" << std::endl << indent+1;
311        arg->print(os, indent+1);
312        os << std::endl << indent << "... to: ";
313        os << targetString();
314        Expression::print( os, indent );
315}
316
317VirtualCastExpr::VirtualCastExpr( Expression * arg_, Type * toType ) : Expression(), arg(arg_) {
318        set_result(toType);
319}
320
321VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr & other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
322}
323
324VirtualCastExpr::~VirtualCastExpr() {
325        delete arg;
326}
327
328void VirtualCastExpr::print( std::ostream & os, Indenter indent ) const {
329        os << "Virtual Cast of:" << std::endl << indent+1;
330        arg->print(os, indent+1);
331        os << std::endl << indent << "... to:";
332        if ( ! result ) {
333                os << " unknown";
334        } else {
335                os << std::endl << indent+1;
336                result->print( os, indent+1 );
337        } // if
338        Expression::print( os, indent );
339}
340
341UntypedMemberExpr::UntypedMemberExpr( Expression * member, Expression * aggregate ) :
342                Expression(), member(member), aggregate(aggregate) {
343        assert( aggregate );
344}
345
346UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr & other ) :
347                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
348}
349
350UntypedMemberExpr::~UntypedMemberExpr() {
351        delete aggregate;
352        delete member;
353}
354
355bool UntypedMemberExpr::get_lvalue() const {
356        return aggregate->get_lvalue();
357}
358
359void UntypedMemberExpr::print( std::ostream & os, Indenter indent ) const {
360        os << "Untyped Member Expression, with field: " << std::endl << indent+1;
361        member->print(os, indent+1 );
362        os << indent << "... from aggregate:" << std::endl << indent+1;
363        aggregate->print(os, indent+1);
364        Expression::print( os, indent );
365}
366
367MemberExpr::MemberExpr( DeclarationWithType * member, Expression * aggregate ) :
368                Expression(), member(member), aggregate(aggregate) {
369        assert( member );
370        assert( aggregate );
371        assert( aggregate->result );
372
373        TypeSubstitution sub = aggregate->result->genericSubstitution();
374        Type * res = member->get_type()->clone();
375        sub.apply( res );
376        result = res;
377        result->get_qualifiers() |= aggregate->result->get_qualifiers();
378}
379
380MemberExpr::MemberExpr( const MemberExpr & other ) :
381                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
382}
383
384MemberExpr::~MemberExpr() {
385        // don't delete the member declaration, since it points somewhere else in the tree
386        delete aggregate;
387}
388
389bool MemberExpr::get_lvalue() const {
390        // This is actually wrong by C, but it works with our current set-up.
391        return true;
392}
393
394void MemberExpr::print( std::ostream & os, Indenter indent ) const {
395        os << "Member Expression, with field:" << std::endl;
396        os << indent+1;
397        member->print( os, indent+1 );
398        os << std::endl << indent << "... from aggregate:" << std::endl << indent+1;
399        aggregate->print(os, indent + 1);
400        Expression::print( os, indent );
401}
402
403UntypedExpr::UntypedExpr( Expression * function, const std::list<Expression *> & args ) :
404                Expression(), function(function), args(args) {}
405
406UntypedExpr::UntypedExpr( const UntypedExpr & other ) :
407                Expression( other ), function( maybeClone( other.function ) ) {
408        cloneAll( other.args, args );
409}
410
411UntypedExpr::~UntypedExpr() {
412        delete function;
413        deleteAll( args );
414}
415
416UntypedExpr * UntypedExpr::createDeref( Expression * expr ) {
417        UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } );
418        if ( Type * type = expr->get_result() ) {
419                Type * base = InitTweak::getPointerBase( type );
420                assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() );
421                ret->set_result( base->clone() );
422                if ( GenPoly::referencesPermissable() ) {
423                        // if references are still allowed in the AST, dereference returns a reference
424                        ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) );
425                }
426        }
427        return ret;
428}
429
430UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) {
431        assert( arg1 && arg2 );
432        UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } );
433        if ( arg1->get_result() && arg2->get_result() ) {
434                // if both expressions are typed, assumes that this assignment is a C bitwise assignment,
435                // so the result is the type of the RHS
436                ret->set_result( arg2->get_result()->clone() );
437        }
438        return ret;
439}
440
441bool UntypedExpr::get_lvalue() const {
442        // from src/GenPoly/Lvalue.cc: isIntrinsicReference
443        static std::set<std::string> lvalueFunctions = { "*?", "?[?]" };
444        std::string fname = InitTweak::getFunctionName( const_cast< UntypedExpr * >( this ) );
445        return lvalueFunctions.count(fname);
446}
447
448void UntypedExpr::print( std::ostream & os, Indenter indent ) const {
449        os << "Applying untyped:" << std::endl;
450        os << indent+1;
451        function->print(os, indent+1);
452        os << std::endl << indent << "...to:" << std::endl;
453        printAll(args, os, indent+1);
454        Expression::print( os, indent );
455}
456
457NameExpr::NameExpr( std::string name ) : Expression(), name(name) {
458        assertf(name != "0", "Zero is not a valid name");
459        assertf(name != "1", "One is not a valid name");
460}
461
462NameExpr::NameExpr( const NameExpr & other ) : Expression( other ), name( other.name ) {
463}
464
465NameExpr::~NameExpr() {}
466
467void NameExpr::print( std::ostream & os, Indenter indent ) const {
468        os << "Name: " << get_name();
469        Expression::print( os, indent );
470}
471
472LogicalExpr::LogicalExpr( Expression * arg1_, Expression * arg2_, bool andp ) :
473                Expression(), arg1(arg1_), arg2(arg2_), isAnd(andp) {
474        set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
475}
476
477LogicalExpr::LogicalExpr( const LogicalExpr & other ) :
478                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
479}
480
481LogicalExpr::~LogicalExpr() {
482        delete arg1;
483        delete arg2;
484}
485
486void LogicalExpr::print( std::ostream & os, Indenter indent )const {
487        os << "Short-circuited operation (" << (isAnd ? "and" : "or") << ") on: ";
488        arg1->print(os);
489        os << " and ";
490        arg2->print(os);
491        Expression::print( os, indent );
492}
493
494ConditionalExpr::ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 ) :
495                Expression(), arg1(arg1), arg2(arg2), arg3(arg3) {}
496
497ConditionalExpr::ConditionalExpr( const ConditionalExpr & other ) :
498                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
499}
500
501ConditionalExpr::~ConditionalExpr() {
502        delete arg1;
503        delete arg2;
504        delete arg3;
505}
506
507bool ConditionalExpr::get_lvalue() const {
508        return false;
509}
510
511void ConditionalExpr::print( std::ostream & os, Indenter indent ) const {
512        os << "Conditional expression on: " << std::endl << indent+1;
513        arg1->print( os, indent+1 );
514        os << indent << "First alternative:" << std::endl << indent+1;
515        arg2->print( os, indent+1 );
516        os << indent << "Second alternative:" << std::endl << indent+1;
517        arg3->print( os, indent+1 );
518        Expression::print( os, indent );
519}
520
521AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( other.inout ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
522
523
524void AsmExpr::print( std::ostream & os, Indenter indent ) const {
525        os << "Asm Expression: " << std::endl;
526        if ( !inout.empty() ) os <<  "[" << inout << "] ";
527        if ( constraint ) constraint->print( os, indent+1 );
528        if ( operand ) operand->print( os, indent+1 );
529}
530
531
532ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
533        assert( callExpr );
534        assert( callExpr->result );
535        set_result( callExpr->result->clone() );
536}
537
538ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
539}
540
541ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
542        set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment
543        delete callExpr;
544}
545
546void ImplicitCopyCtorExpr::print( std::ostream & os, Indenter indent ) const {
547        os <<  "Implicit Copy Constructor Expression: " << std::endl << indent+1;
548        callExpr->print( os, indent+1 );
549}
550
551
552ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) {
553        // allow resolver to type a constructor used as an expression as if it has the same type as its first argument
554        assert( callExpr );
555        Expression * arg = InitTweak::getCallArg( callExpr, 0 );
556        assert( arg );
557        set_result( maybeClone( arg->result ) );
558}
559
560ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
561}
562
563ConstructorExpr::~ConstructorExpr() {
564        delete callExpr;
565}
566
567bool ConstructorExpr::get_lvalue() const {
568        return false;
569}
570
571void ConstructorExpr::print( std::ostream & os, Indenter indent ) const {
572        os <<  "Constructor Expression: " << std::endl << indent+1;
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
589bool CompoundLiteralExpr::get_lvalue() const {
590        return true;
591}
592
593void CompoundLiteralExpr::print( std::ostream & os, Indenter indent ) const {
594        os << "Compound Literal Expression: " << std::endl << indent+1;
595        result->print( os, indent+1 );
596        os << indent+1;
597        initializer->print( os, indent+1 );
598        Expression::print( os, indent );
599}
600
601RangeExpr::RangeExpr( Expression * low, Expression * high ) : low( low ), high( high ) {}
602RangeExpr::RangeExpr( const RangeExpr & other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
603void RangeExpr::print( std::ostream & os, Indenter indent ) const {
604        os << "Range Expression: ";
605        low->print( os, indent );
606        os << " ... ";
607        high->print( os, indent );
608        Expression::print( os, indent );
609}
610
611StmtExpr::StmtExpr( CompoundStmt * statements ) : statements( statements ) {
612        computeResult();
613}
614StmtExpr::StmtExpr( const StmtExpr & other ) : Expression( other ), statements( other.statements->clone() ), resultExpr( other.resultExpr ) {
615        cloneAll( other.returnDecls, returnDecls );
616        cloneAll( other.dtors, dtors );
617}
618StmtExpr::~StmtExpr() {
619        delete statements;
620        deleteAll( dtors );
621        deleteAll( returnDecls );
622}
623void StmtExpr::computeResult() {
624        assert( statements );
625        std::list< Statement * > & body = statements->kids;
626        delete result;
627        result = nullptr;
628        if ( ! returnDecls.empty() ) {
629                // prioritize return decl for result type, since if a return decl exists, then
630                // the StmtExpr is currently in an intermediate state where the body will always
631                // give a void result type.
632                result = returnDecls.front()->get_type()->clone();
633        } else if ( ! body.empty() ) {
634                if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
635                        result = maybeClone( exprStmt->expr->result );
636                }
637        }
638        // ensure that StmtExpr has a result type
639        if ( ! result ) {
640                result = new VoidType( Type::Qualifiers() );
641        }
642}
643bool StmtExpr::get_lvalue() const {
644        return false;
645}
646void StmtExpr::print( std::ostream & os, Indenter indent ) const {
647        os << "Statement Expression: " << std::endl << indent+1;
648        statements->print( os, indent+1 );
649        if ( ! returnDecls.empty() ) {
650                os << indent+1 << "... with returnDecls: ";
651                printAll( returnDecls, os, indent+1 );
652        }
653        if ( ! dtors.empty() ) {
654                os << indent+1 << "... with dtors: ";
655                printAll( dtors, os, indent+1 );
656        }
657        Expression::print( os, indent );
658}
659
660
661long long UniqueExpr::count = 0;
662UniqueExpr::UniqueExpr( Expression * expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) {
663        assert( expr );
664        assert( count != -1 );
665        if ( id == -1 ) id = count++;
666        if ( expr->get_result() ) {
667                set_result( expr->get_result()->clone() );
668        }
669}
670UniqueExpr::UniqueExpr( const UniqueExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) {
671}
672UniqueExpr::~UniqueExpr() {
673        delete expr;
674        delete object;
675        delete var;
676}
677void UniqueExpr::print( std::ostream & os, Indenter indent ) const {
678        os << "Unique Expression with id:" << id << std::endl << indent+1;
679        expr->print( os, indent+1 );
680        if ( object ) {
681                os << indent << "... with decl: ";
682                get_object()->printShort( os, indent+1 );
683        }
684        Expression::print( os, indent );
685}
686
687InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {}
688InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {}
689InitAlternative::~InitAlternative() {
690        delete type;
691        delete designation;
692}
693
694UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {}
695UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {}
696UntypedInitExpr::~UntypedInitExpr() {
697        delete expr;
698}
699
700void UntypedInitExpr::print( std::ostream & os, Indenter indent ) const {
701        os << "Untyped Init Expression" << std::endl << indent+1;
702        expr->print( os, indent+1 );
703        if ( ! initAlts.empty() ) {
704                for ( const InitAlternative & alt : initAlts ) {
705                        os << indent+1 <<  "InitAlternative: ";
706                        alt.type->print( os, indent+1 );
707                        alt.designation->print( os, indent+1 );
708                }
709        }
710}
711
712InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) {
713        set_result( expr->get_result()->clone() );
714}
715InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
716InitExpr::~InitExpr() {
717        delete expr;
718        delete designation;
719}
720
721void InitExpr::print( std::ostream & os, Indenter indent ) const {
722        os << "Init Expression" << std::endl << indent+1;
723        expr->print( os, indent+1 );
724        os << indent+1 << "... with designation: ";
725        designation->print( os, indent+1 );
726}
727
728DeletedExpr::DeletedExpr( Expression * expr, Declaration * deleteStmt ) : expr( expr ), deleteStmt( deleteStmt ) {
729        assert( expr->result );
730        result = expr->result->clone();
731}
732DeletedExpr::DeletedExpr( const DeletedExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), deleteStmt( other.deleteStmt ) {}
733DeletedExpr::~DeletedExpr() {
734        delete expr;
735}
736
737void DeletedExpr::print( std::ostream & os, Indenter indent ) const {
738        os << "Deleted Expression" << std::endl << indent+1;
739        expr->print( os, indent+1 );
740        os << std::endl << indent+1 << "... deleted by: ";
741        deleteStmt->print( os, indent+1 );
742}
743
744
745DefaultArgExpr::DefaultArgExpr( Expression * expr ) : expr( expr ) {
746        assert( expr->result );
747        result = expr->result->clone();
748}
749DefaultArgExpr::DefaultArgExpr( const DefaultArgExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ) {}
750DefaultArgExpr::~DefaultArgExpr() {
751        delete expr;
752}
753
754void DefaultArgExpr::print( std::ostream & os, Indenter indent ) const {
755        os << "Default Argument Expression" << std::endl << indent+1;
756        expr->print( os, indent+1 );
757}
758
759GenericExpr::Association::Association( Type * type, Expression * expr ) : type( type ), expr( expr ), isDefault( false ) {}
760GenericExpr::Association::Association( Expression * expr ) : type( nullptr ), expr( expr ), isDefault( true ) {}
761GenericExpr::Association::Association( const Association & other ) : type( maybeClone( other.type ) ), expr( maybeClone( other.expr ) ), isDefault( other.isDefault ) {}
762GenericExpr::Association::~Association() {
763        delete type;
764        delete expr;
765}
766
767GenericExpr::GenericExpr( Expression * control, const std::list<Association> & assoc ) : Expression(), control( control ), associations( assoc ) {}
768GenericExpr::GenericExpr( const GenericExpr & other ) : Expression(other), control( maybeClone( other.control ) ), associations( other.associations ) {
769}
770GenericExpr::~GenericExpr() {
771        delete control;
772}
773
774void GenericExpr::print( std::ostream & os, Indenter indent ) const {
775        os << "C11 _Generic Expression" << std::endl << indent+1;
776        control->print( os, indent+1 );
777        os << std::endl << indent+1 << "... with associations: " << std::endl;
778        for ( const Association & assoc : associations ) {
779                os << indent+1;
780                if (assoc.isDefault) {
781                        os << "... default: ";
782                        assoc.expr->print( os, indent+1 );
783                } else {
784                        os << "... type: ";
785                        assoc.type->print( os, indent+1 );
786                        os << std::endl << indent+1 << "... expression: ";
787                        assoc.expr->print( os, indent+1 );
788                        os << std::endl;
789                }
790                os << std::endl;
791        }
792}
793
794// Local Variables: //
795// tab-width: 4 //
796// mode: c++ //
797// compile-command: "make install" //
798// End: //
Note: See TracBrowser for help on using the repository browser.