source: src/SynTree/Expression.cc@ 2edd5502

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 2edd5502 was d668182, checked in by Rob Schluntz <rschlunt@…>, 9 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
RevLine 
[0dd3a2f]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//
[3be261a]7// Expression.cc --
[0dd3a2f]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[d58ebf3]11// Last Modified By : Rob Schluntz
[d668182]12// Last Modified On : Fri May 13 13:23:11 2016
[630a82a]13// Update Count : 40
[0dd3a2f]14//
15
[51b73452]16#include <iostream>
17#include <cassert>
18#include <list>
19#include <algorithm>
20
21#include <iterator>
22
23#include "Type.h"
[630a82a]24#include "Initializer.h"
[51b73452]25#include "Expression.h"
26#include "Declaration.h"
27#include "Statement.h"
28#include "TypeSubstitution.h"
[d3b7937]29#include "Common/utility.h"
[51b73452]30
31
32Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {}
[0dd3a2f]33
[d0d9610]34Expression::Expression( const Expression &other ) : env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ) {
[0dd3a2f]35 cloneAll( other.results, results );
36}
37
38Expression::~Expression() {
39 delete env;
[7f5566b]40 delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix
[0dd3a2f]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
[51b73452]50}
51
[59db689]52void Expression::print( std::ostream &os, int indent ) const {
[0dd3a2f]53 if ( env ) {
[cf0941d]54 os << std::string( indent, ' ' ) << "with environment:" << std::endl;
[0dd3a2f]55 env->print( os, indent+2 );
56 } // if
[51b73452]57
[0dd3a2f]58 if ( argName ) {
[cf0941d]59 os << std::string( indent, ' ' ) << "with designator:";
[0dd3a2f]60 argName->print( os, indent+2 );
61 } // if
[51b73452]62}
63
[0dd3a2f]64ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) {
65 add_result( constant.get_type()->clone() );
[51b73452]66}
67
[0dd3a2f]68ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
69}
[51b73452]70
71ConstantExpr::~ConstantExpr() {}
72
[0dd3a2f]73void ConstantExpr::print( std::ostream &os, int indent ) const {
[60089f4]74 os << "constant expression " ;
[cf0941d]75 constant.print( os );
[0dd3a2f]76 Expression::print( os, indent );
[51b73452]77}
78
[0dd3a2f]79VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) {
[db4ecc5]80 assert( var );
81 assert( var->get_type() );
[0dd3a2f]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
[51b73452]86}
87
[0dd3a2f]88VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
[51b73452]89}
90
[0dd3a2f]91VariableExpr::~VariableExpr() {
92 // don't delete the declaration, since it points somewhere else in the tree
[51b73452]93}
94
[0dd3a2f]95void VariableExpr::print( std::ostream &os, int indent ) const {
[89231bc]96 os << "Variable Expression: ";
[51b73452]97
[0dd3a2f]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 );
[51b73452]103}
104
105SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
[0dd3a2f]106 Expression( _aname ), expr(expr_), type(0), isType(false) {
[bfebb6c5]107 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[51b73452]108}
109
110SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
[0dd3a2f]111 Expression( _aname ), expr(0), type(type_), isType(true) {
[bfebb6c5]112 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[51b73452]113}
114
[0dd3a2f]115SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
116 Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
[51b73452]117}
118
[0dd3a2f]119SizeofExpr::~SizeofExpr() {
120 delete expr;
121 delete type;
[51b73452]122}
123
124void SizeofExpr::print( std::ostream &os, int indent) const {
[89231bc]125 os << "Sizeof Expression on: ";
[51b73452]126
[47534159]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) {
[bfebb6c5]138 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[47534159]139}
140
141AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
142 Expression( _aname ), expr(0), type(type_), isType(true) {
[bfebb6c5]143 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[47534159]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
[0dd3a2f]158 if (isType)
159 type->print(os, indent + 2);
160 else
161 expr->print(os, indent + 2);
[51b73452]162
[0dd3a2f]163 os << std::endl;
164 Expression::print( os, indent );
[51b73452]165}
166
[2a4b088]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
[25a054f]192OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
193 Expression( _aname ), type(type_), member(member_) {
[bfebb6c5]194 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
[25a054f]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
[afc1045]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
[25a054f]243 os << std::endl;
244 Expression::print( os, indent );
245}
246
[51b73452]247AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
[0dd3a2f]248 Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
[51b73452]249}
250
251AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
[0dd3a2f]252 Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
[51b73452]253}
254
[0dd3a2f]255AttrExpr::AttrExpr( const AttrExpr &other ) :
256 Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
[51b73452]257}
258
[0dd3a2f]259AttrExpr::~AttrExpr() {
260 delete attr;
261 delete expr;
262 delete type;
[51b73452]263}
264
265void AttrExpr::print( std::ostream &os, int indent) const {
[44b5ca0]266 os << std::string( indent, ' ' ) << "Attr ";
[0dd3a2f]267 attr->print( os, indent + 2 );
268 if ( isType || expr ) {
269 os << "applied to: ";
[51b73452]270
[0dd3a2f]271 if (isType)
272 type->print(os, indent + 2);
273 else
274 expr->print(os, indent + 2);
275 } // if
[51b73452]276
[0dd3a2f]277 os << std::endl;
278 Expression::print( os, indent );
[51b73452]279}
280
[0dd3a2f]281CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
282 add_result(toType);
[51b73452]283}
284
285CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
286}
287
[0dd3a2f]288CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
[51b73452]289}
290
291CastExpr::~CastExpr() {
[0dd3a2f]292 delete arg;
[51b73452]293}
294
295// CastExpr *CastExpr::clone() const { return 0; }
296
297void CastExpr::print( std::ostream &os, int indent ) const {
[89231bc]298 os << "Cast of:" << std::endl << std::string( indent+2, ' ' );
[0dd3a2f]299 arg->print(os, indent+2);
[44b5ca0]300 os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
[0dd3a2f]301 if ( results.empty() ) {
[44b5ca0]302 os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
[0dd3a2f]303 } else {
304 printAll(results, os, indent+2);
305 } // if
306 Expression::print( os, indent );
307}
308
[51b73452]309UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
[0dd3a2f]310 Expression( _aname ), member(_member), aggregate(_aggregate) {}
[51b73452]311
[0dd3a2f]312UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
313 Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
[51b73452]314}
315
316UntypedMemberExpr::~UntypedMemberExpr() {
[0dd3a2f]317 delete aggregate;
[51b73452]318}
319
320void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
[89231bc]321 os << "Untyped Member Expression, with field: " << get_member();
[51b73452]322
[0dd3a2f]323 Expression *agg = get_aggregate();
[9243a501]324 os << ", from aggregate: ";
[89231bc]325 if (agg != 0) {
[bb8ea30]326 os << std::string( indent + 2, ' ' );
[89231bc]327 agg->print(os, indent + 2);
328 }
329 os << std::string( indent+2, ' ' );
[0dd3a2f]330 Expression::print( os, indent );
[51b73452]331}
332
333
334MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
[0dd3a2f]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
[51b73452]340}
341
[0dd3a2f]342MemberExpr::MemberExpr( const MemberExpr &other ) :
343 Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
[51b73452]344}
345
346MemberExpr::~MemberExpr() {
[0dd3a2f]347 delete member;
348 delete aggregate;
[51b73452]349}
350
351void MemberExpr::print( std::ostream &os, int indent ) const {
[89231bc]352 os << "Member Expression, with field: " << std::endl;
[51b73452]353
[0dd3a2f]354 assert( member );
[44b5ca0]355 os << std::string( indent + 2, ' ' );
[0dd3a2f]356 member->print( os, indent + 2 );
357 os << std::endl;
[51b73452]358
[0dd3a2f]359 Expression *agg = get_aggregate();
[44b5ca0]360 os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
[89231bc]361 if (agg != 0) {
[bb8ea30]362 os << std::string( indent + 2, ' ' );
[89231bc]363 agg->print(os, indent + 2);
364 }
365 os << std::string( indent+2, ' ' );
[0dd3a2f]366 Expression::print( os, indent );
[51b73452]367}
368
369
370UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
371
[0dd3a2f]372UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
373 Expression( other ), function( maybeClone( other.function ) ) {
374 cloneAll( other.args, args );
[51b73452]375}
376
377UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
[0dd3a2f]378 Expression( _aname ), function(_function), args(_args) {}
[51b73452]379
380UntypedExpr::~UntypedExpr() {}
381
382void UntypedExpr::print( std::ostream &os, int indent ) const {
[89231bc]383 os << "Applying untyped: " << std::endl;
[bb8ea30]384 os << std::string( indent+2, ' ' );
385 function->print(os, indent + 2);
[44b5ca0]386 os << std::string( indent, ' ' ) << "...to: " << std::endl;
[bb8ea30]387 printAll(args, os, indent + 2);
[0dd3a2f]388 Expression::print( os, indent );
[51b73452]389}
390
[0dd3a2f]391void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
392 std::list<Expression *>::const_iterator i;
[60089f4]393 for (i = args.begin(); i != args.end(); i++) {
394 os << std::string(indent, ' ' );
[0dd3a2f]395 (*i)->print(os, indent);
[60089f4]396 }
[51b73452]397}
398
399NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
400
[0dd3a2f]401NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
[51b73452]402}
403
404NameExpr::~NameExpr() {}
405
406void NameExpr::print( std::ostream &os, int indent ) const {
[89231bc]407 os << "Name: " << get_name() << std::endl;
[0dd3a2f]408 Expression::print( os, indent );
[51b73452]409}
410
411LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
[0dd3a2f]412 Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
413 add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[51b73452]414}
415
[0dd3a2f]416LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
417 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
[51b73452]418}
419
[a08ba92]420LogicalExpr::~LogicalExpr() {
[0dd3a2f]421 delete arg1;
422 delete arg2;
[51b73452]423}
424
425void LogicalExpr::print( std::ostream &os, int indent )const {
[44b5ca0]426 os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
[0dd3a2f]427 arg1->print(os);
428 os << " and ";
429 arg2->print(os);
430 os << std::endl;
431 Expression::print( os, indent );
[51b73452]432}
433
434ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
[0dd3a2f]435 Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
[51b73452]436
[0dd3a2f]437ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
438 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
439}
[51b73452]440
441ConditionalExpr::~ConditionalExpr() {
[0dd3a2f]442 delete arg1;
443 delete arg2;
444 delete arg3;
[51b73452]445}
446
447void ConditionalExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]448 os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl;
[0dd3a2f]449 arg1->print( os, indent+2 );
[44b5ca0]450 os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
[0dd3a2f]451 arg2->print( os, indent+2 );
[44b5ca0]452 os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
[0dd3a2f]453 arg3->print( os, indent+2 );
454 os << std::endl;
455 Expression::print( os, indent );
456}
457
[3be261a]458AsmExpr::AsmExpr( const AsmExpr & other ) : inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
459
460
[7f5566b]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
[db4ecc5]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);
[dc2e7e0]493 os << std::endl << std::string( indent, ' ' ) << "with return temporaries:" << std::endl;
494 printAll(returnDecls, os, indent+2);
[db4ecc5]495 Expression::print( os, indent );
496}
497
[3be261a]498UntypedValofExpr::UntypedValofExpr( const UntypedValofExpr & other ) : Expression( other ), body ( maybeClone( other.body ) ) {}
499
500UntypedValofExpr::~UntypedValofExpr() { delete body; }
501
[0dd3a2f]502void UntypedValofExpr::print( std::ostream &os, int indent ) const {
[44b5ca0]503 os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
[0dd3a2f]504 if ( get_body() != 0 )
505 get_body()->print( os, indent + 2 );
506}
507
[3be261a]508
[630a82a]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
[3be261a]526
[baf7fee]527std::ostream & operator<<( std::ostream & out, Expression * expr ) {
528 expr->print( out );
529 return out;
530}
531
[0dd3a2f]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.