Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    r49db841 rd18540f  
    7272                };
    7373
    74                 /// Updates the call sites of polymorphic functions.
    7574                /// Replaces polymorphic return types with out-parameters,
    7675                /// replaces calls to polymorphic functions with adapter calls,
    7776                /// and adds appropriate type variables to the function call.
    78                 class CallAdapter final : public BoxPass, public WithConstTypeSubstitution, public WithStmtsToAdd, public WithGuards, public WithVisitorRef<CallAdapter>, public WithShortCircuiting {
     77                class Pass1 final : public BoxPass, public WithConstTypeSubstitution, public WithStmtsToAdd, public WithGuards, public WithVisitorRef<Pass1>, public WithShortCircuiting {
    7978                  public:
    80                         CallAdapter();
     79                        Pass1();
    8180
    8281                        void premutate( FunctionDecl * functionDecl );
     
    139138                };
    140139
    141                 /// Updates declarations (and types) that require adapters.
    142140                /// * Moves polymorphic returns in function types to pointer-type parameters
    143141                /// * adds type size and assertion parameters to parameter lists
    144                 struct DeclAdapter final : public BoxPass, public WithGuards {
     142                struct Pass2 final : public BoxPass, public WithGuards {
    145143                        void handleAggDecl();
    146144
     
    212210                };
    213211
    214                 /// Erases unneeded/unwanted polymorphic information.
    215212                /// Replaces initialization of polymorphic values with alloca,
    216213                /// declaration of dtype/ftype with appropriate void expression,
    217214                /// sizeof expressions of polymorphic types with the proper variable,
    218215                /// and strips fields from generic struct declarations.
    219                 struct Eraser final {
     216                struct Pass3 final {
    220217                        void premutate( ObjectDecl * objectDecl );
    221218                        void premutate( FunctionDecl * functionDecl );
     
    228225        void box( std::list< Declaration *>& translationUnit ) {
    229226                PassVisitor<LayoutFunctionBuilder> layoutBuilder;
    230                 PassVisitor<CallAdapter> callAdapter;
    231                 PassVisitor<DeclAdapter> declAdapter;
     227                PassVisitor<Pass1> pass1;
     228                PassVisitor<Pass2> pass2;
    232229                PassVisitor<PolyGenericCalculator> polyCalculator;
    233                 PassVisitor<Eraser> eraser;
     230                PassVisitor<Pass3> pass3;
    234231
    235232                acceptAll( translationUnit, layoutBuilder );
    236                 mutateAll( translationUnit, callAdapter );
    237                 mutateAll( translationUnit, declAdapter );
     233                mutateAll( translationUnit, pass1 );
     234                mutateAll( translationUnit, pass2 );
    238235                mutateAll( translationUnit, polyCalculator );
    239                 mutateAll( translationUnit, eraser );
     236                mutateAll( translationUnit, pass3 );
    240237        }
    241238
    242 ////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////
     239        ////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////////
    243240
    244241        /// Get a list of type declarations that will affect a layout function
     
    420417        }
    421418
    422 ////////////////////////////////////////////// CallAdapter //////////////////////////////////////
     419        ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
    423420
    424421        namespace {
     
    462459                Type *replaceWithConcrete( Type *type, TypeSubstitution const * env, bool doClone = true );
    463460
    464                 CallAdapter::CallAdapter() : tempNamer( "_temp" ) {}
    465 
    466                 void CallAdapter::premutate( FunctionDecl *functionDecl ) {
     461                Pass1::Pass1() : tempNamer( "_temp" ) {}
     462
     463                void Pass1::premutate( FunctionDecl *functionDecl ) {
    467464                        if ( functionDecl->get_statements() ) {         // empty routine body ?
    468465                                // std::cerr << "mutating function: " << functionDecl->get_mangleName() << std::endl;
     
    506503                }
    507504
    508                 void CallAdapter::premutate( TypeDecl *typeDecl ) {
     505                void Pass1::premutate( TypeDecl *typeDecl ) {
    509506                        addToTyVarMap( typeDecl, scopeTyVars );
    510507                }
    511508
    512                 void CallAdapter::premutate( CommaExpr *commaExpr ) {
     509                void Pass1::premutate( CommaExpr *commaExpr ) {
    513510                        // Attempting to find application expressions that were mutated by the copy constructor passes
    514511                        // to use an explicit return variable, so that the variable can be reused as a parameter to the
     
    528525                }
    529526
    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 ) {
     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 ) {
    531528                        Type *polyType = isPolyType( parmType, exprTyVars );
    532529                        if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
     
    555552                }
    556553
    557                 std::list< Expression *>::iterator CallAdapter::passTypeVars( ApplicationExpr *appExpr, Type *polyRetType, const TyVarMap &exprTyVars ) {
     554                std::list< Expression *>::iterator Pass1::passTypeVars( ApplicationExpr *appExpr, Type *polyRetType, const TyVarMap &exprTyVars ) {
    558555                        assert( env );
    559556                        std::list< Expression *>::iterator arg = appExpr->args.begin();
     
    608605                }
    609606
    610                 ObjectDecl *CallAdapter::makeTemporary( Type *type ) {
     607                ObjectDecl *Pass1::makeTemporary( Type *type ) {
    611608                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, type, 0 );
    612609                        stmtsToAddBefore.push_back( new DeclStmt( newObj ) );
     
    614611                }
    615612
    616                 Expression *CallAdapter::addRetParam( ApplicationExpr *appExpr, Type *retType ) {
     613                Expression *Pass1::addRetParam( ApplicationExpr *appExpr, Type *retType ) {
    617614                        // Create temporary to hold return value of polymorphic function and produce that temporary as a result
    618615                        // using a comma expression.
     
    677674                }
    678675
    679                 Expression *CallAdapter::addDynRetParam( ApplicationExpr *appExpr, Type *dynType ) {
     676                Expression *Pass1::addDynRetParam( ApplicationExpr *appExpr, Type *dynType ) {
    680677                        Type *concrete = replaceWithConcrete( dynType, env );
    681678                        // add out-parameter for return value
     
    683680                }
    684681
    685                 Expression *CallAdapter::applyAdapter( ApplicationExpr *appExpr, FunctionType *function ) {
     682                Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function ) {
    686683                        Expression *ret = appExpr;
    687684                        if ( isDynRet( function, scopeTyVars ) ) {
     
    732729                }
    733730
    734                 void CallAdapter::boxParam( Expression *&arg, Type *param, const TyVarMap &exprTyVars ) {
     731                void Pass1::boxParam( Expression *&arg, Type *param, const TyVarMap &exprTyVars ) {
    735732                        assertf( arg->result, "arg does not have result: %s", toString( arg ).c_str() );
    736733                        addCast( arg, param, exprTyVars );
     
    767764                }
    768765
    769                 void CallAdapter::boxParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *function, const TyVarMap &exprTyVars ) {
     766                void Pass1::boxParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *function, const TyVarMap &exprTyVars ) {
    770767                        for ( DeclarationWithType * param : function->parameters ) {
    771768                                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() );
     
    775772                }
    776773
    777                 void CallAdapter::addInferredParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *functionType, const TyVarMap &tyVars ) {
     774                void Pass1::addInferredParams( ApplicationExpr *appExpr, std::list< Expression *>::iterator arg, FunctionType *functionType, const TyVarMap &tyVars ) {
    778775                        for ( TypeDecl * const tyVar : functionType->forall ) {
    779776                                for ( DeclarationWithType * const assert : tyVar->assertions ) {
     
    843840                }
    844841
    845                 FunctionDecl *CallAdapter::makeAdapter( FunctionType const *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
     842                FunctionDecl *Pass1::makeAdapter( FunctionType const *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
    846843                        FunctionType *adapterType = makeAdapterType( adaptee, tyVars );
    847844                        adapterType = ScrubTyVars::scrub( adapterType, tyVars );
     
    903900                }
    904901
    905                 void CallAdapter::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
     902                void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
    906903                        // collect a list of function types passed as parameters or implicit parameters (assertions)
    907904                        std::list<FunctionType const *> functions;
     
    971968                }
    972969
    973                 Expression *CallAdapter::handleIntrinsics( ApplicationExpr *appExpr ) {
     970                Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) {
    974971                        if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->function ) ) {
    975972                                if ( varExpr->var->linkage == LinkageSpec::Intrinsic ) {
     
    10941091                }
    10951092
    1096                 Expression *CallAdapter::postmutate( ApplicationExpr *appExpr ) {
     1093                Expression *Pass1::postmutate( ApplicationExpr *appExpr ) {
    10971094                        // std::cerr << "mutate appExpr: " << InitTweak::getFunctionName( appExpr ) << std::endl;
    10981095                        // for ( auto tyVar : scopeTyVars ) {
     
    11661163                }
    11671164
    1168                 Expression * CallAdapter::postmutate( UntypedExpr *expr ) {
     1165                Expression * Pass1::postmutate( UntypedExpr *expr ) {
    11691166                        if ( isPolyDeref( expr, scopeTyVars, env ) ) {
    11701167                                Expression *ret = expr->args.front();
     
    11761173                }
    11771174
    1178                 void CallAdapter::premutate( AddressExpr * ) { visit_children = false; }
    1179 
    1180                 Expression * CallAdapter::postmutate( AddressExpr * addrExpr ) {
     1175                void Pass1::premutate( AddressExpr * ) { visit_children = false; }
     1176
     1177                Expression * Pass1::postmutate( AddressExpr * addrExpr ) {
    11811178                        assert( addrExpr->arg->result && ! addrExpr->arg->result->isVoid() );
    11821179
     
    12091206                }
    12101207
    1211                 void CallAdapter::premutate( ReturnStmt *returnStmt ) {
     1208                void Pass1::premutate( ReturnStmt *returnStmt ) {
    12121209                        if ( retval && returnStmt->expr ) {
    12131210                                assert( returnStmt->expr->result && ! returnStmt->expr->result->isVoid() );
     
    12171214                }
    12181215
    1219                 void CallAdapter::premutate( PointerType *pointerType ) {
     1216                void Pass1::premutate( PointerType *pointerType ) {
    12201217                        GuardScope( scopeTyVars );
    12211218                        makeTyVarMap( pointerType, scopeTyVars );
    12221219                }
    12231220
    1224                 void CallAdapter::premutate( FunctionType *functionType ) {
     1221                void Pass1::premutate( FunctionType *functionType ) {
    12251222                        GuardScope( scopeTyVars );
    12261223                        makeTyVarMap( functionType, scopeTyVars );
    12271224                }
    12281225
    1229                 void CallAdapter::beginScope() {
     1226                void Pass1::beginScope() {
    12301227                        adapters.beginScope();
    12311228                }
    12321229
    1233                 void CallAdapter::endScope() {
     1230                void Pass1::endScope() {
    12341231                        adapters.endScope();
    12351232                }
    12361233
    1237 ////////////////////////////////////////// DeclAdapter //////////////////////////////////////////
    1238 
    1239                 void DeclAdapter::addAdapters( FunctionType *functionType ) {
     1234////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
     1235
     1236                void Pass2::addAdapters( FunctionType *functionType ) {
    12401237                        std::list< FunctionType const *> functions;
    12411238                        for ( DeclarationWithType * const arg : functionType->parameters ) {
     
    12571254                }
    12581255
    1259                 DeclarationWithType * DeclAdapter::postmutate( FunctionDecl *functionDecl ) {
     1256                DeclarationWithType * Pass2::postmutate( FunctionDecl *functionDecl ) {
    12601257                        FunctionType * ftype = functionDecl->type;
    12611258                        if ( ! ftype->returnVals.empty() && functionDecl->statements ) {
     
    12821279                }
    12831280
    1284                 void DeclAdapter::premutate( StructDecl * ) {
     1281                void Pass2::premutate( StructDecl * ) {
    12851282                        // prevent tyVars from leaking into containing scope
    12861283                        GuardScope( scopeTyVars );
    12871284                }
    12881285
    1289                 void DeclAdapter::premutate( UnionDecl * ) {
     1286                void Pass2::premutate( UnionDecl * ) {
    12901287                        // prevent tyVars from leaking into containing scope
    12911288                        GuardScope( scopeTyVars );
    12921289                }
    12931290
    1294                 void DeclAdapter::premutate( TraitDecl * ) {
     1291                void Pass2::premutate( TraitDecl * ) {
    12951292                        // prevent tyVars from leaking into containing scope
    12961293                        GuardScope( scopeTyVars );
    12971294                }
    12981295
    1299                 void DeclAdapter::premutate( TypeDecl *typeDecl ) {
     1296                void Pass2::premutate( TypeDecl *typeDecl ) {
    13001297                        addToTyVarMap( typeDecl, scopeTyVars );
    13011298                }
    13021299
    1303                 void DeclAdapter::premutate( PointerType *pointerType ) {
     1300                void Pass2::premutate( PointerType *pointerType ) {
    13041301                        GuardScope( scopeTyVars );
    13051302                        makeTyVarMap( pointerType, scopeTyVars );
    13061303                }
    13071304
    1308                 void DeclAdapter::premutate( FunctionType *funcType ) {
     1305                void Pass2::premutate( FunctionType *funcType ) {
    13091306                        GuardScope( scopeTyVars );
    13101307                        makeTyVarMap( funcType, scopeTyVars );
     
    13901387                }
    13911388
    1392 ////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////
     1389////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////////////////////////
    13931390
    13941391                PolyGenericCalculator::PolyGenericCalculator()
     
    14711468                        // make sure that any type information passed into the function is accounted for
    14721469                        for ( DeclarationWithType * const fnParam : funcType->get_parameters() ) {
    1473                                 // condition here duplicates that in DeclAdapter::mutate( FunctionType* )
     1470                                // condition here duplicates that in Pass2::mutate( FunctionType* )
    14741471                                Type *polyType = isPolyType( fnParam->get_type(), scopeTyVars );
    14751472                                if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
     
    18811878                }
    18821879
    1883 ////////////////////////////////////////// Eraser ///////////////////////////////////////////////
    1884 
    1885                 void Eraser::premutate( ObjectDecl * objectDecl ) {
     1880////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
     1881
     1882                void Pass3::premutate( ObjectDecl * objectDecl ) {
    18861883                        ScrubTyVars::scrubAll( objectDecl );
    18871884                }
    18881885
    1889                 void Eraser::premutate( FunctionDecl * functionDecl ) {
     1886                void Pass3::premutate( FunctionDecl * functionDecl ) {
    18901887                        ScrubTyVars::scrubAll( functionDecl );
    18911888                }
    18921889
    1893                 void Eraser::premutate( TypedefDecl * typedefDecl ) {
     1890                void Pass3::premutate( TypedefDecl * typedefDecl ) {
    18941891                        ScrubTyVars::scrubAll( typedefDecl );
    18951892                }
     
    19001897                }
    19011898
    1902                 void Eraser::premutate( StructDecl * structDecl ) {
     1899                void Pass3::premutate( StructDecl * structDecl ) {
    19031900                        stripGenericMembers( structDecl );
    19041901                }
    19051902
    1906                 void Eraser::premutate( UnionDecl * unionDecl ) {
     1903                void Pass3::premutate( UnionDecl * unionDecl ) {
    19071904                        stripGenericMembers( unionDecl );
    19081905                }
Note: See TracChangeset for help on using the changeset viewer.