source: src/SynTree/Statement.cc @ 826ee62

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 826ee62 was 37cdd97, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added a ast node for suspend statements

  • Property mode set to 100644
File size: 17.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 : Andrew Beach
12// Last Modified On : Mon Jan 20 16:03:00 2020
13// Update Count     : 71
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
46Statement::~Statement() {}
47
48ExprStmt::ExprStmt( Expression * expr ) : Statement(), expr( expr ) {}
49
50ExprStmt::ExprStmt( const ExprStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
51
52ExprStmt::~ExprStmt() {
53        delete expr;
54}
55
56void ExprStmt::print( std::ostream & os, Indenter indent ) const {
57        os << "Expression Statement:" << endl << indent+1;
58        expr->print( os, indent+1 );
59}
60
61
62AsmStmt::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 ) {}
63
64AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
65  cloneAll( other.output, output );
66  cloneAll( other.input, input );
67  cloneAll( other.clobber, clobber );
68}
69
70AsmStmt::~AsmStmt() {
71        delete instruction;
72        deleteAll( output );
73        deleteAll( input );
74        deleteAll( clobber );
75}
76
77void AsmStmt::print( std::ostream & os, Indenter indent ) const {
78        os << "Assembler Statement:" << endl;
79        os << indent+1 << "instruction: " << endl << indent;
80        instruction->print( os, indent+1 );
81        if ( ! output.empty() ) {
82                os << endl << indent+1 << "output: " << endl;
83                printAll( output, os, indent+1 );
84        } // if
85        if ( ! input.empty() ) {
86                os << indent+1 << "input: " << endl;
87                printAll( input, os, indent+1 );
88        } // if
89        if ( ! clobber.empty() ) {
90                os << indent+1 << "clobber: " << endl;
91                printAll( clobber, os, indent+1 );
92        } // if
93}
94
95
96DirectiveStmt::DirectiveStmt( const std::string & directive ) : Statement(), directive( directive ) {}
97
98void DirectiveStmt::print( std::ostream & os, Indenter ) const {
99        os << "GCC Directive:" << directive << endl;
100}
101
102
103const char * BranchStmt::brType[] = {
104        "Goto", "Break", "Continue", "Fall Through", "Fall Through Default",
105};
106
107BranchStmt::BranchStmt( Label target, Type type ) throw ( SemanticErrorException ) :
108        Statement(), originalTarget( target ), target( target ), computedTarget( nullptr ), type( type ) {
109        //actually this is a syntactic error signaled by the parser
110        if ( type == BranchStmt::Goto && target.empty() ) {
111                SemanticError( target.get_statement()->location, "goto without target");
112        }
113}
114
115BranchStmt::BranchStmt( Expression * computedTarget, Type type ) throw ( SemanticErrorException ) :
116        Statement(), computedTarget( computedTarget ), type( type ) {
117        if ( type != BranchStmt::Goto || computedTarget == nullptr ) {
118                SemanticError( computedTarget->location, "Computed target not valid in branch statement");
119        }
120}
121
122void BranchStmt::print( std::ostream & os, Indenter indent ) const {
123        assert(type < 5);
124        os << "Branch (" << brType[type] << ")" << endl ;
125        if ( target != "" ) os << indent+1 << "with target: " << target << endl;
126        if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
127        if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
128}
129
130ReturnStmt::ReturnStmt( Expression * expr ) : Statement(), expr( expr ) {}
131
132ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
133
134ReturnStmt::~ReturnStmt() {
135        delete expr;
136}
137
138void ReturnStmt::print( std::ostream & os, Indenter indent ) const {
139        os << "Return Statement, returning: ";
140        if ( expr != nullptr ) {
141                os << endl << indent+1;
142                expr->print( os, indent+1 );
143        }
144        os << endl;
145}
146
147IfStmt::IfStmt( Expression * condition, Statement * thenPart, Statement * elsePart, std::list<Statement *> initialization ):
148        Statement(), condition( condition ), thenPart( thenPart ), elsePart( elsePart ), initialization( initialization ) {}
149
150IfStmt::IfStmt( const IfStmt & other ) :
151        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {
152        cloneAll( other.initialization, initialization );
153}
154
155IfStmt::~IfStmt() {
156        deleteAll( initialization );
157        delete condition;
158        delete thenPart;
159        delete elsePart;
160}
161
162void IfStmt::print( std::ostream & os, Indenter indent ) const {
163        os << "If on condition: " << endl;
164        os << indent+1;
165        condition->print( os, indent+1 );
166
167        if ( !initialization.empty() ) {
168                os << indent << "... with initialization: \n";
169                for ( const Statement * stmt : initialization ) {
170                        os << indent+1;
171                        stmt->print( os, indent+1 );
172                }
173                os << endl;
174        }
175
176        os << indent << "... then: " << endl;
177
178        os << indent+1;
179        thenPart->print( os, indent+1 );
180
181        if ( elsePart != nullptr ) {
182                os << indent << "... else: " << endl;
183                os << indent+1;
184                elsePart->print( os, indent+1 );
185        } // if
186}
187
188SwitchStmt::SwitchStmt( Expression * condition, const std::list<Statement *> & statements ):
189        Statement(), condition( condition ), statements( statements ) {
190}
191
192SwitchStmt::SwitchStmt( const SwitchStmt & other ):
193        Statement( other ), condition( maybeClone( other.condition ) ) {
194        cloneAll( other.statements, statements );
195}
196
197SwitchStmt::~SwitchStmt() {
198        delete condition;
199        // destroy statements
200        deleteAll( statements );
201}
202
203void SwitchStmt::print( std::ostream & os, Indenter indent ) const {
204        os << "Switch on condition: ";
205        condition->print( os );
206        os << endl;
207
208        for ( const Statement * stmt : statements ) {
209                stmt->print( os, indent+1 );
210        }
211}
212
213CaseStmt::CaseStmt( Expression * condition, const std::list<Statement *> & statements, bool deflt ) throw ( SemanticErrorException ) :
214        Statement(), condition( condition ), stmts( statements ), _isDefault( deflt ) {
215        if ( isDefault() && condition != nullptr ) SemanticError( condition, "default case with condition: " );
216}
217
218CaseStmt::CaseStmt( const CaseStmt & other ) :
219        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
220        cloneAll( other.stmts, stmts );
221}
222
223CaseStmt::~CaseStmt() {
224        delete condition;
225        deleteAll( stmts );
226}
227
228CaseStmt * CaseStmt::makeDefault( const std::list<Label> & labels, std::list<Statement *> stmts ) {
229        CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
230        stmt->labels = labels;
231        return stmt;
232}
233
234void CaseStmt::print( std::ostream & os, Indenter indent ) const {
235        if ( isDefault() ) os << indent << "Default ";
236        else {
237                os << indent << "Case ";
238                condition->print( os, indent );
239        } // if
240        os << endl;
241
242        for ( Statement * stmt : stmts ) {
243                os << indent+1;
244                stmt->print( os, indent+1 );
245        }
246}
247
248WhileStmt::WhileStmt( Expression * condition, Statement * body, std::list< Statement * > & initialization, bool isDoWhile ):
249        Statement(), condition( condition), body( body), initialization( initialization ), isDoWhile( isDoWhile) {
250}
251
252WhileStmt::WhileStmt( const WhileStmt & other ):
253        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
254}
255
256WhileStmt::~WhileStmt() {
257        delete body;
258        delete condition;
259}
260
261void WhileStmt::print( std::ostream & os, Indenter indent ) const {
262        os << "While on condition: " << endl ;
263        condition->print( os, indent+1 );
264
265        os << indent << "... with body: " << endl;
266
267        if ( body != nullptr ) body->print( os, indent+1 );
268}
269
270ForStmt::ForStmt( std::list<Statement *> initialization, Expression * condition, Expression * increment, Statement * body ):
271        Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ) {
272}
273
274ForStmt::ForStmt( const ForStmt & other ):
275        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
276                cloneAll( other.initialization, initialization );
277
278}
279
280ForStmt::~ForStmt() {
281        deleteAll( initialization );
282        delete condition;
283        delete increment;
284        delete body;
285}
286
287void ForStmt::print( std::ostream & os, Indenter indent ) const {
288        Statement::print( os, indent ); // print labels
289
290        os << "For Statement" << endl;
291
292        if ( ! initialization.empty() ) {
293                os << indent << "... initialization: \n";
294                for ( Statement * stmt : initialization ) {
295                        os << indent+1;
296                        stmt->print( os, indent+1 );
297                }
298        }
299
300        if ( condition != nullptr ) {
301                os << indent << "... condition: \n" << indent+1;
302                condition->print( os, indent+1 );
303        }
304
305        if ( increment != nullptr ) {
306                os << "\n" << indent << "... increment: \n" << indent+1;
307                increment->print( os, indent+1 );
308        }
309
310        if ( body != nullptr ) {
311                os << "\n" << indent << "... with body: \n" << indent+1;
312                body->print( os, indent+1 );
313        }
314        os << endl;
315}
316
317ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
318                Statement(), kind(kind), expr(expr), target(target)     {
319        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
320}
321
322ThrowStmt::ThrowStmt( const ThrowStmt & other ) :
323        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
324}
325
326ThrowStmt::~ThrowStmt() {
327        delete expr;
328        delete target;
329}
330
331void ThrowStmt::print( std::ostream & os, Indenter indent) const {
332        if ( target ) os << "Non-Local ";
333        os << "Throw Statement, raising: ";
334        expr->print(os, indent+1);
335        if ( target ) {
336                os << "... at: ";
337                target->print(os, indent+1);
338        }
339}
340
341TryStmt::TryStmt( CompoundStmt * tryBlock, std::list<CatchStmt *> & handlers, FinallyStmt * finallyBlock ) :
342        Statement(), block( tryBlock ),  handlers( handlers ), finallyBlock( finallyBlock ) {
343}
344
345TryStmt::TryStmt( const TryStmt & other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
346        cloneAll( other.handlers, handlers );
347}
348
349TryStmt::~TryStmt() {
350        delete block;
351        deleteAll( handlers );
352        delete finallyBlock;
353}
354
355void TryStmt::print( std::ostream & os, Indenter indent ) const {
356        os << "Try Statement" << endl;
357        os << indent << "... with block:" << endl << indent+1;
358        block->print( os, indent+1 );
359
360        // handlers
361        os << indent << "... and handlers:" << endl;
362        for ( const CatchStmt * stmt : handlers ) {
363                os << indent+1;
364                stmt->print( os, indent+1 );
365        }
366
367        // finally block
368        if ( finallyBlock != nullptr ) {
369                os << indent << "... and finally:" << endl << indent+1;
370                finallyBlock->print( os, indent+1 );
371        } // if
372}
373
374CatchStmt::CatchStmt( Kind kind, Declaration * decl, Expression * cond, Statement * body ) :
375        Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
376                assertf( decl, "Catch clause must have a declaration." );
377}
378
379CatchStmt::CatchStmt( const CatchStmt & other ) :
380        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
381}
382
383CatchStmt::~CatchStmt() {
384        delete decl;
385        delete body;
386}
387
388void CatchStmt::print( std::ostream & os, Indenter indent ) const {
389        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
390
391        os << indent << "... catching: ";
392        decl->printShort( os, indent+1 );
393        os << endl;
394
395        if ( cond ) {
396                os << indent << "... with conditional:" << endl << indent+1;
397                cond->print( os, indent+1 );
398        }
399
400        os << indent << "... with block:" << endl;
401        os << indent+1;
402        body->print( os, indent+1 );
403}
404
405
406FinallyStmt::FinallyStmt( CompoundStmt * block ) : Statement(), block( block ) {
407}
408
409FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
410}
411
412FinallyStmt::~FinallyStmt() {
413        delete block;
414}
415
416void FinallyStmt::print( std::ostream & os, Indenter indent ) const {
417        os << "Finally Statement" << endl;
418        os << indent << "... with block:" << endl << indent+1;
419        block->print( os, indent+1 );
420}
421
422SuspendStmt::SuspendStmt( const SuspendStmt & other )
423        : Statement( other )
424        , then( maybeClone(other.then) )
425{}
426
427SuspendStmt::~SuspendStmt() {
428        delete then;
429}
430
431void SuspendStmt::print( std::ostream & os, Indenter indent ) const {
432        os << "Suspend Statement";
433        switch (type) {
434                case None     : os << " with implicit target"; break;
435                case Generator: os << " for generator"       ; break;
436                case Coroutine: os << " for coroutine"       ; break;
437        }
438        os << endl;
439        indent += 1;
440
441        if(then) {
442                os << indent << " with post statement :" << endl;
443                then->print( os, indent + 1);
444        }
445}
446
447WaitForStmt::WaitForStmt() : Statement() {
448        timeout.time      = nullptr;
449        timeout.statement = nullptr;
450        timeout.condition = nullptr;
451        orelse .statement = nullptr;
452        orelse .condition = nullptr;
453}
454
455WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
456        clauses.reserve( other.clauses.size() );
457        for( auto & ocl : other.clauses ) {
458                clauses.emplace_back();
459                clauses.back().target.function = ocl.target.function->clone();
460                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
461                clauses.back().statement = ocl.statement->clone();
462                clauses.back().condition = ocl.condition->clone();
463        }
464
465        timeout.time      = other.timeout.time     ->clone();
466        timeout.statement = other.timeout.statement->clone();
467        timeout.condition = other.timeout.condition->clone();
468        orelse .statement = other.orelse .statement->clone();
469        orelse .condition = other.orelse .condition->clone();
470}
471
472WaitForStmt::~WaitForStmt() {
473        for( auto & clause : clauses ) {
474                delete clause.target.function;
475                deleteAll( clause.target.arguments );
476                delete clause.statement;
477                delete clause.condition;
478        }
479
480        delete timeout.time;
481        delete timeout.statement;
482        delete timeout.condition;
483
484        delete orelse.statement;
485        delete orelse.condition;
486}
487
488void WaitForStmt::print( std::ostream & os, Indenter indent ) const {
489        os << "Waitfor Statement" << endl;
490        indent += 1;
491        for( auto & clause : clauses ) {
492                os << indent << "target function :";
493                if(clause.target.function) { clause.target.function->print(os, indent + 1); }
494                os << endl << indent << "with arguments :" << endl;
495                for( auto & thing : clause.target.arguments) {
496                        if(thing) { thing->print(os, indent + 1); }
497                }
498                os << indent << " with statment :" << endl;
499                if(clause.statement) { clause.statement->print(os, indent + 1); }
500
501                os << indent << " with condition :" << endl;
502                if(clause.condition) { clause.condition->print(os, indent + 1); }
503        }
504
505        os << indent << " timeout of :" << endl;
506        if(timeout.time) { timeout.time->print(os, indent + 1); }
507
508        os << indent << " with statment :" << endl;
509        if(timeout.statement) { timeout.statement->print(os, indent + 1); }
510
511        os << indent << " with condition :" << endl;
512        if(timeout.condition) { timeout.condition->print(os, indent + 1); }
513
514
515        os << indent << " else :" << endl;
516        if(orelse.statement) { orelse.statement->print(os, indent + 1); }
517
518        os << indent << " with condition :" << endl;
519        if(orelse.condition) { orelse.condition->print(os, indent + 1); }
520}
521
522
523WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Declaration("", noStorageClasses, LinkageSpec::Cforall), exprs( exprs ), stmt( stmt ) {}
524WithStmt::WithStmt( const WithStmt & other ) : Declaration( other ), stmt( maybeClone( other.stmt ) ) {
525        cloneAll( other.exprs, exprs );
526}
527WithStmt::~WithStmt() {
528        deleteAll( exprs );
529        delete stmt;
530}
531
532void WithStmt::print( std::ostream & os, Indenter indent ) const {
533        os << "With statement" << endl;
534        os << indent << "... with expressions: " << endl;
535        printAll( exprs, os, indent+1 );
536        os << indent << "... with statement:" << endl << indent+1;
537        stmt->print( os, indent+1 );
538}
539
540
541NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
542}
543
544void NullStmt::print( std::ostream & os, Indenter indent ) const {
545        os << "Null Statement" << endl;
546        Statement::print( os, indent );
547}
548
549ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
550        assert( callStmt );
551}
552
553ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
554}
555
556ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
557        delete callStmt;
558}
559
560void ImplicitCtorDtorStmt::print( std::ostream & os, Indenter indent ) const {
561        os << "Implicit Ctor Dtor Statement" << endl;
562        os << indent << "... with Ctor/Dtor: ";
563        callStmt->print( os, indent+1);
564        os << endl;
565}
566
567// Local Variables: //
568// tab-width: 4 //
569// mode: c++ //
570// compile-command: "make install" //
571// End: //
Note: See TracBrowser for help on using the repository browser.