source: src/SynTree/Expression.cc @ bf32bb8

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since bf32bb8 was bf32bb8, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

implement transformation for MemberTupleExprs?

  • Property mode set to 100644
File size: 18.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 : Fri Aug  5 14:23:56 2016
13// Update Count     : 49
14//
15
16#include <iostream>
17#include <cassert>
18#include <list>
19#include <algorithm>
20
21#include <iterator>
22
23#include "Type.h"
24#include "Initializer.h"
25#include "Expression.h"
26#include "Declaration.h"
27#include "Statement.h"
28#include "TypeSubstitution.h"
29#include "Common/utility.h"
30#include "InitTweak/InitTweak.h"
31
32
33Expression::Expression( Expression *_aname ) : result( 0 ), env( 0 ), argName( _aname ) {}
34
35Expression::Expression( const Expression &other ) : result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ), extension( other.extension ) {
36}
37
38Expression::~Expression() {
39        delete env;
40        delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix
41        delete result;
42}
43
44void Expression::print( std::ostream &os, int indent ) const {
45        if ( env ) {
46                os << std::string( indent, ' ' ) << "with environment:" << std::endl;
47                env->print( os, indent+2 );
48        } // if
49
50        if ( argName ) {
51                os << std::string( indent, ' ' ) << "with designator:";
52                argName->print( os, indent+2 );
53        } // if
54
55        if ( extension ) {
56                os << std::string( indent, ' ' ) << "with extension:";
57        } // if
58}
59
60ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) {
61        set_result( constant.get_type()->clone() );
62}
63
64ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
65}
66
67ConstantExpr::~ConstantExpr() {}
68
69void ConstantExpr::print( std::ostream &os, int indent ) const {
70        os << "constant expression " ;
71        constant.print( os );
72        Expression::print( os, indent );
73}
74
75VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) {
76        assert( var );
77        assert( var->get_type() );
78        Type * type = var->get_type()->clone();
79        type->set_isLvalue( true );
80        set_result( type );
81}
82
83VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
84}
85
86VariableExpr::~VariableExpr() {
87        // don't delete the declaration, since it points somewhere else in the tree
88}
89
90void VariableExpr::print( std::ostream &os, int indent ) const {
91        os << "Variable Expression: ";
92
93        Declaration *decl = get_var();
94        // if ( decl != 0) decl->print(os, indent + 2);
95        if ( decl != 0) decl->printShort(os, indent + 2);
96        os << std::endl;
97        Expression::print( os, indent );
98}
99
100SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
101                Expression( _aname ), expr(expr_), type(0), isType(false) {
102        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
103}
104
105SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
106                Expression( _aname ), expr(0), type(type_), isType(true) {
107        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
108}
109
110SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
111        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
112}
113
114SizeofExpr::~SizeofExpr() {
115        delete expr;
116        delete type;
117}
118
119void SizeofExpr::print( std::ostream &os, int indent) const {
120        os << "Sizeof Expression on: ";
121
122        if (isType)
123                type->print(os, indent + 2);
124        else
125                expr->print(os, indent + 2);
126
127        os << std::endl;
128        Expression::print( os, indent );
129}
130
131AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
132                Expression( _aname ), expr(expr_), type(0), isType(false) {
133        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
134}
135
136AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
137                Expression( _aname ), expr(0), type(type_), isType(true) {
138        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
139}
140
141AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
142        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
143}
144
145AlignofExpr::~AlignofExpr() {
146        delete expr;
147        delete type;
148}
149
150void AlignofExpr::print( std::ostream &os, int indent) const {
151        os << std::string( indent, ' ' ) << "Alignof Expression on: ";
152
153        if (isType)
154                type->print(os, indent + 2);
155        else
156                expr->print(os, indent + 2);
157
158        os << std::endl;
159        Expression::print( os, indent );
160}
161
162UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
163                Expression( _aname ), type(type_), member(member_) {
164        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
165}
166
167UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
168        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
169
170UntypedOffsetofExpr::~UntypedOffsetofExpr() {
171        delete type;
172}
173
174void UntypedOffsetofExpr::print( std::ostream &os, int indent) const {
175        os << std::string( indent, ' ' ) << "Untyped Offsetof Expression on member " << member << " of ";
176
177        if ( type ) {
178                type->print(os, indent + 2);
179        } else {
180                os << "<NULL>";
181        }
182
183        os << std::endl;
184        Expression::print( os, indent );
185}
186
187OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
188                Expression( _aname ), type(type_), member(member_) {
189        set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
190}
191
192OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
193        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
194
195OffsetofExpr::~OffsetofExpr() {
196        delete type;
197}
198
199void OffsetofExpr::print( std::ostream &os, int indent) const {
200        os << std::string( indent, ' ' ) << "Offsetof Expression on member ";
201
202        if ( member ) {
203                os << member->get_name();
204        } else {
205                os << "<NULL>";
206        }
207
208        os << " of ";
209
210        if ( type ) {
211                type->print(os, indent + 2);
212        } else {
213                os << "<NULL>";
214        }
215
216        os << std::endl;
217        Expression::print( os, indent );
218}
219
220OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
221        set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
222}
223
224OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
225
226OffsetPackExpr::~OffsetPackExpr() { delete type; }
227
228void OffsetPackExpr::print( std::ostream &os, int indent ) const {
229        os << std::string( indent, ' ' ) << "Offset pack expression on ";
230
231        if ( type ) {
232                type->print(os, indent + 2);
233        } else {
234                os << "<NULL>";
235        }
236
237        os << std::endl;
238        Expression::print( os, indent );
239}
240
241AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
242                Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
243}
244
245AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
246                Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
247}
248
249AttrExpr::AttrExpr( const AttrExpr &other ) :
250                Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
251}
252
253AttrExpr::~AttrExpr() {
254        delete attr;
255        delete expr;
256        delete type;
257}
258
259void AttrExpr::print( std::ostream &os, int indent) const {
260        os << std::string( indent, ' ' ) << "Attr ";
261        attr->print( os, indent + 2 );
262        if ( isType || expr ) {
263                os << "applied to: ";
264
265                if (isType)
266                        type->print(os, indent + 2);
267                else
268                        expr->print(os, indent + 2);
269        } // if
270
271        os << std::endl;
272        Expression::print( os, indent );
273}
274
275CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
276        set_result(toType);
277}
278
279CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
280        set_result( new VoidType( Type::Qualifiers() ) );
281}
282
283CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
284}
285
286CastExpr::~CastExpr() {
287        delete arg;
288}
289
290// CastExpr *CastExpr::clone() const { return 0; }
291
292void CastExpr::print( std::ostream &os, int indent ) const {
293        os << "Cast of:" << std::endl << std::string( indent+2, ' ' );
294        arg->print(os, indent+2);
295        os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
296        os << std::string( indent+2, ' ' );
297        if ( result->isVoid() ) {
298                os << "nothing";
299        } else {
300                result->print( os, indent+2 );
301        } // if
302        os << std::endl;
303        Expression::print( os, indent );
304}
305
306UntypedMemberExpr::UntypedMemberExpr( Expression * _member, Expression *_aggregate, Expression *_aname ) :
307                Expression( _aname ), member(_member), aggregate(_aggregate) {}
308
309UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
310                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
311}
312
313UntypedMemberExpr::~UntypedMemberExpr() {
314        delete aggregate;
315        delete member;
316}
317
318void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
319        os << "Untyped Member Expression, with field: " << std::endl;
320        os << std::string( indent+2, ' ' );
321        get_member()->print(os, indent+4);
322        os << std::string( indent+2, ' ' );
323
324        Expression *agg = get_aggregate();
325        os << "from aggregate: " << std::endl;
326        if (agg != 0) {
327                os << std::string( indent + 4, ' ' );
328                agg->print(os, indent + 4);
329        }
330        os << std::string( indent+2, ' ' );
331        Expression::print( os, indent );
332}
333
334
335MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
336                Expression( _aname ), member(_member), aggregate(_aggregate) {
337        set_result( member->get_type()->clone() );
338        get_result()->set_isLvalue( true );
339}
340
341//// is this right? It's cloning the member, but the member is a declaration so probably shouldn't be cloned...
342MemberExpr::MemberExpr( const MemberExpr &other ) :
343                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
344}
345
346MemberExpr::~MemberExpr() {
347        // delete member;
348        delete aggregate;
349}
350
351void MemberExpr::print( std::ostream &os, int indent ) const {
352        os << "Member Expression, with field: " << std::endl;
353
354        assert( member );
355        os << std::string( indent + 2, ' ' );
356        member->print( os, indent + 2 );
357        os << std::endl;
358
359        Expression *agg = get_aggregate();
360        os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
361        if (agg != 0) {
362                os << std::string( indent + 2, ' ' );
363                agg->print(os, indent + 2);
364        }
365        os << std::string( indent+2, ' ' );
366        Expression::print( os, indent );
367}
368
369UntypedExpr::UntypedExpr( Expression *_function, const std::list<Expression *> &_args, Expression *_aname ) :
370                Expression( _aname ), function(_function), args(_args) {}
371
372UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
373                Expression( other ), function( maybeClone( other.function ) ) {
374        cloneAll( other.args, args );
375}
376
377UntypedExpr::~UntypedExpr() {
378        delete function;
379        deleteAll( args );
380}
381
382void UntypedExpr::print( std::ostream &os, int indent ) const {
383        os << "Applying untyped: " << std::endl;
384        os << std::string( indent+2, ' ' );
385        function->print(os, indent + 2);
386        os << std::string( indent, ' ' ) << "...to: " << std::endl;
387        printAll(args, os, indent + 2);
388        Expression::print( os, indent );
389}
390
391void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
392        std::list<Expression *>::const_iterator i;
393        for (i = args.begin(); i != args.end(); i++) {
394                os << std::string(indent, ' ' );
395                (*i)->print(os, indent);
396        }
397}
398
399NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
400
401NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
402}
403
404NameExpr::~NameExpr() {}
405
406void NameExpr::print( std::ostream &os, int indent ) const {
407        os << "Name: " << get_name() << std::endl;
408        Expression::print( os, indent );
409}
410
411LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
412                Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
413        set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
414}
415
416LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
417                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
418}
419
420LogicalExpr::~LogicalExpr() {
421        delete arg1;
422        delete arg2;
423}
424
425void LogicalExpr::print( std::ostream &os, int indent )const {
426        os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
427        arg1->print(os);
428        os << " and ";
429        arg2->print(os);
430        os << std::endl;
431        Expression::print( os, indent );
432}
433
434ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
435                Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
436
437ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
438                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
439}
440
441ConditionalExpr::~ConditionalExpr() {
442        delete arg1;
443        delete arg2;
444        delete arg3;
445}
446
447void ConditionalExpr::print( std::ostream &os, int indent ) const {
448        os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl;
449        arg1->print( os, indent+2 );
450        os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
451        arg2->print( os, indent+2 );
452        os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
453        arg3->print( os, indent+2 );
454        os << std::endl;
455        Expression::print( os, indent );
456}
457
458AsmExpr::AsmExpr( const AsmExpr & other ) : inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
459
460
461void AsmExpr::print( std::ostream &os, int indent ) const {
462        os << "Asm Expression: " << std::endl;
463        if ( inout ) inout->print( os, indent + 2 );
464        if ( constraint ) constraint->print( os, indent + 2 );
465        if ( operand ) operand->print( os, indent + 2 );
466}
467
468
469ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
470        assert( callExpr );
471        assert( callExpr->has_result() );
472        set_result( callExpr->get_result()->clone() );
473}
474
475ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
476        cloneAll( other.tempDecls, tempDecls );
477        cloneAll( other.returnDecls, returnDecls );
478        cloneAll( other.dtors, dtors );
479}
480
481ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
482        delete callExpr;
483        deleteAll( tempDecls );
484        deleteAll( returnDecls );
485        deleteAll( dtors );
486}
487
488void ImplicitCopyCtorExpr::print( std::ostream &os, int indent ) const {
489        os <<  "Implicit Copy Constructor Expression: " << std::endl;
490        assert( callExpr );
491        callExpr->print( os, indent + 2 );
492        os << std::endl << std::string( indent, ' ' ) << "with temporaries:" << std::endl;
493        printAll(tempDecls, os, indent+2);
494        os << std::endl << std::string( indent, ' ' ) << "with return temporaries:" << std::endl;
495        printAll(returnDecls, os, indent+2);
496        Expression::print( os, indent );
497}
498
499
500ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) {
501        // allow resolver to type a constructor used as an expression as if it has the same type as its first argument
502        assert( callExpr );
503        Expression * arg = InitTweak::getCallArg( callExpr, 0 );
504        assert( arg );
505        set_result( maybeClone( arg->get_result() ) );
506}
507
508ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
509}
510
511ConstructorExpr::~ConstructorExpr() {
512        delete callExpr;
513}
514
515void ConstructorExpr::print( std::ostream &os, int indent ) const {
516        os <<  "Constructor Expression: " << std::endl;
517        assert( callExpr );
518        os << std::string( indent+2, ' ' );
519        callExpr->print( os, indent + 2 );
520        Expression::print( os, indent );
521}
522
523
524CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
525        assert( type && initializer );
526        set_result( type->clone() );
527}
528
529CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( other.type->clone() ), initializer( other.initializer->clone() ) {}
530
531CompoundLiteralExpr::~CompoundLiteralExpr() {
532        delete initializer;
533        delete type;
534}
535
536void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
537        os << "Compound Literal Expression: " << std::endl;
538        os << std::string( indent+2, ' ' );
539        type->print( os, indent + 2 );
540        os << std::string( indent+2, ' ' );
541        initializer->print( os, indent + 2 );
542}
543
544UntypedValofExpr::UntypedValofExpr( const UntypedValofExpr & other ) : Expression( other ), body ( maybeClone( other.body ) ) {}
545
546UntypedValofExpr::~UntypedValofExpr() { delete body; }
547
548void UntypedValofExpr::print( std::ostream &os, int indent ) const {
549        os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
550        if ( get_body() != 0 )
551                get_body()->print( os, indent + 2 );
552}
553
554RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
555RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
556void RangeExpr::print( std::ostream &os, int indent ) const {
557        os << "Range Expression: ";
558        low->print( os, indent );
559        os << " ... ";
560        high->print( os, indent );
561}
562
563StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) {
564        assert( statements );
565        std::list< Statement * > & body = statements->get_kids();
566        if ( ! body.empty() ) {
567                if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
568                        set_result( maybeClone( exprStmt->get_expr()->get_result() ) );
569                }
570        }
571}
572StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {}
573StmtExpr::~StmtExpr() {
574        delete statements;
575}
576void StmtExpr::print( std::ostream &os, int indent ) const {
577        os << "Statement Expression: " << std::endl << std::string( indent, ' ' );
578        statements->print( os, indent+2 );
579}
580
581
582long long UniqueExpr::count = 0;
583UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( new Expression* ), id( idVal ) {
584        assert( count != -1 );
585        if ( id == -1 ) id = count++;
586        set_expr( expr );
587        assert( expr );
588        if ( expr->get_result() ) {
589                set_result( expr->get_result()->clone() );
590        }
591}
592UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( other.expr ), id( other.id ) {
593}
594UniqueExpr::~UniqueExpr() {
595        if ( expr.unique() ) {
596                delete *expr;
597        }
598}
599void UniqueExpr::print( std::ostream &os, int indent ) const {
600        os << "Unique Expression with id:" << id << std::endl << std::string( indent+2, ' ' );
601        get_expr()->print( os, indent+2 );
602}
603
604std::ostream & operator<<( std::ostream & out, const Expression * expr ) {
605        expr->print( out );
606        return out;
607}
608
609// Local Variables: //
610// tab-width: 4 //
611// mode: c++ //
612// compile-command: "make install" //
613// End: //
Note: See TracBrowser for help on using the repository browser.