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