Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/GenPoly/Box.cc	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -9,7 +9,7 @@
 // Author           : Richard C. Bilson
 // Created On       : Mon May 18 07:44:20 2015
-// Last Modified By : Rob Schluntz
-// Last Modified On : Tue Aug 11 16:22:35 2015
-// Update Count     : 89
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Thu Nov 19 17:40:51 2015
+// Update Count     : 133
 //
 
@@ -47,4 +47,6 @@
 	namespace {
 		const std::list<Label> noLabels;
+
+		FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
 
 		class Pass1 : public PolyMutator {
@@ -78,5 +80,5 @@
 			ObjectDecl *makeTemporary( Type *type );
 
-			typedef std::map< std::string, FunctionDecl *> AdapterMap;
+			typedef std::map< std::string, DeclarationWithType *> AdapterMap;
 			std::map< std::string, DeclarationWithType *> assignOps;
 			std::stack< AdapterMap > adapters;
@@ -144,37 +146,7 @@
 		}
 
-		bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
-			bool doTransform = false;
-			if ( ! function->get_returnVals().empty() ) {
-				if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
-	
-					// figure out if the return type is specified by a type parameter
-					for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
-						if ( (*tyVar)->get_name() == typeInst->get_name() ) {
-							doTransform = true;
-							name = typeInst->get_name();
-							break;
-						} // if
-					} // for
-					if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
-						doTransform = true;
-					} // if
-				} // if
-			} // if
-			return doTransform;
-		}
-
-		bool isPolyRet( FunctionType *function, std::string &name ) {
-			TyVarMap dummyTyVars;
-			return isPolyRet( function, name, dummyTyVars );
-		}
-
-		bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
-			std::string dummyString;
-			return isPolyRet( function, dummyString, otherTyVars );
-		}
-
 		Pass1::Pass1()
 			: useRetval( false ), tempNamer( "_temp" ) {
+			adapters.push(AdapterMap());
 		}
 
@@ -210,9 +182,11 @@
 
 		DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
-			if ( functionDecl->get_statements() ) {
+			if ( functionDecl->get_statements() ) {		// empty routine body ?
+				doBeginScope();
 				TyVarMap oldtyVars = scopeTyVars;
 				DeclarationWithType *oldRetval = retval;
 				bool oldUseRetval = useRetval;
-	
+
+				// process polymorphic return value
 				retval = 0;
 				std::string typeName;
@@ -227,19 +201,44 @@
 				} // if
 	
-				scopeTyVars.clear();
-///     std::cerr << "clear\n";
+				FunctionType *functionType = functionDecl->get_functionType();
 				makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
 				findAssignOps( functionDecl->get_functionType()->get_forall() );
+
+				std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
+				std::list< FunctionType *> functions;
+				for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
+					for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
+						findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter );
+					} // for
+				} // for
+				for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
+					findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter );
+				} // for
+				AdapterMap & adapters = Pass1::adapters.top();
+				for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
+					std::string mangleName = SymTab::Mangler::mangle( *funType );
+					if ( isPolyRet( *funType, scopeTyVars ) ) {
+						// 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 the mangled names to be different.
+						mangleName += "polyret_";
+					} // if
+					if ( adapters.find( mangleName ) == adapters.end() ) {
+						std::string adapterName = makeAdapterName( mangleName );
+						adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) );
+					} // if
+				} // for
+
 				functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
   
 				scopeTyVars = oldtyVars;
-///     std::cerr << "end FunctionDecl: ";
-///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
-///       std::cerr << i->first << " ";
-///     }
-///     std::cerr << "\n";
+				// std::cerr << "end FunctionDecl: ";
+				// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
+				// 	std::cerr << i->first << " ";
+				// }
+				// std::cerr << "\n";
 				retval = oldRetval;
 				useRetval = oldUseRetval;
-				// doEndScope();
+				doEndScope();
 			} // if
 			return functionDecl;
@@ -349,4 +348,7 @@
 			} // if
 			std::string mangleName = SymTab::Mangler::mangle( function );
+			if ( isPolyRet( function, tyVars ) ) {
+				mangleName += "polyret_";
+			} // if
 			std::string adapterName = makeAdapterName( mangleName );
 
@@ -457,8 +459,10 @@
 ///       return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() );
 ///     } else {
-				UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
-				deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
-				deref->get_results().push_back( arg->get_type()->clone() );
-				return deref;
+				if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) {
+					UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
+					deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
+					deref->get_results().push_back( arg->get_type()->clone() );
+					return deref;
+				} // if
 ///     }
 			} // if
@@ -544,7 +548,6 @@
 			} // for
 
-			// parameter function types for which an appropriate adapter has been generated.
-			// we cannot use the types after applying substitutions, since two different 
-			// parameter types may be unified to the same type
+			// parameter function types for which an appropriate adapter has been generated.  we cannot use the types
+			// after applying substitutions, since two different parameter types may be unified to the same type
 			std::set< std::string > adaptersDone;
 
@@ -554,47 +557,33 @@
 				std::string mangleName = SymTab::Mangler::mangle( realFunction );
 
-				// only attempt to create an adapter or pass one as a parameter if we haven't 
-				// already done so for this pre-substitution parameter function type.
+				// only attempt to create an adapter or pass one as a parameter if we haven't already done so for this
+				// pre-substitution parameter function type.
 				if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
-					std::string mangleName = SymTab::Mangler::mangle( realFunction );
 					adaptersDone.insert( adaptersDone.begin(), mangleName );
 					
-					// apply substitution to type variables to figure out what the 
-					// adapter's type should look like
+					// apply substitution to type variables to figure out what the adapter's type should look like
 					assert( env );
 					env->apply( realFunction );
 					mangleName = SymTab::Mangler::mangle( realFunction );
 
-					if ( needsAdapter( realFunction, exprTyVars, true ) ) {
-						// the function still contains type variables, which means we are in a polymorphic
-						// context and the adapter function is a parameter - call the parameter and don't 
-						// create a new adapter.
-						appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
-					} 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
+					if ( isPolyRet( originalFunction, exprTyVars ) ) {
+						mangleName += "polyret_";
+					} // if
+
+					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, DeclarationWithType *>( 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
 			} // for
-		}
+		} // passAdapters
 
 		TypeInstType *isPolyPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {
@@ -769,9 +758,9 @@
 
 		Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
-///     std::cerr << "mutate appExpr: ";
-///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
-///       std::cerr << i->first << " ";
-///     }
-///     std::cerr << "\n";
+			// std::cerr << "mutate appExpr: ";
+			// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
+			// 	std::cerr << i->first << " ";
+			// }
+			// std::cerr << "\n";
 			bool oldUseRetval = useRetval;
 			useRetval = false;
@@ -799,9 +788,9 @@
 				ret = addPolyRetParam( appExpr, function, typeName, arg );
 			} else if ( needsAdapter( function, scopeTyVars ) ) {
-///     std::cerr << "needs adapter: ";
-///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
-///       std::cerr << i->first << " ";
-///     }
-///     std::cerr << "\n";
+				// std::cerr << "needs adapter: ";
+				// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
+				// 	std::cerr << i->first << " ";
+				// }
+				// std::cerr << "\n";
 				// change the application so it calls the adapter rather than the passed function
 				ret = applyAdapter( appExpr, function, arg, scopeTyVars );
@@ -913,5 +902,5 @@
 			// actually, maybe this could (should?) push
 			// a copy of the current map
-			adapters.push(AdapterMap());
+			adapters.push(adapters.top());
 		}
 
@@ -935,4 +924,7 @@
 			for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
 				std::string mangleName = SymTab::Mangler::mangle( *funType );
+				if ( isPolyRet( *funType, scopeTyVars ) ) {
+					mangleName += "polyret_";
+				} // if
 				if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
 					std::string adapterName = makeAdapterName( mangleName );
@@ -1000,4 +992,5 @@
 			for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
 				ObjectDecl *thisParm;
+				// add all size parameters to parameter list
 				if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
 					thisParm = newObj->clone();
@@ -1006,4 +999,5 @@
 					++last;
 				}
+				// move all assertions into parameter list
 				for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
 ///      *assert = (*assert)->acceptMutator( *this );
Index: src/GenPoly/Box.h
===================================================================
--- src/GenPoly/Box.h	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/GenPoly/Box.h	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue May 19 07:32:33 2015
-// Update Count     : 2
+// Last Modified On : Thu Nov 19 17:24:01 2015
+// Update Count     : 5
 //
 
Index: src/GenPoly/GenPoly.cc
===================================================================
--- src/GenPoly/GenPoly.cc	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/GenPoly/GenPoly.cc	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue May 19 07:37:46 2015
-// Update Count     : 1
+// Last Modified On : Thu Nov 19 17:23:44 2015
+// Update Count     : 10
 //
 
@@ -21,36 +21,56 @@
 
 namespace GenPoly {
-	// interface functions
-	bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
-		return isPolyVal( type, tyVars, false );
+	// A function needs an adapter if it returns a polymorphic value or if any of its
+	// parameters have polymorphic type
+	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
+		if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
+			return true;
+		} // if
+		for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
+			if ( isPolyVal( (*innerArg)->get_type(), tyVars ) ) {
+				return true;
+			} // if
+		} // for
+		return false;
 	}
 
-	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {  
-		return needsAdapter( adaptee, tyVars, false );
+	bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
+		bool doTransform = false;
+		if ( ! function->get_returnVals().empty() ) {
+			if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
+	
+				// figure out if the return type is specified by a type parameter
+				for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
+					if ( (*tyVar)->get_name() == typeInst->get_name() ) {
+						doTransform = true;
+						name = typeInst->get_name();
+						break;
+					} // if
+				} // for
+				if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
+					doTransform = true;
+				} // if
+			} // if
+		} // if
+		return doTransform;
 	}
 
-	bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars ) {
-		if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
+	bool isPolyRet( FunctionType *function, std::string &name ) {
+		TyVarMap dummyTyVars;
+		return isPolyRet( function, name, dummyTyVars );
+	}
+
+	bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
+		std::string dummyString;
+		return isPolyRet( function, dummyString, otherTyVars );
+	}
+
+	bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
+		if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
 			if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
 				return true;
 			} // if
-			return considerAllTyVars;
 		} // if
 		return false;
-	}
-
-	// A function needs an adapter if it returns a polymorphic value or if any of its
-	// parameters have polymorphic type
-	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars ) {
-		bool needsAdapter = false;
-		if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars, considerAllTyVars ) ) {
-			needsAdapter = true;
-		} // if
-		for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); ! needsAdapter && innerArg != adaptee->get_parameters().end(); ++innerArg ) {
-			if ( isPolyVal( (*innerArg)->get_type(), tyVars, considerAllTyVars ) ) {
-				needsAdapter = true;
-			} // if
-		} // for
-		return needsAdapter;
 	}
 
Index: src/GenPoly/GenPoly.h
===================================================================
--- src/GenPoly/GenPoly.h	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/GenPoly/GenPoly.h	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue May 19 07:38:34 2015
-// Update Count     : 1
+// Last Modified On : Thu Nov 19 17:24:03 2015
+// Update Count     : 4
 //
 
@@ -25,12 +25,10 @@
 	typedef std::map< std::string, TypeDecl::Kind > TyVarMap;
 
-	// considerAllTyVars allows ignoring the contents of the TyVarMap parameter, for the situations where
-	// it is important only that a TypeInstType node exists.
-
 	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVarr );
-	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars );
-	bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars );
+	bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars );
+	bool isPolyRet( FunctionType *function, std::string &name );
+	bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars );
+//	bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars );
 	bool isPolyVal( Type *type, const TyVarMap &tyVars );
-	bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars );
 	void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap );
 } // namespace GenPoly
