Index: src/AST/Pass.impl.hpp
===================================================================
--- src/AST/Pass.impl.hpp	(revision cd5b58fc76b159ad81278f9373a3ebf2c3563d5b)
+++ src/AST/Pass.impl.hpp	(revision 7f3b5ceac9ffdeb064abe4bb8f8ed5c357d1abac)
@@ -617,4 +617,5 @@
 				maybe_accept( node, &FunctionDecl::returns );
 				maybe_accept( node, &FunctionDecl::type );
+				maybe_accept( node, &FunctionDecl::attributes );
 				// First remember that we are now within a function.
 				ValueGuard< bool > oldInFunction( inFunction );
@@ -625,5 +626,4 @@
 				atFunctionTop = true;
 				maybe_accept( node, &FunctionDecl::stmts );
-				maybe_accept( node, &FunctionDecl::attributes );
 			}
 		}
Index: src/Common/PassVisitor.impl.h
===================================================================
--- src/Common/PassVisitor.impl.h	(revision cd5b58fc76b159ad81278f9373a3ebf2c3563d5b)
+++ src/Common/PassVisitor.impl.h	(revision 7f3b5ceac9ffdeb064abe4bb8f8ed5c357d1abac)
@@ -607,4 +607,5 @@
 			indexerAddId( &func );
 			maybeMutate_impl( node->type, *this );
+			maybeMutate_impl( node->attributes, *this );
 			// First remember that we are now within a function.
 			ValueGuard< bool > oldInFunction( inFunction );
@@ -615,5 +616,4 @@
 			atFunctionTop = true;
 			maybeMutate_impl( node->statements, *this );
-			maybeMutate_impl( node->attributes, *this );
 		}
 	}
Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision cd5b58fc76b159ad81278f9373a3ebf2c3563d5b)
+++ src/GenPoly/Box.cc	(revision 7f3b5ceac9ffdeb064abe4bb8f8ed5c357d1abac)
@@ -68,8 +68,5 @@
 		/// Adds layout-generation functions to polymorphic types.
 		class LayoutFunctionBuilder final : public WithDeclsToAdd, public WithVisitorRef<LayoutFunctionBuilder>, public WithShortCircuiting {
-			// Current level of nested functions:
-			unsigned int functionNesting = 0;
 		public:
-			void previsit( FunctionDecl *functionDecl );
 			void previsit( StructDecl *structDecl );
 			void previsit( UnionDecl *unionDecl );
@@ -237,12 +234,4 @@
 	////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////////
 
-	void LayoutFunctionBuilder::previsit( FunctionDecl *functionDecl ) {
-		visit_children = false;
-		maybeAccept( functionDecl->get_functionType(), *visitor );
-		++functionNesting;
-		maybeAccept( functionDecl->get_statements(), *visitor );
-		--functionNesting;
-	}
-
 	/// Get a list of type declarations that will affect a layout function
 	std::list< TypeDecl* > takeOtypeOnly( std::list< TypeDecl* > &decls ) {
@@ -271,9 +260,9 @@
 
 	/// Builds a layout function declaration
-	FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) {
+	FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, bool isInFunction, 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( layoutofName( typeDecl ),
-													 functionNesting > 0 ? Type::StorageClasses() : Type::StorageClasses( Type::Static ),
+													 isInFunction ? Type::StorageClasses() : Type::StorageClasses( Type::Static ),
 													 LinkageSpec::AutoGen, layoutFnType, new CompoundStmt(),
 													 std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) );
@@ -347,5 +336,5 @@
 
 		// build function decl
-		FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType );
+		FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, isInFunction(), layoutFnType );
 
 		// calculate struct layout in function body
@@ -401,5 +390,5 @@
 
 		// build function decl
-		FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType );
+		FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, isInFunction(), layoutFnType );
 
 		// calculate union layout in function body
@@ -633,6 +622,6 @@
 
 		void Pass1::replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ) {
-			for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
-				TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
+			for ( Expression * const param : params ) {
+				TypeExpr *paramType = dynamic_cast< TypeExpr* >( param );
 				assertf(paramType, "Aggregate parameters should be type expressions");
 				paramType->set_type( replaceWithConcrete( appExpr, paramType->get_type(), false ) );
@@ -753,8 +742,9 @@
 
 		void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
-			for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->parameters.end(); ++param, ++arg ) {
-				assertf( arg != appExpr->args.end(), "boxParams: missing argument for param %s to %s in %s", toString( *param ).c_str(), toString( function ).c_str(), toString( appExpr ).c_str() );
-				addCast( *arg, (*param)->get_type(), exprTyVars );
-				boxParam( (*param)->get_type(), *arg, exprTyVars );
+			for ( DeclarationWithType * param : function->parameters ) {
+				assertf( arg != appExpr->args.end(), "boxParams: missing argument for param %s to %s in %s", toString( param ).c_str(), toString( function ).c_str(), toString( appExpr ).c_str() );
+				addCast( *arg, param->get_type(), exprTyVars );
+				boxParam( param->get_type(), *arg, exprTyVars );
+				++arg;
 			} // for
 		}
@@ -762,11 +752,11 @@
 		void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
 			std::list< Expression *>::iterator cur = arg;
-			for ( Type::ForallList::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
-				for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->assertions.begin(); assert != (*tyVar)->assertions.end(); ++assert ) {
-					InferredParams::const_iterator inferParam = appExpr->inferParams.find( (*assert)->get_uniqueId() );
-					assertf( inferParam != appExpr->inferParams.end(), "addInferredParams missing inferred parameter: %s in: %s", toString( *assert ).c_str(), toString( appExpr ).c_str() );
+			for ( TypeDecl * const tyVar : functionType->forall ) {
+				for ( DeclarationWithType * const assert : tyVar->assertions ) {
+					InferredParams::const_iterator inferParam = appExpr->inferParams.find( assert->get_uniqueId() );
+					assertf( inferParam != appExpr->inferParams.end(), "addInferredParams missing inferred parameter: %s in: %s", toString( assert ).c_str(), toString( appExpr ).c_str() );
 					Expression *newExpr = inferParam->second.expr->clone();
-					addCast( newExpr, (*assert)->get_type(), tyVars );
-					boxParam( (*assert)->get_type(), newExpr, tyVars );
+					addCast( newExpr, assert->get_type(), tyVars );
+					boxParam( assert->get_type(), newExpr, tyVars );
 					appExpr->get_args().insert( cur, newExpr );
 				} // for
@@ -1221,5 +1211,5 @@
 			std::list< DeclarationWithType *> &paramList = functionType->parameters;
 			std::list< FunctionType *> functions;
-			for (  DeclarationWithType * const arg : functionType->parameters ) {
+			for ( DeclarationWithType * const arg : functionType->parameters ) {
 				Type *orig = arg->get_type();
 				findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter );
