Changes in translator/GenPoly/Box.cc [b1a6d6b:bdd516a]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/GenPoly/Box.cc
rb1a6d6b rbdd516a 7 7 8 8 #include <set> 9 #include <stack>10 9 #include <string> 11 10 #include <iterator> … … 51 50 virtual Type *mutate( FunctionType *pointerType ); 52 51 53 virtual void doBeginScope();54 52 virtual void doEndScope(); 55 53 private: … … 68 66 69 67 std::map< std::string, DeclarationWithType *> assignOps; 70 typedef std::map< std::string, FunctionDecl *> AdapterMap; 71 std::stack< AdapterMap > adapters; 68 std::map< std::string, FunctionDecl *> adapters; 72 69 DeclarationWithType *retval; 73 70 bool useRetval; … … 129 126 130 127 namespace { 131 std::string makeAdapterName( const std::string &mangleName ) {132 return "_adapter" + mangleName;133 }134 135 128 bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) { 136 129 bool doTransform = false; … … 226 219 retval = oldRetval; 227 220 useRetval = oldUseRetval; 228 //doEndScope();221 doEndScope(); 229 222 } // if 230 223 return functionDecl; … … 323 316 Type *concrete = env->lookup( typeName ); 324 317 if ( concrete == 0 ) { 325 throw SemanticError( "Unbound type variable " + typeName + " in 318 throw SemanticError( "Unbound type variable " + typeName + " in", appExpr ); 326 319 } // if 327 320 return addRetParam( appExpr, function, concrete, arg ); … … 333 326 ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg ); 334 327 } // if 335 std::string mangleName = SymTab::Mangler::mangle( function );336 std::string adapterName = makeAdapterName( mangleName );337 338 328 appExpr->get_args().push_front( appExpr->get_function() ); 339 appExpr->set_function( new NameExpr( adapterName) );329 appExpr->set_function( new NameExpr( "_adapter" + SymTab::Mangler::mangle( function ) ) ); 340 330 341 331 return ret; … … 347 337 TypeInstType *typeInst = dynamic_cast< TypeInstType *>( param ); 348 338 if ( typeInst && exprTyVars.find( typeInst->get_name() ) != exprTyVars.end() ) { 349 if ( dynamic_cast< TypeInstType *>( arg->get_results().front() ) ) { 350 // if the argument's type is a type parameter, we don't need to box again! 351 return; 352 } else if ( arg->get_results().front()->get_isLvalue() ) { 353 // VariableExpr and MemberExpr are lvalues 339 if ( arg->get_results().front()->get_isLvalue() ) { 354 340 arg = new AddressExpr( arg ); 355 341 } else { … … 385 371 for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) { 386 372 /// std::cout << "parameter is "; 387 /// (*param)->print( std:: fcout );373 /// (*param)->print( std::cout ); 388 374 /// std::cout << std::endl << "argument is "; 389 375 /// (*arg)->print( std::cout ); … … 462 448 } 463 449 464 465 466 450 FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) { 467 451 FunctionType *adapterType = makeAdapterType( adaptee, tyVars ); … … 512 496 CompoundStmt *adapterBody = new CompoundStmt( noLabels ); 513 497 adapterBody->get_kids().push_back( bodyStmt ); 514 std::string adapterName = makeAdapterName( mangleName ); 515 return new FunctionDecl( adapterName, Declaration::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false ); 498 return new FunctionDecl( "_adapter" + mangleName, Declaration::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false ); 516 499 } 517 500 … … 532 515 assert( env ); 533 516 env->apply( realFunction ); 534 535 517 std::string mangleName = SymTab::Mangler::mangle( realFunction ); 536 518 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { 537 AdapterMap & adapters = Pass1::adapters.top(); 538 AdapterMap::iterator adapter = adapters.find( mangleName ); 539 540 if ( needsAdapter( realFunction, exprTyVars, true ) ) { 541 // the function still contains type variables, which means we are in a polymorphic 542 // context and the adapter function is a parameter - call the parameter and don't 543 // create a new adapter. 544 appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) ); 545 continue; 546 } else if ( adapter == adapters.end() ) { 547 FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars ); 519 std::map< std::string, FunctionDecl *>::iterator adapter = adapters.find( mangleName ); 520 if ( adapter == adapters.end() ) { 521 FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars ); 548 522 adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) ); 549 523 stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) ); … … 551 525 assert( adapter != adapters.end() ); 552 526 appExpr->get_args().push_front( new VariableExpr( adapter->second ) ); 553 // appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );554 527 adaptersDone.insert( adaptersDone.begin(), mangleName ); 555 528 } // if … … 872 845 } 873 846 874 void Pass1::doBeginScope() {875 adapters.push(AdapterMap());876 }877 878 847 void Pass1::doEndScope() { 879 adapters. pop();848 adapters.clear(); 880 849 } 881 850 … … 896 865 std::string mangleName = SymTab::Mangler::mangle( *funType ); 897 866 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) { 898 std::string adapterName = makeAdapterName( mangleName ); 899 paramList.push_front( new ObjectDecl( adapterName, Declaration::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ); 867 paramList.push_front( new ObjectDecl( "_adapter" + mangleName, Declaration::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ); 900 868 adaptersDone.insert( adaptersDone.begin(), mangleName ); 901 869 }
Note: See TracChangeset
for help on using the changeset viewer.