source: translator/SynTree/Statement.cc @ c11e31c

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglergc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newstringwith_gc
Last change on this file since c11e31c was c11e31c, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add inline and attribute qualifiers, cfa.y comment formatting, fix error message in isIntegralType

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