source: src/SynTree/Expression.cc @ 0f19f5e5

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0f19f5e5 was 2d80111, checked in by Andrew Beach <ajbeach@…>, 5 years ago

Lvalue is checked through Expression::get_lvalue. Only three other places use Type::get_lvalue directly now: CodeGen/GenType?, ResolvExpr/ConversionCost? & SynTree/TopLvalue?.

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