Ignore:
Timestamp:
Nov 13, 2023, 3:43:43 AM (23 months ago)
Author:
JiadaL <j82liang@…>
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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/Generate.cc

    r0030b508 rfc12f05  
    1919#include <string>                    // for operator<<
    2020
    21 #include "CodeGenerator.h"           // for CodeGenerator, doSemicolon, oper...
     21#include "CodeGeneratorNew.hpp"      // for CodeGenerator_new, doSemicolon, ...
    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
    2823
    2924using namespace std;
    3025
    3126namespace 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 );
    3727
    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
     28namespace {
     29        bool shouldClean( ast::Decl const * decl ) {
     30                return dynamic_cast<ast::TraitDecl const *>( decl );
    6231        }
    6332
    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;
    8242                }
    8343
    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;
    8946                }
     47        };
     48} // namespace
    9049
    91                 bool TreeCleaner::shouldClean( Declaration * decl ) {
    92                         return dynamic_cast< TraitDecl * >( decl );
     50void 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;
    9365                }
    94         } // namespace
     66        }
     67}
     68
    9569} // namespace CodeGen
    9670
Note: See TracChangeset for help on using the changeset viewer.