Index: src/ControlStruct/ExceptTranslateNew.cpp
===================================================================
--- src/ControlStruct/ExceptTranslateNew.cpp	(revision 891f7074e4bf1a3467ca38add7c71ff492fc38a0)
+++ src/ControlStruct/ExceptTranslateNew.cpp	(revision 33b7d490d37c1f84e96a85cdf1c8baad2b08aeb8)
@@ -9,7 +9,7 @@
 // Author           : Andrew Beach
 // Created On       : Mon Nov  8 11:53:00 2021
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Mon Jan 31 18:49:58 2022
-// Update Count     : 1
+// Last Modified By : Andrew Beach
+// Last Modified On : Fri Mar 11 17:51:00 2022
+// Update Count     : 2
 //
 
@@ -27,17 +27,4 @@
 
 	typedef std::list<ast::CatchStmt*> CatchList;
-
-	void split( CatchList& allHandlers, CatchList& terHandlers,
-				CatchList& resHandlers ) {
-		while ( !allHandlers.empty() ) {
-			ast::CatchStmt * stmt = allHandlers.front();
-			allHandlers.pop_front();
-			if (stmt->kind == ast::ExceptionKind::Terminate) {
-				terHandlers.push_back(stmt);
-			} else {
-				resHandlers.push_back(stmt);
-			}
-		}
-	}
 
 	void appendDeclStmt( ast::CompoundStmt * block, ast::DeclWithType * item ) {
@@ -171,21 +158,20 @@
 	ast::Stmt * create_resume_rethrow( const ast::ThrowStmt * throwStmt );
 
-	// Types used in translation, make sure to use clone.
+	// Types used in translation, first group are internal.
+	ast::ObjectDecl * make_index_object( CodeLocation const & ) const;
+	ast::ObjectDecl * make_exception_object( CodeLocation const & ) const;
+	ast::ObjectDecl * make_bool_object( CodeLocation const & ) const;
+	ast::ObjectDecl * make_voidptr_object( CodeLocation const & ) const;
+	ast::ObjectDecl * make_unused_index_object( CodeLocation const & ) const;
 	// void (*function)();
-	ast::FunctionDecl * try_func_t;
+	ast::FunctionDecl * make_try_function( CodeLocation const & ) const;
 	// void (*function)(int, exception);
-	ast::FunctionDecl * catch_func_t;
+	ast::FunctionDecl * make_catch_function( CodeLocation const & ) const;
 	// int (*function)(exception);
-	ast::FunctionDecl * match_func_t;
+	ast::FunctionDecl * make_match_function( CodeLocation const & ) const;
 	// bool (*function)(exception);
-	ast::FunctionDecl * handle_func_t;
+	ast::FunctionDecl * make_handle_function( CodeLocation const & ) const;
 	// void (*function)(__attribute__((unused)) void *);
-	ast::FunctionDecl * finally_func_t;
-
-	ast::StructInstType * create_except_type() {
-		assert( except_decl );
-		return new ast::StructInstType( except_decl );
-	}
-	void init_func_types();
+	ast::FunctionDecl * make_finally_function( CodeLocation const & ) const;
 
 public:
@@ -199,22 +185,35 @@
 };
 
-void TryMutatorCore::init_func_types() {
+ast::ObjectDecl * TryMutatorCore::make_index_object(
+		CodeLocation const & location ) const {
+	return new ast::ObjectDecl(
+		location,
+		"__handler_index",
+		new ast::BasicType(ast::BasicType::SignedInt),
+		nullptr, //init
+		ast::Storage::Classes{},
+		ast::Linkage::Cforall
+		);
+}
+
+ast::ObjectDecl * TryMutatorCore::make_exception_object(
+		CodeLocation const & location ) const {
 	assert( except_decl );
-
-	ast::ObjectDecl index_obj(
-		{},
-		"__handler_index",
-		new ast::BasicType(ast::BasicType::SignedInt)
-		);
-	ast::ObjectDecl exception_obj(
-		{},
+	return new ast::ObjectDecl(
+		location,
 		"__exception_inst",
 		new ast::PointerType(
 			new ast::StructInstType( except_decl )
 			),
-		NULL
-		);
-	ast::ObjectDecl bool_obj(
-		{},
+		nullptr, //init
+		ast::Storage::Classes{},
+		ast::Linkage::Cforall
+		);
+}
+
+ast::ObjectDecl * TryMutatorCore::make_bool_object(
+		CodeLocation const & location ) const {
+	return new ast::ObjectDecl(
+		location,
 		"__ret_bool",
 		new ast::BasicType( ast::BasicType::Bool ),
@@ -225,6 +224,10 @@
 		std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
 		);
-	ast::ObjectDecl voidptr_obj(
-		{},
+}
+
+ast::ObjectDecl * TryMutatorCore::make_voidptr_object(
+		CodeLocation const & location ) const {
+	return new ast::ObjectDecl(
+		location,
 		"__hook",
 		new ast::PointerType(
@@ -237,7 +240,10 @@
 		std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
 		);
-
-	ast::ObjectDecl unused_index_obj(
-		{},
+}
+
+ast::ObjectDecl * TryMutatorCore::make_unused_index_object(
+		CodeLocation const & location ) const {
+	return new ast::ObjectDecl(
+		location,
 		"__handler_index",
 		new ast::BasicType(ast::BasicType::SignedInt),
@@ -248,8 +254,10 @@
 		std::vector<ast::ptr<ast::Attribute>>{ new ast::Attribute( "unused" ) }
 	);
-	//unused_index_obj->attributes.push_back( new Attribute( "unused" ) );
-
-	try_func_t = new ast::FunctionDecl(
-		{},
+}
+
+ast::FunctionDecl * TryMutatorCore::make_try_function(
+		CodeLocation const & location ) const {
+	return new ast::FunctionDecl(
+		location,
 		"try",
 		{}, //forall
@@ -260,10 +268,13 @@
 		ast::Linkage::Cforall
 	);
-
-	catch_func_t = new ast::FunctionDecl(
-		{},
+}
+
+ast::FunctionDecl * TryMutatorCore::make_catch_function(
+		CodeLocation const & location ) const {
+	return new ast::FunctionDecl(
+		location,
 		"catch",
 		{}, //forall
-		{ast::deepCopy(&index_obj), ast::deepCopy(&exception_obj)},//param
+		{ make_index_object( location ), make_exception_object( location ) },
 		{}, //return void
 		nullptr,
@@ -271,32 +282,41 @@
 		ast::Linkage::Cforall
 	);
-
-	match_func_t = new ast::FunctionDecl(
-		{},
+}
+
+ast::FunctionDecl * TryMutatorCore::make_match_function(
+		CodeLocation const & location ) const {
+	return new ast::FunctionDecl(
+		location,
 		"match",
 		{}, //forall
-		{ast::deepCopy(&exception_obj)},
-		{ast::deepCopy(&unused_index_obj)},
+		{ make_exception_object( location ) },
+		{ make_unused_index_object( location ) },
 		nullptr,
 		ast::Storage::Classes{},
 		ast::Linkage::Cforall
 	);
-
-	handle_func_t = new ast::FunctionDecl(
-		{},
+}
+
+ast::FunctionDecl * TryMutatorCore::make_handle_function(
+		CodeLocation const & location ) const {
+	return new ast::FunctionDecl(
+		location,
 		"handle",
 		{}, //forall
-		{ast::deepCopy(&exception_obj)},
-		{ast::deepCopy(&bool_obj)},
+		{ make_exception_object( location ) },
+		{ make_bool_object( location ) },
 		nullptr,
 		ast::Storage::Classes{},
 		ast::Linkage::Cforall
 	);
-
-	finally_func_t = new ast::FunctionDecl(
-		{},
+}
+
+ast::FunctionDecl * TryMutatorCore::make_finally_function(
+		CodeLocation const & location ) const {
+	return new ast::FunctionDecl(
+		location,
 		"finally",
 		{}, //forall
-		{ast::deepCopy(&voidptr_obj)},
+		{ make_voidptr_object( location ) },
 		{}, //return void
 		nullptr,
@@ -304,28 +324,12 @@
 		ast::Linkage::Cforall
 	);
-
-	//catch_func_t.get_parameters().push_back( index_obj.clone() );
-	//catch_func_t.get_parameters().push_back( exception_obj.clone() );
-	//match_func_t.get_returnVals().push_back( unused_index_obj );
-	//match_func_t.get_parameters().push_back( exception_obj.clone() );
-	//handle_func_t.get_returnVals().push_back( bool_obj.clone() );
-	//handle_func_t.get_parameters().push_back( exception_obj.clone() );
-	//finally_func_t.get_parameters().push_back( voidptr_obj.clone() );
 }
 
 // TryStmt Mutation Helpers
-
-/*
-ast::CompoundStmt * TryMutatorCore::take_try_block( ast::TryStmt *tryStmt ) {
-	ast::CompoundStmt * block = tryStmt->body;
-	tryStmt->body = nullptr;
-	return block;
-}
-*/
 
 ast::FunctionDecl * TryMutatorCore::create_try_wrapper(
 		const ast::CompoundStmt *body ) {
 
-	ast::FunctionDecl * ret = ast::deepCopy(try_func_t);
+	ast::FunctionDecl * ret = make_try_function( body->location );
 	ret->stmts = body;
 	return ret;
@@ -339,5 +343,5 @@
 	const CodeLocation loc = handlers.front()->location;
 
-	ast::FunctionDecl * func_t = ast::deepCopy(catch_func_t);
+	ast::FunctionDecl * func_t = make_catch_function( loc );
 	const ast::DeclWithType * index_obj = func_t->params.front();
 	const ast::DeclWithType * except_obj = func_t->params.back();
@@ -386,5 +390,5 @@
 		// handler->body = nullptr;
 
-		handler_wrappers.push_back( new ast::CaseStmt(loc, 
+		handler_wrappers.push_back( new ast::CaseStmt(loc,
 			ast::ConstantExpr::from_int(loc, index) ,
 			{ block, new ast::ReturnStmt( loc, nullptr ) }
@@ -393,18 +397,9 @@
 	// TODO: Some sort of meaningful error on default perhaps?
 
-	/*
-	std::list<Statement*> stmt_handlers;
-	while ( !handler_wrappers.empty() ) {
-		stmt_handlers.push_back( handler_wrappers.front() );
-		handler_wrappers.pop_front();
-	}
-	*/
-
-	ast::SwitchStmt * handler_lookup = new ast::SwitchStmt(loc, 
+	ast::SwitchStmt * handler_lookup = new ast::SwitchStmt( loc,
 		new ast::VariableExpr( loc, index_obj ),
 		std::move(handler_wrappers)
 		);
-	ast::CompoundStmt * body = new ast::CompoundStmt(loc, 
-		{handler_lookup});
+	ast::CompoundStmt * body = new ast::CompoundStmt( loc, {handler_lookup} );
 
 	func_t->stmts = body;
@@ -433,5 +428,5 @@
 
 	// Check for type match.
-	ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc, 
+	ast::VirtualCastExpr * vcex = new ast::VirtualCastExpr(loc,
 		new ast::VariableExpr(loc, except_obj ),
 		local_except->get_type()
@@ -445,12 +440,7 @@
 	}
 	// Construct the match condition.
-	block->push_back( new ast::IfStmt(loc, 
+	block->push_back( new ast::IfStmt(loc,
 		cond, modded_handler->body, nullptr ) );
 
-	// xxx - how does this work in new ast
-	//modded_handler->set_decl( nullptr );
-	//modded_handler->set_cond( nullptr );
-	//modded_handler->set_body( nullptr );
-	//delete modded_handler;
 	return block;
 }
@@ -467,5 +457,5 @@
 	ast::CompoundStmt * body = new ast::CompoundStmt(loc);
 
-	ast::FunctionDecl * func_t = ast::deepCopy(match_func_t);
+	ast::FunctionDecl * func_t = make_match_function( loc );
 	const ast::DeclWithType * except_obj = func_t->params.back();
 
@@ -490,5 +480,5 @@
 	}
 
-	body->push_back( new ast::ReturnStmt(loc, 
+	body->push_back( new ast::ReturnStmt(loc,
 		ast::ConstantExpr::from_int( loc, 0 ) ));
 
@@ -525,5 +515,5 @@
 	ast::CompoundStmt * body = new ast::CompoundStmt(loc);
 
-	ast::FunctionDecl * func_t = ast::deepCopy(handle_func_t);
+	ast::FunctionDecl * func_t = make_handle_function( loc );
 	const ast::DeclWithType * except_obj = func_t->params.back();
 
@@ -535,6 +525,6 @@
 		ast::CompoundStmt * handling_code;
 		if (handler->body.as<ast::CompoundStmt>()) {
-			handling_code = 
-			strict_dynamic_cast<ast::CompoundStmt*>( handler->body.get_and_mutate() );
+			handling_code = strict_dynamic_cast<ast::CompoundStmt*>(
+				handler->body.get_and_mutate() );
 		} else {
 			handling_code = new ast::CompoundStmt(loc);
@@ -600,11 +590,8 @@
 	const ast::CompoundStmt * body = finally->body;
 
-	ast::FunctionDecl * func_t = ast::deepCopy(finally_func_t);
+	ast::FunctionDecl * func_t = make_finally_function( tryStmt->location );
 	func_t->stmts = body;
 
-	// finally->set_block( nullptr );
-	// delete finally;
 	tryStmt->finally = nullptr;
-
 
 	return func_t;
@@ -617,14 +604,4 @@
 
 	const CodeLocation loc = finally_wrapper->location;
-	// Make Cleanup Attribute.
-	/*
-	std::list< ast::Attribute * > attributes;
-	{
-		std::list<  > attr_params;
-		attr_params.push_back( nameOf( finally_wrapper ) );
-		attributes.push_back( new Attribute( "cleanup", attr_params ) );
-	}
-	*/
-
 	return new ast::ObjectDecl(
 		loc,
@@ -644,9 +621,8 @@
 	// return false;
 	const CodeLocation loc = throwStmt->location;
-	ast::Stmt * result = new ast::ReturnStmt(loc, 
+	ast::Stmt * result = new ast::ReturnStmt(loc,
 		ast::ConstantExpr::from_bool( loc, false )
 		);
 	result->labels = throwStmt->labels;
-	// delete throwStmt; done by postvisit
 	return result;
 }
@@ -660,5 +636,4 @@
 		assert( nullptr == except_decl );
 		except_decl = structDecl;
-		init_func_types();
 	} else if ( structDecl->name == "__cfaehm_try_resume_node" ) {
 		assert( nullptr == node_decl );
@@ -706,6 +681,4 @@
 		}
 	}
-	// split( mutStmt->handlers,
-	//		termination_handlers, resumption_handlers );
 
 	if ( resumption_handlers.size() ) {
