source: src/SynTree/Statement.cc @ 135b431

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 135b431 was 135b431, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

First step for waitfor statments, does nothing but is there

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