2 // anonymous.cs: Support for anonymous methods
5 // Miguel de Icaza (miguel@ximain.com)
7 // (C) 2003, 2004 Novell, Inc.
9 // TODO: Ideally, we should have the helper classes emited as a hierarchy to map
10 // their nesting, and have the visibility set to private, instead of NestedAssembly
17 using System.Collections;
18 using System.Reflection;
19 using System.Reflection.Emit;
21 namespace Mono.CSharp {
23 public class AnonymousMethod : Expression {
24 // Used to generate unique method names.
25 static int anonymous_method_count;
27 // An array list of AnonymousMethodParameter or null
28 public Parameters Parameters;
31 // The block that makes up the body for the anonymous mehtod
33 public ToplevelBlock Block;
36 // The container block for this anonymous method.
38 public Block ContainingBlock;
41 // The implicit method we create
47 // The emit context for the anonymous method
48 public EmitContext aec;
49 public InternalParameters amp;
53 // The modifiers applied to the method, we aggregate them
55 int method_modifiers = Modifiers.PRIVATE;
58 // During the resolve stage of the anonymous method body,
59 // we discover the actual scope where we are hosted, or
60 // null to host the method in the same class
62 public ScopeInfo Scope;
65 // Points to our container anonymous method if its present
67 public AnonymousMethod ContainerAnonymousMethod;
69 public AnonymousMethod (Parameters parameters, ToplevelBlock container, ToplevelBlock block, Location l)
71 Parameters = parameters;
76 // The order is important: this setups the CaptureContext tree hierarchy.
78 container.SetHaveAnonymousMethods (l, this);
79 block.SetHaveAnonymousMethods (l, this);
82 public override Expression DoResolve (EmitContext ec)
85 // Set class type, set type
88 eclass = ExprClass.Value;
91 // This hack means `The type is not accessible
92 // anywhere', we depend on special conversion
95 type = TypeManager.anonymous_method_type;
100 public override void Emit (EmitContext ec)
102 // nothing, as we only exist to not do anything.
106 // Creates the host for the anonymous method
108 bool CreateMethodHost (EmitContext ec, Type return_type)
111 // Crude hack follows: we replace the TypeBuilder during the
112 // definition to get the method hosted in the right class
115 TypeBuilder current_type = ec.TypeContainer.TypeBuilder;
116 TypeBuilder type_host = (Scope == null ) // || Scope.ScopeTypeBuilder == null)
117 ? current_type : Scope.ScopeTypeBuilder;
119 if (current_type == null)
120 throw new Exception ("The current_type is null");
122 if (type_host == null)
123 throw new Exception ("Type host is null");
125 if (current_type == type_host && ec.IsStatic){
127 method_modifiers |= Modifiers.STATIC;
132 method = new Method (
133 (TypeContainer) ec.TypeContainer, null,
134 new TypeExpression (return_type, loc),
135 method_modifiers, false, new MemberName ("<#AnonymousMethod>" + anonymous_method_count++),
136 Parameters, null, loc);
137 method.Block = Block;
140 // Swap the TypeBuilder while we define the method, then restore
142 if (current_type != null)
143 ec.TypeContainer.TypeBuilder = type_host;
144 bool res = method.Define ();
145 if (current_type != null)
146 ec.TypeContainer.TypeBuilder = current_type;
150 void Error_ParameterMismatch (Type t)
152 Report.Error (1661, loc, "Anonymous method could not be converted to delegate `" +
153 "{0}' since there is a parameter mismatch", t);
157 // Returns true if this anonymous method can be implicitly
158 // converted to the delegate type `delegate_type'
160 public Expression Compatible (EmitContext ec, Type delegate_type, bool probe)
163 // At this point its the first time we know the return type that is
164 // needed for the anonymous method. We create the method here.
167 MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (ec, delegate_type, loc);
168 invoke_mb = (MethodInfo) invoke_mg.Methods [0];
169 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
172 // If implicit parameters are set, then we must check for out in the parameters
173 // and flag it accordingly.
175 bool out_invalid_check = false;
177 if (Parameters == null){
179 out_invalid_check = true;
182 // We provide a set of inaccessible parameters
185 for (i = 0; i < invoke_pd.Count; i++){
186 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
189 int n = invoke_pd.Count - (params_idx != -1 ? 1 : 0);
190 Parameter [] fixedpars = new Parameter [n];
192 for (i = j = 0; i < invoke_pd.Count; i++){
193 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
195 fixedpars [j] = new Parameter (
196 new TypeExpression (invoke_pd.ParameterType (i), loc),
197 "+" + j, invoke_pd.ParameterModifier (i), null);
201 Parameter variable = null;
202 if (params_idx != -1){
203 variable = new Parameter (
204 new TypeExpression (invoke_pd.ParameterType (params_idx), loc),
205 "+" + params_idx, invoke_pd.ParameterModifier (params_idx), null);
208 Parameters = new Parameters (fixedpars, variable, loc);
212 // First, parameter types of `delegate_type' must be compatible
213 // with the anonymous method.
215 amp = new InternalParameters (Parameters.GetParameterInfo (ec), Parameters);
217 if (amp.Count != invoke_pd.Count){
219 Report.Error (1593, loc,
220 "Anonymous method has {0} parameters, while delegate requires {1}",
221 amp.Count, invoke_pd.Count);
222 Error_ParameterMismatch (delegate_type);
227 for (int i = 0; i < amp.Count; i++){
228 Parameter.Modifier amp_mod = amp.ParameterModifier (i);
229 if (amp_mod != invoke_pd.ParameterModifier (i)){
231 Report.Error (1676, loc,
232 "Signature mismatch in parameter modifier for parameter #0", i + 1);
233 Error_ParameterMismatch (delegate_type);
238 if (amp.ParameterType (i) != invoke_pd.ParameterType (i)){
240 Report.Error (1678, loc,
241 "Signature mismatch in parameter {0}: need `{1}' got `{2}'", i + 1,
242 TypeManager.CSharpName (invoke_pd.ParameterType (i)),
243 TypeManager.CSharpName (amp.ParameterType (i)));
244 Error_ParameterMismatch (delegate_type);
249 if (out_invalid_check && (invoke_pd.ParameterModifier (i) & Parameter.Modifier.OUT) != 0){
251 Report.Error (1676, loc,"Parameter {0} must include the `out' modifier ", i+1);
252 Error_ParameterMismatch (delegate_type);
259 // If we are only probing, return ourselves
265 // Second: the return type of the delegate must be compatible with
266 // the anonymous type. Instead of doing a pass to examine the block
267 // we satisfy the rule by setting the return type on the EmitContext
268 // to be the delegate type return type.
271 //MethodBuilder builder = method_data.MethodBuilder;
272 //ILGenerator ig = builder.GetILGenerator ();
275 aec = new EmitContext (
276 ec.TypeContainer, ec.DeclSpace, loc, null,
277 invoke_mb.ReturnType,
278 /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
279 (ec.InUnsafe ? Modifiers.UNSAFE : 0) |
280 (ec.IsStatic ? Modifiers.STATIC : 0),
281 /* No constructor */ false);
283 aec.CurrentAnonymousMethod = this;
284 ContainerAnonymousMethod = ec.CurrentAnonymousMethod;
285 ContainingBlock = ec.CurrentBlock;
287 if (aec.ResolveTopBlock (ec, Block, amp, loc, out unreachable))
288 return new AnonymousDelegate (this, delegate_type, loc).Resolve (ec);
293 public MethodBuilder GetMethodBuilder ()
295 return method.MethodData.MethodBuilder;
298 public bool EmitMethod (EmitContext ec)
300 if (!CreateMethodHost (ec, invoke_mb.ReturnType))
303 MethodBuilder builder = GetMethodBuilder ();
304 ILGenerator ig = builder.GetILGenerator ();
307 Parameters.LabelParameters (aec, builder, loc);
310 // Adjust based on the computed state of the
311 // method from CreateMethodHost
313 aec.MethodIsStatic = (method_modifiers & Modifiers.STATIC) != 0;
315 aec.EmitMeta (Block, amp);
316 aec.EmitResolvedTopBlock (Block, unreachable);
320 public static void Error_AddressOfCapturedVar (string name, Location loc)
322 Report.Error (1686, loc,
323 "Variable {0} is captured in an anonymous method and its address is also being taken: they are exclusive", name);
328 // This will emit the code for the delegate, as well delegate creation on the host
330 public class AnonymousDelegate : DelegateCreation {
333 public AnonymousDelegate (AnonymousMethod am, Type target_type, Location l)
340 public override Expression DoResolve (EmitContext ec)
342 eclass = ExprClass.Value;
347 public override void Emit (EmitContext ec)
349 if (!am.EmitMethod (ec))
353 // Now emit the delegate creation.
355 if ((am.method.ModFlags & Modifiers.STATIC) == 0)
356 delegate_instance_expression = new AnonymousInstance (am);
358 Expression ml = Expression.MemberLookup (ec, type, ".ctor", loc);
359 constructor_method = ((MethodGroupExpr) ml).Methods [0];
360 delegate_method = am.GetMethodBuilder ();
364 class AnonymousInstance : Expression {
367 public AnonymousInstance (AnonymousMethod am)
370 eclass = ExprClass.Value;
373 public override Expression DoResolve (EmitContext ec)
378 public override void Emit (EmitContext ec)
380 am.aec.EmitMethodHostInstance (ec, am);
385 class CapturedParameter {
387 public FieldBuilder FieldBuilder;
390 public CapturedParameter (Type type, int idx)
398 // Here we cluster all the variables captured on a given scope, we also
399 // keep some extra information that might be required on each scope.
401 public class ScopeInfo {
402 public CaptureContext CaptureContext;
403 public ScopeInfo ParentScope;
404 public Block ScopeBlock;
405 public bool NeedThis = false;
406 public bool HostsParameters = false;
408 // For tracking the number of scopes created.
413 ArrayList locals = new ArrayList ();
414 ArrayList children = new ArrayList ();
417 // The types and fields generated
419 public TypeBuilder ScopeTypeBuilder;
420 public ConstructorBuilder ScopeConstructor;
421 public FieldBuilder THIS;
422 public FieldBuilder ParentLink;
425 // Points to the object of type `ScopeTypeBuilder' that
426 // holds the data for the scope
428 public LocalBuilder ScopeInstance;
431 public ScopeInfo (CaptureContext cc, Block b)
440 public void AddLocal (LocalInfo li)
442 if (locals.Contains (li))
448 public bool IsCaptured (LocalInfo li)
450 return locals.Contains (li);
453 public void AddChild (ScopeInfo si)
455 if (children.Contains (si))
460 static int indent = 0;
464 for (int i = 0; i < indent; i++)
470 //Console.WriteLine (Environment.StackTrace);
472 Console.WriteLine ("START");
475 Console.WriteLine ("NeedThis=" + NeedThis);
476 foreach (LocalInfo li in locals){
478 Console.WriteLine ("var {0}", li.Name);
481 foreach (ScopeInfo si in children)
485 Console.WriteLine ("END");
488 public string MakeHelperName ()
490 return String.Format ("<>AnonHelp<{0}>", id);
493 public void EmitScopeConstructor ()
495 Type [] constructor_types = TypeManager.NoTypes;
496 Parameters constructor_parameters = Parameters.EmptyReadOnlyParameters;
497 ScopeConstructor = ScopeTypeBuilder.DefineConstructor (
498 MethodAttributes.Public | MethodAttributes.HideBySig |
499 MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
500 CallingConventions.HasThis, constructor_types);
501 InternalParameters parameter_info = new InternalParameters (constructor_types, constructor_parameters);
502 TypeManager.RegisterMethod (ScopeConstructor, parameter_info, constructor_types);
504 ILGenerator cig = ScopeConstructor.GetILGenerator ();
505 cig.Emit (OpCodes.Ldarg_0);
506 cig.Emit (OpCodes.Call, TypeManager.object_ctor);
507 cig.Emit (OpCodes.Ret);
510 public void EmitScopeType (EmitContext ec)
514 if (ScopeTypeBuilder != null)
517 TypeBuilder container = ec.TypeContainer.TypeBuilder;
519 ScopeTypeBuilder = container.DefineNestedType (
520 MakeHelperName (), TypeAttributes.AutoLayout | TypeAttributes.Class | TypeAttributes.NestedAssembly,
521 TypeManager.object_type, null);
524 THIS = ScopeTypeBuilder.DefineField ("<>THIS", container, FieldAttributes.Assembly);
526 if (ParentScope != null){
527 if (ParentScope.ScopeTypeBuilder == null){
528 throw new Exception (String.Format ("My parent has not been initialized {0} and {1}", ParentScope, this));
531 ParentLink = ScopeTypeBuilder.DefineField ("<>parent", ParentScope.ScopeTypeBuilder,
532 FieldAttributes.Assembly);
535 if (NeedThis && ParentScope != null)
536 throw new Exception ("I was not expecting THIS && having a parent");
538 foreach (LocalInfo info in locals){
539 info.FieldBuilder = ScopeTypeBuilder.DefineField (
540 info.Name, info.VariableType, FieldAttributes.Assembly);
543 if (HostsParameters){
544 Hashtable captured_parameters = CaptureContext.captured_parameters;
546 foreach (DictionaryEntry de in captured_parameters){
547 string name = (string) de.Key;
548 CapturedParameter cp = (CapturedParameter) de.Value;
551 fb = ScopeTypeBuilder.DefineField ("<p:" + name + ">", cp.Type, FieldAttributes.Assembly);
552 cp.FieldBuilder = fb;
556 EmitScopeConstructor ();
557 foreach (ScopeInfo si in children){
558 si.EmitScopeType (ec);
562 public void CloseTypes ()
564 RootContext.RegisterCompilerGeneratedType (ScopeTypeBuilder);
565 foreach (ScopeInfo si in children)
570 // Emits the initialization code for the scope
572 public void EmitInitScope (EmitContext ec)
574 ILGenerator ig = ec.ig;
579 ig.Emit (OpCodes.Newobj, (ConstructorInfo) ScopeConstructor);
580 ScopeInstance = ig.DeclareLocal (ScopeTypeBuilder);
581 ig.Emit (OpCodes.Stloc, ScopeInstance);
584 ig.Emit (OpCodes.Ldloc, ScopeInstance);
585 ig.Emit (OpCodes.Ldarg_0);
586 ig.Emit (OpCodes.Stfld, THIS);
590 // Copy the parameter values, if any
592 int extra = ec.IsStatic ? 0 : 1;
593 if (HostsParameters){
594 Hashtable captured_parameters = CaptureContext.captured_parameters;
596 foreach (DictionaryEntry de in captured_parameters){
597 CapturedParameter cp = (CapturedParameter) de.Value;
599 ig.Emit (OpCodes.Ldloc, ScopeInstance);
600 ParameterReference.EmitLdArg (ig, cp.Idx + extra);
601 ig.Emit (OpCodes.Stfld, cp.FieldBuilder);
605 if (ParentScope != null){
606 if (!ParentScope.inited)
607 ParentScope.EmitInitScope (ec);
610 // Only emit initialization in our capturecontext world
612 if (ParentScope.CaptureContext == CaptureContext){
613 ig.Emit (OpCodes.Ldloc, ScopeInstance);
614 ig.Emit (OpCodes.Ldloc, ParentScope.ScopeInstance);
615 ig.Emit (OpCodes.Stfld, ParentLink);
617 ig.Emit (OpCodes.Ldloc, ScopeInstance);
618 ig.Emit (OpCodes.Ldarg_0);
619 ig.Emit (OpCodes.Stfld, ParentLink);
625 static void DoPath (StringBuilder sb, ScopeInfo start)
627 if (start.ParentScope != null){
628 DoPath (sb, start.ParentScope);
631 sb.Append ((start.id).ToString ());
634 public override string ToString ()
636 StringBuilder sb = new StringBuilder ();
639 if (CaptureContext != null){
640 sb.Append (CaptureContext.ToString ());
647 return sb.ToString ();
652 // CaptureContext objects are created on demand if a method has
653 // anonymous methods and kept on the ToplevelBlock.
655 // If they exist, all ToplevelBlocks in the containing block are
656 // linked together (children pointing to their parents).
658 public class CaptureContext {
659 public static int count;
664 // Points to the toplevel block that owns this CaptureContext
666 ToplevelBlock toplevel_owner;
667 Hashtable scopes = new Hashtable ();
668 bool have_captured_vars = false;
669 bool referenced_this = false;
670 ScopeInfo topmost = null;
675 Hashtable captured_fields = new Hashtable ();
676 Hashtable captured_variables = new Hashtable ();
677 public Hashtable captured_parameters = new Hashtable ();
678 public AnonymousMethod Host;
680 public CaptureContext (ToplevelBlock toplevel_owner, Location loc, AnonymousMethod host)
683 this.toplevel_owner = toplevel_owner;
690 void DoPath (StringBuilder sb, CaptureContext cc)
692 if (cc.ParentCaptureContext != null){
693 DoPath (sb, cc.ParentCaptureContext);
696 sb.Append (cc_id.ToString ());
699 public override string ToString ()
701 StringBuilder sb = new StringBuilder ();
705 return sb.ToString ();
708 public ToplevelBlock ParentToplevel {
710 return toplevel_owner.Container;
714 public CaptureContext ParentCaptureContext {
716 ToplevelBlock parent = ParentToplevel;
718 return (parent == null) ? null : parent.CaptureContext;
722 // Returns the deepest of two scopes
723 public ScopeInfo Deepest (ScopeInfo a, ScopeInfo b)
735 // If they Scopes are on the same CaptureContext, we do the double
736 // checks just so if there is an invariant change in the future,
737 // we get the exception at the end
739 for (p = a; p != null; p = p.ParentScope)
743 for (p = b; p != null; p = p.ParentScope)
747 CaptureContext ca = a.CaptureContext;
748 CaptureContext cb = b.CaptureContext;
750 for (CaptureContext c = ca; c != null; c = c.ParentCaptureContext)
754 for (CaptureContext c = cb; c != null; c = c.ParentCaptureContext)
757 throw new Exception ("Should never be reached");
760 void AdjustMethodScope (AnonymousMethod am, ScopeInfo scope)
762 am.Scope = Deepest (am.Scope, scope);
765 void LinkScope (ScopeInfo scope, int id)
767 ScopeInfo parent = (ScopeInfo) scopes [id];
768 scope.ParentScope = parent;
769 parent.AddChild (scope);
771 if (scope == topmost)
775 public void AddLocal (AnonymousMethod am, LocalInfo li)
777 if (li.Block.Toplevel != toplevel_owner){
778 ParentCaptureContext.AddLocal (am, li);
781 int block_id = li.Block.ID;
783 if (scopes [block_id] == null){
784 scope = new ScopeInfo (this, li.Block);
785 scopes [block_id] = scope;
787 scope = (ScopeInfo) scopes [block_id];
789 if (topmost == null){
793 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
794 if (scopes [b.ID] != null){
795 LinkScope (scope, b.ID);
800 if (scope.ParentScope == null && ParentCaptureContext != null){
801 CaptureContext pcc = ParentCaptureContext;
803 for (Block b = am.ContainingBlock; b != null; b = b.Parent){
804 if (pcc.scopes [b.ID] != null){
805 pcc.LinkScope (scope, b.ID);
816 AdjustMethodScope (Host, topmost);
821 AdjustMethodScope (am, scope);
823 if (captured_variables [li] != null)
826 have_captured_vars = true;
827 captured_variables [li] = li;
832 // Retursn the CaptureContext for the block that defines the parameter `name'
834 static CaptureContext _ContextForParameter (ToplevelBlock current, string name)
836 ToplevelBlock container = current.Container;
837 if (container != null){
838 CaptureContext cc = _ContextForParameter (container, name);
842 if (current.IsParameterReference (name))
843 return current.ToplevelBlockCaptureContext;
847 static CaptureContext ContextForParameter (ToplevelBlock current, string name)
849 CaptureContext cc = _ContextForParameter (current, name);
851 throw new Exception (String.Format ("request for parameteter {0} failed: not found", name));
856 // Records the captured parameter at the appropriate CaptureContext
858 public void AddParameter (EmitContext ec, AnonymousMethod am, string name, Type t, int idx)
860 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
862 cc.AddParameterToContext (am, name, t, idx);
866 // Records the parameters in the context
868 void AddParameterToContext (AnonymousMethod am, string name, Type t, int idx)
870 if (captured_parameters == null)
871 captured_parameters = new Hashtable ();
872 if (captured_parameters [name] != null)
874 captured_parameters [name] = new CapturedParameter (t, idx);
876 if (topmost == null){
878 // Create one ScopeInfo, if there are none.
880 topmost = new ScopeInfo (this, toplevel_owner);
881 scopes [toplevel_owner.ID] = topmost;
884 // If the topmost ScopeInfo is not at the topblock level, insert
885 // a new ScopeInfo there.
887 if (topmost.ScopeBlock != toplevel_owner){
888 ScopeInfo par_si = new ScopeInfo (this, toplevel_owner);
889 scopes [toplevel_owner.ID] = topmost;
890 topmost.ParentScope = par_si;
895 topmost.HostsParameters = true;
896 AdjustMethodScope (am, topmost);
900 // Captured fields are only recorded on the topmost CaptureContext, because that
901 // one is the one linked to the owner of instance fields
903 public void AddField (FieldExpr fe)
905 if (fe.FieldInfo.IsStatic)
906 throw new Exception ("Attempt to register a static field as a captured field");
908 CaptureContext parent = ParentCaptureContext;
910 parent.AddField (fe);
912 captured_fields [fe] = fe;
915 public void CaptureThis ()
917 CaptureContext parent = ParentCaptureContext;
919 parent.CaptureThis ();
920 referenced_this = true;
923 public bool HaveCapturedVariables {
925 return have_captured_vars;
929 public bool HaveCapturedFields {
931 CaptureContext parent = ParentCaptureContext;
933 return parent.HaveCapturedFields;
934 return captured_fields.Count > 0;
938 public bool IsCaptured (LocalInfo local)
940 foreach (ScopeInfo si in scopes.Values){
941 if (si.IsCaptured (local))
948 // Returns whether the parameter is captured
950 public bool IsParameterCaptured (string name)
952 if (ParentCaptureContext != null && ParentCaptureContext.IsParameterCaptured (name))
955 if (captured_parameters != null)
956 return captured_parameters [name] != null;
960 public void EmitAnonymousHelperClasses (EmitContext ec)
962 if (topmost != null){
963 topmost.NeedThis = HaveCapturedFields || referenced_this;
964 topmost.EmitScopeType (ec);
968 public void CloseAnonymousHelperClasses ()
971 topmost.CloseTypes ();
974 ScopeInfo GetScopeFromBlock (EmitContext ec, Block b)
978 si = (ScopeInfo) scopes [b.ID];
980 throw new Exception ("Si is null for block " + b.ID);
981 si.EmitInitScope (ec);
987 // Emits the opcodes necessary to load the instance of the captured
990 public void EmitCapturedVariableInstance (EmitContext ec, LocalInfo li, AnonymousMethod am)
992 ILGenerator ig = ec.ig;
995 if (li.Block.Toplevel == toplevel_owner){
996 si = GetScopeFromBlock (ec, li.Block);
997 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1002 ig.Emit (OpCodes.Ldarg_0);
1004 while (si.ScopeBlock.ID != li.Block.ID){
1005 if (si.ParentLink != null)
1006 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1007 si = si.ParentScope;
1009 throw new Exception (
1010 String.Format ("Never found block {0} starting at {1} while looking up {2}",
1011 li.Block.ID, am.Scope.ScopeBlock.ID, li.Name));
1017 // Internal routine that loads the instance to reach parameter `name'
1019 void EmitParameterInstance (EmitContext ec, string name)
1021 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1023 cc.EmitParameterInstance (ec, name);
1027 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1028 if (par_info != null){
1030 // FIXME: implementing this.
1033 ILGenerator ig = ec.ig;
1036 if (ec.CurrentBlock == toplevel_owner){
1037 si = GetScopeFromBlock (ec, toplevel_owner);
1038 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1042 si = ec.CurrentAnonymousMethod.Scope;
1043 ig.Emit (OpCodes.Ldarg_0);
1045 while (si.ParentLink != null) {
1046 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1047 si = si.ParentScope;
1053 // Emits the code necessary to load the parameter named `name' within
1054 // an anonymous method.
1056 public void EmitParameter (EmitContext ec, string name)
1058 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1060 cc.EmitParameter (ec, name);
1063 EmitParameterInstance (ec, name);
1064 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1065 if (par_info != null){
1067 // FIXME: implementing this.
1070 ec.ig.Emit (OpCodes.Ldfld, par_info.FieldBuilder);
1074 // Implements the assignment of `source' to the paramenter named `name' within
1075 // an anonymous method.
1077 public void EmitAssignParameter (EmitContext ec, string name, Expression source, bool leave_copy, bool prepare_for_load)
1079 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1081 cc.EmitAssignParameter (ec, name, source, leave_copy, prepare_for_load);
1084 ILGenerator ig = ec.ig;
1085 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1087 EmitParameterInstance (ec, name);
1090 ig.Emit (OpCodes.Dup);
1091 ig.Emit (OpCodes.Stfld, par_info.FieldBuilder);
1095 // Emits the address for the parameter named `name' within
1096 // an anonymous method.
1098 public void EmitAddressOfParameter (EmitContext ec, string name)
1100 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1102 cc.EmitAddressOfParameter (ec, name);
1105 EmitParameterInstance (ec, name);
1106 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1107 ec.ig.Emit (OpCodes.Ldflda, par_info.FieldBuilder);
1111 // The following methods are only invoked on the host for the
1112 // anonymous method.
1114 public void EmitMethodHostInstance (EmitContext target, AnonymousMethod am)
1116 ILGenerator ig = target.ig;
1117 ScopeInfo si = am.Scope;
1120 ig.Emit (OpCodes.Ldarg_0);
1124 si.EmitInitScope (target);
1125 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1128 ArrayList all_scopes = new ArrayList ();
1130 public void AddScope (ScopeInfo si)
1132 all_scopes.Add (si);
1133 toplevel_owner.RegisterCaptureContext (this);
1137 // Links any scopes that were not linked previously
1139 public void AdjustScopes ()
1141 foreach (ScopeInfo scope in all_scopes){
1142 if (scope.ParentScope != null)
1145 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
1146 if (scopes [b.ID] != null){
1147 LinkScope (scope, b.ID);
1152 if (scope.ParentScope == null && ParentCaptureContext != null){
1153 CaptureContext pcc = ParentCaptureContext;
1155 for (Block b = Host.ContainingBlock; b != null; b = b.Parent){
1156 if (pcc.scopes [b.ID] != null){
1157 pcc.LinkScope (scope, b.ID);