Index: src/InitTweak/InitModel.cc
===================================================================
--- src/InitTweak/InitModel.cc	(revision 61f9356c5a7f6b519ed835460f72dc5f34708073)
+++ src/InitTweak/InitModel.cc	(revision a56767c553787fe4920170c20ffd7e869a093d76)
@@ -5,11 +5,11 @@
 // file "LICENCE" distributed with Cforall.
 //
-// InitModel.cc -- 
+// InitModel.cc --
 //
 // Author           : Rodolfo G. Esteves
 // Created On       : Mon May 18 07:44:20 2015
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Tue May 19 16:37:08 2015
-// Update Count     : 1
+// Last Modified By : Rob Schluntz
+// Last Modified On : Thu Jan 07 13:38:46 2016
+// Update Count     : 5
 //
 
@@ -198,5 +198,5 @@
 		assert(init == 0 && single != 0);
 		std::list< Expression * > empty;
-		init = new SingleInit( single->get_expr(), empty );
+		init = new SingleInit( single->get_expr(), empty, false ); // cannot be constructed
 		return;
 	}
@@ -214,5 +214,6 @@
 			} // if
 
-		init = new ListInit( contents );
+		std::list< Expression * > desig;
+		init = new ListInit( contents, desig, false ); // cannot be constructed
 		return;
 	}
Index: src/InitTweak/RemoveInit.cc
===================================================================
--- src/InitTweak/RemoveInit.cc	(revision 61f9356c5a7f6b519ed835460f72dc5f34708073)
+++ src/InitTweak/RemoveInit.cc	(revision a56767c553787fe4920170c20ffd7e869a093d76)
@@ -5,13 +5,15 @@
 // file "LICENCE" distributed with Cforall.
 //
-// RemoveInit.cc -- 
+// RemoveInit.cc --
 //
 // Author           : Rob Schluntz
 // Created On       : Mon May 18 07:44:20 2015
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Tue Dec 15 15:37:26 2015
-// Update Count     : 15
-//
-
+// Last Modified By : Rob Schluntz
+// Last Modified On : Mon Jan 11 14:41:26 2016
+// Update Count     : 118
+//
+
+#include <stack>
+#include <list>
 #include "RemoveInit.h"
 #include "SynTree/Declaration.h"
@@ -21,4 +23,5 @@
 #include "SynTree/Initializer.h"
 #include "SynTree/Mutator.h"
+#include "GenPoly/PolyMutator.h"
 
 namespace InitTweak {
@@ -26,20 +29,14 @@
 		const std::list<Label> noLabels;
 	}
-	
-	class RemoveInit : public Mutator {
+
+	class RemoveInit : public GenPoly::PolyMutator {
 	  public:
 		RemoveInit();
-		virtual ObjectDecl * mutate(ObjectDecl *objDecl);
+		virtual ObjectDecl * mutate( ObjectDecl *objDecl );
 		virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
 
 		virtual Statement * mutate( ReturnStmt * returnStmt );
-		
-		virtual CompoundStmt * mutate(CompoundStmt * compoundStmt);
-		
+
 	  protected:
-		std::list< Statement* > stmtsToAddBefore;
-		std::list< Statement* > stmtsToAddAfter;
-		void mutateStatementList( std::list< Statement* > &statements );
-
 		std::list<DeclarationWithType*> returnVals;
 		UniqueName tempNamer;
@@ -47,30 +44,28 @@
 	};
 
+	class CtorDtor : public GenPoly::PolyMutator {
+	  public:
+		// CtorDtor();
+
+		virtual ObjectDecl * mutate( ObjectDecl * );
+
+		virtual CompoundStmt * mutate( CompoundStmt * compoundStmt );
+
+	  protected:
+		typedef std::map< std::string, DeclarationWithType * > MMMMAP;
+		std::stack< MMMMAP > constructedObjects;
+
+		// to be added before block ends - use push_front so order is correct
+		std::list< Statement * > destructorStmts;
+	};
+
 	void tweak( std::list< Declaration * > translationUnit ) {
 		RemoveInit remover;
+		CtorDtor ctordtor;
 		mutateAll( translationUnit, remover );
+		mutateAll( translationUnit, ctordtor );
 	}
 
 	RemoveInit::RemoveInit() : tempNamer( "_retVal" ) {}
-	
-	void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) {
-		for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
-			if ( ! stmtsToAddAfter.empty() ) {
-				statements.splice( i, stmtsToAddAfter );
-			} // if
-			*i = (*i)->acceptMutator( *this );
-			if ( ! stmtsToAddBefore.empty() ) {
-				statements.splice( i, stmtsToAddBefore );
-			} // if
-		} // for
-		if ( ! stmtsToAddAfter.empty() ) {
-			statements.splice( statements.end(), stmtsToAddAfter );
-		} // if
-	}
-
-	CompoundStmt *RemoveInit::mutate(CompoundStmt *compoundStmt) {
-		mutateStatementList( compoundStmt->get_kids() );
-		return compoundStmt;
-	}
 
 	// in the case where an object has an initializer and a polymorphic type, insert an assignment immediately after the
@@ -95,10 +90,10 @@
 		if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue()  ) {
 			ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), 0 );
-			stmtsToAddBefore.push_back( new DeclStmt( noLabels, newObj ) );
-			
+			stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
+
 			UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
 			assign->get_args().push_back( new AddressExpr (new NameExpr( newObj->get_name() ) ) );
 			assign->get_args().push_back( returnStmt->get_expr() );
-			stmtsToAddBefore.push_back(new ExprStmt(noLabels, assign));
+			stmtsToAdd.push_back(new ExprStmt(noLabels, assign));
 
 			returnStmt->set_expr( new VariableExpr( newObj ) );
@@ -110,5 +105,5 @@
 		std::list<DeclarationWithType*> oldReturnVals = returnVals;
 		std::string oldFuncName = funcName;
-		
+
 		FunctionType * type = functionDecl->get_functionType();
 		returnVals = type->get_returnVals();
@@ -119,4 +114,82 @@
 		return decl;
 	}
+
+	bool tryConstruct( ObjectDecl * objDecl ) {
+		// xxx - handle designations
+		return objDecl->get_init() == NULL ||
+			( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() );
+	}
+
+	ExprStmt * makeCtorDtorStmt( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
+		UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) );
+		expr->get_args().push_back( new VariableExpr( objDecl ) );
+		expr->get_args().splice( expr->get_args().end(), args );
+		return new ExprStmt( noLabels, expr );
+	}
+
+	// InitExpander ctor/dtor being marked as weak symbols
+	// this is causing a bug - Rodolfo's InitExpander is being constructed and destructed
+	// with different fields, which causes a segfault
+
+	class InitExpander : public Visitor {
+	  public:
+	  InitExpander() {}
+	  // ~InitExpander() {}
+		virtual void visit( SingleInit * singleInit );
+		virtual void visit( ListInit * listInit );
+		std::list< Expression * > argList;
+	};
+
+	void InitExpander::visit( SingleInit * singleInit ) {
+		argList.push_back( singleInit->get_value()->clone() );
+	}
+
+	void InitExpander::visit( ListInit * listInit ) {
+		// xxx - for now, assume no nested list inits
+		std::list<Initializer*>::iterator it = listInit->begin_initializers();
+		for ( ; it != listInit->end_initializers(); ++it ) {
+			(*it)->accept( *this );
+		}
+	}
+
+	std::list< Expression * > makeInitList( Initializer * init ) {
+		if ( init ) {
+			InitExpander expander;
+			// init->accept( expander );
+			// std::list< Expression * > l = expander.argList;
+			std::list< Expression * > l;
+			return l;
+		} else {
+			std::list< Expression * > l;
+			return l;
+		}
+	}
+
+	ObjectDecl * CtorDtor::mutate( ObjectDecl * objDecl ) {
+		// hands off if designated or if @=
+		if ( tryConstruct( objDecl ) ) {
+			ExprStmt * ctor = makeCtorDtorStmt( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
+			ExprStmt * dtor = makeCtorDtorStmt( "^?{}", objDecl, std::list< Expression * >() );
+
+			// set_ctor...
+			// need to remember init expression, in case no ctors exist
+			// if ctor does exist, want to use ctor stmt instead of init
+			objDecl->set_ctor( ctor );
+			destructorStmts.push_front( dtor );
+		}
+		return objDecl;
+	}
+
+	CompoundStmt * CtorDtor::mutate( CompoundStmt * compoundStmt ) {
+		CompoundStmt * ret = PolyMutator::mutate( compoundStmt );
+		std::list< Statement * > &statements = ret->get_kids();
+		if ( ! destructorStmts.empty() ) {
+			statements.splice( statements.end(), destructorStmts );
+		} // if
+		return ret;
+	}
+
+
+
 } // namespace InitTweak
 
