source: src/SynTree/Statement.cc @ 592b9fa

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 592b9fa was 8bf784a, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

name mangling for ttype, fix SynTree? operator<< to work with nullptr, add isTtype check, ttype variables are automatically "sized"

  • Property mode set to 100644
File size: 12.6 KB
RevLine 
[0dd3a2f]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//
[3be261a]7// Statement.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[4e06c1e]11// Last Modified By : Peter A. Buhr
[1d4580a]12// Last Modified On : Fri Aug 12 13:58:48 2016
13// Update Count     : 62
[0dd3a2f]14//
15
[51b7345]16#include <functional>
17#include <algorithm>
18#include <iostream>
19#include <list>
20#include <cassert>
21
22#include "Statement.h"
23#include "Expression.h"
24#include "Declaration.h"
25#include "Common/SemanticError.h"
26
27using std::string;
28using std::endl;
29
[2871210]30Statement::Statement( std::list<Label> _labels ) : labels( _labels ) {}
[51b7345]31
[de62360d]32void Statement::print( std::ostream &, int indent ) const {}
[51b7345]33
34Statement::~Statement() {}
35
[2871210]36ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ) : Statement( _labels ), expr( _expr ) {}
[51b7345]37
[3be261a]38ExprStmt::ExprStmt( const ExprStmt &other ) : Statement( other ), expr( maybeClone( other.expr ) ) {}
39
40ExprStmt::~ExprStmt() {
41        delete expr;
42}
[51b7345]43
[de62360d]44void ExprStmt::print( std::ostream &os, int indent ) const {
[bb8ea30]45        os << "Expression Statement:" << endl << std::string( indent + 2, ' ' );
[0dd3a2f]46        expr->print( os, indent + 2 );
[3be261a]47}
[51b7345]48
[7f5566b]49
50AsmStmt::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 ) {}
51
[3be261a]52AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
53  cloneAll( other.output, output );
54  cloneAll( other.input, input );
55  cloneAll( other.clobber, clobber );
56}
57
[7f5566b]58AsmStmt::~AsmStmt() {
59        delete instruction;
60        deleteAll( output );
61        deleteAll( input );
62        deleteAll( clobber );
63}
64
65void AsmStmt::print( std::ostream &os, int indent ) const {
66        os << "Assembler Statement:" << endl;
67        os << std::string( indent, ' ' ) << "instruction: " << endl << std::string( indent, ' ' );
68        instruction->print( os, indent + 2 );
69        if ( ! output.empty() ) {
70                os << endl << std::string( indent, ' ' ) << "output: " << endl;
71                printAll( output, os, indent + 2 );
[3be261a]72        } // if
[7f5566b]73        if ( ! input.empty() ) {
74                os << std::string( indent, ' ' ) << "input: " << endl << std::string( indent, ' ' );
75                printAll( input, os, indent + 2 );
76        } // if
77        if ( ! clobber.empty() ) {
78                os << std::string( indent, ' ' ) << "clobber: " << endl;
79                printAll( clobber, os, indent + 2 );
80        } // if
[3be261a]81}
[7f5566b]82
83
[51b7345]84const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
85
[0dd3a2f]86BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type ) throw ( SemanticError ) :
[2871210]87        Statement( labels ), originalTarget( _target ), target( _target ), computedTarget( NULL ), type( _type ) {
[0dd3a2f]88        //actually this is a syntactic error signaled by the parser
[0f8e4ac]89        if ( type == BranchStmt::Goto && target.empty() )
[0dd3a2f]90                throw SemanticError("goto without target");
[51b7345]91}
92
[0dd3a2f]93BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type ) throw ( SemanticError ) :
[2871210]94        Statement( labels ), computedTarget( _computedTarget ), type( _type ) {
[0dd3a2f]95        if ( type != BranchStmt::Goto || computedTarget == 0 )
96                throw SemanticError("Computed target not valid in branch statement");
[51b7345]97}
98
[de62360d]99void BranchStmt::print( std::ostream &os, int indent ) const {
[44b5ca0]100        os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ;
[51b7345]101}
102
[0dd3a2f]103ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) : Statement( labels ), expr( _expr ), isThrow( throwP ) {}
[51b7345]104
[3be261a]105ReturnStmt::ReturnStmt( const ReturnStmt & other ) : Statement( other ), expr( maybeClone( other.expr ) ), isThrow( other.isThrow ) {}
106
[51b7345]107ReturnStmt::~ReturnStmt() {
[0dd3a2f]108        delete expr;
[51b7345]109}
110
[de62360d]111void ReturnStmt::print( std::ostream &os, int indent ) const {
[89231bc]112        os << string ( isThrow? "Throw":"Return" ) << " Statement, returning: ";
113        if ( expr != 0 ) {
114                os << endl << string( indent+2, ' ' );
115                expr->print( os, indent + 2 );
116        }
[0dd3a2f]117        os << endl;
[51b7345]118}
119
120IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
[2871210]121        Statement( _labels ), condition( _condition ), thenPart( _thenPart ), elsePart( _elsePart ) {}
[51b7345]122
[3be261a]123IfStmt::IfStmt( const IfStmt & other ) :
124        Statement( other ), condition( maybeClone( other.condition ) ), thenPart( maybeClone( other.thenPart ) ), elsePart( maybeClone( other.elsePart ) ) {}
125
[ac71a86]126IfStmt::~IfStmt() {
127        delete condition;
128        delete thenPart;
129        delete elsePart;
130}
[51b7345]131
[de62360d]132void IfStmt::print( std::ostream &os, int indent ) const {
[89231bc]133        os << "If on condition: " << endl ;
[60089f4]134        os << string( indent+4, ' ' );
[0dd3a2f]135        condition->print( os, indent + 4 );
[51b7345]136
[60089f4]137        os << string( indent+2, ' ' ) << "... then: " << endl;
[51b7345]138
[60089f4]139        os << string( indent+4, ' ' );
[0dd3a2f]140        thenPart->print( os, indent + 4 );
[51b7345]141
[0dd3a2f]142        if ( elsePart != 0 ) {
[60089f4]143                os << string( indent+2, ' ' ) << "... else: " << endl;
144                os << string( indent+4, ' ' );
[0dd3a2f]145                elsePart->print( os, indent + 4 );
146        } // if
[51b7345]147}
148
[8688ce1]149SwitchStmt::SwitchStmt( std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_statements ):
150        Statement( _labels ), condition( _condition ), statements( _statements ) {
[0dd3a2f]151}
[51b7345]152
[3be261a]153SwitchStmt::SwitchStmt( const SwitchStmt & other ):
154        Statement( other ), condition( maybeClone( other.condition ) ) {
[8688ce1]155        cloneAll( other.statements, statements );
[3be261a]156}
157
[51b7345]158SwitchStmt::~SwitchStmt() {
[0dd3a2f]159        delete condition;
[8688ce1]160        // destroy statements
[2037f82]161        deleteAll( statements );
[51b7345]162}
163
[de62360d]164void SwitchStmt::print( std::ostream &os, int indent ) const {
[89231bc]165        os << "Switch on condition: ";
[0dd3a2f]166        condition->print( os );
167        os << endl;
[51b7345]168
[8688ce1]169        // statements
[de62360d]170        std::list<Statement *>::const_iterator i;
[8688ce1]171        for ( i = statements.begin(); i != statements.end(); i++)
[de62360d]172                (*i)->print( os, indent + 4 );
[51b7345]173
[8688ce1]174        //for_each( statements.begin(), statements.end(), mem_fun( bind1st(&Statement::print ), os ));
[51b7345]175}
176
[3be261a]177CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition, std::list<Statement *> &_statements, bool deflt ) throw ( SemanticError ) :
[2871210]178        Statement( _labels ), condition( _condition ), stmts( _statements ), _isDefault( deflt ) {
[0dd3a2f]179        if ( isDefault() && condition != 0 )
180                throw SemanticError("default with conditions");
[51b7345]181}
182
[3be261a]183CaseStmt::CaseStmt( const CaseStmt & other ) :
184        Statement( other ), condition( maybeClone(other.condition ) ), _isDefault( other._isDefault ) {
185        cloneAll( other.stmts, stmts );
186}
187
[51b7345]188CaseStmt::~CaseStmt() {
[0dd3a2f]189        delete condition;
[2037f82]190        deleteAll( stmts );
[51b7345]191}
192
[8688ce1]193CaseStmt * CaseStmt::makeDefault( std::list<Label> labels, std::list<Statement *> stmts ) {
194        return new CaseStmt( labels, 0, stmts, true );
[b2152e7a]195}
196
[de62360d]197void CaseStmt::print( std::ostream &os, int indent ) const {
[44b5ca0]198        os << string( indent, ' ' );
[51b7345]199
[de62360d]200        if ( isDefault() )
[0dd3a2f]201                os << "Default ";
202        else {
203                os << "Case ";
204                condition->print( os );
205        } // if
[51b7345]206
[0dd3a2f]207        os << endl;
[51b7345]208
[de62360d]209        std::list<Statement *>::const_iterator i;
[0dd3a2f]210        for ( i = stmts.begin(); i != stmts.end(); i++)
211                (*i )->print( os, indent + 4 );
[51b7345]212}
213
[0dd3a2f]214WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_, Statement *body_, bool isDoWhile_ ):
215        Statement( labels ), condition( condition_), body( body_), isDoWhile( isDoWhile_) {
216}
[51b7345]217
[3be261a]218WhileStmt::WhileStmt( const WhileStmt & other ):
219        Statement( other ), condition( maybeClone( other.condition ) ), body( maybeClone( other.body ) ), isDoWhile( other.isDoWhile ) {
220}
221
[a08ba92]222WhileStmt::~WhileStmt() {
[0dd3a2f]223        delete body;
[2037f82]224        delete condition;
[51b7345]225}
226
[de62360d]227void WhileStmt::print( std::ostream &os, int indent ) const {
[89231bc]228        os << "While on condition: " << endl ;
[0dd3a2f]229        condition->print( os, indent + 4 );
[51b7345]230
[44b5ca0]231        os << string( indent, ' ' ) << ".... with body: " << endl;
[51b7345]232
[0dd3a2f]233        if ( body != 0 ) body->print( os, indent + 4 );
[51b7345]234}
235
[145f1fc]236ForStmt::ForStmt( std::list<Label> labels, std::list<Statement *> initialization_, Expression *condition_, Expression *increment_, Statement *body_ ):
[0dd3a2f]237        Statement( labels ), initialization( initialization_ ), condition( condition_ ), increment( increment_ ), body( body_ ) {
238}
[51b7345]239
[3be261a]240ForStmt::ForStmt( const ForStmt & other ):
241        Statement( other ), condition( maybeClone( other.condition ) ), increment( maybeClone( other.increment ) ), body( maybeClone( other.body ) ) {
242                cloneAll( other.initialization, initialization );
243
244}
245
[51b7345]246ForStmt::~ForStmt() {
[145f1fc]247        deleteAll( initialization );
[0dd3a2f]248        delete condition;
249        delete increment;
250        delete body;
[51b7345]251}
252
[de62360d]253void ForStmt::print( std::ostream &os, int indent ) const {
[89231bc]254        os << "Labels: {";
[de62360d]255        for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
[be5aa1b]256                os << *it << ",";
257        }
258        os << "}" << endl;
259
[44b5ca0]260        os << string( indent, ' ' ) << "For Statement" << endl ;
[51b7345]261
[3be261a]262        os << string( indent + 2, ' ' ) << "initialization: \n";
[145f1fc]263        for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
[bb8ea30]264                os << string( indent + 4, ' ' );
[145f1fc]265                (*it)->print( os, indent + 4 );
266        }
[51b7345]267
[3be261a]268        os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
[bb8ea30]269        if ( condition != 0 ) {
270                os << string( indent + 4, ' ' );
[0dd3a2f]271                condition->print( os, indent + 4 );
[bb8ea30]272        }
[51b7345]273
[3be261a]274        os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
[bb8ea30]275        if ( increment != 0 ) {
276                os << string( indent + 4, ' ' );
[0dd3a2f]277                increment->print( os, indent + 4 );
[bb8ea30]278        }
[51b7345]279
[3be261a]280        os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
[bb8ea30]281        if ( body != 0 ) {
282                os << string( indent + 4, ' ' );
[0dd3a2f]283                body->print( os, indent + 4 );
[bb8ea30]284        }
[51b7345]285
[0dd3a2f]286        os << endl;
[51b7345]287}
288
289TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) :
[0dd3a2f]290        Statement( labels ), block( tryBlock ),  handlers( _handlers ), finallyBlock( _finallyBlock ) {
291}
[51b7345]292
[3be261a]293TryStmt::TryStmt( const TryStmt &other ) : Statement( other ), block( maybeClone( other.block ) ), finallyBlock( maybeClone( other.finallyBlock ) ) {
294        cloneAll( other.handlers, handlers );
[51b7345]295}
296
[a08ba92]297TryStmt::~TryStmt() {
[0dd3a2f]298        delete block;
[2037f82]299        deleteAll( handlers );
300        delete finallyBlock;
[51b7345]301}
302
[de62360d]303void TryStmt::print( std::ostream &os, int indent ) const {
[89231bc]304        os << "Try Statement" << endl;
[44b5ca0]305        os << string( indent + 2, ' ' ) << "with block: " << endl;
[0dd3a2f]306        block->print( os, indent + 4 );
307
308        // handlers
[44b5ca0]309        os << string( indent + 2, ' ' ) << "and handlers: " << endl;
[de62360d]310        for ( std::list<Statement *>::const_iterator i = handlers.begin(); i != handlers.end(); i++)
[0dd3a2f]311                (*i )->print( os, indent + 4 );
312
313        // finally block
314        if ( finallyBlock != 0 ) {
[44b5ca0]315                os << string( indent + 2, ' ' ) << "Finally block: " << endl;
[0dd3a2f]316                finallyBlock->print( os, indent + 4 );
317        } // if
[51b7345]318}
319
[1d4580a]320CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool catchAny ) :
321        Statement( labels ), decl ( _decl ), body( _body ), catchRest ( catchAny ) {
[0dd3a2f]322}
[51b7345]323
[3be261a]324CatchStmt::CatchStmt( const CatchStmt & other ) :
325        Statement( other ), decl ( maybeClone( other.decl ) ), body( maybeClone( other.body ) ), catchRest ( other.catchRest ) {
326}
327
[a08ba92]328CatchStmt::~CatchStmt() {
[0dd3a2f]329        delete decl;
330        delete body;
[51b7345]331}
332
[de62360d]333void CatchStmt::print( std::ostream &os, int indent ) const {
[f1b1e4c]334        os << "Catch Statement" << endl;
[0dd3a2f]335
[44b5ca0]336        os << string( indent, ' ' ) << "... catching" << endl;
[0dd3a2f]337        if ( decl ) {
338                decl->printShort( os, indent + 4 );
339                os << endl;
340        } else if ( catchRest )
[44b5ca0]341                os << string( indent + 4 , ' ' ) << "the rest" << endl;
[0dd3a2f]342        else
[44b5ca0]343                os << string( indent + 4 , ' ' ) << ">>> Error:  this catch clause must have a declaration <<<" << endl;
[51b7345]344}
345
346
[0dd3a2f]347FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) : Statement( labels ), block( _block ) {
348        assert( labels.empty() ); // finally statement cannot be labeled
[51b7345]349}
350
[3be261a]351FinallyStmt::FinallyStmt( const FinallyStmt & other ) : Statement( other ), block( maybeClone( other.block ) ) {
352}
353
[0dd3a2f]354FinallyStmt::~FinallyStmt() {
355        delete block;
[51b7345]356}
357
[de62360d]358void FinallyStmt::print( std::ostream &os, int indent ) const {
[f1b1e4c]359        os << "Finally Statement" << endl;
[44b5ca0]360        os << string( indent + 2, ' ' ) << "with block: " << endl;
[0dd3a2f]361        block->print( os, indent + 4 );
[51b7345]362}
363
364NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
365NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
366
[de62360d]367void NullStmt::print( std::ostream &os, int indent ) const {
[89231bc]368        os << "Null Statement" << endl ;
[51b7345]369}
370
[f1b1e4c]371ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( Statement * callStmt ) : Statement( std::list<Label>() ), callStmt( callStmt ) {
372        assert( callStmt );
373}
374
[74d1804]375ImplicitCtorDtorStmt::ImplicitCtorDtorStmt( const ImplicitCtorDtorStmt & other ) : Statement( other ), callStmt( maybeClone( other.callStmt ) ) {
[f1b1e4c]376}
377
378ImplicitCtorDtorStmt::~ImplicitCtorDtorStmt() {
[74d1804]379        delete callStmt;
[f1b1e4c]380}
381
382void ImplicitCtorDtorStmt::print( std::ostream &os, int indent ) const {
383        os << "Implicit Ctor Dtor Statement" << endl;
384        os << string( indent + 2, ' ' ) << "with Ctor/Dtor: ";
385        callStmt->print( os, indent + 2);
386        os << endl;
387}
388
[3906301]389std::ostream & operator<<( std::ostream & out, const Statement * statement ) {
[8bf784a]390        if ( statement ) {
391                statement->print( out );
392        } else {
393                out << "nullptr";
394        }
[baf7fee]395        return out;
396}
397
[0dd3a2f]398// Local Variables: //
399// tab-width: 4 //
400// mode: c++ //
401// compile-command: "make install" //
402// End: //
Note: See TracBrowser for help on using the repository browser.