Index: src/SymTab/Autogen.cc
===================================================================
--- src/SymTab/Autogen.cc	(revision bd7e60918ad2d7fa73046569632b064782dac72e)
+++ src/SymTab/Autogen.cc	(revision be151bfb7d6e050ea312f07f97188a5d5debbbef)
@@ -54,5 +54,5 @@
 	};
 
-	struct AutogenerateRoutines final : public WithDeclsToAdd, public WithVisitorRef<AutogenerateRoutines>, public WithGuards, public WithShortCircuiting {
+	struct AutogenerateRoutines final : public WithDeclsToAdd, public WithVisitorRef<AutogenerateRoutines>, public WithGuards, public WithShortCircuiting, public WithIndexer {
 		AutogenerateRoutines();
 
@@ -194,11 +194,22 @@
 		Type *refType;
 		unsigned int functionNesting;
-		const std::list< TypeDecl* > & typeParams;
 		OutputIterator out;
-		FuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, const std::list< TypeDecl* > & typeParams, OutputIterator out ) : container( container ), refType( refType ), functionNesting( functionNesting ), typeParams( typeParams ), out( 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 ) {
-			if ( ! shouldGenerate( data.map, container ) ) return;
+			// 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 );
 
@@ -214,6 +225,6 @@
 
 	template< typename OutputIterator, typename Container >
-	FuncGenerator<OutputIterator, Container> makeFuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, const std::list< TypeDecl* > & typeParams, OutputIterator out ) {
-		return FuncGenerator<OutputIterator, Container>( container, refType, functionNesting, typeParams, out );
+	FuncGenerator<OutputIterator, Container> makeFuncGenerator( const Container & container, Type *refType, unsigned int functionNesting, OutputIterator out ) {
+		return FuncGenerator<OutputIterator, Container>( container, refType, functionNesting, out );
 	}
 
@@ -243,5 +254,5 @@
 	// E ?=?(E volatile*, int),
 	//   ?=?(E _Atomic volatile*, int);
-	void makeEnumFunctions( EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
+	void makeEnumFunctions( EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd, SymTab::Indexer & indexer ) {
 
 		// T ?=?(E *, E);
@@ -278,4 +289,9 @@
 		declsToAdd.push_back( dtorDecl );
 		declsToAdd.push_back( assignDecl ); // assignment should come last since it uses copy constructor in return
+
+		indexer.addId( ctorDecl );
+		indexer.addId( copyCtorDecl );
+		indexer.addId( dtorDecl );
+		indexer.addId( assignDecl );
 	}
 
@@ -306,15 +322,4 @@
 				}
 
-				if ( field->get_name() == "" ) {
-					// don't assign to anonymous members
-					// xxx - this is a temporary fix. Anonymous members tie into
-					// our inheritance model. I think the correct way to handle this is to
-					// cast the structure to the type of the member and let the resolver
-					// figure out whether it's valid and have a pass afterwards that fixes
-					// the assignment to use pointer arithmetic with the offset of the
-					// member, much like how generic type members are handled.
-					continue;
-				}
-
 				assert( ! func->get_functionType()->get_parameters().empty() );
 				ObjectDecl * dstParam = dynamic_cast<ObjectDecl*>( func->get_functionType()->get_parameters().front() );
@@ -348,13 +353,4 @@
 				if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( field ) ) ) {
 					// don't make a function whose parameter is an unnamed bitfield
-					continue;
-				} else if ( field->get_name() == "" ) {
-					// don't assign to anonymous members
-					// xxx - this is a temporary fix. Anonymous members tie into
-					// our inheritance model. I think the correct way to handle this is to
-					// cast the structure to the type of the member and let the resolver
-					// figure out whether it's valid and have a pass afterwards that fixes
-					// the assignment to use pointer arithmetic with the offset of the
-					// member, much like how generic type members are handled.
 					continue;
 				} else if ( parameter != params.end() ) {
@@ -379,5 +375,5 @@
 
 	/// 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 ) {
+	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() ) ) {
@@ -385,45 +381,54 @@
 		}
 
-		// Make function polymorphic in same parameters as generic struct, if applicable
-		const std::list< TypeDecl * > & typeParams = aggregateDecl->get_parameters(); // List of type variables to be placed on the generated functions
-
 		// 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, typeParams, back_inserter( newFuncs ) );
+		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() );
 		}
 
-		// field ctors are only generated if default constructor and copy constructor are both generated
-		unsigned numCtors = std::count_if( newFuncs.begin(), newFuncs.end(), [](FunctionDecl * dcl) { return CodeGen::isConstructor( dcl->get_name() ); } );
-
-		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, declsToAdd );
-			}
-		}
-
+		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->get_members().begin(), aggregateDecl->get_members().end(), dcl );
+				makeStructFunctionBody( aggregateDecl->members.begin(), aggregateDecl->members.end(), dcl );
 			} else {
-				makeStructFunctionBody( aggregateDecl->get_members().rbegin(), aggregateDecl->get_members().rend(), dcl, false );
-			}
-			if ( CodeGen::isAssignment( dcl->get_name() ) ) {
+				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->get_parameters().size() == 2 );
-				ObjectDecl * srcParam = strict_dynamic_cast< ObjectDecl * >( assignType->get_parameters().back() );
-				dcl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
-			}
-			declsToAdd.push_back( dcl );
-		}
+				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.
@@ -433,27 +438,32 @@
 		// are generated, since they need access to both
 		if ( numCtors == 2 ) {
+			const auto & typeParams = aggregateDecl->parameters;
 			FunctionType * memCtorType = genDefaultType( refType );
-			cloneAll( typeParams, memCtorType->get_forall() );
-			for ( std::list<Declaration *>::iterator i = aggregateDecl->get_members().begin(); i != aggregateDecl->get_members().end(); ++i ) {
-				DeclarationWithType * member = dynamic_cast<DeclarationWithType *>( *i );
-				assert( member );
-				if ( isUnnamedBitfield( dynamic_cast< ObjectDecl * > ( member ) ) ) {
+			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;
-				} else if ( member->get_name() == "" ) {
-					// don't assign to anonymous members
-					// xxx - this is a temporary fix. Anonymous members tie into
-					// our inheritance model. I think the correct way to handle this is to
-					// cast the structure to the type of the member and let the resolver
-					// figure out whether it's valid/choose the correct unnamed member
+				}
+				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;
 				}
-				memCtorType->get_parameters().push_back( new ObjectDecl( member->get_name(), Type::StorageClasses(), LinkageSpec::Cforall, 0, member->get_type()->clone(), 0 ) );
-				FunctionDecl * ctor = genFunc( "?{}", memCtorType->clone(), functionNesting );
-				makeStructFieldCtorBody( aggregateDecl->get_members().begin(), aggregateDecl->get_members().end(), ctor );
-				declsToAdd.push_back( ctor );
+				definitions.push_back( ctor );
 			}
 			delete memCtorType;
 		}
+
+		declsToAdd.splice( declsToAdd.end(), forwards );
+		declsToAdd.splice( declsToAdd.end(), definitions );
 	}
 
@@ -483,5 +493,5 @@
 
 	/// generates union constructors, destructors, and assignment operator
-	void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd ) {
+	void makeUnionFunctions( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting, std::list< Declaration * > & declsToAdd, SymTab::Indexer & indexer ) {
 		// Make function polymorphic in same parameters as generic union, if applicable
 		const std::list< TypeDecl* > & typeParams = aggregateDecl->get_parameters(); // List of type variables to be placed on the generated functions
@@ -545,4 +555,10 @@
 		declsToAdd.push_back( dtorDecl );
 		declsToAdd.push_back( assignDecl ); // assignment should come last since it uses copy constructor in return
+
+		indexer.addId( ctorDecl );
+		indexer.addId( copyCtorDecl );
+		indexer.addId( dtorDecl );
+		indexer.addId( assignDecl );
+
 		declsToAdd.splice( declsToAdd.end(), memCtors );
 	}
@@ -558,9 +574,9 @@
 
 	void AutogenerateRoutines::previsit( EnumDecl * enumDecl ) {
-		visit_children = false;
-		if ( ! enumDecl->get_members().empty() ) {
+		// must visit children (enum constants) to add them to the indexer
+		if ( enumDecl->has_body() ) {
 			EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
-			// enumInst->set_baseEnum( enumDecl );
-			makeEnumFunctions( enumInst, functionNesting, declsToAddAfter );
+			enumInst->set_baseEnum( enumDecl );
+			makeEnumFunctions( enumInst, functionNesting, declsToAddAfter, indexer );
 		}
 	}
@@ -572,9 +588,9 @@
 			for ( TypeDecl * typeDecl : structDecl->parameters ) {
 				// need to visit assertions so that they are added to the appropriate maps
-				acceptAll( typeDecl->assertions, *visitor );
+				// 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 );
+			makeStructFunctions( structDecl, &structInst, functionNesting, declsToAddAfter, data, indexer );
 			structsDone.insert( structDecl->name );
 		} // if
@@ -583,5 +599,5 @@
 	void AutogenerateRoutines::previsit( UnionDecl * unionDecl ) {
 		visit_children = false;
-		if ( ! unionDecl->get_members().empty() ) {
+		if ( unionDecl->has_body() /* && unionsDone.find( unionDecl->name ) == unionsDone.end() */ ) {
 			UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
 			unionInst.set_baseUnion( unionDecl );
@@ -589,5 +605,5 @@
 				unionInst.get_parameters().push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), typeDecl ) ) );
 			}
-			makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAddAfter );
+			makeUnionFunctions( unionDecl, &unionInst, functionNesting, declsToAddAfter, indexer );
 		} // if
 	}
@@ -608,7 +624,6 @@
 		std::list< FunctionDecl * > newFuncs;
 		std::list< Declaration * > tds { typeDecl };
-		std::list< TypeDecl * > typeParams;
 		TypeInstType refType( Type::Qualifiers(), typeDecl->name, typeDecl );
-		auto generator = makeFuncGenerator( lazy_map( tds, declToTypeDeclBase ), &refType, functionNesting, typeParams, back_inserter( newFuncs ) );
+		auto generator = makeFuncGenerator( lazy_map( tds, declToTypeDeclBase ), &refType, functionNesting, back_inserter( newFuncs ) );
 		for ( const FuncData & d : data ) {
 			generator.gen( d, false );
