source: src/SynTree/Expression.cc @ 6a9d4b4

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

Merge branch 'master' into cleanup-dtors

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