Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision ae4c85a0274ee43a9f1e10b436adffdb989d7643)
+++ src/GenPoly/Box.cc	(revision c29d9ce432aee5b42e1d8d594682e2ece6c8008b)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Rob Schluntz
-// Last Modified On : Thu Jul 30 14:35:40 2015
-// Update Count     : 54
+// Last Modified On : Tue Aug 11 16:22:35 2015
+// Update Count     : 89
 //
 
@@ -77,7 +77,7 @@
 			Expression *handleIntrinsics( ApplicationExpr *appExpr );
 			ObjectDecl *makeTemporary( Type *type );
-  
+
+			typedef std::map< std::string, FunctionDecl *> AdapterMap;
 			std::map< std::string, DeclarationWithType *> assignOps;
-			typedef std::map< std::string, FunctionDecl *> AdapterMap;
 			std::stack< AdapterMap > adapters;
 			DeclarationWithType *retval;
@@ -168,4 +168,9 @@
 			TyVarMap dummyTyVars;
 			return isPolyRet( function, name, dummyTyVars );
+		}
+
+		bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
+			std::string dummyString;
+			return isPolyRet( function, dummyString, otherTyVars );
 		}
 
@@ -526,5 +531,5 @@
 		}
 
-		void Pass1::passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars ) {
+		void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
 			// collect a list of function types passed as parameters or implicit parameters (assertions)
 			std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
@@ -545,4 +550,5 @@
 
 			for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
+				FunctionType *originalFunction = (*funType)->clone();
 				FunctionType *realFunction = (*funType)->clone();
 				std::string mangleName = SymTab::Mangler::mangle( realFunction );
@@ -559,6 +565,4 @@
 					env->apply( realFunction );
 					mangleName = SymTab::Mangler::mangle( realFunction );
-					AdapterMap & adapters = Pass1::adapters.top();
-					AdapterMap::iterator adapter = adapters.find( mangleName );
 
 					if ( needsAdapter( realFunction, exprTyVars, true ) ) {
@@ -567,15 +571,27 @@
 						// create a new adapter.
 						appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
-						continue;
-					} else if ( adapter == adapters.end() ) {
-						// adapter has not been created yet in the current scope, so define it
-						FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
-						adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
-						stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
+					} else {
+						if ( isPolyRet( originalFunction, exprTyVars ) ) {
+							// if the return type involved polymorphic types, then
+							// the adapter will need to take those polymorphic types
+							// as pointers. Therefore, there can be two different
+							// functions with the same mangled name, so we need two adapter map
+							// stacks and also we need the mangled names to be different.
+							mangleName += "polyret_";
+						}
+
+						AdapterMap & adapters = Pass1::adapters.top();
+						AdapterMap::iterator adapter = adapters.find( mangleName );
+						if ( adapter == adapters.end() ) {
+							// adapter has not been created yet in the current scope, so define it
+							FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
+							adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
+							stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
+						} // if
+						assert( adapter != adapters.end() );
+
+						// add the appropriate adapter as a parameter
+						appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
 					} // if
-					assert( adapter != adapters.end() );
-
-					// add the appropriate adapter as a parameter
-					appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
 				} // if
 			} // for
