Changeset 8bafacc for src/CodeGen


Ignore:
Timestamp:
Aug 18, 2017, 2:23:02 PM (7 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
0dd18fd
Parents:
274ce8c
Message:

New system for formating generated code in CodeGenerator?.

Location:
src/CodeGen
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r274ce8c r8bafacc  
    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 14:20:00 2017
     13// Update Count     : 487
    1414//
    1515#include "CodeGenerator.h"
     
    101101        }
    102102
     103        /* Using updateLocation at the beginning of a node and nextLine
     104         * within a node should become the method of formating.
     105         */
     106        void CodeGenerator::updateLocation( CodeLocation const & to ) {
     107                if ( !lineMarks ) {
     108                        return;
     109                } else if ( currentLocation.followedBy( to, 0 ) ) {
     110                        return;
     111                } else if ( currentLocation.followedBy( to, 1 ) ) {
     112                        output << "\n" << indent;
     113                        currentLocation.linenumber += 1;
     114                } else if ( currentLocation.followedBy( to, 2 ) ) {
     115                        output << "\n\n" << indent;
     116                        currentLocation.linenumber += 2;
     117                } else {
     118                        output << "\n# " << to.linenumber << " \"" << to.filename
     119                               << "\"\n" << indent;
     120                        currentLocation = to;
     121                }
     122                output << std::flush;
     123        }
     124
     125        void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) {
     126                updateLocation( to->location );
     127        }
     128
     129        void CodeGenerator::nextLine() {
     130                if ( !lineMarks ) {
     131                        output << "\n" << indent << std::flush;
     132                }
     133        }
     134
    103135        CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
    104136
     
    194226                        ++indent;
    195227                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    196                                 output << lineDirective( *i ) << indent;
     228                                updateLocation( *i );
    197229                                (*i)->accept( *this );
    198230                                output << ";" << endl;
     
    217249        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    218250                extension( enumDecl );
    219                 output << lineDirective ( enumDecl );
     251                updateLocation( enumDecl );
    220252                output << "enum ";
    221253                genAttributes( enumDecl->get_attributes() );
     
    233265                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    234266                                assert( obj );
    235                                 output << lineDirective( obj ) << indent << mangleName( obj );
     267                                updateLocation( obj );
     268                                output << mangleName( obj );
    236269                                if ( obj->get_init() ) {
    237270                                        output << " = ";
     
    251284        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    252285                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
    253                 output << lineDirective( typeDecl );
     286                updateLocation( typeDecl );
    254287                output << "typedef ";
    255288                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
     
    752785        void CodeGenerator::visit( StmtExpr * stmtExpr ) {
    753786                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    754                 output << lineDirective( stmtExpr) << "({" << std::endl;
     787                updateLocation( stmtExpr );
     788                output << "({" << std::endl;
    755789                ++indent;
    756790                unsigned int numStmts = stmts.size();
    757791                unsigned int i = 0;
    758792                for ( Statement * stmt : stmts ) {
    759                         output << lineDirective( stmt ) << indent;
     793                        updateLocation( stmt );
    760794            output << printLabels( stmt->get_labels() );
    761795                        if ( i+1 == numStmts ) {
     
    844878
    845879        void CodeGenerator::visit( IfStmt * ifStmt ) {
    846                 output << lineDirective( ifStmt );
     880                updateLocation( ifStmt );
    847881                output << "if ( ";
    848882                ifStmt->get_condition()->accept( *this );
     
    858892
    859893        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    860                 output << lineDirective( switchStmt );
     894                updateLocation( switchStmt );
    861895                output << "switch ( " ;
    862896                switchStmt->get_condition()->accept( *this );
     
    871905
    872906        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    873                 output << lineDirective( caseStmt );
    874                 output << indent;
     907                updateLocation( caseStmt );
    875908                if ( caseStmt->isDefault()) {
    876909                        output << "default";
  • src/CodeGen/CodeGenerator.h

    r274ce8c r8bafacc  
    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 13:52:00 2017
     13// Update Count     : 55
    1414//
    1515
     
    130130                bool lineMarks = false;
    131131
     132                CodeLocation currentLocation;
     133                void updateLocation( CodeLocation const & to );
     134                void updateLocation( BaseSyntaxNode const * to );
     135                void nextLine();
     136
    132137                void handleStorageClass( DeclarationWithType *decl );
    133138                void handleAggregate( AggregateDecl *aggDecl, const std::string & kind );
Note: See TracChangeset for help on using the changeset viewer.