source: src/SynTree/Expression.cc @ d668182

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

Merge branch 'fix-tree-printing' into ctor

Conflicts:

src/SynTree/Expression.cc
src/SynTree/Initializer.cc
src/SynTree/ObjectDecl.cc

  • Property mode set to 100644
File size: 16.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 : Rob Schluntz
12// Last Modified On : Fri May 13 13:23:11 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 << "constant expression " ;
75        constant.print( os );
76        Expression::print( os, indent );
77}
78
79VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) {
80        assert( var );
81        assert( var->get_type() );
82        add_result( var->get_type()->clone() );
83        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
84                (*i)->set_isLvalue( true );
85        } // for
86}
87
88VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
89}
90
91VariableExpr::~VariableExpr() {
92        // don't delete the declaration, since it points somewhere else in the tree
93}
94
95void VariableExpr::print( std::ostream &os, int indent ) const {
96        os << "Variable Expression: ";
97
98        Declaration *decl = get_var();
99        // if ( decl != 0) decl->print(os, indent + 2);
100        if ( decl != 0) decl->printShort(os, indent + 2);
101        os << std::endl;
102        Expression::print( os, indent );
103}
104
105SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
106                Expression( _aname ), expr(expr_), type(0), isType(false) {
107        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
108}
109
110SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
111                Expression( _aname ), expr(0), type(type_), isType(true) {
112        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
113}
114
115SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
116        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
117}
118
119SizeofExpr::~SizeofExpr() {
120        delete expr;
121        delete type;
122}
123
124void SizeofExpr::print( std::ostream &os, int indent) const {
125        os << "Sizeof Expression on: ";
126
127        if (isType)
128                type->print(os, indent + 2);
129        else
130                expr->print(os, indent + 2);
131
132        os << std::endl;
133        Expression::print( os, indent );
134}
135
136AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
137                Expression( _aname ), expr(expr_), type(0), isType(false) {
138        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
139}
140
141AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
142                Expression( _aname ), expr(0), type(type_), isType(true) {
143        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
144}
145
146AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
147        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
148}
149
150AlignofExpr::~AlignofExpr() {
151        delete expr;
152        delete type;
153}
154
155void AlignofExpr::print( std::ostream &os, int indent) const {
156        os << std::string( indent, ' ' ) << "Alignof Expression on: ";
157
158        if (isType)
159                type->print(os, indent + 2);
160        else
161                expr->print(os, indent + 2);
162
163        os << std::endl;
164        Expression::print( os, indent );
165}
166
167UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
168                Expression( _aname ), type(type_), member(member_) {
169        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
170}
171
172UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
173        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
174
175UntypedOffsetofExpr::~UntypedOffsetofExpr() {
176        delete type;
177}
178
179void UntypedOffsetofExpr::print( std::ostream &os, int indent) const {
180        os << std::string( indent, ' ' ) << "Untyped Offsetof Expression on member " << member << " of ";
181
182        if ( type ) {
183                type->print(os, indent + 2);
184        } else {
185                os << "<NULL>";
186        }
187
188        os << std::endl;
189        Expression::print( os, indent );
190}
191
192OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
193                Expression( _aname ), type(type_), member(member_) {
194        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
195}
196
197OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
198        Expression( other ), type( maybeClone( other.type ) ), member( maybeClone( other.member ) ) {}
199
200OffsetofExpr::~OffsetofExpr() {
201        delete type;
202        delete member;
203}
204
205void OffsetofExpr::print( std::ostream &os, int indent) const {
206        os << std::string( indent, ' ' ) << "Offsetof Expression on member ";
207
208        if ( member ) {
209                os << member->get_name();
210        } else {
211                os << "<NULL>";
212        }
213
214        os << " of ";
215
216        if ( type ) {
217                type->print(os, indent + 2);
218        } else {
219                os << "<NULL>";
220        }
221
222        os << std::endl;
223        Expression::print( os, indent );
224}
225
226OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
227        add_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
228}
229
230OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
231
232OffsetPackExpr::~OffsetPackExpr() { delete type; }
233
234void OffsetPackExpr::print( std::ostream &os, int indent ) const {
235        os << std::string( indent, ' ' ) << "Offset pack expression on ";
236
237        if ( type ) {
238                type->print(os, indent + 2);
239        } else {
240                os << "<NULL>";
241        }
242
243        os << std::endl;
244        Expression::print( os, indent );
245}
246
247AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
248                Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
249}
250
251AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
252                Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
253}
254
255AttrExpr::AttrExpr( const AttrExpr &other ) :
256                Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
257}
258
259AttrExpr::~AttrExpr() {
260        delete attr;
261        delete expr;
262        delete type;
263}
264
265void AttrExpr::print( std::ostream &os, int indent) const {
266        os << std::string( indent, ' ' ) << "Attr ";
267        attr->print( os, indent + 2 );
268        if ( isType || expr ) {
269                os << "applied to: ";
270
271                if (isType)
272                        type->print(os, indent + 2);
273                else
274                        expr->print(os, indent + 2);
275        } // if
276
277        os << std::endl;
278        Expression::print( os, indent );
279}
280
281CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
282        add_result(toType);
283}
284
285CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
286}
287
288CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
289}
290
291CastExpr::~CastExpr() {
292        delete arg;
293}
294
295// CastExpr *CastExpr::clone() const { return 0; }
296
297void CastExpr::print( std::ostream &os, int indent ) const {
298        os << "Cast of:" << std::endl << std::string( indent+2, ' ' );
299        arg->print(os, indent+2);
300        os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
301        if ( results.empty() ) {
302                os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
303        } else {
304                printAll(results, os, indent+2);
305        } // if
306        Expression::print( os, indent );
307}
308
309UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
310                Expression( _aname ), member(_member), aggregate(_aggregate) {}
311
312UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
313                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
314}
315
316UntypedMemberExpr::~UntypedMemberExpr() {
317        delete aggregate;
318}
319
320void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
321        os << "Untyped Member Expression, with field: " << get_member();
322
323        Expression *agg = get_aggregate();
324        os << ", from aggregate: ";
325        if (agg != 0) {
326                os << std::string( indent + 2, ' ' );
327                agg->print(os, indent + 2);
328        }
329        os << std::string( indent+2, ' ' );
330        Expression::print( os, indent );
331}
332
333
334MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
335                Expression( _aname ), member(_member), aggregate(_aggregate) {
336        add_result( member->get_type()->clone() );
337        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
338                (*i)->set_isLvalue( true );
339        } // for
340}
341
342MemberExpr::MemberExpr( const MemberExpr &other ) :
343                Expression( other ), member( maybeClone( 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
369
370UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
371
372UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
373                Expression( other ), function( maybeClone( other.function ) ) {
374        cloneAll( other.args, args );
375}
376
377UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
378                Expression( _aname ), function(_function), args(_args) {}
379
380UntypedExpr::~UntypedExpr() {}
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        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.