Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/Generate.cc

    rc6b4432 r60a8062  
    1919#include <string>                    // for operator<<
    2020
    21 #include "CodeGeneratorNew.hpp"      // for CodeGenerator_new, doSemicolon, ...
     21#include "CodeGenerator.h"           // for CodeGenerator, doSemicolon, oper...
    2222#include "GenType.h"                 // for genPrettyType
     23#include "Common/PassVisitor.h"      // for PassVisitor
     24#include "SynTree/LinkageSpec.h"     // for isBuiltin, isGeneratable
     25#include "SynTree/BaseSyntaxNode.h"  // for BaseSyntaxNode
     26#include "SynTree/Declaration.h"     // for Declaration
     27#include "SynTree/Type.h"            // for Type
    2328
    2429using namespace std;
    2530
    2631namespace CodeGen {
     32        namespace {
     33                /// Removes misc. nodes that should not exist in CodeGen
     34                struct TreeCleaner {
     35                        void premutate( CompoundStmt * stmt );
     36                        Statement * postmutate( ImplicitCtorDtorStmt * stmt );
    2737
    28 namespace {
    29         bool shouldClean( ast::Decl const * decl ) {
    30                 return dynamic_cast<ast::TraitDecl const *>( decl );
     38                        static bool shouldClean( Declaration * );
     39                };
     40
     41                void cleanTree( std::list< Declaration * > & translationUnit ) {
     42                        PassVisitor<TreeCleaner> cleaner;
     43                        filter( translationUnit, [](Declaration * decl) { return TreeCleaner::shouldClean(decl); }, false );
     44                        mutateAll( translationUnit, cleaner );
     45                } // cleanTree
     46        } // namespace
     47
     48        void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC, bool lineMarks, bool printExprTypes ) {
     49                cleanTree( translationUnit );
     50
     51                PassVisitor<CodeGenerator> cgv( os, pretty, generateC, lineMarks, printExprTypes );
     52                for ( auto & dcl : translationUnit ) {
     53                        if ( LinkageSpec::isGeneratable( dcl->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( dcl->get_linkage() ) ) ) {
     54                                cgv.pass.updateLocation( dcl );
     55                                dcl->accept(cgv);
     56                                if ( doSemicolon( dcl ) ) {
     57                                        os << ";";
     58                                } // if
     59                                os << cgv.pass.endl;
     60                        } // if
     61                } // for
    3162        }
    3263
    33         /// Removes various nodes that should not exist in CodeGen.
    34         struct TreeCleaner_new {
    35                 ast::CompoundStmt const * previsit( ast::CompoundStmt const * stmt ) {
    36                         auto mutStmt = ast::mutate( stmt );
    37                         erase_if( mutStmt->kids, []( ast::Stmt const * stmt ){
    38                                 auto declStmt = dynamic_cast<ast::DeclStmt const *>( stmt );
    39                                 return ( declStmt ) ? shouldClean( declStmt->decl ) : false;
    40                         } );
    41                         return mutStmt;
     64        void generate( BaseSyntaxNode * node, std::ostream & os ) {
     65                if ( Type * type = dynamic_cast< Type * >( node ) ) {
     66                        os << genPrettyType( type, "" );
     67                } else {
     68                        PassVisitor<CodeGenerator> cgv( os, true, false, false, false );
     69                        node->accept( cgv );
     70                }
     71                os << std::endl;
     72        }
     73
     74        namespace {
     75                void TreeCleaner::premutate( CompoundStmt * cstmt ) {
     76                        filter( cstmt->kids, [](Statement * stmt) {
     77                                if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
     78                                        return shouldClean( declStmt->decl );
     79                                }
     80                                return false;
     81                        }, false );
    4282                }
    4383
    44                 ast::Stmt const * postvisit( ast::ImplicitCtorDtorStmt const * stmt ) {
    45                         return stmt->callStmt;
     84                Statement * TreeCleaner::postmutate( ImplicitCtorDtorStmt * stmt ) {
     85                        Statement * callStmt = nullptr;
     86                        std::swap( stmt->callStmt, callStmt );
     87                        delete stmt;
     88                        return callStmt;
    4689                }
    47         };
    48 } // namespace
    4990
    50 void generate( ast::TranslationUnit & translationUnit, std::ostream & os, bool doIntrinsics,
    51                 bool pretty, bool generateC, bool lineMarks, bool printExprTypes ) {
    52         erase_if( translationUnit.decls, shouldClean );
    53         ast::Pass<TreeCleaner_new>::run( translationUnit );
    54 
    55         ast::Pass<CodeGenerator_new> cgv( os,
    56                         Options( pretty, generateC, lineMarks, printExprTypes ) );
    57         for ( auto & decl : translationUnit.decls ) {
    58                 if ( decl->linkage.is_generatable && (doIntrinsics || !decl->linkage.is_builtin ) ) {
    59                         cgv.core.updateLocation( decl );
    60                         decl->accept( cgv );
    61                         if ( doSemicolon( decl ) ) {
    62                                 os << ";";
    63                         }
    64                         os << cgv.core.endl;
     91                bool TreeCleaner::shouldClean( Declaration * decl ) {
     92                        return dynamic_cast< TraitDecl * >( decl );
    6593                }
    66         }
    67 }
    68 
     94        } // namespace
    6995} // namespace CodeGen
    7096
Note: See TracChangeset for help on using the changeset viewer.