source: src/SynTree/Expression.cc@ 1ae06fa

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 1ae06fa was 9236060, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 23.7 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 : Tue Jul 25 14:15:47 2017
13// Update Count : 54
14//
15
16#include <iostream>
17#include <cassert>
18#include <list>
19#include <algorithm>
20
21#include <iterator>
22
23#include "Declaration.h"
24#include "Expression.h"
25#include "Initializer.h"
26#include "Statement.h"
27#include "Type.h"
28#include "TypeSubstitution.h"
29#include "VarExprReplacer.h"
30
31#include "Common/utility.h"
32#include "Common/PassVisitor.h"
33
34#include "InitTweak/InitTweak.h"
35
36#include "GenPoly/Lvalue.h"
37
38Expression::Expression( Expression *_aname ) : result( 0 ), env( 0 ), argName( _aname ) {}
39
40Expression::Expression( const Expression &other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ), extension( other.extension ) {
41}
42
43Expression::~Expression() {
44 delete env;
45 delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix
46 delete result;
47}
48
49void Expression::print( std::ostream &os, int indent ) const {
50 if ( env ) {
51 os << std::string( indent, ' ' ) << "with environment:" << std::endl;
52 env->print( os, indent+2 );
53 } // if
54
55 if ( argName ) {
56 os << std::string( indent, ' ' ) << "with designator:";
57 argName->print( os, indent+2 );
58 } // if
59
60 if ( extension ) {
61 os << std::string( indent, ' ' ) << "with extension:";
62 } // if
63}
64
65ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) {
66 set_result( constant.get_type()->clone() );
67}
68
69ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) {
70}
71
72ConstantExpr::~ConstantExpr() {}
73
74void ConstantExpr::print( std::ostream &os, int indent ) const {
75 os << "constant expression " ;
76 constant.print( os );
77 Expression::print( os, indent );
78}
79
80VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) {
81 assert( var );
82 assert( var->get_type() );
83 Type * type = var->get_type()->clone();
84 type->set_lvalue( true );
85 set_result( type );
86}
87
88VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) {
89}
90
91VariableExpr::~VariableExpr() {
92 // don't delete the declaration, since it points somewhere else in the tree
93}
94
95VariableExpr * VariableExpr::functionPointer( FunctionDecl * func ) {
96 VariableExpr * funcExpr = new VariableExpr( func );
97 funcExpr->set_result( new PointerType( Type::Qualifiers(), funcExpr->get_result() ) );
98 return funcExpr;
99}
100
101void VariableExpr::print( std::ostream &os, int indent ) const {
102 os << "Variable Expression: ";
103
104 Declaration *decl = get_var();
105 if ( decl != 0) decl->printShort(os, indent + 2);
106 os << std::endl;
107 Expression::print( os, indent );
108}
109
110SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) :
111 Expression( _aname ), expr(expr_), type(0), isType(false) {
112 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
113}
114
115SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) :
116 Expression( _aname ), expr(0), type(type_), isType(true) {
117 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
118}
119
120SizeofExpr::SizeofExpr( const SizeofExpr &other ) :
121 Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
122}
123
124SizeofExpr::~SizeofExpr() {
125 delete expr;
126 delete type;
127}
128
129void SizeofExpr::print( std::ostream &os, int indent) const {
130 os << "Sizeof Expression on: ";
131
132 if (isType)
133 type->print(os, indent + 2);
134 else
135 expr->print(os, indent + 2);
136
137 os << std::endl;
138 Expression::print( os, indent );
139}
140
141AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) :
142 Expression( _aname ), expr(expr_), type(0), isType(false) {
143 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
144}
145
146AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) :
147 Expression( _aname ), expr(0), type(type_), isType(true) {
148 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
149}
150
151AlignofExpr::AlignofExpr( const AlignofExpr &other ) :
152 Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
153}
154
155AlignofExpr::~AlignofExpr() {
156 delete expr;
157 delete type;
158}
159
160void AlignofExpr::print( std::ostream &os, int indent) const {
161 os << "Alignof Expression on: ";
162
163 if (isType)
164 type->print(os, indent + 2);
165 else
166 expr->print(os, indent + 2);
167
168 os << std::endl;
169 Expression::print( os, indent );
170}
171
172UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) :
173 Expression( _aname ), type(type_), member(member_) {
174 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
175}
176
177UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) :
178 Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
179
180UntypedOffsetofExpr::~UntypedOffsetofExpr() {
181 delete type;
182}
183
184void UntypedOffsetofExpr::print( std::ostream &os, int indent) const {
185 os << std::string( indent, ' ' ) << "Untyped Offsetof Expression on member " << member << " of ";
186
187 if ( type ) {
188 type->print(os, indent + 2);
189 } else {
190 os << "<NULL>";
191 }
192
193 os << std::endl;
194 Expression::print( os, indent );
195}
196
197OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) :
198 Expression( _aname ), type(type_), member(member_) {
199 set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) );
200}
201
202OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) :
203 Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {}
204
205OffsetofExpr::~OffsetofExpr() {
206 delete type;
207}
208
209void OffsetofExpr::print( std::ostream &os, int indent) const {
210 os << std::string( indent, ' ' ) << "Offsetof Expression on member ";
211
212 if ( member ) {
213 os << member->get_name();
214 } else {
215 os << "<NULL>";
216 }
217
218 os << " of ";
219
220 if ( type ) {
221 type->print(os, indent + 2);
222 } else {
223 os << "<NULL>";
224 }
225
226 os << std::endl;
227 Expression::print( os, indent );
228}
229
230OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) {
231 set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) );
232}
233
234OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {}
235
236OffsetPackExpr::~OffsetPackExpr() { delete type; }
237
238void OffsetPackExpr::print( std::ostream &os, int indent ) const {
239 os << std::string( indent, ' ' ) << "Offset pack expression on ";
240
241 if ( type ) {
242 type->print(os, indent + 2);
243 } else {
244 os << "<NULL>";
245 }
246
247 os << std::endl;
248 Expression::print( os, indent );
249}
250
251AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) :
252 Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) {
253}
254
255AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) :
256 Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) {
257}
258
259AttrExpr::AttrExpr( const AttrExpr &other ) :
260 Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) {
261}
262
263AttrExpr::~AttrExpr() {
264 delete attr;
265 delete expr;
266 delete type;
267}
268
269void AttrExpr::print( std::ostream &os, int indent) const {
270 os << "Attr ";
271 attr->print( os, indent + 2 );
272 if ( isType || expr ) {
273 os << "applied to: ";
274
275 if (isType)
276 type->print(os, indent + 2);
277 else
278 expr->print(os, indent + 2);
279 } // if
280
281 os << std::endl;
282 Expression::print( os, indent );
283}
284
285CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) {
286 set_result(toType);
287}
288
289CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) {
290 set_result( new VoidType( Type::Qualifiers() ) );
291}
292
293CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
294}
295
296CastExpr::~CastExpr() {
297 delete arg;
298}
299
300void CastExpr::print( std::ostream &os, int indent ) const {
301 os << "Cast of:" << std::endl << std::string( indent+2, ' ' );
302 arg->print(os, indent+2);
303 os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
304 os << std::string( indent+2, ' ' );
305 if ( result->isVoid() ) {
306 os << "nothing";
307 } else {
308 result->print( os, indent+2 );
309 } // if
310 os << std::endl;
311 Expression::print( os, indent );
312}
313
314VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) {
315 set_result(toType);
316}
317
318VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
319}
320
321VirtualCastExpr::~VirtualCastExpr() {
322 delete arg;
323}
324
325void VirtualCastExpr::print( std::ostream &os, int indent ) const {
326 os << "Virtual Cast of:" << std::endl << std::string( indent+2, ' ' );
327 arg->print(os, indent+2);
328 os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl;
329 os << std::string( indent+2, ' ' );
330 if ( ! result ) {
331 os << "unknown";
332 } else {
333 result->print( os, indent+2 );
334 } // if
335 os << std::endl;
336 Expression::print( os, indent );
337}
338
339UntypedMemberExpr::UntypedMemberExpr( Expression * _member, Expression *_aggregate, Expression *_aname ) :
340 Expression( _aname ), member(_member), aggregate(_aggregate) {}
341
342UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) :
343 Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) {
344}
345
346UntypedMemberExpr::~UntypedMemberExpr() {
347 delete aggregate;
348 delete member;
349}
350
351void UntypedMemberExpr::print( std::ostream &os, int indent ) const {
352 os << "Untyped Member Expression, with field: " << std::endl;
353 os << std::string( indent+2, ' ' );
354 get_member()->print(os, indent+4);
355 os << std::string( indent+2, ' ' );
356
357 Expression *agg = get_aggregate();
358 os << "from aggregate: " << std::endl;
359 if (agg != 0) {
360 os << std::string( indent + 4, ' ' );
361 agg->print(os, indent + 4);
362 }
363 os << std::string( indent+2, ' ' );
364 Expression::print( os, indent );
365}
366
367namespace {
368 TypeSubstitution makeSub( Type * t ) {
369 if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( t ) ) {
370 return makeSub( refType->get_base() );
371 } else if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) {
372 return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() );
373 } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) {
374 return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() );
375 } else {
376 assertf( false, "makeSub expects struct or union type for aggregate, but got: %s", toString( t ).c_str() );
377 }
378 }
379}
380
381
382MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) :
383 Expression( _aname ), member(_member), aggregate(_aggregate) {
384
385 TypeSubstitution sub( makeSub( aggregate->get_result() ) );
386 Type * res = member->get_type()->clone();
387 sub.apply( res );
388 set_result( res );
389 get_result()->set_lvalue( true );
390}
391
392MemberExpr::MemberExpr( const MemberExpr &other ) :
393 Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) {
394}
395
396MemberExpr::~MemberExpr() {
397 // don't delete the member declaration, since it points somewhere else in the tree
398 delete aggregate;
399}
400
401void MemberExpr::print( std::ostream &os, int indent ) const {
402 os << "Member Expression, with field: " << std::endl;
403
404 assert( member );
405 os << std::string( indent + 2, ' ' );
406 member->print( os, indent + 2 );
407 os << std::endl;
408
409 Expression *agg = get_aggregate();
410 os << std::string( indent, ' ' ) << "from aggregate: " << std::endl;
411 if (agg != 0) {
412 os << std::string( indent + 2, ' ' );
413 agg->print(os, indent + 2);
414 }
415 os << std::string( indent+2, ' ' );
416 Expression::print( os, indent );
417}
418
419UntypedExpr::UntypedExpr( Expression *_function, const std::list<Expression *> &_args, Expression *_aname ) :
420 Expression( _aname ), function(_function), args(_args) {}
421
422UntypedExpr::UntypedExpr( const UntypedExpr &other ) :
423 Expression( other ), function( maybeClone( other.function ) ) {
424 cloneAll( other.args, args );
425}
426
427UntypedExpr::~UntypedExpr() {
428 delete function;
429 deleteAll( args );
430}
431
432UntypedExpr * UntypedExpr::createDeref( Expression * expr ) {
433 UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } );
434 if ( Type * type = expr->get_result() ) {
435 Type * base = InitTweak::getPointerBase( type );
436 assertf( base, "expected pointer type in dereference (type was %s)", toString( type ).c_str() );
437 ret->set_result( base->clone() );
438 if ( GenPoly::referencesPermissable() ) {
439 // if references are still allowed in the AST, dereference returns a reference
440 ret->set_result( new ReferenceType( Type::Qualifiers(), ret->get_result() ) );
441 } else {
442 // references have been removed, in which case dereference returns an lvalue of the base type.
443 ret->get_result()->set_lvalue( true );
444 }
445 }
446 return ret;
447}
448
449UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) {
450 assert( arg1 && arg2 );
451 UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } );
452 if ( arg1->get_result() && arg2->get_result() ) {
453 // if both expressions are typed, assumes that this assignment is a C bitwise assignment,
454 // so the result is the type of the RHS
455 ret->set_result( arg2->get_result()->clone() );
456 }
457 return ret;
458}
459
460
461void UntypedExpr::print( std::ostream &os, int indent ) const {
462 os << "Applying untyped: " << std::endl;
463 os << std::string( indent+2, ' ' );
464 function->print(os, indent + 2);
465 os << std::string( indent, ' ' ) << "...to: " << std::endl;
466 printAll(args, os, indent + 2);
467 Expression::print( os, indent );
468}
469
470void UntypedExpr::printArgs( std::ostream &os, int indent ) const {
471 std::list<Expression *>::const_iterator i;
472 for (i = args.begin(); i != args.end(); i++) {
473 os << std::string(indent, ' ' );
474 (*i)->print(os, indent);
475 }
476}
477
478NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) {
479 assertf(_name != "0", "Zero is not a valid name\n");
480 assertf(_name != "1", "One is not a valid name\n");
481}
482
483NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) {
484}
485
486NameExpr::~NameExpr() {}
487
488void NameExpr::print( std::ostream &os, int indent ) const {
489 os << "Name: " << get_name() << std::endl;
490 Expression::print( os, indent );
491}
492
493LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) :
494 Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) {
495 set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
496}
497
498LogicalExpr::LogicalExpr( const LogicalExpr &other ) :
499 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) {
500}
501
502LogicalExpr::~LogicalExpr() {
503 delete arg1;
504 delete arg2;
505}
506
507void LogicalExpr::print( std::ostream &os, int indent )const {
508 os << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: ";
509 arg1->print(os);
510 os << " and ";
511 arg2->print(os);
512 os << std::endl;
513 Expression::print( os, indent );
514}
515
516ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) :
517 Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {}
518
519ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) :
520 Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) {
521}
522
523ConditionalExpr::~ConditionalExpr() {
524 delete arg1;
525 delete arg2;
526 delete arg3;
527}
528
529void ConditionalExpr::print( std::ostream &os, int indent ) const {
530 os << "Conditional expression on: " << std::endl;
531 os << std::string( indent+2, ' ' );
532 arg1->print( os, indent+2 );
533 os << std::string( indent, ' ' ) << "First alternative:" << std::endl;
534 os << std::string( indent+2, ' ' );
535 arg2->print( os, indent+2 );
536 os << std::string( indent, ' ' ) << "Second alternative:" << std::endl;
537 os << std::string( indent+2, ' ' );
538 arg3->print( os, indent+2 );
539 os << std::endl;
540 Expression::print( os, indent );
541}
542
543AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {}
544
545
546void AsmExpr::print( std::ostream &os, int indent ) const {
547 os << "Asm Expression: " << std::endl;
548 if ( inout ) inout->print( os, indent + 2 );
549 if ( constraint ) constraint->print( os, indent + 2 );
550 if ( operand ) operand->print( os, indent + 2 );
551}
552
553
554ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) {
555 assert( callExpr );
556 assert( callExpr->has_result() );
557 set_result( callExpr->get_result()->clone() );
558}
559
560ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
561 cloneAll( other.tempDecls, tempDecls );
562 cloneAll( other.returnDecls, returnDecls );
563 cloneAll( other.dtors, dtors );
564}
565
566ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() {
567 set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment
568 delete callExpr;
569 deleteAll( tempDecls );
570 deleteAll( returnDecls );
571 deleteAll( dtors );
572}
573
574void ImplicitCopyCtorExpr::print( std::ostream &os, int indent ) const {
575 os << "Implicit Copy Constructor Expression: " << std::endl;
576 assert( callExpr );
577 os << std::string( indent+2, ' ' );
578 callExpr->print( os, indent + 2 );
579 os << std::endl << std::string( indent, ' ' ) << "with temporaries:" << std::endl;
580 printAll(tempDecls, os, indent+2);
581 os << std::endl << std::string( indent, ' ' ) << "with return temporaries:" << std::endl;
582 printAll(returnDecls, os, indent+2);
583 Expression::print( os, indent );
584}
585
586
587ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) {
588 // allow resolver to type a constructor used as an expression as if it has the same type as its first argument
589 assert( callExpr );
590 Expression * arg = InitTweak::getCallArg( callExpr, 0 );
591 assert( arg );
592 set_result( maybeClone( arg->get_result() ) );
593}
594
595ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) {
596}
597
598ConstructorExpr::~ConstructorExpr() {
599 delete callExpr;
600}
601
602void ConstructorExpr::print( std::ostream &os, int indent ) const {
603 os << "Constructor Expression: " << std::endl;
604 assert( callExpr );
605 os << std::string( indent+2, ' ' );
606 callExpr->print( os, indent + 2 );
607 Expression::print( os, indent );
608}
609
610
611CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) {
612 assert( type && initializer );
613 set_result( type );
614}
615
616CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {}
617
618CompoundLiteralExpr::~CompoundLiteralExpr() {
619 delete initializer;
620}
621
622void CompoundLiteralExpr::print( std::ostream &os, int indent ) const {
623 os << "Compound Literal Expression: " << std::endl;
624 os << std::string( indent+2, ' ' );
625 get_result()->print( os, indent + 2 );
626 os << std::string( indent+2, ' ' );
627 initializer->print( os, indent + 2 );
628 Expression::print( os, indent );
629}
630
631RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {}
632RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {}
633void RangeExpr::print( std::ostream &os, int indent ) const {
634 os << "Range Expression: ";
635 low->print( os, indent );
636 os << " ... ";
637 high->print( os, indent );
638 Expression::print( os, indent );
639}
640
641StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) {
642 assert( statements );
643 std::list< Statement * > & body = statements->get_kids();
644 if ( ! body.empty() ) {
645 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) {
646 set_result( maybeClone( exprStmt->get_expr()->get_result() ) );
647 }
648 }
649}
650StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) {
651 cloneAll( other.returnDecls, returnDecls );
652 cloneAll( other.dtors, dtors );
653}
654StmtExpr::~StmtExpr() {
655 delete statements;
656 deleteAll( dtors );
657 deleteAll( returnDecls );
658}
659void StmtExpr::print( std::ostream &os, int indent ) const {
660 os << "Statement Expression: " << std::endl << std::string( indent, ' ' );
661 statements->print( os, indent+2 );
662 if ( ! returnDecls.empty() ) {
663 os << std::string( indent+2, ' ' ) << "with returnDecls: ";
664 printAll( returnDecls, os, indent+2 );
665 }
666 if ( ! dtors.empty() ) {
667 os << std::string( indent+2, ' ' ) << "with dtors: ";
668 printAll( dtors, os, indent+2 );
669 }
670 Expression::print( os, indent );
671}
672
673
674long long UniqueExpr::count = 0;
675UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) {
676 assert( expr );
677 assert( count != -1 );
678 if ( id == -1 ) id = count++;
679 if ( expr->get_result() ) {
680 set_result( expr->get_result()->clone() );
681 }
682}
683UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) {
684}
685UniqueExpr::~UniqueExpr() {
686 delete expr;
687 delete object;
688 delete var;
689}
690void UniqueExpr::print( std::ostream &os, int indent ) const {
691 os << "Unique Expression with id:" << id << std::endl << std::string( indent+2, ' ' );
692 get_expr()->print( os, indent+2 );
693 if ( get_object() ) {
694 os << std::string( indent+2, ' ' ) << "with decl: ";
695 get_object()->printShort( os, indent+2 );
696 }
697 Expression::print( os, indent );
698}
699
700InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {}
701InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {}
702InitAlternative::~InitAlternative() {
703 delete type;
704 delete designation;
705}
706
707UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {}
708UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {}
709UntypedInitExpr::~UntypedInitExpr() {
710 delete expr;
711}
712
713void UntypedInitExpr::print( std::ostream & os, int indent ) const {
714 os << "Untyped Init Expression" << std::endl << std::string( indent+2, ' ' );
715 expr->print( os, indent+2 );
716 if ( ! initAlts.empty() ) {
717 for ( const InitAlternative & alt : initAlts ) {
718 os << std::string( indent+2, ' ' ) << "InitAlternative: ";
719 alt.type->print( os, indent+2 );
720 alt.designation->print( os, indent+2 );
721 }
722 }
723}
724
725InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) {
726 set_result( expr->get_result()->clone() );
727}
728InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {}
729InitExpr::~InitExpr() {
730 delete expr;
731 delete designation;
732}
733
734void InitExpr::print( std::ostream & os, int indent ) const {
735 os << "Init Expression" << std::endl << std::string( indent+2, ' ' );
736 expr->print( os, indent+2 );
737 os << std::string( indent+2, ' ' ) << "with designation: ";
738 designation->print( os, indent+2 );
739}
740
741
742std::ostream & operator<<( std::ostream & out, const Expression * expr ) {
743 if ( expr ) {
744 expr->print( out );
745 } else {
746 out << "nullptr";
747 }
748 return out;
749}
750
751// Local Variables: //
752// tab-width: 4 //
753// mode: c++ //
754// compile-command: "make install" //
755// End: //
Note: See TracBrowser for help on using the repository browser.