Changeset fc12f05 for src/CodeGen/Generate.cc
- Timestamp:
- Nov 13, 2023, 3:43:43 AM (23 months ago)
- Branches:
- master
- Children:
- 25f2798
- Parents:
- 0030b508 (diff), 2174191 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/Generate.cc
r0030b508 rfc12f05 19 19 #include <string> // for operator<< 20 20 21 #include "CodeGenerator .h" // for CodeGenerator, doSemicolon, oper...21 #include "CodeGeneratorNew.hpp" // for CodeGenerator_new, doSemicolon, ... 22 22 #include "GenType.h" // for genPrettyType 23 #include "Common/PassVisitor.h" // for PassVisitor24 #include "SynTree/LinkageSpec.h" // for isBuiltin, isGeneratable25 #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode26 #include "SynTree/Declaration.h" // for Declaration27 #include "SynTree/Type.h" // for Type28 23 29 24 using namespace std; 30 25 31 26 namespace CodeGen { 32 namespace {33 /// Removes misc. nodes that should not exist in CodeGen34 struct TreeCleaner {35 void premutate( CompoundStmt * stmt );36 Statement * postmutate( ImplicitCtorDtorStmt * stmt );37 27 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 28 namespace { 29 bool shouldClean( ast::Decl const * decl ) { 30 return dynamic_cast<ast::TraitDecl const *>( decl ); 62 31 } 63 32 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 ); 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; 82 42 } 83 43 84 Statement * TreeCleaner::postmutate( ImplicitCtorDtorStmt * stmt ) { 85 Statement * callStmt = nullptr; 86 std::swap( stmt->callStmt, callStmt ); 87 delete stmt; 88 return callStmt; 44 ast::Stmt const * postvisit( ast::ImplicitCtorDtorStmt const * stmt ) { 45 return stmt->callStmt; 89 46 } 47 }; 48 } // namespace 90 49 91 bool TreeCleaner::shouldClean( Declaration * decl ) { 92 return dynamic_cast< TraitDecl * >( decl ); 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; 93 65 } 94 } // namespace 66 } 67 } 68 95 69 } // namespace CodeGen 96 70
Note:
See TracChangeset
for help on using the changeset viewer.