source: src/SynTree/Expression.cc @ 546e712

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 546e712 was 546e712, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fix for 1 bug of N

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