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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4ffdd63 was 4ffdd63, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

change FixCopyCtors? to insert comma expressions and UntypedExpr? assignments rather than changing the location of the argument. Remove copyCtors list from ImplicitCopyCtorExpr?

  • Property mode set to 100644
File size: 16.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 : Rob Schluntz
12// Last Modified On : Wed Apr 27 17:07:19 2016
13// Update Count     : 40
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
31
32Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {}
33
34Expression::Expression( const Expression &other ) : env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ) {
35        cloneAll( other.results, results );
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        deleteAll( results );
42}
43
44void Expression::add_result( Type *t ) {
45        if ( TupleType *tuple = dynamic_cast< TupleType* >( t ) ) {
46                std::copy( tuple->get_types().begin(), tuple->get_types().end(), back_inserter( results ) );
47        } else {
48                results.push_back(t);
49        } // if
50}
51
52void Expression::print( std::ostream &os, int indent ) const {
53        if ( env ) {
54                os << std::string( indent, ' ' ) << "with environment:" << std::endl;
55                env->print( os, indent+2 );
56        } // if
57
58        if ( argName ) {
59                os << std::string( indent, ' ' ) << "with designator:";
60                argName->print( os, indent+2 );
61        } // if
62}
63
64ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) {
65        add_result( constant.get_type()->clone() );
66}
67
68ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
69}
70
71ConstantExpr::~ConstantExpr() {}
72
73void ConstantExpr::print( std::ostream &os, int indent ) const {
74        os << std::string( indent, ' ' ) << "constant expression " ;
75        constant.print( os );
76        Expression::print( os, indent );
77        os << std::endl;
78}
79
80VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) {
81        assert( var );
82        assert( var->get_type() );
83        add_result( var->get_type()->clone() );
84        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
85                (*i)->set_isLvalue( true );
86        } // for
87}
88
89VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
90}
91
92VariableExpr::~VariableExpr() {
93        // don't delete the declaration, since it points somewhere else in the tree
94}
95
96void VariableExpr::print( std::ostream &os, int indent ) const {
97        os << "Variable Expression: ";
98
99        Declaration *decl = get_var();
100        // if ( decl != 0) decl->print(os, indent + 2);
101        if ( decl != 0) decl->printShort(os, indent + 2);
102        os << std::endl;
103        Expression::print( os, indent );
104}
105
106SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
107                Expression( _aname ), expr(expr_), type(0), isType(false) {
108        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
109}
110
111SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
112                Expression( _aname ), expr(0), type(type_), isType(true) {
113        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
114}
115
116SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
117        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
118}
119
120SizeofExpr::~SizeofExpr() {
121        delete expr;
122        delete type;
123}
124
125void SizeofExpr::print( std::ostream &os, int indent) const {
126        os << "Sizeof Expression on: ";
127
128        if (isType)
129                type->print(os, indent + 2);
130        else
131                expr->print(os, indent + 2);
132
133        os << std::endl;
134        Expression::print( os, indent );
135}
136
137AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
138                Expression( _aname ), expr(expr_), type(0), isType(false) {
139        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
140}
141
142AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
143                Expression( _aname ), expr(0), type(type_), isType(true) {
144        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
145}
146
147AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
148        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
149}
150
151AlignofExpr::~AlignofExpr() {
152        delete expr;
153        delete type;
154}
155
156void AlignofExpr::print( std::ostream &os, int indent) const {
157        os << std::string( indent, ' ' ) << "Alignof Expression on: ";
158
159        if (isType)
160                type->print(os, indent + 2);
161        else
162                expr->print(os, indent + 2);
163
164        os << std::endl;
165        Expression::print( os, indent );
166}
167
168UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
169                Expression( _aname ), type(type_), member(member_) {
170        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
171}
172
173UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
174        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
175
176UntypedOffsetofExpr::~UntypedOffsetofExpr() {
177        delete type;
178}
179
180void UntypedOffsetofExpr::print( std::ostream &os, int indent) const {
181        os << std::string( indent, ' ' ) << "Untyped Offsetof Expression on member " << member << " of ";
182
183        if ( type ) {
184                type->print(os, indent + 2);
185        } else {
186                os << "<NULL>";
187        }
188
189        os << std::endl;
190        Expression::print( os, indent );
191}
192
193OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
194                Expression( _aname ), type(type_), member(member_) {
195        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
196}
197
198OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
199        Expression( other ), type( maybeClone( other.type ) ), member( maybeClone( other.member ) ) {}
200
201OffsetofExpr::~OffsetofExpr() {
202        delete type;
203        delete member;
204}
205
206void OffsetofExpr::print( std::ostream &os, int indent) const {
207        os << std::string( indent, ' ' ) << "Offsetof Expression on member ";
208
209        if ( member ) {
210                os << member->get_name();
211        } else {
212                os << "<NULL>";
213        }
214
215        os << " of ";
216
217        if ( type ) {
218                type->print(os, indent + 2);
219        } else {
220                os << "<NULL>";
221        }
222
223        os << std::endl;
224        Expression::print( os, indent );
225}
226
227OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
228        add_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
229}
230
231OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
232
233OffsetPackExpr::~OffsetPackExpr() { delete type; }
234
235void OffsetPackExpr::print( std::ostream &os, int indent ) const {
236        os << std::string( indent, ' ' ) << "Offset pack expression on ";
237
238        if ( type ) {
239                type->print(os, indent + 2);
240        } else {
241                os << "<NULL>";
242        }
243
244        os << std::endl;
245        Expression::print( os, indent );
246}
247
248AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
249                Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
250}
251
252AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
253                Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
254}
255
256AttrExpr::AttrExpr( const AttrExpr &other ) :
257                Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
258}
259
260AttrExpr::~AttrExpr() {
261        delete attr;
262        delete expr;
263        delete type;
264}
265
266void AttrExpr::print( std::ostream &os, int indent) const {
267        os << std::string( indent, ' ' ) << "Attr ";
268        attr->print( os, indent + 2 );
269        if ( isType || expr ) {
270                os << "applied to: ";
271
272                if (isType)
273                        type->print(os, indent + 2);
274                else
275                        expr->print(os, indent + 2);
276        } // if
277
278        os << std::endl;
279        Expression::print( os, indent );
280}
281
282CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
283        add_result(toType);
284}
285
286CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
287}
288
289CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
290}
291
292CastExpr::~CastExpr() {
293        delete arg;
294}
295
296// CastExpr *CastExpr::clone() const { return 0; }
297
298void CastExpr::print( std::ostream &os, int indent ) const {
299        os << "Cast of:" << std::endl << std::string( indent+2, ' ' );
300        arg->print(os, indent+2);
301        os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
302        if ( results.empty() ) {
303                os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
304        } else {
305                printAll(results, os, indent+2);
306        } // if
307        Expression::print( os, indent );
308}
309
310UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
311                Expression( _aname ), member(_member), aggregate(_aggregate) {}
312
313UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
314                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
315}
316
317UntypedMemberExpr::~UntypedMemberExpr() {
318        delete aggregate;
319}
320
321void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
322        os << "Untyped Member Expression, with field: " << get_member();
323
324        Expression *agg = get_aggregate();
325        os << std::string( indent, ' ' ) << "from aggregate: ";
326        if (agg != 0) {
327                os << std::string( indent+2, ' ' );
328                agg->print(os, indent + 2);
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        add_result( member->get_type()->clone() );
338        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
339                (*i)->set_isLvalue( true );
340        } // for
341}
342
343MemberExpr::MemberExpr( const MemberExpr &other ) :
344                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
345}
346
347MemberExpr::~MemberExpr() {
348        delete member;
349        delete aggregate;
350}
351
352void MemberExpr::print( std::ostream &os, int indent ) const {
353        os << "Member Expression, with field: " << std::endl;
354
355        assert( member );
356        os << std::string( indent + 2, ' ' );
357        member->print( os, indent + 2 );
358        os << std::endl;
359
360        Expression *agg = get_aggregate();
361        os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
362        if (agg != 0) {
363                os << std::string( indent+2, ' ' );
364                agg->print(os, indent + 2);
365        }
366        os << std::string( indent+2, ' ' );
367        Expression::print( os, indent );
368}
369
370
371UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
372
373UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
374                Expression( other ), function( maybeClone( other.function ) ) {
375        cloneAll( other.args, args );
376}
377
378UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
379                Expression( _aname ), function(_function), args(_args) {}
380
381UntypedExpr::~UntypedExpr() {}
382
383void UntypedExpr::print( std::ostream &os, int indent ) const {
384        os << "Applying untyped: " << std::endl;
385        os << std::string( indent, ' ' );
386        function->print(os, indent + 4);
387        os << std::string( indent, ' ' ) << "...to: " << std::endl;
388        os << std::string( indent, ' ' );
389        printArgs(os, indent + 4);
390        Expression::print( os, indent );
391}
392
393void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
394        std::list<Expression *>::const_iterator i;
395        for (i = args.begin(); i != args.end(); i++)
396                (*i)->print(os, indent);
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        add_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        cloneAll( callExpr->get_results(), results );
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 << std::string( indent, ' ' ) <<  "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
498UntypedValofExpr::UntypedValofExpr( const UntypedValofExpr & other ) : Expression( other ), body ( maybeClone( other.body ) ) {}
499
500UntypedValofExpr::~UntypedValofExpr() { delete body; }
501
502void UntypedValofExpr::print( std::ostream &os, int indent ) const {
503        os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
504        if ( get_body() != 0 )
505                get_body()->print( os, indent + 2 );
506}
507
508
509CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
510        add_result( type->clone() );
511}
512
513CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( maybeClone( other.type ) ), initializer( maybeClone( other.initializer ) ) {}
514
515CompoundLiteralExpr::~CompoundLiteralExpr() {
516        delete initializer;
517        delete type;
518}
519
520void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
521        os << "Compound Literal Expression: " << std::endl;
522        if ( type ) type->print( os, indent + 2 );
523        if ( initializer ) initializer->print( os, indent + 2 );
524}
525
526
527std::ostream & operator<<( std::ostream & out, Expression * expr ) {
528        expr->print( out );
529        return out;
530}
531
532// Local Variables: //
533// tab-width: 4 //
534// mode: c++ //
535// compile-command: "make install" //
536// End: //
Note: See TracBrowser for help on using the repository browser.