source: src/SynTree/Expression.cc @ bb8ea30

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

fix printing for ArrayType?, Expression, Initializer, etc.

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