source: src/SynTree/Expression.cc@ 4ef8fb3

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 4ef8fb3 was 3da470c, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

Merge branch 'master' of plg2:software/cfa/cfa-cc

  • 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 : Peter A. Buhr
12// Last Modified On : Fri Apr 8 17:16:23 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 << 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::LongUnsignedInt ) );
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::LongUnsignedInt ) );
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
135AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
136 Expression( _aname ), expr(expr_), type(0), isType(false) {
137 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
138}
139
140AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
141 Expression( _aname ), expr(0), type(type_), isType(true) {
142 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
143}
144
145AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
146 Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
147}
148
149AlignofExpr::~AlignofExpr() {
150 delete expr;
151 delete type;
152}
153
154void AlignofExpr::print( std::ostream &os, int indent) const {
155 os << std::string( indent, ' ' ) << "Alignof Expression on: ";
156
157 if (isType)
158 type->print(os, indent + 2);
159 else
160 expr->print(os, indent + 2);
161
162 os << std::endl;
163 Expression::print( os, indent );
164}
165
166UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
167 Expression( _aname ), type(type_), member(member_) {
168 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
169}
170
171UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
172 Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
173
174UntypedOffsetofExpr::~UntypedOffsetofExpr() {
175 delete type;
176}
177
178void UntypedOffsetofExpr::print( std::ostream &os, int indent) const {
179 os << std::string( indent, ' ' ) << "Untyped Offsetof Expression on member " << member << " of ";
180
181 if ( type ) {
182 type->print(os, indent + 2);
183 } else {
184 os << "<NULL>";
185 }
186
187 os << std::endl;
188 Expression::print( os, indent );
189}
190
191OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
192 Expression( _aname ), type(type_), member(member_) {
193 add_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
194}
195
196OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
197 Expression( other ), type( maybeClone( other.type ) ), member( maybeClone( other.member ) ) {}
198
199OffsetofExpr::~OffsetofExpr() {
200 delete type;
201 delete member;
202}
203
204void OffsetofExpr::print( std::ostream &os, int indent) const {
205 os << std::string( indent, ' ' ) << "Offsetof Expression on member ";
206
207 if ( member ) {
208 os << member->get_name();
209 } else {
210 os << "<NULL>";
211 }
212
213 os << " of ";
214
215 if ( type ) {
216 type->print(os, indent + 2);
217 } else {
218 os << "<NULL>";
219 }
220
221 os << std::endl;
222 Expression::print( os, indent );
223}
224
225OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
226 add_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
227}
228
229OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
230
231OffsetPackExpr::~OffsetPackExpr() { delete type; }
232
233void OffsetPackExpr::print( std::ostream &os, int indent ) const {
234 os << std::string( indent, ' ' ) << "Offset pack expression on ";
235
236 if ( type ) {
237 type->print(os, indent + 2);
238 } else {
239 os << "<NULL>";
240 }
241
242 os << std::endl;
243 Expression::print( os, indent );
244}
245
246AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
247 Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
248}
249
250AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
251 Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
252}
253
254AttrExpr::AttrExpr( const AttrExpr &other ) :
255 Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
256}
257
258AttrExpr::~AttrExpr() {
259 delete attr;
260 delete expr;
261 delete type;
262}
263
264void AttrExpr::print( std::ostream &os, int indent) const {
265 os << std::string( indent, ' ' ) << "Attr ";
266 attr->print( os, indent + 2 );
267 if ( isType || expr ) {
268 os << "applied to: ";
269
270 if (isType)
271 type->print(os, indent + 2);
272 else
273 expr->print(os, indent + 2);
274 } // if
275
276 os << std::endl;
277 Expression::print( os, indent );
278}
279
280CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
281 add_result(toType);
282}
283
284CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
285}
286
287CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
288}
289
290CastExpr::~CastExpr() {
291 delete arg;
292}
293
294// CastExpr *CastExpr::clone() const { return 0; }
295
296void CastExpr::print( std::ostream &os, int indent ) const {
297 os << std::string( indent, ' ' ) << "Cast of:" << std::endl;
298 arg->print(os, indent+2);
299 os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
300 if ( results.empty() ) {
301 os << std::string( indent+2, ' ' ) << "nothing" << std::endl;
302 } else {
303 printAll(results, os, indent+2);
304 } // if
305 Expression::print( os, indent );
306}
307
308UntypedMemberExpr::UntypedMemberExpr( std::string _member, Expression *_aggregate, Expression *_aname ) :
309 Expression( _aname ), member(_member), aggregate(_aggregate) {}
310
311UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
312 Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
313}
314
315UntypedMemberExpr::~UntypedMemberExpr() {
316 delete aggregate;
317}
318
319void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
320 os << std::string( indent, ' ' ) << "Member Expression, with field: " << get_member();
321
322 Expression *agg = get_aggregate();
323 os << std::string( indent, ' ' ) << "from aggregate: ";
324 if (agg != 0) agg->print(os, indent + 2);
325 Expression::print( os, indent );
326}
327
328
329MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
330 Expression( _aname ), member(_member), aggregate(_aggregate) {
331 add_result( member->get_type()->clone() );
332 for ( std::list< Type* >::iterator i = get_results().begin(); i != get_results().end(); ++i ) {
333 (*i)->set_isLvalue( true );
334 } // for
335}
336
337MemberExpr::MemberExpr( const MemberExpr &other ) :
338 Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
339}
340
341MemberExpr::~MemberExpr() {
342 delete member;
343 delete aggregate;
344}
345
346void MemberExpr::print( std::ostream &os, int indent ) const {
347 os << std::string( indent, ' ' ) << "Member Expression, with field: " << std::endl;
348
349 assert( member );
350 os << std::string( indent + 2, ' ' );
351 member->print( os, indent + 2 );
352 os << std::endl;
353
354 Expression *agg = get_aggregate();
355 os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
356 if (agg != 0) agg->print(os, indent + 2);
357 Expression::print( os, indent );
358}
359
360
361UntypedExpr::UntypedExpr( Expression *_function, Expression *_aname ) : Expression( _aname ), function( _function ) {}
362
363UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
364 Expression( other ), function( maybeClone( other.function ) ) {
365 cloneAll( other.args, args );
366}
367
368UntypedExpr::UntypedExpr( Expression *_function, std::list<Expression *> &_args, Expression *_aname ) :
369 Expression( _aname ), function(_function), args(_args) {}
370
371UntypedExpr::~UntypedExpr() {}
372
373void UntypedExpr::print( std::ostream &os, int indent ) const {
374 os << std::string( indent, ' ' ) << "Applying untyped: " << std::endl;
375 function->print(os, indent + 4);
376 os << std::string( indent, ' ' ) << "...to: " << std::endl;
377 printArgs(os, indent + 4);
378 Expression::print( os, indent );
379}
380
381void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
382 std::list<Expression *>::const_iterator i;
383 for (i = args.begin(); i != args.end(); i++)
384 (*i)->print(os, indent);
385}
386
387NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {}
388
389NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
390}
391
392NameExpr::~NameExpr() {}
393
394void NameExpr::print( std::ostream &os, int indent ) const {
395 os << std::string( indent, ' ' ) << "Name: " << get_name() << std::endl;
396 Expression::print( os, indent );
397}
398
399LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
400 Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
401 add_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
402}
403
404LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
405 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
406}
407
408LogicalExpr::~LogicalExpr() {
409 delete arg1;
410 delete arg2;
411}
412
413void LogicalExpr::print( std::ostream &os, int indent )const {
414 os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
415 arg1->print(os);
416 os << " and ";
417 arg2->print(os);
418 os << std::endl;
419 Expression::print( os, indent );
420}
421
422ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
423 Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
424
425ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
426 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
427}
428
429ConditionalExpr::~ConditionalExpr() {
430 delete arg1;
431 delete arg2;
432 delete arg3;
433}
434
435void ConditionalExpr::print( std::ostream &os, int indent ) const {
436 os << std::string( indent, ' ' ) << "Conditional expression on: " << std::endl;
437 arg1->print( os, indent+2 );
438 os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
439 arg2->print( os, indent+2 );
440 os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
441 arg3->print( os, indent+2 );
442 os << std::endl;
443 Expression::print( os, indent );
444}
445
446AsmExpr::AsmExpr( const AsmExpr & other ) : inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
447
448
449void AsmExpr::print( std::ostream &os, int indent ) const {
450 os << "Asm Expression: " << std::endl;
451 if ( inout ) inout->print( os, indent + 2 );
452 if ( constraint ) constraint->print( os, indent + 2 );
453 if ( operand ) operand->print( os, indent + 2 );
454}
455
456UntypedValofExpr::UntypedValofExpr( const UntypedValofExpr & other ) : Expression( other ), body ( maybeClone( other.body ) ) {}
457
458UntypedValofExpr::~UntypedValofExpr() { delete body; }
459
460void UntypedValofExpr::print( std::ostream &os, int indent ) const {
461 os << std::string( indent, ' ' ) << "Valof Expression: " << std::endl;
462 if ( get_body() != 0 )
463 get_body()->print( os, indent + 2 );
464}
465
466
467CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : type( type ), initializer( initializer ) {
468 add_result( type->clone() );
469}
470
471CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), type( maybeClone( other.type ) ), initializer( maybeClone( other.initializer ) ) {}
472
473CompoundLiteralExpr::~CompoundLiteralExpr() {
474 delete initializer;
475 delete type;
476}
477
478void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
479 os << "Compound Literal Expression: " << std::endl;
480 if ( type ) type->print( os, indent + 2 );
481 if ( initializer ) initializer->print( os, indent + 2 );
482}
483
484
485std::ostream & operator<<( std::ostream & out, Expression * expr ) {
486 expr->print( out );
487 return out;
488}
489
490// Local Variables: //
491// tab-width: 4 //
492// mode: c++ //
493// compile-command: "make install" //
494// End: //
Note: See TracBrowser for help on using the repository browser.