Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rde62360d r94b4364  
    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 : Tue Jun 23 16:16:57 2015
    13 // Update Count     : 133
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Mon Jun 15 12:47:16 2015
     13// Update Count     : 142
    1414//
    1515
     
    4444        }
    4545
    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 ) {
     46        ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
     47          return output << string( cg.cur_indent, ' ' );
     48        }
     49
     50        ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
     51                return indent( output );
     52        }
     53
     54        CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
     55
     56        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
     57                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5058                //output << std::string( init );
    5159        }
    5260
    53         CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
    54                         : cur_indent( indent ), insideFunction( infunp ), output( os ) {
     61        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
     62                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5563                //output << std::string( init );
    5664        }
     
    6371                } // if
    6472        }
    65  
     73
    6674        //*** Declarations
    6775        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
     
    6977                if ( functionDecl->get_isInline() ) {
    7078                        output << "inline ";
    71                 } // if
    72                 if ( functionDecl->get_isNoreturn() ) {
    73                         output << "_Noreturn ";
    7479                } // if
    7580                output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
     
    108113
    109114                if ( ! memb.empty() ) {
    110                         output << endl << string( cur_indent, ' ' ) << "{" << endl;
     115                        output << " {" << endl;
    111116
    112117                        cur_indent += CodeGenerator::tabsize;
    113118                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    114                                 output << string( cur_indent, ' ' );
     119                                output << indent;
    115120                                (*i)->accept(*this );
    116121                                output << ";" << endl;
     
    119124                        cur_indent -= CodeGenerator::tabsize;
    120125
    121                         output << string( cur_indent, ' ' ) << "}";
     126                        output << indent << "}";
    122127                } // if
    123128        }
     
    142147
    143148                if ( ! memb.empty() ) {
    144                         output << endl << "{" << endl;
     149                        output << " {" << endl;
    145150
    146151                        cur_indent += CodeGenerator::tabsize;
     
    148153                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    149154                                assert( obj );
    150                                 output << string( cur_indent, ' ' ) << mangleName( obj );
     155                                output << indent << mangleName( obj );
    151156                                if ( obj->get_init() ) {
    152157                                        output << " = ";
     
    158163                        cur_indent -= CodeGenerator::tabsize;
    159164
    160                         output << "}" << endl;
     165                        output << indent << "}";
    161166                } // if
    162167        }
     
    298303                                  case OT_PREFIX:
    299304                                  case OT_PREFIXASSIGN:
    300                                   case OT_LABELADDRESS:
    301305                                        assert( untypedExpr->get_args().size() == 1 );
    302306                                        output << "(";
     
    322326                                        output << ")";
    323327                                        break;
    324                                        
     328             
    325329                                  case OT_CONSTANT:
    326330                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    449453
    450454                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
    451                         output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
     455                        output << indent << printLabels( (*i)->get_labels() );
    452456                        (*i)->accept(*this );
    453457
     
    459463                cur_indent -= CodeGenerator::tabsize;
    460464
    461                 output << string( cur_indent, ' ' ) << "}";
     465                output << indent << "}";
    462466        }
    463467
     
    499503                cur_indent -= CodeGenerator::tabsize;
    500504
    501                 output << string( cur_indent, ' ' ) << "}";
     505                output << indent << "}";
    502506        }
    503507
    504508        void CodeGenerator::visit( CaseStmt *caseStmt ) {
    505                 output << string( cur_indent, ' ' );
     509                output << indent;
    506510                if ( caseStmt->isDefault()) {
    507511                        output << "default";
     
    516520                cur_indent += CodeGenerator::tabsize;
    517521                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    518                         output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
     522                        output << indent << printLabels( (*i)->get_labels() )  ;
    519523                        (*i)->accept(*this );
    520524                        output << endl;
     
    569573                whileStmt->get_body()->accept( *this );
    570574
    571                 output << string( cur_indent, ' ' );
     575                output << indent;
    572576
    573577                if ( whileStmt->get_isDoWhile() ) {
     
    601605
    602606        void CodeGenerator::visit( NullStmt *nullStmt ) {
    603                 //output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
     607                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    604608                output << "/* null statement */ ;";
    605609        }
     
    638642                        break;
    639643                  case DeclarationNode::Inline:
    640                         output << "inline ";
     644                        // handled as special via isInline flag (FIX)
    641645                        break;
    642646                  case DeclarationNode::Fortran:
     647                        // not handled
    643648                        output << "fortran ";
    644649                        break;
    645650                  case DeclarationNode::Noreturn:
     651                        // not handled
    646652                        output << "_Noreturn ";
    647653                        break;
    648654                  case DeclarationNode::Threadlocal:
     655                        // not handled
    649656                        output << "_Thread_local ";
    650657                        break;
Note: See TracChangeset for help on using the changeset viewer.