Index: src/GenPoly/Specialize.cc
===================================================================
--- src/GenPoly/Specialize.cc	(revision 7f9968adc291989fb495461dc2b178248a560216)
+++ src/GenPoly/Specialize.cc	(revision 74cfafb235e29a75b88ba08ecc8decc452cdb6f7)
@@ -9,7 +9,7 @@
 // Author           : Richard C. Bilson
 // Created On       : Mon May 18 07:44:20 2015
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Fri Dec 13 23:40:49 2019
-// Update Count     : 32
+// Last Modified By : Andrew Beach
+// Last Modified On : Thr Jul  2 17:42:00 2020
+// Update Count     : 33
 //
 
@@ -42,5 +42,6 @@
 
 namespace GenPoly {
-	struct Specialize final : public WithConstTypeSubstitution, public WithStmtsToAdd, public WithVisitorRef<Specialize> {
+	struct Specialize final : public WithConstTypeSubstitution,
+			public WithDeclsToAdd, public WithVisitorRef<Specialize> {
 		Expression * postmutate( ApplicationExpr *applicationExpr );
 		Expression * postmutate( CastExpr *castExpr );
@@ -248,16 +249,20 @@
 		} // if
 
-		// handle any specializations that may still be present
-		std::string oldParamPrefix = paramPrefix;
-		paramPrefix += "p";
-		// save stmtsToAddBefore in oldStmts
-		std::list< Statement* > oldStmts;
-		oldStmts.splice( oldStmts.end(), stmtsToAddBefore );
-		appExpr->acceptMutator( *visitor );
-		paramPrefix = oldParamPrefix;
-		// write any statements added for recursive specializations into the thunk body
-		thunkFunc->statements->kids.splice( thunkFunc->statements->kids.end(), stmtsToAddBefore );
-		// restore oldStmts into stmtsToAddBefore
-		stmtsToAddBefore.splice( stmtsToAddBefore.end(), oldStmts );
+		// Handle any specializations that may still be present.
+		{
+			std::string oldParamPrefix = paramPrefix;
+			paramPrefix += "p";
+			std::list< Declaration * > oldDecls;
+			oldDecls.splice( oldDecls.end(), declsToAddBefore );
+
+			appExpr->acceptMutator( *visitor );
+			// Write recursive specializations into the thunk body.
+			for ( Declaration * decl : declsToAddBefore ) {
+				thunkFunc->statements->kids.push_back( new DeclStmt( decl ) );
+			}
+
+			declsToAddBefore = std::move( oldDecls );
+			paramPrefix = oldParamPrefix;
+		}
 
 		// add return (or valueless expression) to the thunk
@@ -270,6 +275,6 @@
 		thunkFunc->statements->kids.push_back( appStmt );
 
-		// add thunk definition to queue of statements to add
-		stmtsToAddBefore.push_back( new DeclStmt( thunkFunc ) );
+		// Add the thunk definition (converted to DeclStmt if appproprate).
+		declsToAddBefore.push_back( thunkFunc );
 		// return address of thunk function as replacement expression
 		return new AddressExpr( new VariableExpr( thunkFunc ) );
