Changes in / [4cc585b:5916272]


Ignore:
Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r4cc585b r5916272  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tus Jul 25 15:29:00 2017
    13 // Update Count     : 486
     12// Last Modified On : Fri Aug 18 15:34:00 2017
     13// Update Count     : 488
    1414//
    1515#include "CodeGenerator.h"
     
    7979        }
    8080
    81         CodeGenerator::LineMarker::LineMarker(
    82                         CodeLocation const & loc, bool toPrint) :
    83                 loc(loc), toPrint(toPrint)
    84         {}
    85 
    86         CodeGenerator::LineMarker CodeGenerator::lineDirective(
    87                         BaseSyntaxNode const * node) {
    88                 return LineMarker(node->location, lineMarks);
    89         }
    90 
    91         std::ostream & operator<<(std::ostream & out,
    92                         CodeGenerator::LineMarker const & marker) {
    93                 if (marker.toPrint && marker.loc.isSet()) {
    94                         return out << "\n# " << marker.loc.linenumber << " \""
    95                                 << marker.loc.filename << "\"\n";
    96                 } else if (marker.toPrint) {
    97                         return out << "\n/* Missing CodeLocation */\n";
    98                 } else {
    99                 return out;
     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;
     99                }
     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;
    100110                }
    101111        }
     
    194204                        ++indent;
    195205                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    196                                 output << lineDirective( *i ) << indent;
     206                                updateLocation( *i );
    197207                                (*i)->accept( *this );
    198208                                output << ";" << endl;
     
    217227        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    218228                extension( enumDecl );
    219                 output << lineDirective ( enumDecl );
     229                updateLocation( enumDecl );
    220230                output << "enum ";
    221231                genAttributes( enumDecl->get_attributes() );
     
    233243                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    234244                                assert( obj );
    235                                 output << lineDirective( obj ) << indent << mangleName( obj );
     245                                updateLocation( obj );
     246                                output << mangleName( obj );
    236247                                if ( obj->get_init() ) {
    237248                                        output << " = ";
     
    251262        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    252263                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
    253                 output << lineDirective( typeDecl );
     264                updateLocation( typeDecl );
    254265                output << "typedef ";
    255266                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
     
    752763        void CodeGenerator::visit( StmtExpr * stmtExpr ) {
    753764                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    754                 output << lineDirective( stmtExpr) << "({" << std::endl;
     765                updateLocation( stmtExpr );
     766                output << "({" << std::endl;
    755767                ++indent;
    756768                unsigned int numStmts = stmts.size();
    757769                unsigned int i = 0;
    758770                for ( Statement * stmt : stmts ) {
    759                         output << lineDirective( stmt ) << indent;
     771                        updateLocation( stmt );
    760772            output << printLabels( stmt->get_labels() );
    761773                        if ( i+1 == numStmts ) {
     
    844856
    845857        void CodeGenerator::visit( IfStmt * ifStmt ) {
    846                 output << lineDirective( ifStmt );
     858                updateLocation( ifStmt );
    847859                output << "if ( ";
    848860                ifStmt->get_condition()->accept( *this );
     
    858870
    859871        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    860                 output << lineDirective( switchStmt );
     872                updateLocation( switchStmt );
    861873                output << "switch ( " ;
    862874                switchStmt->get_condition()->accept( *this );
     
    871883
    872884        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    873                 output << lineDirective( caseStmt );
    874                 output << indent;
     885                updateLocation( caseStmt );
    875886                if ( caseStmt->isDefault()) {
    876887                        output << "default";
  • src/CodeGen/CodeGenerator.h

    r4cc585b r5916272  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tus Jul 25 25:30:00 2017
    13 // Update Count     : 54
     12// Last Modified On : Fri Aug 18 15:40:00 2017
     13// Update Count     : 56
    1414//
    1515
     
    108108                };
    109109
    110                 struct LineMarker {
    111                         CodeLocation const & loc;
    112                         bool toPrint;
    113 
    114                         LineMarker(CodeLocation const & loc, bool toPrint);
    115                 };
    116 
    117                 LineMarker lineDirective(BaseSyntaxNode const * node);
    118 
    119110                void asmName( DeclarationWithType *decl );
    120111
    121112                void extension( Expression *expr );
    122113                void extension( Declaration *decl );
     114
     115                void updateLocation( BaseSyntaxNode const * to );
    123116          private:
    124117                Indenter indent;
     
    129122                bool genC = false;    // true if output has to be C code
    130123                bool lineMarks = false;
     124
     125                CodeLocation currentLocation;
     126                void updateLocation( CodeLocation const & to );
     127                void nextLine();
    131128
    132129                void handleStorageClass( DeclarationWithType *decl );
     
    155152        /// returns C-compatible name of declaration
    156153        std::string genName( DeclarationWithType * decl );
    157 
    158         std::ostream & operator<<(std::ostream &,
    159                 CodeGenerator::LineMarker const &);
    160154} // namespace CodeGen
    161155
  • src/CodeGen/Generate.cc

    r4cc585b r5916272  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed May 19 13:05:00 2017
    13 // Update Count     : 6
     12// Last Modified On : Fri Aug 18 15:39:00 2017
     13// Update Count     : 7
    1414//
    1515#include "Generate.h"
     
    3333                for ( auto & dcl : translationUnit ) {
    3434                        if ( LinkageSpec::isGeneratable( dcl->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( dcl->get_linkage() ) ) ) {
    35                                 os << cgv.lineDirective(dcl);
     35                                cgv.updateLocation( dcl );
    3636                                dcl->accept(cgv);
    3737                                if ( doSemicolon( dcl ) ) {
  • src/ControlStruct/ForExprMutator.cc

    r4cc585b r5916272  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 17 15:32:46 2017
    13 // Update Count     : 11
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri Aug 18 10:22:00 2017
     13// Update Count     : 12
    1414//
    1515
     
    2121
    2222namespace ControlStruct {
    23         Statement *ForExprMutator::postmutate( IfStmt *ifStmt ) {
    24                 std::list<Statement *> &init = ifStmt->get_initialization();
    25                 if ( init.size() == 0 ) {
    26                         return ifStmt;
    27                 } // if
     23        Statement *hoist( Statement *originalStmt, std::list<Statement *> &init ) {
     24                // If no hoisting is needed, skip:
     25                if ( 0 == init.size() ) {
     26                        return originalStmt;
     27                }
    2828
    29                 // create compound statement, move initializers outside, leave _for_ as-is
     29                // Create compound statement, move initializers outside,
     30                // the resut of the original stays as is.
    3031                CompoundStmt *block = new CompoundStmt( std::list< Label >() );
    3132                std::list<Statement *> &stmts = block->get_kids();
    3233                stmts.splice( stmts.end(), init );
    3334
    34                 // add for to the new block
    35                 stmts.push_back( ifStmt );
     35                // Add for to the new block.
     36                stmts.push_back( originalStmt );
    3637                return block;
    3738        }
    3839
     40        Statement *ForExprMutator::postmutate( IfStmt *ifStmt ) {
     41                return hoist( ifStmt, ifStmt->initialization );
     42        }
    3943        Statement *ForExprMutator::postmutate( ForStmt *forStmt ) {
    4044                // hoist any initializer declarations to make them C89 (rather than C99)
    41                 std::list<Statement *> &init = forStmt->get_initialization();
    42                 if ( init.size() == 0 ) {
    43                         return forStmt;
    44                 } // if
    45 
    46                 // create compound statement, move initializers outside, leave _for_ as-is
    47                 CompoundStmt *block = new CompoundStmt( std::list< Label >() );
    48                 std::list<Statement *> &stmts = block->get_kids();
    49                 stmts.splice( stmts.end(), init );
    50 
    51                 // add for to the new block
    52                 stmts.push_back( forStmt );
    53                 return block;
     45                return hoist( forStmt, forStmt->initialization );
    5446        }
    5547} // namespace ControlStruct
Note: See TracChangeset for help on using the changeset viewer.