Index: src/SymTab/Autogen.cc
===================================================================
--- src/SymTab/Autogen.cc	(revision be151bfb7d6e050ea312f07f97188a5d5debbbef)
+++ src/SymTab/Autogen.cc	(revision a117f7c8c1b582839efb906967e6dda9e0840614)
@@ -43,13 +43,11 @@
 namespace SymTab {
 	Type * SizeType = 0;
-	typedef ScopedMap< std::string, bool > TypeMap;
-
-	/// Data used to generate functions generically. Specifically, the name of the generated function, a function which generates the routine protoype, and a map which contains data to determine whether a function should be generated.
+
+	/// Data used to generate functions generically. Specifically, the name of the generated function and a function which generates the routine protoype
 	struct FuncData {
 		typedef FunctionType * (*TypeGen)( Type * );
-		FuncData( const std::string & fname, const TypeGen & genType, TypeMap & map ) : fname( fname ), genType( genType ), map( map ) {}
+		FuncData( const std::string & fname, const TypeGen & genType ) : fname( fname ), genType( genType ) {}
 		std::string fname;
 		TypeGen genType;
-		TypeMap & map;
 	};
 
@@ -70,10 +68,9 @@
 
 	  private:
+
 		GenPoly::ScopedSet< std::string > structsDone;
 		unsigned int functionNesting = 0;     // current level of nested functions
-		/// Note: the following maps could be ScopedSets, but it should be easier to work
-		/// deleted functions in if they are maps, since the value false can be inserted
-		/// at the current scope without affecting outer scopes or requiring copies.
-		TypeMap copyable, assignable, constructable, destructable;
+
+		InitTweak::ManagedTypes managedTypes;
 		std::vector< FuncData > data;
 	};
@@ -81,9 +78,9 @@
 	/// generates routines for tuple types.
 	struct AutogenTupleRoutines : public WithDeclsToAdd, public WithVisitorRef<AutogenTupleRoutines>, public WithGuards, public WithShortCircuiting {
-		void previsit( FunctionDecl *functionDecl );
-
-		void postvisit( TupleType *tupleType );
-
-		void previsit( CompoundStmt *compoundStmt );
+		void previsit( FunctionDecl * functionDecl );
+
+		void postvisit( TupleType * tupleType );
+
+		void previsit( CompoundStmt * compoundStmt );
 
 	  private:
@@ -99,4 +96,79 @@
 		// AutogenTupleRoutines tupleGenerator;
 		// acceptAll( translationUnit, tupleGenerator );
+	}
+
+	//=============================================================================================
+	// FuncGenerator definitions
+	//=============================================================================================
+	class FuncGenerator {
+	public:
+		std::list< Declaration * > definitions, forwards;
+
+		FuncGenerator( Type * type, unsigned int functionNesting, const std::vector< FuncData > & data, SymTab::Indexer & indexer ) : type( type ), functionNesting( functionNesting ), data( data ), indexer( indexer ) {}
+
+		virtual bool shouldAutogen() const = 0;
+		void genStandardFuncs();
+		virtual void genFieldCtors() = 0;
+	protected:
+		Type * type;
+		unsigned int functionNesting;
+		const std::vector< FuncData > & data;
+		SymTab::Indexer & indexer;
+
+		virtual void genFuncBody( FunctionDecl * dcl ) = 0;
+		virtual bool isConcurrentType() const = 0;
+
+		void resolve( FunctionDecl * dcl );
+		void generatePrototypes( std::list< FunctionDecl * > & newFuncs );
+	};
+
+	class StructFuncGenerator : public FuncGenerator {
+		StructDecl * aggregateDecl;
+	public:
+		StructFuncGenerator( StructDecl * aggregateDecl, StructInstType * refType, const std::vector< FuncData > & data,  unsigned int functionNesting, SymTab::Indexer & indexer ) : FuncGenerator( refType, functionNesting, data, indexer ), aggregateDecl( aggregateDecl) {}
+
+		virtual bool shouldAutogen() const override;
+		virtual bool isConcurrentType() const override;
+
+		virtual void genFuncBody( FunctionDecl * dcl ) override;
+		virtual void genFieldCtors() override;
+
+	private:
+		/// generates a single struct member operation (constructor call, destructor call, assignment call)
+		void makeMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward = true );
+
+		/// generates the body of a struct function by iterating the struct members (via parameters) - generates default ctor, copy ctor, assignment, and dtor bodies, but NOT field ctor bodies
+		template<typename Iterator>
+		void makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward = true );
+
+		/// generate the body of a constructor which takes parameters that match fields, e.g.
+		/// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
+		template<typename Iterator>
+		void makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func );
+	};
+
+	class TypeFuncGenerator : public FuncGenerator {
+		TypeDecl * typeDecl;
+	public:
+		TypeFuncGenerator( TypeDecl * typeDecl, TypeInstType * refType, unsigned int functionNesting, const std::vector<FuncData> & data, SymTab::Indexer & indexer ) : FuncGenerator( refType, functionNesting, data, indexer ), typeDecl( typeDecl ) {}
+
+		virtual bool shouldAutogen() const override;
+		virtual void genFuncBody( FunctionDecl * dcl ) override;
+		virtual bool isConcurrentType() const override;
+		virtual void genFieldCtors() override;
+	};
+
+	//=============================================================================================
+	// helper functions
+	//=============================================================================================
+	void generateFunctions( FuncGenerator & gen, std::list< Declaration * > & declsToAdd ) {
+		if ( ! gen.shouldAutogen() ) return;
+
+		// generate each of the functions based on the supplied FuncData objects
+		gen.genStandardFuncs();
+		gen.genFieldCtors();
+
+		declsToAdd.splice( declsToAdd.end(), gen.forwards );
+		declsToAdd.splice( declsToAdd.end(), gen.definitions );
 	}
 
@@ -149,82 +221,4 @@
 		decl->fixUniqueId();
 		return decl;
-	}
-
-	/// inserts base type of first argument into map if pred(funcDecl) is true
-	void insert( FunctionDecl *funcDecl, TypeMap & map, FunctionDecl * (*pred)(Declaration *) ) {
-		// insert type into constructable, etc. map if appropriate
-		if ( pred( funcDecl ) ) {
-			FunctionType * ftype = funcDecl->get_functionType();
-			assert( ! ftype->get_parameters().empty() );
-			Type * t = InitTweak::getPointerBase( ftype->get_parameters().front()->get_type() );
-			assert( t );
-			map.insert( Mangler::mangleType( t ), true );
-		}
-	}
-
-	/// using map and t, determines if is constructable, etc.
-	bool lookup( const TypeMap & map, Type * t ) {
-		assertf( t, "Autogenerate lookup was given non-type: %s", toString( t ).c_str() );
-		if ( dynamic_cast< PointerType * >( t ) ) {
-			// will need more complicated checking if we want this to work with pointer types, since currently
-			return true;
-		} else if ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) {
-			// an array's constructor, etc. is generated on the fly based on the base type's constructor, etc.
-			return lookup( map, at->get_base() );
-		}
-		TypeMap::const_iterator it = map.find( Mangler::mangleType( t ) );
-		if ( it != map.end() ) return it->second;
-		// something that does not appear in the map is by default not constructable, etc.
-		return false;
-	}
-
-	/// using map and aggr, examines each member to determine if constructor, etc. should be generated
-	template<typename Container>
-	bool shouldGenerate( const TypeMap & map, const Container & container ) {
-		for ( Type * t : container ) {
-			if ( ! lookup( map, t ) ) return false;
-		}
-		return true;
-	}
-
-	/// data structure for abstracting the generation of special functions
-	template< typename OutputIterator, typename Container >
-	struct FuncGenerator {
-		const Container & container;
-		Type *refType;
-		unsigned int functionNesting;
-		OutputIterator out;
-		FuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, OutputIterator out ) : container( container ), refType( refType ), functionNesting( functionNesting ), out( out ) {}
-
-		const std::list< TypeDecl * > getGenericParams( Type * t ) {
-			std::list< TypeDecl * > * ret = nullptr;
-			if ( StructInstType * inst = dynamic_cast< StructInstType * > ( t ) ) {
-				ret = inst->get_baseParameters();
-			} else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( t ) ) {
-				ret = inst->get_baseParameters();
-			}
-			return ret ? *ret : std::list< TypeDecl * >();
-		}
-
-		/// generates a function (?{}, ?=?, ^?{}) based on the data argument and members. If function is generated, inserts the type into the map.
-		void gen( const FuncData & data, bool concurrent_type ) {
-			// Make function polymorphic in same parameters as generic struct, if applicable
-			std::list< TypeDecl * > typeParams = getGenericParams( refType ); // List of type variables to be placed on the generated functions
-
-			FunctionType * ftype = data.genType( refType );
-
-			if ( concurrent_type && CodeGen::isDestructor( data.fname ) ) {
-				ftype->parameters.front()->get_type()->set_mutex( true );
-			}
-
-			cloneAll( typeParams, ftype->forall );
-			*out++ = genFunc( data.fname, ftype, functionNesting );
-			data.map.insert( Mangler::mangleType( refType ), true );
-		}
-	};
-
-	template< typename OutputIterator, typename Container >
-	FuncGenerator<OutputIterator, Container> makeFuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, OutputIterator out ) {
-		return FuncGenerator<OutputIterator, Container>( container, refType, functionNesting, out );
 	}
 
@@ -296,6 +290,134 @@
 	}
 
-	/// generates a single struct member operation (constructor call, destructor call, assignment call)
-	void makeStructMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward = true ) {
+	Type * declToType( Declaration * decl ) {
+		if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
+			return dwt->get_type();
+		}
+		return nullptr;
+	}
+
+	Type * declToTypeDeclBase( Declaration * decl ) {
+		if ( TypeDecl * td = dynamic_cast< TypeDecl * >( decl ) ) {
+			return td->base;
+		}
+		return nullptr;
+	}
+
+	const std::list< TypeDecl * > getGenericParams( Type * t ) {
+		std::list< TypeDecl * > * ret = nullptr;
+		if ( StructInstType * inst = dynamic_cast< StructInstType * > ( t ) ) {
+			ret = inst->get_baseParameters();
+		} else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( t ) ) {
+			ret = inst->get_baseParameters();
+		}
+		return ret ? *ret : std::list< TypeDecl * >();
+	}
+
+	//=============================================================================================
+	// FuncGenerator member definitions
+	//=============================================================================================
+	void FuncGenerator::genStandardFuncs() {
+		std::list< FunctionDecl * > newFuncs;
+		generatePrototypes( newFuncs );
+
+		for ( FunctionDecl * dcl : newFuncs ) {
+			genFuncBody( dcl );
+			resolve( dcl );
+		}
+	}
+
+	void FuncGenerator::generatePrototypes( std::list< FunctionDecl * > & newFuncs ) {
+		bool concurrent_type = isConcurrentType();
+		for ( const FuncData & data : data ) {
+			// generate a function (?{}, ?=?, ^?{}) based on the current FuncData.
+			FunctionType * ftype = data.genType( type );
+
+			// destructor for concurrent type must be mutex
+			if ( concurrent_type && CodeGen::isDestructor( data.fname ) ) {
+				ftype->parameters.front()->get_type()->set_mutex( true );
+			}
+
+			// Make function polymorphic in same parameters as generic struct, if applicable
+			std::list< TypeDecl * > typeParams = getGenericParams( type ); // List of type variables to be placed on the generated functions
+			cloneAll( typeParams, ftype->forall );
+
+			newFuncs.push_back( genFunc( data.fname, ftype, functionNesting ) );
+		}
+	}
+
+	void FuncGenerator::resolve( FunctionDecl * dcl ) {
+		try {
+			ResolvExpr::resolveDecl( dcl, indexer );
+			if ( functionNesting == 0 ) {
+				// forward declare if top-level struct, so that
+				// type is complete as soon as its body ends
+				// Note: this is necessary if we want structs which contain
+				// generic (otype) structs as members.
+				addForwardDecl( dcl, forwards );
+			}
+			definitions.push_back( dcl );
+			indexer.addId( dcl );
+		} catch ( SemanticError err ) {
+			// okay if decl does not resolve - that means the function should not be generated
+			delete dcl;
+		}
+	}
+
+	bool StructFuncGenerator::shouldAutogen() const {
+		// Builtins do not use autogeneration.
+		return ! aggregateDecl->linkage.is_builtin;
+	}
+	bool StructFuncGenerator::isConcurrentType() const { return aggregateDecl->is_thread() || aggregateDecl->is_monitor(); }
+
+	void StructFuncGenerator::genFuncBody( FunctionDecl * dcl ) {
+		// generate appropriate calls to member ctor, assignment
+		// destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
+		if ( ! CodeGen::isDestructor( dcl->get_name() ) ) {
+			makeFunctionBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), dcl );
+		} else {
+			makeFunctionBody( aggregateDecl->members.rbegin(), aggregateDecl->members.rend(), dcl, false );
+		}
+		if ( CodeGen::isAssignment( dcl->name ) ) {
+			// assignment needs to return a value
+			FunctionType * assignType = dcl->type;
+			assert( assignType->parameters.size() == 2 );
+			assert( assignType->returnVals.size() == 1 );
+			ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.back() );
+			ObjectDecl * retParam = strict_dynamic_cast< ObjectDecl * >( assignType->returnVals.front() );
+
+			dcl->statements->push_back( new ExprStmt( noLabels, new UntypedExpr( new NameExpr("?{}"), { new VariableExpr( retParam ), new VariableExpr( srcParam ) } ) ) );
+			dcl->statements->push_back( new ReturnStmt( noLabels, new VariableExpr( retParam ) ) );
+		}
+	}
+
+	void StructFuncGenerator::genFieldCtors() {
+		// field ctors are only generated if default constructor and copy constructor are both generated
+		unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), [](Declaration * dcl) { return CodeGen::isConstructor( dcl->get_name() ); } );
+
+		// create constructors which take each member type as a parameter.
+		// for example, for struct A { int x, y; }; generate
+		//   void ?{}(A *, int) and void ?{}(A *, int, int)
+		// Field constructors are only generated if default and copy constructor
+		// are generated, since they need access to both
+		if ( numCtors == 2 ) {
+			const auto & typeParams = aggregateDecl->parameters;
+			FunctionType * memCtorType = genDefaultType( type );
+			cloneAll( typeParams, memCtorType->forall );
+			for ( Declaration * member : aggregateDecl->members ) {
+				DeclarationWithType * field = strict_dynamic_cast<DeclarationWithType *>( member );
+				if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) {
+					// don't make a function whose parameter is an unnamed bitfield
+					continue;
+				}
+				memCtorType->parameters.push_back( new ObjectDecl( field->name, Type::StorageClasses(), LinkageSpec::Cforall, 0, field->get_type()->clone(), 0 ) );
+				FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting );
+				makeFieldCtorBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), ctor );
+				resolve( ctor );
+			}
+			delete memCtorType;
+		}
+	}
+
+	void StructFuncGenerator::makeMemberOp( ObjectDecl * dstParam, Expression * src, DeclarationWithType * field, FunctionDecl * func, bool forward ) {
 		InitTweak::InitExpander srcParam( src );
 
@@ -305,7 +427,6 @@
 	}
 
-	/// generates the body of a struct function by iterating the struct members (via parameters) - generates default ctor, copy ctor, assignment, and dtor bodies, but NOT field ctor bodies
 	template<typename Iterator>
-	void makeStructFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward = true ) {
+	void StructFuncGenerator::makeFunctionBody( Iterator member, Iterator end, FunctionDecl * func, bool forward ) {
 		for ( ; member != end; ++member ) {
 			if ( DeclarationWithType *field = dynamic_cast< DeclarationWithType * >( *member ) ) { // otherwise some form of type declaration, e.g. Aggregate
@@ -332,15 +453,13 @@
 
 				Expression *srcselect = srcParam ? new MemberExpr( field, new VariableExpr( srcParam ) ) : nullptr;
-				makeStructMemberOp( dstParam, srcselect, field, func, forward );
+				makeMemberOp( dstParam, srcselect, field, func, forward );
 			} // if
 		} // for
-	} // makeStructFunctionBody
-
-	/// generate the body of a constructor which takes parameters that match fields, e.g.
-	/// void ?{}(A *, int) and void?{}(A *, int, int) for a struct A which has two int fields.
+	} // makeFunctionBody
+
 	template<typename Iterator>
-	void makeStructFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ) {
-		FunctionType * ftype = func->get_functionType();
-		std::list<DeclarationWithType*> & params = ftype->get_parameters();
+	void StructFuncGenerator::makeFieldCtorBody( Iterator member, Iterator end, FunctionDecl * func ) {
+		FunctionType * ftype = func->type;
+		std::list<DeclarationWithType*> & params = ftype->parameters;
 		assert( params.size() >= 2 );  // should not call this function for default ctor, etc.
 
@@ -357,113 +476,12 @@
 					// matching parameter, initialize field with copy ctor
 					Expression *srcselect = new VariableExpr(*parameter);
-					makeStructMemberOp( dstParam, srcselect, field, func );
+					makeMemberOp( dstParam, srcselect, field, func );
 					++parameter;
 				} else {
 					// no matching parameter, initialize field with default ctor
-					makeStructMemberOp( dstParam, nullptr, field, func );
+					makeMemberOp( dstParam, nullptr, field, func );
 				}
 			}
 		}
-	}
-
-	Type * declToType( Declaration * decl ) {
-		if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
-			return dwt->get_type();
-		}
-		return nullptr;
-	}
-
-	/// generates struct constructors, destructor, and assignment functions
-	void makeStructFunctions( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd, const std::vector< FuncData > & data, SymTab::Indexer & indexer ) {
-		// Builtins do not use autogeneration.
-		if ( LinkageSpec::isBuiltin( aggregateDecl->get_linkage() ) ) {
-			return;
-		}
-
-		// generate each of the functions based on the supplied FuncData objects
-		std::list< FunctionDecl * > newFuncs;
-		// structure that iterates aggregate decl members, returning their types
-		auto generator = makeFuncGenerator( lazy_map( aggregateDecl->members, declToType ), refType, functionNesting, back_inserter( newFuncs ) );
-		for ( const FuncData & d : data ) {
-			generator.gen( d, aggregateDecl->is_thread() || aggregateDecl->is_monitor() );
-		}
-
-		std::list< Declaration * > definitions, forwards;
-		for ( FunctionDecl * dcl : newFuncs ) {
-			// generate appropriate calls to member ctor, assignment
-			// destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
-			if ( ! CodeGen::isDestructor( dcl->get_name() ) ) {
-				makeStructFunctionBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), dcl );
-			} else {
-				makeStructFunctionBody( aggregateDecl->members.rbegin(), aggregateDecl->members.rend(), dcl, false );
-			}
-			if ( CodeGen::isAssignment( dcl->name ) ) {
-				// assignment needs to return a value
-				FunctionType * assignType = dcl->get_functionType();
-				assert( assignType->parameters.size() == 2 );
-				assert( assignType->returnVals.size() == 1 );
-				ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.back() );
-				ObjectDecl * retParam = strict_dynamic_cast< ObjectDecl * >( assignType->returnVals.front() );
-
-				dcl->statements->push_back( new ExprStmt( noLabels, new UntypedExpr( new NameExpr("?{}"), { new VariableExpr( retParam ), new VariableExpr( srcParam ) } ) ) );
-				dcl->statements->push_back( new ReturnStmt( noLabels, new VariableExpr( retParam ) ) );
-			}
-
-			try {
-				ResolvExpr::resolveDecl( dcl, indexer );
-			} catch ( SemanticError err ) {
-				// okay if decl does not resolve - that means the function should not be generated
-				delete dcl;
-				continue;
-			}
-
-			if ( functionNesting == 0 ) {
-				// forward declare if top-level struct, so that
-				// type is complete as soon as its body ends
-				// Note: this is necessary if we want structs which contain
-				// generic (otype) structs as members.
-				addForwardDecl( dcl, forwards );
-			}
-			definitions.push_back( dcl );
-			indexer.addId( dcl );
-		}
-
-		// field ctors are only generated if default constructor and copy constructor are both generated
-		unsigned numCtors = std::count_if( definitions.begin(), definitions.end(), [](Declaration * dcl) { return CodeGen::isConstructor( dcl->get_name() ); } );
-
-		// create constructors which take each member type as a parameter.
-		// for example, for struct A { int x, y; }; generate
-		//   void ?{}(A *, int) and void ?{}(A *, int, int)
-		// Field constructors are only generated if default and copy constructor
-		// are generated, since they need access to both
-		if ( numCtors == 2 ) {
-			const auto & typeParams = aggregateDecl->parameters;
-			FunctionType * memCtorType = genDefaultType( refType );
-			cloneAll( typeParams, memCtorType->forall );
-			for ( Declaration * member : aggregateDecl->members ) {
-				DeclarationWithType * field = dynamic_cast<DeclarationWithType *>( member );
-				assert( field );
-				if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) {
-					// don't make a function whose parameter is an unnamed bitfield
-					continue;
-				}
-				memCtorType->get_parameters().push_back( new ObjectDecl( field->get_name(), Type::StorageClasses(), LinkageSpec::Cforall, 0, field->get_type()->clone(), 0 ) );
-				FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting );
-				makeStructFieldCtorBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), ctor );
-
-				try {
-					ResolvExpr::resolveDecl( ctor, indexer );
-				} catch ( SemanticError err ) {
-					// okay if decl does not resolve - that means the function should not be generated
-					delete ctor;
-					continue;
-				}
-				definitions.push_back( ctor );
-			}
-			delete memCtorType;
-		}
-
-		declsToAdd.splice( declsToAdd.end(), forwards );
-		declsToAdd.splice( declsToAdd.end(), definitions );
 	}
 
@@ -564,11 +582,41 @@
 	}
 
+	bool TypeFuncGenerator::shouldAutogen() const { return true; };
+
+	void TypeFuncGenerator::genFuncBody( FunctionDecl * dcl ) {
+		FunctionType * ftype = dcl->type;
+		assertf( ftype->parameters.size() == 1 || ftype->parameters.size() == 2, "Incorrect number of parameters in autogenerated typedecl function: %zd", ftype->parameters.size() );
+		DeclarationWithType * dst = ftype->parameters.front();
+		DeclarationWithType * src = ftype->parameters.size() == 2 ? ftype->parameters.back() : nullptr;
+		// generate appropriate calls to member ctor, assignment
+		UntypedExpr * expr = new UntypedExpr( new NameExpr( dcl->name ) );
+		expr->args.push_back( new CastExpr( new VariableExpr( dst ), new ReferenceType( Type::Qualifiers(), typeDecl->base->clone() ) ) );
+		if ( src ) expr->args.push_back( new CastExpr( new VariableExpr( src ), typeDecl->base->clone() ) );
+		dcl->statements->kids.push_back( new ExprStmt( noLabels, expr ) );
+		if ( CodeGen::isAssignment( dcl->get_name() ) ) {
+			// assignment needs to return a value
+			FunctionType * assignType = dcl->type;
+			assert( assignType->parameters.size() == 2 );
+			ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.back() );
+			dcl->statements->kids.push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
+		}
+	};
+
+	// xxx - should reach in and determine if base type is concurrent?
+	bool TypeFuncGenerator::isConcurrentType() const { return false; };
+
+	// opaque types do not have field constructors
+	void TypeFuncGenerator::genFieldCtors() { return; };
+
+	//=============================================================================================
+	// Visitor definitions
+	//=============================================================================================
 	AutogenerateRoutines::AutogenerateRoutines() {
 		// the order here determines the order that these functions are generated.
 		// assignment should come last since it uses copy constructor in return.
-		data.emplace_back( "?{}", genDefaultType, constructable );
-		data.emplace_back( "?{}", genCopyType, copyable );
-		data.emplace_back( "^?{}", genDefaultType, destructable );
-		data.emplace_back( "?=?", genAssignType, assignable );
+		data.emplace_back( "?{}", genDefaultType );
+		data.emplace_back( "?{}", genCopyType );
+		data.emplace_back( "^?{}", genDefaultType );
+		data.emplace_back( "?=?", genAssignType );
 	}
 
@@ -584,14 +632,12 @@
 	void AutogenerateRoutines::previsit( StructDecl * structDecl ) {
 		visit_children = false;
-		if ( structDecl->has_body() && structsDone.find( structDecl->name ) == structsDone.end() ) {
+		if ( structDecl->has_body() ) {
 			StructInstType structInst( Type::Qualifiers(), structDecl->name );
+			structInst.set_baseStruct( structDecl );
 			for ( TypeDecl * typeDecl : structDecl->parameters ) {
-				// need to visit assertions so that they are added to the appropriate maps
-				// acceptAll( typeDecl->assertions, *visitor );
 				structInst.parameters.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->name, typeDecl ) ) );
 			}
-			structInst.set_baseStruct( structDecl );
-			makeStructFunctions( structDecl, &structInst, functionNesting, declsToAddAfter, data, indexer );
-			structsDone.insert( structDecl->name );
+			StructFuncGenerator gen( structDecl, &structInst, data, functionNesting, indexer );
+			generateFunctions( gen, declsToAddAfter );
 		} // if
 	}
@@ -599,5 +645,5 @@
 	void AutogenerateRoutines::previsit( UnionDecl * unionDecl ) {
 		visit_children = false;
-		if ( unionDecl->has_body() /* && unionsDone.find( unionDecl->name ) == unionsDone.end() */ ) {
+		if ( unionDecl->has_body()  ) {
 			UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
 			unionInst.set_baseUnion( unionDecl );
@@ -609,11 +655,4 @@
 	}
 
-	Type * declToTypeDeclBase( Declaration * decl ) {
-		if ( TypeDecl * td = dynamic_cast< TypeDecl * >( decl ) ) {
-			return td->base;
-		}
-		return nullptr;
-	}
-
 	// generate ctor/dtors/assign for typedecls, e.g., otype T = int *;
 	void AutogenerateRoutines::previsit( TypeDecl * typeDecl ) {
@@ -621,43 +660,7 @@
 		if ( ! typeDecl->base ) return;
 
-		// generate each of the functions based on the supplied FuncData objects
-		std::list< FunctionDecl * > newFuncs;
-		std::list< Declaration * > tds { typeDecl };
 		TypeInstType refType( Type::Qualifiers(), typeDecl->name, typeDecl );
-		auto generator = makeFuncGenerator( lazy_map( tds, declToTypeDeclBase ), &refType, functionNesting, back_inserter( newFuncs ) );
-		for ( const FuncData & d : data ) {
-			generator.gen( d, false );
-		}
-
-		if ( functionNesting == 0 ) {
-			// forward declare if top-level struct, so that
-			// type is complete as soon as its body ends
-			// Note: this is necessary if we want structs which contain
-			// generic (otype) structs as members.
-			for ( FunctionDecl * dcl : newFuncs ) {
-				addForwardDecl( dcl, declsToAddAfter );
-			}
-		}
-
-		for ( FunctionDecl * dcl : newFuncs ) {
-			FunctionType * ftype = dcl->type;
-			assertf( ftype->parameters.size() == 1 || ftype->parameters.size() == 2, "Incorrect number of parameters in autogenerated typedecl function: %zd", ftype->parameters.size() );
-			DeclarationWithType * dst = ftype->parameters.front();
-			DeclarationWithType * src = ftype->parameters.size() == 2 ? ftype->parameters.back() : nullptr;
-			// generate appropriate calls to member ctor, assignment
-			// destructor needs to do everything in reverse, so pass "forward" based on whether the function is a destructor
-			UntypedExpr * expr = new UntypedExpr( new NameExpr( dcl->name ) );
-			expr->args.push_back( new CastExpr( new VariableExpr( dst ), new ReferenceType( Type::Qualifiers(), typeDecl->base->clone() ) ) );
-			if ( src ) expr->args.push_back( new CastExpr( new VariableExpr( src ), typeDecl->base->clone() ) );
-			dcl->statements->kids.push_back( new ExprStmt( noLabels, expr ) );
-			if ( CodeGen::isAssignment( dcl->get_name() ) ) {
-				// assignment needs to return a value
-				FunctionType * assignType = dcl->type;
-				assert( assignType->parameters.size() == 2 );
-				ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.back() );
-				dcl->statements->kids.push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
-			}
-			declsToAddAfter.push_back( dcl );
-		}
+		TypeFuncGenerator gen( typeDecl, &refType, functionNesting, data, indexer );
+		generateFunctions( gen, declsToAddAfter );
 	}
 
@@ -680,8 +683,5 @@
 		visit_children = false;
 		// record the existence of this function as appropriate
-		insert( functionDecl, constructable, InitTweak::isDefaultConstructor );
-		insert( functionDecl, assignable, InitTweak::isAssignment );
-		insert( functionDecl, copyable, InitTweak::isCopyConstructor );
-		insert( functionDecl, destructable, InitTweak::isDestructor );
+		managedTypes.handleDWT( functionDecl );
 
 		maybeAccept( functionDecl->type, *visitor );
@@ -692,8 +692,5 @@
 
 	void AutogenerateRoutines::previsit( CompoundStmt * ) {
-		GuardScope( constructable );
-		GuardScope( assignable );
-		GuardScope( copyable );
-		GuardScope( destructable );
+		GuardScope( managedTypes );
 		GuardScope( structsDone );
 	}
