Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rd104b02 r87e08e24  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tus Jul 25 15:29:00 2017
    13 // Update Count     : 486
     12// Last Modified On : Fri Aug 18 15:34:00 2017
     13// Update Count     : 488
    1414//
    1515#include "CodeGenerator.h"
     
    7979        }
    8080
    81         CodeGenerator::LineMarker::LineMarker(
    82                         CodeLocation const & loc, bool toPrint) :
    83                 loc(loc), toPrint(toPrint)
    84         {}
    85 
    86         CodeGenerator::LineMarker CodeGenerator::lineDirective(
    87                         BaseSyntaxNode const * node) {
    88                 return LineMarker(node->location, lineMarks);
    89         }
    90 
    91         std::ostream & operator<<(std::ostream & out,
    92                         CodeGenerator::LineMarker const & marker) {
    93                 if (marker.toPrint && marker.loc.isSet()) {
    94                         return out << "\n# " << marker.loc.linenumber << " \""
    95                                 << marker.loc.filename << "\"\n";
    96                 } else if (marker.toPrint) {
    97                         return out << "\n/* Missing CodeLocation */\n";
    98                 } else {
    99                 return out;
     81        /* Using updateLocation at the beginning of a node and nextLine
     82         * within a node should become the method of formating.
     83         */
     84        void CodeGenerator::updateLocation( CodeLocation const & to ) {
     85                if ( !lineMarks ) {
     86                        return;
     87                } else if ( currentLocation.followedBy( to, 0 ) ) {
     88                        return;
     89                } else if ( currentLocation.followedBy( to, 1 ) ) {
     90                        output << "\n" << indent;
     91                        currentLocation.linenumber += 1;
     92                } else if ( currentLocation.followedBy( to, 2 ) ) {
     93                        output << "\n\n" << indent;
     94                        currentLocation.linenumber += 2;
     95                } else {
     96                        output << "\n# " << to.linenumber << " \"" << to.filename
     97                               << "\"\n" << indent;
     98                        currentLocation = to;
     99                }
     100                output << std::flush;
     101        }
     102
     103        void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) {
     104                updateLocation( to->location );
     105        }
     106
     107        void CodeGenerator::nextLine() {
     108                if ( !lineMarks ) {
     109                        output << "\n" << indent << std::flush;
    100110                }
    101111        }
     
    195205                        ++indent;
    196206                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    197                                 output << lineDirective( *i ) << indent;
     207                                updateLocation( *i );
     208                                output << indent;
    198209                                (*i)->accept( *this );
    199210                                output << ";" << endl;
     
    218229        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    219230                extension( enumDecl );
    220                 output << lineDirective ( enumDecl );
     231                updateLocation( enumDecl );
    221232                output << "enum ";
    222233                genAttributes( enumDecl->get_attributes() );
     
    234245                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    235246                                assert( obj );
    236                                 output << lineDirective( obj ) << indent << mangleName( obj );
     247                                updateLocation( obj );
     248                                output << indent << mangleName( obj );
    237249                                if ( obj->get_init() ) {
    238250                                        output << " = ";
     
    252264        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    253265                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
    254                 output << lineDirective( typeDecl );
     266                updateLocation( typeDecl );
    255267                output << "typedef ";
    256268                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
     
    741753        void CodeGenerator::visit( StmtExpr * stmtExpr ) {
    742754                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    743                 output << lineDirective( stmtExpr) << "({" << std::endl;
     755                updateLocation( stmtExpr );
     756                output << "({" << std::endl;
    744757                ++indent;
    745758                unsigned int numStmts = stmts.size();
    746759                unsigned int i = 0;
    747760                for ( Statement * stmt : stmts ) {
    748                         output << lineDirective( stmt ) << indent;
     761                        updateLocation( stmt );
    749762                        output << printLabels( stmt->get_labels() );
    750763                        if ( i+1 == numStmts ) {
     
    832845
    833846        void CodeGenerator::visit( IfStmt * ifStmt ) {
    834                 output << lineDirective( ifStmt );
     847                updateLocation( ifStmt );
    835848                output << "if ( ";
    836849                ifStmt->get_condition()->accept( *this );
     
    846859
    847860        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    848                 output << lineDirective( switchStmt );
     861                updateLocation( switchStmt );
    849862                output << "switch ( " ;
    850863                switchStmt->get_condition()->accept( *this );
     
    859872
    860873        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    861                 output << lineDirective( caseStmt );
    862                 output << indent;
     874                updateLocation( caseStmt );
    863875                if ( caseStmt->isDefault()) {
    864876                        output << "default";
Note: See TracChangeset for help on using the changeset viewer.