Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision 6943f0511582b37034c3046e163992d7c9c2b3df)
+++ src/GenPoly/Box.cc	(revision a5a71d0e95113b304e03502a14ccab969324804f)
@@ -14,15 +14,20 @@
 //
 
+#include <algorithm>
+#include <iterator>
+#include <list>
+#include <map>
 #include <set>
 #include <stack>
 #include <string>
-#include <iterator>
-#include <algorithm>
+#include <utility>
+#include <vector>
 #include <cassert>
 
 #include "Box.h"
-#include "InstantiateGeneric.h"
+#include "DeclMutator.h"
 #include "PolyMutator.h"
 #include "FindFunction.h"
+#include "ScopedMap.h"
 #include "ScrubTyVars.h"
 
@@ -30,9 +35,11 @@
 
 #include "SynTree/Constant.h"
-#include "SynTree/Type.h"
+#include "SynTree/Declaration.h"
 #include "SynTree/Expression.h"
 #include "SynTree/Initializer.h"
+#include "SynTree/Mutator.h"
 #include "SynTree/Statement.h"
-#include "SynTree/Mutator.h"
+#include "SynTree/Type.h"
+#include "SynTree/TypeSubstitution.h"
 
 #include "ResolvExpr/TypeEnvironment.h"
@@ -40,4 +47,5 @@
 #include "ResolvExpr/typeops.h"
 
+#include "SymTab/Indexer.h"
 #include "SymTab/Mangler.h"
 
@@ -54,4 +62,121 @@
 		FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
 
+		/// Key for a unique concrete type; generic base type paired with type parameter list
+		struct ConcreteType {
+			ConcreteType() : base(NULL), params() {}
+
+			ConcreteType(AggregateDecl *_base, const std::list< Type* >& _params) : base(_base), params() { cloneAll(_params, params); }
+
+			ConcreteType(const ConcreteType& that) : base(that.base), params() { cloneAll(that.params, params); }
+
+			/// Extracts types from a list of TypeExpr*
+			ConcreteType(AggregateDecl *_base, const std::list< TypeExpr* >& _params) : base(_base), params() {
+				for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) {
+					params.push_back( (*param)->get_type()->clone() );
+				}
+			}
+
+			ConcreteType& operator= (const ConcreteType& that) {
+				deleteAll( params );
+				params.clear();
+
+				base = that.base;
+				cloneAll( that.params, params );
+
+				return *this;
+			}
+
+			~ConcreteType() { deleteAll( params ); }
+
+			bool operator== (const ConcreteType& that) const {
+				if ( base != that.base ) return false;
+
+				SymTab::Indexer dummy;
+				if ( params.size() != that.params.size() ) return false;
+				for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) {
+					if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false;
+				}
+				return true;
+			}
+
+			AggregateDecl *base;        ///< Base generic type
+			std::list< Type* > params;  ///< Instantiation parameters
+		};
+
+		/// Maps a concrete type to the some value, accounting for scope
+		template< typename Value >
+		class InstantiationMap {
+			/// Information about a specific instantiation of a generic type
+			struct Instantiation {
+				ConcreteType key;  ///< Instantiation parameters for this type
+				Value *value;      ///< Value for this instantiation
+
+				Instantiation() : key(), value(0) {}
+				Instantiation(const ConcreteType &_key, Value *_value) : key(_key), value(_value) {}
+			};
+			/// Map of generic types to instantiations of them
+			typedef std::map< AggregateDecl*, std::vector< Instantiation > > Scope;
+
+			std::vector< Scope > scopes;  ///< list of scopes, from outermost to innermost
+
+		public:
+			/// Starts a new scope
+			void beginScope() {
+				Scope scope;
+				scopes.push_back(scope);
+			}
+
+			/// Ends a scope
+			void endScope() {
+				scopes.pop_back();
+			}
+
+			/// Default constructor initializes with one scope
+			InstantiationMap() { beginScope(); }
+
+//		private:
+			/// Gets the value for the concrete instantiation of this type, assuming it has already been instantiated in the current scope.
+			/// Returns NULL on none such.
+			Value *lookup( AggregateDecl *generic, const std::list< TypeExpr* >& params ) {
+				ConcreteType key(generic, params);
+				// scan scopes from innermost out
+				for ( typename std::vector< Scope >::const_reverse_iterator scope = scopes.rbegin(); scope != scopes.rend(); ++scope ) {
+					// skip scope if no instantiations of this generic type
+					typename Scope::const_iterator insts = scope->find( generic );
+					if ( insts == scope->end() ) continue;
+					// look through instantiations for matches to concrete type
+					for ( typename std::vector< Instantiation >::const_iterator inst = insts->second.begin(); inst != insts->second.end(); ++inst ) {
+						if ( inst->key == key ) return inst->value;
+					}
+				}
+				// no matching instantiation found
+				return 0;
+			}
+		public:
+//			StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)lookup( inst->get_baseStruct(), typeSubs ); }
+//			UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)lookup( inst->get_baseUnion(), typeSubs ); }
+
+//		private:
+			/// Adds a value for a concrete type to the current scope
+			void insert( AggregateDecl *generic, const std::list< TypeExpr* > &params, Value *value ) {
+				ConcreteType key(generic, params);
+				scopes.back()[generic].push_back( Instantiation( key, value ) );
+			}
+//		public:
+//			void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { insert( inst->get_baseStruct(), typeSubs, decl ); }
+//			void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { insert( inst->get_baseUnion(), typeSubs, decl ); }
+		};
+
+		/// Adds layout-generation functions to polymorphic types
+		class LayoutFunctionBuilder : public DeclMutator {
+			unsigned int functionNesting;  // current level of nested functions
+		public:
+			LayoutFunctionBuilder() : functionNesting( 0 ) {}
+
+			virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
+			virtual Declaration *mutate( StructDecl *structDecl );
+			virtual Declaration *mutate( UnionDecl *unionDecl );
+		};
+		
 		/// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call
 		class Pass1 : public PolyMutator {
@@ -100,8 +225,7 @@
 			ObjectDecl *makeTemporary( Type *type );
 
-			typedef std::map< std::string, DeclarationWithType *> AdapterMap;
 			std::map< std::string, DeclarationWithType *> assignOps;
 			ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps;
-			std::stack< AdapterMap > adapters;
+			ScopedMap< std::string, DeclarationWithType* > adapters;
 			DeclarationWithType *retval;
 			bool useRetval;
@@ -124,4 +248,32 @@
 
 			std::map< UniqueId, std::string > adapterName;
+		};
+
+		/// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately
+		class GenericInstantiator : public DeclMutator {
+			/// Map of (generic type, parameter list) pairs to concrete type instantiations
+			InstantiationMap< AggregateDecl > instantiations;
+			/// Namer for concrete types
+			UniqueName typeNamer;
+
+		public:
+			GenericInstantiator() : DeclMutator(), instantiations(), typeNamer("_conc_") {}
+
+			virtual Type* mutate( StructInstType *inst );
+			virtual Type* mutate( UnionInstType *inst );
+
+	// 		virtual Expression* mutate( MemberExpr *memberExpr );
+
+			virtual void doBeginScope();
+			virtual void doEndScope();
+		private:
+			/// Wrap instantiation lookup for structs
+			StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)instantiations.lookup( inst->get_baseStruct(), typeSubs ); }
+			/// Wrap instantiation lookup for unions
+			UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)instantiations.lookup( inst->get_baseUnion(), typeSubs ); }
+			/// Wrap instantiation insertion for structs
+			void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { instantiations.insert( inst->get_baseStruct(), typeSubs, decl ); }
+			/// Wrap instantiation insertion for unions
+			void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { instantiations.insert( inst->get_baseUnion(), typeSubs, decl ); }
 		};
 
@@ -159,13 +311,4 @@
 	} // anonymous namespace
 
-	void printAllNotBuiltin( const std::list< Declaration *>& translationUnit, std::ostream &os ) {
-		for ( std::list< Declaration *>::const_iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
-			if ( ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) {
-				(*i)->print( os );
-				os << std::endl;
-			} // if
-		} // for
-	}
-
 	/// version of mutateAll with special handling for translation unit so you can check the end of the prelude when debugging
 	template< typename MutatorType >
@@ -195,15 +338,218 @@
 
 	void box( std::list< Declaration *>& translationUnit ) {
+		LayoutFunctionBuilder layoutBuilder;
 		Pass1 pass1;
 		Pass2 pass2;
+		GenericInstantiator instantiator;
 		MemberExprFixer memberFixer;
 		Pass3 pass3;
+		
+		layoutBuilder.mutateDeclarationList( translationUnit );
 		mutateTranslationUnit/*All*/( translationUnit, pass1 );
 		mutateTranslationUnit/*All*/( translationUnit, pass2 );
-		instantiateGeneric( translationUnit );
+//		instantiateGeneric( translationUnit );
+		instantiator.mutateDeclarationList( translationUnit );
 		mutateTranslationUnit/*All*/( translationUnit, memberFixer );
 		mutateTranslationUnit/*All*/( translationUnit, pass3 );
 	}
 
+	////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////////
+
+	DeclarationWithType *LayoutFunctionBuilder::mutate( FunctionDecl *functionDecl ) {
+		functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) );
+		mutateAll( functionDecl->get_oldDecls(), *this );
+		++functionNesting;
+		functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
+		--functionNesting;
+		return functionDecl;
+	}
+	
+	/// Get a list of type declarations that will affect a layout function
+	std::list< TypeDecl* > takeOtypeOnly( std::list< TypeDecl* > &decls ) {
+		std::list< TypeDecl * > otypeDecls;
+
+		for ( std::list< TypeDecl* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) {
+			if ( (*decl)->get_kind() == TypeDecl::Any ) {
+				otypeDecls.push_back( *decl );
+			}
+		}
+		
+		return otypeDecls;
+	}
+
+	/// Adds parameters for otype layout to a function type
+	void addOtypeParams( FunctionType *layoutFnType, std::list< TypeDecl* > &otypeParams ) {
+		BasicType sizeAlignType( Type::Qualifiers(), BasicType::LongUnsignedInt );
+		
+		for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) {
+			TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param );
+			layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( &paramType ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
+			layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( &paramType ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
+		}
+	}
+
+	/// Builds a layout function declaration
+	FunctionDecl *buildLayoutFunctionDecl( const std::string &typeName, unsigned int functionNesting, FunctionType *layoutFnType ) {
+		// Routines at global scope marked "static" to prevent multiple definitions is separate translation units
+		// because each unit generates copies of the default routines for each aggregate.
+		FunctionDecl *layoutDecl = new FunctionDecl(
+			"__layoutof_" + typeName, functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );
+		layoutDecl->fixUniqueId();
+		return layoutDecl;
+	}
+
+	/// Makes a unary operation
+	Expression *makeOp( const std::string &name, Expression *arg ) {
+		UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) );
+		expr->get_args().push_back( arg );
+		return expr;
+	}
+
+	/// Makes a binary operation
+	Expression *makeOp( const std::string &name, Expression *lhs, Expression *rhs ) {
+		UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) );
+		expr->get_args().push_back( lhs );
+		expr->get_args().push_back( rhs );
+		return expr;
+	}
+
+	/// Returns the dereference of a local pointer variable
+	Expression *derefVar( ObjectDecl *var ) {
+		return makeOp( "*?", new VariableExpr( var ) );
+	}
+
+	/// makes an if-statement with a single-expression if-block and no then block
+	Statement *makeCond( Expression *cond, Expression *ifPart ) {
+		return new IfStmt( noLabels, cond, new ExprStmt( noLabels, ifPart ), 0 );
+	}
+
+	/// makes a statement that assigns rhs to lhs if lhs < rhs
+	Statement *makeAssignMax( Expression *lhs, Expression *rhs ) {
+		return makeCond( makeOp( "?<?", lhs, rhs ), makeOp( "?=?", lhs->clone(), rhs->clone() ) );
+	}
+
+	/// makes a statement that aligns lhs to rhs (rhs should be an integer power of two)
+	Statement *makeAlignTo( Expression *lhs, Expression *rhs ) {
+		// check that the lhs is zeroed out to the level of rhs
+		Expression *ifCond = makeOp( "?&?", lhs, makeOp( "?-?", rhs, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "1" ) ) ) );
+		// if not aligned, increment to alignment
+		Expression *ifExpr = makeOp( "?+=?", lhs->clone(), makeOp( "?-?", rhs->clone(), ifCond->clone() ) );
+		return makeCond( ifCond, ifExpr );
+	}
+	
+	/// adds an expression to a compound statement
+	void addExpr( CompoundStmt *stmts, Expression *expr ) {
+		stmts->get_kids().push_back( new ExprStmt( noLabels, expr ) );
+	}
+
+	/// adds a statement to a compound statement
+	void addStmt( CompoundStmt *stmts, Statement *stmt ) {
+		stmts->get_kids().push_back( stmt );
+	}
+	
+	Declaration *LayoutFunctionBuilder::mutate( StructDecl *structDecl ) {
+		// do not generate layout function for "empty" tag structs
+		if ( structDecl->get_members().empty() ) return structDecl;
+
+		// get parameters that can change layout, exiting early if none
+		std::list< TypeDecl* > otypeParams = takeOtypeOnly( structDecl->get_parameters() );
+		if ( otypeParams.empty() ) return structDecl;
+
+		// build layout function signature
+		FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false );
+		BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
+		PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType );
+		
+		ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
+		layoutFnType->get_parameters().push_back( sizeParam );
+		ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
+		layoutFnType->get_parameters().push_back( alignParam );
+		ObjectDecl *offsetParam = new ObjectDecl( "__offsetof_" + structDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
+		layoutFnType->get_parameters().push_back( offsetParam );
+		addOtypeParams( layoutFnType, otypeParams );
+
+		// build function decl
+		FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl->get_name(), functionNesting, layoutFnType );
+
+		// calculate struct layout in function body
+
+		// initialize size and alignment to 0 and 1 (will have at least one member to re-edit size
+		addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "0" ) ) ) );
+		addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) );
+		unsigned long n_members = 0;
+		bool firstMember = true;
+		for ( std::list< Declaration* >::const_iterator member = structDecl->get_members().begin(); member != structDecl->get_members().end(); ++member ) {
+			DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member );
+			assert( dwt );
+			Type *memberType = dwt->get_type();
+
+			if ( firstMember ) {
+				firstMember = false;
+			} else {
+				// make sure all members after the first (automatically aligned at 0) are properly padded for alignment
+				addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), new AlignofExpr( memberType->clone() ) ) );
+			}
+			
+			// place current size in the current offset index
+			addExpr( layoutDecl->get_statements(), makeOp( "?=?", makeOp( "?[?]", new VariableExpr( offsetParam ), new ConstantExpr( Constant::from( n_members ) ) ),
+			                                                      derefVar( sizeParam ) ) );
+			++n_members;
+
+			// add member size to current size
+			addExpr( layoutDecl->get_statements(), makeOp( "?+=?", derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) );
+			
+			// take max of member alignment and global alignment
+			addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) );
+		}
+		// make sure the type is end-padded to a multiple of its alignment
+		addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) );
+
+		addDeclarationAfter( layoutDecl );
+		return structDecl;
+	}
+	
+	Declaration *LayoutFunctionBuilder::mutate( UnionDecl *unionDecl ) {
+		// do not generate layout function for "empty" tag unions
+		if ( unionDecl->get_members().empty() ) return unionDecl;
+		
+		// get parameters that can change layout, exiting early if none
+		std::list< TypeDecl* > otypeParams = takeOtypeOnly( unionDecl->get_parameters() );
+		if ( otypeParams.empty() ) return unionDecl;
+
+		// build layout function signature
+		FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false );
+		BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
+		PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType );
+		
+		ObjectDecl *sizeParam = new ObjectDecl( "__sizeof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
+		layoutFnType->get_parameters().push_back( sizeParam );
+		ObjectDecl *alignParam = new ObjectDecl( "__alignof_" + unionDecl->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
+		layoutFnType->get_parameters().push_back( alignParam );
+		addOtypeParams( layoutFnType, otypeParams );
+
+		// build function decl
+		FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl->get_name(), functionNesting, layoutFnType );
+
+		// calculate union layout in function body
+		addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) );
+		addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) );
+		for ( std::list< Declaration* >::const_iterator member = unionDecl->get_members().begin(); member != unionDecl->get_members().end(); ++member ) {
+			DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member );
+			assert( dwt );
+			Type *memberType = dwt->get_type();
+			
+			// take max member size and global size
+			addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) );
+			
+			// take max of member alignment and global alignment
+			addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) );
+		}
+		// make sure the type is end-padded to a multiple of its alignment
+		addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) );
+
+		addDeclarationAfter( layoutDecl );
+		return unionDecl;
+	}
+	
 	////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
 
@@ -245,7 +591,5 @@
 		}
 
-		Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {
-			adapters.push(AdapterMap());
-		}
+		Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {}
 
 		/// Returns T if the given declaration is (*?=?)(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise
@@ -350,5 +694,4 @@
 				} // for
 
-				AdapterMap & adapters = Pass1::adapters.top();
 				for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
 					std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
@@ -773,10 +1116,11 @@
 					mangleName += makePolyMonoSuffix( originalFunction, exprTyVars );
 
-					AdapterMap & adapters = Pass1::adapters.top();
-					AdapterMap::iterator adapter = adapters.find( mangleName );
+					typedef ScopedMap< std::string, DeclarationWithType* >::iterator AdapterIter;
+					AdapterIter adapter = adapters.find( mangleName );
 					if ( adapter == adapters.end() ) {
 						// adapter has not been created yet in the current scope, so define it
 						FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
-						adapter = adapters.insert( adapters.begin(), std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
+						std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
+						adapter = answer.first;
 						stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
 					} // if
@@ -1157,11 +1501,10 @@
 
 		void Pass1::doBeginScope() {
-			// push a copy of the current map
-			adapters.push(adapters.top());
+			adapters.beginScope();
 			scopedAssignOps.beginScope();
 		}
 
 		void Pass1::doEndScope() {
-			adapters.pop();
+			adapters.endScope();
 			scopedAssignOps.endScope();
 		}
@@ -1310,4 +1653,193 @@
 		}
 
+//////////////////////////////////////// GenericInstantiator //////////////////////////////////////////////////
+
+		/// Makes substitutions of params into baseParams; returns true if all parameters substituted for a concrete type
+		bool makeSubstitutions( const std::list< TypeDecl* >& baseParams, const std::list< Expression* >& params, std::list< TypeExpr* >& out ) {
+			bool allConcrete = true;  // will finish the substitution list even if they're not all concrete
+
+			// substitute concrete types for given parameters, and incomplete types for placeholders
+			std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin();
+			std::list< Expression* >::const_iterator param = params.begin();
+			for ( ; baseParam != baseParams.end() && param != params.end(); ++baseParam, ++param ) {
+	// 			switch ( (*baseParam)->get_kind() ) {
+	// 			case TypeDecl::Any: {   // any type is a valid substitution here; complete types can be used to instantiate generics
+					TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
+					assert(paramType && "Aggregate parameters should be type expressions");
+					out.push_back( paramType->clone() );
+					// check that the substituted type isn't a type variable itself
+					if ( dynamic_cast< TypeInstType* >( paramType->get_type() ) ) {
+						allConcrete = false;
+					}
+	// 				break;
+	// 			}
+	// 			case TypeDecl::Dtype:  // dtype can be consistently replaced with void [only pointers, which become void*]
+	// 				out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) );
+	// 				break;
+	// 			case TypeDecl::Ftype:  // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype]
+	// 				out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) );
+	// 				break;
+	// 			}
+			}
+
+			// if any parameters left over, not done
+			if ( baseParam != baseParams.end() ) return false;
+	// 		// if not enough parameters given, substitute remaining incomplete types for placeholders
+	// 		for ( ; baseParam != baseParams.end(); ++baseParam ) {
+	// 			switch ( (*baseParam)->get_kind() ) {
+	// 			case TypeDecl::Any:    // no more substitutions here, fail early
+	// 				return false;
+	// 			case TypeDecl::Dtype:  // dtype can be consistently replaced with void [only pointers, which become void*]
+	// 				out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) );
+	// 				break;
+	// 			case TypeDecl::Ftype:  // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype]
+	// 				out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) );
+	// 				break;
+	// 			}
+	// 		}
+
+			return allConcrete;
+		}
+
+		/// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out
+		void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs,
+								std::list< Declaration* >& out ) {
+			// substitute types into new members
+			TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
+			for ( std::list< Declaration* >::const_iterator member = in.begin(); member != in.end(); ++member ) {
+				Declaration *newMember = (*member)->clone();
+				subs.apply(newMember);
+				out.push_back( newMember );
+			}
+		}
+
+		Type* GenericInstantiator::mutate( StructInstType *inst ) {
+			// mutate subtypes
+			Type *mutated = Mutator::mutate( inst );
+			inst = dynamic_cast< StructInstType* >( mutated );
+			if ( ! inst ) return mutated;
+
+			// exit early if no need for further mutation
+			if ( inst->get_parameters().empty() ) return inst;
+			assert( inst->get_baseParameters() && "Base struct has parameters" );
+
+			// check if type can be concretely instantiated; put substitutions into typeSubs
+			std::list< TypeExpr* > typeSubs;
+			if ( ! makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ) ) {
+				deleteAll( typeSubs );
+				return inst;
+			}
+
+			// make concrete instantiation of generic type
+			StructDecl *concDecl = lookup( inst, typeSubs );
+			if ( ! concDecl ) {
+				// set concDecl to new type, insert type declaration into statements to add
+				concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) );
+				substituteMembers( inst->get_baseStruct()->get_members(), *inst->get_baseParameters(), typeSubs, 	concDecl->get_members() );
+				DeclMutator::addDeclaration( concDecl );
+				insert( inst, typeSubs, concDecl );
+			}
+			StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() );
+			newInst->set_baseStruct( concDecl );
+
+			deleteAll( typeSubs );
+			delete inst;
+			return newInst;
+		}
+
+		Type* GenericInstantiator::mutate( UnionInstType *inst ) {
+			// mutate subtypes
+			Type *mutated = Mutator::mutate( inst );
+			inst = dynamic_cast< UnionInstType* >( mutated );
+			if ( ! inst ) return mutated;
+
+			// exit early if no need for further mutation
+			if ( inst->get_parameters().empty() ) return inst;
+			assert( inst->get_baseParameters() && "Base union has parameters" );
+
+			// check if type can be concretely instantiated; put substitutions into typeSubs
+			std::list< TypeExpr* > typeSubs;
+			if ( ! makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ) ) {
+				deleteAll( typeSubs );
+				return inst;
+			}
+
+			// make concrete instantiation of generic type
+			UnionDecl *concDecl = lookup( inst, typeSubs );
+			if ( ! concDecl ) {
+				// set concDecl to new type, insert type declaration into statements to add
+				concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) );
+				substituteMembers( inst->get_baseUnion()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() );
+				DeclMutator::addDeclaration( concDecl );
+				insert( inst, typeSubs, concDecl );
+			}
+			UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() );
+			newInst->set_baseUnion( concDecl );
+
+			deleteAll( typeSubs );
+			delete inst;
+			return newInst;
+		}
+
+	// 	/// Gets the base struct or union declaration for a member expression; NULL if not applicable
+	// 	AggregateDecl* getMemberBaseDecl( MemberExpr *memberExpr ) {
+	// 		// get variable for member aggregate
+	// 		VariableExpr *varExpr = dynamic_cast< VariableExpr* >( memberExpr->get_aggregate() );
+	// 		if ( ! varExpr ) return NULL;
+	//
+	// 		// get object for variable
+	// 		ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() );
+	// 		if ( ! objectDecl ) return NULL;
+	//
+	// 		// get base declaration from object type
+	// 		Type *objectType = objectDecl->get_type();
+	// 		StructInstType *structType = dynamic_cast< StructInstType* >( objectType );
+	// 		if ( structType ) return structType->get_baseStruct();
+	// 		UnionInstType *unionType = dynamic_cast< UnionInstType* >( objectType );
+	// 		if ( unionType ) return unionType->get_baseUnion();
+	//
+	// 		return NULL;
+	// 	}
+	//
+	// 	/// Finds the declaration with the given name, returning decls.end() if none such
+	// 	std::list< Declaration* >::const_iterator findDeclNamed( const std::list< Declaration* > &decls, const std::string &name ) {
+	// 		for( std::list< Declaration* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) {
+	// 			if ( (*decl)->get_name() == name ) return decl;
+	// 		}
+	// 		return decls.end();
+	// 	}
+	//
+	// 	Expression* Instantiate::mutate( MemberExpr *memberExpr ) {
+	// 		// mutate, exiting early if no longer MemberExpr
+	// 		Expression *expr = Mutator::mutate( memberExpr );
+	// 		memberExpr = dynamic_cast< MemberExpr* >( expr );
+	// 		if ( ! memberExpr ) return expr;
+	//
+	// 		// get declaration of member and base declaration of member, exiting early if not found
+	// 		AggregateDecl *memberBase = getMemberBaseDecl( memberExpr );
+	// 		if ( ! memberBase ) return memberExpr;
+	// 		DeclarationWithType *memberDecl = memberExpr->get_member();
+	// 		std::list< Declaration* >::const_iterator baseIt = findDeclNamed( memberBase->get_members(), memberDecl->get_name() );
+	// 		if ( baseIt == memberBase->get_members().end() ) return memberExpr;
+	// 		DeclarationWithType *baseDecl = dynamic_cast< DeclarationWithType* >( *baseIt );
+	// 		if ( ! baseDecl ) return memberExpr;
+	//
+	// 		// check if stated type of the member is not the type of the member's declaration; if so, need a cast
+	// 		// this *SHOULD* be safe, I don't think anything but the void-replacements I put in for dtypes would make it past the typechecker
+	// 		SymTab::Indexer dummy;
+	// 		if ( ResolvExpr::typesCompatible( memberDecl->get_type(), baseDecl->get_type(), dummy ) ) return memberExpr;
+	// 		else return new CastExpr( memberExpr, memberDecl->get_type() );
+	// 	}
+
+		void GenericInstantiator::doBeginScope() {
+			DeclMutator::doBeginScope();
+			instantiations.beginScope();
+		}
+
+		void GenericInstantiator::doEndScope() {
+			DeclMutator::doEndScope();
+			instantiations.endScope();
+		}
+
 ////////////////////////////////////////// MemberExprFixer ////////////////////////////////////////////////////
 
Index: src/GenPoly/DeclMutator.cc
===================================================================
--- src/GenPoly/DeclMutator.cc	(revision 6943f0511582b37034c3046e163992d7c9c2b3df)
+++ src/GenPoly/DeclMutator.cc	(revision a5a71d0e95113b304e03502a14ccab969324804f)
@@ -24,10 +24,15 @@
 	}
 
-	DeclMutator::DeclMutator() : Mutator(), declsToAdd(1) {}
+	DeclMutator::DeclMutator() : Mutator(), declsToAdd(1), declsToAddAfter(1) {}
 
 	DeclMutator::~DeclMutator() {}
 	
 	void DeclMutator::mutateDeclarationList( std::list< Declaration* > &decls ) {
-		for ( std::list< Declaration* >::iterator decl = decls.begin(); decl != decls.end(); ++decl ) {
+		for ( std::list< Declaration* >::iterator decl = decls.begin(); ; ++decl ) {
+			// splice in new declarations after previous decl
+			decls.splice( decl, declsToAddAfter.back() );
+
+			if ( decl == decls.end() ) break;
+			
 			// run mutator on declaration
 			*decl = maybeMutate( *decl, *this );
@@ -39,6 +44,7 @@
 
 	void DeclMutator::doBeginScope() {
-		// add new decl list for inside of scope
+		// add new decl lists for inside of scope
 		declsToAdd.resize( declsToAdd.size()+1 );
+		declsToAddAfter.resize( declsToAddAfter.size()+1 );
 	}
 
@@ -49,4 +55,9 @@
 		newBack->splice( newBack->end(), *back );
 		declsToAdd.pop_back();
+		
+		back = declsToAddAfter.rbegin();
+		newBack = back + 1;
+		newBack->splice( newBack->end(), *back );
+		declsToAddAfter.pop_back();
 	}
 
@@ -61,5 +72,8 @@
 		stmt = maybeMutate( stmt, *this );
 		// return if no declarations to add
-		if ( declsToAdd.back().empty() ) return stmt;
+		if ( declsToAdd.back().empty() && declsToAddAfter.back().empty() ) {
+			doEndScope();
+			return stmt;
+		}
 
 		// otherwise add declarations to new compound statement
@@ -71,8 +85,15 @@
 		declsToAdd.back().clear();
 
+		// add mutated statement
+		compound->get_kids().push_back( stmt );
+
+		// add declarations after to new compound statement
+		for ( std::list< Declaration* >::iterator decl = declsToAddAfter.back().begin(); decl != declsToAddAfter.back().end(); ++decl ) {
+			DeclStmt *declStmt = new DeclStmt( noLabels, *decl );
+			compound->get_kids().push_back( declStmt );
+		}
+		declsToAddAfter.back().clear();
+
 		doEndScope();
-
-		// add mutated statement and return
-		compound->get_kids().push_back( stmt );
 		return compound;
 	}
@@ -80,6 +101,16 @@
 	void DeclMutator::mutateStatementList( std::list< Statement* > &stmts ) {
 		doBeginScope();
+
 		
-		for ( std::list< Statement* >::iterator stmt = stmts.begin(); stmt != stmts.end(); ++stmt ) {
+		for ( std::list< Statement* >::iterator stmt = stmts.begin(); ; ++stmt ) {
+			// add any new declarations after the previous statement
+			for ( std::list< Declaration* >::iterator decl = declsToAddAfter.back().begin(); decl != declsToAddAfter.back().end(); ++decl ) {
+				DeclStmt *declStmt = new DeclStmt( noLabels, *decl );
+				stmts.insert( stmt, declStmt );
+			}
+			declsToAddAfter.back().clear();
+
+			if ( stmt == stmts.end() ) break;
+			
 			// run mutator on statement
 			*stmt = maybeMutate( *stmt, *this );
@@ -92,5 +123,5 @@
 			declsToAdd.back().clear();
 		}
-
+		
 		doEndScope();
 	}
@@ -98,4 +129,8 @@
 	void DeclMutator::addDeclaration( Declaration *decl ) {
 		declsToAdd.back().push_back( decl );
+	}
+
+	void DeclMutator::addDeclarationAfter( Declaration *decl ) {
+		declsToAddAfter.back().push_back( decl );
 	}
 
Index: src/GenPoly/DeclMutator.h
===================================================================
--- src/GenPoly/DeclMutator.h	(revision 6943f0511582b37034c3046e163992d7c9c2b3df)
+++ src/GenPoly/DeclMutator.h	(revision a5a71d0e95113b304e03502a14ccab969324804f)
@@ -55,7 +55,11 @@
 		/// Add a declaration to the list to be added before the current position
 		void addDeclaration( Declaration* decl );
+		/// Add a declaration to the list to be added after the current position
+		void addDeclarationAfter( Declaration* decl );
 	private:
 		/// A stack of declarations to add before the current declaration or statement
 		std::vector< std::list< Declaration* > > declsToAdd;
+		/// A stack of declarations to add after the current declaration or statement
+		std::vector< std::list< Declaration* > > declsToAddAfter;
 	};
 } // namespace
Index: src/GenPoly/InstantiateGeneric.cc
===================================================================
--- src/GenPoly/InstantiateGeneric.cc	(revision 6943f0511582b37034c3046e163992d7c9c2b3df)
+++ 	(revision )
@@ -1,360 +1,0 @@
-//
-// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
-//
-// The contents of this file are covered under the licence agreement in the
-// file "LICENCE" distributed with Cforall.
-//
-// InstantiateGeneric.cc --
-//
-// Author           : Aaron B. Moss
-// Created On       : Wed Nov 11 14:55:01 2015
-// Last Modified By : Aaron B. Moss
-// Last Modified On : Wed Nov 11 14:55:01 2015
-// Update Count     : 1
-//
-
-#include <list>
-#include <map>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include "InstantiateGeneric.h"
-#include "DeclMutator.h"
-
-#include "ResolvExpr/typeops.h"
-#include "SymTab/Indexer.h"
-#include "SynTree/Declaration.h"
-#include "SynTree/Mutator.h"
-#include "SynTree/Statement.h"
-#include "SynTree/Type.h"
-#include "SynTree/TypeSubstitution.h"
-
-#include "Common/UniqueName.h"
-#include "Common/utility.h"
-
-namespace GenPoly {
-
-	/// Key for a unique concrete type; generic base type paired with type parameter list
-	struct ConcreteType {
-		ConcreteType() : base(NULL), params() {}
-
-		ConcreteType(AggregateDecl *_base, const std::list< Type* >& _params) : base(_base), params() { cloneAll(_params, params); }
-
-		ConcreteType(const ConcreteType& that) : base(that.base), params() { cloneAll(that.params, params); }
-
-		/// Extracts types from a list of TypeExpr*
-		ConcreteType(AggregateDecl *_base, const std::list< TypeExpr* >& _params) : base(_base), params() {
-			for ( std::list< TypeExpr* >::const_iterator param = _params.begin(); param != _params.end(); ++param ) {
-				params.push_back( (*param)->get_type()->clone() );
-			}
-		}
-
-		ConcreteType& operator= (const ConcreteType& that) {
-			deleteAll( params );
-			params.clear();
-
-			base = that.base;
-			cloneAll( that.params, params );
-
-			return *this;
-		}
-
-		~ConcreteType() { deleteAll( params ); }
-
-		bool operator== (const ConcreteType& that) const {
-			if ( base != that.base ) return false;
-
-			SymTab::Indexer dummy;
-			if ( params.size() != that.params.size() ) return false;
-			for ( std::list< Type* >::const_iterator it = params.begin(), jt = that.params.begin(); it != params.end(); ++it, ++jt ) {
-				if ( ! ResolvExpr::typesCompatible( *it, *jt, dummy ) ) return false;
-			}
-			return true;
-		}
-
-		AggregateDecl *base;        ///< Base generic type
-		std::list< Type* > params;  ///< Instantiation parameters
-	};
-	
-	/// Maps a concrete type to the instantiated struct type, accounting for scope
-	class InstantiationMap {
-		/// Instantiation of a generic type, with key information to find it
-		struct Instantiation {
-			ConcreteType key;     ///< Instantiation parameters for this type
-			AggregateDecl *decl;  ///< Declaration of the instantiated generic type
-
-			Instantiation() : key(), decl(0) {}
-			Instantiation(const ConcreteType &_key, AggregateDecl *_decl) : key(_key), decl(_decl) {}
-		};
-		/// Map of generic types to instantiations of them
-		typedef std::map< AggregateDecl*, std::vector< Instantiation > > Scope;
-
-		std::vector< Scope > scopes;  ///< list of scopes, from outermost to innermost
-
-	public:
-		/// Starts a new scope
-		void beginScope() {
-			Scope scope;
-			scopes.push_back(scope);
-		}
-
-		/// Ends a scope
-		void endScope() {
-			scopes.pop_back();
-		}
-
-		/// Default constructor initializes with one scope
-		InstantiationMap() { beginScope(); }
-
-	private:
-		/// Gets the declaration for the concrete instantiation of this type, assuming it has already been instantiated in the current scope.
-		/// Returns NULL on none such.
-		AggregateDecl* lookup( AggregateDecl *generic, const std::list< TypeExpr* >& params ) {
-			ConcreteType key(generic, params);
-			// scan scopes from innermost out
-			for ( std::vector< Scope >::const_reverse_iterator scope = scopes.rbegin(); scope != scopes.rend(); ++scope ) {
-				// skip scope if no instantiations of this generic type
-				Scope::const_iterator insts = scope->find( generic );
-				if ( insts == scope->end() ) continue;
-				// look through instantiations for matches to concrete type
-				for ( std::vector< Instantiation >::const_iterator inst = insts->second.begin(); inst != insts->second.end(); ++inst ) {
-					if ( inst->key == key ) return inst->decl;
-				}
-			}
-			// no matching instantiation found
-			return NULL;
-		}
-	public:
-		StructDecl* lookup( StructInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (StructDecl*)lookup( inst->get_baseStruct(), typeSubs ); }
-		UnionDecl* lookup( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs ) { return (UnionDecl*)lookup( inst->get_baseUnion(), typeSubs ); }
-
-	private:
-		/// Adds a declaration for a concrete type to the current scope
-		void insert( AggregateDecl *generic, const std::list< TypeExpr* > &params, AggregateDecl *decl ) {
-			ConcreteType key(generic, params);
-			scopes.back()[generic].push_back( Instantiation( key, decl ) );
-		}
-	public:
-		void insert( StructInstType *inst, const std::list< TypeExpr* > &typeSubs, StructDecl *decl ) { insert( inst->get_baseStruct(), typeSubs, decl ); }
-		void insert( UnionInstType *inst, const std::list< TypeExpr* > &typeSubs, UnionDecl *decl ) { insert( inst->get_baseUnion(), typeSubs, decl ); }
-	};
-
-	/// Mutator pass that replaces concrete instantiations of generic types with actual struct declarations, scoped appropriately
-	class Instantiate : public DeclMutator {
-		/// Map of (generic type, parameter list) pairs to concrete type instantiations
-		InstantiationMap instantiations;
-		/// Namer for concrete types
-		UniqueName typeNamer;
-
-	public:
-		Instantiate() : DeclMutator(), instantiations(), typeNamer("_conc_") {}
-
-		virtual Type* mutate( StructInstType *inst );
-		virtual Type* mutate( UnionInstType *inst );
-
-// 		virtual Expression* mutate( MemberExpr *memberExpr );
-		
-		virtual void doBeginScope();
-		virtual void doEndScope();
-	};
-	
-	void instantiateGeneric( std::list< Declaration* >& translationUnit ) {
-		Instantiate instantiator;
-		instantiator.mutateDeclarationList( translationUnit );
-	}
-
-	/// Makes substitutions of params into baseParams; returns true if all parameters substituted for a concrete type
-	bool makeSubstitutions( const std::list< TypeDecl* >& baseParams, const std::list< Expression* >& params, std::list< TypeExpr* >& out ) {
- 		bool allConcrete = true;  // will finish the substitution list even if they're not all concrete
-
-		// substitute concrete types for given parameters, and incomplete types for placeholders
-		std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin();
-		std::list< Expression* >::const_iterator param = params.begin();
-		for ( ; baseParam != baseParams.end() && param != params.end(); ++baseParam, ++param ) {
-// 			switch ( (*baseParam)->get_kind() ) {
-// 			case TypeDecl::Any: {   // any type is a valid substitution here; complete types can be used to instantiate generics
-				TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
-				assert(paramType && "Aggregate parameters should be type expressions");
-				out.push_back( paramType->clone() );
-				// check that the substituted type isn't a type variable itself
-				if ( dynamic_cast< TypeInstType* >( paramType->get_type() ) ) {
- 					allConcrete = false;
-				}
-// 				break;
-// 			}
-// 			case TypeDecl::Dtype:  // dtype can be consistently replaced with void [only pointers, which become void*]
-// 				out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) );
-// 				break;
-// 			case TypeDecl::Ftype:  // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype]
-// 				out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) );
-// 				break;
-// 			}
-		}
-
-		// if any parameters left over, not done
-		if ( baseParam != baseParams.end() ) return false;
-// 		// if not enough parameters given, substitute remaining incomplete types for placeholders
-// 		for ( ; baseParam != baseParams.end(); ++baseParam ) {
-// 			switch ( (*baseParam)->get_kind() ) {
-// 			case TypeDecl::Any:    // no more substitutions here, fail early
-// 				return false;
-// 			case TypeDecl::Dtype:  // dtype can be consistently replaced with void [only pointers, which become void*]
-// 				out.push_back( new TypeExpr( new VoidType( Type::Qualifiers() ) ) );
-// 				break;
-// 			case TypeDecl::Ftype:  // pointer-to-ftype can be consistently replaced with void (*)(void) [similar to dtype]
-// 				out.push_back( new TypeExpr( new FunctionType( Type::Qualifiers(), false ) ) );
-// 				break;
-// 			}
-// 		}
-
- 		return allConcrete;
-	}
-
-	/// Substitutes types of members of in according to baseParams => typeSubs, appending the result to out
-	void substituteMembers( const std::list< Declaration* >& in, const std::list< TypeDecl* >& baseParams, const std::list< TypeExpr* >& typeSubs, 
-						    std::list< Declaration* >& out ) {
-		// substitute types into new members
-		TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
-		for ( std::list< Declaration* >::const_iterator member = in.begin(); member != in.end(); ++member ) {
-			Declaration *newMember = (*member)->clone();
-			subs.apply(newMember);
-			out.push_back( newMember );
-		}
-	}
-
-	Type* Instantiate::mutate( StructInstType *inst ) {
-		// mutate subtypes
-		Type *mutated = Mutator::mutate( inst );
-		inst = dynamic_cast< StructInstType* >( mutated );
-		if ( ! inst ) return mutated;
-
-		// exit early if no need for further mutation
-		if ( inst->get_parameters().empty() ) return inst;
-		assert( inst->get_baseParameters() && "Base struct has parameters" );
-
-		// check if type can be concretely instantiated; put substitutions into typeSubs
-		std::list< TypeExpr* > typeSubs;
-		if ( ! makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ) ) {
-			deleteAll( typeSubs );
-			return inst;
-		}
-		
-		// make concrete instantiation of generic type
-		StructDecl *concDecl = instantiations.lookup( inst, typeSubs );
-		if ( ! concDecl ) {
-			// set concDecl to new type, insert type declaration into statements to add
-			concDecl = new StructDecl( typeNamer.newName( inst->get_name() ) );
-			substituteMembers( inst->get_baseStruct()->get_members(), *inst->get_baseParameters(), typeSubs, 	concDecl->get_members() );
-			DeclMutator::addDeclaration( concDecl );
-			instantiations.insert( inst, typeSubs, concDecl );
-		}
-		StructInstType *newInst = new StructInstType( inst->get_qualifiers(), concDecl->get_name() );
-		newInst->set_baseStruct( concDecl );
-
-		deleteAll( typeSubs );
-		delete inst;
-		return newInst;
-	}
-	
-	Type* Instantiate::mutate( UnionInstType *inst ) {
-		// mutate subtypes
-		Type *mutated = Mutator::mutate( inst );
-		inst = dynamic_cast< UnionInstType* >( mutated );
-		if ( ! inst ) return mutated;
-
-		// exit early if no need for further mutation
-		if ( inst->get_parameters().empty() ) return inst;
-		assert( inst->get_baseParameters() && "Base union has parameters" );
-
-		// check if type can be concretely instantiated; put substitutions into typeSubs
-		std::list< TypeExpr* > typeSubs;
-		if ( ! makeSubstitutions( *inst->get_baseParameters(), inst->get_parameters(), typeSubs ) ) {
-			deleteAll( typeSubs );
-			return inst;
-		}
-		
-		// make concrete instantiation of generic type
-		UnionDecl *concDecl = instantiations.lookup( inst, typeSubs );
-		if ( ! concDecl ) {
-			// set concDecl to new type, insert type declaration into statements to add
-			concDecl = new UnionDecl( typeNamer.newName( inst->get_name() ) );
-			substituteMembers( inst->get_baseUnion()->get_members(), *inst->get_baseParameters(), typeSubs, concDecl->get_members() );
-			DeclMutator::addDeclaration( concDecl );
-			instantiations.insert( inst, typeSubs, concDecl );
-		}
-		UnionInstType *newInst = new UnionInstType( inst->get_qualifiers(), concDecl->get_name() );
-		newInst->set_baseUnion( concDecl );
-
-		deleteAll( typeSubs );
-		delete inst;
-		return newInst;
-	}
-
-// 	/// Gets the base struct or union declaration for a member expression; NULL if not applicable
-// 	AggregateDecl* getMemberBaseDecl( MemberExpr *memberExpr ) {
-// 		// get variable for member aggregate
-// 		VariableExpr *varExpr = dynamic_cast< VariableExpr* >( memberExpr->get_aggregate() );
-// 		if ( ! varExpr ) return NULL;
-// 
-// 		// get object for variable
-// 		ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() );
-// 		if ( ! objectDecl ) return NULL;
-// 
-// 		// get base declaration from object type
-// 		Type *objectType = objectDecl->get_type();
-// 		StructInstType *structType = dynamic_cast< StructInstType* >( objectType );
-// 		if ( structType ) return structType->get_baseStruct();
-// 		UnionInstType *unionType = dynamic_cast< UnionInstType* >( objectType );
-// 		if ( unionType ) return unionType->get_baseUnion();
-// 
-// 		return NULL;
-// 	}
-// 
-// 	/// Finds the declaration with the given name, returning decls.end() if none such
-// 	std::list< Declaration* >::const_iterator findDeclNamed( const std::list< Declaration* > &decls, const std::string &name ) {
-// 		for( std::list< Declaration* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) {
-// 			if ( (*decl)->get_name() == name ) return decl;
-// 		}
-// 		return decls.end();
-// 	}
-// 	
-// 	Expression* Instantiate::mutate( MemberExpr *memberExpr ) {
-// 		// mutate, exiting early if no longer MemberExpr
-// 		Expression *expr = Mutator::mutate( memberExpr );
-// 		memberExpr = dynamic_cast< MemberExpr* >( expr );
-// 		if ( ! memberExpr ) return expr;
-// 
-// 		// get declaration of member and base declaration of member, exiting early if not found
-// 		AggregateDecl *memberBase = getMemberBaseDecl( memberExpr );
-// 		if ( ! memberBase ) return memberExpr;
-// 		DeclarationWithType *memberDecl = memberExpr->get_member();
-// 		std::list< Declaration* >::const_iterator baseIt = findDeclNamed( memberBase->get_members(), memberDecl->get_name() );
-// 		if ( baseIt == memberBase->get_members().end() ) return memberExpr;
-// 		DeclarationWithType *baseDecl = dynamic_cast< DeclarationWithType* >( *baseIt );
-// 		if ( ! baseDecl ) return memberExpr;
-// 
-// 		// check if stated type of the member is not the type of the member's declaration; if so, need a cast
-// 		// this *SHOULD* be safe, I don't think anything but the void-replacements I put in for dtypes would make it past the typechecker
-// 		SymTab::Indexer dummy;
-// 		if ( ResolvExpr::typesCompatible( memberDecl->get_type(), baseDecl->get_type(), dummy ) ) return memberExpr;
-// 		else return new CastExpr( memberExpr, memberDecl->get_type() );
-// 	}
-	
-	void Instantiate::doBeginScope() {
-		DeclMutator::doBeginScope();
-		instantiations.beginScope();
-	}
-
-	void Instantiate::doEndScope() {
-		DeclMutator::doEndScope();
-		instantiations.endScope();
-	}
-	
-}  // namespace GenPoly
-
-// Local Variables: //
-// tab-width: 4 //
-// mode: c++ //
-// compile-command: "make install" //
-// End: //
Index: src/GenPoly/InstantiateGeneric.h
===================================================================
--- src/GenPoly/InstantiateGeneric.h	(revision 6943f0511582b37034c3046e163992d7c9c2b3df)
+++ 	(revision )
@@ -1,33 +1,0 @@
-//
-// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
-//
-// The contents of this file are covered under the licence agreement in the
-// file "LICENCE" distributed with Cforall.
-//
-// InstantiateGeneric.h --
-//
-// Author           : Aaron B. Moss
-// Created On       : Wed Nov 11 14:55:01 2015
-// Last Modified By : Aaron B. Moss
-// Last Modified On : Wed Nov 11 14:55:01 2015
-// Update Count     : 1
-//
-
-#ifndef _INSTANTIATEGENERIC_H
-#define _INSTANTIATEGENERIC_H
-
-#include <list>
-#include "SynTree/SynTree.h"
-
-namespace GenPoly {
-	/// Replaces generic structs used outside polymorphic contexts with their concrete instantiations
-	void instantiateGeneric( std::list< Declaration* >& translationUnit );
-} // namespace GenPoly
-
-#endif // _INSTANTIATEGENERIC_H
-
-// Local Variables: //
-// tab-width: 4 //
-// mode: c++ //
-// compile-command: "make install" //
-// End: //
Index: src/GenPoly/module.mk
===================================================================
--- src/GenPoly/module.mk	(revision 6943f0511582b37034c3046e163992d7c9c2b3df)
+++ src/GenPoly/module.mk	(revision a5a71d0e95113b304e03502a14ccab969324804f)
@@ -23,4 +23,3 @@
        GenPoly/CopyParams.cc \
        GenPoly/FindFunction.cc \
-       GenPoly/InstantiateGeneric.cc \
        GenPoly/DeclMutator.cc
