Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision 43181074867ab6b19a3a764f8f4989f623670408)
+++ src/GenPoly/Box.cc	(revision dc124819c235617d272c2eee265317c408956362)
@@ -25,5 +25,4 @@
 #include "PolyMutator.h"
 #include "FindFunction.h"
-#include "ScopedMap.h"
 #include "ScrubTyVars.h"
 
@@ -38,4 +37,6 @@
 
 #include "ResolvExpr/TypeEnvironment.h"
+#include "ResolvExpr/TypeMap.h"
+#include "ResolvExpr/typeops.h"
 
 #include "SymTab/Mangler.h"
@@ -101,5 +102,5 @@
 			typedef std::map< std::string, DeclarationWithType *> AdapterMap;
 			std::map< std::string, DeclarationWithType *> assignOps;
-			ScopedMap< std::string, DeclarationWithType *> scopedAssignOps;
+			ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps;
 			std::stack< AdapterMap > adapters;
 			DeclarationWithType *retval;
@@ -248,12 +249,12 @@
 		}
 
-		/// returns T if the given declaration is: (*?=?)(T *, T) for some T (return not checked, but maybe should be), NULL otherwise
-		ReferenceToType *isAssignment( DeclarationWithType *decl ) {
+		/// Returns T if the given declaration is (*?=?)(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise
+		TypeInstType *isTypeInstAssignment( DeclarationWithType *decl ) {
 			if ( decl->get_name() == "?=?" ) {
 				if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) {
 					if ( funType->get_parameters().size() == 2 ) {
 						if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) {
-							if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( pointer->get_base() ) ) {
-								if ( ReferenceToType *refType2 = dynamic_cast< ReferenceToType *>( funType->get_parameters().back()->get_type() ) ) {
+							if ( TypeInstType *refType = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) {
+								if ( TypeInstType *refType2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) {
 									if ( refType->get_name() == refType2->get_name() ) {
 										return refType;
@@ -267,4 +268,30 @@
 			return 0;
 		}
+		
+		/// returns T if the given declaration is: (*?=?)(T *, T) for some type T (return not checked, but maybe should be), NULL otherwise
+		/// Only picks assignments where neither parameter is cv-qualified
+		Type *isAssignment( DeclarationWithType *decl ) {
+			if ( decl->get_name() == "?=?" ) {
+				if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) {
+					if ( funType->get_parameters().size() == 2 ) {
+						Type::Qualifiers defaultQualifiers;
+						Type *paramType1 = funType->get_parameters().front()->get_type();
+						if ( paramType1->get_qualifiers() != defaultQualifiers ) return 0;
+						Type *paramType2 = funType->get_parameters().back()->get_type();
+						if ( paramType2->get_qualifiers() != defaultQualifiers ) return 0;
+						
+						if ( PointerType *pointerType = dynamic_cast< PointerType* >( paramType1 ) ) {
+							Type *baseType1 = pointerType->get_base();
+							if ( baseType1->get_qualifiers() != defaultQualifiers ) return 0;
+							SymTab::Indexer dummy;
+							if ( ResolvExpr::typesCompatible( baseType1, paramType2, dummy ) ) {
+								return baseType1;
+							} // if
+						} // if
+					} // if
+				} // if
+			} // if
+			return 0;
+		}
 
 		void Pass1::findAssignOps( const std::list< TypeDecl *> &forall ) {
@@ -274,5 +301,5 @@
 				for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) {
 					std::string typeName;
-					if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( isAssignment( *assert ) ) ) {
+					if ( TypeInstType *typeInst = isTypeInstAssignment( *assert ) ) {
 						assignOps[ typeInst->get_name() ] = *assert;
 					} // if
@@ -283,7 +310,7 @@
 		DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
 			// if this is a polymorphic assignment function, put it in the map for this scope
-			if ( ReferenceToType *refType = isAssignment( functionDecl ) ) {
-				if ( ! dynamic_cast< TypeInstType* >( refType ) ) {
-					scopedAssignOps.insert( refType->get_name(), functionDecl );
+			if ( Type *assignedType = isAssignment( functionDecl ) ) {
+				if ( ! dynamic_cast< TypeInstType* >( assignedType ) ) {
+					scopedAssignOps.insert( assignedType, functionDecl );
 				}
 			}
@@ -934,7 +961,7 @@
 			TyVarMap exprTyVars;
 			makeTyVarMap( function, exprTyVars );
-			ReferenceToType *polyRetType = 0;
-
-			if ( polyRetType = isPolyRet( function ) ) {
+			ReferenceToType *polyRetType = isPolyRet( function );
+
+			if ( polyRetType ) {
 				ret = addPolyRetParam( appExpr, function, polyRetType, arg );
 			} else if ( needsAdapter( function, scopeTyVars ) ) {
@@ -1042,11 +1069,10 @@
 				} else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) {
 					// find assignment operator for generic type
-					ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = scopedAssignOps.find( refType->get_name() );
-					if ( assignIter == scopedAssignOps.end() ) {
+					DeclarationWithType *functionDecl = scopedAssignOps.find( refType );
+					if ( ! functionDecl ) {
 						throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() );
 					}
 
 					// wrap it up in an application expression
-					DeclarationWithType *functionDecl = assignIter->second;
 					assignExpr = new ApplicationExpr( wrapFunctionDecl( functionDecl ) );
 					assignExpr->set_env( env->clone() );
@@ -1063,5 +1089,5 @@
 						assert( ! asserts.empty() && "Type param needs assignment operator assertion" );
 						DeclarationWithType *actualDecl = asserts.front();
-						ReferenceToType *actualType = isAssignment( actualDecl );
+						TypeInstType *actualType = isTypeInstAssignment( actualDecl );
 						assert( actualType && "First assertion of type with assertions should be assignment operator" );
 						TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt );
@@ -1077,12 +1103,10 @@
 							}
 							assertAssign = assertAssignIt->second;
-							//assignExpr->get_env()->add( formalTypeInstType->get_name(), actualType );
-						} else if ( ReferenceToType *formalReferenceType = dynamic_cast< ReferenceToType* >( formalType ) )  {
-							ScopedMap< std::string, DeclarationWithType *>::const_iterator assertAssignIt = scopedAssignOps.find( formalReferenceType->get_name() );
-							if ( assertAssignIt == scopedAssignOps.end() ) {
-								throw SemanticError( "No assignment operation found for ", formalReferenceType );
+						} else {
+							assertAssign = scopedAssignOps.find( formalType );
+							if ( ! assertAssign ) {
+								throw SemanticError( "No assignment operation found for ", formalType );
 							}
-							assertAssign = assertAssignIt->second;
-						} else assert( false && "returning polymorphic types with non struct/polymorphic parameters not yet supported" );
+						}
 						
 
