Changeset c0714bf for src/InitTweak


Ignore:
Timestamp:
Sep 15, 2017, 8:07:51 PM (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:
33a25f9
Parents:
eada3cf
Message:

Convert ResolveCopyCtors? to PassVisitor?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixInit.cc

    reada3cf rc0714bf  
    7070namespace InitTweak {
    7171        namespace {
    72                 typedef std::unordered_map< Expression *, TypeSubstitution * > EnvMap;
    7372                typedef std::unordered_map< int, int > UnqCount;
    7473
    75                 class InsertImplicitCalls : public WithTypeSubstitution {
    76                 public:
     74                struct InsertImplicitCalls : public WithTypeSubstitution {
    7775                        /// wrap function application expressions as ImplicitCopyCtorExpr nodes so that it is easy to identify which
    7876                        /// function calls need their parameters to be copy constructed
    79                         static void insert( std::list< Declaration * > & translationUnit, EnvMap & envMap );
    80 
    81                         InsertImplicitCalls( EnvMap & envMap ) : envMap( envMap ) {}
     77                        static void insert( std::list< Declaration * > & translationUnit );
    8278
    8379                        Expression * postmutate( ApplicationExpr * appExpr );
    84                         void premutate( StmtExpr * stmtExpr );
    85 
    86                         // collects environments for relevant nodes
    87                         EnvMap & envMap;
    8880                };
    8981
    90                 class ResolveCopyCtors final : public SymTab::Indexer {
    91                 public:
     82                struct ResolveCopyCtors final : public WithIndexer, public WithShortCircuiting, public WithTypeSubstitution {
    9283                        /// generate temporary ObjectDecls for each argument and return value of each ImplicitCopyCtorExpr,
    9384                        /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both
    9485                        /// arguments and return value temporaries
    95                         static void resolveImplicitCalls( std::list< Declaration * > & translationUnit, const EnvMap & envMap, UnqCount & unqCount );
    96 
    97                         typedef SymTab::Indexer Parent;
    98                         using Parent::visit;
    99 
    100                         ResolveCopyCtors( const EnvMap & envMap, UnqCount & unqCount ) : envMap( envMap ), unqCount( unqCount ) {}
    101 
    102                         virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ) override;
    103                         virtual void visit( UniqueExpr * unqExpr ) override;
    104                         virtual void visit( StmtExpr * stmtExpr ) override;
     86                        static void resolveImplicitCalls( std::list< Declaration * > & translationUnit, UnqCount & unqCount );
     87
     88                        ResolveCopyCtors( UnqCount & unqCount ) : unqCount( unqCount ) {}
     89
     90                        void postvisit( ImplicitCopyCtorExpr * impCpCtorExpr );
     91                        void postvisit( StmtExpr * stmtExpr );
     92                        void previsit( UniqueExpr * unqExpr );
     93                        void postvisit( UniqueExpr * unqExpr );
    10594
    10695                        /// create and resolve ctor/dtor expression: fname(var, [cpArg])
     
    111100                        void destructRet( ObjectDecl * ret, ImplicitCopyCtorExpr * impCpCtorExpr );
    112101
    113                         TypeSubstitution * env;
    114                         const EnvMap & envMap;
    115102                        UnqCount & unqCount; // count the number of times each unique expr ID appears
     103                        std::unordered_set< int > vars;
    116104                };
    117105
     
    292280                InitTweak::fixGlobalInit( translationUnit, filename, inLibrary );
    293281
    294                 EnvMap envMap;
    295282                UnqCount unqCount;
    296283
    297                 InsertImplicitCalls::insert( translationUnit, envMap );
    298                 ResolveCopyCtors::resolveImplicitCalls( translationUnit, envMap, unqCount );
     284                InsertImplicitCalls::insert( translationUnit );
     285                ResolveCopyCtors::resolveImplicitCalls( translationUnit, unqCount );
    299286                InsertDtors::insert( translationUnit );
    300287                FixInit::fixInitializers( translationUnit );
     
    315302
    316303        namespace {
    317                 void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit, EnvMap & envMap ) {
    318                         PassVisitor<InsertImplicitCalls> inserter( envMap );
     304                void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit ) {
     305                        PassVisitor<InsertImplicitCalls> inserter;
    319306                        mutateAll( translationUnit, inserter );
    320307                }
    321308
    322                 void ResolveCopyCtors::resolveImplicitCalls( std::list< Declaration * > & translationUnit, const EnvMap & envMap, UnqCount & unqCount ) {
    323                         ResolveCopyCtors resolver( envMap, unqCount );
     309                void ResolveCopyCtors::resolveImplicitCalls( std::list< Declaration * > & translationUnit, UnqCount & unqCount ) {
     310                        PassVisitor<ResolveCopyCtors> resolver( unqCount );
    324311                        acceptAll( translationUnit, resolver );
    325312                }
     
    395382                        // wrap each function call so that it is easy to identify nodes that have to be copy constructed
    396383                        ImplicitCopyCtorExpr * expr = new ImplicitCopyCtorExpr( appExpr );
    397                         // save the type substitution into the envMap so that it is easy to find.
     384                        // Move the type substitution to the new top-level, if it is attached to the appExpr.
    398385                        // Ensure it is not deleted with the ImplicitCopyCtorExpr by removing it before deletion.
    399386                        // The substitution is needed to obtain the type of temporary variables so that copy constructor
    400                         // calls can be resolved. Normally this is what PolyMutator is for, but the pass that resolves
    401                         // copy constructor calls must be an Indexer. We could alternatively make a PolyIndexer which
    402                         // saves the environment, or compute the types of temporaries here, but it's much simpler to
    403                         // save the environment here, and more cohesive to compute temporary variables and resolve copy
    404                         // constructor calls together.
     387                        // calls can be resolved.
    405388                        assert( env );
    406                         envMap[expr] = env;
     389                        std::swap( expr->env, appExpr->env );
    407390                        return expr;
    408                 }
    409 
    410                 void InsertImplicitCalls::premutate( StmtExpr * stmtExpr ) {
    411                         assert( env );
    412                         envMap[stmtExpr] = env;
    413391                }
    414392
     
    428406                        // (VariableExpr and already resolved expression)
    429407                        CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; )
    430                         Expression * resolved = ResolvExpr::findVoidExpression( untyped, *this );
     408                        Expression * resolved = ResolvExpr::findVoidExpression( untyped, indexer );
    431409                        assert( resolved );
    432410                        if ( resolved->get_env() ) {
     
    477455                }
    478456
    479                 void ResolveCopyCtors::visit( ImplicitCopyCtorExpr *impCpCtorExpr ) {
     457                void ResolveCopyCtors::postvisit( ImplicitCopyCtorExpr *impCpCtorExpr ) {
    480458                        CP_CTOR_PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; )
    481                         Parent::visit( impCpCtorExpr );
    482                         env = envMap.at(impCpCtorExpr);
    483                         assert( env );
    484459
    485460                        ApplicationExpr * appExpr = impCpCtorExpr->get_callExpr();
     
    510485                }
    511486
    512                 void ResolveCopyCtors::visit( StmtExpr * stmtExpr ) {
    513                         Parent::visit( stmtExpr );
    514                         env = envMap.at(stmtExpr);
     487                void ResolveCopyCtors::postvisit( StmtExpr * stmtExpr ) {
     488                        assert( env );
    515489                        assert( stmtExpr->get_result() );
    516490                        Type * result = stmtExpr->get_result();
     
    534508                                stmtExpr->get_dtors().push_front( makeCtorDtor( "^?{}", ret ) );
    535509                        } // if
    536 
    537                 }
    538 
    539                 void ResolveCopyCtors::visit( UniqueExpr * unqExpr ) {
    540                         static std::unordered_set< int > vars;
     510                }
     511
     512                void ResolveCopyCtors::previsit( UniqueExpr * unqExpr ) {
    541513                        unqCount[ unqExpr->get_id() ]++;  // count the number of unique expressions for each ID
    542514                        if ( vars.count( unqExpr->get_id() ) ) {
    543515                                // xxx - hack to prevent double-handling of unique exprs, otherwise too many temporary variables and destructors are generated
     516                                visit_children = false;
     517                        }
     518                }
     519
     520                void ResolveCopyCtors::postvisit( UniqueExpr * unqExpr ) {
     521                        if ( vars.count( unqExpr->get_id() ) ) {
     522                                // xxx - hack to prevent double-handling of unique exprs, otherwise too many temporary variables and destructors are generated
    544523                                return;
    545524                        }
    546525
    547                         Parent::visit( unqExpr );
    548526                        // it should never be necessary to wrap a void-returning expression in a UniqueExpr - if this assumption changes, this needs to be rethought
    549527                        assert( unqExpr->get_result() );
     
    582560
    583561                        // xxx - update to work with multiple return values
    584                         ObjectDecl * returnDecl = returnDecls.empty() ? NULL : returnDecls.front();
     562                        ObjectDecl * returnDecl = returnDecls.empty() ? nullptr : returnDecls.front();
    585563                        Expression * callExpr = impCpCtorExpr->get_callExpr();
    586564
     
    591569                        tempDecls.clear();
    592570                        returnDecls.clear();
    593                         impCpCtorExpr->set_callExpr( NULL );
    594                         impCpCtorExpr->set_env( NULL );
     571                        impCpCtorExpr->set_callExpr( nullptr );
     572                        std::swap( impCpCtorExpr->env, callExpr->env );
     573                        assert( impCpCtorExpr->env == nullptr );
    595574                        delete impCpCtorExpr;
    596575
Note: See TracChangeset for help on using the changeset viewer.