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 ? current_type : Scope.ScopeTypeBuilder;
118 if (current_type == null)
119 throw new Exception ("The current_type is null");
121 if (type_host == null)
122 throw new Exception ("Type host is null");
124 if (current_type == type_host && ec.IsStatic){
126 method_modifiers |= Modifiers.STATIC;
130 method = new Method (
131 (TypeContainer) ec.TypeContainer,
132 new TypeExpression (return_type, loc),
133 method_modifiers, false, new MemberName ("<#AnonymousMethod>" + anonymous_method_count++),
134 Parameters, null, loc);
135 method.Block = Block;
139 // Swap the TypeBuilder while we define the method, then restore
141 if (current_type != null)
142 ec.TypeContainer.TypeBuilder = type_host;
143 bool res = method.Define ();
144 if (current_type != null)
145 ec.TypeContainer.TypeBuilder = current_type;
149 void Error_ParameterMismatch (Type t)
151 Report.Error (1661, loc, "Anonymous method could not be converted to delegate `" +
152 "{0}' since there is a parameter mismatch", t);
156 // Returns true if this anonymous method can be implicitly
157 // converted to the delegate type `delegate_type'
159 public Expression Compatible (EmitContext ec, Type delegate_type, bool probe)
162 // At this point its the first time we know the return type that is
163 // needed for the anonymous method. We create the method here.
166 invoke_mb = (MethodInfo) Delegate.GetInvokeMethod (ec, delegate_type, loc);
167 ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
170 // If implicit parameters are set, then we must check for out in the parameters
171 // and flag it accordingly.
173 bool out_invalid_check = false;
175 if (Parameters == null){
177 out_invalid_check = true;
180 // We provide a set of inaccessible parameters
183 for (i = 0; i < invoke_pd.Count; i++){
184 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
187 int n = invoke_pd.Count - (params_idx != -1 ? 1 : 0);
188 Parameter [] fixedpars = new Parameter [n];
190 for (i = j = 0; i < invoke_pd.Count; i++){
191 if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
193 fixedpars [j] = new Parameter (
194 new TypeExpression (invoke_pd.ParameterType (i), loc),
195 "+" + j, invoke_pd.ParameterModifier (i), null);
199 Parameter variable = null;
200 if (params_idx != -1){
201 variable = new Parameter (
202 new TypeExpression (invoke_pd.ParameterType (params_idx), loc),
203 "+" + params_idx, invoke_pd.ParameterModifier (params_idx), null);
206 Parameters = new Parameters (fixedpars, variable, loc);
210 // First, parameter types of `delegate_type' must be compatible
211 // with the anonymous method.
213 amp = new InternalParameters (Parameters.GetParameterInfo (ec), Parameters);
215 if (amp.Count != invoke_pd.Count){
217 Report.Error (1593, loc,
218 "Anonymous method has {0} parameters, while delegate requires {1}",
219 amp.Count, invoke_pd.Count);
220 Error_ParameterMismatch (delegate_type);
225 for (int i = 0; i < amp.Count; i++){
226 Parameter.Modifier amp_mod = amp.ParameterModifier (i);
227 if (amp_mod != invoke_pd.ParameterModifier (i)){
229 Report.Error (1676, loc,
230 "Signature mismatch in parameter modifier for parameter #0", i + 1);
231 Error_ParameterMismatch (delegate_type);
236 if (amp.ParameterType (i) != invoke_pd.ParameterType (i)){
238 Report.Error (1678, loc,
239 "Signature mismatch in parameter {0}: need `{1}' got `{2}'", i + 1,
240 TypeManager.CSharpName (invoke_pd.ParameterType (i)),
241 TypeManager.CSharpName (amp.ParameterType (i)));
242 Error_ParameterMismatch (delegate_type);
247 if (out_invalid_check && (invoke_pd.ParameterModifier (i) & Parameter.Modifier.OUT) != 0){
249 Report.Error (1676, loc,"Parameter {0} must include the `out' modifier ", i+1);
250 Error_ParameterMismatch (delegate_type);
257 // If we are only probing, return ourselves
263 // Second: the return type of the delegate must be compatible with
264 // the anonymous type. Instead of doing a pass to examine the block
265 // we satisfy the rule by setting the return type on the EmitContext
266 // to be the delegate type return type.
269 //MethodBuilder builder = method_data.MethodBuilder;
270 //ILGenerator ig = builder.GetILGenerator ();
273 aec = new EmitContext (
274 ec.TypeContainer, ec.DeclSpace, loc, null,
275 invoke_mb.ReturnType,
276 /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
277 (ec.InUnsafe ? Modifiers.UNSAFE : 0),
278 /* No constructor */ false);
280 aec.CurrentAnonymousMethod = this;
281 ContainerAnonymousMethod = ec.CurrentAnonymousMethod;
282 ContainingBlock = ec.CurrentBlock;
284 if (aec.ResolveTopBlock (ec, Block, amp, loc, out unreachable))
285 return new AnonymousDelegate (this, delegate_type, loc).Resolve (ec);
290 public MethodBuilder GetMethodBuilder ()
292 return method.MethodData.MethodBuilder;
295 public void EmitMethod (EmitContext ec)
297 if (!CreateMethodHost (ec, invoke_mb.ReturnType))
300 MethodBuilder builder = GetMethodBuilder ();
301 ILGenerator ig = builder.GetILGenerator ();
304 Parameters.LabelParameters (aec, builder, loc);
307 // Adjust based on the computed state of the
308 // method from CreateMethodHost
310 if ((method_modifiers & Modifiers.STATIC) != 0)
313 aec.EmitMeta (Block, amp);
314 aec.EmitResolvedTopBlock (Block, unreachable);
317 public static void Error_AddressOfCapturedVar (string name, Location loc)
319 Report.Error (1686, loc,
320 "Variable {0} is captured in an anonymous method and its address is also being taken: they are exclusive", name);
325 // This will emit the code for the delegate, as well delegate creation on the host
327 public class AnonymousDelegate : DelegateCreation {
330 public AnonymousDelegate (AnonymousMethod am, Type target_type, Location l)
337 public override Expression DoResolve (EmitContext ec)
339 eclass = ExprClass.Value;
343 public override void Emit (EmitContext 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.RegisterHelperClass (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){
602 // Only emit initialization in our capturecontext world
604 if (ParentScope.CaptureContext == CaptureContext){
605 ig.Emit (OpCodes.Ldloc, ScopeInstance);
606 ig.Emit (OpCodes.Ldloc, ParentScope.ScopeInstance);
607 ig.Emit (OpCodes.Stfld, ParentLink);
609 ig.Emit (OpCodes.Ldloc, ScopeInstance);
610 ig.Emit (OpCodes.Ldarg_0);
611 ig.Emit (OpCodes.Stfld, ParentLink);
617 static void DoPath (StringBuilder sb, ScopeInfo start)
619 if (start.ParentScope != null){
620 DoPath (sb, start.ParentScope);
623 sb.Append ((start.id).ToString ());
626 public override string ToString ()
628 StringBuilder sb = new StringBuilder ();
631 if (CaptureContext != null){
632 sb.Append (CaptureContext.ToString ());
639 return sb.ToString ();
644 // CaptureContext objects are created on demand if a method has
645 // anonymous methods and kept on the ToplevelBlock.
647 // If they exist, all ToplevelBlocks in the containing block are
648 // linked together (children pointing to their parents).
650 public class CaptureContext {
651 public static int count;
656 // Points to the toplevel block that owns this CaptureContext
658 ToplevelBlock toplevel_owner;
659 Hashtable scopes = new Hashtable ();
660 bool have_captured_vars = false;
661 ScopeInfo topmost = null;
666 Hashtable captured_fields = new Hashtable ();
667 Hashtable captured_variables = new Hashtable ();
668 public Hashtable captured_parameters = new Hashtable ();
669 public AnonymousMethod Host;
671 public CaptureContext (ToplevelBlock toplevel_owner, Location loc, AnonymousMethod host)
674 this.toplevel_owner = toplevel_owner;
681 void DoPath (StringBuilder sb, CaptureContext cc)
683 if (cc.ParentCaptureContext != null){
684 DoPath (sb, cc.ParentCaptureContext);
687 sb.Append (cc_id.ToString ());
690 public override string ToString ()
692 StringBuilder sb = new StringBuilder ();
696 return sb.ToString ();
699 public ToplevelBlock ParentToplevel {
701 return toplevel_owner.Container;
705 public CaptureContext ParentCaptureContext {
707 ToplevelBlock parent = ParentToplevel;
709 return (parent == null) ? null : parent.CaptureContext;
713 // Returns the deepest of two scopes
714 public ScopeInfo Deepest (ScopeInfo a, ScopeInfo b)
726 // If they Scopes are on the same CaptureContext, we do the double
727 // checks just so if there is an invariant change in the future,
728 // we get the exception at the end
730 for (p = a; p != null; p = p.ParentScope)
734 for (p = b; p != null; p = p.ParentScope)
738 CaptureContext ca = a.CaptureContext;
739 CaptureContext cb = b.CaptureContext;
741 for (CaptureContext c = ca; c != null; c = c.ParentCaptureContext)
745 for (CaptureContext c = cb; c != null; c = c.ParentCaptureContext)
748 throw new Exception ("Should never be reached");
751 void AdjustMethodScope (AnonymousMethod am, ScopeInfo scope)
753 am.Scope = Deepest (am.Scope, scope);
756 void LinkScope (ScopeInfo scope, int id)
758 ScopeInfo parent = (ScopeInfo) scopes [id];
759 scope.ParentScope = parent;
760 parent.AddChild (scope);
762 if (scope == topmost)
766 public void AddLocal (AnonymousMethod am, LocalInfo li)
768 if (li.Block.Toplevel != toplevel_owner){
769 ParentCaptureContext.AddLocal (am, li);
772 int block_id = li.Block.ID;
774 if (scopes [block_id] == null){
775 scope = new ScopeInfo (this, li.Block);
776 scopes [block_id] = scope;
778 scope = (ScopeInfo) scopes [block_id];
780 if (topmost == null){
784 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
785 if (scopes [b.ID] != null){
786 LinkScope (scope, b.ID);
791 if (scope.ParentScope == null && ParentCaptureContext != null){
792 CaptureContext pcc = ParentCaptureContext;
794 for (Block b = am.ContainingBlock; b != null; b = b.Parent){
795 if (pcc.scopes [b.ID] != null){
796 pcc.LinkScope (scope, b.ID);
807 AdjustMethodScope (Host, topmost);
812 AdjustMethodScope (am, scope);
814 if (captured_variables [li] != null)
817 have_captured_vars = true;
818 captured_variables [li] = li;
823 // Retursn the CaptureContext for the block that defines the parameter `name'
825 static CaptureContext _ContextForParameter (ToplevelBlock current, string name)
827 ToplevelBlock container = current.Container;
828 if (container != null){
829 CaptureContext cc = _ContextForParameter (container, name);
833 if (current.IsParameterReference (name))
834 return current.ToplevelBlockCaptureContext;
838 static CaptureContext ContextForParameter (ToplevelBlock current, string name)
840 CaptureContext cc = _ContextForParameter (current, name);
842 throw new Exception (String.Format ("request for parameteter {0} failed: not found", name));
847 // Records the captured parameter at the appropriate CaptureContext
849 public void AddParameter (EmitContext ec, AnonymousMethod am, string name, Type t, int idx)
851 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
853 cc.AddParameterToContext (am, name, t, idx);
857 // Records the parameters in the context
859 void AddParameterToContext (AnonymousMethod am, string name, Type t, int idx)
861 if (captured_parameters == null)
862 captured_parameters = new Hashtable ();
863 if (captured_parameters [name] != null)
865 captured_parameters [name] = new CapturedParameter (t, idx);
867 if (topmost == null){
869 // Create one ScopeInfo, if there are none.
871 topmost = new ScopeInfo (this, toplevel_owner);
872 scopes [toplevel_owner.ID] = topmost;
875 // If the topmost ScopeInfo is not at the topblock level, insert
876 // a new ScopeInfo there.
878 if (topmost.ScopeBlock != toplevel_owner){
879 ScopeInfo par_si = new ScopeInfo (this, toplevel_owner);
880 scopes [toplevel_owner.ID] = topmost;
881 topmost.ParentScope = par_si;
886 topmost.HostsParameters = true;
887 AdjustMethodScope (am, topmost);
891 // Captured fields are only recorded on the topmost CaptureContext, because that
892 // one is the one linked to the owner of instance fields
894 public void AddField (FieldExpr fe)
896 if (fe.FieldInfo.IsStatic)
897 throw new Exception ("Attempt to register a static field as a captured field");
899 CaptureContext parent = ParentCaptureContext;
901 parent.AddField (fe);
903 captured_fields [fe] = fe;
907 public bool HaveCapturedVariables {
909 return have_captured_vars;
913 public bool HaveCapturedFields {
915 CaptureContext parent = ParentCaptureContext;
917 return parent.HaveCapturedFields;
918 return captured_fields.Count > 0;
922 public bool IsCaptured (LocalInfo local)
924 foreach (ScopeInfo si in scopes.Values){
925 if (si.IsCaptured (local))
932 // Returns whether the parameter is captured
934 public bool IsParameterCaptured (string name)
936 if (ParentCaptureContext != null && ParentCaptureContext.IsParameterCaptured (name))
939 if (captured_parameters != null)
940 return captured_parameters [name] != null;
944 public void EmitHelperClasses (EmitContext ec)
946 if (topmost != null){
947 topmost.NeedThis = HaveCapturedFields;
948 topmost.EmitScopeType (ec);
952 public void CloseHelperClasses ()
955 topmost.CloseTypes ();
958 ScopeInfo GetScopeFromBlock (EmitContext ec, Block b)
962 si = (ScopeInfo) scopes [b.ID];
964 throw new Exception ("Si is null for block " + b.ID);
965 si.EmitInitScope (ec);
971 // Emits the opcodes necessary to load the instance of the captured
974 public void EmitCapturedVariableInstance (EmitContext ec, LocalInfo li, AnonymousMethod am)
976 ILGenerator ig = ec.ig;
979 if (li.Block.Toplevel == toplevel_owner){
980 si = GetScopeFromBlock (ec, li.Block);
981 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
986 ig.Emit (OpCodes.Ldarg_0);
988 while (si.ScopeBlock.ID != li.Block.ID){
989 if (si.ParentLink != null)
990 ig.Emit (OpCodes.Ldfld, si.ParentLink);
993 throw new Exception (
994 String.Format ("Never found block {0} starting at {1} while looking up {2}",
995 li.Block.ID, am.Scope.ScopeBlock.ID, li.Name));
1001 // Internal routine that loads the instance to reach parameter `name'
1003 void EmitParameterInstance (EmitContext ec, string name)
1005 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1007 cc.EmitParameterInstance (ec, name);
1011 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1012 if (par_info != null){
1014 // FIXME: implementing this.
1017 ILGenerator ig = ec.ig;
1020 if (ec.CurrentBlock == toplevel_owner){
1021 si = GetScopeFromBlock (ec, toplevel_owner);
1022 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1026 si = ec.CurrentAnonymousMethod.Scope;
1027 ig.Emit (OpCodes.Ldarg_0);
1029 while (si.ParentLink != null) {
1030 ig.Emit (OpCodes.Ldfld, si.ParentLink);
1031 si = si.ParentScope;
1037 // Emits the code necessary to load the parameter named `name' within
1038 // an anonymous method.
1040 public void EmitParameter (EmitContext ec, string name)
1042 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1044 cc.EmitParameter (ec, name);
1047 EmitParameterInstance (ec, name);
1048 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1049 if (par_info != null){
1051 // FIXME: implementing this.
1054 ec.ig.Emit (OpCodes.Ldfld, par_info.FieldBuilder);
1058 // Implements the assignment of `source' to the paramenter named `name' within
1059 // an anonymous method.
1061 public void EmitAssignParameter (EmitContext ec, string name, Expression source, bool leave_copy, bool prepare_for_load)
1063 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1065 cc.EmitAssignParameter (ec, name, source, leave_copy, prepare_for_load);
1068 ILGenerator ig = ec.ig;
1069 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1071 EmitParameterInstance (ec, name);
1074 ig.Emit (OpCodes.Dup);
1075 ig.Emit (OpCodes.Stfld, par_info.FieldBuilder);
1079 // Emits the address for the parameter named `name' within
1080 // an anonymous method.
1082 public void EmitAddressOfParameter (EmitContext ec, string name)
1084 CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1086 cc.EmitAddressOfParameter (ec, name);
1089 EmitParameterInstance (ec, name);
1090 CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1091 ec.ig.Emit (OpCodes.Ldflda, par_info.FieldBuilder);
1095 // The following methods are only invoked on the host for the
1096 // anonymous method.
1098 public void EmitMethodHostInstance (EmitContext target, AnonymousMethod am)
1100 ILGenerator ig = target.ig;
1101 ScopeInfo si = am.Scope;
1104 ig.Emit (OpCodes.Ldarg_0);
1108 si.EmitInitScope (target);
1109 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1112 ArrayList all_scopes = new ArrayList ();
1114 public void AddScope (ScopeInfo si)
1116 all_scopes.Add (si);
1117 toplevel_owner.RegisterCaptureContext (this);
1121 // Links any scopes that were not linked previously
1123 public void AdjustScopes ()
1125 foreach (ScopeInfo scope in all_scopes){
1126 if (scope.ParentScope != null)
1129 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
1130 if (scopes [b.ID] != null){
1131 LinkScope (scope, b.ID);
1136 if (scope.ParentScope == null && ParentCaptureContext != null){
1137 CaptureContext pcc = ParentCaptureContext;
1139 for (Block b = Host.ContainingBlock; b != null; b = b.Parent){
1140 if (pcc.scopes [b.ID] != null){
1141 pcc.LinkScope (scope, b.ID);