Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Statement.cc

    r70d826cd r50377a4  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 17 16:17:20 2017
    13 // Update Count     : 67
     12// Last Modified On : Sun Sep  3 20:46:44 2017
     13// Update Count     : 68
    1414//
    1515
     
    3434Statement::Statement( std::list<Label> labels ) : labels( labels ) {}
    3535
    36 void Statement::print( __attribute__((unused)) std::ostream &, __attribute__((unused)) int indent ) const {}
     36void Statement::print( std::ostream & os, Indenter ) const {
     37        if ( ! labels.empty() ) {
     38                os << "Labels: {";
     39                for ( const Label & l : labels ) {
     40                        os << l << ",";
     41                }
     42                os << "}" << endl;
     43        }
     44}
    3745
    3846Statement::~Statement() {}
     
    4654}
    4755
    48 void 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 
    54 AsmStmt::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 ) {}
     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( std::list<Label> labels, bool voltile, Expression *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 ) {}
    5563
    5664AsmStmt::AsmStmt( const AsmStmt & other ) : Statement( other ), voltile( other.voltile ), instruction( maybeClone( other.instruction ) ), gotolabels( other.gotolabels ) {
     
    6775}
    6876
    69 void AsmStmt::print( std::ostream &os, int indent ) const {
     77void AsmStmt::print( std::ostream &os, Indenter indent ) const {
    7078        os << "Assembler Statement:" << endl;
    71         os << std::string( indent, ' ' ) << "instruction: " << endl << std::string( indent, ' ' );
    72         instruction->print( os, indent + 2 );
     79        os << indent+1 << "instruction: " << endl << indent;
     80        instruction->print( os, indent+1 );
    7381        if ( ! output.empty() ) {
    74                 os << endl << std::string( indent, ' ' ) << "output: " << endl;
    75                 printAll( output, os, indent + 2 );
     82                os << endl << indent+1 << "output: " << endl;
     83                printAll( output, os, indent+1 );
    7684        } // if
    7785        if ( ! input.empty() ) {
    78                 os << std::string( indent, ' ' ) << "input: " << endl << std::string( indent, ' ' );
    79                 printAll( input, os, indent + 2 );
     86                os << indent+1 << "input: " << endl;
     87                printAll( input, os, indent+1 );
    8088        } // if
    8189        if ( ! clobber.empty() ) {
    82                 os << std::string( indent, ' ' ) << "clobber: " << endl;
    83                 printAll( clobber, os, indent + 2 );
     90                os << indent+1 << "clobber: " << endl;
     91                printAll( clobber, os, indent+1 );
    8492        } // if
    8593}
     
    103111}
    104112
    105 void BranchStmt::print( std::ostream &os, int indent ) const {
    106         os << string( indent, ' ' ) << "Branch (" << brType[type] << ")" << endl ;
    107         if ( target != "" ) os << string( indent+2, ' ' ) << "with target: " << target << endl;
    108         if ( originalTarget != "" ) os << string( indent+2, ' ' ) << "with original target: " << originalTarget << endl;
    109         if ( computedTarget != nullptr ) os << string( indent+2, ' ' ) << "with computed target: " << computedTarget << endl;
     113void BranchStmt::print( std::ostream &os, Indenter indent ) const {
     114        os << "Branch (" << brType[type] << ")" << endl ;
     115        if ( target != "" ) os << indent+1 << "with target: " << target << endl;
     116        if ( originalTarget != "" ) os << indent+1 << "with original target: " << originalTarget << endl;
     117        if ( computedTarget != nullptr ) os << indent+1 << "with computed target: " << computedTarget << endl;
    110118}
    111119
     
    118126}
    119127
    120 void ReturnStmt::print( std::ostream &os, int indent ) const {
    121         os <<  "Return Statement, returning: ";
    122         if ( expr != 0 ) {
    123                 os << endl << string( indent+2, ' ' );
    124                 expr->print( os, indent + 2 );
     128void ReturnStmt::print( std::ostream &os, Indenter indent ) const {
     129        os << "Return Statement, returning: ";
     130        if ( expr != nullptr ) {
     131                os << endl << indent+1;
     132                expr->print( os, indent+1 );
    125133        }
    126134        os << endl;
     
    142150}
    143151
    144 void IfStmt::print( std::ostream &os, int indent ) const {
    145         os << "If on condition: " << endl ;
    146         os << string( indent+4, ' ' );
    147         condition->print( os, indent + 4 );
     152void IfStmt::print( std::ostream &os, Indenter indent ) const {
     153        os << "If on condition: " << endl;
     154        os << indent+1;
     155        condition->print( os, indent+1 );
    148156
    149157        if ( !initialization.empty() ) {
    150                 os << string( indent + 2, ' ' ) << "initialization: \n";
    151                 for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
    152                         os << string( indent + 4, ' ' );
    153                         (*it)->print( os, indent + 4 );
     158                os << indent << "... with initialization: \n";
     159                for ( const Statement * stmt : initialization ) {
     160                        os << indent+1;
     161                        stmt->print( os, indent+1 );
    154162                }
    155163                os << endl;
    156164        }
    157165
    158         os << string( indent+2, ' ' ) << "... then: " << endl;
    159 
    160         os << string( indent+4, ' ' );
    161         thenPart->print( os, indent + 4 );
     166        os << indent << "... then: " << endl;
     167
     168        os << indent+1;
     169        thenPart->print( os, indent+1 );
    162170
    163171        if ( elsePart != 0 ) {
    164                 os << string( indent+2, ' ' ) << "... else: " << endl;
    165                 os << string( indent+4, ' ' );
    166                 elsePart->print( os, indent + 4 );
    167         } // if
    168 }
    169 
    170 SwitchStmt::SwitchStmt( std::list<Label> labels, Expression * condition, std::list<Statement *> &statements ):
     172                os << indent << "... else: " << endl;
     173                os << indent+1;
     174                elsePart->print( os, indent+1 );
     175        } // if
     176}
     177
     178SwitchStmt::SwitchStmt( std::list<Label> labels, Expression * condition, const std::list<Statement *> &statements ):
    171179        Statement( labels ), condition( condition ), statements( statements ) {
    172180}
     
    183191}
    184192
    185 void SwitchStmt::print( std::ostream &os, int indent ) const {
     193void SwitchStmt::print( std::ostream &os, Indenter indent ) const {
    186194        os << "Switch on condition: ";
    187195        condition->print( os );
    188196        os << endl;
    189197
    190         // statements
    191         std::list<Statement *>::const_iterator i;
    192         for ( i = statements.begin(); i != statements.end(); i++)
    193                 (*i)->print( os, indent + 4 );
    194 
    195         //for_each( statements.begin(), statements.end(), mem_fun( bind1st(&Statement::print ), os ));
    196 }
    197 
    198 CaseStmt::CaseStmt( std::list<Label> labels, Expression *condition, std::list<Statement *> &statements, bool deflt ) throw ( SemanticError ) :
     198        for ( const Statement * stmt : statements ) {
     199                stmt->print( os, indent+1 );
     200        }
     201}
     202
     203CaseStmt::CaseStmt( std::list<Label> labels, Expression *condition, const std::list<Statement *> &statements, bool deflt ) throw ( SemanticError ) :
    199204        Statement( labels ), condition( condition ), stmts( statements ), _isDefault( deflt ) {
    200         if ( isDefault() && condition != 0 )
    201                 throw SemanticError("default with conditions");
     205        if ( isDefault() && condition != 0 ) throw SemanticError("default case with condition: ", condition);
    202206}
    203207
     
    216220}
    217221
    218 void CaseStmt::print( std::ostream &os, int indent ) const {
    219         os << string( indent, ' ' );
    220 
    221         if ( isDefault() )
    222                 os << "Default ";
     222void CaseStmt::print( std::ostream &os, Indenter indent ) const {
     223        if ( isDefault() ) os << "Default ";
    223224        else {
    224225                os << "Case ";
    225                 condition->print( os );
    226         } // if
    227 
    228         os << endl;
    229 
    230         std::list<Statement *>::const_iterator i;
    231         for ( i = stmts.begin(); i != stmts.end(); i++)
    232                 (*i )->print( os, indent + 4 );
     226                condition->print( os, indent );
     227        } // if
     228        os << endl;
     229
     230        for ( Statement * stmt : stmts ) {
     231                stmt->print( os, indent+1 );
     232        }
    233233}
    234234
     
    246246}
    247247
    248 void WhileStmt::print( std::ostream &os, int indent ) const {
     248void WhileStmt::print( std::ostream &os, Indenter indent ) const {
    249249        os << "While on condition: " << endl ;
    250         condition->print( os, indent + 4 );
    251 
    252         os << string( indent, ' ' ) << ".... with body: " << endl;
    253 
    254         if ( body != 0 ) body->print( os, indent + 4 );
     250        condition->print( os, indent+1 );
     251
     252        os << indent << "... with body: " << endl;
     253
     254        if ( body != 0 ) body->print( os, indent+1 );
    255255}
    256256
     
    272272}
    273273
    274 void ForStmt::print( std::ostream &os, int indent ) const {
    275         os << "Labels: {";
    276         for ( std::list<Label>::const_iterator it = get_labels().begin(); it != get_labels().end(); ++it) {
    277                 os << *it << ",";
    278         }
    279         os << "}" << endl;
    280 
    281         os << string( indent, ' ' ) << "For Statement" << endl ;
    282 
    283         os << string( indent + 2, ' ' ) << "initialization: \n";
    284         for ( std::list<Statement *>::const_iterator it = initialization.begin(); it != initialization.end(); ++it ) {
    285                 os << string( indent + 4, ' ' );
    286                 (*it)->print( os, indent + 4 );
    287         }
    288 
    289         os << "\n" << string( indent + 2, ' ' ) << "condition: \n";
    290         if ( condition != 0 ) {
    291                 os << string( indent + 4, ' ' );
    292                 condition->print( os, indent + 4 );
    293         }
    294 
    295         os << "\n" << string( indent + 2, ' ' ) << "increment: \n";
    296         if ( increment != 0 ) {
    297                 os << string( indent + 4, ' ' );
    298                 increment->print( os, indent + 4 );
    299         }
    300 
    301         os << "\n" << string( indent + 2, ' ' ) << "statement block: \n";
     274void ForStmt::print( std::ostream &os, Indenter indent ) const {
     275        Statement::print( os, indent ); // print labels
     276
     277        os << "For Statement" << endl;
     278
     279        if ( ! initialization.empty() ) {
     280                os << indent << "... initialization: \n";
     281                for ( Statement * stmt : initialization ) {
     282                        os << indent+1;
     283                        stmt->print( os, indent+1 );
     284                }
     285        }
     286
     287        if ( condition != nullptr ) {
     288                os << indent << "... condition: \n" << indent+1;
     289                condition->print( os, indent+1 );
     290        }
     291
     292        if ( increment != nullptr ) {
     293                os << "\n" << indent << "... increment: \n" << indent+1;
     294                increment->print( os, indent+1 );
     295        }
     296
    302297        if ( body != 0 ) {
    303                 os << string( indent + 4, ' ' );
    304                 body->print( os, indent + 4 );
    305         }
    306 
     298                os << "\n" << indent << "... with body: \n" << indent+1;
     299                body->print( os, indent+1 );
     300        }
    307301        os << endl;
    308302}
     
    322316}
    323317
    324 void ThrowStmt::print( std::ostream &os, int indent) const {
     318void ThrowStmt::print( std::ostream &os, Indenter indent) const {
     319        if ( target ) os << "Non-Local ";
     320        os << "Throw Statement, raising: ";
     321        expr->print(os, indent+1);
    325322        if ( target ) {
    326                 os << "Non-Local ";
    327         }
    328         os << "Throw Statement, raising: ";
    329         expr->print(os, indent + 4);
    330         if ( target ) {
    331                 os << "At: ";
    332                 target->print(os, indent + 4);
     323                os << "... at: ";
     324                target->print(os, indent+1);
    333325        }
    334326}
     
    348340}
    349341
    350 void TryStmt::print( std::ostream &os, int indent ) const {
     342void TryStmt::print( std::ostream &os, Indenter indent ) const {
    351343        os << "Try Statement" << endl;
    352         os << string( indent + 2, ' ' ) << "with block:" << endl;
    353         os << string( indent + 4, ' ' );
    354         block->print( os, indent + 4 );
     344        os << indent << "... with block:" << endl << indent+1;
     345        block->print( os, indent+1 );
    355346
    356347        // handlers
    357         os << string( indent + 2, ' ' ) << "and handlers:" << endl;
    358         for ( std::list<CatchStmt *>::const_iterator i = handlers.begin(); i != handlers.end(); i++) {
    359                 os << string( indent + 4, ' ' );
    360                 (*i )->print( os, indent + 4 );
     348        os << indent << "... and handlers:" << endl;
     349        for ( const CatchStmt * stmt : handlers ) {
     350                os << indent+1;
     351                stmt->print( os, indent+1 );
    361352        }
    362353
    363354        // finally block
    364355        if ( finallyBlock != 0 ) {
    365                 os << string( indent + 2, ' ' ) << "and finally:" << endl;
    366                 finallyBlock->print( os, indent + 4 );
     356                os << indent << "... and finally:" << endl << indent+1;
     357                finallyBlock->print( os, indent+1 );
    367358        } // if
    368359}
     
    370361CatchStmt::CatchStmt( std::list<Label> labels, Kind kind, Declaration *decl, Expression *cond, Statement *body ) :
    371362        Statement( labels ), kind ( kind ), decl ( decl ), cond ( cond ), body( body ) {
     363                assertf( decl, "Catch clause must have a declaration." );
    372364}
    373365
     
    381373}
    382374
    383 void CatchStmt::print( std::ostream &os, int indent ) const {
     375void CatchStmt::print( std::ostream &os, Indenter indent ) const {
    384376        os << "Catch " << ((Terminate == kind) ? "Terminate" : "Resume") << " Statement" << endl;
    385377
    386         os << string( indent + 2, ' ' ) << "... catching: ";
    387         if ( decl ) {
    388                 decl->printShort( os, indent + 4 );
    389                 os << endl;
    390         }
    391         else
    392                 os << string( indent + 4 , ' ' ) << ">>> Error:  this catch clause must have a declaration <<<" << endl;
     378        os << indent << "... catching: ";
     379        decl->printShort( os, indent+1 );
     380        os << endl;
    393381
    394382        if ( cond ) {
    395                 os << string( indent + 2, ' ' ) << "with conditional:" << endl;
    396                 os << string( indent + 4, ' ' );
    397                 cond->print( os, indent + 4 );
    398         }
    399         else
    400                 os << string( indent + 2, ' ' ) << "with no conditional" << endl;
    401 
    402         os << string( indent + 2, ' ' ) << "with block:" << endl;
    403         os << string( indent + 4, ' ' );
    404         body->print( os, indent + 4 );
     383                os << indent << "... with conditional:" << endl << indent+1;
     384                cond->print( os, indent+1 );
     385        }
     386
     387        os << indent << "... with block:" << endl;
     388        os << indent+1;
     389        body->print( os, indent+1 );
    405390}
    406391
     
    417402}
    418403
    419 void FinallyStmt::print( std::ostream &os, int indent ) const {
     404void FinallyStmt::print( std::ostream &os, Indenter indent ) const {
    420405        os << "Finally Statement" << endl;
    421         os << string( indent + 2, ' ' ) << "with block:" << endl;
    422         os << string( indent + 4, ' ' );
    423         block->print( os, indent + 4 );
     406        os << indent << "... with block:" << endl << indent+1;
     407        block->print( os, indent+1 );
    424408}
    425409
     
    465449}
    466450
    467 void WaitForStmt::print( std::ostream &os, int indent ) const {
     451void WaitForStmt::print( std::ostream &os, Indenter indent ) const {
    468452        os << "Waitfor Statement" << endl;
    469         os << string( indent + 2, ' ' ) << "with block:" << endl;
    470         os << string( indent + 4, ' ' );
     453        os << indent << "... with block:" << endl << indent+1;
    471454        // block->print( os, indent + 4 );
    472455}
     
    475458NullStmt::NullStmt() : Statement( std::list<Label>() ) {}
    476459
    477 void NullStmt::print( std::ostream &os, __attribute__((unused)) int indent ) const {
    478         os << "Null Statement" << endl ;
     460void NullStmt::print( std::ostream &os, Indenter ) const {
     461        os << "Null Statement" << endl;
    479462}
    480463
     
    490473}
    491474
    492 void ImplicitCtorDtorStmt::print( std::ostream &os, int indent ) const {
     475void ImplicitCtorDtorStmt::print( std::ostream &os, Indenter indent ) const {
    493476        os << "Implicit Ctor Dtor Statement" << endl;
    494         os << string( indent + 2, ' ' ) << "with Ctor/Dtor: ";
    495         callStmt->print( os, indent + 2);
    496         os << endl;
    497 }
    498 
    499 std::ostream & operator<<( std::ostream & out, const Statement * statement ) {
    500         if ( statement ) {
    501                 statement->print( out );
    502         } else {
    503                 out << "nullptr";
    504         }
    505         return out;
     477        os << indent << "... with Ctor/Dtor: ";
     478        callStmt->print( os, indent+1);
     479        os << endl;
    506480}
    507481
Note: See TracChangeset for help on using the changeset viewer.