Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r0dd18fd ra5f0529  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Aug 18 15:34:00 2017
    13 // Update Count     : 488
     12// Last Modified On : Tus Jul 25 15:29:00 2017
     13// Update Count     : 486
    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
    6674        CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
    6775                labels = &l;
     
    7987        }
    8088
    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;
     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;
    99108                }
    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 ) {}
     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 ) {}
    114112
    115113        string CodeGenerator::mangleName( DeclarationWithType * decl ) {
     
    202200                        output << " {" << endl;
    203201
    204                         ++indent;
     202                        cur_indent += CodeGenerator::tabsize;
    205203                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    206                                 updateLocation( *i );
     204                                output << lineDirective( *i ) << indent;
    207205                                (*i)->accept( *this );
    208206                                output << ";" << endl;
    209207                        } // for
    210208
    211                         --indent;
     209                        cur_indent -= CodeGenerator::tabsize;
    212210
    213211                        output << indent << "}";
     
    227225        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    228226                extension( enumDecl );
    229                 updateLocation( enumDecl );
     227                output << lineDirective ( enumDecl );
    230228                output << "enum ";
    231229                genAttributes( enumDecl->get_attributes() );
     
    239237                        output << " {" << endl;
    240238
    241                         ++indent;
     239                        cur_indent += CodeGenerator::tabsize;
    242240                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    243241                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    244242                                assert( obj );
    245                                 updateLocation( obj );
    246                                 output << mangleName( obj );
     243                                output << lineDirective( obj ) << indent << mangleName( obj );
    247244                                if ( obj->get_init() ) {
    248245                                        output << " = ";
     
    252249                        } // for
    253250
    254                         --indent;
     251                        cur_indent -= CodeGenerator::tabsize;
    255252
    256253                        output << indent << "}";
     
    262259        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    263260                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
    264                 updateLocation( typeDecl );
     261                output << lineDirective( typeDecl );
    265262                output << "typedef ";
    266263                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
     
    763760        void CodeGenerator::visit( StmtExpr * stmtExpr ) {
    764761                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    765                 updateLocation( stmtExpr );
    766                 output << "({" << std::endl;
    767                 ++indent;
     762                output << lineDirective( stmtExpr) << "({" << std::endl;
     763                cur_indent += CodeGenerator::tabsize;
    768764                unsigned int numStmts = stmts.size();
    769765                unsigned int i = 0;
    770766                for ( Statement * stmt : stmts ) {
    771                         updateLocation( stmt );
     767                        output << lineDirective( stmt ) << indent;
    772768            output << printLabels( stmt->get_labels() );
    773769                        if ( i+1 == numStmts ) {
     
    788784                        ++i;
    789785                }
    790                 --indent;
     786                cur_indent -= CodeGenerator::tabsize;
    791787                output << indent << "})";
    792788        }
     
    797793                output << "{" << endl;
    798794
    799                 ++indent;
     795                cur_indent += CodeGenerator::tabsize;
    800796
    801797                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
     
    808804                        } // if
    809805                } // for
    810                 --indent;
     806                cur_indent -= CodeGenerator::tabsize;
    811807
    812808                output << indent << "}";
     
    856852
    857853        void CodeGenerator::visit( IfStmt * ifStmt ) {
    858                 updateLocation( ifStmt );
     854                output << lineDirective( ifStmt );
    859855                output << "if ( ";
    860856                ifStmt->get_condition()->accept( *this );
     
    870866
    871867        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    872                 updateLocation( switchStmt );
     868                output << lineDirective( switchStmt );
    873869                output << "switch ( " ;
    874870                switchStmt->get_condition()->accept( *this );
     
    876872
    877873                output << "{" << std::endl;
    878                 ++indent;
     874                cur_indent += CodeGenerator::tabsize;
    879875                acceptAll( switchStmt->get_statements(), *this );
    880                 --indent;
     876                cur_indent -= CodeGenerator::tabsize;
    881877                output << indent << "}";
    882878        }
    883879
    884880        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    885                 updateLocation( caseStmt );
     881                output << lineDirective( caseStmt );
     882                output << indent;
    886883                if ( caseStmt->isDefault()) {
    887884                        output << "default";
     
    894891                std::list<Statement *> sts = caseStmt->get_statements();
    895892
    896                 ++indent;
     893                cur_indent += CodeGenerator::tabsize;
    897894                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    898895                        output << indent << printLabels( (*i)->get_labels() )  ;
     
    900897                        output << endl;
    901898                } // for
    902                 --indent;
     899                cur_indent -= CodeGenerator::tabsize;
    903900        }
    904901
Note: See TracChangeset for help on using the changeset viewer.