Ignore:
Timestamp:
Sep 20, 2017, 11:47:26 AM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
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:
47b5b63
Parents:
a9a4771
git-author:
Rob Schluntz <rschlunt@…> (09/20/17 11:46:26)
git-committer:
Rob Schluntz <rschlunt@…> (09/20/17 11:47:26)
Message:

Cleanup CodeGen? and make linemarkers a bit more accurate

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    ra9a4771 rd22e90f  
    7979        }
    8080
    81         /* Using updateLocation at the beginning of a node and nextLine
     81        /* Using updateLocation at the beginning of a node and endl
    8282         * within a node should become the method of formating.
    8383         */
    8484        void CodeGenerator::updateLocation( CodeLocation const & to ) {
    85                 if ( !lineMarks ) {
    86                         return;
    87                 } else if ( currentLocation.followedBy( to, 0 ) ) {
     85                // skip if linemarks shouldn't appear or if codelocation is unset
     86                if ( !lineMarks || to.isUnset() ) return;
     87
     88                if ( currentLocation.followedBy( to, 0 ) ) {
    8889                        return;
    8990                } else if ( currentLocation.followedBy( to, 1 ) ) {
     
    105106        }
    106107
    107         void CodeGenerator::nextLine() {
    108                 if ( !lineMarks ) {
    109                         output << "\n" << indent << std::flush;
    110                 }
    111         }
    112 
    113         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 ) {}
     108        // replace endl
     109        ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const {
     110                // if ( !cg.lineMarks ) {
     111                //      os << "\n" << cg.indent << std::flush;
     112                // }
     113                os << "\n" << std::flush;
     114                cg.currentLocation.first_line++;
     115                // os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/";
     116                return os;
     117        }
     118
     119        CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ), endl( *this ) {}
    114120
    115121        string CodeGenerator::mangleName( DeclarationWithType * decl ) {
     
    140146
    141147        // *** BaseSyntaxNode
    142         void CodeGenerator::previsit( BaseSyntaxNode * ) {
     148        void CodeGenerator::previsit( BaseSyntaxNode * node ) {
    143149                // turn off automatic recursion for all nodes, to allow each visitor to
    144150                // precisely control the order in which its children are visited.
    145151                visit_children = false;
     152                updateLocation( node );
    146153        }
    147154
     
    165172                asmName( functionDecl );
    166173
    167                 // acceptAll( functionDecl->get_oldDecls(), *visitor );
    168174                if ( functionDecl->get_statements() ) {
    169175                        functionDecl->get_statements()->accept( *visitor );
     
    216222                        ++indent;
    217223                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    218                                 updateLocation( *i );
    219224                                output << indent;
    220225                                (*i)->accept( *visitor );
     
    240245        void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
    241246                extension( enumDecl );
    242                 updateLocation( enumDecl );
    243247                output << "enum ";
    244248                genAttributes( enumDecl->get_attributes() );
     
    255259                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    256260                                assert( obj );
    257                                 updateLocation( obj );
    258261                                output << indent << mangleName( obj );
    259262                                if ( obj->get_init() ) {
     
    278281        void CodeGenerator::postvisit( TypedefDecl * typeDecl ) {
    279282                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
    280                 updateLocation( typeDecl );
    281283                output << "typedef ";
    282284                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
     
    337339        }
    338340
    339         void CodeGenerator::postvisit( __attribute__((unused)) ConstructorInit * init ){
     341        void CodeGenerator::postvisit( ConstructorInit * init ){
    340342                assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
    341343                // pseudo-output for constructor/destructor pairs
    342                 output << "<ctorinit>{" << std::endl << ++indent << "ctor: ";
     344                output << "<ctorinit>{" << endl << ++indent << "ctor: ";
    343345                maybeAccept( init->get_ctor(), *visitor );
    344                 output << ", " << std::endl << indent << "dtor: ";
     346                output << ", " << endl << indent << "dtor: ";
    345347                maybeAccept( init->get_dtor(), *visitor );
    346                 output << std::endl << --indent << "}";
     348                output << endl << --indent << "}";
    347349        }
    348350
     
    763765        void CodeGenerator::postvisit( StmtExpr * stmtExpr ) {
    764766                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    765                 updateLocation( stmtExpr );
    766                 output << "({" << std::endl;
     767                output << "({" << endl;
    767768                ++indent;
    768769                unsigned int numStmts = stmts.size();
    769770                unsigned int i = 0;
    770771                for ( Statement * stmt : stmts ) {
    771                         updateLocation( stmt );
    772772                        output << indent << printLabels( stmt->get_labels() );
    773773                        if ( i+1 == numStmts ) {
     
    860860
    861861        void CodeGenerator::postvisit( IfStmt * ifStmt ) {
    862                 updateLocation( ifStmt );
    863862                output << "if ( ";
    864863                ifStmt->get_condition()->accept( *visitor );
     
    874873
    875874        void CodeGenerator::postvisit( SwitchStmt * switchStmt ) {
    876                 updateLocation( switchStmt );
    877875                output << "switch ( " ;
    878876                switchStmt->get_condition()->accept( *visitor );
    879877                output << " ) ";
    880878
    881                 output << "{" << std::endl;
     879                output << "{" << endl;
    882880                ++indent;
    883881                acceptAll( switchStmt->get_statements(), *visitor );
     
    887885
    888886        void CodeGenerator::postvisit( CaseStmt * caseStmt ) {
    889                 updateLocation( caseStmt );
    890887                if ( caseStmt->isDefault()) {
    891888                        output << "default";
     
    894891                        caseStmt->get_condition()->accept( *visitor );
    895892                } // if
    896                 output << ":\n";
     893                output << ":" << endl;
    897894
    898895                std::list<Statement *> sts = caseStmt->get_statements();
Note: See TracChangeset for help on using the changeset viewer.