source: src/SynTree/Statement.cc @ 43aec9e

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 43aec9e was 6180274, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

more cleanup, make more function parameters const, remove more std::

  • Property mode set to 100644
File size: 18.4 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 : Wed Feb  2 20:19:33 2022
13// Update Count     : 90
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 namespace std;
32
33
34Statement::Statement( const list<Label> & labels ) : labels( labels ) {}
35
36void Statement::print( 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( 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, const list<Expression *> output, const list<Expression *> input, const list<ConstantExpr *> clobber, const 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( 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 string & directive ) : Statement(), directive( directive ) {}
97
98void DirectiveStmt::print( 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( ostream & os, Indenter indent ) const {
123        assertf(type < BranchStmts, "CFA internal error: invalid branch statement" );
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( 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 * then, Statement * else_, const list<Statement *> initialization ):
148        Statement(), condition( condition ), then( then ), else_( else_ ), initialization( initialization ) {}
149
150IfStmt::IfStmt( const IfStmt & other ) :
151        Statement( other ), condition( maybeClone( other.condition ) ), then( maybeClone( other.then ) ), else_( maybeClone( other.else_ ) ) {
152        cloneAll( other.initialization, initialization );
153}
154
155IfStmt::~IfStmt() {
156        deleteAll( initialization );
157        delete condition;
158        delete then;
159        delete else_;
160}
161
162void IfStmt::print( 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        then->print( os, indent + 1 );
180
181        if ( else_ != nullptr ) {
182                os << indent << "... else: " << endl;
183                os << indent + 1;
184                else_->print( os, indent + 1 );
185        } // if
186}
187
188SwitchStmt::SwitchStmt( Expression * condition, const 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( 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 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 list<Label> & labels, list<Statement *> stmts ) {
229        CaseStmt * stmt = new CaseStmt( nullptr, stmts, true );
230        stmt->labels = labels;
231        return stmt;
232}
233
234void CaseStmt::print( 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
248WhileDoStmt::WhileDoStmt( Expression * condition, Statement * body, const list< Statement * > & initialization, bool isDoWhile ):
249        Statement(), condition( condition ), body( body ), else_( nullptr ), initialization( initialization ), isDoWhile( isDoWhile) {
250}
251
252WhileDoStmt::WhileDoStmt( Expression * condition, Statement * body, Statement * else_, const list< Statement * > & initialization, bool isDoWhile ):
253        Statement(), condition( condition), body( body ), else_( else_ ), initialization( initialization ), isDoWhile( isDoWhile) {
254}
255
256WhileDoStmt::WhileDoStmt( const WhileDoStmt & other ):
257        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
258}
259
260WhileDoStmt::~WhileDoStmt() {
261        delete body;
262        delete condition;
263}
264
265void WhileDoStmt::print( ostream & os, Indenter indent ) const {
266        os << "While on condition: " << endl ;
267        condition->print( os, indent + 1 );
268
269        os << indent << "... with body: " << endl;
270
271        if ( body != nullptr ) body->print( os, indent + 1 );
272}
273
274ForStmt::ForStmt( const list<Statement *> initialization, Expression * condition, Expression * increment, Statement * body, Statement * else_ ):
275        Statement(), initialization( initialization ), condition( condition ), increment( increment ), body( body ), else_( else_ ) {
276}
277
278ForStmt::ForStmt( const ForStmt & other ):
279        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ), else_( maybeClone( other.else_ ) ) {
280                cloneAll( other.initialization, initialization );
281
282}
283
284ForStmt::~ForStmt() {
285        deleteAll( initialization );
286        delete condition;
287        delete increment;
288        delete body;
289        delete else_;
290}
291
292void ForStmt::print( ostream & os, Indenter indent ) const {
293        Statement::print( os, indent ); // print labels
294
295        os << "For Statement" << endl;
296
297        if ( ! initialization.empty() ) {
298                os << indent << "... initialization: \n";
299                for ( Statement * stmt : initialization ) {
300                        os << indent + 1;
301                        stmt->print( os, indent + 1 );
302                }
303        }
304
305        if ( condition != nullptr ) {
306                os << indent << "... condition: \n" << indent + 1;
307                condition->print( os, indent + 1 );
308        }
309
310        if ( increment != nullptr ) {
311                os << "\n" << indent << "... increment: \n" << indent + 1;
312                increment->print( os, indent + 1 );
313        }
314
315        if ( body != nullptr ) {
316                os << "\n" << indent << "... with body: \n" << indent + 1;
317                body->print( os, indent + 1 );
318        }
319
320        if ( else_ != nullptr ) {
321                os << "\n" << indent << "... with body: \n" << indent + 1;
322                else_->print( os, indent + 1 );
323        }
324        os << endl;
325}
326
327ThrowStmt::ThrowStmt( Kind kind, Expression * expr, Expression * target ) :
328                Statement(), kind(kind), expr(expr), target(target)     {
329        assertf(Resume == kind || nullptr == target, "Non-local termination throw is not accepted." );
330}
331
332ThrowStmt::ThrowStmt( const ThrowStmt & other ) :
333        Statement ( other ), kind( other.kind ), expr( maybeClone( other.expr ) ), target( maybeClone( other.target ) ) {
334}
335
336ThrowStmt::~ThrowStmt() {
337        delete expr;
338        delete target;
339}
340
341void ThrowStmt::print( ostream & os, Indenter indent) const {
342        if ( target ) os << "Non-Local ";
343        os << "Throw Statement, raising: ";
344        expr->print(os, indent + 1);
345        if ( target ) {
346                os << "... at: ";
347                target->print(os, indent + 1);
348        }
349}
350
351TryStmt::TryStmt( CompoundStmt * tryBlock, const list<CatchStmt *> & handlers, FinallyStmt * finallyBlock ) :
352        Statement(), block( tryBlock ),  handlers( handlers ), finallyBlock( finallyBlock ) {
353}
354
355TryStmt::TryStmt( const TryStmt & other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
356        cloneAll( other.handlers, handlers );
357}
358
359TryStmt::~TryStmt() {
360        delete block;
361        deleteAll( handlers );
362        delete finallyBlock;
363}
364
365void TryStmt::print( ostream & os, Indenter indent ) const {
366        os << "Try Statement" << endl;
367        os << indent << "... with block:" << endl << indent + 1;
368        block->print( os, indent + 1 );
369
370        // handlers
371        os << indent << "... and handlers:" << endl;
372        for ( const CatchStmt * stmt : handlers ) {
373                os << indent + 1;
374                stmt->print( os, indent + 1 );
375        }
376
377        // finally block
378        if ( finallyBlock != nullptr ) {
379                os << indent << "... and finally:" << endl << indent + 1;
380                finallyBlock->print( os, indent + 1 );
381        } // if
382}
383
384CatchStmt::CatchStmt( Kind kind, Declaration * decl, Expression * cond, Statement * body ) :
385        Statement(), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
386                assertf( decl, "Catch clause must have a declaration." );
387}
388
389CatchStmt::CatchStmt( const CatchStmt & other ) :
390        Statement( other ), kind ( other.kind ), decl ( maybeClone( other.decl ) ), cond ( maybeClone( other.cond ) ), body( maybeClone( other.body ) ) {
391}
392
393CatchStmt::~CatchStmt() {
394        delete decl;
395        delete body;
396}
397
398void CatchStmt::print( ostream & os, Indenter indent ) const {
399        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
400
401        os << indent << "... catching: ";
402        decl->printShort( os, indent + 1 );
403        os << endl;
404
405        if ( cond ) {
406                os << indent << "... with conditional:" << endl << indent + 1;
407                cond->print( os, indent + 1 );
408        }
409
410        os << indent << "... with block:" << endl;
411        os << indent + 1;
412        body->print( os, indent + 1 );
413}
414
415
416FinallyStmt::FinallyStmt( CompoundStmt * block ) : Statement(), block( block ) {
417}
418
419FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
420}
421
422FinallyStmt::~FinallyStmt() {
423        delete block;
424}
425
426void FinallyStmt::print( ostream & os, Indenter indent ) const {
427        os << "Finally Statement" << endl;
428        os << indent << "... with block:" << endl << indent + 1;
429        block->print( os, indent + 1 );
430}
431
432SuspendStmt::SuspendStmt( const SuspendStmt & other )
433        : Statement( other )
434        , then( maybeClone(other.then) )
435{}
436
437SuspendStmt::~SuspendStmt() {
438        delete then;
439}
440
441void SuspendStmt::print( ostream & os, Indenter indent ) const {
442        os << "Suspend Statement";
443        switch (type) {
444                case None     : os << " with implicit target"; break;
445                case Generator: os << " for generator"       ; break;
446                case Coroutine: os << " for coroutine"       ; break;
447        }
448        os << endl;
449        indent += 1;
450
451        if(then) {
452                os << indent << " with post statement :" << endl;
453                then->print( os, indent + 1);
454        }
455}
456
457WaitForStmt::WaitForStmt() : Statement() {
458        timeout.time      = nullptr;
459        timeout.statement = nullptr;
460        timeout.condition = nullptr;
461        orelse .statement = nullptr;
462        orelse .condition = nullptr;
463}
464
465WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
466        clauses.reserve( other.clauses.size() );
467        for( auto & ocl : other.clauses ) {
468                clauses.emplace_back();
469                clauses.back().target.function = ocl.target.function->clone();
470                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
471                clauses.back().statement = ocl.statement->clone();
472                clauses.back().condition = ocl.condition->clone();
473        }
474
475        timeout.time      = other.timeout.time     ->clone();
476        timeout.statement = other.timeout.statement->clone();
477        timeout.condition = other.timeout.condition->clone();
478        orelse .statement = other.orelse .statement->clone();
479        orelse .condition = other.orelse .condition->clone();
480}
481
482WaitForStmt::~WaitForStmt() {
483        for( auto & clause : clauses ) {
484                delete clause.target.function;
485                deleteAll( clause.target.arguments );
486                delete clause.statement;
487                delete clause.condition;
488        }
489
490        delete timeout.time;
491        delete timeout.statement;
492        delete timeout.condition;
493
494        delete orelse.statement;
495        delete orelse.condition;
496}
497
498void WaitForStmt::print( ostream & os, Indenter indent ) const {
499        os << "Waitfor Statement" << endl;
500        indent += 1;
501        for( auto & clause : clauses ) {
502                os << indent << "target function :";
503                if(clause.target.function) { clause.target.function->print(os, indent + 1); }
504                os << endl << indent << "with arguments :" << endl;
505                for( auto & thing : clause.target.arguments) {
506                        if(thing) { thing->print(os, indent + 1); }
507                }
508                os << indent << " with statment :" << endl;
509                if(clause.statement) { clause.statement->print(os, indent + 1); }
510
511                os << indent << " with condition :" << endl;
512                if(clause.condition) { clause.condition->print(os, indent + 1); }
513        }
514
515        os << indent << " timeout of :" << endl;
516        if(timeout.time) { timeout.time->print(os, indent + 1); }
517
518        os << indent << " with statment :" << endl;
519        if(timeout.statement) { timeout.statement->print(os, indent + 1); }
520
521        os << indent << " with condition :" << endl;
522        if(timeout.condition) { timeout.condition->print(os, indent + 1); }
523
524
525        os << indent << " else :" << endl;
526        if(orelse.statement) { orelse.statement->print(os, indent + 1); }
527
528        os << indent << " with condition :" << endl;
529        if(orelse.condition) { orelse.condition->print(os, indent + 1); }
530}
531
532
533WithStmt::WithStmt( const list< Expression * > & exprs, Statement * stmt ) : Declaration("", noStorageClasses, LinkageSpec::Cforall), exprs( exprs ), stmt( stmt ) {}
534WithStmt::WithStmt( const WithStmt & other ) : Declaration( other ), stmt( maybeClone( other.stmt ) ) {
535        cloneAll( other.exprs, exprs );
536}
537WithStmt::~WithStmt() {
538        deleteAll( exprs );
539        delete stmt;
540}
541
542void WithStmt::print( ostream & os, Indenter indent ) const {
543        os << "With statement" << endl;
544        os << indent << "... with expressions: " << endl;
545        printAll( exprs, os, indent + 1 );
546        os << indent << "... with statement:" << endl << indent + 1;
547        stmt->print( os, indent + 1 );
548}
549
550
551NullStmt::NullStmt( const list<Label> & labels ) : Statement( labels ) {
552}
553
554void NullStmt::print( ostream & os, Indenter indent ) const {
555        os << "Null Statement" << endl;
556        Statement::print( os, indent );
557}
558
559ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
560        assert( callStmt );
561}
562
563ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
564}
565
566ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
567        delete callStmt;
568}
569
570void ImplicitCtorDtorStmt::print( ostream & os, Indenter indent ) const {
571        os << "Implicit Ctor Dtor Statement" << endl;
572        os << indent << "... with Ctor/Dtor: ";
573        callStmt->print( os, indent + 1);
574        os << endl;
575}
576
577MutexStmt::MutexStmt( Statement * stmt, const list<Expression *> mutexObjs ) 
578        : Statement(), stmt( stmt ), mutexObjs( mutexObjs ) { }
579
580MutexStmt::MutexStmt( const MutexStmt & other ) : Statement( other ), stmt( maybeClone( other.stmt ) ) {
581        cloneAll( other.mutexObjs, mutexObjs );
582}
583
584MutexStmt::~MutexStmt() {
585        deleteAll( mutexObjs );
586        delete stmt;
587}
588
589void MutexStmt::print( ostream & os, Indenter indent ) const {
590        os << "Mutex Statement" << endl;
591        os << indent << "... with Expressions: " << endl;
592        for (auto * obj : mutexObjs) {
593                os << indent + 1;
594                obj->print( os, indent + 1);
595                os << endl;
596        }
597        os << indent << "... with Statement: " << endl << indent + 1;
598        stmt->print( os, indent + 1 );
599}
600
601// Local Variables: //
602// tab-width: 4 //
603// mode: c++ //
604// compile-command: "make install" //
605// End: //
Note: See TracBrowser for help on using the repository browser.