- Timestamp:
- Dec 20, 2022, 1:25:59 PM (16 months ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- df9e412
- Parents:
- 199456c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/Box.cc
r199456c r49db841 72 72 }; 73 73 74 /// Updates the call sites of polymorphic functions. 74 75 /// Replaces polymorphic return types with out-parameters, 75 76 /// replaces calls to polymorphic functions with adapter calls, 76 77 /// 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 { 78 79 public: 79 Pass1();80 CallAdapter(); 80 81 81 82 void premutate( FunctionDecl * functionDecl ); … … 138 139 }; 139 140 141 /// Updates declarations (and types) that require adapters. 140 142 /// * Moves polymorphic returns in function types to pointer-type parameters 141 143 /// * adds type size and assertion parameters to parameter lists 142 struct Pass2final : public BoxPass, public WithGuards {144 struct DeclAdapter final : public BoxPass, public WithGuards { 143 145 void handleAggDecl(); 144 146 … … 210 212 }; 211 213 214 /// Erases unneeded/unwanted polymorphic information. 212 215 /// Replaces initialization of polymorphic values with alloca, 213 216 /// declaration of dtype/ftype with appropriate void expression, 214 217 /// sizeof expressions of polymorphic types with the proper variable, 215 218 /// and strips fields from generic struct declarations. 216 struct Pass3final {219 struct Eraser final { 217 220 void premutate( ObjectDecl * objectDecl ); 218 221 void premutate( FunctionDecl * functionDecl ); … … 225 228 void box( std::list< Declaration *>& translationUnit ) { 226 229 PassVisitor<LayoutFunctionBuilder> layoutBuilder; 227 PassVisitor< Pass1> pass1;228 PassVisitor< Pass2> pass2;230 PassVisitor<CallAdapter> callAdapter; 231 PassVisitor<DeclAdapter> declAdapter; 229 232 PassVisitor<PolyGenericCalculator> polyCalculator; 230 PassVisitor< Pass3> pass3;233 PassVisitor<Eraser> eraser; 231 234 232 235 acceptAll( translationUnit, layoutBuilder ); 233 mutateAll( translationUnit, pass1);234 mutateAll( translationUnit, pass2);236 mutateAll( translationUnit, callAdapter ); 237 mutateAll( translationUnit, declAdapter ); 235 238 mutateAll( translationUnit, polyCalculator ); 236 mutateAll( translationUnit, pass3);239 mutateAll( translationUnit, eraser ); 237 240 } 238 241 239 ////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////////242 ////////////////////////////////// LayoutFunctionBuilder //////////////////////////////////////// 240 243 241 244 /// Get a list of type declarations that will affect a layout function … … 417 420 } 418 421 419 ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////422 ////////////////////////////////////////////// CallAdapter ////////////////////////////////////// 420 423 421 424 namespace { … … 459 462 Type *replaceWithConcrete( Type *type, TypeSubstitution const * env, bool doClone = true ); 460 463 461 Pass1::Pass1() : tempNamer( "_temp" ) {}462 463 void Pass1::premutate( FunctionDecl *functionDecl ) {464 CallAdapter::CallAdapter() : tempNamer( "_temp" ) {} 465 466 void CallAdapter::premutate( FunctionDecl *functionDecl ) { 464 467 if ( functionDecl->get_statements() ) { // empty routine body ? 465 468 // std::cerr << "mutating function: " << functionDecl->get_mangleName() << std::endl; … … 503 506 } 504 507 505 void Pass1::premutate( TypeDecl *typeDecl ) {508 void CallAdapter::premutate( TypeDecl *typeDecl ) { 506 509 addToTyVarMap( typeDecl, scopeTyVars ); 507 510 } 508 511 509 void Pass1::premutate( CommaExpr *commaExpr ) {512 void CallAdapter::premutate( CommaExpr *commaExpr ) { 510 513 // Attempting to find application expressions that were mutated by the copy constructor passes 511 514 // to use an explicit return variable, so that the variable can be reused as a parameter to the … … 525 528 } 526 529 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 ) { 528 531 Type *polyType = isPolyType( parmType, exprTyVars ); 529 532 if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { … … 552 555 } 553 556 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 ) { 555 558 assert( env ); 556 559 std::list< Expression *>::iterator arg = appExpr->args.begin(); … … 605 608 } 606 609 607 ObjectDecl * Pass1::makeTemporary( Type *type ) {610 ObjectDecl *CallAdapter::makeTemporary( Type *type ) { 608 611 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), Type::StorageClasses(), LinkageSpec::C, 0, type, 0 ); 609 612 stmtsToAddBefore.push_back( new DeclStmt( newObj ) ); … … 611 614 } 612 615 613 Expression * Pass1::addRetParam( ApplicationExpr *appExpr, Type *retType ) {616 Expression *CallAdapter::addRetParam( ApplicationExpr *appExpr, Type *retType ) { 614 617 // Create temporary to hold return value of polymorphic function and produce that temporary as a result 615 618 // using a comma expression. … … 674 677 } 675 678 676 Expression * Pass1::addDynRetParam( ApplicationExpr *appExpr, Type *dynType ) {679 Expression *CallAdapter::addDynRetParam( ApplicationExpr *appExpr, Type *dynType ) { 677 680 Type *concrete = replaceWithConcrete( dynType, env ); 678 681 // add out-parameter for return value … … 680 683 } 681 684 682 Expression * Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function ) {685 Expression *CallAdapter::applyAdapter( ApplicationExpr *appExpr, FunctionType *function ) { 683 686 Expression *ret = appExpr; 684 687 if ( isDynRet( function, scopeTyVars ) ) { … … 729 732 } 730 733 731 void Pass1::boxParam( Expression *&arg, Type *param, const TyVarMap &exprTyVars ) {734 void CallAdapter::boxParam( Expression *&arg, Type *param, const TyVarMap &exprTyVars ) { 732 735 assertf( arg->result, "arg does not have result: %s", toString( arg ).c_str() ); 733 736 addCast( arg, param, exprTyVars ); … … 764 767 } 765 768 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 ) { 767 770 for ( DeclarationWithType * param : function->parameters ) { 768 771 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() ); … … 772 775 } 773 776 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 ) { 775 778 for ( TypeDecl * const tyVar : functionType->forall ) { 776 779 for ( DeclarationWithType * const assert : tyVar->assertions ) { … … 840 843 } 841 844 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 ) { 843 846 FunctionType *adapterType = makeAdapterType( adaptee, tyVars ); 844 847 adapterType = ScrubTyVars::scrub( adapterType, tyVars ); … … 900 903 } 901 904 902 void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {905 void CallAdapter::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) { 903 906 // collect a list of function types passed as parameters or implicit parameters (assertions) 904 907 std::list<FunctionType const *> functions; … … 968 971 } 969 972 970 Expression * Pass1::handleIntrinsics( ApplicationExpr *appExpr ) {973 Expression *CallAdapter::handleIntrinsics( ApplicationExpr *appExpr ) { 971 974 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->function ) ) { 972 975 if ( varExpr->var->linkage == LinkageSpec::Intrinsic ) { … … 1091 1094 } 1092 1095 1093 Expression * Pass1::postmutate( ApplicationExpr *appExpr ) {1096 Expression *CallAdapter::postmutate( ApplicationExpr *appExpr ) { 1094 1097 // std::cerr << "mutate appExpr: " << InitTweak::getFunctionName( appExpr ) << std::endl; 1095 1098 // for ( auto tyVar : scopeTyVars ) { … … 1163 1166 } 1164 1167 1165 Expression * Pass1::postmutate( UntypedExpr *expr ) {1168 Expression * CallAdapter::postmutate( UntypedExpr *expr ) { 1166 1169 if ( isPolyDeref( expr, scopeTyVars, env ) ) { 1167 1170 Expression *ret = expr->args.front(); … … 1173 1176 } 1174 1177 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 ) { 1178 1181 assert( addrExpr->arg->result && ! addrExpr->arg->result->isVoid() ); 1179 1182 … … 1206 1209 } 1207 1210 1208 void Pass1::premutate( ReturnStmt *returnStmt ) {1211 void CallAdapter::premutate( ReturnStmt *returnStmt ) { 1209 1212 if ( retval && returnStmt->expr ) { 1210 1213 assert( returnStmt->expr->result && ! returnStmt->expr->result->isVoid() ); … … 1214 1217 } 1215 1218 1216 void Pass1::premutate( PointerType *pointerType ) {1219 void CallAdapter::premutate( PointerType *pointerType ) { 1217 1220 GuardScope( scopeTyVars ); 1218 1221 makeTyVarMap( pointerType, scopeTyVars ); 1219 1222 } 1220 1223 1221 void Pass1::premutate( FunctionType *functionType ) {1224 void CallAdapter::premutate( FunctionType *functionType ) { 1222 1225 GuardScope( scopeTyVars ); 1223 1226 makeTyVarMap( functionType, scopeTyVars ); 1224 1227 } 1225 1228 1226 void Pass1::beginScope() {1229 void CallAdapter::beginScope() { 1227 1230 adapters.beginScope(); 1228 1231 } 1229 1232 1230 void Pass1::endScope() {1233 void CallAdapter::endScope() { 1231 1234 adapters.endScope(); 1232 1235 } 1233 1236 1234 ////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////1235 1236 void Pass2::addAdapters( FunctionType *functionType ) {1237 ////////////////////////////////////////// DeclAdapter ////////////////////////////////////////// 1238 1239 void DeclAdapter::addAdapters( FunctionType *functionType ) { 1237 1240 std::list< FunctionType const *> functions; 1238 1241 for ( DeclarationWithType * const arg : functionType->parameters ) { … … 1254 1257 } 1255 1258 1256 DeclarationWithType * Pass2::postmutate( FunctionDecl *functionDecl ) {1259 DeclarationWithType * DeclAdapter::postmutate( FunctionDecl *functionDecl ) { 1257 1260 FunctionType * ftype = functionDecl->type; 1258 1261 if ( ! ftype->returnVals.empty() && functionDecl->statements ) { … … 1279 1282 } 1280 1283 1281 void Pass2::premutate( StructDecl * ) {1284 void DeclAdapter::premutate( StructDecl * ) { 1282 1285 // prevent tyVars from leaking into containing scope 1283 1286 GuardScope( scopeTyVars ); 1284 1287 } 1285 1288 1286 void Pass2::premutate( UnionDecl * ) {1289 void DeclAdapter::premutate( UnionDecl * ) { 1287 1290 // prevent tyVars from leaking into containing scope 1288 1291 GuardScope( scopeTyVars ); 1289 1292 } 1290 1293 1291 void Pass2::premutate( TraitDecl * ) {1294 void DeclAdapter::premutate( TraitDecl * ) { 1292 1295 // prevent tyVars from leaking into containing scope 1293 1296 GuardScope( scopeTyVars ); 1294 1297 } 1295 1298 1296 void Pass2::premutate( TypeDecl *typeDecl ) {1299 void DeclAdapter::premutate( TypeDecl *typeDecl ) { 1297 1300 addToTyVarMap( typeDecl, scopeTyVars ); 1298 1301 } 1299 1302 1300 void Pass2::premutate( PointerType *pointerType ) {1303 void DeclAdapter::premutate( PointerType *pointerType ) { 1301 1304 GuardScope( scopeTyVars ); 1302 1305 makeTyVarMap( pointerType, scopeTyVars ); 1303 1306 } 1304 1307 1305 void Pass2::premutate( FunctionType *funcType ) {1308 void DeclAdapter::premutate( FunctionType *funcType ) { 1306 1309 GuardScope( scopeTyVars ); 1307 1310 makeTyVarMap( funcType, scopeTyVars ); … … 1387 1390 } 1388 1391 1389 ////////////////////////////////////////// PolyGenericCalculator //////////////////////////////// ////////////////////1392 ////////////////////////////////////////// PolyGenericCalculator //////////////////////////////// 1390 1393 1391 1394 PolyGenericCalculator::PolyGenericCalculator() … … 1468 1471 // make sure that any type information passed into the function is accounted for 1469 1472 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* ) 1471 1474 Type *polyType = isPolyType( fnParam->get_type(), scopeTyVars ); 1472 1475 if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) { … … 1878 1881 } 1879 1882 1880 ////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////1881 1882 void Pass3::premutate( ObjectDecl * objectDecl ) {1883 ////////////////////////////////////////// Eraser /////////////////////////////////////////////// 1884 1885 void Eraser::premutate( ObjectDecl * objectDecl ) { 1883 1886 ScrubTyVars::scrubAll( objectDecl ); 1884 1887 } 1885 1888 1886 void Pass3::premutate( FunctionDecl * functionDecl ) {1889 void Eraser::premutate( FunctionDecl * functionDecl ) { 1887 1890 ScrubTyVars::scrubAll( functionDecl ); 1888 1891 } 1889 1892 1890 void Pass3::premutate( TypedefDecl * typedefDecl ) {1893 void Eraser::premutate( TypedefDecl * typedefDecl ) { 1891 1894 ScrubTyVars::scrubAll( typedefDecl ); 1892 1895 } … … 1897 1900 } 1898 1901 1899 void Pass3::premutate( StructDecl * structDecl ) {1902 void Eraser::premutate( StructDecl * structDecl ) { 1900 1903 stripGenericMembers( structDecl ); 1901 1904 } 1902 1905 1903 void Pass3::premutate( UnionDecl * unionDecl ) {1906 void Eraser::premutate( UnionDecl * unionDecl ) { 1904 1907 stripGenericMembers( unionDecl ); 1905 1908 }
Note: See TracChangeset
for help on using the changeset viewer.