source: src/SynTree/Expression.cc @ 906e24d

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

replace results list on Expressions with a single Type field

  • Property mode set to 100644
File size: 17.1 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( std::string _member, Expression *_aggregate, Expression *_aname ) :
307                Expression( _aname ), member(_member), aggregate(_aggregate) {}
308
309UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
310                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
311}
312
313UntypedMemberExpr::~UntypedMemberExpr() {
314        delete aggregate;
315}
316
317void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
318        os << "Untyped Member Expression, with field: " << get_member();
319
320        Expression *agg = get_aggregate();
321        os << ", from aggregate: ";
322        if (agg != 0) {
323                os << std::string( indent + 2, ' ' );
324                agg->print(os, indent + 2);
325        }
326        os << std::string( indent+2, ' ' );
327        Expression::print( os, indent );
328}
329
330
331MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
332                Expression( _aname ), member(_member), aggregate(_aggregate) {
333        set_result( member->get_type()->clone() );
334        get_result()->set_isLvalue( true );
335}
336
337//// is this right? It's cloning the member, but the member is a declaration so probably shouldn't be cloned...
338MemberExpr::MemberExpr( const MemberExpr &other ) :
339                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
340}
341
342MemberExpr::~MemberExpr() {
343        // delete member;
344        delete aggregate;
345}
346
347void MemberExpr::print( std::ostream &os, int indent ) const {
348        os << "Member Expression, with field: " << std::endl;
349
350        assert( member );
351        os << std::string( indent + 2, ' ' );
352        member->print( os, indent + 2 );
353        os << std::endl;
354
355        Expression *agg = get_aggregate();
356        os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
357        if (agg != 0) {
358                os << std::string( indent + 2, ' ' );
359                agg->print(os, indent + 2);
360        }
361        os << std::string( indent+2, ' ' );
362        Expression::print( os, indent );
363}
364
365
366UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
367
368UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
369                Expression( other ), function( maybeClone( other.function ) ) {
370        cloneAll( other.args, args );
371}
372
373UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
374                Expression( _aname ), function(_function), args(_args) {}
375
376UntypedExpr::~UntypedExpr() {
377        delete function;
378        deleteAll( args );
379}
380
381void UntypedExpr::print( std::ostream &os, int indent ) const {
382        os << "Applying untyped: " << std::endl;
383        os << std::string( indent+2, ' ' );
384        function->print(os, indent + 2);
385        os << std::string( indent, ' ' ) << "...to: " << std::endl;
386        printAll(args, os, indent + 2);
387        Expression::print( os, indent );
388}
389
390void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
391        std::list<Expression *>::const_iterator i;
392        for (i = args.begin(); i != args.end(); i++) {
393                os << std::string(indent, ' ' );
394                (*i)->print(os, indent);
395        }
396}
397
398NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
399
400NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
401}
402
403NameExpr::~NameExpr() {}
404
405void NameExpr::print( std::ostream &os, int indent ) const {
406        os << "Name: " << get_name() << std::endl;
407        Expression::print( os, indent );
408}
409
410LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
411                Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
412        set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
413}
414
415LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
416                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
417}
418
419LogicalExpr::~LogicalExpr() {
420        delete arg1;
421        delete arg2;
422}
423
424void LogicalExpr::print( std::ostream &os, int indent )const {
425        os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
426        arg1->print(os);
427        os << " and ";
428        arg2->print(os);
429        os << std::endl;
430        Expression::print( os, indent );
431}
432
433ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
434                Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
435
436ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
437                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
438}
439
440ConditionalExpr::~ConditionalExpr() {
441        delete arg1;
442        delete arg2;
443        delete arg3;
444}
445
446void ConditionalExpr::print( std::ostream &os, int indent ) const {
447        os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl;
448        arg1->print( os, indent+2 );
449        os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
450        arg2->print( os, indent+2 );
451        os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
452        arg3->print( os, indent+2 );
453        os << std::endl;
454        Expression::print( os, indent );
455}
456
457AsmExpr::AsmExpr( const AsmExpr & other ) : inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
458
459
460void AsmExpr::print( std::ostream &os, int indent ) const {
461        os << "Asm Expression: " << std::endl;
462        if ( inout ) inout->print( os, indent + 2 );
463        if ( constraint ) constraint->print( os, indent + 2 );
464        if ( operand ) operand->print( os, indent + 2 );
465}
466
467
468ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
469        assert( callExpr );
470        assert( callExpr->has_result() );
471        set_result( callExpr->get_result()->clone() );
472}
473
474ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
475        cloneAll( other.tempDecls, tempDecls );
476        cloneAll( other.returnDecls, returnDecls );
477        cloneAll( other.dtors, dtors );
478}
479
480ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
481        delete callExpr;
482        deleteAll( tempDecls );
483        deleteAll( returnDecls );
484        deleteAll( dtors );
485}
486
487void ImplicitCopyCtorExpr::print( std::ostream &os, int indent ) const {
488        os <<  "Implicit Copy Constructor Expression: " << std::endl;
489        assert( callExpr );
490        callExpr->print( os, indent + 2 );
491        os << std::endl << std::string( indent, ' ' ) << "with temporaries:" << std::endl;
492        printAll(tempDecls, os, indent+2);
493        os << std::endl << std::string( indent, ' ' ) << "with return temporaries:" << std::endl;
494        printAll(returnDecls, os, indent+2);
495        Expression::print( os, indent );
496}
497
498
499ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) {
500        // allow resolver to type a constructor used as an expression as if it has the same type as its first argument
501        assert( callExpr );
502        Expression * arg = InitTweak::getCallArg( callExpr, 0 );
503        assert( arg );
504        set_result( maybeClone( arg->get_result() ) );
505}
506
507ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
508}
509
510ConstructorExpr::~ConstructorExpr() {
511        delete callExpr;
512}
513
514void ConstructorExpr::print( std::ostream &os, int indent ) const {
515        os <<  "Constructor Expression: " << std::endl;
516        assert( callExpr );
517        os << std::string( indent+2, ' ' );
518        callExpr->print( os, indent + 2 );
519        Expression::print( os, indent );
520}
521
522
523CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
524        set_result( type->clone() );
525}
526
527CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( maybeClone( other.type ) ), initializer( maybeClone( other.initializer ) ) {}
528
529CompoundLiteralExpr::~CompoundLiteralExpr() {
530        delete initializer;
531        delete type;
532}
533
534void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
535        os << "Compound Literal Expression: " << std::endl;
536        if ( type ) type->print( os, indent + 2 );
537        if ( initializer ) initializer->print( os, indent + 2 );
538}
539
540UntypedValofExpr::UntypedValofExpr( const UntypedValofExpr & other ) : Expression( other ), body ( maybeClone( other.body ) ) {}
541
542UntypedValofExpr::~UntypedValofExpr() { delete body; }
543
544void UntypedValofExpr::print( std::ostream &os, int indent ) const {
545        os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
546        if ( get_body() != 0 )
547                get_body()->print( os, indent + 2 );
548}
549
550RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
551RangeExpr::RangeExpr( const RangeExpr &other ) : low( other.low->clone() ), high( other.high->clone() ) {}
552void RangeExpr::print( std::ostream &os, int indent ) const {
553        os << std::string( indent, ' ' ) << "Range Expression: ";
554        low->print( os, indent );
555        os << " ... ";
556        high->print( os, indent );
557}
558
559std::ostream & operator<<( std::ostream & out, const Expression * expr ) {
560        expr->print( out );
561        return out;
562}
563
564// Local Variables: //
565// tab-width: 4 //
566// mode: c++ //
567// compile-command: "make install" //
568// End: //
Note: See TracBrowser for help on using the repository browser.