source: src/SynTree/Expression.cc @ 4ef08f7

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 4ef08f7 was 4ef08f7, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Implemented KeywordCast? in CandidateFinder? of new AST.

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