Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r5f642e38 r29cf9c8  
    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 : Thu Mar 30 16:38:01 2017
    13 // Update Count     : 482
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Tus May  9 16:50:00 2017
     13// Update Count     : 484
    1414//
    1515
     
    4141namespace CodeGen {
    4242        int CodeGenerator::tabsize = 4;
     43
     44        // Pseudo Function: output << lineDirective(currentNode);
     45    struct lineDirective {
     46        CodeLocation const & loc;
     47                lineDirective(CodeLocation const & location) : loc(location) {}
     48                lineDirective(BaseSyntaxNode const * node) : loc(node->location) {}
     49        };
     50        std::ostream & operator<<(std::ostream & out, lineDirective const & ld) {
     51                if (ld.loc.isSet())
     52                        return out << "\n# " << ld.loc.linenumber << " \""
     53                                << ld.loc.filename << "\"\n";
     54                return out << "\n// Unset Location\n";
     55        }
    4356
    4457        // the kinds of statements that would ideally be followed by whitespace
     
    128141
    129142
    130         //*** Declarations
     143        // *** Declarations
    131144        void CodeGenerator::visit( FunctionDecl * functionDecl ) {
     145                output << lineDirective( functionDecl );
     146
    132147                extension( functionDecl );
    133148                genAttributes( functionDecl->get_attributes() );
     
    153168                }
    154169
     170                output << lineDirective( objectDecl );
     171
    155172                extension( objectDecl );
    156173                genAttributes( objectDecl->get_attributes() );
     
    192209                        cur_indent += CodeGenerator::tabsize;
    193210                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    194                                 output << indent;
     211                                output << lineDirective( *i ) << indent;
    195212                                (*i)->accept( *this );
    196213                                output << ";" << endl;
     
    204221
    205222        void CodeGenerator::visit( StructDecl * structDecl ) {
     223                output << lineDirective( structDecl );
     224
    206225                extension( structDecl );
    207226                handleAggregate( structDecl, "struct " );
     
    209228
    210229        void CodeGenerator::visit( UnionDecl * unionDecl ) {
     230                output << lineDirective( unionDecl );
     231
    211232                extension( unionDecl );
    212233                handleAggregate( unionDecl, "union " );
     
    215236        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    216237                extension( enumDecl );
     238                output << lineDirective ( enumDecl );
    217239                output << "enum ";
    218240                genAttributes( enumDecl->get_attributes() );
     
    230252                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    231253                                assert( obj );
    232                                 output << indent << mangleName( obj );
     254                                output << lineDirective( obj ) << indent << mangleName( obj );
    233255                                if ( obj->get_init() ) {
    234256                                        output << " = ";
     
    248270        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    249271                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
     272                output << lineDirective( typeDecl );
    250273                output << "typedef ";
    251274                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
     
    262285                        } // if
    263286                } else {
    264                         output << typeDecl->typeString() << " " << typeDecl->get_name();
     287                        output << typeDecl->genTypeString() << " " << typeDecl->get_name();
     288                        if ( typeDecl->get_kind() != TypeDecl::Any && typeDecl->get_sized() ) {
     289                                output << " | sized(" << typeDecl->get_name() << ")";
     290                        }
    265291                        if ( ! typeDecl->get_assertions().empty() ) {
    266292                                output << " | { ";
     
    316342        }
    317343
    318         //*** Expressions
     344        // *** Expressions
    319345        void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
    320346                extension( applicationExpr );
     
    719745        void CodeGenerator::visit( StmtExpr * stmtExpr ) {
    720746                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    721                 output << "({" << std::endl;
     747                output << lineDirective( stmtExpr) << "({" << std::endl;
    722748                cur_indent += CodeGenerator::tabsize;
    723749                unsigned int numStmts = stmts.size();
    724750                unsigned int i = 0;
    725751                for ( Statement * stmt : stmts ) {
    726                         output << indent << printLabels( stmt->get_labels() );
     752                        output << lineDirective( stmt ) << indent;
     753            output << printLabels( stmt->get_labels() );
    727754                        if ( i+1 == numStmts ) {
    728755                                // last statement in a statement expression needs to be handled specially -
     
    746773        }
    747774
    748         //*** Statements
     775        // *** Statements
    749776        void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
    750777                std::list<Statement*> ks = compoundStmt->get_kids();
     
    769796        void CodeGenerator::visit( ExprStmt * exprStmt ) {
    770797                assert( exprStmt );
    771                 // cast the top-level expression to void to reduce gcc warnings.
    772                 Expression * expr = new CastExpr( exprStmt->get_expr() );
     798                Expression * expr = exprStmt->get_expr();
     799                if ( genC ) {
     800                        // cast the top-level expression to void to reduce gcc warnings.
     801                        expr = new CastExpr( expr );
     802                }
    773803                expr->accept( *this );
    774804                output << ";";
     
    807837
    808838        void CodeGenerator::visit( IfStmt * ifStmt ) {
     839                output << lineDirective( ifStmt );
    809840                output << "if ( ";
    810841                ifStmt->get_condition()->accept( *this );
     
    820851
    821852        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
     853                output << lineDirective( switchStmt );
    822854                output << "switch ( " ;
    823855                switchStmt->get_condition()->accept( *this );
     
    832864
    833865        void CodeGenerator::visit( CaseStmt * caseStmt ) {
     866                output << lineDirective( caseStmt );
    834867                output << indent;
    835868                if ( caseStmt->isDefault()) {
Note: See TracChangeset for help on using the changeset viewer.