source: src/SynTree/Statement.cc@ 08222c7

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 08222c7 was ee3c93d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add support for while loops with control declarations

  • Property mode set to 100644
File size: 16.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// Statement.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 : Sun Sep 3 20:46:44 2017
13// Update Count : 68
14//
15
16#include "SynTree/Statement.h"
17
18#include <stddef.h> // for NULL
19#include <cassert> // for assert, assertf
20#include <iostream> // for operator<<, basic_ostream, endl
21#include <list> // for list, list<>::const_iterator, _Lis...
22#include <string> // for operator<<, string, char_traits
23
24#include "Common/SemanticError.h" // for SemanticError
25#include "Common/utility.h" // for maybeClone, cloneAll, deleteAll
26#include "Declaration.h" // for Declaration
27#include "Expression.h" // for Expression, ConstantExpr
28#include "Statement.h" // for Statement, ForStmt, AsmStmt, Catch...
29#include "SynTree/Label.h" // for Label, operator<<
30
31using std::string;
32using std::endl;
33
34Statement::Statement( const std::list<Label> & labels ) : labels( labels ) {}
35
36void Statement::print( std::ostream & os, Indenter indent ) const {
37 if ( ! labels.empty() ) {
38 os << indent << "... Labels: {";
39 for ( const Label & l : labels ) {
40 os << l << ",";
41 }
42 os << "}" << endl;
43 }
44}
45
46Statement::~Statement() {}
47
48ExprStmt::ExprStmt( Expression *expr ) : Statement(), expr( expr ) {}
49
50ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
51
52ExprStmt::~ExprStmt() {
53 delete expr;
54}
55
56void ExprStmt::print( std::ostream &os, Indenter indent ) const {
57 os << "Expression Statement:" << endl << indent+1;
58 expr->print( os, indent+1 );
59}
60
61
62AsmStmt::AsmStmt( bool voltile, Expression *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ) : Statement(), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ), gotolabels( gotolabels ) {}
63
64AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
65 cloneAll( other.output, output );
66 cloneAll( other.input, input );
67 cloneAll( other.clobber, clobber );
68}
69
70AsmStmt::~AsmStmt() {
71 delete instruction;
72 deleteAll( output );
73 deleteAll( input );
74 deleteAll( clobber );
75}
76
77void AsmStmt::print( std::ostream &os, Indenter indent ) const {
78 os << "Assembler Statement:" << endl;
79 os << indent+1 << "instruction: " << endl << indent;
80 instruction->print( os, indent+1 );
81 if ( ! output.empty() ) {
82 os << endl << indent+1 << "output: " << endl;
83 printAll( output, os, indent+1 );
84 } // if
85 if ( ! input.empty() ) {
86 os << indent+1 << "input: " << endl;
87 printAll( input, os, indent+1 );
88 } // if
89 if ( ! clobber.empty() ) {
90 os << indent+1 << "clobber: " << endl;
91 printAll( clobber, os, indent+1 );
92 } // if
93}
94
95
96DirectiveStmt::DirectiveStmt( const std::string & directive ) : Statement(), directive( directive ) {}
97
98void DirectiveStmt::print( std::ostream &os, Indenter ) const {
99 os << "GCC Directive:" << directive << endl;
100}
101
102
103const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
104
105BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) :
106 Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
107 //actually this is a syntactic error signaled by the parser
108 if ( type == BranchStmt::Goto && target.empty() ) {
109 SemanticError( target.get_statement()->location, "goto without target");
110 }
111}
112
113BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) :
114 Statement(), computedTarget( computedTarget ), type( type ) {
115 if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
116 SemanticError( computedTarget->location, "Computed target not valid in branch statement");
117 }
118}
119
120void BranchStmt::print( std::ostream &os, Indenter indent ) const {
121 os << "Branch (" << brType[type] << ")" << endl ;
122 if ( target != "" ) os << indent+1 << "with target: " << target << endl;
123 if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
124 if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
125}
126
127ReturnStmt::ReturnStmt( Expression *expr ) : Statement(), expr( expr ) {}
128
129ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
130
131ReturnStmt::~ReturnStmt() {
132 delete expr;
133}
134
135void ReturnStmt::print( std::ostream &os, Indenter indent ) const {
136 os << "Return Statement, returning: ";
137 if ( expr != nullptr ) {
138 os << endl << indent+1;
139 expr->print( os, indent+1 );
140 }
141 os << endl;
142}
143
144IfStmt::IfStmt( Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
145 Statement(), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
146
147IfStmt::IfStmt( const IfStmt & other ) :
148 Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
149 cloneAll( other.initialization, initialization );
150}
151
152IfStmt::~IfStmt() {
153 deleteAll( initialization );
154 delete condition;
155 delete thenPart;
156 delete elsePart;
157}
158
159void IfStmt::print( std::ostream &os, Indenter indent ) const {
160 os << "If on condition: " << endl;
161 os << indent+1;
162 condition->print( os, indent+1 );
163
164 if ( !initialization.empty() ) {
165 os << indent << "... with initialization: \n";
166 for ( const Statement * stmt : initialization ) {
167 os << indent+1;
168 stmt->print( os, indent+1 );
169 }
170 os << endl;
171 }
172
173 os << indent << "... then: " << endl;
174
175 os << indent+1;
176 thenPart->print( os, indent+1 );
177
178 if ( elsePart != 0 ) {
179 os << indent << "... else: " << endl;
180 os << indent+1;
181 elsePart->print( os, indent+1 );
182 } // if
183}
184
185SwitchStmt::SwitchStmt( Expression * condition, const std::list<Statement *> &statements ):
186 Statement(), condition( condition ), statements( statements ) {
187}
188
189SwitchStmt::SwitchStmt( const SwitchStmt & other ):
190 Statement( other ), condition( maybeClone( other.condition ) ) {
191 cloneAll( other.statements, statements );
192}
193
194SwitchStmt::~SwitchStmt() {
195 delete condition;
196 // destroy statements
197 deleteAll( statements );
198}
199
200void SwitchStmt::print( std::ostream &os, Indenter indent ) const {
201 os << "Switch on condition: ";
202 condition->print( os );
203 os << endl;
204
205 for ( const Statement * stmt : statements ) {
206 stmt->print( os, indent+1 );
207 }
208}
209
210CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) :
211 Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) {
212 if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " );
213}
214
215CaseStmt::CaseStmt( const CaseStmt & other ) :
216 Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
217 cloneAll( other.stmts, stmts );
218}
219
220CaseStmt::~CaseStmt() {
221 delete condition;
222 deleteAll( stmts );
223}
224
225CaseStmt * CaseStmt::makeDefault( const std::list<Label> & labels, std::list<Statement *> stmts ) {
226 CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
227 stmt->labels = labels;
228 return stmt;
229}
230
231void CaseStmt::print( std::ostream &os, Indenter indent ) const {
232 if ( isDefault() ) os << indent << "Default ";
233 else {
234 os << indent << "Case ";
235 condition->print( os, indent );
236 } // if
237 os << endl;
238
239 for ( Statement * stmt : stmts ) {
240 os << indent+1;
241 stmt->print( os, indent+1 );
242 }
243}
244
245WhileStmt::WhileStmt( Expression *condition, Statement *body, std::list< Statement * > & initialization, bool isDoWhile ):
246 Statement(), condition( condition), body( body), initialization( initialization ), isDoWhile( isDoWhile) {
247}
248
249WhileStmt::WhileStmt( const WhileStmt & other ):
250 Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
251}
252
253WhileStmt::~WhileStmt() {
254 delete body;
255 delete condition;
256}
257
258void WhileStmt::print( std::ostream &os, Indenter indent ) const {
259 os << "While on condition: " << endl ;
260 condition->print( os, indent+1 );
261
262 os << indent << "... with body: " << endl;
263
264 if ( body != 0 ) body->print( os, indent+1 );
265}
266
267ForStmt::ForStmt( std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
268 Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
269}
270
271ForStmt::ForStmt( const ForStmt & other ):
272 Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
273 cloneAll( other.initialization, initialization );
274
275}
276
277ForStmt::~ForStmt() {
278 deleteAll( initialization );
279 delete condition;
280 delete increment;
281 delete body;
282}
283
284void ForStmt::print( std::ostream &os, Indenter indent ) const {
285 Statement::print( os, indent ); // print labels
286
287 os << "For Statement" << endl;
288
289 if ( ! initialization.empty() ) {
290 os << indent << "... initialization: \n";
291 for ( Statement * stmt : initialization ) {
292 os << indent+1;
293 stmt->print( os, indent+1 );
294 }
295 }
296
297 if ( condition != nullptr ) {
298 os << indent << "... condition: \n" << indent+1;
299 condition->print( os, indent+1 );
300 }
301
302 if ( increment != nullptr ) {
303 os << "\n" << indent << "... increment: \n" << indent+1;
304 increment->print( os, indent+1 );
305 }
306
307 if ( body != 0 ) {
308 os << "\n" << indent << "... with body: \n" << indent+1;
309 body->print( os, indent+1 );
310 }
311 os << endl;
312}
313
314ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
315 Statement(), kind(kind), expr(expr), target(target) {
316 assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
317}
318
319ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
320 Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
321}
322
323ThrowStmt::~ThrowStmt() {
324 delete expr;
325 delete target;
326}
327
328void ThrowStmt::print( std::ostream &os, Indenter indent) const {
329 if ( target ) os << "Non-Local ";
330 os << "Throw Statement, raising: ";
331 expr->print(os, indent+1);
332 if ( target ) {
333 os << "... at: ";
334 target->print(os, indent+1);
335 }
336}
337
338TryStmt::TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
339 Statement(), block( tryBlock ), handlers( handlers ), finallyBlock( finallyBlock ) {
340}
341
342TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
343 cloneAll( other.handlers, handlers );
344}
345
346TryStmt::~TryStmt() {
347 delete block;
348 deleteAll( handlers );
349 delete finallyBlock;
350}
351
352void TryStmt::print( std::ostream &os, Indenter indent ) const {
353 os << "Try Statement" << endl;
354 os << indent << "... with block:" << endl << indent+1;
355 block->print( os, indent+1 );
356
357 // handlers
358 os << indent << "... and handlers:" << endl;
359 for ( const CatchStmt * stmt : handlers ) {
360 os << indent+1;
361 stmt->print( os, indent+1 );
362 }
363
364 // finally block
365 if ( finallyBlock != 0 ) {
366 os << indent << "... and finally:" << endl << indent+1;
367 finallyBlock->print( os, indent+1 );
368 } // if
369}
370
371CatchStmt::CatchStmt( Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
372 Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
373 assertf( decl, "Catch clause must have a declaration." );
374}
375
376CatchStmt::CatchStmt( const CatchStmt & other ) :
377 Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
378}
379
380CatchStmt::~CatchStmt() {
381 delete decl;
382 delete body;
383}
384
385void CatchStmt::print( std::ostream &os, Indenter indent ) const {
386 os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
387
388 os << indent << "... catching: ";
389 decl->printShort( os, indent+1 );
390 os << endl;
391
392 if ( cond ) {
393 os << indent << "... with conditional:" << endl << indent+1;
394 cond->print( os, indent+1 );
395 }
396
397 os << indent << "... with block:" << endl;
398 os << indent+1;
399 body->print( os, indent+1 );
400}
401
402
403FinallyStmt::FinallyStmt( CompoundStmt *block ) : Statement(), block( block ) {
404}
405
406FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
407}
408
409FinallyStmt::~FinallyStmt() {
410 delete block;
411}
412
413void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
414 os << "Finally Statement" << endl;
415 os << indent << "... with block:" << endl << indent+1;
416 block->print( os, indent+1 );
417}
418
419WaitForStmt::WaitForStmt() : Statement() {
420 timeout.time = nullptr;
421 timeout.statement = nullptr;
422 timeout.condition = nullptr;
423 orelse .statement = nullptr;
424 orelse .condition = nullptr;
425}
426
427WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
428 clauses.reserve( other.clauses.size() );
429 for( auto & ocl : other.clauses ) {
430 clauses.emplace_back();
431 clauses.back().target.function = ocl.target.function->clone();
432 cloneAll( ocl.target.arguments, clauses.back().target.arguments );
433 clauses.back().statement = ocl.statement->clone();
434 clauses.back().condition = ocl.condition->clone();
435 }
436
437 timeout.time = other.timeout.time ->clone();
438 timeout.statement = other.timeout.statement->clone();
439 timeout.condition = other.timeout.condition->clone();
440 orelse .statement = other.orelse .statement->clone();
441 orelse .condition = other.orelse .condition->clone();
442}
443
444WaitForStmt::~WaitForStmt() {
445 for( auto & clause : clauses ) {
446 delete clause.target.function;
447 deleteAll( clause.target.arguments );
448 delete clause.statement;
449 delete clause.condition;
450 }
451
452 delete timeout.time;
453 delete timeout.statement;
454 delete timeout.condition;
455
456 delete orelse.statement;
457 delete orelse.condition;
458}
459
460void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
461 os << "Waitfor Statement" << endl;
462 indent += 1;
463 for( auto & clause : clauses ) {
464 os << indent << "target function :";
465 if(clause.target.function) { clause.target.function->print(os, indent + 1); }
466 os << endl << indent << "with arguments :" << endl;
467 for( auto & thing : clause.target.arguments) {
468 if(thing) { thing->print(os, indent + 1); }
469 }
470 os << indent << " with statment :" << endl;
471 if(clause.statement) { clause.statement->print(os, indent + 1); }
472
473 os << indent << " with condition :" << endl;
474 if(clause.condition) { clause.condition->print(os, indent + 1); }
475 }
476
477 os << indent << " timeout of :" << endl;
478 if(timeout.time) { timeout.time->print(os, indent + 1); }
479
480 os << indent << " with statment :" << endl;
481 if(timeout.statement) { timeout.statement->print(os, indent + 1); }
482
483 os << indent << " with condition :" << endl;
484 if(timeout.condition) { timeout.condition->print(os, indent + 1); }
485
486
487 os << indent << " else :" << endl;
488 if(orelse.statement) { orelse.statement->print(os, indent + 1); }
489
490 os << indent << " with condition :" << endl;
491 if(orelse.condition) { orelse.condition->print(os, indent + 1); }
492}
493
494
495WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {}
496WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) {
497 cloneAll( other.exprs, exprs );
498}
499WithStmt::~WithStmt() {
500 deleteAll( exprs );
501 delete stmt;
502}
503
504void WithStmt::print( std::ostream & os, Indenter indent ) const {
505 os << "With statement" << endl;
506 os << indent << "... with expressions: " << endl;
507 printAll( exprs, os, indent+1 );
508 os << indent << "... with statement:" << endl << indent+1;
509 stmt->print( os, indent+1 );
510}
511
512
513NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
514}
515
516void NullStmt::print( std::ostream &os, Indenter indent ) const {
517 os << "Null Statement" << endl;
518 Statement::print( os, indent );
519}
520
521ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
522 assert( callStmt );
523}
524
525ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
526}
527
528ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
529 delete callStmt;
530}
531
532void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
533 os << "Implicit Ctor Dtor Statement" << endl;
534 os << indent << "... with Ctor/Dtor: ";
535 callStmt->print( os, indent+1);
536 os << endl;
537}
538
539// Local Variables: //
540// tab-width: 4 //
541// mode: c++ //
542// compile-command: "make install" //
543// End: //
Note: See TracBrowser for help on using the repository browser.