source: src/SynTree/Statement.cc @ 89231bc

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 89231bc was 89231bc, checked in by Rob Schluntz <rschlunt@…>, 8 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
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 : Rob Schluntz
12// Last Modified On : Tue Apr 26 12:33:33 2016
13// Update Count     : 54
14//
15
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
30Statement::Statement( std::list<Label> _labels ) : labels( _labels ) {}
31
32void Statement::print( std::ostream &, int indent ) const {}
33
34Statement::~Statement() {}
35
36ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ) : Statement( _labels ), expr( _expr ) {}
37
38ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
39
40ExprStmt::~ExprStmt() {
41        delete expr;
42}
43
44void ExprStmt::print( std::ostream &os, int indent ) const {
45        os << "Expression Statement:" << endl << std::string( indent, ' ' );
46        expr->print( os, indent + 2 );
47}
48
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
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
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 );
72        } // if
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
81}
82
83
84const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
85
86BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) :
87        Statement( labels ), originalTarget( _target ), target( _target ), computedTarget( NULL ), type( _type ) {
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");
91}
92
93BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) :
94        Statement( labels ), computedTarget( _computedTarget ), type( _type ) {
95        if ( type != BranchStmt::Goto || computedTarget == 0 )
96                throw SemanticError("Computed target not valid in branch statement");
97}
98
99void BranchStmt::print( std::ostream &os, int indent ) const {
100        os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ;
101}
102
103ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) : Statement( labels ), expr( _expr ), isThrow( throwP ) {}
104
105ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ), isThrow( other.isThrow ) {}
106
107ReturnStmt::~ReturnStmt() {
108        delete expr;
109}
110
111void ReturnStmt::print( std::ostream &os, int indent ) const {
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        }
117        os << endl;
118}
119
120IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
121        Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {}
122
123IfStmt::IfStmt( const IfStmt & other ) :
124        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {}
125
126IfStmt::~IfStmt() {}
127
128void IfStmt::print( std::ostream &os, int indent ) const {
129        os << "If on condition: " << endl ;
130        condition->print( os, indent + 4 );
131
132        os << string( indent, ' ' ) << ".... and branches: " << endl;
133
134        thenPart->print( os, indent + 4 );
135
136        if ( elsePart != 0 ) {
137                elsePart->print( os, indent + 4 );
138        } // if
139}
140
141SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
142        Statement( _labels ), condition( _condition ), branches( _branches ) {
143}
144
145SwitchStmt::SwitchStmt( const SwitchStmt & other ):
146        Statement( other ), condition( maybeClone( other.condition ) ) {
147        cloneAll( other.branches, branches );
148}
149
150SwitchStmt::~SwitchStmt() {
151        delete condition;
152        // destroy branches
153}
154
155void SwitchStmt::add_case( CaseStmt *c ) {}
156
157void SwitchStmt::print( std::ostream &os, int indent ) const {
158        os << "Switch on condition: ";
159        condition->print( os );
160        os << endl;
161
162        // branches
163        std::list<Statement *>::const_iterator i;
164        for ( i = branches.begin(); i != branches.end(); i++)
165                (*i)->print( os, indent + 4 );
166
167        //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
168}
169
170CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
171        Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
172        if ( isDefault() && condition != 0 )
173                throw SemanticError("default with conditions");
174}
175
176CaseStmt::CaseStmt( const CaseStmt & other ) :
177        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
178        cloneAll( other.stmts, stmts );
179}
180
181CaseStmt::~CaseStmt() {
182        delete condition;
183}
184
185CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> branches ) {
186        return new CaseStmt( labels, 0, branches, true );
187}
188
189void CaseStmt::print( std::ostream &os, int indent ) const {
190        os << string( indent, ' ' );
191
192        if ( isDefault() )
193                os << "Default ";
194        else {
195                os << "Case ";
196                condition->print( os );
197        } // if
198
199        os << endl;
200
201        std::list<Statement *>::const_iterator i;
202        for ( i = stmts.begin(); i != stmts.end(); i++)
203                (*i )->print( os, indent + 4 );
204}
205
206//ChooseStmt::ChooseStmt( std::list<Label> labels, Expression *condition, Statement *body ) {}
207ChooseStmt::ChooseStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches ):
208        Statement( _labels ), condition( _condition ), branches( _branches ) {
209}
210
211ChooseStmt::ChooseStmt( const ChooseStmt & other ):
212        Statement( other ), condition( maybeClone( other.condition ) ) {
213                cloneAll( other.branches, branches );
214}
215
216ChooseStmt::~ChooseStmt() {
217        delete condition;
218}
219
220void ChooseStmt::add_case( CaseStmt *c ) {}
221
222void ChooseStmt::print( std::ostream &os, int indent ) const {
223        os << "Choose on condition: ";
224        condition->print( os );
225        os << endl;
226
227        // branches
228        std::list<Statement *>::const_iterator i;
229        for ( i = branches.begin(); i != branches.end(); i++)
230                (*i )->print( os, indent + 4 );
231
232        //for_each( branches.begin(), branches.end(), mem_fun( bind1st(&Statement::print ), os ));
233}
234
235void FallthruStmt::print( std::ostream &os, int indent ) const {
236        os << string( indent, ' ' ) << "Fall-through statement" << endl;
237}
238
239WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
240        Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
241}
242
243WhileStmt::WhileStmt( const WhileStmt & other ):
244        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
245}
246
247WhileStmt::~WhileStmt() {
248        delete body;
249}
250
251void WhileStmt::print( std::ostream &os, int indent ) const {
252        os << "While on condition: " << endl ;
253        condition->print( os, indent + 4 );
254
255        os << string( indent, ' ' ) << ".... with body: " << endl;
256
257        if ( body != 0 ) body->print( os, indent + 4 );
258}
259
260ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
261        Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
262}
263
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
270ForStmt::~ForStmt() {
271        deleteAll( initialization );
272        delete condition;
273        delete increment;
274        delete body;
275}
276
277void ForStmt::print( std::ostream &os, int indent ) const {
278        os << "Labels: {";
279        for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
280                os << *it << ",";
281        }
282        os << "}" << endl;
283
284        os << string( indent, ' ' ) << "For Statement" << endl ;
285
286        os << string( indent + 2, ' ' ) << "initialization: \n";
287        for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
288                (*it)->print( os, indent + 4 );
289        }
290
291        os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
292        if ( condition != 0 )
293                condition->print( os, indent + 4 );
294
295        os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
296        if ( increment != 0 )
297                increment->print( os, indent + 4 );
298
299        os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
300        if ( body != 0 )
301                body->print( os, indent + 4 );
302
303        os << endl;
304}
305
306TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) :
307        Statement( labels ), block( tryBlock ),  handlers( _handlers ), finallyBlock( _finallyBlock ) {
308}
309
310TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
311        cloneAll( other.handlers, handlers );
312}
313
314TryStmt::~TryStmt() {
315        delete block;
316}
317
318void TryStmt::print( std::ostream &os, int indent ) const {
319        os << "Try Statement" << endl;
320        os << string( indent + 2, ' ' ) << "with block: " << endl;
321        block->print( os, indent + 4 );
322
323        // handlers
324        os << string( indent + 2, ' ' ) << "and handlers: " << endl;
325        for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++)
326                (*i )->print( os, indent + 4 );
327
328        // finally block
329        if ( finallyBlock != 0 ) {
330                os << string( indent + 2, ' ' ) << "Finally block: " << endl;
331                finallyBlock->print( os, indent + 4 );
332        } // if
333}
334
335CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool isCatchRest ) :
336        Statement( labels ), decl ( _decl ), body( _body ), catchRest ( isCatchRest ) {
337}
338
339CatchStmt::CatchStmt( const CatchStmt & other ) :
340        Statement( other ), decl ( maybeClone( other.decl ) ), body( maybeClone( other.body ) ), catchRest ( other.catchRest ) {
341}
342
343CatchStmt::~CatchStmt() {
344        delete decl;
345        delete body;
346}
347
348void CatchStmt::print( std::ostream &os, int indent ) const {
349        os << string( indent, ' ' ) << "Catch Statement" << endl;
350
351        os << string( indent, ' ' ) << "... catching" << endl;
352        if ( decl ) {
353                decl->printShort( os, indent + 4 );
354                os << endl;
355        } else if ( catchRest )
356                os << string( indent + 4 , ' ' ) << "the rest" << endl;
357        else
358                os << string( indent + 4 , ' ' ) << ">>> Error:  this catch clause must have a declaration <<<" << endl;
359}
360
361
362FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
363        assert( labels.empty() ); // finally statement cannot be labeled
364}
365
366FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
367}
368
369FinallyStmt::~FinallyStmt() {
370        delete block;
371}
372
373void FinallyStmt::print( std::ostream &os, int indent ) const {
374        os << string( indent, ' ' ) << "Finally Statement" << endl;
375        os << string( indent + 2, ' ' ) << "with block: " << endl;
376        block->print( os, indent + 4 );
377}
378
379NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
380NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
381
382void NullStmt::print( std::ostream &os, int indent ) const {
383        os << "Null Statement" << endl ;
384}
385
386std::ostream & operator<<( std::ostream & out, Statement * statement ) {
387        statement->print( out );
388        return out;
389}
390
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.