source: src/SynTree/Statement.cc@ b762122

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 b762122 was 7f5566b, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

asm statement, memory leaks

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