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.INTERNAL;
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,
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 invoke_mb = (MethodInfo) Delegate.GetInvokeMethod (ec, delegate_type, loc);
168 ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
171 // If implicit parameters are set, then we must check for out in the parameters
172 // and flag it accordingly.
174 bool out_invalid_check = false;
176 if (Parameters == null){
178 out_invalid_check = true;
181 // We provide a set of inaccessible parameters
184 for (i = 0; i < invoke_pd.Count; i++){
185 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
188 int n = invoke_pd.Count - (params_idx != -1 ? 1 : 0);
189 Parameter [] fixedpars = new Parameter [n];
191 for (i = j = 0; i < invoke_pd.Count; i++){
192 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
194 fixedpars [j] = new Parameter (
195 new TypeExpression (invoke_pd.ParameterType (i), loc),
196 "+" + j, invoke_pd.ParameterModifier (i), null);
200 Parameter variable = null;
201 if (params_idx != -1){
202 variable = new Parameter (
203 new TypeExpression (invoke_pd.ParameterType (params_idx), loc),
204 "+" + params_idx, invoke_pd.ParameterModifier (params_idx), null);
207 Parameters = new Parameters (fixedpars, variable, loc);
211 // First, parameter types of `delegate_type' must be compatible
212 // with the anonymous method.
214 amp = new InternalParameters (Parameters.GetParameterInfo (ec), Parameters);
216 if (amp.Count != invoke_pd.Count){
218 Report.Error (1593, loc,
219 "Anonymous method has {0} parameters, while delegate requires {1}",
220 amp.Count, invoke_pd.Count);
221 Error_ParameterMismatch (delegate_type);
226 for (int i = 0; i < amp.Count; i++){
227 Parameter.Modifier amp_mod = amp.ParameterModifier (i);
228 if (amp_mod != invoke_pd.ParameterModifier (i)){
230 Report.Error (1676, loc,
231 "Signature mismatch in parameter modifier for parameter #0", i + 1);
232 Error_ParameterMismatch (delegate_type);
237 if (amp.ParameterType (i) != invoke_pd.ParameterType (i)){
239 Report.Error (1678, loc,
240 "Signature mismatch in parameter {0}: need `{1}' got `{2}'", i + 1,
241 TypeManager.CSharpName (invoke_pd.ParameterType (i)),
242 TypeManager.CSharpName (amp.ParameterType (i)));
243 Error_ParameterMismatch (delegate_type);
248 if (out_invalid_check && (invoke_pd.ParameterModifier (i) & Parameter.Modifier.OUT) != 0){
250 Report.Error (1676, loc,"Parameter {0} must include the `out' modifier ", i+1);
251 Error_ParameterMismatch (delegate_type);
258 // If we are only probing, return ourselves
264 // Second: the return type of the delegate must be compatible with
265 // the anonymous type. Instead of doing a pass to examine the block
266 // we satisfy the rule by setting the return type on the EmitContext
267 // to be the delegate type return type.
270 //MethodBuilder builder = method_data.MethodBuilder;
271 //ILGenerator ig = builder.GetILGenerator ();
274 aec = new EmitContext (
275 ec.TypeContainer, ec.DeclSpace, loc, null,
276 invoke_mb.ReturnType,
277 /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
278 (ec.InUnsafe ? Modifiers.UNSAFE : 0) |
279 (ec.IsStatic ? Modifiers.STATIC : 0),
280 /* No constructor */ false);
282 aec.CurrentAnonymousMethod = this;
283 ContainerAnonymousMethod = ec.CurrentAnonymousMethod;
284 ContainingBlock = ec.CurrentBlock;
286 if (aec.ResolveTopBlock (ec, Block, amp, loc, out unreachable))
287 return new AnonymousDelegate (this, delegate_type, loc).Resolve (ec);
292 public MethodBuilder GetMethodBuilder ()
294 return method.MethodData.MethodBuilder;
297 public void EmitMethod (EmitContext ec)
299 if (!CreateMethodHost (ec, invoke_mb.ReturnType))
302 MethodBuilder builder = GetMethodBuilder ();
303 ILGenerator ig = builder.GetILGenerator ();
306 Parameters.LabelParameters (aec, builder, loc);
309 // Adjust based on the computed state of the
310 // method from CreateMethodHost
312 aec.MethodIsStatic = (method_modifiers & Modifiers.STATIC) != 0;
314 aec.EmitMeta (Block, amp);
315 aec.EmitResolvedTopBlock (Block, unreachable);
318 public static void Error_AddressOfCapturedVar (string name, Location loc)
320 Report.Error (1686, loc,
321 "Variable {0} is captured in an anonymous method and its address is also being taken: they are exclusive", name);
326 // This will emit the code for the delegate, as well delegate creation on the host
328 public class AnonymousDelegate : DelegateCreation {
331 public AnonymousDelegate (AnonymousMethod am, Type target_type, Location l)
338 public override Expression DoResolve (EmitContext ec)
340 eclass = ExprClass.Value;
344 public override void Emit (EmitContext ec)
349 // Now emit the delegate creation.
351 if ((am.method.ModFlags & Modifiers.STATIC) == 0)
352 delegate_instance_expression = new AnonymousInstance (am);
354 Expression ml = Expression.MemberLookup (ec, type, ".ctor", loc);
355 constructor_method = ((MethodGroupExpr) ml).Methods [0];
356 delegate_method = am.GetMethodBuilder ();
360 class AnonymousInstance : Expression {
363 public AnonymousInstance (AnonymousMethod am)
366 eclass = ExprClass.Value;
369 public override Expression DoResolve (EmitContext ec)
374 public override void Emit (EmitContext ec)
376 am.aec.EmitMethodHostInstance (ec, am);
381 class CapturedParameter {
383 public FieldBuilder FieldBuilder;
386 public CapturedParameter (Type type, int idx)
394 // Here we cluster all the variables captured on a given scope, we also
395 // keep some extra information that might be required on each scope.
397 public class ScopeInfo {
398 public CaptureContext CaptureContext;
399 public ScopeInfo ParentScope;
400 public Block ScopeBlock;
401 public bool NeedThis = false;
402 public bool HostsParameters = false;
404 // For tracking the number of scopes created.
409 ArrayList locals = new ArrayList ();
410 ArrayList children = new ArrayList ();
413 // The types and fields generated
415 public TypeBuilder ScopeTypeBuilder;
416 public ConstructorBuilder ScopeConstructor;
417 public FieldBuilder THIS;
418 public FieldBuilder ParentLink;
421 // Points to the object of type `ScopeTypeBuilder' that
422 // holds the data for the scope
424 public LocalBuilder ScopeInstance;
427 public ScopeInfo (CaptureContext cc, Block b)
436 public void AddLocal (LocalInfo li)
438 if (locals.Contains (li))
444 public bool IsCaptured (LocalInfo li)
446 return locals.Contains (li);
449 public void AddChild (ScopeInfo si)
451 if (children.Contains (si))
456 static int indent = 0;
460 for (int i = 0; i < indent; i++)
466 //Console.WriteLine (Environment.StackTrace);
468 Console.WriteLine ("START");
471 Console.WriteLine ("NeedThis=" + NeedThis);
472 foreach (LocalInfo li in locals){
474 Console.WriteLine ("var {0}", li.Name);
477 foreach (ScopeInfo si in children)
481 Console.WriteLine ("END");
484 public string MakeHelperName ()
486 return String.Format ("<>AnonHelp<{0}>", id);
489 public void EmitScopeConstructor ()
491 Type [] constructor_types = TypeManager.NoTypes;
492 Parameters constructor_parameters = Parameters.EmptyReadOnlyParameters;
493 ScopeConstructor = ScopeTypeBuilder.DefineConstructor (
494 MethodAttributes.Public | MethodAttributes.HideBySig |
495 MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
496 CallingConventions.HasThis, constructor_types);
497 InternalParameters parameter_info = new InternalParameters (constructor_types, constructor_parameters);
498 TypeManager.RegisterMethod (ScopeConstructor, parameter_info, constructor_types);
500 ILGenerator cig = ScopeConstructor.GetILGenerator ();
501 cig.Emit (OpCodes.Ldarg_0);
502 cig.Emit (OpCodes.Call, TypeManager.object_ctor);
503 cig.Emit (OpCodes.Ret);
506 public void EmitScopeType (EmitContext ec)
510 if (ScopeTypeBuilder != null)
513 TypeBuilder container = ec.TypeContainer.TypeBuilder;
515 ScopeTypeBuilder = container.DefineNestedType (
516 MakeHelperName (), TypeAttributes.AutoLayout | TypeAttributes.Class | TypeAttributes.NestedAssembly,
517 TypeManager.object_type, null);
520 THIS = ScopeTypeBuilder.DefineField ("<>THIS", container, FieldAttributes.Assembly);
522 if (ParentScope != null){
523 if (ParentScope.ScopeTypeBuilder == null){
524 throw new Exception (String.Format ("My parent has not been initialized {0} and {1}", ParentScope, this));
527 ParentLink = ScopeTypeBuilder.DefineField ("<>parent", ParentScope.ScopeTypeBuilder,
528 FieldAttributes.Assembly);
531 if (NeedThis && ParentScope != null)
532 throw new Exception ("I was not expecting THIS && having a parent");
534 foreach (LocalInfo info in locals){
535 info.FieldBuilder = ScopeTypeBuilder.DefineField (
536 info.Name, info.VariableType, FieldAttributes.Assembly);
539 if (HostsParameters){
540 Hashtable captured_parameters = CaptureContext.captured_parameters;
542 foreach (DictionaryEntry de in captured_parameters){
543 string name = (string) de.Key;
544 CapturedParameter cp = (CapturedParameter) de.Value;
547 fb = ScopeTypeBuilder.DefineField ("<p:" + name + ">", cp.Type, FieldAttributes.Assembly);
548 cp.FieldBuilder = fb;
552 EmitScopeConstructor ();
553 foreach (ScopeInfo si in children){
554 si.EmitScopeType (ec);
558 public void CloseTypes ()
560 RootContext.RegisterCompilerGeneratedType (ScopeTypeBuilder);
561 foreach (ScopeInfo si in children)
566 // Emits the initialization code for the scope
568 public void EmitInitScope (EmitContext ec)
570 ILGenerator ig = ec.ig;
575 ig.Emit (OpCodes.Newobj, (ConstructorInfo) ScopeConstructor);
576 ScopeInstance = ig.DeclareLocal (ScopeTypeBuilder);
577 ig.Emit (OpCodes.Stloc, ScopeInstance);
580 ig.Emit (OpCodes.Ldloc, ScopeInstance);
581 ig.Emit (OpCodes.Ldarg_0);
582 ig.Emit (OpCodes.Stfld, THIS);
586 // Copy the parameter values, if any
588 int extra = ec.IsStatic ? 0 : 1;
589 if (HostsParameters){
590 Hashtable captured_parameters = CaptureContext.captured_parameters;
592 foreach (DictionaryEntry de in captured_parameters){
593 CapturedParameter cp = (CapturedParameter) de.Value;
595 ig.Emit (OpCodes.Ldloc, ScopeInstance);
596 ParameterReference.EmitLdArg (ig, cp.Idx + extra);
597 ig.Emit (OpCodes.Stfld, cp.FieldBuilder);
601 if (ParentScope != null){
602 if (!ParentScope.inited)
603 ParentScope.EmitInitScope (ec);
606 // Only emit initialization in our capturecontext world
608 if (ParentScope.CaptureContext == CaptureContext){
609 ig.Emit (OpCodes.Ldloc, ScopeInstance);
610 ig.Emit (OpCodes.Ldloc, ParentScope.ScopeInstance);
611 ig.Emit (OpCodes.Stfld, ParentLink);
613 ig.Emit (OpCodes.Ldloc, ScopeInstance);
614 ig.Emit (OpCodes.Ldarg_0);
615 ig.Emit (OpCodes.Stfld, ParentLink);
621 static void DoPath (StringBuilder sb, ScopeInfo start)
623 if (start.ParentScope != null){
624 DoPath (sb, start.ParentScope);
627 sb.Append ((start.id).ToString ());
630 public override string ToString ()
632 StringBuilder sb = new StringBuilder ();
635 if (CaptureContext != null){
636 sb.Append (CaptureContext.ToString ());
643 return sb.ToString ();
648 // CaptureContext objects are created on demand if a method has
649 // anonymous methods and kept on the ToplevelBlock.
651 // If they exist, all ToplevelBlocks in the containing block are
652 // linked together (children pointing to their parents).
654 public class CaptureContext {
655 public static int count;
660 // Points to the toplevel block that owns this CaptureContext
662 ToplevelBlock toplevel_owner;
663 Hashtable scopes = new Hashtable ();
664 bool have_captured_vars = false;
665 bool referenced_this = false;
666 ScopeInfo topmost = null;
671 Hashtable captured_fields = new Hashtable ();
672 Hashtable captured_variables = new Hashtable ();
673 public Hashtable captured_parameters = new Hashtable ();
674 public AnonymousMethod Host;
676 public CaptureContext (ToplevelBlock toplevel_owner, Location loc, AnonymousMethod host)
679 this.toplevel_owner = toplevel_owner;
686 void DoPath (StringBuilder sb, CaptureContext cc)
688 if (cc.ParentCaptureContext != null){
689 DoPath (sb, cc.ParentCaptureContext);
692 sb.Append (cc_id.ToString ());
695 public override string ToString ()
697 StringBuilder sb = new StringBuilder ();
701 return sb.ToString ();
704 public ToplevelBlock ParentToplevel {
706 return toplevel_owner.Container;
710 public CaptureContext ParentCaptureContext {
712 ToplevelBlock parent = ParentToplevel;
714 return (parent == null) ? null : parent.CaptureContext;
718 // Returns the deepest of two scopes
719 public ScopeInfo Deepest (ScopeInfo a, ScopeInfo b)
731 // If they Scopes are on the same CaptureContext, we do the double
732 // checks just so if there is an invariant change in the future,
733 // we get the exception at the end
735 for (p = a; p != null; p = p.ParentScope)
739 for (p = b; p != null; p = p.ParentScope)
743 CaptureContext ca = a.CaptureContext;
744 CaptureContext cb = b.CaptureContext;
746 for (CaptureContext c = ca; c != null; c = c.ParentCaptureContext)
750 for (CaptureContext c = cb; c != null; c = c.ParentCaptureContext)
753 throw new Exception ("Should never be reached");
756 void AdjustMethodScope (AnonymousMethod am, ScopeInfo scope)
758 am.Scope = Deepest (am.Scope, scope);
761 void LinkScope (ScopeInfo scope, int id)
763 ScopeInfo parent = (ScopeInfo) scopes [id];
764 scope.ParentScope = parent;
765 parent.AddChild (scope);
767 if (scope == topmost)
771 public void AddLocal (AnonymousMethod am, LocalInfo li)
773 if (li.Block.Toplevel != toplevel_owner){
774 ParentCaptureContext.AddLocal (am, li);
777 int block_id = li.Block.ID;
779 if (scopes [block_id] == null){
780 scope = new ScopeInfo (this, li.Block);
781 scopes [block_id] = scope;
783 scope = (ScopeInfo) scopes [block_id];
785 if (topmost == null){
789 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
790 if (scopes [b.ID] != null){
791 LinkScope (scope, b.ID);
796 if (scope.ParentScope == null && ParentCaptureContext != null){
797 CaptureContext pcc = ParentCaptureContext;
799 for (Block b = am.ContainingBlock; b != null; b = b.Parent){
800 if (pcc.scopes [b.ID] != null){
801 pcc.LinkScope (scope, b.ID);
812 AdjustMethodScope (Host, topmost);
817 AdjustMethodScope (am, scope);
819 if (captured_variables [li] != null)
822 have_captured_vars = true;
823 captured_variables [li] = li;
828 // Retursn the CaptureContext for the block that defines the parameter `name'
830 static CaptureContext _ContextForParameter (ToplevelBlock current, string name)
832 ToplevelBlock container = current.Container;
833 if (container != null){
834 CaptureContext cc = _ContextForParameter (container, name);
838 if (current.IsParameterReference (name))
839 return current.ToplevelBlockCaptureContext;
843 static CaptureContext ContextForParameter (ToplevelBlock current, string name)
845 CaptureContext cc = _ContextForParameter (current, name);
847 throw new Exception (String.Format ("request for parameteter {0} failed: not found", name));
852 // Records the captured parameter at the appropriate CaptureContext
854 public void AddParameter (EmitContext ec, AnonymousMethod am, string name, Type t, int idx)
856 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
858 cc.AddParameterToContext (am, name, t, idx);
862 // Records the parameters in the context
864 void AddParameterToContext (AnonymousMethod am, string name, Type t, int idx)
866 if (captured_parameters == null)
867 captured_parameters = new Hashtable ();
868 if (captured_parameters [name] != null)
870 captured_parameters [name] = new CapturedParameter (t, idx);
872 if (topmost == null){
874 // Create one ScopeInfo, if there are none.
876 topmost = new ScopeInfo (this, toplevel_owner);
877 scopes [toplevel_owner.ID] = topmost;
880 // If the topmost ScopeInfo is not at the topblock level, insert
881 // a new ScopeInfo there.
883 if (topmost.ScopeBlock != toplevel_owner){
884 ScopeInfo par_si = new ScopeInfo (this, toplevel_owner);
885 scopes [toplevel_owner.ID] = topmost;
886 topmost.ParentScope = par_si;
891 topmost.HostsParameters = true;
892 AdjustMethodScope (am, topmost);
896 // Captured fields are only recorded on the topmost CaptureContext, because that
897 // one is the one linked to the owner of instance fields
899 public void AddField (FieldExpr fe)
901 if (fe.FieldInfo.IsStatic)
902 throw new Exception ("Attempt to register a static field as a captured field");
904 CaptureContext parent = ParentCaptureContext;
906 parent.AddField (fe);
908 captured_fields [fe] = fe;
911 public void CaptureThis ()
913 CaptureContext parent = ParentCaptureContext;
915 parent.CaptureThis ();
916 referenced_this = true;
919 public bool HaveCapturedVariables {
921 return have_captured_vars;
925 public bool HaveCapturedFields {
927 CaptureContext parent = ParentCaptureContext;
929 return parent.HaveCapturedFields;
930 return captured_fields.Count > 0;
934 public bool IsCaptured (LocalInfo local)
936 foreach (ScopeInfo si in scopes.Values){
937 if (si.IsCaptured (local))
944 // Returns whether the parameter is captured
946 public bool IsParameterCaptured (string name)
948 if (ParentCaptureContext != null && ParentCaptureContext.IsParameterCaptured (name))
951 if (captured_parameters != null)
952 return captured_parameters [name] != null;
956 public void EmitAnonymousHelperClasses (EmitContext ec)
958 if (topmost != null){
959 topmost.NeedThis = HaveCapturedFields || referenced_this;
960 topmost.EmitScopeType (ec);
964 public void CloseAnonymousHelperClasses ()
967 topmost.CloseTypes ();
970 ScopeInfo GetScopeFromBlock (EmitContext ec, Block b)
974 si = (ScopeInfo) scopes [b.ID];
976 throw new Exception ("Si is null for block " + b.ID);
977 si.EmitInitScope (ec);
983 // Emits the opcodes necessary to load the instance of the captured
986 public void EmitCapturedVariableInstance (EmitContext ec, LocalInfo li, AnonymousMethod am)
988 ILGenerator ig = ec.ig;
991 if (li.Block.Toplevel == toplevel_owner){
992 si = GetScopeFromBlock (ec, li.Block);
993 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
998 ig.Emit (OpCodes.Ldarg_0);
1000 while (si.ScopeBlock.ID != li.Block.ID){
1001 if (si.ParentLink != null)
1002 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1003 si = si.ParentScope;
1005 throw new Exception (
1006 String.Format ("Never found block {0} starting at {1} while looking up {2}",
1007 li.Block.ID, am.Scope.ScopeBlock.ID, li.Name));
1013 // Internal routine that loads the instance to reach parameter `name'
1015 void EmitParameterInstance (EmitContext ec, string name)
1017 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1019 cc.EmitParameterInstance (ec, name);
1023 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1024 if (par_info != null){
1026 // FIXME: implementing this.
1029 ILGenerator ig = ec.ig;
1032 if (ec.CurrentBlock == toplevel_owner){
1033 si = GetScopeFromBlock (ec, toplevel_owner);
1034 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1038 si = ec.CurrentAnonymousMethod.Scope;
1039 ig.Emit (OpCodes.Ldarg_0);
1041 while (si.ParentLink != null) {
1042 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1043 si = si.ParentScope;
1049 // Emits the code necessary to load the parameter named `name' within
1050 // an anonymous method.
1052 public void EmitParameter (EmitContext ec, string name)
1054 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1056 cc.EmitParameter (ec, name);
1059 EmitParameterInstance (ec, name);
1060 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1061 if (par_info != null){
1063 // FIXME: implementing this.
1066 ec.ig.Emit (OpCodes.Ldfld, par_info.FieldBuilder);
1070 // Implements the assignment of `source' to the paramenter named `name' within
1071 // an anonymous method.
1073 public void EmitAssignParameter (EmitContext ec, string name, Expression source, bool leave_copy, bool prepare_for_load)
1075 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1077 cc.EmitAssignParameter (ec, name, source, leave_copy, prepare_for_load);
1080 ILGenerator ig = ec.ig;
1081 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1083 EmitParameterInstance (ec, name);
1086 ig.Emit (OpCodes.Dup);
1087 ig.Emit (OpCodes.Stfld, par_info.FieldBuilder);
1091 // Emits the address for the parameter named `name' within
1092 // an anonymous method.
1094 public void EmitAddressOfParameter (EmitContext ec, string name)
1096 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1098 cc.EmitAddressOfParameter (ec, name);
1101 EmitParameterInstance (ec, name);
1102 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1103 ec.ig.Emit (OpCodes.Ldflda, par_info.FieldBuilder);
1107 // The following methods are only invoked on the host for the
1108 // anonymous method.
1110 public void EmitMethodHostInstance (EmitContext target, AnonymousMethod am)
1112 ILGenerator ig = target.ig;
1113 ScopeInfo si = am.Scope;
1116 ig.Emit (OpCodes.Ldarg_0);
1120 si.EmitInitScope (target);
1121 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1124 ArrayList all_scopes = new ArrayList ();
1126 public void AddScope (ScopeInfo si)
1128 all_scopes.Add (si);
1129 toplevel_owner.RegisterCaptureContext (this);
1133 // Links any scopes that were not linked previously
1135 public void AdjustScopes ()
1137 foreach (ScopeInfo scope in all_scopes){
1138 if (scope.ParentScope != null)
1141 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
1142 if (scopes [b.ID] != null){
1143 LinkScope (scope, b.ID);
1148 if (scope.ParentScope == null && ParentCaptureContext != null){
1149 CaptureContext pcc = ParentCaptureContext;
1151 for (Block b = Host.ContainingBlock; b != null; b = b.Parent){
1152 if (pcc.scopes [b.ID] != null){
1153 pcc.LinkScope (scope, b.ID);