source: src/SynTree/Expression.cc @ 89231bc

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

fix printing for various AST nodes, including ObjectDecl?, ExprStmt?, etc. - assume preceding indentation printed by parent

  • Property mode set to 100644
File size: 15.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 : Tue Apr 26 12:52:40 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        add_result( var->get_type()->clone() );
82        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
83                (*i)->set_isLvalue( true );
84        } // for
85}
86
87VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
88}
89
90VariableExpr::~VariableExpr() {
91        // don't delete the declaration, since it points somewhere else in the tree
92}
93
94void VariableExpr::print( std::ostream &os, int indent ) const {
95        os << "Variable Expression: ";
96
97        Declaration *decl = get_var();
98        // if ( decl != 0) decl->print(os, indent + 2);
99        if ( decl != 0) decl->printShort(os, indent + 2);
100        os << std::endl;
101        Expression::print( os, indent );
102}
103
104SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
105                Expression( _aname ), expr(expr_), type(0), isType(false) {
106        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
107}
108
109SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
110                Expression( _aname ), expr(0), type(type_), isType(true) {
111        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
112}
113
114SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
115        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
116}
117
118SizeofExpr::~SizeofExpr() {
119        delete expr;
120        delete type;
121}
122
123void SizeofExpr::print( std::ostream &os, int indent) const {
124        os << "Sizeof Expression on: ";
125
126        if (isType)
127                type->print(os, indent + 2);
128        else
129                expr->print(os, indent + 2);
130
131        os << std::endl;
132        Expression::print( os, indent );
133}
134
135AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
136                Expression( _aname ), expr(expr_), type(0), isType(false) {
137        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
138}
139
140AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
141                Expression( _aname ), expr(0), type(type_), isType(true) {
142        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
143}
144
145AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
146        Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
147}
148
149AlignofExpr::~AlignofExpr() {
150        delete expr;
151        delete type;
152}
153
154void AlignofExpr::print( std::ostream &os, int indent) const {
155        os << std::string( indent, ' ' ) << "Alignof Expression on: ";
156
157        if (isType)
158                type->print(os, indent + 2);
159        else
160                expr->print(os, indent + 2);
161
162        os << std::endl;
163        Expression::print( os, indent );
164}
165
166UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
167                Expression( _aname ), type(type_), member(member_) {
168        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
169}
170
171UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
172        Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
173
174UntypedOffsetofExpr::~UntypedOffsetofExpr() {
175        delete type;
176}
177
178void UntypedOffsetofExpr::print( std::ostream &os, int indent) const {
179        os << std::string( indent, ' ' ) << "Untyped Offsetof Expression on member " << member << " of ";
180
181        if ( type ) {
182                type->print(os, indent + 2);
183        } else {
184                os << "<NULL>";
185        }
186
187        os << std::endl;
188        Expression::print( os, indent );
189}
190
191OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
192                Expression( _aname ), type(type_), member(member_) {
193        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
194}
195
196OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
197        Expression( other ), type( maybeClone( other.type ) ), member( maybeClone( other.member ) ) {}
198
199OffsetofExpr::~OffsetofExpr() {
200        delete type;
201        delete member;
202}
203
204void OffsetofExpr::print( std::ostream &os, int indent) const {
205        os << std::string( indent, ' ' ) << "Offsetof Expression on member ";
206
207        if ( member ) {
208                os << member->get_name();
209        } else {
210                os << "<NULL>";
211        }
212
213        os << " of ";
214
215        if ( type ) {
216                type->print(os, indent + 2);
217        } else {
218                os << "<NULL>";
219        }
220
221        os << std::endl;
222        Expression::print( os, indent );
223}
224
225OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
226        add_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
227}
228
229OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
230
231OffsetPackExpr::~OffsetPackExpr() { delete type; }
232
233void OffsetPackExpr::print( std::ostream &os, int indent ) const {
234        os << std::string( indent, ' ' ) << "Offset pack expression on ";
235
236        if ( type ) {
237                type->print(os, indent + 2);
238        } else {
239                os << "<NULL>";
240        }
241
242        os << std::endl;
243        Expression::print( os, indent );
244}
245
246AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
247                Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
248}
249
250AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
251                Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
252}
253
254AttrExpr::AttrExpr( const AttrExpr &other ) :
255                Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
256}
257
258AttrExpr::~AttrExpr() {
259        delete attr;
260        delete expr;
261        delete type;
262}
263
264void AttrExpr::print( std::ostream &os, int indent) const {
265        os << std::string( indent, ' ' ) << "Attr ";
266        attr->print( os, indent + 2 );
267        if ( isType || expr ) {
268                os << "applied to: ";
269
270                if (isType)
271                        type->print(os, indent + 2);
272                else
273                        expr->print(os, indent + 2);
274        } // if
275
276        os << std::endl;
277        Expression::print( os, indent );
278}
279
280CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
281        add_result(toType);
282}
283
284CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
285}
286
287CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
288}
289
290CastExpr::~CastExpr() {
291        delete arg;
292}
293
294// CastExpr *CastExpr::clone() const { return 0; }
295
296void CastExpr::print( std::ostream &os, int indent ) const {
297        os << "Cast of:" << std::endl << std::string( indent+2, ' ' );
298        arg->print(os, indent+2);
299        os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
300        if ( results.empty() ) {
301                os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
302        } else {
303                printAll(results, os, indent+2);
304        } // if
305        Expression::print( os, indent );
306}
307
308UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
309                Expression( _aname ), member(_member), aggregate(_aggregate) {}
310
311UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
312                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
313}
314
315UntypedMemberExpr::~UntypedMemberExpr() {
316        delete aggregate;
317}
318
319void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
320        os << "Untyped Member Expression, with field: " << get_member();
321
322        Expression *agg = get_aggregate();
323        os << std::string( indent, ' ' ) << "from aggregate: ";
324        if (agg != 0) {
325                os << std::string( indent+2, ' ' );
326                agg->print(os, indent + 2);
327        }
328        os << std::string( indent+2, ' ' );
329        Expression::print( os, indent );
330}
331
332
333MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
334                Expression( _aname ), member(_member), aggregate(_aggregate) {
335        add_result( member->get_type()->clone() );
336        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
337                (*i)->set_isLvalue( true );
338        } // for
339}
340
341MemberExpr::MemberExpr( const MemberExpr &other ) :
342                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
343}
344
345MemberExpr::~MemberExpr() {
346        delete member;
347        delete aggregate;
348}
349
350void MemberExpr::print( std::ostream &os, int indent ) const {
351        os << "Member Expression, with field: " << std::endl;
352
353        assert( member );
354        os << std::string( indent + 2, ' ' );
355        member->print( os, indent + 2 );
356        os << std::endl;
357
358        Expression *agg = get_aggregate();
359        os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
360        if (agg != 0) {
361                os << std::string( indent+2, ' ' );
362                agg->print(os, indent + 2);
363        }
364        os << std::string( indent+2, ' ' );
365        Expression::print( os, indent );
366}
367
368
369UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
370
371UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
372                Expression( other ), function( maybeClone( other.function ) ) {
373        cloneAll( other.args, args );
374}
375
376UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
377                Expression( _aname ), function(_function), args(_args) {}
378
379UntypedExpr::~UntypedExpr() {}
380
381void UntypedExpr::print( std::ostream &os, int indent ) const {
382        os << "Applying untyped: " << std::endl;
383        os << std::string( indent, ' ' );
384        function->print(os, indent + 4);
385        os << std::string( indent, ' ' ) << "...to: " << std::endl;
386        os << std::string( indent, ' ' );
387        printArgs(os, indent + 4);
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                (*i)->print(os, indent);
395}
396
397NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
398
399NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
400}
401
402NameExpr::~NameExpr() {}
403
404void NameExpr::print( std::ostream &os, int indent ) const {
405        os << "Name: " << get_name() << std::endl;
406        Expression::print( os, indent );
407}
408
409LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
410                Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
411        add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
412}
413
414LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
415                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
416}
417
418LogicalExpr::~LogicalExpr() {
419        delete arg1;
420        delete arg2;
421}
422
423void LogicalExpr::print( std::ostream &os, int indent )const {
424        os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
425        arg1->print(os);
426        os << " and ";
427        arg2->print(os);
428        os << std::endl;
429        Expression::print( os, indent );
430}
431
432ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
433                Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
434
435ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
436                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
437}
438
439ConditionalExpr::~ConditionalExpr() {
440        delete arg1;
441        delete arg2;
442        delete arg3;
443}
444
445void ConditionalExpr::print( std::ostream &os, int indent ) const {
446        os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl;
447        arg1->print( os, indent+2 );
448        os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
449        arg2->print( os, indent+2 );
450        os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
451        arg3->print( os, indent+2 );
452        os << std::endl;
453        Expression::print( os, indent );
454}
455
456AsmExpr::AsmExpr( const AsmExpr & other ) : inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
457
458
459void AsmExpr::print( std::ostream &os, int indent ) const {
460        os << "Asm Expression: " << std::endl;
461        if ( inout ) inout->print( os, indent + 2 );
462        if ( constraint ) constraint->print( os, indent + 2 );
463        if ( operand ) operand->print( os, indent + 2 );
464}
465
466UntypedValofExpr::UntypedValofExpr( const UntypedValofExpr & other ) : Expression( other ), body ( maybeClone( other.body ) ) {}
467
468UntypedValofExpr::~UntypedValofExpr() { delete body; }
469
470void UntypedValofExpr::print( std::ostream &os, int indent ) const {
471        os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
472        if ( get_body() != 0 )
473                get_body()->print( os, indent + 2 );
474}
475
476
477CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
478        add_result( type->clone() );
479}
480
481CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( maybeClone( other.type ) ), initializer( maybeClone( other.initializer ) ) {}
482
483CompoundLiteralExpr::~CompoundLiteralExpr() {
484        delete initializer;
485        delete type;
486}
487
488void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
489        os << "Compound Literal Expression: " << std::endl;
490        if ( type ) type->print( os, indent + 2 );
491        if ( initializer ) initializer->print( os, indent + 2 );
492}
493
494
495std::ostream & operator<<( std::ostream & out, Expression * expr ) {
496        expr->print( out );
497        return out;
498}
499
500// Local Variables: //
501// tab-width: 4 //
502// mode: c++ //
503// compile-command: "make install" //
504// End: //
Note: See TracBrowser for help on using the repository browser.