Index: src/ControlStruct/ExceptTranslate.cc
===================================================================
--- src/ControlStruct/ExceptTranslate.cc	(revision 03eedd50b3945625e0567aa281f4866f35435a82)
+++ src/ControlStruct/ExceptTranslate.cc	(revision 63be52cda3344298e672cb759dd9344817a325ee)
@@ -10,6 +10,6 @@
 // Created On       : Wed Jun 14 16:49:00 2017
 // Last Modified By : Andrew Beach
-// Last Modified On : Fri Jul 28 15:34:00 2017
-// Update Count     : 6
+// Last Modified On : Wed Aug 02 12:09:00 2017
+// Update Count     : 7
 //
 
@@ -25,20 +25,106 @@
 namespace ControlStruct {
 
-	// void (*function)();
-	static FunctionType try_func_t(noQualifiers, false);
-	// void (*function)(int, exception);
-	static FunctionType catch_func_t(noQualifiers, false);
-	// int (*function)(exception);
-	static FunctionType match_func_t(noQualifiers, false);
-	// bool (*function)(exception);
-	static FunctionType handle_func_t(noQualifiers, false);
-	// void (*function)(__attribute__((unused)) void *);
-	static FunctionType finally_func_t(noQualifiers, false);
-
-	static void init_func_types() {
-		static bool init_complete = false;
-		if (init_complete) {
-			return;
-		}
+	// Buricratic Helpers (Not having to do with the paritular operation.)
+
+	typedef std::list<CatchStmt*> CatchList;
+
+	void split( CatchList& allHandlers, CatchList& terHandlers,
+				CatchList& resHandlers ) {
+		while ( !allHandlers.empty() ) {
+			CatchStmt * stmt = allHandlers.front();
+			allHandlers.pop_front();
+			if (CatchStmt::Terminate == stmt->get_kind()) {
+				terHandlers.push_back(stmt);
+			} else {
+				resHandlers.push_back(stmt);
+			}
+		}
+	}
+
+	void appendDeclStmt( CompoundStmt * block, Declaration * item ) {
+		block->push_back(new DeclStmt(noLabels, item));
+	}
+
+	Expression * nameOf( DeclarationWithType * decl ) {
+		return new VariableExpr( decl );
+	}
+
+	class ExceptionMutatorCore : public WithGuards {
+		enum Context { NoHandler, TerHandler, ResHandler };
+
+		// Also need to handle goto, break & continue.
+		// They need to be cut off in a ResHandler, until we enter another
+		// loop, switch or the goto stays within the function.
+
+		Context cur_context;
+
+		// The current (innermost) termination handler exception declaration.
+		ObjectDecl * handler_except_decl;
+
+		// The built in types used in translation.
+		StructDecl * except_decl;
+		StructDecl * node_decl;
+		StructDecl * hook_decl;
+
+		// The many helper functions for code/syntree generation.
+		Statement * create_given_throw(
+			const char * throwFunc, ThrowStmt * throwStmt );
+		Statement * create_terminate_throw( ThrowStmt * throwStmt );
+		Statement * create_terminate_rethrow( ThrowStmt * throwStmt );
+		Statement * create_resume_throw( ThrowStmt * throwStmt );
+		Statement * create_resume_rethrow( ThrowStmt * throwStmt );
+		CompoundStmt * take_try_block( TryStmt * tryStmt );
+		FunctionDecl * create_try_wrapper( CompoundStmt * body );
+		FunctionDecl * create_terminate_catch( CatchList &handlers );
+		CompoundStmt * create_single_matcher(
+			DeclarationWithType * except_obj, CatchStmt * modded_handler );
+		FunctionDecl * create_terminate_match( CatchList &handlers );
+		CompoundStmt * create_terminate_caller( FunctionDecl * try_wrapper,
+			FunctionDecl * terminate_catch, FunctionDecl * terminate_match );
+		FunctionDecl * create_resume_handler( CatchList &handlers );
+		CompoundStmt * create_resume_wrapper(
+			Statement * wraps, FunctionDecl * resume_handler );
+		FunctionDecl * create_finally_wrapper( TryStmt * tryStmt );
+		ObjectDecl * create_finally_hook( FunctionDecl * finally_wrapper );
+
+		// Types used in translation, make sure to use clone.
+		// void (*function)();
+		FunctionType try_func_t;
+		// void (*function)(int, exception);
+		FunctionType catch_func_t;
+		// int (*function)(exception);
+		FunctionType match_func_t;
+		// bool (*function)(exception);
+		FunctionType handle_func_t;
+		// void (*function)(__attribute__((unused)) void *);
+		FunctionType finally_func_t;
+
+		StructInstType * create_except_type() {
+			assert( except_decl );
+			return new StructInstType( noQualifiers, except_decl );
+		}
+		void init_func_types();
+
+	public:
+		ExceptionMutatorCore() :
+			cur_context( NoHandler ),
+			handler_except_decl( nullptr ),
+			except_decl( nullptr ), node_decl( nullptr ), hook_decl( nullptr ),
+			try_func_t( noQualifiers, false ),
+			catch_func_t( noQualifiers, false ),
+			match_func_t( noQualifiers, false ),
+			handle_func_t( noQualifiers, false ),
+			finally_func_t( noQualifiers, false )
+		{
+			init_func_types();
+		}
+
+		void premutate( CatchStmt *catchStmt );
+		void premutate( StructDecl *structDecl );
+		Statement * postmutate( ThrowStmt *throwStmt );
+		Statement * postmutate( TryStmt *tryStmt );
+	};
+
+	void ExceptionMutatorCore::init_func_types() {
 		ObjectDecl index_obj(
 			"__handler_index",
@@ -56,4 +142,5 @@
 			new PointerType(
 				noQualifiers,
+				//new StructInstType( noQualifiers, except_decl )
 				new BasicType( noQualifiers, BasicType::SignedInt )
 				),
@@ -65,5 +152,5 @@
 			LinkageSpec::Cforall,
 			/*bitfieldWidth*/ NULL,
-			new BasicType(noQualifiers, BasicType::Bool),
+			new BasicType( noQualifiers, BasicType::Bool ),
 			/*init*/ NULL
 			);
@@ -78,5 +165,5 @@
 					noQualifiers
 					),
-				std::list<Attribute *>{new Attribute("unused")}
+				std::list<Attribute *>{ new Attribute( "unused" ) }
 				),
 			NULL
@@ -90,36 +177,9 @@
 		handle_func_t.get_parameters().push_back( exception_obj.clone() );
 		finally_func_t.get_parameters().push_back( voidptr_obj.clone() );
-
-		init_complete = true;
-	}
-
-	// Buricratic Helpers (Not having to do with the paritular operation.)
-
-	typedef std::list<CatchStmt*> CatchList;
-
-	void split( CatchList& allHandlers, CatchList& terHandlers,
-				CatchList& resHandlers ) {
-		while ( !allHandlers.empty() ) {
-			CatchStmt * stmt = allHandlers.front();
-			allHandlers.pop_front();
-			if (CatchStmt::Terminate == stmt->get_kind()) {
-				terHandlers.push_back(stmt);
-			} else {
-				resHandlers.push_back(stmt);
-			}
-		}
-	}
-
-	void appendDeclStmt( CompoundStmt * block, Declaration * item ) {
-		block->push_back(new DeclStmt(noLabels, item));
-	}
-
-	Expression * nameOf( DeclarationWithType * decl ) {
-		return new VariableExpr( decl );
 	}
 
 	// ThrowStmt Mutation Helpers
 
-	Statement * create_given_throw(
+	Statement * ExceptionMutatorCore::create_given_throw(
 			const char * throwFunc, ThrowStmt * throwStmt ) {
 		// There is an extra copy here we might be able to remove with
@@ -144,11 +204,12 @@
 	}
 
-	Statement * create_terminate_throw( ThrowStmt *throwStmt ) {
+	Statement * ExceptionMutatorCore::create_terminate_throw(
+			ThrowStmt *throwStmt ) {
 		// { int NAME = EXPR; __throw_terminate( &NAME ); }
 		return create_given_throw( "__cfaehm__throw_terminate", throwStmt );
 	}
 
-	Statement * create_terminate_rethrow( ThrowStmt *throwStmt,
-			ObjectDecl *handler_except_decl ) {
+	Statement * ExceptionMutatorCore::create_terminate_rethrow(
+			ThrowStmt *throwStmt ) {
 		// { `handler_except_decl` = NULL; __rethrow_terminate(); }
 		assert( nullptr == throwStmt->get_expr() );
@@ -173,10 +234,12 @@
 	}
 
-	Statement * create_resume_throw( ThrowStmt *throwStmt ) {
+	Statement * ExceptionMutatorCore::create_resume_throw(
+			ThrowStmt *throwStmt ) {
 		// __throw_resume( EXPR );
 		return create_given_throw( "__cfaehm__throw_resume", throwStmt );
 	}
 
-	Statement * create_resume_rethrow( ThrowStmt *throwStmt ) {
+	Statement * ExceptionMutatorCore::create_resume_rethrow(
+			ThrowStmt *throwStmt ) {
 		// return false;
 		Statement * result = new ReturnStmt(
@@ -190,10 +253,13 @@
 	// TryStmt Mutation Helpers
 
-	CompoundStmt * take_try_block( TryStmt *tryStmt ) {
+	// XXX: Leave out?
+	CompoundStmt * ExceptionMutatorCore::take_try_block( TryStmt *tryStmt ) {
 		CompoundStmt * block = tryStmt->get_block();
 		tryStmt->set_block( nullptr );
 		return block;
 	}
-	FunctionDecl * create_try_wrapper( CompoundStmt *body ) {
+
+	FunctionDecl * ExceptionMutatorCore::create_try_wrapper(
+			CompoundStmt *body ) {
 
 		return new FunctionDecl( "try", Type::StorageClasses(),
@@ -201,5 +267,6 @@
 	}
 
-	FunctionDecl * create_terminate_catch( CatchList &handlers ) {
+	FunctionDecl * ExceptionMutatorCore::create_terminate_catch(
+			CatchList &handlers ) {
 		std::list<CaseStmt *> handler_wrappers;
 
@@ -297,5 +364,5 @@
 	// Create a single check from a moddified handler.
 	// except_obj is referenced, modded_handler will be freed.
-	CompoundStmt *create_single_matcher(
+	CompoundStmt * ExceptionMutatorCore::create_single_matcher(
 			DeclarationWithType * except_obj, CatchStmt * modded_handler ) {
 		CompoundStmt * block = new CompoundStmt( noLabels );
@@ -362,5 +429,6 @@
 	}
 
-	FunctionDecl * create_terminate_match( CatchList &handlers ) {
+	FunctionDecl * ExceptionMutatorCore::create_terminate_match(
+			CatchList &handlers ) {
 		// int match(exception * except) {
 		//     HANDLER WRAPPERS { return `index`; }
@@ -398,8 +466,8 @@
 	}
 
-	CompoundStmt * create_terminate_caller(
+	CompoundStmt * ExceptionMutatorCore::create_terminate_caller(
 			FunctionDecl * try_wrapper,
 			FunctionDecl * terminate_catch,
-			FunctionDecl * terminate_match) {
+			FunctionDecl * terminate_match ) {
 		// { __cfaehm__try_terminate(`try`, `catch`, `match`); }
 
@@ -416,5 +484,6 @@
 	}
 
-	FunctionDecl * create_resume_handler( CatchList &handlers ) {
+	FunctionDecl * ExceptionMutatorCore::create_resume_handler(
+			CatchList &handlers ) {
 		// bool handle(exception * except) {
 		//     HANDLER WRAPPERS { `hander->body`; return true; }
@@ -452,6 +521,5 @@
 	}
 
-	CompoundStmt * create_resume_wrapper(
-			StructDecl * node_decl,
+	CompoundStmt * ExceptionMutatorCore::create_resume_wrapper(
 			Statement * wraps,
 			FunctionDecl * resume_handler ) {
@@ -497,5 +565,6 @@
 	}
 
-	FunctionDecl * create_finally_wrapper( TryStmt * tryStmt ) {
+	FunctionDecl * ExceptionMutatorCore::create_finally_wrapper(
+			TryStmt * tryStmt ) {
 		// void finally() { <finally code> }
 		FinallyStmt * finally = tryStmt->get_finally();
@@ -509,6 +578,6 @@
 	}
 
-	ObjectDecl * create_finally_hook(
-			StructDecl * hook_decl, FunctionDecl * finally_wrapper ) {
+	ObjectDecl * ExceptionMutatorCore::create_finally_hook(
+			FunctionDecl * finally_wrapper ) {
 		// struct __cfaehm__cleanup_hook __finally_hook
 		//   	__attribute__((cleanup( finally_wrapper )));
@@ -536,41 +605,9 @@
 	}
 
-
-	class ExceptionMutatorCore : public WithGuards {
-		enum Context { NoHandler, TerHandler, ResHandler };
-
-		// Also need to handle goto, break & continue.
-		// They need to be cut off in a ResHandler, until we enter another
-		// loop, switch or the goto stays within the function.
-
-		Context cur_context;
-
-		// The current (innermost) termination handler exception declaration.
-		ObjectDecl * handler_except_decl;
-
-		// We might not need this, but a unique base for each try block's
-		// generated functions might be nice.
-		//std::string curFunctionName;
-		//unsigned int try_count = 0;
-
-		StructDecl *node_decl;
-		StructDecl *hook_decl;
-
-	public:
-		ExceptionMutatorCore() :
-			cur_context(NoHandler),
-			handler_except_decl( nullptr ),
-			node_decl( nullptr ), hook_decl( nullptr )
-		{}
-
-		void premutate( CatchStmt *catchStmt );
-		void premutate( StructDecl *structDecl );
-		Statement * postmutate( ThrowStmt *throwStmt );
-		Statement * postmutate( TryStmt *tryStmt );
-	};
-
+	// Visiting/Mutating Functions
 	void ExceptionMutatorCore::premutate( CatchStmt *catchStmt ) {
 		// Currently, we make up the declaration, as there isn't one for
 		// integers.
+		assert( ! catchStmt->get_decl() );
 		ObjectDecl * tmp = new ObjectDecl(
 			"_hidden_local",
@@ -629,6 +666,5 @@
 				return create_terminate_throw( throwStmt );
 			} else if ( TerHandler == cur_context ) {
-				return create_terminate_rethrow(
-					throwStmt, handler_except_decl );
+				return create_terminate_rethrow( throwStmt );
 			} else {
 				assertf(false, "Invalid throw in %s at %i\n",
@@ -666,6 +702,5 @@
 			appendDeclStmt( block, finally_block );
 			// Create and add the finally cleanup hook.
-			appendDeclStmt( block,
-				create_finally_hook( hook_decl, finally_block ) );
+			appendDeclStmt( block, create_finally_hook( finally_block ) );
 		}
 
@@ -681,5 +716,5 @@
 			appendDeclStmt( block, resume_handler );
 			// Prepare hooks
-			inner = create_resume_wrapper( node_decl, inner, resume_handler );
+			inner = create_resume_wrapper( inner, resume_handler );
 		}
 
@@ -706,6 +741,4 @@
 
 	void translateEHM( std::list< Declaration *> & translationUnit ) {
-		init_func_types();
-
 		PassVisitor<ExceptionMutatorCore> translator;
 		mutateAll( translationUnit, translator );
