Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision 2e60a1acf6f276ab4fa11bb30b9ab76c82c0489d)
+++ src/GenPoly/Box.cc	(revision 6635c74de4f9c9e0fcd036b6a3fd012d355e65d4)
@@ -26,4 +26,5 @@
 #include "PolyMutator.h"
 #include "FindFunction.h"
+#include "ScopedMap.h"
 #include "ScrubTyVars.h"
 
@@ -112,8 +113,7 @@
 			ObjectDecl *makeTemporary( Type *type );
 
-			typedef std::map< std::string, DeclarationWithType *> AdapterMap;
 			std::map< std::string, DeclarationWithType *> assignOps;
 			ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps;
-			std::stack< AdapterMap > adapters;
+			ScopedMap< std::string, DeclarationWithType* > adapters;
 			DeclarationWithType *retval;
 			bool useRetval;
@@ -448,7 +448,5 @@
 		}
 
-		Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {
-			adapters.push(AdapterMap());
-		}
+		Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {}
 
 		/// Returns T if the given declaration is (*?=?)(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise
@@ -553,5 +551,4 @@
 				} // for
 
-				AdapterMap & adapters = Pass1::adapters.top();
 				for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
 					std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
@@ -975,10 +972,11 @@
 					mangleName += makePolyMonoSuffix( originalFunction, exprTyVars );
 
-					AdapterMap & adapters = Pass1::adapters.top();
-					AdapterMap::iterator adapter = adapters.find( mangleName );
+					typedef ScopedMap< std::string, DeclarationWithType* >::iterator AdapterIter;
+					AdapterIter 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 ) );
+						std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
+						adapter = answer.first;
 						stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
 					} // if
@@ -1359,11 +1357,10 @@
 
 		void Pass1::doBeginScope() {
-			// push a copy of the current map
-			adapters.push(adapters.top());
+			adapters.beginScope();
 			scopedAssignOps.beginScope();
 		}
 
 		void Pass1::doEndScope() {
-			adapters.pop();
+			adapters.endScope();
 			scopedAssignOps.endScope();
 		}
