source: src/SynTree/Expression.cc @ f006f01

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 f006f01 was 6eb8948, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

make TupleAssignment? generate temporaries, add StmtExpr? for GCC statement expressions, expand tuple assignment expressions, collapse SolvedTupleExpr?, MassAssignExpr?, and MultipleAssignExpr? into TupleAssignExpr?

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