private void AddScopeCleanupCalls(StatementList stmts, BasicBlock source, BasicBlock target, List<Scope> nonTrivialScopes)
{
// No scope change, so nothing needed here
if (source.Scope == target.Scope)
return;
//
// Scopes are different. Figure out if we're moving in or out. If the target scope
// isn't "outward", then do nothing.
for (Scope scope = source.Scope; scope != target.Scope; scope = scope.OuterScope)
{
// If we encounter the method-level scope, then this must be an inward move
if (scope is MethodScope)
return;
}
// It's an outward move, so call the cleanup method for each scope that we're exiting
for (Scope scope = source.Scope; scope != target.Scope; scope = scope.OuterScope)
{
Debug.Assert(scope != null);
if (nonTrivialScopes.Contains(scope))
{
stmts.Add(
new ExpressionStatement(
new MethodCall(
Identifier.For("Scope" + scope.UniqueKey.ToString()),
new ExpressionList()
)
)
);
}
}
}