source: src/SynTree/Expression.cc @ bfebb6c5

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 bfebb6c5 was bfebb6c5, checked in by Aaron Moss <a3moss@…>, 8 years ago

Switch type of {size,align,offset}of to unsigned long

  • Property mode set to 100644
File size: 12.5 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 Dec 09 14:10:29 2015
13// Update Count     : 34
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 ) ), argName( maybeClone( other.get_argName() ) ) {
34        cloneAll( other.results, results );
35}
36
37Expression::~Expression() {
38        delete env;
39        delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix
40        deleteAll( results );
41}
42
43void Expression::add_result( Type *t ) {
44        if ( TupleType *tuple = dynamic_cast< TupleType* >( t ) ) {
45                std::copy( tuple->get_types().begin(), tuple->get_types().end(), back_inserter( results ) );
46        } else {
47                results.push_back(t);
48        } // if
49}
50
51void Expression::print( std::ostream &os, int indent ) const {
52        if ( env ) {
53                os << std::string( indent, ' ' ) << "with environment:" << std::endl;
54                env->print( os, indent+2 );
55        } // if
56
57        if ( argName ) {
58                os << std::string( indent, ' ' ) << "with designator:";
59                argName->print( os, indent+2 );
60        } // if
61}
62
63ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) {
64        add_result( constant.get_type()->clone() );
65}
66
67ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
68}
69
70ConstantExpr::~ConstantExpr() {}
71
72void ConstantExpr::print( std::ostream &os, int indent ) const {
73        os << std::string( indent, ' ' ) << "constant expression " ;
74        constant.print( os );
75        Expression::print( os, indent );
76        os << std::endl;
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 << std::string( indent, ' ' ) << "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 << std::string( indent, ' ' ) << "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
165OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
166                Expression( _aname ), type(type_), member(member_) {
167        add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
168}
169
170OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
171        Expression( other ), type( maybeClone( other.type ) ), member( maybeClone( other.member ) ) {}
172
173OffsetofExpr::~OffsetofExpr() {
174        delete type;
175        delete member;
176}
177
178void OffsetofExpr::print( std::ostream &os, int indent) const {
179        os << std::string( indent, ' ' ) << "Offsetof Expression on member ";
180
181        if ( member ) {
182                os << member->get_name();
183        } else {
184                os << "<NULL>";
185        }
186
187        os << " of ";
188
189        if ( type ) {
190                type->print(os, indent + 2);
191        } else {
192                os << "<NULL>";
193        }
194
195        os << std::endl;
196        Expression::print( os, indent );
197}
198
199AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
200                Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
201}
202
203AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
204                Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
205}
206
207AttrExpr::AttrExpr( const AttrExpr &other ) :
208                Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
209}
210
211AttrExpr::~AttrExpr() {
212        delete attr;
213        delete expr;
214        delete type;
215}
216
217void AttrExpr::print( std::ostream &os, int indent) const {
218        os << std::string( indent, ' ' ) << "Attr ";
219        attr->print( os, indent + 2 );
220        if ( isType || expr ) {
221                os << "applied to: ";
222
223                if (isType)
224                        type->print(os, indent + 2);
225                else
226                        expr->print(os, indent + 2);
227        } // if
228
229        os << std::endl;
230        Expression::print( os, indent );
231}
232
233CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
234        add_result(toType);
235}
236
237CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
238}
239
240CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
241}
242
243CastExpr::~CastExpr() {
244        delete arg;
245}
246
247// CastExpr *CastExpr::clone() const { return 0; }
248
249void CastExpr::print( std::ostream &os, int indent ) const {
250        os << std::string( indent, ' ' ) << "Cast of:" << std::endl;
251        arg->print(os, indent+2);
252        os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
253        if ( results.empty() ) {
254                os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
255        } else {
256                printAll(results, os, indent+2);
257        } // if
258        Expression::print( os, indent );
259}
260
261UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
262                Expression( _aname ), member(_member), aggregate(_aggregate) {}
263
264UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
265                Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
266}
267
268UntypedMemberExpr::~UntypedMemberExpr() {
269        delete aggregate;
270}
271
272void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
273        os << std::string( indent, ' ' ) << "Member Expression, with field: " << get_member();
274
275        Expression *agg = get_aggregate();
276        os << std::string( indent, ' ' ) << "from aggregate: ";
277        if (agg != 0) agg->print(os, indent + 2);
278        Expression::print( os, indent );
279}
280
281
282MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
283                Expression( _aname ), member(_member), aggregate(_aggregate) {
284        add_result( member->get_type()->clone() );
285        for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
286                (*i)->set_isLvalue( true );
287        } // for
288}
289
290MemberExpr::MemberExpr( const MemberExpr &other ) :
291                Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
292}
293
294MemberExpr::~MemberExpr() {
295        delete member;
296        delete aggregate;
297}
298
299void MemberExpr::print( std::ostream &os, int indent ) const {
300        os << std::string( indent, ' ' ) << "Member Expression, with field: " << std::endl;
301
302        assert( member );
303        os << std::string( indent + 2, ' ' );
304        member->print( os, indent + 2 );
305        os << std::endl;
306
307        Expression *agg = get_aggregate();
308        os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
309        if (agg != 0) agg->print(os, indent + 2);
310        Expression::print( os, indent );
311}
312
313
314UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
315
316UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
317                Expression( other ), function( maybeClone( other.function ) ) {
318        cloneAll( other.args, args );
319}
320
321UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
322                Expression( _aname ), function(_function), args(_args) {}
323
324UntypedExpr::~UntypedExpr() {}
325
326void UntypedExpr::print( std::ostream &os, int indent ) const {
327        os << std::string( indent, ' ' ) << "Applying untyped: " << std::endl;
328        function->print(os, indent + 4);
329        os << std::string( indent, ' ' ) << "...to: " << std::endl;
330        printArgs(os, indent + 4);
331        Expression::print( os, indent );
332}
333
334void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
335        std::list<Expression *>::const_iterator i;
336        for (i = args.begin(); i != args.end(); i++)
337                (*i)->print(os, indent);
338}
339
340NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
341
342NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
343}
344
345NameExpr::~NameExpr() {}
346
347void NameExpr::print( std::ostream &os, int indent ) const {
348        os << std::string( indent, ' ' ) << "Name: " << get_name() << std::endl;
349        Expression::print( os, indent );
350}
351
352LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
353                Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
354        add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
355}
356
357LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
358                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
359}
360
361LogicalExpr::~LogicalExpr() {
362        delete arg1;
363        delete arg2;
364}
365
366void LogicalExpr::print( std::ostream &os, int indent )const {
367        os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
368        arg1->print(os);
369        os << " and ";
370        arg2->print(os);
371        os << std::endl;
372        Expression::print( os, indent );
373}
374
375ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
376                Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
377
378ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
379                Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
380}
381
382ConditionalExpr::~ConditionalExpr() {
383        delete arg1;
384        delete arg2;
385        delete arg3;
386}
387
388void ConditionalExpr::print( std::ostream &os, int indent ) const {
389        os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl;
390        arg1->print( os, indent+2 );
391        os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
392        arg2->print( os, indent+2 );
393        os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
394        arg3->print( os, indent+2 );
395        os << std::endl;
396        Expression::print( os, indent );
397}
398
399void AsmExpr::print( std::ostream &os, int indent ) const {
400        os << "Asm Expression: " << std::endl;
401        if ( inout ) inout->print( os, indent + 2 );
402        if ( constraint ) constraint->print( os, indent + 2 );
403        if ( operand ) operand->print( os, indent + 2 );
404}
405
406void UntypedValofExpr::print( std::ostream &os, int indent ) const {
407        os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
408        if ( get_body() != 0 )
409                get_body()->print( os, indent + 2 );
410}
411
412std::ostream & operator<<( std::ostream & out, Expression * expr ) {
413        expr->print( out );
414        return out;
415}
416
417// Local Variables: //
418// tab-width: 4 //
419// mode: c++ //
420// compile-command: "make install" //
421// End: //
Note: See TracBrowser for help on using the repository browser.