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,
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);
170 if (Parameters == null){
174 // We provide a set of inaccessible parameters
177 for (i = 0; i < invoke_pd.Count; i++){
178 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
181 int n = invoke_pd.Count - (params_idx != -1 ? 1 : 0);
182 Parameter [] fixedpars = new Parameter [n];
184 for (i = j = 0; i < invoke_pd.Count; i++){
185 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
187 fixedpars [j] = new Parameter (
188 new TypeExpression (invoke_pd.ParameterType (i), loc),
189 "+" + j, invoke_pd.ParameterModifier (i), null);
193 Parameter variable = null;
194 if (params_idx != -1){
195 variable = new Parameter (
196 new TypeExpression (invoke_pd.ParameterType (params_idx), loc),
197 "+" + params_idx, invoke_pd.ParameterModifier (params_idx), null);
200 Parameters = new Parameters (fixedpars, variable, loc);
204 // First, parameter types of `delegate_type' must be compatible
205 // with the anonymous method.
207 amp = new InternalParameters (Parameters.GetParameterInfo (ec), Parameters);
209 if (amp.Count != invoke_pd.Count){
211 Report.Error (1593, loc,
212 "Anonymous method has {0} parameters, while delegate requires {1}",
213 amp.Count, invoke_pd.Count);
214 Error_ParameterMismatch (delegate_type);
219 for (int i = 0; i < amp.Count; i++){
220 Parameter.Modifier amp_mod = amp.ParameterModifier (i);
222 if ((amp_mod & (Parameter.Modifier.OUT | Parameter.Modifier.REF)) != 0){
224 Error_ParameterMismatch (delegate_type);
225 Report.Error (1677, loc, "Parameter '{0}' should not be declared with the '{1}' keyword",
226 i+1, amp.ModifierDesc (i));
231 if (amp_mod != invoke_pd.ParameterModifier (i)){
233 Report.Error (1676, loc,
234 "Signature mismatch in parameter modifier for parameter #0", i + 1);
235 Error_ParameterMismatch (delegate_type);
240 if (amp.ParameterType (i) != invoke_pd.ParameterType (i)){
242 Report.Error (1678, loc,
243 "Signature mismatch in parameter {0}: need `{1}' got `{2}'", i + 1,
244 TypeManager.CSharpName (invoke_pd.ParameterType (i)),
245 TypeManager.CSharpName (amp.ParameterType (i)));
246 Error_ParameterMismatch (delegate_type);
253 // If we are only probing, return ourselves
259 // Second: the return type of the delegate must be compatible with
260 // the anonymous type. Instead of doing a pass to examine the block
261 // we satisfy the rule by setting the return type on the EmitContext
262 // to be the delegate type return type.
265 //MethodBuilder builder = method_data.MethodBuilder;
266 //ILGenerator ig = builder.GetILGenerator ();
269 aec = new EmitContext (
270 ec.TypeContainer, ec.DeclSpace, loc, null,
271 invoke_mb.ReturnType,
272 /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
273 (ec.InUnsafe ? Modifiers.UNSAFE : 0) |
274 (ec.IsStatic ? Modifiers.STATIC : 0),
275 /* No constructor */ false);
277 aec.CurrentAnonymousMethod = this;
278 ContainerAnonymousMethod = ec.CurrentAnonymousMethod;
279 ContainingBlock = ec.CurrentBlock;
281 if (aec.ResolveTopBlock (ec, Block, amp, loc, out unreachable))
282 return new AnonymousDelegate (this, delegate_type, loc).Resolve (ec);
287 public MethodBuilder GetMethodBuilder ()
289 return method.MethodData.MethodBuilder;
292 public bool EmitMethod (EmitContext ec)
294 if (!CreateMethodHost (ec, invoke_mb.ReturnType))
297 MethodBuilder builder = GetMethodBuilder ();
298 ILGenerator ig = builder.GetILGenerator ();
301 Parameters.LabelParameters (aec, builder, loc);
304 // Adjust based on the computed state of the
305 // method from CreateMethodHost
307 aec.MethodIsStatic = (method_modifiers & Modifiers.STATIC) != 0;
309 aec.EmitMeta (Block, amp);
310 aec.EmitResolvedTopBlock (Block, unreachable);
314 public static void Error_AddressOfCapturedVar (string name, Location loc)
316 Report.Error (1686, loc,
317 "Variable {0} is captured in an anonymous method and its address is also being taken: they are exclusive", name);
322 // This will emit the code for the delegate, as well delegate creation on the host
324 public class AnonymousDelegate : DelegateCreation {
327 public AnonymousDelegate (AnonymousMethod am, Type target_type, Location l)
334 public override Expression DoResolve (EmitContext ec)
336 eclass = ExprClass.Value;
341 public override void Emit (EmitContext ec)
343 if (!am.EmitMethod (ec))
347 // Now emit the delegate creation.
349 if ((am.method.ModFlags & Modifiers.STATIC) == 0)
350 delegate_instance_expression = new AnonymousInstance (am);
352 Expression ml = Expression.MemberLookup (ec, type, ".ctor", loc);
353 constructor_method = ((MethodGroupExpr) ml).Methods [0];
354 delegate_method = am.GetMethodBuilder ();
358 class AnonymousInstance : Expression {
361 public AnonymousInstance (AnonymousMethod am)
364 eclass = ExprClass.Value;
367 public override Expression DoResolve (EmitContext ec)
372 public override void Emit (EmitContext ec)
374 am.aec.EmitMethodHostInstance (ec, am);
379 class CapturedParameter {
381 public FieldBuilder FieldBuilder;
384 public CapturedParameter (Type type, int idx)
392 // Here we cluster all the variables captured on a given scope, we also
393 // keep some extra information that might be required on each scope.
395 public class ScopeInfo {
396 public CaptureContext CaptureContext;
397 public ScopeInfo ParentScope;
398 public Block ScopeBlock;
399 public bool NeedThis = false;
400 public bool HostsParameters = false;
402 // For tracking the number of scopes created.
407 ArrayList locals = new ArrayList ();
408 ArrayList children = new ArrayList ();
411 // The types and fields generated
413 public TypeBuilder ScopeTypeBuilder;
414 public ConstructorBuilder ScopeConstructor;
415 public FieldBuilder THIS;
416 public FieldBuilder ParentLink;
419 // Points to the object of type `ScopeTypeBuilder' that
420 // holds the data for the scope
422 public LocalBuilder ScopeInstance;
425 public ScopeInfo (CaptureContext cc, Block b)
434 public void AddLocal (LocalInfo li)
436 if (locals.Contains (li))
442 public bool IsCaptured (LocalInfo li)
444 return locals.Contains (li);
447 public void AddChild (ScopeInfo si)
449 if (children.Contains (si))
454 static int indent = 0;
458 for (int i = 0; i < indent; i++)
464 //Console.WriteLine (Environment.StackTrace);
466 Console.WriteLine ("START");
469 Console.WriteLine ("NeedThis=" + NeedThis);
470 foreach (LocalInfo li in locals){
472 Console.WriteLine ("var {0}", li.Name);
475 foreach (ScopeInfo si in children)
479 Console.WriteLine ("END");
482 public string MakeHelperName ()
484 return String.Format ("<>AnonHelp<{0}>", id);
487 public void EmitScopeConstructor ()
489 Type [] constructor_types = TypeManager.NoTypes;
490 Parameters constructor_parameters = Parameters.EmptyReadOnlyParameters;
491 ScopeConstructor = ScopeTypeBuilder.DefineConstructor (
492 MethodAttributes.Public | MethodAttributes.HideBySig |
493 MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
494 CallingConventions.HasThis, constructor_types);
495 InternalParameters parameter_info = new InternalParameters (constructor_types, constructor_parameters);
496 TypeManager.RegisterMethod (ScopeConstructor, parameter_info, constructor_types);
498 ILGenerator cig = ScopeConstructor.GetILGenerator ();
499 cig.Emit (OpCodes.Ldarg_0);
500 cig.Emit (OpCodes.Call, TypeManager.object_ctor);
501 cig.Emit (OpCodes.Ret);
504 public void EmitScopeType (EmitContext ec)
508 if (ScopeTypeBuilder != null)
511 TypeBuilder container = ec.TypeContainer.TypeBuilder;
513 ScopeTypeBuilder = container.DefineNestedType (
514 MakeHelperName (), TypeAttributes.AutoLayout | TypeAttributes.Class | TypeAttributes.NestedAssembly,
515 TypeManager.object_type, null);
518 THIS = ScopeTypeBuilder.DefineField ("<>THIS", container, FieldAttributes.Assembly);
520 if (ParentScope != null){
521 if (ParentScope.ScopeTypeBuilder == null){
522 throw new Exception (String.Format ("My parent has not been initialized {0} and {1}", ParentScope, this));
525 ParentLink = ScopeTypeBuilder.DefineField ("<>parent", ParentScope.ScopeTypeBuilder,
526 FieldAttributes.Assembly);
529 if (NeedThis && ParentScope != null)
530 throw new Exception ("I was not expecting THIS && having a parent");
532 foreach (LocalInfo info in locals){
533 info.FieldBuilder = ScopeTypeBuilder.DefineField (
534 info.Name, info.VariableType, FieldAttributes.Assembly);
537 if (HostsParameters){
538 Hashtable captured_parameters = CaptureContext.captured_parameters;
540 foreach (DictionaryEntry de in captured_parameters){
541 string name = (string) de.Key;
542 CapturedParameter cp = (CapturedParameter) de.Value;
545 fb = ScopeTypeBuilder.DefineField ("<p:" + name + ">", cp.Type, FieldAttributes.Assembly);
546 cp.FieldBuilder = fb;
550 EmitScopeConstructor ();
551 foreach (ScopeInfo si in children){
552 si.EmitScopeType (ec);
556 public void CloseTypes ()
558 RootContext.RegisterCompilerGeneratedType (ScopeTypeBuilder);
559 foreach (ScopeInfo si in children)
564 // Emits the initialization code for the scope
566 public void EmitInitScope (EmitContext ec)
568 ILGenerator ig = ec.ig;
573 ig.Emit (OpCodes.Newobj, (ConstructorInfo) ScopeConstructor);
574 ScopeInstance = ig.DeclareLocal (ScopeTypeBuilder);
575 ig.Emit (OpCodes.Stloc, ScopeInstance);
578 ig.Emit (OpCodes.Ldloc, ScopeInstance);
579 ig.Emit (OpCodes.Ldarg_0);
580 ig.Emit (OpCodes.Stfld, THIS);
584 // Copy the parameter values, if any
586 int extra = ec.IsStatic ? 0 : 1;
587 if (HostsParameters){
588 Hashtable captured_parameters = CaptureContext.captured_parameters;
590 foreach (DictionaryEntry de in captured_parameters){
591 CapturedParameter cp = (CapturedParameter) de.Value;
593 ig.Emit (OpCodes.Ldloc, ScopeInstance);
594 ParameterReference.EmitLdArg (ig, cp.Idx + extra);
595 ig.Emit (OpCodes.Stfld, cp.FieldBuilder);
599 if (ParentScope != null){
600 if (!ParentScope.inited)
601 ParentScope.EmitInitScope (ec);
604 // Only emit initialization in our capturecontext world
606 if (ParentScope.CaptureContext == CaptureContext){
607 ig.Emit (OpCodes.Ldloc, ScopeInstance);
608 ig.Emit (OpCodes.Ldloc, ParentScope.ScopeInstance);
609 ig.Emit (OpCodes.Stfld, ParentLink);
611 ig.Emit (OpCodes.Ldloc, ScopeInstance);
612 ig.Emit (OpCodes.Ldarg_0);
613 ig.Emit (OpCodes.Stfld, ParentLink);
619 static void DoPath (StringBuilder sb, ScopeInfo start)
621 if (start.ParentScope != null){
622 DoPath (sb, start.ParentScope);
625 sb.Append ((start.id).ToString ());
628 public override string ToString ()
630 StringBuilder sb = new StringBuilder ();
633 if (CaptureContext != null){
634 sb.Append (CaptureContext.ToString ());
641 return sb.ToString ();
646 // CaptureContext objects are created on demand if a method has
647 // anonymous methods and kept on the ToplevelBlock.
649 // If they exist, all ToplevelBlocks in the containing block are
650 // linked together (children pointing to their parents).
652 public class CaptureContext {
653 public static int count;
658 // Points to the toplevel block that owns this CaptureContext
660 ToplevelBlock toplevel_owner;
661 Hashtable scopes = new Hashtable ();
662 bool have_captured_vars = false;
663 bool referenced_this = false;
664 ScopeInfo topmost = null;
669 Hashtable captured_fields = new Hashtable ();
670 Hashtable captured_variables = new Hashtable ();
671 public Hashtable captured_parameters = new Hashtable ();
672 public AnonymousMethod Host;
674 public CaptureContext (ToplevelBlock toplevel_owner, Location loc, AnonymousMethod host)
677 this.toplevel_owner = toplevel_owner;
684 void DoPath (StringBuilder sb, CaptureContext cc)
686 if (cc.ParentCaptureContext != null){
687 DoPath (sb, cc.ParentCaptureContext);
690 sb.Append (cc_id.ToString ());
693 public override string ToString ()
695 StringBuilder sb = new StringBuilder ();
699 return sb.ToString ();
702 public ToplevelBlock ParentToplevel {
704 return toplevel_owner.Container;
708 public CaptureContext ParentCaptureContext {
710 ToplevelBlock parent = ParentToplevel;
712 return (parent == null) ? null : parent.CaptureContext;
716 // Returns the deepest of two scopes
717 public ScopeInfo Deepest (ScopeInfo a, ScopeInfo b)
729 // If they Scopes are on the same CaptureContext, we do the double
730 // checks just so if there is an invariant change in the future,
731 // we get the exception at the end
733 for (p = a; p != null; p = p.ParentScope)
737 for (p = b; p != null; p = p.ParentScope)
741 CaptureContext ca = a.CaptureContext;
742 CaptureContext cb = b.CaptureContext;
744 for (CaptureContext c = ca; c != null; c = c.ParentCaptureContext)
748 for (CaptureContext c = cb; c != null; c = c.ParentCaptureContext)
751 throw new Exception ("Should never be reached");
754 void AdjustMethodScope (AnonymousMethod am, ScopeInfo scope)
756 am.Scope = Deepest (am.Scope, scope);
759 void LinkScope (ScopeInfo scope, int id)
761 ScopeInfo parent = (ScopeInfo) scopes [id];
762 scope.ParentScope = parent;
763 parent.AddChild (scope);
765 if (scope == topmost)
769 public void AddLocal (AnonymousMethod am, LocalInfo li)
771 if (li.Block.Toplevel != toplevel_owner){
772 ParentCaptureContext.AddLocal (am, li);
775 int block_id = li.Block.ID;
777 if (scopes [block_id] == null){
778 scope = new ScopeInfo (this, li.Block);
779 scopes [block_id] = scope;
781 scope = (ScopeInfo) scopes [block_id];
783 if (topmost == null){
787 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
788 if (scopes [b.ID] != null){
789 LinkScope (scope, b.ID);
794 if (scope.ParentScope == null && ParentCaptureContext != null){
795 CaptureContext pcc = ParentCaptureContext;
797 for (Block b = am.ContainingBlock; b != null; b = b.Parent){
798 if (pcc.scopes [b.ID] != null){
799 pcc.LinkScope (scope, b.ID);
810 AdjustMethodScope (Host, topmost);
815 AdjustMethodScope (am, scope);
817 if (captured_variables [li] != null)
820 have_captured_vars = true;
821 captured_variables [li] = li;
826 // Retursn the CaptureContext for the block that defines the parameter `name'
828 static CaptureContext _ContextForParameter (ToplevelBlock current, string name)
830 ToplevelBlock container = current.Container;
831 if (container != null){
832 CaptureContext cc = _ContextForParameter (container, name);
836 if (current.IsParameterReference (name))
837 return current.ToplevelBlockCaptureContext;
841 static CaptureContext ContextForParameter (ToplevelBlock current, string name)
843 CaptureContext cc = _ContextForParameter (current, name);
845 throw new Exception (String.Format ("request for parameteter {0} failed: not found", name));
850 // Records the captured parameter at the appropriate CaptureContext
852 public void AddParameter (EmitContext ec, AnonymousMethod am, string name, Type t, int idx)
854 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
856 cc.AddParameterToContext (am, name, t, idx);
860 // Records the parameters in the context
862 void AddParameterToContext (AnonymousMethod am, string name, Type t, int idx)
864 if (captured_parameters == null)
865 captured_parameters = new Hashtable ();
866 if (captured_parameters [name] != null)
868 captured_parameters [name] = new CapturedParameter (t, idx);
870 if (topmost == null){
872 // Create one ScopeInfo, if there are none.
874 topmost = new ScopeInfo (this, toplevel_owner);
875 scopes [toplevel_owner.ID] = topmost;
878 // If the topmost ScopeInfo is not at the topblock level, insert
879 // a new ScopeInfo there.
881 if (topmost.ScopeBlock != toplevel_owner){
882 ScopeInfo par_si = new ScopeInfo (this, toplevel_owner);
883 scopes [toplevel_owner.ID] = topmost;
884 topmost.ParentScope = par_si;
889 topmost.HostsParameters = true;
890 AdjustMethodScope (am, topmost);
894 // Captured fields are only recorded on the topmost CaptureContext, because that
895 // one is the one linked to the owner of instance fields
897 public void AddField (FieldExpr fe)
899 if (fe.FieldInfo.IsStatic)
900 throw new Exception ("Attempt to register a static field as a captured field");
902 CaptureContext parent = ParentCaptureContext;
904 parent.AddField (fe);
906 captured_fields [fe] = fe;
909 public void CaptureThis ()
911 CaptureContext parent = ParentCaptureContext;
913 parent.CaptureThis ();
914 referenced_this = true;
917 public bool HaveCapturedVariables {
919 return have_captured_vars;
923 public bool HaveCapturedFields {
925 CaptureContext parent = ParentCaptureContext;
927 return parent.HaveCapturedFields;
928 return captured_fields.Count > 0;
932 public bool IsCaptured (LocalInfo local)
934 foreach (ScopeInfo si in scopes.Values){
935 if (si.IsCaptured (local))
942 // Returns whether the parameter is captured
944 public bool IsParameterCaptured (string name)
946 if (ParentCaptureContext != null && ParentCaptureContext.IsParameterCaptured (name))
949 if (captured_parameters != null)
950 return captured_parameters [name] != null;
954 public void EmitAnonymousHelperClasses (EmitContext ec)
956 if (topmost != null){
957 topmost.NeedThis = HaveCapturedFields || referenced_this;
958 topmost.EmitScopeType (ec);
962 public void CloseAnonymousHelperClasses ()
965 topmost.CloseTypes ();
968 ScopeInfo GetScopeFromBlock (EmitContext ec, Block b)
972 si = (ScopeInfo) scopes [b.ID];
974 throw new Exception ("Si is null for block " + b.ID);
975 si.EmitInitScope (ec);
981 // Emits the opcodes necessary to load the instance of the captured
984 public void EmitCapturedVariableInstance (EmitContext ec, LocalInfo li, AnonymousMethod am)
986 ILGenerator ig = ec.ig;
989 if (li.Block.Toplevel == toplevel_owner){
990 si = GetScopeFromBlock (ec, li.Block);
991 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
996 ig.Emit (OpCodes.Ldarg_0);
998 while (si.ScopeBlock.ID != li.Block.ID){
999 if (si.ParentLink != null)
1000 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1001 si = si.ParentScope;
1003 throw new Exception (
1004 String.Format ("Never found block {0} starting at {1} while looking up {2}",
1005 li.Block.ID, am.Scope.ScopeBlock.ID, li.Name));
1011 // Internal routine that loads the instance to reach parameter `name'
1013 void EmitParameterInstance (EmitContext ec, string name)
1015 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1017 cc.EmitParameterInstance (ec, name);
1021 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1022 if (par_info != null){
1024 // FIXME: implementing this.
1027 ILGenerator ig = ec.ig;
1030 if (ec.CurrentBlock == toplevel_owner){
1031 si = GetScopeFromBlock (ec, toplevel_owner);
1032 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1036 si = ec.CurrentAnonymousMethod.Scope;
1037 ig.Emit (OpCodes.Ldarg_0);
1039 while (si.ParentLink != null) {
1040 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1041 si = si.ParentScope;
1047 // Emits the code necessary to load the parameter named `name' within
1048 // an anonymous method.
1050 public void EmitParameter (EmitContext ec, string name)
1052 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1054 cc.EmitParameter (ec, name);
1057 EmitParameterInstance (ec, name);
1058 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1059 if (par_info != null){
1061 // FIXME: implementing this.
1064 ec.ig.Emit (OpCodes.Ldfld, par_info.FieldBuilder);
1068 // Implements the assignment of `source' to the paramenter named `name' within
1069 // an anonymous method.
1071 public void EmitAssignParameter (EmitContext ec, string name, Expression source, bool leave_copy, bool prepare_for_load)
1073 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1075 cc.EmitAssignParameter (ec, name, source, leave_copy, prepare_for_load);
1078 ILGenerator ig = ec.ig;
1079 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1081 EmitParameterInstance (ec, name);
1084 ig.Emit (OpCodes.Dup);
1085 ig.Emit (OpCodes.Stfld, par_info.FieldBuilder);
1089 // Emits the address for the parameter named `name' within
1090 // an anonymous method.
1092 public void EmitAddressOfParameter (EmitContext ec, string name)
1094 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1096 cc.EmitAddressOfParameter (ec, name);
1099 EmitParameterInstance (ec, name);
1100 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1101 ec.ig.Emit (OpCodes.Ldflda, par_info.FieldBuilder);
1105 // The following methods are only invoked on the host for the
1106 // anonymous method.
1108 public void EmitMethodHostInstance (EmitContext target, AnonymousMethod am)
1110 ILGenerator ig = target.ig;
1111 ScopeInfo si = am.Scope;
1114 ig.Emit (OpCodes.Ldarg_0);
1118 si.EmitInitScope (target);
1119 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1122 ArrayList all_scopes = new ArrayList ();
1124 public void AddScope (ScopeInfo si)
1126 all_scopes.Add (si);
1127 toplevel_owner.RegisterCaptureContext (this);
1131 // Links any scopes that were not linked previously
1133 public void AdjustScopes ()
1135 foreach (ScopeInfo scope in all_scopes){
1136 if (scope.ParentScope != null)
1139 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
1140 if (scopes [b.ID] != null){
1141 LinkScope (scope, b.ID);
1146 if (scope.ParentScope == null && ParentCaptureContext != null){
1147 CaptureContext pcc = ParentCaptureContext;
1149 for (Block b = Host.ContainingBlock; b != null; b = b.Parent){
1150 if (pcc.scopes [b.ID] != null){
1151 pcc.LinkScope (scope, b.ID);