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);
171 if (Parameters == null){
175 // We provide a set of inaccessible parameters
178 for (i = 0; i < invoke_pd.Count; i++){
179 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
182 int n = invoke_pd.Count - (params_idx != -1 ? 1 : 0);
183 Parameter [] fixedpars = new Parameter [n];
185 for (i = j = 0; i < invoke_pd.Count; i++){
186 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
188 fixedpars [j] = new Parameter (
189 new TypeExpression (invoke_pd.ParameterType (i), loc),
190 "+" + j, invoke_pd.ParameterModifier (i), null);
194 Parameter variable = null;
195 if (params_idx != -1){
196 variable = new Parameter (
197 new TypeExpression (invoke_pd.ParameterType (params_idx), loc),
198 "+" + params_idx, invoke_pd.ParameterModifier (params_idx), null);
201 Parameters = new Parameters (fixedpars, variable, loc);
205 // First, parameter types of `delegate_type' must be compatible
206 // with the anonymous method.
208 amp = new InternalParameters (Parameters.GetParameterInfo (ec), Parameters);
210 if (amp.Count != invoke_pd.Count){
212 Report.Error (1593, loc,
213 "Anonymous method has {0} parameters, while delegate requires {1}",
214 amp.Count, invoke_pd.Count);
215 Error_ParameterMismatch (delegate_type);
220 for (int i = 0; i < amp.Count; i++){
221 Parameter.Modifier amp_mod = amp.ParameterModifier (i);
223 if ((amp_mod & (Parameter.Modifier.OUT | Parameter.Modifier.REF)) != 0){
225 Error_ParameterMismatch (delegate_type);
226 Report.Error (1677, loc, "Parameter '{0}' should not be declared with the '{1}' keyword",
227 i+1, amp.ModifierDesc (i));
232 if (amp_mod != invoke_pd.ParameterModifier (i)){
234 Report.Error (1676, loc,
235 "Signature mismatch in parameter modifier for parameter #0", i + 1);
236 Error_ParameterMismatch (delegate_type);
241 if (amp.ParameterType (i) != invoke_pd.ParameterType (i)){
243 Report.Error (1678, loc,
244 "Signature mismatch in parameter {0}: need `{1}' got `{2}'", i + 1,
245 TypeManager.CSharpName (invoke_pd.ParameterType (i)),
246 TypeManager.CSharpName (amp.ParameterType (i)));
247 Error_ParameterMismatch (delegate_type);
254 // If we are only probing, return ourselves
260 // Second: the return type of the delegate must be compatible with
261 // the anonymous type. Instead of doing a pass to examine the block
262 // we satisfy the rule by setting the return type on the EmitContext
263 // to be the delegate type return type.
266 //MethodBuilder builder = method_data.MethodBuilder;
267 //ILGenerator ig = builder.GetILGenerator ();
270 aec = new EmitContext (
271 ec.TypeContainer, ec.DeclSpace, loc, null,
272 invoke_mb.ReturnType,
273 /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
274 (ec.InUnsafe ? Modifiers.UNSAFE : 0) |
275 (ec.IsStatic ? Modifiers.STATIC : 0),
276 /* No constructor */ false);
278 aec.CurrentAnonymousMethod = this;
279 ContainerAnonymousMethod = ec.CurrentAnonymousMethod;
280 ContainingBlock = ec.CurrentBlock;
282 if (aec.ResolveTopBlock (ec, Block, amp, loc, out unreachable))
283 return new AnonymousDelegate (this, delegate_type, loc).Resolve (ec);
288 public MethodBuilder GetMethodBuilder ()
290 return method.MethodData.MethodBuilder;
293 public bool EmitMethod (EmitContext ec)
295 if (!CreateMethodHost (ec, invoke_mb.ReturnType))
298 MethodBuilder builder = GetMethodBuilder ();
299 ILGenerator ig = builder.GetILGenerator ();
302 Parameters.LabelParameters (aec, builder, loc);
305 // Adjust based on the computed state of the
306 // method from CreateMethodHost
308 aec.MethodIsStatic = (method_modifiers & Modifiers.STATIC) != 0;
310 aec.EmitMeta (Block, amp);
311 aec.EmitResolvedTopBlock (Block, unreachable);
315 public static void Error_AddressOfCapturedVar (string name, Location loc)
317 Report.Error (1686, loc,
318 "Variable {0} is captured in an anonymous method and its address is also being taken: they are exclusive", name);
323 // This will emit the code for the delegate, as well delegate creation on the host
325 public class AnonymousDelegate : DelegateCreation {
328 public AnonymousDelegate (AnonymousMethod am, Type target_type, Location l)
335 public override Expression DoResolve (EmitContext ec)
337 eclass = ExprClass.Value;
342 public override void Emit (EmitContext ec)
344 if (!am.EmitMethod (ec))
348 // Now emit the delegate creation.
350 if ((am.method.ModFlags & Modifiers.STATIC) == 0)
351 delegate_instance_expression = new AnonymousInstance (am);
353 Expression ml = Expression.MemberLookup (ec, type, ".ctor", loc);
354 constructor_method = ((MethodGroupExpr) ml).Methods [0];
355 delegate_method = am.GetMethodBuilder ();
359 class AnonymousInstance : Expression {
362 public AnonymousInstance (AnonymousMethod am)
365 eclass = ExprClass.Value;
368 public override Expression DoResolve (EmitContext ec)
373 public override void Emit (EmitContext ec)
375 am.aec.EmitMethodHostInstance (ec, am);
380 class CapturedParameter {
382 public FieldBuilder FieldBuilder;
385 public CapturedParameter (Type type, int idx)
393 // Here we cluster all the variables captured on a given scope, we also
394 // keep some extra information that might be required on each scope.
396 public class ScopeInfo {
397 public CaptureContext CaptureContext;
398 public ScopeInfo ParentScope;
399 public Block ScopeBlock;
400 public bool NeedThis = false;
401 public bool HostsParameters = false;
403 // For tracking the number of scopes created.
408 ArrayList locals = new ArrayList ();
409 ArrayList children = new ArrayList ();
412 // The types and fields generated
414 public TypeBuilder ScopeTypeBuilder;
415 public ConstructorBuilder ScopeConstructor;
416 public FieldBuilder THIS;
417 public FieldBuilder ParentLink;
420 // Points to the object of type `ScopeTypeBuilder' that
421 // holds the data for the scope
423 public LocalBuilder ScopeInstance;
426 public ScopeInfo (CaptureContext cc, Block b)
435 public void AddLocal (LocalInfo li)
437 if (locals.Contains (li))
443 public bool IsCaptured (LocalInfo li)
445 return locals.Contains (li);
448 public void AddChild (ScopeInfo si)
450 if (children.Contains (si))
455 static int indent = 0;
459 for (int i = 0; i < indent; i++)
465 //Console.WriteLine (Environment.StackTrace);
467 Console.WriteLine ("START");
470 Console.WriteLine ("NeedThis=" + NeedThis);
471 foreach (LocalInfo li in locals){
473 Console.WriteLine ("var {0}", li.Name);
476 foreach (ScopeInfo si in children)
480 Console.WriteLine ("END");
483 public string MakeHelperName ()
485 return String.Format ("<>AnonHelp<{0}>", id);
488 public void EmitScopeConstructor ()
490 Type [] constructor_types = TypeManager.NoTypes;
491 Parameters constructor_parameters = Parameters.EmptyReadOnlyParameters;
492 ScopeConstructor = ScopeTypeBuilder.DefineConstructor (
493 MethodAttributes.Public | MethodAttributes.HideBySig |
494 MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
495 CallingConventions.HasThis, constructor_types);
496 InternalParameters parameter_info = new InternalParameters (constructor_types, constructor_parameters);
497 TypeManager.RegisterMethod (ScopeConstructor, parameter_info, constructor_types);
499 ILGenerator cig = ScopeConstructor.GetILGenerator ();
500 cig.Emit (OpCodes.Ldarg_0);
501 cig.Emit (OpCodes.Call, TypeManager.object_ctor);
502 cig.Emit (OpCodes.Ret);
505 public void EmitScopeType (EmitContext ec)
509 if (ScopeTypeBuilder != null)
512 TypeBuilder container = ec.TypeContainer.TypeBuilder;
514 ScopeTypeBuilder = container.DefineNestedType (
515 MakeHelperName (), TypeAttributes.AutoLayout | TypeAttributes.Class | TypeAttributes.NestedAssembly,
516 TypeManager.object_type, null);
519 THIS = ScopeTypeBuilder.DefineField ("<>THIS", container, FieldAttributes.Assembly);
521 if (ParentScope != null){
522 if (ParentScope.ScopeTypeBuilder == null){
523 throw new Exception (String.Format ("My parent has not been initialized {0} and {1}", ParentScope, this));
526 ParentLink = ScopeTypeBuilder.DefineField ("<>parent", ParentScope.ScopeTypeBuilder,
527 FieldAttributes.Assembly);
530 if (NeedThis && ParentScope != null)
531 throw new Exception ("I was not expecting THIS && having a parent");
533 foreach (LocalInfo info in locals){
534 info.FieldBuilder = ScopeTypeBuilder.DefineField (
535 info.Name, info.VariableType, FieldAttributes.Assembly);
538 if (HostsParameters){
539 Hashtable captured_parameters = CaptureContext.captured_parameters;
541 foreach (DictionaryEntry de in captured_parameters){
542 string name = (string) de.Key;
543 CapturedParameter cp = (CapturedParameter) de.Value;
546 fb = ScopeTypeBuilder.DefineField ("<p:" + name + ">", cp.Type, FieldAttributes.Assembly);
547 cp.FieldBuilder = fb;
551 EmitScopeConstructor ();
552 foreach (ScopeInfo si in children){
553 si.EmitScopeType (ec);
557 public void CloseTypes ()
559 RootContext.RegisterCompilerGeneratedType (ScopeTypeBuilder);
560 foreach (ScopeInfo si in children)
565 // Emits the initialization code for the scope
567 public void EmitInitScope (EmitContext ec)
569 ILGenerator ig = ec.ig;
574 ig.Emit (OpCodes.Newobj, (ConstructorInfo) ScopeConstructor);
575 ScopeInstance = ig.DeclareLocal (ScopeTypeBuilder);
576 ig.Emit (OpCodes.Stloc, ScopeInstance);
579 ig.Emit (OpCodes.Ldloc, ScopeInstance);
580 ig.Emit (OpCodes.Ldarg_0);
581 ig.Emit (OpCodes.Stfld, THIS);
585 // Copy the parameter values, if any
587 int extra = ec.IsStatic ? 0 : 1;
588 if (HostsParameters){
589 Hashtable captured_parameters = CaptureContext.captured_parameters;
591 foreach (DictionaryEntry de in captured_parameters){
592 CapturedParameter cp = (CapturedParameter) de.Value;
594 ig.Emit (OpCodes.Ldloc, ScopeInstance);
595 ParameterReference.EmitLdArg (ig, cp.Idx + extra);
596 ig.Emit (OpCodes.Stfld, cp.FieldBuilder);
600 if (ParentScope != null){
601 if (!ParentScope.inited)
602 ParentScope.EmitInitScope (ec);
605 // Only emit initialization in our capturecontext world
607 if (ParentScope.CaptureContext == CaptureContext){
608 ig.Emit (OpCodes.Ldloc, ScopeInstance);
609 ig.Emit (OpCodes.Ldloc, ParentScope.ScopeInstance);
610 ig.Emit (OpCodes.Stfld, ParentLink);
612 ig.Emit (OpCodes.Ldloc, ScopeInstance);
613 ig.Emit (OpCodes.Ldarg_0);
614 ig.Emit (OpCodes.Stfld, ParentLink);
620 static void DoPath (StringBuilder sb, ScopeInfo start)
622 if (start.ParentScope != null){
623 DoPath (sb, start.ParentScope);
626 sb.Append ((start.id).ToString ());
629 public override string ToString ()
631 StringBuilder sb = new StringBuilder ();
634 if (CaptureContext != null){
635 sb.Append (CaptureContext.ToString ());
642 return sb.ToString ();
647 // CaptureContext objects are created on demand if a method has
648 // anonymous methods and kept on the ToplevelBlock.
650 // If they exist, all ToplevelBlocks in the containing block are
651 // linked together (children pointing to their parents).
653 public class CaptureContext {
654 public static int count;
659 // Points to the toplevel block that owns this CaptureContext
661 ToplevelBlock toplevel_owner;
662 Hashtable scopes = new Hashtable ();
663 bool have_captured_vars = false;
664 bool referenced_this = false;
665 ScopeInfo topmost = null;
670 Hashtable captured_fields = new Hashtable ();
671 Hashtable captured_variables = new Hashtable ();
672 public Hashtable captured_parameters = new Hashtable ();
673 public AnonymousMethod Host;
675 public CaptureContext (ToplevelBlock toplevel_owner, Location loc, AnonymousMethod host)
678 this.toplevel_owner = toplevel_owner;
685 void DoPath (StringBuilder sb, CaptureContext cc)
687 if (cc.ParentCaptureContext != null){
688 DoPath (sb, cc.ParentCaptureContext);
691 sb.Append (cc_id.ToString ());
694 public override string ToString ()
696 StringBuilder sb = new StringBuilder ();
700 return sb.ToString ();
703 public ToplevelBlock ParentToplevel {
705 return toplevel_owner.Container;
709 public CaptureContext ParentCaptureContext {
711 ToplevelBlock parent = ParentToplevel;
713 return (parent == null) ? null : parent.CaptureContext;
717 // Returns the deepest of two scopes
718 public ScopeInfo Deepest (ScopeInfo a, ScopeInfo b)
730 // If they Scopes are on the same CaptureContext, we do the double
731 // checks just so if there is an invariant change in the future,
732 // we get the exception at the end
734 for (p = a; p != null; p = p.ParentScope)
738 for (p = b; p != null; p = p.ParentScope)
742 CaptureContext ca = a.CaptureContext;
743 CaptureContext cb = b.CaptureContext;
745 for (CaptureContext c = ca; c != null; c = c.ParentCaptureContext)
749 for (CaptureContext c = cb; c != null; c = c.ParentCaptureContext)
752 throw new Exception ("Should never be reached");
755 void AdjustMethodScope (AnonymousMethod am, ScopeInfo scope)
757 am.Scope = Deepest (am.Scope, scope);
760 void LinkScope (ScopeInfo scope, int id)
762 ScopeInfo parent = (ScopeInfo) scopes [id];
763 scope.ParentScope = parent;
764 parent.AddChild (scope);
766 if (scope == topmost)
770 public void AddLocal (AnonymousMethod am, LocalInfo li)
772 if (li.Block.Toplevel != toplevel_owner){
773 ParentCaptureContext.AddLocal (am, li);
776 int block_id = li.Block.ID;
778 if (scopes [block_id] == null){
779 scope = new ScopeInfo (this, li.Block);
780 scopes [block_id] = scope;
782 scope = (ScopeInfo) scopes [block_id];
784 if (topmost == null){
788 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
789 if (scopes [b.ID] != null){
790 LinkScope (scope, b.ID);
795 if (scope.ParentScope == null && ParentCaptureContext != null){
796 CaptureContext pcc = ParentCaptureContext;
798 for (Block b = am.ContainingBlock; b != null; b = b.Parent){
799 if (pcc.scopes [b.ID] != null){
800 pcc.LinkScope (scope, b.ID);
811 AdjustMethodScope (Host, topmost);
816 AdjustMethodScope (am, scope);
818 if (captured_variables [li] != null)
821 have_captured_vars = true;
822 captured_variables [li] = li;
827 // Retursn the CaptureContext for the block that defines the parameter `name'
829 static CaptureContext _ContextForParameter (ToplevelBlock current, string name)
831 ToplevelBlock container = current.Container;
832 if (container != null){
833 CaptureContext cc = _ContextForParameter (container, name);
837 if (current.IsParameterReference (name))
838 return current.ToplevelBlockCaptureContext;
842 static CaptureContext ContextForParameter (ToplevelBlock current, string name)
844 CaptureContext cc = _ContextForParameter (current, name);
846 throw new Exception (String.Format ("request for parameteter {0} failed: not found", name));
851 // Records the captured parameter at the appropriate CaptureContext
853 public void AddParameter (EmitContext ec, AnonymousMethod am, string name, Type t, int idx)
855 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
857 cc.AddParameterToContext (am, name, t, idx);
861 // Records the parameters in the context
863 void AddParameterToContext (AnonymousMethod am, string name, Type t, int idx)
865 if (captured_parameters == null)
866 captured_parameters = new Hashtable ();
867 if (captured_parameters [name] != null)
869 captured_parameters [name] = new CapturedParameter (t, idx);
871 if (topmost == null){
873 // Create one ScopeInfo, if there are none.
875 topmost = new ScopeInfo (this, toplevel_owner);
876 scopes [toplevel_owner.ID] = topmost;
879 // If the topmost ScopeInfo is not at the topblock level, insert
880 // a new ScopeInfo there.
882 if (topmost.ScopeBlock != toplevel_owner){
883 ScopeInfo par_si = new ScopeInfo (this, toplevel_owner);
884 scopes [toplevel_owner.ID] = topmost;
885 topmost.ParentScope = par_si;
890 topmost.HostsParameters = true;
891 AdjustMethodScope (am, topmost);
895 // Captured fields are only recorded on the topmost CaptureContext, because that
896 // one is the one linked to the owner of instance fields
898 public void AddField (FieldExpr fe)
900 if (fe.FieldInfo.IsStatic)
901 throw new Exception ("Attempt to register a static field as a captured field");
903 CaptureContext parent = ParentCaptureContext;
905 parent.AddField (fe);
907 captured_fields [fe] = fe;
910 public void CaptureThis ()
912 CaptureContext parent = ParentCaptureContext;
914 parent.CaptureThis ();
915 referenced_this = true;
918 public bool HaveCapturedVariables {
920 return have_captured_vars;
924 public bool HaveCapturedFields {
926 CaptureContext parent = ParentCaptureContext;
928 return parent.HaveCapturedFields;
929 return captured_fields.Count > 0;
933 public bool IsCaptured (LocalInfo local)
935 foreach (ScopeInfo si in scopes.Values){
936 if (si.IsCaptured (local))
943 // Returns whether the parameter is captured
945 public bool IsParameterCaptured (string name)
947 if (ParentCaptureContext != null && ParentCaptureContext.IsParameterCaptured (name))
950 if (captured_parameters != null)
951 return captured_parameters [name] != null;
955 public void EmitAnonymousHelperClasses (EmitContext ec)
957 if (topmost != null){
958 topmost.NeedThis = HaveCapturedFields || referenced_this;
959 topmost.EmitScopeType (ec);
963 public void CloseAnonymousHelperClasses ()
966 topmost.CloseTypes ();
969 ScopeInfo GetScopeFromBlock (EmitContext ec, Block b)
973 si = (ScopeInfo) scopes [b.ID];
975 throw new Exception ("Si is null for block " + b.ID);
976 si.EmitInitScope (ec);
982 // Emits the opcodes necessary to load the instance of the captured
985 public void EmitCapturedVariableInstance (EmitContext ec, LocalInfo li, AnonymousMethod am)
987 ILGenerator ig = ec.ig;
990 if (li.Block.Toplevel == toplevel_owner){
991 si = GetScopeFromBlock (ec, li.Block);
992 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
997 ig.Emit (OpCodes.Ldarg_0);
999 while (si.ScopeBlock.ID != li.Block.ID){
1000 if (si.ParentLink != null)
1001 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1002 si = si.ParentScope;
1004 throw new Exception (
1005 String.Format ("Never found block {0} starting at {1} while looking up {2}",
1006 li.Block.ID, am.Scope.ScopeBlock.ID, li.Name));
1012 // Internal routine that loads the instance to reach parameter `name'
1014 void EmitParameterInstance (EmitContext ec, string name)
1016 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1018 cc.EmitParameterInstance (ec, name);
1022 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1023 if (par_info != null){
1025 // FIXME: implementing this.
1028 ILGenerator ig = ec.ig;
1031 if (ec.CurrentBlock == toplevel_owner){
1032 si = GetScopeFromBlock (ec, toplevel_owner);
1033 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1037 si = ec.CurrentAnonymousMethod.Scope;
1038 ig.Emit (OpCodes.Ldarg_0);
1040 while (si.ParentLink != null) {
1041 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1042 si = si.ParentScope;
1048 // Emits the code necessary to load the parameter named `name' within
1049 // an anonymous method.
1051 public void EmitParameter (EmitContext ec, string name)
1053 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1055 cc.EmitParameter (ec, name);
1058 EmitParameterInstance (ec, name);
1059 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1060 if (par_info != null){
1062 // FIXME: implementing this.
1065 ec.ig.Emit (OpCodes.Ldfld, par_info.FieldBuilder);
1069 // Implements the assignment of `source' to the paramenter named `name' within
1070 // an anonymous method.
1072 public void EmitAssignParameter (EmitContext ec, string name, Expression source, bool leave_copy, bool prepare_for_load)
1074 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1076 cc.EmitAssignParameter (ec, name, source, leave_copy, prepare_for_load);
1079 ILGenerator ig = ec.ig;
1080 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1082 EmitParameterInstance (ec, name);
1085 ig.Emit (OpCodes.Dup);
1086 ig.Emit (OpCodes.Stfld, par_info.FieldBuilder);
1090 // Emits the address for the parameter named `name' within
1091 // an anonymous method.
1093 public void EmitAddressOfParameter (EmitContext ec, string name)
1095 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1097 cc.EmitAddressOfParameter (ec, name);
1100 EmitParameterInstance (ec, name);
1101 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1102 ec.ig.Emit (OpCodes.Ldflda, par_info.FieldBuilder);
1106 // The following methods are only invoked on the host for the
1107 // anonymous method.
1109 public void EmitMethodHostInstance (EmitContext target, AnonymousMethod am)
1111 ILGenerator ig = target.ig;
1112 ScopeInfo si = am.Scope;
1115 ig.Emit (OpCodes.Ldarg_0);
1119 si.EmitInitScope (target);
1120 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1123 ArrayList all_scopes = new ArrayList ();
1125 public void AddScope (ScopeInfo si)
1127 all_scopes.Add (si);
1128 toplevel_owner.RegisterCaptureContext (this);
1132 // Links any scopes that were not linked previously
1134 public void AdjustScopes ()
1136 foreach (ScopeInfo scope in all_scopes){
1137 if (scope.ParentScope != null)
1140 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
1141 if (scopes [b.ID] != null){
1142 LinkScope (scope, b.ID);
1147 if (scope.ParentScope == null && ParentCaptureContext != null){
1148 CaptureContext pcc = ParentCaptureContext;
1150 for (Block b = Host.ContainingBlock; b != null; b = b.Parent){
1151 if (pcc.scopes [b.ID] != null){
1152 pcc.LinkScope (scope, b.ID);