Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rcda48b6 r68cd1ce  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Thu Jun 11 13:22:39 2015
    13 // Update Count     : 137
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Sat Jun 13 07:12:27 2015
     13// Update Count     : 129
    1414//
    1515
     
    1919#include <list>
    2020
     21#include "Parser/ParseNode.h"
     22
    2123#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"
    2627
    2728#include "utility.h"
     
    4344        }
    4445
    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 ) {
     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 ) {
    5750                //output << std::string( init );
    5851        }
    5952
    60         CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
    61                         : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
     53        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
     54                        : cur_indent( indent ), insideFunction( infunp ), output( os ) {
    6255                //output << std::string( init );
    6356        }
     
    7063                } // if
    7164        }
    72  
     65  
    7366        //*** Declarations
    7467        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
     
    112105
    113106                if ( ! memb.empty() ) {
    114                         output << endl << indent << "{" << endl;
     107                        output << endl << string( cur_indent, ' ' ) << "{" << endl;
    115108
    116109                        cur_indent += CodeGenerator::tabsize;
    117110                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    118                                 output << indent;
     111                                output << string( cur_indent, ' ' );
    119112                                (*i)->accept(*this );
    120113                                output << ";" << endl;
     
    123116                        cur_indent -= CodeGenerator::tabsize;
    124117
    125                         output << indent << "}";
     118                        output << string( cur_indent, ' ' ) << "}";
    126119                } // if
    127120        }
     
    146139
    147140                if ( ! memb.empty() ) {
    148                         output << " {" << endl;
     141                        output << endl << "{" << endl;
    149142
    150143                        cur_indent += CodeGenerator::tabsize;
     
    152145                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    153146                                assert( obj );
    154                                 output << indent << mangleName( obj );
     147                                output << string( cur_indent, ' ' ) << mangleName( obj );
    155148                                if ( obj->get_init() ) {
    156149                                        output << " = ";
     
    162155                        cur_indent -= CodeGenerator::tabsize;
    163156
    164                         output << indent << "}";
     157                        output << "}" << endl;
    165158                } // if
    166159        }
     
    452445
    453446                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
    454                         output << indent << printLabels( (*i)->get_labels() );
     447                        output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
    455448                        (*i)->accept(*this );
    456449
     
    462455                cur_indent -= CodeGenerator::tabsize;
    463456
    464                 output << indent << "}";
     457                output << string( cur_indent, ' ' ) << "}";
    465458        }
    466459
     
    502495                cur_indent -= CodeGenerator::tabsize;
    503496
    504                 output << indent << "}";
     497                output << string( cur_indent, ' ' ) << "}";
    505498        }
    506499
    507500        void CodeGenerator::visit( CaseStmt *caseStmt ) {
    508                 output << indent;
     501                output << string( cur_indent, ' ' );
    509502                if ( caseStmt->isDefault()) {
    510503                        output << "default";
     
    519512                cur_indent += CodeGenerator::tabsize;
    520513                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    521                         output << indent << printLabels( (*i)->get_labels() )  ;
     514                        output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
    522515                        (*i)->accept(*this );
    523516                        output << endl;
     
    572565                whileStmt->get_body()->accept( *this );
    573566
    574                 output << indent;
     567                output << string( cur_indent, ' ' );
    575568
    576569                if ( whileStmt->get_isDoWhile() ) {
     
    604597
    605598        void CodeGenerator::visit( NullStmt *nullStmt ) {
    606                 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
     599                //output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
    607600                output << "/* null statement */ ;";
    608601        }
     
    628621        void CodeGenerator::handleStorageClass( Declaration *decl ) {
    629622                switch ( decl->get_storageClass() ) {
    630                   case Declaration::NoStorageClass:
    631                         break;
    632                   case Declaration::Extern:
     623                  case DeclarationNode::Extern:
    633624                        output << "extern ";
    634625                        break;
    635                   case Declaration::Static:
     626                  case DeclarationNode::Static:
    636627                        output << "static ";
    637628                        break;
    638                   case Declaration::Auto:
     629                  case DeclarationNode::Auto:
    639630                        // silently drop storage class
    640631                        break;
    641                   case Declaration::Register:
     632                  case DeclarationNode::Register:
    642633                        output << "register ";
    643634                        break;
    644                   case Declaration::Inline:
     635                  case DeclarationNode::Inline:
    645636                        // handled as special via isInline flag (FIX)
    646637                        break;
    647                   case Declaration::Fortran:
     638                  case DeclarationNode::Fortran:
    648639                        // 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:
    649651                        break;
    650652                } // switch
Note: See TracChangeset for help on using the changeset viewer.