source: src/SynTree/Statement.cc @ 843054c2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 843054c2 was 843054c2, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: seventh groups of files

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