source: src/SynTree/Expression.cc @ f673c13c

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

WithStmt? is now a Declaration

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