source: src/SynTree/Statement.cc@ 6fa409e

new-env
Last change on this file since 6fa409e was 28f3a19, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge branch 'master' into with_gc

  • Property mode set to 100644
File size: 15.1 KB
RevLine 
[0dd3a2f]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//
[3be261a]7// Statement.cc --
[0dd3a2f]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[6d49ea3]11// Last Modified By : Peter A. Buhr
[e612146c]12// Last Modified On : Sun Sep 3 20:46:44 2017
13// Update Count : 68
[0dd3a2f]14//
15
[ea6332d]16#include "SynTree/Statement.h"
[51b73452]17
[ea6332d]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<<
[51b73452]30
31using std::string;
32using std::endl;
33
[ba3706f]34Statement::Statement( const std::list<Label> & labels ) : labels( labels ) {}
[51b73452]35
[6a276a0]36void Statement::print( std::ostream & os, Indenter indent ) const {
[50377a4]37 if ( ! labels.empty() ) {
[6a276a0]38 os << indent << "... Labels: {";
[50377a4]39 for ( const Label & l : labels ) {
40 os << l << ",";
41 }
42 os << "}" << endl;
43 }
44}
[51b73452]45
[ba3706f]46ExprStmt::ExprStmt( Expression *expr ) : Statement(), expr( expr ) {}
[51b73452]47
[3be261a]48ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
49
[50377a4]50void ExprStmt::print( std::ostream &os, Indenter indent ) const {
51 os << "Expression Statement:" << endl << indent+1;
52 expr->print( os, indent+1 );
[3be261a]53}
[51b73452]54
[7f5566b]55
[ba3706f]56AsmStmt::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 ) {}
[7f5566b]57
[3be261a]58AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
59 cloneAll( other.output, output );
60 cloneAll( other.input, input );
61 cloneAll( other.clobber, clobber );
62}
63
[50377a4]64void AsmStmt::print( std::ostream &os, Indenter indent ) const {
[7f5566b]65 os << "Assembler Statement:" << endl;
[50377a4]66 os << indent+1 << "instruction: " << endl << indent;
67 instruction->print( os, indent+1 );
[7f5566b]68 if ( ! output.empty() ) {
[50377a4]69 os << endl << indent+1 << "output: " << endl;
70 printAll( output, os, indent+1 );
[3be261a]71 } // if
[7f5566b]72 if ( ! input.empty() ) {
[50377a4]73 os << indent+1 << "input: " << endl;
74 printAll( input, os, indent+1 );
[7f5566b]75 } // if
76 if ( ! clobber.empty() ) {
[50377a4]77 os << indent+1 << "clobber: " << endl;
78 printAll( clobber, os, indent+1 );
[7f5566b]79 } // if
[3be261a]80}
[7f5566b]81
82
[cc32d83]83DirectiveStmt::DirectiveStmt( const std::string & directive ) : Statement(), directive( directive ) {}
84
85void DirectiveStmt::print( std::ostream &os, Indenter ) const {
86 os << "GCC Directive:" << directive << endl;
87}
88
89
[51b73452]90const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
91
[a16764a6]92BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) :
[ba3706f]93 Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
[0dd3a2f]94 //actually this is a syntactic error signaled by the parser
[b0dfbc4]95 if ( type == BranchStmt::Goto && target.empty() ) {
[a16764a6]96 SemanticError( target.get_statement()->location, "goto without target");
[b0dfbc4]97 }
[51b73452]98}
99
[a16764a6]100BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) :
[ba3706f]101 Statement(), computedTarget( computedTarget ), type( type ) {
[b0dfbc4]102 if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
[a16764a6]103 SemanticError( computedTarget->location, "Computed target not valid in branch statement");
[b0dfbc4]104 }
[51b73452]105}
106
[50377a4]107void BranchStmt::print( std::ostream &os, Indenter indent ) const {
108 os << "Branch (" << brType[type] << ")" << endl ;
109 if ( target != "" ) os << indent+1 << "with target: " << target << endl;
110 if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
111 if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
[51b73452]112}
113
[ba3706f]114ReturnStmt::ReturnStmt( Expression *expr ) : Statement(), expr( expr ) {}
[51b73452]115
[daf1af8]116ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
[3be261a]117
[50377a4]118void ReturnStmt::print( std::ostream &os, Indenter indent ) const {
119 os << "Return Statement, returning: ";
120 if ( expr != nullptr ) {
121 os << endl << indent+1;
122 expr->print( os, indent+1 );
[89231bc]123 }
[0dd3a2f]124 os << endl;
[51b73452]125}
126
[ba3706f]127IfStmt::IfStmt( Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
128 Statement(), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
[51b73452]129
[3be261a]130IfStmt::IfStmt( const IfStmt & other ) :
[6d49ea3]131 Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
132 cloneAll( other.initialization, initialization );
133}
[3be261a]134
[50377a4]135void IfStmt::print( std::ostream &os, Indenter indent ) const {
136 os << "If on condition: " << endl;
137 os << indent+1;
138 condition->print( os, indent+1 );
[51b73452]139
[6d49ea3]140 if ( !initialization.empty() ) {
[50377a4]141 os << indent << "... with initialization: \n";
142 for ( const Statement * stmt : initialization ) {
143 os << indent+1;
144 stmt->print( os, indent+1 );
[6d49ea3]145 }
146 os << endl;
147 }
148
[50377a4]149 os << indent << "... then: " << endl;
[51b73452]150
[50377a4]151 os << indent+1;
152 thenPart->print( os, indent+1 );
[51b73452]153
[0dd3a2f]154 if ( elsePart != 0 ) {
[50377a4]155 os << indent << "... else: " << endl;
156 os << indent+1;
157 elsePart->print( os, indent+1 );
[0dd3a2f]158 } // if
[51b73452]159}
160
[ba3706f]161SwitchStmt::SwitchStmt( Expression * condition, const std::list<Statement *> &statements ):
162 Statement(), condition( condition ), statements( statements ) {
[0dd3a2f]163}
[51b73452]164
[3be261a]165SwitchStmt::SwitchStmt( const SwitchStmt & other ):
166 Statement( other ), condition( maybeClone( other.condition ) ) {
[8688ce1]167 cloneAll( other.statements, statements );
[3be261a]168}
169
[50377a4]170void SwitchStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]171 os << "Switch on condition: ";
[0dd3a2f]172 condition->print( os );
173 os << endl;
[51b73452]174
[50377a4]175 for ( const Statement * stmt : statements ) {
176 stmt->print( os, indent+1 );
177 }
[51b73452]178}
179
[a16764a6]180CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) :
[ba3706f]181 Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) {
[a16764a6]182 if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " );
[51b73452]183}
184
[3be261a]185CaseStmt::CaseStmt( const CaseStmt & other ) :
186 Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
187 cloneAll( other.stmts, stmts );
188}
189
[ba3706f]190CaseStmt * CaseStmt::makeDefault( const std::list<Label> & labels, std::list<Statement *> stmts ) {
191 CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
192 stmt->labels = labels;
193 return stmt;
[b2152e7a]194}
195
[50377a4]196void CaseStmt::print( std::ostream &os, Indenter indent ) const {
[6a276a0]197 if ( isDefault() ) os << indent << "Default ";
[0dd3a2f]198 else {
[6a276a0]199 os << indent << "Case ";
[50377a4]200 condition->print( os, indent );
[0dd3a2f]201 } // if
202 os << endl;
[51b73452]203
[50377a4]204 for ( Statement * stmt : stmts ) {
[6a276a0]205 os << indent+1;
[50377a4]206 stmt->print( os, indent+1 );
207 }
[51b73452]208}
209
[ee3c93d]210WhileStmt::WhileStmt( Expression *condition, Statement *body, std::list< Statement * > & initialization, bool isDoWhile ):
211 Statement(), condition( condition), body( body), initialization( initialization ), isDoWhile( isDoWhile) {
[0dd3a2f]212}
[51b73452]213
[3be261a]214WhileStmt::WhileStmt( const WhileStmt & other ):
215 Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
216}
217
[50377a4]218void WhileStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]219 os << "While on condition: " << endl ;
[50377a4]220 condition->print( os, indent+1 );
[51b73452]221
[50377a4]222 os << indent << "... with body: " << endl;
[51b73452]223
[50377a4]224 if ( body != 0 ) body->print( os, indent+1 );
[51b73452]225}
226
[ba3706f]227ForStmt::ForStmt( std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
228 Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
[0dd3a2f]229}
[51b73452]230
[3be261a]231ForStmt::ForStmt( const ForStmt & other ):
232 Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
233 cloneAll( other.initialization, initialization );
234
235}
236
[50377a4]237void ForStmt::print( std::ostream &os, Indenter indent ) const {
238 Statement::print( os, indent ); // print labels
[be5aa1b]239
[50377a4]240 os << "For Statement" << endl;
[51b73452]241
[50377a4]242 if ( ! initialization.empty() ) {
243 os << indent << "... initialization: \n";
244 for ( Statement * stmt : initialization ) {
245 os << indent+1;
246 stmt->print( os, indent+1 );
247 }
[145f1fc]248 }
[51b73452]249
[50377a4]250 if ( condition != nullptr ) {
251 os << indent << "... condition: \n" << indent+1;
252 condition->print( os, indent+1 );
[bb8ea30]253 }
[51b73452]254
[50377a4]255 if ( increment != nullptr ) {
256 os << "\n" << indent << "... increment: \n" << indent+1;
257 increment->print( os, indent+1 );
[bb8ea30]258 }
[51b73452]259
[bb8ea30]260 if ( body != 0 ) {
[50377a4]261 os << "\n" << indent << "... with body: \n" << indent+1;
262 body->print( os, indent+1 );
[bb8ea30]263 }
[0dd3a2f]264 os << endl;
[51b73452]265}
266
[ba3706f]267ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
268 Statement(), kind(kind), expr(expr), target(target) {
[daf1af8]269 assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
270}
271
272ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
273 Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
274}
275
[50377a4]276void ThrowStmt::print( std::ostream &os, Indenter indent) const {
277 if ( target ) os << "Non-Local ";
[daf1af8]278 os << "Throw Statement, raising: ";
[50377a4]279 expr->print(os, indent+1);
[daf1af8]280 if ( target ) {
[50377a4]281 os << "... at: ";
282 target->print(os, indent+1);
[daf1af8]283 }
284}
285
[ba3706f]286TryStmt::TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
287 Statement(), block( tryBlock ), handlers( handlers ), finallyBlock( finallyBlock ) {
[0dd3a2f]288}
[51b73452]289
[3be261a]290TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
291 cloneAll( other.handlers, handlers );
[51b73452]292}
293
[50377a4]294void TryStmt::print( std::ostream &os, Indenter indent ) const {
[89231bc]295 os << "Try Statement" << endl;
[50377a4]296 os << indent << "... with block:" << endl << indent+1;
297 block->print( os, indent+1 );
[0dd3a2f]298
299 // handlers
[50377a4]300 os << indent << "... and handlers:" << endl;
301 for ( const CatchStmt * stmt : handlers ) {
302 os << indent+1;
303 stmt->print( os, indent+1 );
[406a6e6]304 }
[0dd3a2f]305
306 // finally block
307 if ( finallyBlock != 0 ) {
[50377a4]308 os << indent << "... and finally:" << endl << indent+1;
309 finallyBlock->print( os, indent+1 );
[0dd3a2f]310 } // if
[51b73452]311}
312
[ba3706f]313CatchStmt::CatchStmt( Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
314 Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
[50377a4]315 assertf( decl, "Catch clause must have a declaration." );
[0dd3a2f]316}
[51b73452]317
[3be261a]318CatchStmt::CatchStmt( const CatchStmt & other ) :
[ca78437]319 Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
[3be261a]320}
321
[50377a4]322void CatchStmt::print( std::ostream &os, Indenter indent ) const {
[ca78437]323 os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
[0dd3a2f]324
[50377a4]325 os << indent << "... catching: ";
326 decl->printShort( os, indent+1 );
327 os << endl;
[406a6e6]328
329 if ( cond ) {
[50377a4]330 os << indent << "... with conditional:" << endl << indent+1;
331 cond->print( os, indent+1 );
[406a6e6]332 }
333
[50377a4]334 os << indent << "... with block:" << endl;
335 os << indent+1;
336 body->print( os, indent+1 );
[51b73452]337}
338
339
[ba3706f]340FinallyStmt::FinallyStmt( CompoundStmt *block ) : Statement(), block( block ) {
[51b73452]341}
342
[3be261a]343FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
344}
345
[50377a4]346void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
[f1b1e4c]347 os << "Finally Statement" << endl;
[50377a4]348 os << indent << "... with block:" << endl << indent+1;
349 block->print( os, indent+1 );
[51b73452]350}
351
[ba3706f]352WaitForStmt::WaitForStmt() : Statement() {
[135b431]353 timeout.time = nullptr;
354 timeout.statement = nullptr;
355 timeout.condition = nullptr;
356 orelse .statement = nullptr;
357 orelse .condition = nullptr;
358}
359
360WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
361 clauses.reserve( other.clauses.size() );
362 for( auto & ocl : other.clauses ) {
363 clauses.emplace_back();
364 clauses.back().target.function = ocl.target.function->clone();
365 cloneAll( ocl.target.arguments, clauses.back().target.arguments );
366 clauses.back().statement = ocl.statement->clone();
367 clauses.back().condition = ocl.condition->clone();
368 }
369
370 timeout.time = other.timeout.time ->clone();
371 timeout.statement = other.timeout.statement->clone();
372 timeout.condition = other.timeout.condition->clone();
373 orelse .statement = other.orelse .statement->clone();
374 orelse .condition = other.orelse .condition->clone();
375}
376
[50377a4]377void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
[135b431]378 os << "Waitfor Statement" << endl;
[b9f383f]379 indent += 1;
380 for( auto & clause : clauses ) {
381 os << indent << "target function :";
382 if(clause.target.function) { clause.target.function->print(os, indent + 1); }
383 os << endl << indent << "with arguments :" << endl;
384 for( auto & thing : clause.target.arguments) {
385 if(thing) { thing->print(os, indent + 1); }
386 }
387 os << indent << " with statment :" << endl;
388 if(clause.statement) { clause.statement->print(os, indent + 1); }
389
390 os << indent << " with condition :" << endl;
391 if(clause.condition) { clause.condition->print(os, indent + 1); }
392 }
393
394 os << indent << " timeout of :" << endl;
395 if(timeout.time) { timeout.time->print(os, indent + 1); }
396
397 os << indent << " with statment :" << endl;
398 if(timeout.statement) { timeout.statement->print(os, indent + 1); }
399
400 os << indent << " with condition :" << endl;
401 if(timeout.condition) { timeout.condition->print(os, indent + 1); }
402
403
404 os << indent << " else :" << endl;
405 if(orelse.statement) { orelse.statement->print(os, indent + 1); }
406
407 os << indent << " with condition :" << endl;
408 if(orelse.condition) { orelse.condition->print(os, indent + 1); }
[135b431]409}
410
[61255ad]411
[3ca540f]412WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {}
[61255ad]413WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) {
414 cloneAll( other.exprs, exprs );
415}
416
417void WithStmt::print( std::ostream & os, Indenter indent ) const {
418 os << "With statement" << endl;
[44b4114]419 os << indent << "... with expressions: " << endl;
420 printAll( exprs, os, indent+1 );
[61255ad]421 os << indent << "... with statement:" << endl << indent+1;
422 stmt->print( os, indent+1 );
423}
424
425
[ba3706f]426NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
427}
[51b73452]428
[6a276a0]429void NullStmt::print( std::ostream &os, Indenter indent ) const {
[50377a4]430 os << "Null Statement" << endl;
[6a276a0]431 Statement::print( os, indent );
[51b73452]432}
433
[ba3706f]434ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
[f1b1e4c]435 assert( callStmt );
436}
437
[74d1804]438ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
[f1b1e4c]439}
440
[50377a4]441void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
[f1b1e4c]442 os << "Implicit Ctor Dtor Statement" << endl;
[50377a4]443 os << indent << "... with Ctor/Dtor: ";
444 callStmt->print( os, indent+1);
[f1b1e4c]445 os << endl;
446}
447
[0dd3a2f]448// Local Variables: //
449// tab-width: 4 //
450// mode: c++ //
451// compile-command: "make install" //
452// End: //
Note: See TracBrowser for help on using the repository browser.