Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    ra5f0529 r0dd18fd  
    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"
     
    6464        } // extension
    6565
    66         ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {
    67           return output << string( cg.cur_indent, ' ' );
    68         }
    69 
    70         ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {
    71                 return indent( output );
    72         }
    73 
    7466        CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
    7567                labels = &l;
     
    8779        }
    8880
    89         CodeGenerator::LineMarker::LineMarker(
    90                         CodeLocation const & loc, bool toPrint) :
    91                 loc(loc), toPrint(toPrint)
    92         {}
    93 
    94         CodeGenerator::LineMarker CodeGenerator::lineDirective(
    95                         BaseSyntaxNode const * node) {
    96                 return LineMarker(node->location, lineMarks);
    97         }
    98 
    99         std::ostream & operator<<(std::ostream & out,
    100                         CodeGenerator::LineMarker const & marker) {
    101                 if (marker.toPrint && marker.loc.isSet()) {
    102                         return out << "\n# " << marker.loc.linenumber << " \""
    103                                 << marker.loc.filename << "\"\n";
    104                 } else if (marker.toPrint) {
    105                         return out << "\n/* Missing CodeLocation */\n";
    106                 } else {
    107                 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;
    10899                }
    109         }
    110 
    111         CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
     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;
     110                }
     111        }
     112
     113        CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
    112114
    113115        string CodeGenerator::mangleName( DeclarationWithType * decl ) {
     
    200202                        output << " {" << endl;
    201203
    202                         cur_indent += CodeGenerator::tabsize;
     204                        ++indent;
    203205                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    204                                 output << lineDirective( *i ) << indent;
     206                                updateLocation( *i );
    205207                                (*i)->accept( *this );
    206208                                output << ";" << endl;
    207209                        } // for
    208210
    209                         cur_indent -= CodeGenerator::tabsize;
     211                        --indent;
    210212
    211213                        output << indent << "}";
     
    225227        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    226228                extension( enumDecl );
    227                 output << lineDirective ( enumDecl );
     229                updateLocation( enumDecl );
    228230                output << "enum ";
    229231                genAttributes( enumDecl->get_attributes() );
     
    237239                        output << " {" << endl;
    238240
    239                         cur_indent += CodeGenerator::tabsize;
     241                        ++indent;
    240242                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    241243                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    242244                                assert( obj );
    243                                 output << lineDirective( obj ) << indent << mangleName( obj );
     245                                updateLocation( obj );
     246                                output << mangleName( obj );
    244247                                if ( obj->get_init() ) {
    245248                                        output << " = ";
     
    249252                        } // for
    250253
    251                         cur_indent -= CodeGenerator::tabsize;
     254                        --indent;
    252255
    253256                        output << indent << "}";
     
    259262        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    260263                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
    261                 output << lineDirective( typeDecl );
     264                updateLocation( typeDecl );
    262265                output << "typedef ";
    263266                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
     
    760763        void CodeGenerator::visit( StmtExpr * stmtExpr ) {
    761764                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    762                 output << lineDirective( stmtExpr) << "({" << std::endl;
    763                 cur_indent += CodeGenerator::tabsize;
     765                updateLocation( stmtExpr );
     766                output << "({" << std::endl;
     767                ++indent;
    764768                unsigned int numStmts = stmts.size();
    765769                unsigned int i = 0;
    766770                for ( Statement * stmt : stmts ) {
    767                         output << lineDirective( stmt ) << indent;
     771                        updateLocation( stmt );
    768772            output << printLabels( stmt->get_labels() );
    769773                        if ( i+1 == numStmts ) {
     
    784788                        ++i;
    785789                }
    786                 cur_indent -= CodeGenerator::tabsize;
     790                --indent;
    787791                output << indent << "})";
    788792        }
     
    793797                output << "{" << endl;
    794798
    795                 cur_indent += CodeGenerator::tabsize;
     799                ++indent;
    796800
    797801                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
     
    804808                        } // if
    805809                } // for
    806                 cur_indent -= CodeGenerator::tabsize;
     810                --indent;
    807811
    808812                output << indent << "}";
     
    852856
    853857        void CodeGenerator::visit( IfStmt * ifStmt ) {
    854                 output << lineDirective( ifStmt );
     858                updateLocation( ifStmt );
    855859                output << "if ( ";
    856860                ifStmt->get_condition()->accept( *this );
     
    866870
    867871        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    868                 output << lineDirective( switchStmt );
     872                updateLocation( switchStmt );
    869873                output << "switch ( " ;
    870874                switchStmt->get_condition()->accept( *this );
     
    872876
    873877                output << "{" << std::endl;
    874                 cur_indent += CodeGenerator::tabsize;
     878                ++indent;
    875879                acceptAll( switchStmt->get_statements(), *this );
    876                 cur_indent -= CodeGenerator::tabsize;
     880                --indent;
    877881                output << indent << "}";
    878882        }
    879883
    880884        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    881                 output << lineDirective( caseStmt );
    882                 output << indent;
     885                updateLocation( caseStmt );
    883886                if ( caseStmt->isDefault()) {
    884887                        output << "default";
     
    891894                std::list<Statement *> sts = caseStmt->get_statements();
    892895
    893                 cur_indent += CodeGenerator::tabsize;
     896                ++indent;
    894897                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    895898                        output << indent << printLabels( (*i)->get_labels() )  ;
     
    897900                        output << endl;
    898901                } // for
    899                 cur_indent -= CodeGenerator::tabsize;
     902                --indent;
    900903        }
    901904
Note: See TracChangeset for help on using the changeset viewer.