**** Merged r40457 from MCS ****
[mono.git] / mcs / gmcs / anonymous.cs
1 //
2 // anonymous.cs: Support for anonymous methods
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximain.com)
6 //
7 // (C) 2003, 2004 Novell, Inc.
8 //
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
11 //
12 //
13 //
14
15 using System;
16 using System.Text;
17 using System.Collections;
18 using System.Reflection;
19 using System.Reflection.Emit;
20
21 namespace Mono.CSharp {
22
23         public class AnonymousMethod : Expression {
24                 // Used to generate unique method names.
25                 static int anonymous_method_count;
26                     
27                 // An array list of AnonymousMethodParameter or null
28                 public Parameters Parameters;
29                 
30                 //
31                 // The block that makes up the body for the anonymous mehtod
32                 //
33                 public ToplevelBlock Block;
34
35                 //
36                 // The container block for this anonymous method.
37                 //
38                 public Block ContainingBlock;
39
40                 //
41                 // The implicit method we create
42                 //
43                 public Method method;
44
45                 MethodInfo invoke_mb;
46                 
47                 // The emit context for the anonymous method
48                 public EmitContext aec;
49                 public InternalParameters amp;
50                 bool unreachable;
51                 
52                 //
53                 // The modifiers applied to the method, we aggregate them
54                 //
55                 int method_modifiers = Modifiers.PRIVATE;
56                 
57                 //
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
61                 //
62                 public ScopeInfo Scope;
63
64                 //
65                 // Points to our container anonymous method if its present
66                 //
67                 public AnonymousMethod ContainerAnonymousMethod;
68                 
69                 public AnonymousMethod (Parameters parameters, ToplevelBlock container, ToplevelBlock block, Location l)
70                 {
71                         Parameters = parameters;
72                         Block = block;
73                         loc = l;
74
75                         //
76                         // The order is important: this setups the CaptureContext tree hierarchy.
77                         //
78                         container.SetHaveAnonymousMethods (l, this);
79                         block.SetHaveAnonymousMethods (l, this);
80                 }
81
82                 public override Expression DoResolve (EmitContext ec)
83                 {
84                         //
85                         // Set class type, set type
86                         //
87
88                         eclass = ExprClass.Value;
89
90                         //
91                         // This hack means `The type is not accessible
92                         // anywhere', we depend on special conversion
93                         // rules.
94                         // 
95                         type = TypeManager.anonymous_method_type;
96
97                         return this;
98                 }
99
100                 public override void Emit (EmitContext ec)
101                 {
102                         // nothing, as we only exist to not do anything.
103                 }
104
105                 //
106                 // Creates the host for the anonymous method
107                 //
108                 bool CreateMethodHost (EmitContext ec, Type return_type)
109                 {
110                         //
111                         // Crude hack follows: we replace the TypeBuilder during the
112                         // definition to get the method hosted in the right class
113                         //
114                         
115                         TypeBuilder current_type = ec.TypeContainer.TypeBuilder;
116                         TypeBuilder type_host = (Scope == null ) // || Scope.ScopeTypeBuilder == null)
117                                 ? current_type : Scope.ScopeTypeBuilder;
118
119                         if (current_type == null)
120                                 throw new Exception ("The current_type is null");
121                         
122                         if (type_host == null)
123                                 throw new Exception ("Type host is null");
124                         
125                         if (current_type == type_host && ec.IsStatic){
126                                 if (ec.IsStatic)
127                                         method_modifiers |= Modifiers.STATIC;
128                                 current_type = null;
129                         } 
130
131                         method = new Method (
132                                 (TypeContainer) ec.TypeContainer, null,
133                                 new TypeExpression (return_type, loc),
134                                 method_modifiers, false, new MemberName ("<#AnonymousMethod>" + anonymous_method_count++),
135                                 Parameters, null, loc);
136                         method.Block = Block;
137
138                         
139                         //
140                         // Swap the TypeBuilder while we define the method, then restore
141                         //
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;
147                         return res;
148                 }
149                 
150                 void Error_ParameterMismatch (Type t)
151                 {
152                         Report.Error (1661, loc, "Anonymous method could not be converted to delegate `" +
153                                       "{0}' since there is a parameter mismatch", t);
154         }
155
156                 //
157                 // Returns true if this anonymous method can be implicitly
158                 // converted to the delegate type `delegate_type'
159                 //
160                 public Expression Compatible (EmitContext ec, Type delegate_type, bool probe)
161                 {
162                         //
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.
165                         //
166
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);
170
171                         //
172                         // If implicit parameters are set, then we must check for out in the parameters
173                         // and flag it accordingly.
174                         //
175                         bool out_invalid_check = false;
176                         
177                         if (Parameters == null){
178                                 int i, j;
179                                 out_invalid_check = true;
180                                 
181                                 //
182                                 // We provide a set of inaccessible parameters
183                                 //
184                                 int params_idx = -1;
185                                 for (i = 0; i < invoke_pd.Count; i++){
186                                         if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
187                                                 params_idx = i;
188                                 }
189                                 int n = invoke_pd.Count - (params_idx != -1 ? 1 : 0);
190                                 Parameter [] fixedpars = new Parameter [n];
191                                 
192                                 for (i =  j = 0; i < invoke_pd.Count; i++){
193                                         if (invoke_pd.ParameterModifier (i) == Parameter.Modifier.PARAMS)
194                                                 continue;
195                                         fixedpars [j] = new Parameter (
196                                                 new TypeExpression (invoke_pd.ParameterType (i), loc),
197                                                 "+" + j, invoke_pd.ParameterModifier (i), null);
198                                         j++;
199                                 }
200                                 
201                                 Parameter variable = null;
202                                 if (params_idx != -1){
203                                         variable = new Parameter (
204                                                 new TypeExpression (invoke_pd.ParameterType (params_idx), loc),
205                                                 "+" + params_idx, invoke_pd.ParameterModifier (params_idx), null);
206                                 }
207
208                                 Parameters = new Parameters (fixedpars, variable, loc);
209                         }
210                         
211                         //
212                         // First, parameter types of `delegate_type' must be compatible
213                         // with the anonymous method.
214                         //
215                         amp = new InternalParameters (Parameters.GetParameterInfo (ec), Parameters);
216                         
217                         if (amp.Count != invoke_pd.Count){
218                                 if (!probe){
219                                         Report.Error (1593, loc, 
220                                               "Anonymous method has {0} parameters, while delegate requires {1}",
221                                               amp.Count, invoke_pd.Count);
222                                         Error_ParameterMismatch (delegate_type);
223                                 }
224                                 return null;
225                         }
226                         
227                         for (int i = 0; i < amp.Count; i++){
228                                 Parameter.Modifier amp_mod = amp.ParameterModifier (i);
229                                 if (amp_mod != invoke_pd.ParameterModifier (i)){
230                                         if (!probe){
231                                                 Report.Error (1676, loc, 
232                                                       "Signature mismatch in parameter modifier for parameter #0", i + 1);
233                                                 Error_ParameterMismatch (delegate_type);
234                                         }
235                                         return null;
236                                 }
237                                 
238                                 if (amp.ParameterType (i) != invoke_pd.ParameterType (i)){
239                                         if (!probe){
240                                                 Report.Error (1678, loc, 
241                                                                       "Signature mismatch in parameter {0}: need `{1}' got `{2}'", i + 1,
242                                                                       TypeManager.CSharpName (invoke_pd.ParameterType (i)),
243                                                                       TypeManager.CSharpName (amp.ParameterType (i)));
244                                                 Error_ParameterMismatch (delegate_type);
245                                         }
246                                         return null;
247                                 }
248                                 
249                                 if (out_invalid_check && (invoke_pd.ParameterModifier (i) & Parameter.Modifier.OUT) != 0){
250                                         if (!probe){
251                                                 Report.Error (1676, loc,"Parameter {0} must include the `out' modifier ", i+1);
252                                                 Error_ParameterMismatch (delegate_type);
253                                         }
254                                         return null;
255                                 }
256                         }
257
258                         //
259                         // If we are only probing, return ourselves
260                         //
261                         if (probe)
262                                 return this;
263                         
264                         //
265                         // Second: the return type of the delegate must be compatible with 
266                         // the anonymous type.   Instead of doing a pass to examine the block
267                         // we satisfy the rule by setting the return type on the EmitContext
268                         // to be the delegate type return type.
269                         //
270
271                         //MethodBuilder builder = method_data.MethodBuilder;
272                         //ILGenerator ig = builder.GetILGenerator ();
273
274                         
275                         aec = new EmitContext (
276                                 ec.TypeContainer, ec.DeclSpace, loc, null,
277                                 invoke_mb.ReturnType,
278                                 /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
279                                 (ec.InUnsafe ? Modifiers.UNSAFE : 0),
280                                 /* No constructor */ false);
281
282                         aec.CurrentAnonymousMethod = this;
283                         ContainerAnonymousMethod = ec.CurrentAnonymousMethod;
284                         ContainingBlock = ec.CurrentBlock;
285                 
286                         if (aec.ResolveTopBlock (ec, Block, amp, loc, out unreachable))
287                                 return new AnonymousDelegate (this, delegate_type, loc).Resolve (ec);
288
289                         return null;
290                 }
291
292                 public MethodBuilder GetMethodBuilder ()
293                 {
294                         return method.MethodData.MethodBuilder;
295                 }
296                 
297                 public bool EmitMethod (EmitContext ec)
298                 {
299                         if (!CreateMethodHost (ec, invoke_mb.ReturnType))
300                                 return false;
301
302                         MethodBuilder builder = GetMethodBuilder ();
303                         ILGenerator ig = builder.GetILGenerator ();
304                         aec.ig = ig;
305                         
306                         Parameters.LabelParameters (aec, builder, loc);
307
308                         //
309                         // Adjust based on the computed state of the
310                         // method from CreateMethodHost
311                         
312                         if ((method_modifiers & Modifiers.STATIC) != 0)
313                                 aec.IsStatic = true;
314                         
315                         aec.EmitMeta (Block, amp);
316                         aec.EmitResolvedTopBlock (Block, unreachable);
317                         return true;
318                 }
319
320                 public static void Error_AddressOfCapturedVar (string name, Location loc)
321                 {
322                         Report.Error (1686, loc,
323                                       "Variable {0} is captured in an anonymous method and its address is also being taken: they are exclusive", name);
324                 }
325         }
326
327         //
328         // This will emit the code for the delegate, as well delegate creation on the host
329         //
330         public class AnonymousDelegate : DelegateCreation {
331                 AnonymousMethod am;
332
333                 public AnonymousDelegate (AnonymousMethod am, Type target_type, Location l)
334                 {
335                         type = target_type;
336                         loc = l;
337                         this.am = am;
338                 }
339
340                 public override Expression DoResolve (EmitContext ec)
341                 {
342                         eclass = ExprClass.Value;
343
344                         return this;
345                 }
346                 
347                 public override void Emit (EmitContext ec)
348                 {
349                         if (!am.EmitMethod (ec))
350                                 return;
351
352                         //
353                         // Now emit the delegate creation.
354                         //
355                         if ((am.method.ModFlags & Modifiers.STATIC) == 0)
356                                 delegate_instance_expression = new AnonymousInstance (am);
357                         
358                         Expression ml = Expression.MemberLookup (ec, type, ".ctor", loc);
359                         constructor_method = ((MethodGroupExpr) ml).Methods [0];
360                         delegate_method = am.GetMethodBuilder ();
361                         base.Emit (ec);
362                 }
363
364                 class AnonymousInstance : Expression {
365                         AnonymousMethod am;
366                         
367                         public AnonymousInstance (AnonymousMethod am)
368                         {
369                                 this.am = am;
370                                 eclass = ExprClass.Value;
371                         }
372
373                         public override Expression DoResolve (EmitContext ec)
374                         {
375                                 return this;
376                         }
377                         
378                         public override void Emit (EmitContext ec)
379                         {
380                                 am.aec.EmitMethodHostInstance (ec, am);
381                         }
382                 }
383         }
384
385         class CapturedParameter {
386                 public Type Type;
387                 public FieldBuilder FieldBuilder;
388                 public int Idx;
389
390                 public CapturedParameter (Type type, int idx)
391                 {
392                         Type = type;
393                         Idx = idx;
394                 }
395         }
396
397         //
398         // Here we cluster all the variables captured on a given scope, we also
399         // keep some extra information that might be required on each scope.
400         //
401         public class ScopeInfo {
402                 public CaptureContext CaptureContext;
403                 public ScopeInfo ParentScope;
404                 public Block ScopeBlock;
405                 public bool NeedThis = false;
406                 public bool HostsParameters = false;
407                 
408                 // For tracking the number of scopes created.
409                 public int id;
410                 static int count;
411                 bool inited = false;
412                 
413                 ArrayList locals = new ArrayList ();
414                 ArrayList children = new ArrayList ();
415
416                 //
417                 // The types and fields generated
418                 //
419                 public TypeBuilder ScopeTypeBuilder;
420                 public ConstructorBuilder ScopeConstructor;
421                 public FieldBuilder THIS;
422                 public FieldBuilder ParentLink;
423
424                 //
425                 // Points to the object of type `ScopeTypeBuilder' that
426                 // holds the data for the scope
427                 //
428                 public LocalBuilder ScopeInstance;
429
430                 
431                 public ScopeInfo (CaptureContext cc, Block b)
432                 {
433                         CaptureContext = cc;
434                         ScopeBlock = b;
435                         id = count++;
436
437                         cc.AddScope (this);
438                 }
439
440                 public void AddLocal (LocalInfo li)
441                 {
442                         if (locals.Contains (li))
443                                 return;
444
445                         locals.Add (li);
446 }
447         
448                 public bool IsCaptured (LocalInfo li)
449                 {
450                         return locals.Contains (li);
451                 }
452                 
453                 public void AddChild (ScopeInfo si)
454                 {
455                         if (children.Contains (si))
456                                 return;
457                         children.Add (si);
458                 }
459
460                 static int indent = 0;
461
462                 void Pad ()
463                 {
464                         for (int i = 0; i < indent; i++)
465                                 Console.Write ("    ");
466                 }
467
468                 void EmitDebug ()
469                 {
470                         //Console.WriteLine (Environment.StackTrace);
471                         Pad ();
472                         Console.WriteLine ("START");
473                         indent++;
474                         Pad ();
475                         Console.WriteLine ("NeedThis=" + NeedThis);
476                         foreach (LocalInfo li in locals){
477                                 Pad ();
478                                 Console.WriteLine ("var {0}", li.Name);
479                         }
480                         
481                         foreach (ScopeInfo si in children)
482                                 si.EmitDebug ();
483                         indent--;
484                         Pad ();
485                         Console.WriteLine ("END");
486                 }
487                 
488                 public string MakeHelperName ()
489                 {
490                         return String.Format ("<>AnonHelp<{0}>", id);
491                 }
492
493                 public void EmitScopeConstructor ()
494                 {
495                         Type [] constructor_types = TypeManager.NoTypes;
496                         Parameters constructor_parameters = Parameters.EmptyReadOnlyParameters;
497                         ScopeConstructor = ScopeTypeBuilder.DefineConstructor (
498                                 MethodAttributes.Public | MethodAttributes.HideBySig |
499                                 MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
500                                 CallingConventions.HasThis, constructor_types);
501                         InternalParameters parameter_info = new InternalParameters (constructor_types, constructor_parameters);
502                         TypeManager.RegisterMethod (ScopeConstructor, parameter_info, constructor_types);
503
504                         ILGenerator cig = ScopeConstructor.GetILGenerator ();
505                         cig.Emit (OpCodes.Ldarg_0);
506                         cig.Emit (OpCodes.Call, TypeManager.object_ctor);
507                         cig.Emit (OpCodes.Ret);
508                 }
509                 
510                 public void EmitScopeType (EmitContext ec)
511                 {
512                         //EmitDebug ();
513
514                         if (ScopeTypeBuilder != null)
515                                 return;
516                         
517                         TypeBuilder container = ec.TypeContainer.TypeBuilder;
518
519                         ScopeTypeBuilder = container.DefineNestedType (
520                                 MakeHelperName (), TypeAttributes.AutoLayout | TypeAttributes.Class | TypeAttributes.NestedAssembly,
521                                 TypeManager.object_type, null);
522
523                         if (NeedThis)
524                                 THIS = ScopeTypeBuilder.DefineField ("<>THIS", container, FieldAttributes.Assembly);
525
526                         if (ParentScope != null){
527                                 if (ParentScope.ScopeTypeBuilder == null){
528                                         throw new Exception (String.Format ("My parent has not been initialized {0} and {1}", ParentScope, this));
529                                 }
530                                 
531                                 ParentLink = ScopeTypeBuilder.DefineField ("<>parent", ParentScope.ScopeTypeBuilder,
532                                                                            FieldAttributes.Assembly);
533                         }
534                         
535                         if (NeedThis && ParentScope != null)
536                                 throw new Exception ("I was not expecting THIS && having a parent");
537
538                         foreach (LocalInfo info in locals){
539                                 info.FieldBuilder = ScopeTypeBuilder.DefineField (
540                                         info.Name, info.VariableType, FieldAttributes.Assembly);
541                         }
542
543                         if (HostsParameters){
544                                 Hashtable captured_parameters = CaptureContext.captured_parameters;
545                                 
546                                 foreach (DictionaryEntry de in captured_parameters){
547                                         string name = (string) de.Key;
548                                         CapturedParameter cp = (CapturedParameter) de.Value;
549                                         FieldBuilder fb;
550                                         
551                                         fb = ScopeTypeBuilder.DefineField ("<p:" + name + ">", cp.Type, FieldAttributes.Assembly);
552                                         cp.FieldBuilder = fb;
553                                 }
554                         }
555
556                         EmitScopeConstructor ();
557                         foreach (ScopeInfo si in children){
558                                 si.EmitScopeType (ec);
559                         }
560                 }
561
562                 public void CloseTypes ()
563                 {
564                         RootContext.RegisterHelperClass (ScopeTypeBuilder);
565                         foreach (ScopeInfo si in children)
566                                 si.CloseTypes ();
567                 }
568
569                 //
570                 // Emits the initialization code for the scope
571                 //
572                 public void EmitInitScope (EmitContext ec)
573                 {
574                         ILGenerator ig = ec.ig;
575
576                         if (inited)
577                                 return;
578
579                         ig.Emit (OpCodes.Newobj, (ConstructorInfo) ScopeConstructor);
580                         ScopeInstance = ig.DeclareLocal (ScopeTypeBuilder);
581                         ig.Emit (OpCodes.Stloc, ScopeInstance);
582
583                         if (THIS != null){
584                                 ig.Emit (OpCodes.Ldloc, ScopeInstance);
585                                 ig.Emit (OpCodes.Ldarg_0);
586                                 ig.Emit (OpCodes.Stfld, THIS);
587                         }
588
589                         //
590                         // Copy the parameter values, if any
591                         //
592                         int extra = ec.IsStatic ? 0 : 1;
593                         if (HostsParameters){
594                                 Hashtable captured_parameters = CaptureContext.captured_parameters;
595                                 
596                                 foreach (DictionaryEntry de in captured_parameters){
597                                         CapturedParameter cp = (CapturedParameter) de.Value;
598
599                                         ig.Emit (OpCodes.Ldloc, ScopeInstance);
600                                         ParameterReference.EmitLdArg (ig, cp.Idx + extra);
601                                         ig.Emit (OpCodes.Stfld, cp.FieldBuilder);
602                                 }
603                         }
604                         
605                         if (ParentScope != null){
606                                 if (!ParentScope.inited)
607                                         ParentScope.EmitInitScope (ec);
608                                 
609                                 //
610                                 // Only emit initialization in our capturecontext world
611                                 //
612                                 if (ParentScope.CaptureContext == CaptureContext){
613                                         ig.Emit (OpCodes.Ldloc, ScopeInstance);
614                                         ig.Emit (OpCodes.Ldloc, ParentScope.ScopeInstance);
615                                         ig.Emit (OpCodes.Stfld, ParentLink);
616                                 } else {
617                                         ig.Emit (OpCodes.Ldloc, ScopeInstance);
618                                         ig.Emit (OpCodes.Ldarg_0);
619                                         ig.Emit (OpCodes.Stfld, ParentLink);
620                                 }
621                         }
622                         inited = true;
623                 }
624
625                 static void DoPath (StringBuilder sb, ScopeInfo start)
626                 {
627                         if (start.ParentScope != null){
628                                 DoPath (sb, start.ParentScope);
629                                 sb.Append (", ");
630                         }
631                         sb.Append ((start.id).ToString ());
632                 }
633                 
634                 public override string ToString ()
635                 {
636                         StringBuilder sb = new StringBuilder ();
637                         
638                         sb.Append ("{");
639                         if (CaptureContext != null){
640                                 sb.Append (CaptureContext.ToString ());
641                                 sb.Append (":");
642                         }
643
644                         DoPath (sb, this);
645                         sb.Append ("}");
646
647                         return sb.ToString ();
648                 }
649         }
650
651         //
652         // CaptureContext objects are created on demand if a method has
653         // anonymous methods and kept on the ToplevelBlock.
654         //
655         // If they exist, all ToplevelBlocks in the containing block are
656         // linked together (children pointing to their parents).
657         //
658         public class CaptureContext {
659                 public static int count;
660                 public int cc_id;
661                 public Location loc;
662                 
663                 //
664                 // Points to the toplevel block that owns this CaptureContext
665                 //
666                 ToplevelBlock toplevel_owner;
667                 Hashtable scopes = new Hashtable ();
668                 bool have_captured_vars = false;
669                 ScopeInfo topmost = null;
670
671                 //
672                 // Captured fields
673                 //
674                 Hashtable captured_fields = new Hashtable ();
675                 Hashtable captured_variables = new Hashtable ();
676                 public Hashtable captured_parameters = new Hashtable ();
677                 public AnonymousMethod Host;
678                 
679                 public CaptureContext (ToplevelBlock toplevel_owner, Location loc, AnonymousMethod host)
680                 {
681                         cc_id = count++;
682                         this.toplevel_owner = toplevel_owner;
683                         this.loc = loc;
684
685                         if (host != null)
686                                 Host = host;
687                 }
688
689                 void DoPath (StringBuilder sb, CaptureContext cc)
690                 {
691                         if (cc.ParentCaptureContext != null){
692                                 DoPath (sb, cc.ParentCaptureContext);
693                                 sb.Append (".");
694                         }
695                         sb.Append (cc_id.ToString ());
696                 }
697                 
698                 public override string ToString ()
699                 {
700                         StringBuilder sb = new StringBuilder ();
701                         sb.Append ("[");
702                         DoPath (sb, this);
703                         sb.Append ("]");
704                         return sb.ToString ();
705                 }
706
707                 public ToplevelBlock ParentToplevel {
708                         get {
709                                 return toplevel_owner.Container;
710                         }
711                 }
712
713                 public CaptureContext ParentCaptureContext {
714                         get {
715                                 ToplevelBlock parent = ParentToplevel;
716                                 
717                                 return (parent == null) ? null : parent.CaptureContext;
718                         }
719                 }
720
721                 // Returns the deepest of two scopes
722                 public ScopeInfo Deepest (ScopeInfo a, ScopeInfo b)
723                 {
724                         ScopeInfo p;
725
726                         if (a == null)
727                                 return b;
728                         if (b == null)
729                                 return a;
730                         if (a == b)
731                                 return a;
732
733                         //
734                         // If they Scopes are on the same CaptureContext, we do the double
735                         // checks just so if there is an invariant change in the future,
736                         // we get the exception at the end
737                         //
738                         for (p = a; p != null; p = p.ParentScope)
739                                 if (p == b)
740                                         return a;
741                         
742                         for (p = b; p != null; p = p.ParentScope)
743                                 if (p == a)
744                                         return b;
745
746                         CaptureContext ca = a.CaptureContext;
747                         CaptureContext cb = b.CaptureContext;
748
749                         for (CaptureContext c = ca; c != null; c = c.ParentCaptureContext)
750                                 if (c == cb)
751                                         return a;
752
753                         for (CaptureContext c = cb; c != null; c = c.ParentCaptureContext)
754                                 if (c == ca)
755                                         return b;
756                         throw new Exception ("Should never be reached");
757                 }
758
759                 void AdjustMethodScope (AnonymousMethod am, ScopeInfo scope)
760                 {
761                         am.Scope = Deepest (am.Scope, scope);
762                 }
763
764                 void LinkScope (ScopeInfo scope, int id)
765                 {
766                         ScopeInfo parent = (ScopeInfo) scopes [id];
767                         scope.ParentScope = parent;
768                         parent.AddChild (scope);
769
770                         if (scope == topmost)
771                                 topmost = parent;
772                 }
773                 
774                 public void AddLocal (AnonymousMethod am, LocalInfo li)
775                 {
776                         if (li.Block.Toplevel != toplevel_owner){
777                                 ParentCaptureContext.AddLocal (am, li);
778                                 return;
779                         }
780                         int block_id = li.Block.ID;
781                         ScopeInfo scope;
782                         if (scopes [block_id] == null){
783                                 scope = new ScopeInfo (this, li.Block);
784                                 scopes [block_id] = scope;
785                         } else
786                                 scope = (ScopeInfo) scopes [block_id];
787
788                         if (topmost == null){
789                                 topmost = scope;
790                         } else {
791                                 // Link to parent
792                                 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
793                                         if (scopes [b.ID] != null){
794                                                 LinkScope (scope, b.ID);
795                                                 break;
796                                         }
797                                 }
798
799                                 if (scope.ParentScope == null && ParentCaptureContext != null){
800                                         CaptureContext pcc = ParentCaptureContext;
801                                         
802                                         for (Block b = am.ContainingBlock; b != null; b = b.Parent){
803                                                 if (pcc.scopes [b.ID] != null){
804                                                         pcc.LinkScope (scope, b.ID);
805                                                         break;
806                                                 }
807                                         }
808                                 }
809                         }
810
811                         //
812                         // Adjust the owner
813                         //
814                         if (Host != null)
815                                 AdjustMethodScope (Host, topmost);
816
817                         //
818                         // Adjust the user
819                         //
820                         AdjustMethodScope (am, scope);
821                         
822                         if (captured_variables [li] != null)
823                                 return;
824                         
825                         have_captured_vars = true;
826                         captured_variables [li] = li;
827                         scope.AddLocal (li);
828                 }
829
830                 //
831                 // Retursn the CaptureContext for the block that defines the parameter `name'
832                 //
833                 static CaptureContext _ContextForParameter (ToplevelBlock current, string name)
834                 {
835                         ToplevelBlock container = current.Container;
836                         if (container != null){
837                                 CaptureContext cc = _ContextForParameter (container, name);
838                                 if (cc != null)
839                                         return cc;
840                         }
841                         if (current.IsParameterReference (name))
842                                 return current.ToplevelBlockCaptureContext;
843                         return null;
844                 }
845
846                 static CaptureContext ContextForParameter (ToplevelBlock current, string name)
847                 {
848                         CaptureContext cc = _ContextForParameter (current, name);
849                         if (cc == null)
850                                 throw new Exception (String.Format ("request for parameteter {0} failed: not found", name));
851                         return cc;
852                 }
853                 
854                 //
855                 // Records the captured parameter at the appropriate CaptureContext
856                 //
857                 public void AddParameter (EmitContext ec, AnonymousMethod am, string name, Type t, int idx)
858                 {
859                         CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
860
861                         cc.AddParameterToContext (am, name, t, idx);
862                 }
863
864                 //
865                 // Records the parameters in the context
866                 //
867                 void AddParameterToContext (AnonymousMethod am, string name, Type t, int idx)
868                 {
869                         if (captured_parameters == null)
870                                 captured_parameters = new Hashtable ();
871                         if (captured_parameters [name] != null)
872                                 return;
873                         captured_parameters [name] = new CapturedParameter (t, idx);
874
875                         if (topmost == null){
876                                 //
877                                 // Create one ScopeInfo, if there are none.
878                                 //
879                                 topmost = new ScopeInfo (this, toplevel_owner);
880                                 scopes [toplevel_owner.ID] = topmost;
881                         } else {
882                                 //
883                                 // If the topmost ScopeInfo is not at the topblock level, insert
884                                 // a new ScopeInfo there.
885                                 //
886                                 if (topmost.ScopeBlock != toplevel_owner){
887                                         ScopeInfo par_si = new ScopeInfo (this, toplevel_owner);
888                                         scopes [toplevel_owner.ID] = topmost;
889                                         topmost.ParentScope = par_si;
890                                         topmost = par_si;
891                                 }
892                         }
893                         
894                         topmost.HostsParameters = true;
895                         AdjustMethodScope (am, topmost);
896                 }
897
898                 //
899                 // Captured fields are only recorded on the topmost CaptureContext, because that
900                 // one is the one linked to the owner of instance fields
901                 //
902                 public void AddField (FieldExpr fe)
903                 {
904                         if (fe.FieldInfo.IsStatic)
905                                 throw new Exception ("Attempt to register a static field as a captured field");
906                         
907                         CaptureContext parent = ParentCaptureContext;
908                         if (parent != null)
909                                 parent.AddField (fe);
910                         else
911                                 captured_fields [fe] = fe;
912                 }
913                 
914
915                 public bool HaveCapturedVariables {
916                         get {
917                                 return have_captured_vars;
918                         }
919                 }
920                 
921                 public bool HaveCapturedFields {
922                         get {
923                                 CaptureContext parent = ParentCaptureContext;
924                                 if (parent != null)
925                                         return parent.HaveCapturedFields;
926                                 return captured_fields.Count > 0;
927                         }
928                 }
929
930                 public bool IsCaptured (LocalInfo local)
931                 {
932                         foreach (ScopeInfo si in scopes.Values){
933                                 if (si.IsCaptured (local))
934                                         return true;
935                         }
936                         return false;
937                 }
938
939                 //
940                 // Returns whether the parameter is captured
941                 //
942                 public bool IsParameterCaptured (string name)
943                 {
944                         if (ParentCaptureContext != null && ParentCaptureContext.IsParameterCaptured (name))
945                                 return true;
946                         
947                         if (captured_parameters != null)
948                                 return captured_parameters [name] != null;
949                         return false;
950                 }
951
952                 public void EmitAnonymousHelperClasses (EmitContext ec)
953                 {
954                         if (topmost != null){
955                                 topmost.NeedThis = HaveCapturedFields;
956                                 topmost.EmitScopeType (ec);
957                         } 
958                 }
959
960                 public void CloseAnonymousHelperClasses ()
961                 {
962                         if (topmost != null)
963                                 topmost.CloseTypes ();
964                 }
965
966                 ScopeInfo GetScopeFromBlock (EmitContext ec, Block b)
967                 {
968                         ScopeInfo si;
969                         
970                         si = (ScopeInfo) scopes [b.ID];
971                         if (si == null)
972                                 throw new Exception ("Si is null for block " + b.ID);
973                         si.EmitInitScope (ec);
974
975                         return si;
976                 }
977
978                 //
979                 // Emits the opcodes necessary to load the instance of the captured
980                 // variable in `li'
981                 //
982                 public void EmitCapturedVariableInstance (EmitContext ec, LocalInfo li, AnonymousMethod am)
983                 {
984                         ILGenerator ig = ec.ig;
985                         ScopeInfo si;
986                         
987                         if (li.Block.Toplevel == toplevel_owner){
988                                 si = GetScopeFromBlock (ec, li.Block);
989                                 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
990                                 return;
991                         }
992
993                         si = am.Scope;
994                         ig.Emit (OpCodes.Ldarg_0);
995                         if (si != null){
996                                 while (si.ScopeBlock.ID != li.Block.ID){
997                                         if (si.ParentLink != null)
998                                                 ig.Emit (OpCodes.Ldfld, si.ParentLink);
999                                         si = si.ParentScope;
1000                                         if (si == null) 
1001                                                 throw new Exception (
1002                                                              String.Format ("Never found block {0} starting at {1} while looking up {2}",
1003                                                                             li.Block.ID, am.Scope.ScopeBlock.ID, li.Name));
1004                                 }
1005                         }
1006                 }
1007
1008                 //
1009                 // Internal routine that loads the instance to reach parameter `name'
1010                 //
1011                 void EmitParameterInstance (EmitContext ec, string name)
1012                 {
1013                         CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1014                         if (cc != this){
1015                                 cc.EmitParameterInstance (ec, name);
1016                                 return;
1017                         }
1018                         
1019                         CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1020                         if (par_info != null){
1021                                 // 
1022                                 // FIXME: implementing this.
1023                                 //
1024                         }
1025                         ILGenerator ig = ec.ig;
1026
1027                         ScopeInfo si;
1028                         if (ec.CurrentBlock == toplevel_owner){
1029                                 si = GetScopeFromBlock (ec, toplevel_owner);
1030                                 ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1031                                 return;
1032                         }
1033                         
1034                         si = ec.CurrentAnonymousMethod.Scope;
1035                         ig.Emit (OpCodes.Ldarg_0);
1036                         if (si != null){
1037                                 while (si.ParentLink != null) {
1038                                         ig.Emit (OpCodes.Ldfld, si.ParentLink);
1039                                         si = si.ParentScope;
1040                                 } 
1041                         }
1042                 }
1043
1044                 //
1045                 // Emits the code necessary to load the parameter named `name' within
1046                 // an anonymous method.
1047                 //
1048                 public void EmitParameter (EmitContext ec, string name)
1049                 {
1050                         CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1051                         if (cc != this){
1052                                 cc.EmitParameter (ec, name);
1053                                 return;
1054                         }
1055                         EmitParameterInstance (ec, name);
1056                         CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1057                         if (par_info != null){
1058                                 // 
1059                                 // FIXME: implementing this.
1060                                 //
1061                         }
1062                         ec.ig.Emit (OpCodes.Ldfld, par_info.FieldBuilder);
1063                 }
1064
1065                 //
1066                 // Implements the assignment of `source' to the paramenter named `name' within
1067                 // an anonymous method.
1068                 //
1069                 public void EmitAssignParameter (EmitContext ec, string name, Expression source, bool leave_copy, bool prepare_for_load)
1070                 {
1071                         CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1072                         if (cc != this){
1073                                 cc.EmitAssignParameter (ec, name, source, leave_copy, prepare_for_load);
1074                                 return;
1075                         }
1076                         ILGenerator ig = ec.ig;
1077                         CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1078
1079                         EmitParameterInstance (ec, name);
1080                         source.Emit (ec);
1081                         if (leave_copy)
1082                                 ig.Emit (OpCodes.Dup);
1083                         ig.Emit (OpCodes.Stfld, par_info.FieldBuilder);
1084                 }
1085
1086                 //
1087                 // Emits the address for the parameter named `name' within
1088                 // an anonymous method.
1089                 //
1090                 public void EmitAddressOfParameter (EmitContext ec, string name)
1091                 {
1092                         CaptureContext cc = ContextForParameter (ec.CurrentBlock.Toplevel, name);
1093                         if (cc != this){
1094                                 cc.EmitAddressOfParameter (ec, name);
1095                                 return;
1096                         }
1097                         EmitParameterInstance (ec, name);
1098                         CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
1099                         ec.ig.Emit (OpCodes.Ldflda, par_info.FieldBuilder);
1100                 }
1101
1102                 //
1103                 // The following methods are only invoked on the host for the
1104                 // anonymous method.
1105                 //
1106                 public void EmitMethodHostInstance (EmitContext target, AnonymousMethod am)
1107                 {
1108                         ILGenerator ig = target.ig;
1109                         ScopeInfo si = am.Scope;
1110                         
1111                         if (si == null){
1112                                 ig.Emit (OpCodes.Ldarg_0);
1113                                 return;
1114                         }
1115
1116                         si.EmitInitScope (target);
1117                         ig.Emit (OpCodes.Ldloc, si.ScopeInstance);
1118                 }
1119
1120                 ArrayList all_scopes = new ArrayList ();
1121                 
1122                 public void AddScope (ScopeInfo si)
1123                 {
1124                         all_scopes.Add (si);
1125                         toplevel_owner.RegisterCaptureContext (this);
1126                 }
1127
1128                 //
1129                 // Links any scopes that were not linked previously
1130                 //
1131                 public void AdjustScopes ()
1132                 {
1133                         foreach (ScopeInfo scope in all_scopes){
1134                                 if (scope.ParentScope != null)
1135                                         continue;
1136
1137                                 for (Block b = scope.ScopeBlock.Parent; b != null; b = b.Parent){
1138                                         if (scopes [b.ID] != null){
1139                                                 LinkScope (scope, b.ID);
1140                                                 break;
1141                                         }
1142                                 }
1143
1144                                 if (scope.ParentScope == null && ParentCaptureContext != null){
1145                                         CaptureContext pcc = ParentCaptureContext;
1146                                         
1147                                         for (Block b = Host.ContainingBlock; b != null; b = b.Parent){
1148                                                 if (pcc.scopes [b.ID] != null){
1149                                                         pcc.LinkScope (scope, b.ID);
1150                                                         break;
1151                                                 }
1152                                         }
1153                                 }
1154                         }
1155                 }
1156         }
1157 }