Changeset 49db841 for src


Ignore:
Timestamp:
Dec 20, 2022, 1:25:59 PM (16 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master
Children:
df9e412
Parents:
199456c
Message:

Cleaning old box pass for easier translation. Renamed the numbered sub-passes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r199456c r49db841  
    7272                };
    7373
     74                /// Updates the call sites of polymorphic functions.
    7475                /// Replaces polymorphic return types with out-parameters,
    7576                /// replaces calls to polymorphic functions with adapter calls,
    7677                /// and adds appropriate type variables to the function call.
    77                 class Pass1 final : public BoxPass, public WithConstTypeSubstitution, public WithStmtsToAdd, public WithGuards, public WithVisitorRef<Pass1>, public WithShortCircuiting {
     78                class CallAdapter final : public BoxPass, public WithConstTypeSubstitution, public WithStmtsToAdd, public WithGuards, public WithVisitorRef<CallAdapter>, public WithShortCircuiting {
    7879                  public:
    79                         Pass1();
     80                        CallAdapter();
    8081
    8182                        void premutate( FunctionDecl * functionDecl );
     
    138139                };
    139140
     141                /// Updates declarations (and types) that require adapters.
    140142                /// * Moves polymorphic returns in function types to pointer-type parameters
    141143                /// * adds type size and assertion parameters to parameter lists
    142                 struct Pass2 final : public BoxPass, public WithGuards {
     144                struct DeclAdapter final : public BoxPass, public WithGuards {
    143145                        void handleAggDecl();
    144146
     
    210212                };
    211213
     214                /// Erases unneeded/unwanted polymorphic information.
    212215                /// Replaces initialization of polymorphic values with alloca,
    213216                /// declaration of dtype/ftype with appropriate void expression,
    214217                /// sizeof expressions of polymorphic types with the proper variable,
    215218                /// and strips fields from generic struct declarations.
    216                 struct Pass3 final {
     219                struct Eraser final {
    217220                        void premutate( ObjectDecl * objectDecl );
    218221                        void premutate( FunctionDecl * functionDecl );
     
    225228        void box( std::list< Declaration *>& translationUnit ) {
    226229                PassVisitor<LayoutFunctionBuilder> layoutBuilder;
    227                 PassVisitor<Pass1> pass1;
    228                 PassVisitor<Pass2> pass2;
     230                PassVisitor<CallAdapter> callAdapter;
     231                PassVisitor<DeclAdapter> declAdapter;
    229232                PassVisitor<PolyGenericCalculator> polyCalculator;
    230                 PassVisitor<Pass3> pass3;
     233                PassVisitor<Eraser> eraser;
    231234
    232235                acceptAll( translationUnit, layoutBuilder );
    233                 mutateAll( translationUnit, pass1 );
    234                 mutateAll( translationUnit, pass2 );
     236                mutateAll( translationUnit, callAdapter );
     237                mutateAll( translationUnit, declAdapter );
    235238                mutateAll( translationUnit, polyCalculator );
    236                 mutateAll( translationUnit, pass3 );
     239                mutateAll( translationUnit, eraser );
    237240        }
    238241
    239         ////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////////
     242////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////
    240243
    241244        /// Get a list of type declarations that will affect a layout function
     
    417420        }
    418421
    419         ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
     422////////////////////////////////////////////// CallAdapter //////////////////////////////////////
    420423
    421424        namespace {
     
    459462                Type *replaceWithConcrete( Type *type, TypeSubstitution const * env, bool doClone = true );
    460463
    461                 Pass1::Pass1() : tempNamer( "_temp" ) {}
    462 
    463                 void Pass1::premutate( FunctionDecl *functionDecl ) {
     464                CallAdapter::CallAdapter() : tempNamer( "_temp" ) {}
     465
     466                void CallAdapter::premutate( FunctionDecl *functionDecl ) {
    464467                        if ( functionDecl->get_statements() ) {         // empty routine body ?
    465468                                // std::cerr << "mutating function: " << functionDecl->get_mangleName() << std::endl;
     
    503506                }
    504507
    505                 void Pass1::premutate( TypeDecl *typeDecl ) {
     508                void CallAdapter::premutate( TypeDecl *typeDecl ) {
    506509                        addToTyVarMap( typeDecl, scopeTyVars );
    507510                }
    508511
    509                 void Pass1::premutate( CommaExpr *commaExpr ) {
     512                void CallAdapter::premutate( CommaExpr *commaExpr ) {
    510513                        // Attempting to find application expressions that were mutated by the copy constructor passes
    511514                        // to use an explicit return variable, so that the variable can be reused as a parameter to the
     
    525528                }
    526529
    527                 std::list< Expression *>::iterator Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) {
     530                std::list< Expression *>::iterator CallAdapter::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) {
    528531                        Type *polyType = isPolyType( parmType, exprTyVars );
    529532                        if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
     
    552555                }
    553556
    554                 std::list< Expression *>::iterator Pass1::passTypeVars( ApplicationExpr *appExpr, Type *polyRetType, const TyVarMap &exprTyVars ) {
     557                std::list< Expression *>::iterator CallAdapter::passTypeVars( ApplicationExpr *appExpr, Type *polyRetType, const TyVarMap &exprTyVars ) {
    555558                        assert( env );
    556559                        std::list< Expression *>::iterator arg = appExpr->args.begin();
     
    605608                }
    606609
    607                 ObjectDecl *Pass1::makeTemporary( Type *type ) {
     610                ObjectDecl *CallAdapter::makeTemporary( Type *type ) {
    608611                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, type, 0 );
    609612                        stmtsToAddBefore.push_back( new DeclStmt( newObj ) );
     
    611614                }
    612615
    613                 Expression *Pass1::addRetParam( ApplicationExpr *appExpr, Type *retType ) {
     616                Expression *CallAdapter::addRetParam( ApplicationExpr *appExpr, Type *retType ) {
    614617                        // Create temporary to hold return value of polymorphic function and produce that temporary as a result
    615618                        // using a comma expression.
     
    674677                }
    675678
    676                 Expression *Pass1::addDynRetParam( ApplicationExpr *appExpr, Type *dynType ) {
     679                Expression *CallAdapter::addDynRetParam( ApplicationExpr *appExpr, Type *dynType ) {
    677680                        Type *concrete = replaceWithConcrete( dynType, env );
    678681                        // add out-parameter for return value
     
    680683                }
    681684
    682                 Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function ) {
     685                Expression *CallAdapter::applyAdapter( ApplicationExpr *appExpr, FunctionType *function ) {
    683686                        Expression *ret = appExpr;
    684687                        if ( isDynRet( function, scopeTyVars ) ) {
     
    729732                }
    730733
    731                 void Pass1::boxParam( Expression *&arg, Type *param, const TyVarMap &exprTyVars ) {
     734                void CallAdapter::boxParam( Expression *&arg, Type *param, const TyVarMap &exprTyVars ) {
    732735                        assertf( arg->result, "arg does not have result: %s", toString( arg ).c_str() );
    733736                        addCast( arg, param, exprTyVars );
     
    764767                }
    765768
    766                 void Pass1::boxParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *function, const TyVarMap &exprTyVars ) {
     769                void CallAdapter::boxParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *function, const TyVarMap &exprTyVars ) {
    767770                        for ( DeclarationWithType * param : function->parameters ) {
    768771                                assertf( arg != appExpr->args.end(), "boxParams: missing argument for param %s to %s in %s", toString( param ).c_str(), toString( function ).c_str(), toString( appExpr ).c_str() );
     
    772775                }
    773776
    774                 void Pass1::addInferredParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *functionType, const TyVarMap &tyVars ) {
     777                void CallAdapter::addInferredParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *functionType, const TyVarMap &tyVars ) {
    775778                        for ( TypeDecl * const tyVar : functionType->forall ) {
    776779                                for ( DeclarationWithType * const assert : tyVar->assertions ) {
     
    840843                }
    841844
    842                 FunctionDecl *Pass1::makeAdapter( FunctionType const *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
     845                FunctionDecl *CallAdapter::makeAdapter( FunctionType const *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
    843846                        FunctionType *adapterType = makeAdapterType( adaptee, tyVars );
    844847                        adapterType = ScrubTyVars::scrub( adapterType, tyVars );
     
    900903                }
    901904
    902                 void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
     905                void CallAdapter::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
    903906                        // collect a list of function types passed as parameters or implicit parameters (assertions)
    904907                        std::list<FunctionType const *> functions;
     
    968971                }
    969972
    970                 Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) {
     973                Expression *CallAdapter::handleIntrinsics( ApplicationExpr *appExpr ) {
    971974                        if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->function ) ) {
    972975                                if ( varExpr->var->linkage == LinkageSpec::Intrinsic ) {
     
    10911094                }
    10921095
    1093                 Expression *Pass1::postmutate( ApplicationExpr *appExpr ) {
     1096                Expression *CallAdapter::postmutate( ApplicationExpr *appExpr ) {
    10941097                        // std::cerr << "mutate appExpr: " << InitTweak::getFunctionName( appExpr ) << std::endl;
    10951098                        // for ( auto tyVar : scopeTyVars ) {
     
    11631166                }
    11641167
    1165                 Expression * Pass1::postmutate( UntypedExpr *expr ) {
     1168                Expression * CallAdapter::postmutate( UntypedExpr *expr ) {
    11661169                        if ( isPolyDeref( expr, scopeTyVars, env ) ) {
    11671170                                Expression *ret = expr->args.front();
     
    11731176                }
    11741177
    1175                 void Pass1::premutate( AddressExpr * ) { visit_children = false; }
    1176 
    1177                 Expression * Pass1::postmutate( AddressExpr * addrExpr ) {
     1178                void CallAdapter::premutate( AddressExpr * ) { visit_children = false; }
     1179
     1180                Expression * CallAdapter::postmutate( AddressExpr * addrExpr ) {
    11781181                        assert( addrExpr->arg->result && ! addrExpr->arg->result->isVoid() );
    11791182
     
    12061209                }
    12071210
    1208                 void Pass1::premutate( ReturnStmt *returnStmt ) {
     1211                void CallAdapter::premutate( ReturnStmt *returnStmt ) {
    12091212                        if ( retval && returnStmt->expr ) {
    12101213                                assert( returnStmt->expr->result && ! returnStmt->expr->result->isVoid() );
     
    12141217                }
    12151218
    1216                 void Pass1::premutate( PointerType *pointerType ) {
     1219                void CallAdapter::premutate( PointerType *pointerType ) {
    12171220                        GuardScope( scopeTyVars );
    12181221                        makeTyVarMap( pointerType, scopeTyVars );
    12191222                }
    12201223
    1221                 void Pass1::premutate( FunctionType *functionType ) {
     1224                void CallAdapter::premutate( FunctionType *functionType ) {
    12221225                        GuardScope( scopeTyVars );
    12231226                        makeTyVarMap( functionType, scopeTyVars );
    12241227                }
    12251228
    1226                 void Pass1::beginScope() {
     1229                void CallAdapter::beginScope() {
    12271230                        adapters.beginScope();
    12281231                }
    12291232
    1230                 void Pass1::endScope() {
     1233                void CallAdapter::endScope() {
    12311234                        adapters.endScope();
    12321235                }
    12331236
    1234 ////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
    1235 
    1236                 void Pass2::addAdapters( FunctionType *functionType ) {
     1237////////////////////////////////////////// DeclAdapter //////////////////////////////////////////
     1238
     1239                void DeclAdapter::addAdapters( FunctionType *functionType ) {
    12371240                        std::list< FunctionType const *> functions;
    12381241                        for ( DeclarationWithType * const arg : functionType->parameters ) {
     
    12541257                }
    12551258
    1256                 DeclarationWithType * Pass2::postmutate( FunctionDecl *functionDecl ) {
     1259                DeclarationWithType * DeclAdapter::postmutate( FunctionDecl *functionDecl ) {
    12571260                        FunctionType * ftype = functionDecl->type;
    12581261                        if ( ! ftype->returnVals.empty() && functionDecl->statements ) {
     
    12791282                }
    12801283
    1281                 void Pass2::premutate( StructDecl * ) {
     1284                void DeclAdapter::premutate( StructDecl * ) {
    12821285                        // prevent tyVars from leaking into containing scope
    12831286                        GuardScope( scopeTyVars );
    12841287                }
    12851288
    1286                 void Pass2::premutate( UnionDecl * ) {
     1289                void DeclAdapter::premutate( UnionDecl * ) {
    12871290                        // prevent tyVars from leaking into containing scope
    12881291                        GuardScope( scopeTyVars );
    12891292                }
    12901293
    1291                 void Pass2::premutate( TraitDecl * ) {
     1294                void DeclAdapter::premutate( TraitDecl * ) {
    12921295                        // prevent tyVars from leaking into containing scope
    12931296                        GuardScope( scopeTyVars );
    12941297                }
    12951298
    1296                 void Pass2::premutate( TypeDecl *typeDecl ) {
     1299                void DeclAdapter::premutate( TypeDecl *typeDecl ) {
    12971300                        addToTyVarMap( typeDecl, scopeTyVars );
    12981301                }
    12991302
    1300                 void Pass2::premutate( PointerType *pointerType ) {
     1303                void DeclAdapter::premutate( PointerType *pointerType ) {
    13011304                        GuardScope( scopeTyVars );
    13021305                        makeTyVarMap( pointerType, scopeTyVars );
    13031306                }
    13041307
    1305                 void Pass2::premutate( FunctionType *funcType ) {
     1308                void DeclAdapter::premutate( FunctionType *funcType ) {
    13061309                        GuardScope( scopeTyVars );
    13071310                        makeTyVarMap( funcType, scopeTyVars );
     
    13871390                }
    13881391
    1389 ////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////////////////////////
     1392////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////
    13901393
    13911394                PolyGenericCalculator::PolyGenericCalculator()
     
    14681471                        // make sure that any type information passed into the function is accounted for
    14691472                        for ( DeclarationWithType * const fnParam : funcType->get_parameters() ) {
    1470                                 // condition here duplicates that in Pass2::mutate( FunctionType* )
     1473                                // condition here duplicates that in DeclAdapter::mutate( FunctionType* )
    14711474                                Type *polyType = isPolyType( fnParam->get_type(), scopeTyVars );
    14721475                                if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
     
    18781881                }
    18791882
    1880 ////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
    1881 
    1882                 void Pass3::premutate( ObjectDecl * objectDecl ) {
     1883////////////////////////////////////////// Eraser ///////////////////////////////////////////////
     1884
     1885                void Eraser::premutate( ObjectDecl * objectDecl ) {
    18831886                        ScrubTyVars::scrubAll( objectDecl );
    18841887                }
    18851888
    1886                 void Pass3::premutate( FunctionDecl * functionDecl ) {
     1889                void Eraser::premutate( FunctionDecl * functionDecl ) {
    18871890                        ScrubTyVars::scrubAll( functionDecl );
    18881891                }
    18891892
    1890                 void Pass3::premutate( TypedefDecl * typedefDecl ) {
     1893                void Eraser::premutate( TypedefDecl * typedefDecl ) {
    18911894                        ScrubTyVars::scrubAll( typedefDecl );
    18921895                }
     
    18971900                }
    18981901
    1899                 void Pass3::premutate( StructDecl * structDecl ) {
     1902                void Eraser::premutate( StructDecl * structDecl ) {
    19001903                        stripGenericMembers( structDecl );
    19011904                }
    19021905
    1903                 void Pass3::premutate( UnionDecl * unionDecl ) {
     1906                void Eraser::premutate( UnionDecl * unionDecl ) {
    19041907                        stripGenericMembers( unionDecl );
    19051908                }
Note: See TracChangeset for help on using the changeset viewer.