source: translator/SynTree/Expression.cc @ 42dcae7

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 42dcae7 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 10.4 KB
Line 
1#include <iostream>
2#include <cassert>
3#include <list>
4#include <algorithm>
5
6#include <iterator>
7
8#include "Type.h"
9#include "Expression.h"
10#include "Declaration.h"
11#include "Statement.h"
12#include "TypeSubstitution.h"
13#include "utility.h"
14
15
16//*** Expression
17Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {}
18Expression::Expression( const Expression &other )
19    : env( maybeClone( other.env ) )
20 {
21     cloneAll( other.results, results );
22     argName = other.get_argName();
23 }
24Expression::~Expression()
25{
26    delete env;
27    // delete argName;  // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix
28    deleteAll( results );
29}
30
31void
32Expression::add_result( Type *t )
33{
34    if( TupleType *tuple = dynamic_cast< TupleType* >( t ) ) {
35        std::copy( tuple->get_types().begin(), tuple->get_types().end(), back_inserter( results ) );
36    } else {
37        results.push_back(t);
38    }
39}
40
41void Expression::print(std::ostream &os, int indent) const {
42    if( env ) {
43        os << std::string(indent, ' ') << "with environment:" << std::endl;
44        env->print( os, indent+2 );
45    }
46
47    if( argName ) {
48        os << std::string(indent, ' ') << "with designator:";
49        argName->print( os, indent+2 );
50    }
51}
52
53//*** ConstantExpr
54ConstantExpr::ConstantExpr(Constant _c, Expression *_aname ):
55    Expression( _aname ), constant(_c)
56{
57    add_result( constant.get_type()->clone() );
58}
59
60ConstantExpr::ConstantExpr( const ConstantExpr &other)
61    : Expression( other ), constant( other.constant )
62{}
63
64ConstantExpr::~ConstantExpr() {}
65
66void ConstantExpr::print(std::ostream &os, int indent) const {
67    os << std::string(indent, ' ') << "Constant Expression: " ;
68    constant.print(os);
69    os << std::endl;
70    Expression::print( os, indent );
71}
72
73//*** VariableExpr
74VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var )
75{
76    add_result( var->get_type()->clone() );
77    for( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
78        (*i)->set_isLvalue( true );
79    }
80}
81
82VariableExpr::VariableExpr( const VariableExpr &other )
83    : Expression( other ), var( other.var )
84{
85}
86
87VariableExpr::~VariableExpr()
88{
89    // don't delete the declaration, since it points somewhere else in the tree
90}
91
92void VariableExpr::print(std::ostream &os, int indent) const {
93    os << std::string(indent, ' ') << "Variable Expression: ";
94
95    Declaration *decl = get_var();
96    // if( decl != 0) decl->print(os, indent + 2);
97    if( decl != 0) decl->printShort(os, indent + 2);
98    os << std::endl;
99    Expression::print( os, indent );
100}
101
102//*** SizeofExpr
103SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
104    Expression( _aname ), expr(expr_), type(0), isType(false)
105{
106    add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) );
107}
108
109SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
110    Expression( _aname ), expr(0), type(type_), isType(true)
111{
112    add_result( new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ) );
113}
114
115SizeofExpr::SizeofExpr( const SizeofExpr &other )
116    : Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType )
117{
118}
119
120SizeofExpr::~SizeofExpr(){
121    delete expr;
122    delete type;
123}
124
125void SizeofExpr::print( std::ostream &os, int indent) const {
126    os << std::string(indent, ' ') << "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
137//*** AttrExpr
138AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
139    Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false)
140{
141}
142
143AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
144    Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true)
145{
146}
147
148AttrExpr::AttrExpr( const AttrExpr &other )
149    : Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType )
150{
151}
152
153AttrExpr::~AttrExpr(){
154    delete attr;
155    delete expr;
156    delete type;
157}
158
159void AttrExpr::print( std::ostream &os, int indent) const {
160    os << std::string(indent, ' ') << "Attr ";
161    attr->print( os, indent + 2 );
162    if( isType || expr ) {
163        os << "applied to: ";
164
165        if(isType)
166            type->print(os, indent + 2);
167        else
168            expr->print(os, indent + 2);
169    }
170
171    os << std::endl;
172    Expression::print( os, indent );
173}
174
175//*** CastExpr
176CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_)
177{
178    add_result(toType);
179}
180
181CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
182}
183
184CastExpr::CastExpr( const CastExpr &other )
185    : Expression( other ), arg( maybeClone( other.arg ) )
186{
187}
188
189CastExpr::~CastExpr() {
190    delete arg;
191}
192
193// CastExpr *CastExpr::clone() const { return 0; }
194
195void CastExpr::print( std::ostream &os, int indent ) const {
196    os << std::string(indent, ' ') << "Cast of:" << std::endl;
197    arg->print(os, indent+2);
198    os << std::endl << std::string(indent, ' ') << "to:" << std::endl;
199    if( results.empty() ) {
200        os << std::string(indent+2, ' ') << "nothing" << std::endl;
201    } else {
202        printAll(results, os, indent+2);
203    }
204    Expression::print( os, indent );
205}
206
207//*** UntypedMemberExpr
208UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
209    Expression( _aname ), member(_member), aggregate(_aggregate) {}
210
211UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other )
212    : Expression( other ),   member( other.member ), aggregate( maybeClone( other.aggregate ) )
213{
214}
215
216UntypedMemberExpr::~UntypedMemberExpr() {
217    delete aggregate;
218}
219
220void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
221    os << std::string(indent, ' ') << "Member Expression, with field: " << get_member();
222
223    Expression *agg = get_aggregate();
224    os << std::string(indent, ' ') << "from aggregate: ";
225    if (agg != 0) agg->print(os, indent + 2);
226    Expression::print( os, indent );
227}
228
229
230//*** MemberExpr
231MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
232    Expression( _aname ), member(_member), aggregate(_aggregate)
233{
234    add_result( member->get_type()->clone() );
235    for( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
236        (*i)->set_isLvalue( true );
237    }
238}
239
240MemberExpr::MemberExpr( const MemberExpr &other )
241    : Expression( other ),   member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) )
242{
243}
244
245MemberExpr::~MemberExpr() {
246    delete member;
247    delete aggregate;
248}
249
250void MemberExpr::print( std::ostream &os, int indent ) const {
251    os << std::string(indent, ' ') << "Member Expression, with field: " << std::endl;
252
253    assert( member );
254    os << std::string(indent + 2, ' ');
255    member->print( os, indent + 2 );
256    os << std::endl;
257
258    Expression *agg = get_aggregate();
259    os << std::string(indent, ' ') << "from aggregate: " << std::endl;
260    if (agg != 0) agg->print(os, indent + 2);
261    Expression::print( os, indent );
262}
263
264
265//*** UntypedExpr
266UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
267
268UntypedExpr::UntypedExpr( const UntypedExpr &other )
269    : Expression( other ), function( maybeClone( other.function ) )
270{
271    cloneAll( other.args, args );
272}
273
274UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
275    Expression( _aname ), function(_function), args(_args) {}
276
277UntypedExpr::~UntypedExpr() {}
278
279void UntypedExpr::print( std::ostream &os, int indent ) const {
280    os << std::string(indent, ' ') << "Applying untyped: " << std::endl;
281    function->print(os, indent + 4);
282    os << "\r" << std::string(indent, ' ') << "...to: " << std::endl;
283    printArgs(os, indent + 4);
284    Expression::print( os, indent );
285}
286
287void UntypedExpr::printArgs(std::ostream &os, int indent ) const {
288    std::list<Expression *>::const_iterator i;
289    for(i = args.begin(); i != args.end(); i++)
290        (*i)->print(os, indent);
291}
292
293//*** NameExpr
294NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
295
296NameExpr::NameExpr( const NameExpr &other )
297    : Expression( other ), name( other.name )
298{
299}
300
301NameExpr::~NameExpr() {}
302
303void NameExpr::print( std::ostream &os, int indent ) const {
304    os << std::string(indent, ' ') << "Name: " << get_name() << std::endl;
305    Expression::print( os, indent );
306}
307
308//*** LogicalExpr
309LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
310    Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp)
311{
312    add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
313}
314
315LogicalExpr::LogicalExpr( const LogicalExpr &other )
316    : Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd )
317{
318}
319
320LogicalExpr::~LogicalExpr(){
321    delete arg1;
322    delete arg2;
323}
324
325void LogicalExpr::print( std::ostream &os, int indent )const {
326    os << std::string(indent, ' ') << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
327    arg1->print(os);
328    os << " and ";
329    arg2->print(os);
330    os << std::endl;
331    Expression::print( os, indent );
332}
333
334//*** ConditionalExpr
335ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
336    Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
337
338ConditionalExpr::ConditionalExpr( const ConditionalExpr &other )
339     : Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) )
340 {
341 }
342
343ConditionalExpr::~ConditionalExpr() {
344    delete arg1;
345    delete arg2;
346    delete arg3;
347}
348
349void ConditionalExpr::print( std::ostream &os, int indent ) const {
350    os << std::string(indent, ' ') << "Conditional expression on: " << std::endl;
351    arg1->print( os, indent+2 );
352    os << std::string(indent, ' ') << "First alternative:" << std::endl;
353    arg2->print( os, indent+2 );
354    os << std::string(indent, ' ') << "Second alternative:" << std::endl;
355    arg3->print( os, indent+2 );
356    os << std::endl;
357    Expression::print( os, indent );
358}
359
360//*** UntypedValofExpr
361void UntypedValofExpr::print( std::ostream &os, int indent ) const
362{
363    os << std::string(indent, ' ') << "Valof Expression: " << std::endl;
364    if( get_body() != 0 )
365        get_body()->print( os, indent + 2 );
366}
367
368
Note: See TracBrowser for help on using the repository browser.