source: src/SynTree/Statement.cc @ f980549

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f980549 was e149f77, checked in by Thierry Delisle <tdelisle@…>, 7 years ago
  • moved print routine to base syntax node and implementated in code gen.
  • added virtual and override where needed in the syntree.
  • Property mode set to 100644
File size: 16.3 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( std::list<Label> labels ) : labels( labels ) {}
35
36void Statement::print( __attribute__((unused)) std::ostream &, __attribute__((unused)) int indent ) const {}
37
38Statement::~Statement() {}
39
40ExprStmt::ExprStmt( std::list<Label> labels, Expression *expr ) : Statement( labels ), expr( expr ) {}
41
42ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
43
44ExprStmt::~ExprStmt() {
45        delete expr;
46}
47
48void ExprStmt::print( std::ostream &os, int indent ) const {
49        os << "Expression Statement:" << endl << std::string( indent + 2, ' ' );
50        expr->print( os, indent + 2 );
51}
52
53
54AsmStmt::AsmStmt( std::list<Label> labels, bool voltile, Expression *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 ) {}
55
56AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
57  cloneAll( other.output, output );
58  cloneAll( other.input, input );
59  cloneAll( other.clobber, clobber );
60}
61
62AsmStmt::~AsmStmt() {
63        delete instruction;
64        deleteAll( output );
65        deleteAll( input );
66        deleteAll( clobber );
67}
68
69void AsmStmt::print( std::ostream &os, int indent ) const {
70        os << "Assembler Statement:" << endl;
71        os << std::string( indent, ' ' ) << "instruction: " << endl << std::string( indent, ' ' );
72        instruction->print( os, indent + 2 );
73        if ( ! output.empty() ) {
74                os << endl << std::string( indent, ' ' ) << "output: " << endl;
75                printAll( output, os, indent + 2 );
76        } // if
77        if ( ! input.empty() ) {
78                os << std::string( indent, ' ' ) << "input: " << endl << std::string( indent, ' ' );
79                printAll( input, os, indent + 2 );
80        } // if
81        if ( ! clobber.empty() ) {
82                os << std::string( indent, ' ' ) << "clobber: " << endl;
83                printAll( clobber, os, indent + 2 );
84        } // if
85}
86
87
88const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
89
90BranchStmt::BranchStmt( std::list<Label> labels, Label target, Type type ) throw ( SemanticError ) :
91        Statement( labels ), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
92        //actually this is a syntactic error signaled by the parser
93        if ( type == BranchStmt::Goto && target.empty() ) {
94                throw SemanticError("goto without target");
95        }
96}
97
98BranchStmt::BranchStmt( std::list<Label> labels, Expression *computedTarget, Type type ) throw ( SemanticError ) :
99        Statement( labels ), computedTarget( computedTarget ), type( type ) {
100        if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
101                throw SemanticError("Computed target not valid in branch statement");
102        }
103}
104
105void BranchStmt::print( std::ostream &os, int indent ) const {
106        os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ;
107        if ( target != "" ) os << string( indent+2, ' ' ) << "with target: " << target << endl;
108        if ( originalTarget != "" ) os << string( indent+2, ' ' ) << "with original target: " << originalTarget << endl;
109        if ( computedTarget != nullptr ) os << string( indent+2, ' ' ) << "with computed target: " << computedTarget << endl;
110}
111
112ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *expr ) : Statement( labels ), expr( expr ) {}
113
114ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
115
116ReturnStmt::~ReturnStmt() {
117        delete expr;
118}
119
120void ReturnStmt::print( std::ostream &os, int indent ) const {
121        os <<  "Return Statement, returning: ";
122        if ( expr != 0 ) {
123                os << endl << string( indent+2, ' ' );
124                expr->print( os, indent + 2 );
125        }
126        os << endl;
127}
128
129IfStmt::IfStmt( std::list<Label> labels, Expression *condition, Statement *thenPart, Statement *elsePart, std::list<Statement *> initialization ):
130        Statement( labels ), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
131
132IfStmt::IfStmt( const IfStmt & other ) :
133        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
134        cloneAll( other.initialization, initialization );
135}
136
137IfStmt::~IfStmt() {
138        deleteAll( initialization );
139        delete condition;
140        delete thenPart;
141        delete elsePart;
142}
143
144void IfStmt::print( std::ostream &os, int indent ) const {
145        os << "If on condition: " << endl ;
146        os << string( indent+4, ' ' );
147        condition->print( os, indent + 4 );
148
149        if ( !initialization.empty() ) {
150                os << string( indent + 2, ' ' ) << "initialization: \n";
151                for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
152                        os << string( indent + 4, ' ' );
153                        (*it)->print( os, indent + 4 );
154                }
155                os << endl;
156        }
157
158        os << string( indent+2, ' ' ) << "... then: " << endl;
159
160        os << string( indent+4, ' ' );
161        thenPart->print( os, indent + 4 );
162
163        if ( elsePart != 0 ) {
164                os << string( indent+2, ' ' ) << "... else: " << endl;
165                os << string( indent+4, ' ' );
166                elsePart->print( os, indent + 4 );
167        } // if
168}
169
170SwitchStmt::SwitchStmt( std::list<Label> labels, Expression * condition, const std::list<Statement *> &statements ):
171        Statement( labels ), condition( condition ), statements( statements ) {
172}
173
174SwitchStmt::SwitchStmt( const SwitchStmt & other ):
175        Statement( other ), condition( maybeClone( other.condition ) ) {
176        cloneAll( other.statements, statements );
177}
178
179SwitchStmt::~SwitchStmt() {
180        delete condition;
181        // destroy statements
182        deleteAll( statements );
183}
184
185void SwitchStmt::print( std::ostream &os, int indent ) const {
186        os << "Switch on condition: ";
187        condition->print( os );
188        os << endl;
189
190        // statements
191        std::list<Statement *>::const_iterator i;
192        for ( i = statements.begin(); i != statements.end(); i++)
193                (*i)->print( os, indent + 4 );
194
195        //for_each( statements.begin(), statements.end(), mem_fun( bind1st(&Statement::print ), os ));
196}
197
198CaseStmt::CaseStmt( std::list<Label> labels, Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticError ) :
199        Statement( labels ), condition( condition ), stmts( statements ), _isDefault( deflt ) {
200        if ( isDefault() && condition != 0 )
201                throw SemanticError("default with conditions");
202}
203
204CaseStmt::CaseStmt( const CaseStmt & other ) :
205        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
206        cloneAll( other.stmts, stmts );
207}
208
209CaseStmt::~CaseStmt() {
210        delete condition;
211        deleteAll( stmts );
212}
213
214CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> stmts ) {
215        return new CaseStmt( labels, 0, stmts, true );
216}
217
218void CaseStmt::print( std::ostream &os, int indent ) const {
219        os << string( indent, ' ' );
220
221        if ( isDefault() )
222                os << "Default ";
223        else {
224                os << "Case ";
225                condition->print( os );
226        } // if
227
228        os << endl;
229
230        std::list<Statement *>::const_iterator i;
231        for ( i = stmts.begin(); i != stmts.end(); i++)
232                (*i )->print( os, indent + 4 );
233}
234
235WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition, Statement *body, bool isDoWhile ):
236        Statement( labels ), condition( condition), body( body), isDoWhile( isDoWhile) {
237}
238
239WhileStmt::WhileStmt( const WhileStmt & other ):
240        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
241}
242
243WhileStmt::~WhileStmt() {
244        delete body;
245        delete condition;
246}
247
248void WhileStmt::print( std::ostream &os, int indent ) const {
249        os << "While on condition: " << endl ;
250        condition->print( os, indent + 4 );
251
252        os << string( indent, ' ' ) << ".... with body: " << endl;
253
254        if ( body != 0 ) body->print( os, indent + 4 );
255}
256
257ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization, Expression *condition, Expression *increment, Statement *body ):
258        Statement( labels ), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
259}
260
261ForStmt::ForStmt( const ForStmt & other ):
262        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
263                cloneAll( other.initialization, initialization );
264
265}
266
267ForStmt::~ForStmt() {
268        deleteAll( initialization );
269        delete condition;
270        delete increment;
271        delete body;
272}
273
274void ForStmt::print( std::ostream &os, int indent ) const {
275        os << "Labels: {";
276        for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
277                os << *it << ",";
278        }
279        os << "}" << endl;
280
281        os << string( indent, ' ' ) << "For Statement" << endl ;
282
283        os << string( indent + 2, ' ' ) << "initialization: \n";
284        for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
285                os << string( indent + 4, ' ' );
286                (*it)->print( os, indent + 4 );
287        }
288
289        os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
290        if ( condition != 0 ) {
291                os << string( indent + 4, ' ' );
292                condition->print( os, indent + 4 );
293        }
294
295        os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
296        if ( increment != 0 ) {
297                os << string( indent + 4, ' ' );
298                increment->print( os, indent + 4 );
299        }
300
301        os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
302        if ( body != 0 ) {
303                os << string( indent + 4, ' ' );
304                body->print( os, indent + 4 );
305        }
306
307        os << endl;
308}
309
310ThrowStmt::ThrowStmt( std::list<Label> labels, Kind kind, Expression * expr, Expression * target ) :
311                Statement( labels ), kind(kind), expr(expr), target(target)     {
312        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
313}
314
315ThrowStmt::ThrowStmt( const ThrowStmt &other ) :
316        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
317}
318
319ThrowStmt::~ThrowStmt() {
320        delete expr;
321        delete target;
322}
323
324void ThrowStmt::print( std::ostream &os, int indent) const {
325        if ( target ) {
326                os << "Non-Local ";
327        }
328        os << "Throw Statement, raising: ";
329        expr->print(os, indent + 4);
330        if ( target ) {
331                os << "At: ";
332                target->print(os, indent + 4);
333        }
334}
335
336TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<CatchStmt *> &handlers, FinallyStmt *finallyBlock ) :
337        Statement( labels ), block( tryBlock ),  handlers( handlers ), finallyBlock( finallyBlock ) {
338}
339
340TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
341        cloneAll( other.handlers, handlers );
342}
343
344TryStmt::~TryStmt() {
345        delete block;
346        deleteAll( handlers );
347        delete finallyBlock;
348}
349
350void TryStmt::print( std::ostream &os, int indent ) const {
351        os << "Try Statement" << endl;
352        os << string( indent + 2, ' ' ) << "with block:" << endl;
353        os << string( indent + 4, ' ' );
354        block->print( os, indent + 4 );
355
356        // handlers
357        os << string( indent + 2, ' ' ) << "and handlers:" << endl;
358        for ( std::list<CatchStmt *>::const_iterator i = handlers.begin(); i != handlers.end(); i++) {
359                os << string( indent + 4, ' ' );
360                (*i )->print( os, indent + 4 );
361        }
362
363        // finally block
364        if ( finallyBlock != 0 ) {
365                os << string( indent + 2, ' ' ) << "and finally:" << endl;
366                finallyBlock->print( os, indent + 4 );
367        } // if
368}
369
370CatchStmt::CatchStmt( std::list<Label> labels, Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
371        Statement( labels ), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
372}
373
374CatchStmt::CatchStmt( const CatchStmt & other ) :
375        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
376}
377
378CatchStmt::~CatchStmt() {
379        delete decl;
380        delete body;
381}
382
383void CatchStmt::print( std::ostream &os, int indent ) const {
384        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
385
386        os << string( indent + 2, ' ' ) << "... catching: ";
387        if ( decl ) {
388                decl->printShort( os, indent + 4 );
389                os << endl;
390        }
391        else
392                os << string( indent + 4 , ' ' ) << ">>> Error:  this catch clause must have a declaration <<<" << endl;
393
394        if ( cond ) {
395                os << string( indent + 2, ' ' ) << "with conditional:" << endl;
396                os << string( indent + 4, ' ' );
397                cond->print( os, indent + 4 );
398        }
399        else
400                os << string( indent + 2, ' ' ) << "with no conditional" << endl;
401
402        os << string( indent + 2, ' ' ) << "with block:" << endl;
403        os << string( indent + 4, ' ' );
404        body->print( os, indent + 4 );
405}
406
407
408FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *block ) : Statement( labels ), block( block ) {
409        assert( labels.empty() ); // finally statement cannot be labeled
410}
411
412FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
413}
414
415FinallyStmt::~FinallyStmt() {
416        delete block;
417}
418
419void FinallyStmt::print( std::ostream &os, int indent ) const {
420        os << "Finally Statement" << endl;
421        os << string( indent + 2, ' ' ) << "with block:" << endl;
422        os << string( indent + 4, ' ' );
423        block->print( os, indent + 4 );
424}
425
426WaitForStmt::WaitForStmt( std::list<Label> labels ) : Statement( labels ) {
427        timeout.time      = nullptr;
428        timeout.statement = nullptr;
429        timeout.condition = nullptr;
430        orelse .statement = nullptr;
431        orelse .condition = nullptr;
432}
433
434WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
435        clauses.reserve( other.clauses.size() );
436        for( auto & ocl : other.clauses ) {
437                clauses.emplace_back();
438                clauses.back().target.function = ocl.target.function->clone();
439                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
440                clauses.back().statement = ocl.statement->clone();
441                clauses.back().condition = ocl.condition->clone();
442        }
443
444        timeout.time      = other.timeout.time     ->clone();
445        timeout.statement = other.timeout.statement->clone();
446        timeout.condition = other.timeout.condition->clone();
447        orelse .statement = other.orelse .statement->clone();
448        orelse .condition = other.orelse .condition->clone();
449}
450
451WaitForStmt::~WaitForStmt() {
452        for( auto & clause : clauses ) {
453                delete clause.target.function;
454                deleteAll( clause.target.arguments );
455                delete clause.statement;
456                delete clause.condition;
457        }
458
459        delete timeout.time;
460        delete timeout.statement;
461        delete timeout.condition;
462
463        delete orelse.statement;
464        delete orelse.condition;
465}
466
467void WaitForStmt::print( std::ostream &os, int indent ) const {
468        os << "Waitfor Statement" << endl;
469        os << string( indent + 2, ' ' ) << "with block:" << endl;
470        os << string( indent + 4, ' ' );
471        // block->print( os, indent + 4 );
472}
473
474NullStmt::NullStmt( std::list<Label> labels ) : Statement( labels ) {}
475NullStmt::NullStmt() : Statement( std::list<Label>() ) {}
476
477void NullStmt::print( std::ostream &os, __attribute__((unused)) int indent ) const {
478        os << "Null Statement" << endl ;
479}
480
481ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement( std::list<Label>() ), callStmt( callStmt ) {
482        assert( callStmt );
483}
484
485ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
486}
487
488ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
489        delete callStmt;
490}
491
492void ImplicitCtorDtorStmt::print( std::ostream &os, int indent ) const {
493        os << "Implicit Ctor Dtor Statement" << endl;
494        os << string( indent + 2, ' ' ) << "with Ctor/Dtor: ";
495        callStmt->print( os, indent + 2);
496        os << endl;
497}
498
499// Local Variables: //
500// tab-width: 4 //
501// mode: c++ //
502// compile-command: "make install" //
503// End: //
Note: See TracBrowser for help on using the repository browser.