Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision b0b958a2dfe5052fbfc59bc8543a758787df2f9d)
+++ src/GenPoly/Box.cc	(revision cf16f94d500fdd6115ce4918ac612fd7946e7f19)
@@ -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 : Thu Nov 26 17:01:55 2015
-// Update Count     : 191
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Tue Dec 15 15:30:31 2015
+// Update Count     : 215
 //
 
@@ -53,14 +53,14 @@
 		  public:
 			Pass1();
-			virtual Expression *mutate( ApplicationExpr *appExpr );
-			virtual Expression *mutate( AddressExpr *addrExpr );
-			virtual Expression *mutate( UntypedExpr *expr );
-			virtual DeclarationWithType* mutate( FunctionDecl *functionDecl );
-			virtual TypeDecl *mutate( TypeDecl *typeDecl );
-			virtual Expression *mutate( CommaExpr *commaExpr );
-			virtual Expression *mutate( ConditionalExpr *condExpr );
-			virtual Statement *mutate(ReturnStmt *catchStmt);
-			virtual Type *mutate( PointerType *pointerType );
-			virtual Type *mutate( FunctionType *pointerType );
+			virtual Expression * mutate( ApplicationExpr *appExpr );
+			virtual Expression * mutate( AddressExpr *addrExpr );
+			virtual Expression * mutate( UntypedExpr *expr );
+			virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
+			virtual TypeDecl * mutate( TypeDecl *typeDecl );
+			virtual Expression * mutate( CommaExpr *commaExpr );
+			virtual Expression * mutate( ConditionalExpr *condExpr );
+			virtual Statement * mutate( ReturnStmt *returnStmt );
+			virtual Type * mutate( PointerType *pointerType );
+			virtual Type * mutate( FunctionType *functionType );
   
 			virtual void doBeginScope();
@@ -190,6 +190,10 @@
 							if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) {
 								if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) {
-									name = typeInst->get_name();
-									return true;
+									if ( TypeInstType *typeInst2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) {
+										if ( typeInst->get_name() == typeInst2->get_name() ) {
+											name = typeInst->get_name();
+											return true;
+										} // if
+									} // if
 								} // if
 							} // if
@@ -340,23 +344,30 @@
 
 		Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) {
-			if ( useRetval ) {
-				assert( retval );
-				arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) );
-				arg++;
-			} else {
-				ObjectDecl *newObj = makeTemporary( retType->clone() );
-				Expression *paramExpr = new VariableExpr( newObj );
-				if ( ! isPolyType( newObj->get_type(), env, scopeTyVars ) ) {
-					paramExpr = new AddressExpr( paramExpr );
-				} // if
-				arg = appExpr->get_args().insert( arg, paramExpr );
-				arg++;
-///     stmtsToAdd.push_back( new ExprStmt( noLabels, appExpr ) );
-				CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) );
-				commaExpr->set_env( appExpr->get_env() );
-				appExpr->set_env( 0 );
-				return commaExpr;
-			} // if
-			return appExpr;
+			// ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
+			// if ( useRetval ) {
+			// 	assert( retval );
+			// 	arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) );
+			// 	arg++;
+			// } else {
+
+			// Create temporary to hold return value of polymorphic function and produce that temporary as a result
+			// using a comma expression.  Possibly change comma expression into statement expression "{}" for multiple
+			// return values.
+			ObjectDecl *newObj = makeTemporary( retType->clone() );
+			Expression *paramExpr = new VariableExpr( newObj );
+			// If the type of the temporary is not polymorphic, box temporary by taking its address; otherwise the
+			// temporary is already boxed and can be used directly.
+			if ( ! isPolyType( newObj->get_type(), env, scopeTyVars ) ) {
+				paramExpr = new AddressExpr( paramExpr );
+			} // if
+			arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call
+			arg++;
+			// Build a comma expression to call the function and emulate a normal return.
+			CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) );
+			commaExpr->set_env( appExpr->get_env() );
+			appExpr->set_env( 0 );
+			return commaExpr;
+			// } // if
+			// return appExpr;
 		}
 
@@ -413,6 +424,6 @@
 			Type *newType = formal->clone();
 			std::list< FunctionType *> functions;
-			// instead of functions needing adapters, this really ought to look for
-			// any function mentioning a polymorphic type
+			// instead of functions needing adapters, this really ought to look for any function mentioning a
+			// polymorphic type
 			findAndReplaceFunction( newType, functions, tyVars, needsAdapter );
 			if ( ! functions.empty() ) {
@@ -852,6 +863,24 @@
 		Expression *Pass1::mutate( AddressExpr *addrExpr ) {
 			assert( ! addrExpr->get_arg()->get_results().empty() );
+
+			bool needs = false;
+			if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) {
+				if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), env, scopeTyVars ) ) {
+					if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
+						if ( name->get_name() == "*?" ) {
+							if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) {
+								assert( ! appExpr->get_function()->get_results().empty() );
+								PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() );
+								assert( pointer );
+								FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() );
+								assert( function );
+								needs = needsAdapter( function, scopeTyVars );
+							} // if
+						} // if
+					} // if
+				} // if
+			} // if
 			addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
-			if ( isPolyType( addrExpr->get_arg()->get_results().front(), env, scopeTyVars ) ) {
+			if ( isPolyType( addrExpr->get_arg()->get_results().front(), env, scopeTyVars ) || needs ) {
 				Expression *ret = addrExpr->get_arg();
 				delete ret->get_results().front();
@@ -865,39 +894,40 @@
 		}
 
-		Statement * Pass1::mutate(ReturnStmt *retStmt) {
-			// a cast expr on a polymorphic return value is either redundant or invalid
-			while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( retStmt->get_expr() ) ) {
-				retStmt->set_expr( castExpr->get_arg() );
-				retStmt->get_expr()->set_env( castExpr->get_env() );
-				castExpr->set_env( 0 );
-				castExpr->set_arg( 0 );
-				delete castExpr;
-			}
-			if ( retval && retStmt->get_expr() ) {
-				assert( ! retStmt->get_expr()->get_results().empty() );
-				if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) {
-///       retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
-					TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() );
-					assert( typeInst );
-					std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
-					if ( assignIter == assignOps.end() ) {
-						throw SemanticError( "Attempt to return dtype or ftype object in ", retStmt->get_expr() );
-					} // if
-					ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) );
-					Expression *retParm = new NameExpr( retval->get_name() );
-					retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) );
-					assignExpr->get_args().push_back( retParm );
-					assignExpr->get_args().push_back( retStmt->get_expr() );
-					stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) );
-				} else {
-					useRetval = true;
-					stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( retStmt->get_expr() ) ) );
-					useRetval = false;
-				} // if
-				retStmt->set_expr( 0 );
+		Statement * Pass1::mutate( ReturnStmt *returnStmt ) {
+			if ( retval && returnStmt->get_expr() ) {
+				assert( ! returnStmt->get_expr()->get_results().empty() );
+				// ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
+				// if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) {
+				// a cast expr on a polymorphic return value is either redundant or invalid
+				while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) {
+					returnStmt->set_expr( castExpr->get_arg() );
+					returnStmt->get_expr()->set_env( castExpr->get_env() );
+					castExpr->set_env( 0 );
+					castExpr->set_arg( 0 );
+					delete castExpr;
+				} // while
+				TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() );
+				assert( typeInst );
+				std::map< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
+				if ( assignIter == assignOps.end() ) {
+					throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() );
+				} // if
+				ApplicationExpr *assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) );
+				Expression *retParm = new NameExpr( retval->get_name() );
+				retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) );
+				assignExpr->get_args().push_back( retParm );
+				assignExpr->get_args().push_back( returnStmt->get_expr() );
+				stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) );
+				// } else {
+				// 	std::cerr << "THOMAS " << std::endl;
+				// 	useRetval = true;
+				// 	stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) );
+				// 	useRetval = false;
+				// } // if
+				returnStmt->set_expr( 0 );
 			} else {
-				retStmt->set_expr( mutateExpression( retStmt->get_expr() ) );
-			} // if
-			return retStmt;
+				returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) );
+			} // if
+			return returnStmt;
 		}
 
@@ -1097,6 +1127,5 @@
 			if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
 				if ( isPolyVal( objectDecl->get_type(), scopeTyVars ) ) {
-					// change initialization of a polymorphic value object
-					// to allocate storage with alloca
+					// change initialization of a polymorphic value object to allocate storage with alloca
 					TypeInstType *typeInst = dynamic_cast< TypeInstType *>( objectDecl->get_type() );
 					assert( typeInst );
Index: src/GenPoly/GenPoly.cc
===================================================================
--- src/GenPoly/GenPoly.cc	(revision b0b958a2dfe5052fbfc59bc8543a758787df2f9d)
+++ src/GenPoly/GenPoly.cc	(revision cf16f94d500fdd6115ce4918ac612fd7946e7f19)
@@ -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 Nov 24 15:23:08 2015
-// Update Count     : 11
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Tue Dec  1 15:18:54 2015
+// Update Count     : 12
 //
 
@@ -21,6 +21,5 @@
 
 namespace GenPoly {
-	// A function needs an adapter if it returns a polymorphic value or if any of its
-	// parameters have polymorphic type
+	// 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 ) ) {
Index: src/GenPoly/Lvalue.cc
===================================================================
--- src/GenPoly/Lvalue.cc	(revision b0b958a2dfe5052fbfc59bc8543a758787df2f9d)
+++ src/GenPoly/Lvalue.cc	(revision cf16f94d500fdd6115ce4918ac612fd7946e7f19)
@@ -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:41:33 2015
-// Update Count     : 1
+// Last Modified On : Tue Dec 15 15:33:13 2015
+// Update Count     : 3
 //
 
@@ -120,12 +120,15 @@
 			if ( retval && retStmt->get_expr() ) {
 				assert( ! retStmt->get_expr()->get_results().empty() );
-				while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {
-					retStmt->set_expr( castExpr->get_arg() );
-					retStmt->get_expr()->set_env( castExpr->get_env() );
-					castExpr->set_env( 0 );
-					castExpr->set_arg( 0 );
-					delete castExpr;
-				} // while
 				if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) {
+					// ***** Code Removal ***** because casts may be stripped already
+
+					// strip casts because not allowed to take address of cast
+					// while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {
+					// 	retStmt->set_expr( castExpr->get_arg() );
+					// 	retStmt->get_expr()->set_env( castExpr->get_env() );
+					// 	castExpr->set_env( 0 );
+					// 	castExpr->set_arg( 0 );
+					// 	delete castExpr;
+					// } // while
 					retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) );
 				} else {
