source: src/SynTree/Statement.cc @ 5ee7d36

arm-ehjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 5ee7d36 was 5ee7d36, checked in by Andrew Beach <ajbeach@…>, 21 months ago

BranchStmt::brType did not have enough names to print all kinds of branches.

  • Property mode set to 100644
File size: 16.7 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
422WaitForStmt::WaitForStmt() : Statement() {
423        timeout.time      = nullptr;
424        timeout.statement = nullptr;
425        timeout.condition = nullptr;
426        orelse .statement = nullptr;
427        orelse .condition = nullptr;
428}
429
430WaitForStmt::WaitForStmt( const WaitForStmt & other ) : Statement( other ) {
431        clauses.reserve( other.clauses.size() );
432        for( auto & ocl : other.clauses ) {
433                clauses.emplace_back();
434                clauses.back().target.function = ocl.target.function->clone();
435                cloneAll( ocl.target.arguments, clauses.back().target.arguments );
436                clauses.back().statement = ocl.statement->clone();
437                clauses.back().condition = ocl.condition->clone();
438        }
439
440        timeout.time      = other.timeout.time     ->clone();
441        timeout.statement = other.timeout.statement->clone();
442        timeout.condition = other.timeout.condition->clone();
443        orelse .statement = other.orelse .statement->clone();
444        orelse .condition = other.orelse .condition->clone();
445}
446
447WaitForStmt::~WaitForStmt() {
448        for( auto & clause : clauses ) {
449                delete clause.target.function;
450                deleteAll( clause.target.arguments );
451                delete clause.statement;
452                delete clause.condition;
453        }
454
455        delete timeout.time;
456        delete timeout.statement;
457        delete timeout.condition;
458
459        delete orelse.statement;
460        delete orelse.condition;
461}
462
463void WaitForStmt::print( std::ostream & os, Indenter indent ) const {
464        os << "Waitfor Statement" << endl;
465        indent += 1;
466        for( auto & clause : clauses ) {
467                os << indent << "target function :";
468                if(clause.target.function) { clause.target.function->print(os, indent + 1); }
469                os << endl << indent << "with arguments :" << endl;
470                for( auto & thing : clause.target.arguments) {
471                        if(thing) { thing->print(os, indent + 1); }
472                }
473                os << indent << " with statment :" << endl;
474                if(clause.statement) { clause.statement->print(os, indent + 1); }
475
476                os << indent << " with condition :" << endl;
477                if(clause.condition) { clause.condition->print(os, indent + 1); }
478        }
479
480        os << indent << " timeout of :" << endl;
481        if(timeout.time) { timeout.time->print(os, indent + 1); }
482
483        os << indent << " with statment :" << endl;
484        if(timeout.statement) { timeout.statement->print(os, indent + 1); }
485
486        os << indent << " with condition :" << endl;
487        if(timeout.condition) { timeout.condition->print(os, indent + 1); }
488
489
490        os << indent << " else :" << endl;
491        if(orelse.statement) { orelse.statement->print(os, indent + 1); }
492
493        os << indent << " with condition :" << endl;
494        if(orelse.condition) { orelse.condition->print(os, indent + 1); }
495}
496
497
498WithStmt::WithStmt( const std::list< Expression * > & exprs, Statement * stmt ) : Declaration("", noStorageClasses, LinkageSpec::Cforall), exprs( exprs ), stmt( stmt ) {}
499WithStmt::WithStmt( const WithStmt & other ) : Declaration( other ), stmt( maybeClone( other.stmt ) ) {
500        cloneAll( other.exprs, exprs );
501}
502WithStmt::~WithStmt() {
503        deleteAll( exprs );
504        delete stmt;
505}
506
507void WithStmt::print( std::ostream & os, Indenter indent ) const {
508        os << "With statement" << endl;
509        os << indent << "... with expressions: " << endl;
510        printAll( exprs, os, indent+1 );
511        os << indent << "... with statement:" << endl << indent+1;
512        stmt->print( os, indent+1 );
513}
514
515
516NullStmt::NullStmt( const std::list<Label> & labels ) : Statement( labels ) {
517}
518
519void NullStmt::print( std::ostream & os, Indenter indent ) const {
520        os << "Null Statement" << endl;
521        Statement::print( os, indent );
522}
523
524ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement(), callStmt( callStmt ) {
525        assert( callStmt );
526}
527
528ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
529}
530
531ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
532        delete callStmt;
533}
534
535void ImplicitCtorDtorStmt::print( std::ostream & os, Indenter indent ) const {
536        os << "Implicit Ctor Dtor Statement" << endl;
537        os << indent << "... with Ctor/Dtor: ";
538        callStmt->print( os, indent+1);
539        os << endl;
540}
541
542// Local Variables: //
543// tab-width: 4 //
544// mode: c++ //
545// compile-command: "make install" //
546// End: //
Note: See TracBrowser for help on using the repository browser.