source: src/SynTree/Statement.cc@ 4ffdd63

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 4ffdd63 was 89231bc, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fix printing for various AST nodes, including ObjectDecl, ExprStmt, etc. - assume preceding indentation printed by parent

  • Property mode set to 100644
File size: 12.6 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
[baf7fee]11// Last Modified By : Rob Schluntz
[89231bc]12// Last Modified On : Tue Apr 26 12:33:33 2016
[baf7fee]13// Update Count : 54
[0dd3a2f]14//
15
[51b73452]16#include <functional>
17#include <algorithm>
18#include <iostream>
19#include <list>
20#include <cassert>
21
22#include "Statement.h"
23#include "Expression.h"
24#include "Declaration.h"
25#include "Common/SemanticError.h"
26
27using std::string;
28using std::endl;
29
[2871210]30Statement::Statement( std::list<Label> _labels ) : labels( _labels ) {}
[51b73452]31
[de62360d]32void Statement::print( std::ostream &, int indent ) const {}
[51b73452]33
34Statement::~Statement() {}
35
[2871210]36ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ) : Statement( _labels ), expr( _expr ) {}
[51b73452]37
[3be261a]38ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
39
40ExprStmt::~ExprStmt() {
41 delete expr;
42}
[51b73452]43
[de62360d]44void ExprStmt::print( std::ostream &os, int indent ) const {
[89231bc]45 os << "Expression Statement:" << endl << std::string( indent, ' ' );
[0dd3a2f]46 expr->print( os, indent + 2 );
[3be261a]47}
[51b73452]48
[7f5566b]49
50AsmStmt::AsmStmt( std::list<Label> labels, bool voltile, ConstantExpr *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ) : Statement( labels ), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ), gotolabels( gotolabels ) {}
51
[3be261a]52AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
53 cloneAll( other.output, output );
54 cloneAll( other.input, input );
55 cloneAll( other.clobber, clobber );
56}
57
[7f5566b]58AsmStmt::~AsmStmt() {
59 delete instruction;
60 deleteAll( output );
61 deleteAll( input );
62 deleteAll( clobber );
63}
64
65void AsmStmt::print( std::ostream &os, int indent ) const {
66 os << "Assembler Statement:" << endl;
67 os << std::string( indent, ' ' ) << "instruction: " << endl << std::string( indent, ' ' );
68 instruction->print( os, indent + 2 );
69 if ( ! output.empty() ) {
70 os << endl << std::string( indent, ' ' ) << "output: " << endl;
71 printAll( output, os, indent + 2 );
[3be261a]72 } // if
[7f5566b]73 if ( ! input.empty() ) {
74 os << std::string( indent, ' ' ) << "input: " << endl << std::string( indent, ' ' );
75 printAll( input, os, indent + 2 );
76 } // if
77 if ( ! clobber.empty() ) {
78 os << std::string( indent, ' ' ) << "clobber: " << endl;
79 printAll( clobber, os, indent + 2 );
80 } // if
[3be261a]81}
[7f5566b]82
83
[51b73452]84const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
85
[0dd3a2f]86BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) :
[2871210]87 Statement( labels ), originalTarget( _target ), target( _target ), computedTarget( NULL ), type( _type ) {
[0dd3a2f]88 //actually this is a syntactic error signaled by the parser
89 if ( type == BranchStmt::Goto && target.size() == 0 )
90 throw SemanticError("goto without target");
[51b73452]91}
92
[0dd3a2f]93BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) :
[2871210]94 Statement( labels ), computedTarget( _computedTarget ), type( _type ) {
[0dd3a2f]95 if ( type != BranchStmt::Goto || computedTarget == 0 )
96 throw SemanticError("Computed target not valid in branch statement");
[51b73452]97}
98
[de62360d]99void BranchStmt::print( std::ostream &os, int indent ) const {
[44b5ca0]100 os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ;
[51b73452]101}
102
[0dd3a2f]103ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) : Statement( labels ), expr( _expr ), isThrow( throwP ) {}
[51b73452]104
[3be261a]105ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ), isThrow( other.isThrow ) {}
106
[51b73452]107ReturnStmt::~ReturnStmt() {
[0dd3a2f]108 delete expr;
[51b73452]109}
110
[de62360d]111void ReturnStmt::print( std::ostream &os, int indent ) const {
[89231bc]112 os << string ( isThrow? "Throw":"Return" ) << " Statement, returning: ";
113 if ( expr != 0 ) {
114 os << endl << string( indent+2, ' ' );
115 expr->print( os, indent + 2 );
116 }
[0dd3a2f]117 os << endl;
[51b73452]118}
119
120IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
[2871210]121 Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {}
[51b73452]122
[3be261a]123IfStmt::IfStmt( const IfStmt & other ) :
124 Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {}
125
[51b73452]126IfStmt::~IfStmt() {}
127
[de62360d]128void IfStmt::print( std::ostream &os, int indent ) const {
[89231bc]129 os << "If on condition: " << endl ;
[0dd3a2f]130 condition->print( os, indent + 4 );
[51b73452]131
[44b5ca0]132 os << string( indent, ' ' ) << ".... and branches: " << endl;
[51b73452]133
[0dd3a2f]134 thenPart->print( os, indent + 4 );
[51b73452]135
[0dd3a2f]136 if ( elsePart != 0 ) {
137 elsePart->print( os, indent + 4 );
138 } // if
[51b73452]139}
140
[0dd3a2f]141SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
[2871210]142 Statement( _labels ), condition( _condition ), branches( _branches ) {
[0dd3a2f]143}
[51b73452]144
[3be261a]145SwitchStmt::SwitchStmt( const SwitchStmt & other ):
146 Statement( other ), condition( maybeClone( other.condition ) ) {
147 cloneAll( other.branches, branches );
148}
149
[51b73452]150SwitchStmt::~SwitchStmt() {
[0dd3a2f]151 delete condition;
152 // destroy branches
[51b73452]153}
154
[0dd3a2f]155void SwitchStmt::add_case( CaseStmt *c ) {}
[51b73452]156
[de62360d]157void SwitchStmt::print( std::ostream &os, int indent ) const {
[89231bc]158 os << "Switch on condition: ";
[0dd3a2f]159 condition->print( os );
160 os << endl;
[51b73452]161
[0dd3a2f]162 // branches
[de62360d]163 std::list<Statement *>::const_iterator i;
[0dd3a2f]164 for ( i = branches.begin(); i != branches.end(); i++)
[de62360d]165 (*i)->print( os, indent + 4 );
[51b73452]166
[0dd3a2f]167 //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
[51b73452]168}
169
[3be261a]170CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
[2871210]171 Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
[0dd3a2f]172 if ( isDefault() && condition != 0 )
173 throw SemanticError("default with conditions");
[51b73452]174}
175
[3be261a]176CaseStmt::CaseStmt( const CaseStmt & other ) :
177 Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
178 cloneAll( other.stmts, stmts );
179}
180
[51b73452]181CaseStmt::~CaseStmt() {
[0dd3a2f]182 delete condition;
[51b73452]183}
184
[b2152e7a]185CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> branches ) {
186 return new CaseStmt( labels, 0, branches, true );
187}
188
[de62360d]189void CaseStmt::print( std::ostream &os, int indent ) const {
[44b5ca0]190 os << string( indent, ' ' );
[51b73452]191
[de62360d]192 if ( isDefault() )
[0dd3a2f]193 os << "Default ";
194 else {
195 os << "Case ";
196 condition->print( os );
197 } // if
[51b73452]198
[0dd3a2f]199 os << endl;
[51b73452]200
[de62360d]201 std::list<Statement *>::const_iterator i;
[0dd3a2f]202 for ( i = stmts.begin(); i != stmts.end(); i++)
203 (*i )->print( os, indent + 4 );
[51b73452]204}
205
206//ChooseStmt::ChooseStmt( std::list<Label> labels, Expression *condition, Statement *body ) {}
[0dd3a2f]207ChooseStmt::ChooseStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
[2871210]208 Statement( _labels ), condition( _condition ), branches( _branches ) {
[0dd3a2f]209}
[51b73452]210
[3be261a]211ChooseStmt::ChooseStmt( const ChooseStmt & other ):
212 Statement( other ), condition( maybeClone( other.condition ) ) {
213 cloneAll( other.branches, branches );
214}
215
[51b73452]216ChooseStmt::~ChooseStmt() {
[0dd3a2f]217 delete condition;
[51b73452]218}
219
[0dd3a2f]220void ChooseStmt::add_case( CaseStmt *c ) {}
[51b73452]221
[de62360d]222void ChooseStmt::print( std::ostream &os, int indent ) const {
[89231bc]223 os << "Choose on condition: ";
[0dd3a2f]224 condition->print( os );
225 os << endl;
[51b73452]226
[0dd3a2f]227 // branches
[de62360d]228 std::list<Statement *>::const_iterator i;
[0dd3a2f]229 for ( i = branches.begin(); i != branches.end(); i++)
230 (*i )->print( os, indent + 4 );
[51b73452]231
[0dd3a2f]232 //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
[51b73452]233}
234
[de62360d]235void FallthruStmt::print( std::ostream &os, int indent ) const {
[44b5ca0]236 os << string( indent, ' ' ) << "Fall-through statement" << endl;
[51b73452]237}
238
[0dd3a2f]239WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
240 Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
241}
[51b73452]242
[3be261a]243WhileStmt::WhileStmt( const WhileStmt & other ):
244 Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
245}
246
[a08ba92]247WhileStmt::~WhileStmt() {
[0dd3a2f]248 delete body;
[51b73452]249}
250
[de62360d]251void WhileStmt::print( std::ostream &os, int indent ) const {
[89231bc]252 os << "While on condition: " << endl ;
[0dd3a2f]253 condition->print( os, indent + 4 );
[51b73452]254
[44b5ca0]255 os << string( indent, ' ' ) << ".... with body: " << endl;
[51b73452]256
[0dd3a2f]257 if ( body != 0 ) body->print( os, indent + 4 );
[51b73452]258}
259
[145f1fc]260ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
[0dd3a2f]261 Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
262}
[51b73452]263
[3be261a]264ForStmt::ForStmt( const ForStmt & other ):
265 Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
266 cloneAll( other.initialization, initialization );
267
268}
269
[51b73452]270ForStmt::~ForStmt() {
[145f1fc]271 deleteAll( initialization );
[0dd3a2f]272 delete condition;
273 delete increment;
274 delete body;
[51b73452]275}
276
[de62360d]277void ForStmt::print( std::ostream &os, int indent ) const {
[89231bc]278 os << "Labels: {";
[de62360d]279 for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
[be5aa1b]280 os << *it << ",";
281 }
282 os << "}" << endl;
283
[44b5ca0]284 os << string( indent, ' ' ) << "For Statement" << endl ;
[51b73452]285
[3be261a]286 os << string( indent + 2, ' ' ) << "initialization: \n";
[145f1fc]287 for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
288 (*it)->print( os, indent + 4 );
289 }
[51b73452]290
[3be261a]291 os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
[0dd3a2f]292 if ( condition != 0 )
293 condition->print( os, indent + 4 );
[51b73452]294
[3be261a]295 os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
[0dd3a2f]296 if ( increment != 0 )
297 increment->print( os, indent + 4 );
[51b73452]298
[3be261a]299 os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
[0dd3a2f]300 if ( body != 0 )
301 body->print( os, indent + 4 );
[51b73452]302
[0dd3a2f]303 os << endl;
[51b73452]304}
305
306TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) :
[0dd3a2f]307 Statement( labels ), block( tryBlock ), handlers( _handlers ), finallyBlock( _finallyBlock ) {
308}
[51b73452]309
[3be261a]310TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
311 cloneAll( other.handlers, handlers );
[51b73452]312}
313
[a08ba92]314TryStmt::~TryStmt() {
[0dd3a2f]315 delete block;
[51b73452]316}
317
[de62360d]318void TryStmt::print( std::ostream &os, int indent ) const {
[89231bc]319 os << "Try Statement" << endl;
[44b5ca0]320 os << string( indent + 2, ' ' ) << "with block: " << endl;
[0dd3a2f]321 block->print( os, indent + 4 );
322
323 // handlers
[44b5ca0]324 os << string( indent + 2, ' ' ) << "and handlers: " << endl;
[de62360d]325 for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++)
[0dd3a2f]326 (*i )->print( os, indent + 4 );
327
328 // finally block
329 if ( finallyBlock != 0 ) {
[44b5ca0]330 os << string( indent + 2, ' ' ) << "Finally block: " << endl;
[0dd3a2f]331 finallyBlock->print( os, indent + 4 );
332 } // if
[51b73452]333}
334
335CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool isCatchRest ) :
[0dd3a2f]336 Statement( labels ), decl ( _decl ), body( _body ), catchRest ( isCatchRest ) {
337}
[51b73452]338
[3be261a]339CatchStmt::CatchStmt( const CatchStmt & other ) :
340 Statement( other ), decl ( maybeClone( other.decl ) ), body( maybeClone( other.body ) ), catchRest ( other.catchRest ) {
341}
342
[a08ba92]343CatchStmt::~CatchStmt() {
[0dd3a2f]344 delete decl;
345 delete body;
[51b73452]346}
347
[de62360d]348void CatchStmt::print( std::ostream &os, int indent ) const {
[44b5ca0]349 os << string( indent, ' ' ) << "Catch Statement" << endl;
[0dd3a2f]350
[44b5ca0]351 os << string( indent, ' ' ) << "... catching" << endl;
[0dd3a2f]352 if ( decl ) {
353 decl->printShort( os, indent + 4 );
354 os << endl;
355 } else if ( catchRest )
[44b5ca0]356 os << string( indent + 4 , ' ' ) << "the rest" << endl;
[0dd3a2f]357 else
[44b5ca0]358 os << string( indent + 4 , ' ' ) << ">>> Error: this catch clause must have a declaration <<<" << endl;
[51b73452]359}
360
361
[0dd3a2f]362FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
363 assert( labels.empty() ); // finally statement cannot be labeled
[51b73452]364}
365
[3be261a]366FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
367}
368
[0dd3a2f]369FinallyStmt::~FinallyStmt() {
370 delete block;
[51b73452]371}
372
[de62360d]373void FinallyStmt::print( std::ostream &os, int indent ) const {
[44b5ca0]374 os << string( indent, ' ' ) << "Finally Statement" << endl;
375 os << string( indent + 2, ' ' ) << "with block: " << endl;
[0dd3a2f]376 block->print( os, indent + 4 );
[51b73452]377}
378
379NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
380NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
381
[de62360d]382void NullStmt::print( std::ostream &os, int indent ) const {
[89231bc]383 os << "Null Statement" << endl ;
[51b73452]384}
385
[baf7fee]386std::ostream & operator<<( std::ostream & out, Statement * statement ) {
387 statement->print( out );
388 return out;
389}
390
[0dd3a2f]391// Local Variables: //
392// tab-width: 4 //
393// mode: c++ //
394// compile-command: "make install" //
395// End: //
Note: See TracBrowser for help on using the repository browser.