source: src/SynTree/Statement.cc @ 1cdfa82

new-envwith_gc
Last change on this file since 1cdfa82 was 1cdfa82, checked in by Aaron Moss <a3moss@…>, 6 years ago

Merge remote-tracking branch 'origin/master' into with_gc

  • Property mode set to 100644
File size: 13.9 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 : Sun Sep  3 20:46:44 2017
13// Update Count     : 68
14//
15
16#include "SynTree/Statement.h"
17
18#include <stddef.h>                // for NULL
19#include <cassert>                 // for assert, assertf
20#include <iostream>                // for operator<<, basic_ostream, endl
21#include <list>                    // for list, list<>::const_iterator, _Lis...
22#include <string>                  // for operator<<, string, char_traits
23
24#include "Common/SemanticError.h"  // for SemanticError
25#include "Common/utility.h"        // for maybeClone, cloneAll, deleteAll
26#include "Declaration.h"           // for Declaration
27#include "Expression.h"            // for Expression, ConstantExpr
28#include "Statement.h"             // for Statement, ForStmt, AsmStmt, Catch...
29#include "SynTree/Label.h"         // for Label, operator<<
30
31using std::string;
32using std::endl;
33
34Statement::Statement( const std::list<Label> & labels ) : labels( labels ) {}
35
36void Statement::print( std::ostream & os, Indenter indent ) const {
37        if ( ! labels.empty() ) {
38                os << indent << "... Labels: {";
39                for ( const Label & l : labels ) {
40                        os << l << ",";
41                }
42                os << "}" << endl;
43        }
44}
45
46ExprStmt::ExprStmt( Expression *expr ) : Statement(), expr( expr ) {}
47
48ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
49
50void ExprStmt::print( std::ostream &os, Indenter indent ) const {
51        os << "Expression Statement:" << endl << indent+1;
52        expr->print( os, indent+1 );
53}
54
55
56AsmStmt::AsmStmt( bool voltile, Expression *instruction, std::list<Expression *> output, std::list<Expression *> input, std::list<ConstantExpr *> clobber, std::list<Label> gotolabels ) : Statement(), voltile( voltile ), instruction( instruction ), output( output ), input( input ), clobber( clobber ), gotolabels( gotolabels ) {}
57
58AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
59  cloneAll( other.output, output );
60  cloneAll( other.input, input );
61  cloneAll( other.clobber, clobber );
62}
63
64void AsmStmt::print( std::ostream &os, Indenter indent ) const {
65        os << "Assembler Statement:" << endl;
66        os << indent+1 << "instruction: " << endl << indent;
67        instruction->print( os, indent+1 );
68        if ( ! output.empty() ) {
69                os << endl << indent+1 << "output: " << endl;
70                printAll( output, os, indent+1 );
71        } // if
72        if ( ! input.empty() ) {
73                os << indent+1 << "input: " << endl;
74                printAll( input, os, indent+1 );
75        } // if
76        if ( ! clobber.empty() ) {
77                os << indent+1 << "clobber: " << endl;
78                printAll( clobber, os, indent+1 );
79        } // if
80}
81
82
83const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
84
85BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) :
86        Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
87        //actually this is a syntactic error signaled by the parser
88        if ( type == BranchStmt::Goto && target.empty() ) {
89                SemanticError( target.get_statement()->location, "goto without target");
90        }
91}
92
93BranchStmt::BranchStmt( Expression *computedTarget, Type type ) throw ( SemanticErrorException ) :
94        Statement(), computedTarget( computedTarget ), type( type ) {
95        if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
96                SemanticError( computedTarget->location, "Computed target not valid in branch statement");
97        }
98}
99
100void BranchStmt::print( std::ostream &os, Indenter indent ) const {
101        os << "Branch (" << brType[type] << ")" << endl ;
102        if ( target != "" ) os << indent+1 << "with target: " << target << endl;
103        if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
104        if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
105}
106
107ReturnStmt::ReturnStmt( Expression *expr ) : Statement(), expr( expr ) {}
108
109ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
110
111void ReturnStmt::print( std::ostream &os, Indenter indent ) const {
112        os << "Return Statement, returning: ";
113        if ( expr != nullptr ) {
114                os << endl << indent+1;
115                expr->print( os, indent+1 );
116        }
117        os << endl;
118}
119
120IfStmt::IfStmt( Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
121        Statement(), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
122
123IfStmt::IfStmt( const IfStmt & other ) :
124        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
125        cloneAll( other.initialization, initialization );
126}
127
128void IfStmt::print( std::ostream &os, Indenter indent ) const {
129        os << "If on condition: " << endl;
130        os << indent+1;
131        condition->print( os, indent+1 );
132
133        if ( !initialization.empty() ) {
134                os << indent << "... with initialization: \n";
135                for ( const Statement * stmt : initialization ) {
136                        os << indent+1;
137                        stmt->print( os, indent+1 );
138                }
139                os << endl;
140        }
141
142        os << indent << "... then: " << endl;
143
144        os << indent+1;
145        thenPart->print( os, indent+1 );
146
147        if ( elsePart != 0 ) {
148                os << indent << "... else: " << endl;
149                os << indent+1;
150                elsePart->print( os, indent+1 );
151        } // if
152}
153
154SwitchStmt::SwitchStmt( Expression * condition, const std::list<Statement *> &statements ):
155        Statement(), condition( condition ), statements( statements ) {
156}
157
158SwitchStmt::SwitchStmt( const SwitchStmt & other ):
159        Statement( other ), condition( maybeClone( other.condition ) ) {
160        cloneAll( other.statements, statements );
161}
162
163void SwitchStmt::print( std::ostream &os, Indenter indent ) const {
164        os << "Switch on condition: ";
165        condition->print( os );
166        os << endl;
167
168        for ( const Statement * stmt : statements ) {
169                stmt->print( os, indent+1 );
170        }
171}
172
173CaseStmt::CaseStmt( Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticErrorException ) :
174        Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) {
175        if ( isDefault() && condition != 0 ) SemanticError( condition, "default case with condition: " );
176}
177
178CaseStmt::CaseStmt( const CaseStmt & other ) :
179        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
180        cloneAll( other.stmts, stmts );
181}
182
183CaseStmt * CaseStmt::makeDefault( const std::list<Label> & labels, std::list<Statement *> stmts ) {
184        CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
185        stmt->labels = labels;
186        return stmt;
187}
188
189void CaseStmt::print( std::ostream &os, Indenter indent ) const {
190        if ( isDefault() ) os << indent << "Default ";
191        else {
192                os << indent << "Case ";
193                condition->print( os, indent );
194        } // if
195        os << endl;
196
197        for ( Statement * stmt : stmts ) {
198                os << indent+1;
199                stmt->print( os, indent+1 );
200        }
201}
202
203WhileStmt::WhileStmt( Expression *condition, Statement *body, bool isDoWhile ):
204        Statement(), condition( condition), body( body), isDoWhile( isDoWhile) {
205}
206
207WhileStmt::WhileStmt( const WhileStmt & other ):
208        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
209}
210
211void WhileStmt::print( std::ostream &os, Indenter indent ) const {
212        os << "While on condition: " << endl ;
213        condition->print( os, indent+1 );
214
215        os << indent << "... with body: " << endl;
216
217        if ( body != 0 ) body->print( os, indent+1 );
218}
219
220ForStmt::ForStmt( std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
221        Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
222}
223
224ForStmt::ForStmt( const ForStmt & other ):
225        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
226                cloneAll( other.initialization, initialization );
227
228}
229
230void ForStmt::print( std::ostream &os, Indenter indent ) const {
231        Statement::print( os, indent ); // print labels
232
233        os << "For Statement" << endl;
234
235        if ( ! initialization.empty() ) {
236                os << indent << "... initialization: \n";
237                for ( Statement * stmt : initialization ) {
238                        os << indent+1;
239                        stmt->print( os, indent+1 );
240                }
241        }
242
243        if ( condition != nullptr ) {
244                os << indent << "... condition: \n" << indent+1;
245                condition->print( os, indent+1 );
246        }
247
248        if ( increment != nullptr ) {
249                os << "\n" << indent << "... increment: \n" << indent+1;
250                increment->print( os, indent+1 );
251        }
252
253        if ( body != 0 ) {
254                os << "\n" << indent << "... with body: \n" << indent+1;
255                body->print( os, indent+1 );
256        }
257        os << endl;
258}
259
260ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
261                Statement(), kind(kind), expr(expr), target(target)     {
262        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
263}
264
265ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
266        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
267}
268
269void ThrowStmt::print( std::ostream &os, Indenter indent) const {
270        if ( target ) os << "Non-Local ";
271        os << "Throw Statement, raising: ";
272        expr->print(os, indent+1);
273        if ( target ) {
274                os << "... at: ";
275                target->print(os, indent+1);
276        }
277}
278
279TryStmt::TryStmt( CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
280        Statement(), block( tryBlock ),  handlers( handlers ), finallyBlock( finallyBlock ) {
281}
282
283TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
284        cloneAll( other.handlers, handlers );
285}
286
287void TryStmt::print( std::ostream &os, Indenter indent ) const {
288        os << "Try Statement" << endl;
289        os << indent << "... with block:" << endl << indent+1;
290        block->print( os, indent+1 );
291
292        // handlers
293        os << indent << "... and handlers:" << endl;
294        for ( const CatchStmt * stmt : handlers ) {
295                os << indent+1;
296                stmt->print( os, indent+1 );
297        }
298
299        // finally block
300        if ( finallyBlock != 0 ) {
301                os << indent << "... and finally:" << endl << indent+1;
302                finallyBlock->print( os, indent+1 );
303        } // if
304}
305
306CatchStmt::CatchStmt( Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
307        Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
308                assertf( decl, "Catch clause must have a declaration." );
309}
310
311CatchStmt::CatchStmt( const CatchStmt & other ) :
312        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
313}
314
315void CatchStmt::print( std::ostream &os, Indenter indent ) const {
316        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
317
318        os << indent << "... catching: ";
319        decl->printShort( os, indent+1 );
320        os << endl;
321
322        if ( cond ) {
323                os << indent << "... with conditional:" << endl << indent+1;
324                cond->print( os, indent+1 );
325        }
326
327        os << indent << "... with block:" << endl;
328        os << indent+1;
329        body->print( os, indent+1 );
330}
331
332
333FinallyStmt::FinallyStmt( CompoundStmt *block ) : Statement(), block( block ) {
334}
335
336FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
337}
338
339void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
340        os << "Finally Statement" << endl;
341        os << indent << "... with block:" << endl << indent+1;
342        block->print( os, indent+1 );
343}
344
345WaitForStmt::WaitForStmt() : Statement() {
346        timeout.time      = nullptr;
347        timeout.statement = nullptr;
348        timeout.condition = nullptr;
349        orelse .statement = nullptr;
350        orelse .condition = nullptr;
351}
352
353WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
354        clauses.reserve( other.clauses.size() );
355        for( auto & ocl : other.clauses ) {
356                clauses.emplace_back();
357                clauses.back().target.function = ocl.target.function->clone();
358                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
359                clauses.back().statement = ocl.statement->clone();
360                clauses.back().condition = ocl.condition->clone();
361        }
362
363        timeout.time      = other.timeout.time     ->clone();
364        timeout.statement = other.timeout.statement->clone();
365        timeout.condition = other.timeout.condition->clone();
366        orelse .statement = other.orelse .statement->clone();
367        orelse .condition = other.orelse .condition->clone();
368}
369
370void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
371        os << "Waitfor Statement" << endl;
372        os << indent << "... with block:" << endl << indent+1;
373        // block->print( os, indent + 4 );
374}
375
376
377WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Statement(), exprs( exprs ), stmt( stmt ) {}
378WithStmt::WithStmt( const WithStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) {
379        cloneAll( other.exprs, exprs );
380}
381
382void WithStmt::print( std::ostream & os, Indenter indent ) const {
383        os << "With statement" << endl;
384        os << indent << "... with expressions: " << endl;
385        printAll( exprs, os, indent+1 );
386        os << indent << "... with statement:" << endl << indent+1;
387        stmt->print( os, indent+1 );
388}
389
390
391NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
392}
393
394void NullStmt::print( std::ostream &os, Indenter indent ) const {
395        os << "Null Statement" << endl;
396        Statement::print( os, indent );
397}
398
399ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
400        assert( callStmt );
401}
402
403ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
404}
405
406void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
407        os << "Implicit Ctor Dtor Statement" << endl;
408        os << indent << "... with Ctor/Dtor: ";
409        callStmt->print( os, indent+1);
410        os << endl;
411}
412
413// Local Variables: //
414// tab-width: 4 //
415// mode: c++ //
416// compile-command: "make install" //
417// End: //
Note: See TracBrowser for help on using the repository browser.