Index: src/InitTweak/FixInit.cc
===================================================================
--- src/InitTweak/FixInit.cc	(revision ba3706fcc236a2f6c927dc1a9da14313751a7aa4)
+++ src/InitTweak/FixInit.cc	(revision c13e8dc8e6f0df8fbbc9501a53af72999d0ecc57)
@@ -54,4 +54,5 @@
 #include "SynTree/Type.h"              // for Type, Type::StorageClasses
 #include "SynTree/TypeSubstitution.h"  // for TypeSubstitution, operator<<
+#include "SynTree/VarExprReplacer.h"   // for VarExprReplacer
 #include "SynTree/Visitor.h"           // for acceptAll, maybeAccept
 
@@ -158,10 +159,6 @@
 			using Parent::previsit;
 
-			void previsit( ObjectDecl * objDecl );
 			void previsit( FunctionDecl * funcDecl );
 
-			void previsit( CompoundStmt * compoundStmt );
-			void postvisit( CompoundStmt * compoundStmt );
-			void previsit( ReturnStmt * returnStmt );
 			void previsit( BranchStmt * stmt );
 		private:
@@ -203,4 +200,6 @@
 			static void generate( std::list< Declaration * > & translationUnit );
 
+			void previsit( StructDecl * structDecl );
+
 			void previsit( FunctionDecl * funcDecl );
 			void postvisit( FunctionDecl * funcDecl );
@@ -220,4 +219,8 @@
 			bool isCtor = false; // true if current function is a constructor
 			StructDecl * structDecl = nullptr;
+
+			// special built-in functions necessary for this to work
+			StructDecl * dtorStruct = nullptr;
+			FunctionDecl * dtorStructDestroy = nullptr;
 		};
 
@@ -644,4 +647,55 @@
 		}
 
+		DeclarationWithType * getDtorFunc( ObjectDecl * objDecl, Statement * input, std::list< Statement * > & stmtsToAdd ) {
+			// unwrap implicit statement wrapper
+			Statement * dtor = input;
+			if ( ImplicitCtorDtorStmt * implicit = dynamic_cast< ImplicitCtorDtorStmt * >( input ) ) {
+				// dtor = implicit->callStmt;
+				// implicit->callStmt = nullptr;
+			}
+			assert( dtor );
+			std::list< Expression * > matches;
+			collectCtorDtorCalls( dtor, matches );
+
+			if ( dynamic_cast< ExprStmt * >( dtor ) ) {
+				// only one destructor call in the expression
+				if ( matches.size() == 1 ) {
+					DeclarationWithType * func = getFunction( matches.front() );
+					assertf( func, "getFunction failed to find function in %s", toString( matches.front() ).c_str() );
+
+					// cleanup argument must be a function, not an object (including function pointer)
+					if ( FunctionDecl * dtorFunc = dynamic_cast< FunctionDecl * > ( func ) ) {
+						if ( dtorFunc->type->forall.empty() ) {
+							// simple case where the destructor is a monomorphic function call - can simply
+							// use that function as the cleanup function.
+							delete dtor;
+							return func;
+						}
+					}
+				}
+			}
+
+			// otherwise the cleanup is more complicated - need to build a single argument cleanup function that
+			// wraps the more complicated code.
+			static UniqueName dtorNamer( "__cleanup_dtor" );
+			FunctionDecl * dtorFunc = FunctionDecl::newFunction( dtorNamer.newName(), SymTab::genDefaultType( objDecl->type->stripReferences(), false ), new CompoundStmt( noLabels ) );
+			stmtsToAdd.push_back( new DeclStmt( noLabels, dtorFunc ) );
+
+			// the original code contains uses of objDecl - replace them with the newly generated 'this' parameter.
+			ObjectDecl * thisParam = getParamThis( dtorFunc->type );
+			Expression * replacement = new VariableExpr( thisParam );
+
+			Type * base = replacement->result->stripReferences();
+			if ( dynamic_cast< ArrayType * >( base ) || dynamic_cast< TupleType * > ( base ) ) {
+				// need to cast away reference for array types, since the destructor is generated without the reference type,
+				// and for tuple types since tuple indexing does not work directly on a reference
+				replacement = new CastExpr( replacement, base->clone() );
+			}
+			VarExprReplacer::replace( dtor, { std::make_pair( objDecl, replacement ) } );
+			dtorFunc->statements->push_back( dtor );
+
+			return dtorFunc;
+		}
+
 		DeclarationWithType * FixInit::postmutate( ObjectDecl *objDecl ) {
 			// since this removes the init field from objDecl, it must occur after children are mutated (i.e. postmutate)
@@ -756,4 +810,19 @@
 							ctorInit->ctor = nullptr;
 						}
+
+						Statement * dtor = ctorInit->dtor;
+						if ( dtor ) {
+							ImplicitCtorDtorStmt * implicit = strict_dynamic_cast< ImplicitCtorDtorStmt * >( dtor );
+							Statement * dtorStmt = implicit->callStmt;
+
+							// don't need to call intrinsic dtor, because it does nothing, but
+							// non-intrinsic dtors must be called
+							if ( ! isIntrinsicSingleArgCallStmt( dtorStmt ) ) {
+								// set dtor location to the object's location for error messages
+								DeclarationWithType * dtorFunc = getDtorFunc( objDecl, dtorStmt, stmtsToAddBefore );
+								objDecl->attributes.push_back( new Attribute( "cleanup", { new VariableExpr( dtorFunc ) } ) );
+								ctorInit->dtor = nullptr;
+							} // if
+						}
 					} // if
 				} else if ( Initializer * init = ctorInit->init ) {
@@ -798,36 +867,4 @@
 
 
-		template<typename Iterator, typename OutputIterator>
-		void insertDtors( Iterator begin, Iterator end, OutputIterator out ) {
-			for ( Iterator it = begin ; it != end ; ++it ) {
-				// extract destructor statement from the object decl and insert it into the output. Note that this is
-				// only called on lists of non-static objects with implicit non-intrinsic dtors, so if the user manually
-				// calls an intrinsic dtor then the call must (and will) still be generated since the argument may
-				// contain side effects.
-				ObjectDecl * objDecl = *it;
-				ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() );
-				assert( ctorInit && ctorInit->get_dtor() );
-				*out++ = ctorInit->get_dtor()->clone();
-			} // for
-		}
-
-		void InsertDtors::previsit( ObjectDecl * objDecl ) {
-			// remember non-static destructed objects so that their destructors can be inserted later
-			if ( ! objDecl->get_storageClasses().is_static ) {
-				if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
-					// a decision should have been made by the resolver, so ctor and init are not both non-NULL
-					assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
-					Statement * dtor = ctorInit->get_dtor();
-					// don't need to call intrinsic dtor, because it does nothing, but
-					// non-intrinsic dtors must be called
-					if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
-						// set dtor location to the object's location for error messages
-						ctorInit->dtor->location = objDecl->location;
-						reverseDeclOrder.front().push_front( objDecl );
-					} // if
-				} // if
-			} // if
-		}
-
 		void InsertDtors::previsit( FunctionDecl * funcDecl ) {
 			// each function needs to have its own set of labels
@@ -842,29 +879,4 @@
 		}
 
-		void InsertDtors::previsit( CompoundStmt * compoundStmt ) {
-			// visit statements - this will also populate reverseDeclOrder list.  don't want to dump all destructors
-			// when block is left, just the destructors associated with variables defined in this block, so push a new
-			// list to the top of the stack so that we can differentiate scopes
-			reverseDeclOrder.push_front( OrderedDecls() );
-			Parent::previsit( compoundStmt );
-		}
-
-		void InsertDtors::postvisit( CompoundStmt * compoundStmt ) {
-			// add destructors for the current scope that we're exiting, unless the last statement is a return, which
-			// causes unreachable code warnings
-			std::list< Statement * > & statements = compoundStmt->get_kids();
-			if ( ! statements.empty() && ! dynamic_cast< ReturnStmt * >( statements.back() ) ) {
-				insertDtors( reverseDeclOrder.front().begin(), reverseDeclOrder.front().end(), back_inserter( statements ) );
-			}
-			reverseDeclOrder.pop_front();
-		}
-
-		void InsertDtors::previsit( ReturnStmt * ) {
-			// return exits all scopes, so dump destructors for all scopes
-			for ( OrderedDecls & od : reverseDeclOrder ) {
-				insertDtors( od.begin(), od.end(), back_inserter( stmtsToAddBefore ) );
-			} // for
-		}
-
 		// Handle break/continue/goto in the same manner as C++.  Basic idea: any objects that are in scope at the
 		// BranchStmt but not at the labelled (target) statement must be destructed.  If there are any objects in scope
@@ -894,22 +906,4 @@
 			if ( ! diff.empty() ) {
 				throw SemanticError( std::string("jump to label '") + stmt->get_target().get_name() + "' crosses initialization of " + (*diff.begin())->get_name() + " ", stmt );
-			} // if
-			// S_G-S_L results in set of objects that must be destructed
-			diff.clear();
-			std::set_difference( curVars.begin(), curVars.end(), lvars.begin(), lvars.end(), std::inserter( diff, diff.end() ) );
-			DTOR_PRINT(
-				std::cerr << "S_G-S_L = " << printSet( diff ) << std::endl;
-			)
-			if ( ! diff.empty() ) {
-				// create an auxilliary set for fast lookup -- can't make diff a set, because diff ordering should be consistent for error messages.
-				std::unordered_set<ObjectDecl *> needsDestructor( diff.begin(), diff.end() );
-
-				// go through decl ordered list of objectdecl. for each element that occurs in diff, output destructor
-				OrderedDecls ordered;
-				for ( OrderedDecls & rdo : reverseDeclOrder ) {
-					// add elements from reverseDeclOrder into ordered if they occur in diff - it is key that this happens in reverse declaration order.
-					copy_if( rdo.begin(), rdo.end(), back_inserter( ordered ), [&]( ObjectDecl * objDecl ) { return needsDestructor.count( objDecl ); } );
-				} // for
-				insertDtors( ordered.begin(), ordered.end(), back_inserter( stmtsToAddBefore ) );
 			} // if
 		}
@@ -937,4 +931,10 @@
 		}
 
+		void GenStructMemberCalls::previsit( StructDecl * structDecl ) {
+			if ( ! dtorStruct && structDecl->name == "__Destructor" ) {
+				dtorStruct = structDecl;
+			}
+		}
+
 		void GenStructMemberCalls::previsit( FunctionDecl * funcDecl ) {
 			GuardValue( function );
@@ -949,4 +949,9 @@
 			unhandled.clear();
 			usedUninit.clear();
+
+			if ( ! dtorStructDestroy && funcDecl->name == "__destroy_Destructor" ) {
+				dtorStructDestroy = funcDecl;
+				return;
+			}
 
 			function = funcDecl;
@@ -960,4 +965,5 @@
 				if ( structType ) {
 					structDecl = structType->get_baseStruct();
+					if ( structDecl == dtorStruct ) return;
 					for ( Declaration * member : structDecl->get_members() ) {
 						if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {
@@ -1029,8 +1035,29 @@
 							callStmt->acceptMutator( resolver );
 							if ( isCtor ) {
-								function->get_statements()->push_front( callStmt );
+								function->statements->push_front( callStmt );
 							} else {
 								// destructor statements should be added at the end
-								function->get_statements()->push_back( callStmt );
+								// function->get_statements()->push_back( callStmt );
+
+								// Destructor _dtor0 = { &b.a1, (void (*)(void *)_destroy_A };
+								std::list< Statement * > stmtsToAdd;
+
+								static UniqueName memberDtorNamer = { "__memberDtor" };
+								assertf( dtorStruct, "builtin __Destructor not found." );
+								assertf( dtorStructDestroy, "builtin __destroy_Destructor not found." );
+
+								Expression * thisExpr = new AddressExpr( new VariableExpr( thisParam ) );
+								Expression * dtorExpr = new VariableExpr( getDtorFunc( thisParam, callStmt, stmtsToAdd ) );
+
+								// cast destructor pointer to void (*)(void *), to silence GCC incompatible pointer warnings
+								FunctionType * dtorFtype = new FunctionType( Type::Qualifiers(), false );
+								dtorFtype->parameters.push_back( ObjectDecl::newObject( "", new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ), nullptr ) );
+								Type * dtorType = new PointerType( Type::Qualifiers(), dtorFtype );
+
+								ObjectDecl * destructor = ObjectDecl::newObject( memberDtorNamer.newName(), new StructInstType( Type::Qualifiers(), dtorStruct ), new ListInit( { new SingleInit( thisExpr ), new SingleInit( new CastExpr( dtorExpr, dtorType ) ) } ) );
+								function->statements->push_front( new DeclStmt( noLabels, destructor ) );
+								destructor->attributes.push_back( new Attribute( "cleanup", { new VariableExpr( dtorStructDestroy ) } ) );
+
+								function->statements->kids.splice( function->statements->kids.begin(), stmtsToAdd );
 							}
 						} catch ( SemanticError & error ) {
