source: src/SynTree/Expression.cc @ e869d663

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

some print reformatting

  • Property mode set to 100644
File size: 10.6 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 : Wed Aug 12 14:02:45 2015
13// Update Count     : 30
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 "Expression.h"
25#include "Declaration.h"
26#include "Statement.h"
27#include "TypeSubstitution.h"
28#include "utility.h"
29
30
31Expression::Expression( Expression *_aname ) : env( 0 ), argName( _aname ) {}
32
33Expression::Expression( const Expression &other ) : env( maybeClone( other.env ) ) {
34        cloneAll( other.results, results );
35        argName = other.get_argName();
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 << std::string( indent, ' ' ) << "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::UnsignedInt ) );
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::UnsignedInt ) );
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 << std::string( indent, ' ' ) << "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
135AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
136                Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
137}
138
139AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
140                Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
141}
142
143AttrExpr::AttrExpr( const AttrExpr &other ) :
144                Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
145}
146
147AttrExpr::~AttrExpr() {
148        delete attr;
149        delete expr;
150        delete type;
151}
152
153void AttrExpr::print( std::ostream &os, int indent) const {
154        os << std::string( indent, ' ' ) << "Attr ";
155        attr->print( os, indent + 2 );
156        if ( isType || expr ) {
157                os << "applied to: ";
158
159                if (isType)
160                        type->print(os, indent + 2);
161                else
162                        expr->print(os, indent + 2);
163        } // if
164
165        os << std::endl;
166        Expression::print( os, indent );
167}
168
169CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
170        add_result(toType);
171}
172
173CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
174}
175
176CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
177}
178
179CastExpr::~CastExpr() {
180        delete arg;
181}
182
183// CastExpr *CastExpr::clone() const { return 0; }
184
185void CastExpr::print( std::ostream &os, int indent ) const {
186        os << std::string( indent, ' ' ) << "Cast of:" << std::endl;
187        arg->print(os, indent+2);
188        os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
189        if ( results.empty() ) {
190                os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
191        } else {
192                printAll(results, os, indent+2);
193        } // if
194        Expression::print( os, indent );
195}
196
197UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
198                Expression( _aname ), member(_member), aggregate(_aggregate) {}
199
200UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
201                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
202}
203
204UntypedMemberExpr::~UntypedMemberExpr() {
205        delete aggregate;
206}
207
208void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
209        os << std::string( indent, ' ' ) << "Member Expression, with field: " << get_member();
210
211        Expression *agg = get_aggregate();
212        os << std::string( indent, ' ' ) << "from aggregate: ";
213        if (agg != 0) agg->print(os, indent + 2);
214        Expression::print( os, indent );
215}
216
217
218MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
219                Expression( _aname ), member(_member), aggregate(_aggregate) {
220        add_result( member->get_type()->clone() );
221        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
222                (*i)->set_isLvalue( true );
223        } // for
224}
225
226MemberExpr::MemberExpr( const MemberExpr &other ) :
227                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
228}
229
230MemberExpr::~MemberExpr() {
231        delete member;
232        delete aggregate;
233}
234
235void MemberExpr::print( std::ostream &os, int indent ) const {
236        os << std::string( indent, ' ' ) << "Member Expression, with field: " << std::endl;
237
238        assert( member );
239        os << std::string( indent + 2, ' ' );
240        member->print( os, indent + 2 );
241        os << std::endl;
242
243        Expression *agg = get_aggregate();
244        os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
245        if (agg != 0) agg->print(os, indent + 2);
246        Expression::print( os, indent );
247}
248
249
250UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
251
252UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
253                Expression( other ), function( maybeClone( other.function ) ) {
254        cloneAll( other.args, args );
255}
256
257UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
258                Expression( _aname ), function(_function), args(_args) {}
259
260UntypedExpr::~UntypedExpr() {}
261
262void UntypedExpr::print( std::ostream &os, int indent ) const {
263        os << std::string( indent, ' ' ) << "Applying untyped: " << std::endl;
264        function->print(os, indent + 4);
265        os << std::string( indent, ' ' ) << "...to: " << std::endl;
266        printArgs(os, indent + 4);
267        Expression::print( os, indent );
268}
269
270void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
271        std::list<Expression *>::const_iterator i;
272        for (i = args.begin(); i != args.end(); i++)
273                (*i)->print(os, indent);
274}
275
276NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
277
278NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
279}
280
281NameExpr::~NameExpr() {}
282
283void NameExpr::print( std::ostream &os, int indent ) const {
284        os << std::string( indent, ' ' ) << "Name: " << get_name() << std::endl;
285        Expression::print( os, indent );
286}
287
288LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
289                Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
290        add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
291}
292
293LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
294                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
295}
296
297LogicalExpr::~LogicalExpr() {
298        delete arg1;
299        delete arg2;
300}
301
302void LogicalExpr::print( std::ostream &os, int indent )const {
303        os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
304        arg1->print(os);
305        os << " and ";
306        arg2->print(os);
307        os << std::endl;
308        Expression::print( os, indent );
309}
310
311ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
312                Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
313
314ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
315                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
316}
317
318ConditionalExpr::~ConditionalExpr() {
319        delete arg1;
320        delete arg2;
321        delete arg3;
322}
323
324void ConditionalExpr::print( std::ostream &os, int indent ) const {
325        os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl;
326        arg1->print( os, indent+2 );
327        os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
328        arg2->print( os, indent+2 );
329        os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
330        arg3->print( os, indent+2 );
331        os << std::endl;
332        Expression::print( os, indent );
333}
334
335void AsmExpr::print( std::ostream &os, int indent ) const {
336        os << "Asm Expression: " << std::endl;
337        if ( inout ) inout->print( os, indent + 2 );
338        if ( constraint ) constraint->print( os, indent + 2 );
339        if ( operand ) operand->print( os, indent + 2 );
340}
341
342void UntypedValofExpr::print( std::ostream &os, int indent ) const {
343        os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
344        if ( get_body() != 0 )
345                get_body()->print( os, indent + 2 );
346}
347
348// Local Variables: //
349// tab-width: 4 //
350// mode: c++ //
351// compile-command: "make install" //
352// End: //
Note: See TracBrowser for help on using the repository browser.