Skip to content

Commit

Permalink
Create less anynymous method context classes for async delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-safar committed Oct 25, 2011
1 parent c722897 commit 394140f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
12 changes: 6 additions & 6 deletions mcs/mcs/iterators.cs
Expand Up @@ -144,7 +144,6 @@ public enum State
}

Field pc_field;
int local_name_idx;
StateMachineMethod method;

protected StateMachine (Block block, TypeContainer parent, MemberBase host, TypeParameter[] tparams, string name)
Expand Down Expand Up @@ -183,11 +182,6 @@ protected override bool DoDefineMembers ()

return base.DoDefineMembers ();
}

protected override string GetVariableMangledName (LocalVariable local_info)
{
return "<" + local_info.Name + ">__" + local_name_idx++.ToString ("X");
}
}

class IteratorStorey : StateMachine
Expand Down Expand Up @@ -376,6 +370,7 @@ protected override Expression DoResolve (ResolveContext ec)
TypeExpr iterator_type_expr;
Field current_field;
Field disposing_field;
int local_name_idx;

TypeExpr enumerator_type;
TypeExpr enumerable_type;
Expand Down Expand Up @@ -549,6 +544,11 @@ protected override void EmitHoistedParameters (EmitContext ec, IList<HoistedPara
base.EmitHoistedParameters (ec, hoisted);
base.EmitHoistedParameters (ec, hoisted_params_copy);
}

protected override string GetVariableMangledName (LocalVariable local_info)
{
return "<" + local_info.Name + ">__" + local_name_idx++.ToString ("X");
}
}

public class StateMachineMethod : Method
Expand Down
14 changes: 9 additions & 5 deletions mcs/mcs/statement.cs
Expand Up @@ -2213,15 +2213,17 @@ public ExplicitBlock (Block parent, Flags flags, Location start, Location end)
public AnonymousMethodStorey CreateAnonymousMethodStorey (ResolveContext ec)
{
//
// An iterator has only 1 storey block
// Return same story for iterator and async blocks unless we are
// in nested anonymous method
//
if (ec.CurrentAnonymousMethod.IsIterator)
return ec.CurrentAnonymousMethod.Storey;
if (ec.CurrentAnonymousMethod is StateMachineInitializer && ParametersBlock.Original == ec.CurrentAnonymousMethod.Block.Original)
return ec.CurrentAnonymousMethod.Storey;

//
// When referencing a variable in iterator storey from children anonymous method
// When referencing a variable in parent iterator/async storey
// from nested anonymous method
//
if (ParametersBlock.am_storey is IteratorStorey) {
if (ParametersBlock.am_storey is StateMachine) {
return ParametersBlock.am_storey;
}

Expand Down Expand Up @@ -2693,6 +2695,7 @@ public void WrapIntoIterator (IMethodData method, TypeContainer host, TypeSpec i
ParametersBlock pb = new ParametersBlock (this, ParametersCompiled.EmptyReadOnlyParameters, StartLocation);
pb.EndLocation = EndLocation;
pb.statements = statements;
pb.original = this;

var iterator = new Iterator (pb, method, host, iterator_type, is_enumerable);
am_storey = new IteratorStorey (iterator);
Expand All @@ -2706,6 +2709,7 @@ public void WrapIntoAsyncTask (IMemberContext context, TypeContainer host, TypeS
ParametersBlock pb = new ParametersBlock (this, ParametersCompiled.EmptyReadOnlyParameters, StartLocation);
pb.EndLocation = EndLocation;
pb.statements = statements;
pb.original = this;

var block_type = host.Module.Compiler.BuiltinTypes.Void;
var initializer = new AsyncInitializer (pb, host, block_type);
Expand Down
44 changes: 10 additions & 34 deletions mcs/tests/ver-il-net_4_5.xml
Expand Up @@ -54157,7 +54157,7 @@
</type>
<type name="Tester+&lt;SwitchTest_1&gt;c__async0">
<method name="Void MoveNext()">
<size>403</size>
<size>362</size>
</method>
<method name="System.String &lt;&gt;m__4()">
<size>6</size>
Expand All @@ -54168,7 +54168,7 @@
</type>
<type name="Tester+&lt;Using_1&gt;c__async1">
<method name="Void MoveNext()">
<size>830</size>
<size>759</size>
</method>
<method name="Base &lt;&gt;m__5()">
<size>6</size>
Expand All @@ -54193,7 +54193,7 @@
</type>
<type name="Tester+&lt;Foreach_1&gt;c__async2">
<method name="Void MoveNext()">
<size>368</size>
<size>315</size>
</method>
<method name="System.Collections.Generic.List`1[System.Int32] &lt;&gt;m__9()">
<size>29</size>
Expand All @@ -54202,21 +54202,6 @@
<size>36</size>
</method>
</type>
<type name="Tester+&lt;SwitchTest_1&gt;c__async0+&lt;SwitchTest_1&gt;c__AnonStorey3">
<method name="Void .ctor()">
<size>7</size>
</method>
</type>
<type name="Tester+&lt;Using_1&gt;c__async1+&lt;Using_1&gt;c__AnonStorey4">
<method name="Void .ctor()">
<size>7</size>
</method>
</type>
<type name="Tester+&lt;Foreach_1&gt;c__async2+&lt;Foreach_1&gt;c__AnonStorey5">
<method name="Void .ctor()">
<size>7</size>
</method>
</type>
</test>
<test name="test-async-17.cs">
<type name="Tester">
Expand Down Expand Up @@ -54362,51 +54347,42 @@
</type>
<type name="Tester+&lt;Lambda_1&gt;c__async0">
<method name="Void MoveNext()">
<size>291</size>
<size>243</size>
</method>
<method name="Void .ctor()">
<size>36</size>
</method>
</type>
<type name="Tester+&lt;Lambda_2&gt;c__async1">
<method name="Void MoveNext()">
<size>291</size>
<size>243</size>
</method>
<method name="Void .ctor()">
<size>36</size>
</method>
</type>
<type name="Tester+&lt;Lambda_3&gt;c__async2`1[T]">
<method name="Void MoveNext()">
<size>291</size>
<size>243</size>
</method>
<method name="Void .ctor()">
<size>36</size>
</method>
</type>
<type name="Tester+&lt;Lambda_1&gt;c__async0+&lt;Lambda_1&gt;c__AnonStorey3">
<type name="Tester+&lt;Lambda_1&gt;c__async0">
<method name="Int32 &lt;&gt;m__0()">
<size>7</size>
</method>
<method name="Void .ctor()">
<size>7</size>
</method>
</type>
<type name="Tester+&lt;Lambda_2&gt;c__async1+&lt;Lambda_2&gt;c__AnonStorey4">
<type name="Tester+&lt;Lambda_2&gt;c__async1">
<method name="Int32 &lt;&gt;m__1()">
<size>19</size>
</method>
<method name="Void .ctor()">
<size>7</size>
<size>14</size>
</method>
</type>
<type name="Tester+&lt;Lambda_3&gt;c__async2`1+&lt;Lambda_3&gt;c__AnonStorey5`1[T]">
<type name="Tester+&lt;Lambda_3&gt;c__async2`1[T]">
<method name="Int32 &lt;&gt;m__2()">
<size>7</size>
</method>
<method name="Void .ctor()">
<size>7</size>
</method>
</type>
</test>
<test name="test-async-19.cs">
Expand Down

0 comments on commit 394140f

Please sign in to comment.