private void AddScopeCleanupMethod(Class methodClass, Scope scope)
{
Method scopeMethod = (Method)Templates.GetTypeTemplateByName("ScopeMethod").Members[0];
scopeMethod.Name = new Identifier("Scope" + scope.UniqueKey.ToString());
methodClass.Members.Add(scopeMethod);
scopeMethod.DeclaringType = methodClass;
// Generate a "finalizing" statement as appropriate for each local variable in the scope
for (int i = 0, n = scope.Members.Count; i < n; i++)
{
Field f = scope.Members[i] as Field;
if (f == null)
continue;
if (f.Type == SystemTypes.Boolean)
{
// locals._field = false;
scopeMethod.Body.Statements.Add(
new ExpressionStatement(
new AssignmentExpression(
new AssignmentStatement(
new QualifiedIdentifier(Identifier.For("locals"), f.Name),
new Literal(false, SystemTypes.Boolean)
)
)
)
);
}
else // if (GetTypeClassification(f.Type) == TypeClassification.Heap)
{
// locals._field = 0; // note - this is good for pointers, ints, bytes, and enums
scopeMethod.Body.Statements.Add(
new ExpressionStatement(
new AssignmentExpression(
new AssignmentStatement(
new QualifiedIdentifier(Identifier.For("locals"), f.Name),
new Literal(0, SystemTypes.Int32)
)
)
)
);
}
}
}