Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r68cd1ce rcda48b6  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun 13 07:12:27 2015
    13 // Update Count     : 129
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Thu Jun 11 13:22:39 2015
     13// Update Count     : 137
    1414//
    1515
     
    1919#include <list>
    2020
    21 #include "Parser/ParseNode.h"
    22 
    2321#include "SynTree/Type.h"
     22#include "SynTree/Declaration.h"
     23#include "SynTree/Statement.h"
    2424#include "SynTree/Expression.h"
    2525#include "SynTree/Initializer.h"
    26 #include "SynTree/Statement.h"
    2726
    2827#include "utility.h"
     
    4443        }
    4544
    46         CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
    47 
    48         CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
    49                         : cur_indent( indent ), insideFunction( infunp ), output( os ) {
     45        ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
     46          return output << string( cg.cur_indent, ' ' );
     47        }
     48
     49        ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
     50                return indent( output );
     51        }
     52
     53        CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
     54
     55        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
     56                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5057                //output << std::string( init );
    5158        }
    5259
    53         CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
    54                         : cur_indent( indent ), insideFunction( infunp ), output( os ) {
     60        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
     61                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5562                //output << std::string( init );
    5663        }
     
    6370                } // if
    6471        }
    65   
     72 
    6673        //*** Declarations
    6774        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
     
    105112
    106113                if ( ! memb.empty() ) {
    107                         output << endl << string( cur_indent, ' ' ) << "{" << endl;
     114                        output << endl << indent << "{" << endl;
    108115
    109116                        cur_indent += CodeGenerator::tabsize;
    110117                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    111                                 output << string( cur_indent, ' ' );
     118                                output << indent;
    112119                                (*i)->accept(*this );
    113120                                output << ";" << endl;
     
    116123                        cur_indent -= CodeGenerator::tabsize;
    117124
    118                         output << string( cur_indent, ' ' ) << "}";
     125                        output << indent << "}";
    119126                } // if
    120127        }
     
    139146
    140147                if ( ! memb.empty() ) {
    141                         output << endl << "{" << endl;
     148                        output << " {" << endl;
    142149
    143150                        cur_indent += CodeGenerator::tabsize;
     
    145152                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    146153                                assert( obj );
    147                                 output << string( cur_indent, ' ' ) << mangleName( obj );
     154                                output << indent << mangleName( obj );
    148155                                if ( obj->get_init() ) {
    149156                                        output << " = ";
     
    155162                        cur_indent -= CodeGenerator::tabsize;
    156163
    157                         output << "}" << endl;
     164                        output << indent << "}";
    158165                } // if
    159166        }
     
    445452
    446453                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
    447                         output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
     454                        output << indent << printLabels( (*i)->get_labels() );
    448455                        (*i)->accept(*this );
    449456
     
    455462                cur_indent -= CodeGenerator::tabsize;
    456463
    457                 output << string( cur_indent, ' ' ) << "}";
     464                output << indent << "}";
    458465        }
    459466
     
    495502                cur_indent -= CodeGenerator::tabsize;
    496503
    497                 output << string( cur_indent, ' ' ) << "}";
     504                output << indent << "}";
    498505        }
    499506
    500507        void CodeGenerator::visit( CaseStmt *caseStmt ) {
    501                 output << string( cur_indent, ' ' );
     508                output << indent;
    502509                if ( caseStmt->isDefault()) {
    503510                        output << "default";
     
    512519                cur_indent += CodeGenerator::tabsize;
    513520                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    514                         output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
     521                        output << indent << printLabels( (*i)->get_labels() )  ;
    515522                        (*i)->accept(*this );
    516523                        output << endl;
     
    565572                whileStmt->get_body()->accept( *this );
    566573
    567                 output << string( cur_indent, ' ' );
     574                output << indent;
    568575
    569576                if ( whileStmt->get_isDoWhile() ) {
     
    597604
    598605        void CodeGenerator::visit( NullStmt *nullStmt ) {
    599                 //output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
     606                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    600607                output << "/* null statement */ ;";
    601608        }
     
    621628        void CodeGenerator::handleStorageClass( Declaration *decl ) {
    622629                switch ( decl->get_storageClass() ) {
    623                   case DeclarationNode::Extern:
     630                  case Declaration::NoStorageClass:
     631                        break;
     632                  case Declaration::Extern:
    624633                        output << "extern ";
    625634                        break;
    626                   case DeclarationNode::Static:
     635                  case Declaration::Static:
    627636                        output << "static ";
    628637                        break;
    629                   case DeclarationNode::Auto:
     638                  case Declaration::Auto:
    630639                        // silently drop storage class
    631640                        break;
    632                   case DeclarationNode::Register:
     641                  case Declaration::Register:
    633642                        output << "register ";
    634643                        break;
    635                   case DeclarationNode::Inline:
     644                  case Declaration::Inline:
    636645                        // handled as special via isInline flag (FIX)
    637646                        break;
    638                   case DeclarationNode::Fortran:
     647                  case Declaration::Fortran:
    639648                        // not handled
    640                         output << "fortran ";
    641                         break;
    642                   case DeclarationNode::Noreturn:
    643                         // not handled
    644                         output << "_Noreturn ";
    645                         break;
    646                   case DeclarationNode::Threadlocal:
    647                         // not handled
    648                         output << "_Thread_local ";
    649                         break;
    650                   case DeclarationNode::NoStorageClass:
    651649                        break;
    652650                } // switch
Note: See TracChangeset for help on using the changeset viewer.