* statement.cs (ToplevelBlock.ResolveMeta): Move CS0136 checks ...
[mono.git] / mcs / mcs / 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 abstract class CompilerGeneratedClass : Class
24         {
25                 GenericMethod generic_method;
26                 static int next_index = 0;
27
28                 private static MemberName MakeProxyName (GenericMethod generic, Location loc)
29                 {
30                         string name = MakeName ("CompilerGenerated");
31                         if (generic != null) {
32                                 TypeArguments args = new TypeArguments (loc);
33                                 foreach (TypeParameter tparam in generic.CurrentTypeParameters)
34                                         args.Add (new SimpleName (tparam.Name, loc));
35                                 return new MemberName (name, args, loc);
36                         } else
37                                 return new MemberName (name, loc);
38                 }
39
40                 public static string MakeName (string prefix)
41                 {
42                         return "<>c__" + prefix + next_index++;
43                 }
44
45                 protected CompilerGeneratedClass (DeclSpace parent, GenericMethod generic,
46                                                   int mod, Location loc)
47                         : base (parent.NamespaceEntry, parent,
48                                 MakeProxyName (generic, loc), mod | Modifiers.COMPILER_GENERATED, null)
49                 {
50                         this.generic_method = generic;
51
52                         if (generic != null) {
53                                 ArrayList list = new ArrayList ();
54                                 foreach (TypeParameter tparam in generic.TypeParameters) {
55                                         if (tparam.Constraints != null)
56                                                 list.Add (tparam.Constraints.Clone ());
57                                 }
58                                 SetParameterInfo (list);
59                         }
60
61                         parent.PartialContainer.AddCompilerGeneratedClass (this);
62                 }
63
64                 protected override bool DefineNestedTypes ()
65                 {
66                         RootContext.RegisterCompilerGeneratedType (TypeBuilder);
67                         return base.DefineNestedTypes ();
68                 }
69
70                 protected override bool DoDefineMembers ()
71                 {
72                         members_defined = true;
73
74                         if (!base.DoDefineMembers ())
75                                 return false;
76
77                         if (CompilerGenerated != null) {
78                                 foreach (CompilerGeneratedClass c in CompilerGenerated) {
79                                         if (!c.DefineMembers ())
80                                                 throw new InternalErrorException ();
81                                 }
82                         }
83
84                         return true;
85                 }
86
87                 protected override bool DoResolveMembers ()
88                 {
89                         if (CompilerGenerated != null) {
90                                 foreach (CompilerGeneratedClass c in CompilerGenerated) {
91                                         if (!c.ResolveMembers ())
92                                                 return false;
93                                 }
94                         }
95
96                         return base.DoResolveMembers ();
97                 }
98
99                 public GenericMethod GenericMethod {
100                         get { return generic_method; }
101                 }
102
103                 public Parameters InflateParameters (Parameters ps)
104                 {
105                         if (generic_method == null)
106                                 return ps;
107
108                         int n = ps.Count;
109                         if (n == 0)
110                                 return ps;
111
112                         Parameter[] inflated_params = new Parameter [n];
113                         Type[] inflated_types = new Type [n];
114
115                         for (int i = 0; i < n; ++i) {
116                                 Parameter p = ps [i];
117                                 Type it = InflateType (p.ExternalType ()).ResolveAsTypeTerminal (this, false).Type;
118                                 inflated_types [i] = it;
119                                 inflated_params [i] = new Parameter (it, p.Name, p.ModFlags, p.OptAttributes, p.Location);
120                         }
121                         return new Parameters (inflated_params, inflated_types);
122                 }
123
124                 public TypeExpr InflateType (Type it)
125                 {
126 #if GMCS_SOURCE
127                         if (generic_method == null)
128                                 return new TypeExpression (it, Location);
129
130                         if (it.IsGenericParameter && (it.DeclaringMethod != null)) {
131                                 int pos = it.GenericParameterPosition;
132                                 it = CurrentTypeParameters [pos].Type;
133                         } else if (it.IsGenericType) {
134                                 Type[] args = it.GetGenericArguments ();
135
136                                 TypeArguments inflated = new TypeArguments (Location);
137                                 foreach (Type t in args)
138                                         inflated.Add (InflateType (t));
139
140                                 return new ConstructedType (it, inflated, Location);
141                         } else if (it.IsArray) {
142                                 TypeExpr et_expr = InflateType (it.GetElementType ());
143                                 int rank = it.GetArrayRank ();
144
145                                 Type et = et_expr.ResolveAsTypeTerminal (this, false).Type;
146                                 it = et.MakeArrayType (rank);
147                         }
148 #endif
149
150                         return new TypeExpression (it, Location);
151                 }
152
153                 public Field CaptureVariable (string name, TypeExpr type)
154                 {
155                         if (members_defined)
156                                 throw new InternalErrorException ("Helper class already defined!");
157                         if (type == null)
158                                 throw new ArgumentNullException ();
159
160                         return new CapturedVariableField (this, name, type);
161                 }
162
163                 bool members_defined;
164
165                 internal void CheckMembersDefined ()
166                 {
167                         if (members_defined)
168                                 throw new InternalErrorException ("Helper class already defined!");
169                 }
170
171                 protected class CapturedVariableField : Field
172                 {
173                         public CapturedVariableField (CompilerGeneratedClass helper, string name,
174                                                       TypeExpr type)
175                                 : base (helper, type, Modifiers.INTERNAL, name, null, helper.Location)
176                         {
177                                 helper.AddField (this);
178                         }
179                 }
180         }
181
182         public class ScopeInfo : CompilerGeneratedClass
183         {
184                 protected readonly RootScopeInfo RootScope;
185                 new public readonly DeclSpace Parent;
186                 public readonly int ID = ++next_id;
187                 public Block ScopeBlock;
188
189                 static int next_id;
190
191                 public static ScopeInfo CreateScope (Block block)
192                 {
193                         ToplevelBlock toplevel = block.Toplevel;
194                         AnonymousContainer ac = toplevel.AnonymousContainer;
195
196                         Report.Debug (128, "CREATE SCOPE", block, block.ScopeInfo, toplevel, ac);
197
198                         if (ac == null)
199                                 return new ScopeInfo (block, toplevel.RootScope.Parent,
200                                                       toplevel.RootScope.GenericMethod);
201
202                         Report.Debug (128, "CREATE SCOPE #1", ac, ac.Host, ac.Scope, ac.Block,
203                                       ac.Container, ac.ContainerAnonymousMethod,
204                                       ac.Location);
205
206                         Block b;
207                         ScopeInfo parent = null;
208
209                         for (b = ac.Block; b != null; b = b.Parent) {
210                                 if (b.ScopeInfo != null) {
211                                         parent = b.ScopeInfo;
212                                         break;
213                                 }
214                         }
215
216                         Report.Debug (128, "CREATE SCOPE #2", parent);
217
218                         ScopeInfo new_scope = new ScopeInfo (block, parent, null);
219
220                         Report.Debug (128, "CREATE SCOPE #3", new_scope);
221
222                         return new_scope;
223                 }
224
225                 private static int default_modflags (DeclSpace parent)
226                 {
227                         return parent is CompilerGeneratedClass ? Modifiers.PUBLIC : Modifiers.PRIVATE;
228                 }
229
230                 protected ScopeInfo (Block block, DeclSpace parent, GenericMethod generic)
231                         : base (parent, generic, default_modflags (parent), block.StartLocation)
232                 {
233                         Parent = parent;
234                         RootScope = block.Toplevel.RootScope;
235                         ScopeBlock = block;
236
237                         Report.Debug (128, "NEW SCOPE", this, block,
238                                       block.Parent, block.Toplevel);
239
240                         RootScope.AddScope (this);
241                 }
242
243                 protected ScopeInfo (ToplevelBlock toplevel, DeclSpace parent,
244                                      GenericMethod generic, Location loc)
245                         : base (parent, generic, default_modflags (parent), loc)
246                 {
247                         Parent = parent;
248                         RootScope = (RootScopeInfo) this;
249                         ScopeBlock = toplevel;
250
251                         Report.Debug (128, "NEW ROOT SCOPE", this, toplevel, loc);
252                 }
253
254                 protected ScopeInitializer scope_initializer;
255
256                 Hashtable locals = new Hashtable ();
257                 Hashtable captured_scopes = new Hashtable ();
258                 Hashtable captured_params;
259
260                 protected CapturedScope[] CapturedScopes {
261                         get {
262                                 CapturedScope[] list = new CapturedScope [captured_scopes.Count];
263                                 captured_scopes.Values.CopyTo (list, 0);
264                                 return list;
265                         }
266                 }
267
268                 protected CapturedVariable GetCapturedScope (ScopeInfo scope)
269                 {
270                         return (CapturedVariable) captured_scopes [scope];
271                 }
272
273                 protected void EmitScopeInstance (EmitContext ec)
274                 {
275                         if (scope_initializer == null) {
276                                 //
277                                 // This is needed if someone overwrites the Emit method
278                                 // of Statement and manually calls Block.Emit without
279                                 // this snippet first:
280                                 // 
281                                 //   ec.EmitScopeInitFromBlock (The_Block);
282                                 //   The_Block.Emit (ec);
283                                 // 
284                                 throw new InternalErrorException ();
285                         }
286
287                         scope_initializer.Emit (ec);
288                 }
289
290                 public ExpressionStatement GetScopeInitializer (EmitContext ec)
291                 {
292                         Report.Debug (128, "GET SCOPE INITIALIZER",
293                                       this, GetType (), scope_initializer, ScopeBlock);
294
295                         if (scope_initializer == null) {
296                                 scope_initializer = CreateScopeInitializer ();
297                                 if (scope_initializer.Resolve (ec) == null)
298                                         throw new InternalErrorException ();
299                         }
300
301                         return scope_initializer;
302                 }
303
304                 public Type GetScopeType (EmitContext ec)
305                 {
306                         if (!IsGeneric)
307                                 return TypeBuilder;
308
309                         TypeArguments targs = new TypeArguments (Location);
310
311                         if (ec.DeclContainer.Parent.IsGeneric)
312                                 foreach (TypeParameter t in ec.DeclContainer.Parent.TypeParameters)
313                                         targs.Add (new TypeParameterExpr (t, Location));
314                         if (ec.DeclContainer.IsGeneric)
315                                 foreach (TypeParameter t in ec.DeclContainer.CurrentTypeParameters)
316                                         targs.Add (new TypeParameterExpr (t, Location));
317
318                         Report.Debug (128, "GET SCOPE TYPE", this, TypeBuilder, targs,
319                                       ec.DeclContainer, ec.DeclContainer.GetType (),
320                                       ec.DeclContainer.Parent.Name);
321
322                         TypeExpr te = new ConstructedType (TypeBuilder, targs, Location);
323                         te = te.ResolveAsTypeTerminal (ec, false);
324                         if ((te == null) || (te.Type == null))
325                                 return null;
326                         return te.Type;
327                 }
328
329                 protected override bool DoDefineMembers ()
330                 {
331                         Report.Debug (64, "SCOPE INFO DEFINE MEMBERS", this, GetType (), IsGeneric,
332                                       Parent.IsGeneric, GenericMethod);
333
334                         foreach (CapturedScope child in CapturedScopes) {
335                                 if (!child.DefineMembers ())
336                                         return false;
337                         }
338
339                         return base.DoDefineMembers ();
340                 }
341
342                 protected override bool DoResolveMembers ()
343                 {
344                         Report.Debug (64, "SCOPE INFO RESOLVE MEMBERS", this, GetType (), IsGeneric,
345                                       Parent.IsGeneric, GenericMethod);
346
347                         return base.DoResolveMembers ();
348                 }
349
350                 public Variable CaptureScope (ScopeInfo child)
351                 {
352                         CheckMembersDefined ();
353                         Report.Debug (128, "CAPTURE SCOPE", this, GetType (), child, child.GetType ());
354                         if (child == this)
355                                 throw new InternalErrorException ();
356                         CapturedScope captured = (CapturedScope) captured_scopes [child];
357                         if (captured == null) {
358                                 captured = new CapturedScope (this, child);
359                                 captured_scopes.Add (child, captured);
360                         }
361                         return captured;
362                 }
363
364                 public Variable AddLocal (LocalInfo local)
365                 {
366                         Report.Debug (128, "CAPTURE LOCAL", this, local);
367                         Variable var = (Variable) locals [local];
368                         if (var == null) {
369                                 var = new CapturedLocal (this, local);
370                                 locals.Add (local, var);
371                                 local.IsCaptured = true;
372                         }
373                         return var;
374                 }
375
376                 public Variable GetCapturedVariable (LocalInfo local)
377                 {
378                         return (Variable) locals [local];
379                 }
380
381                 public bool HostsParameters {
382                         get { return captured_params != null; }
383                 }
384
385                 public Variable GetCapturedParameter (Parameter par)
386                 {
387                         if (captured_params != null)
388                                 return (Variable) captured_params [par];
389                         else
390                                 return null;
391                 }
392
393                 public bool IsParameterCaptured (string name)
394                 {                       
395                         if (captured_params != null)
396                                 return captured_params [name] != null;
397                         return false;
398                 }
399
400                 public Variable AddParameter (Parameter par, int idx)
401                 {
402                         if (captured_params == null)
403                                 captured_params = new Hashtable ();
404
405                         Variable var = (Variable) captured_params [par];
406                         if (var == null) {
407                                 var = new CapturedParameter (this, par, idx);
408                                 captured_params.Add (par, var);
409                                 par.IsCaptured = true;
410                         }
411
412                         return var;
413                 }
414
415                 protected string MakeFieldName (string local_name)
416                 {
417                         return "<" + ID + ":" + local_name + ">";
418                 }
419
420                 protected virtual ScopeInitializer CreateScopeInitializer ()
421                 {
422                         return new ScopeInitializer (this);
423                 }
424
425                 protected abstract class CapturedVariable : Variable
426                 {
427                         public readonly ScopeInfo Scope;
428                         public readonly string Name;
429
430                         public FieldExpr FieldInstance;
431                         protected Field field;
432
433                         protected CapturedVariable (ScopeInfo scope, string name)
434                         {
435                                 this.Scope = scope;
436                                 this.Name = name;
437                         }
438
439                         protected CapturedVariable (ScopeInfo scope, string name, Type type)
440                                 : this (scope, name)
441                         {
442                                 this.field = scope.CaptureVariable (
443                                         scope.MakeFieldName (name), scope.RootScope.InflateType (type));
444                         }
445
446                         public Field Field {
447                                 get { return field; }
448                         }
449
450                         public override Type Type {
451                                 get { return Field.MemberType; }
452                         }
453
454                         public override bool HasInstance {
455                                 get { return true; }
456                         }
457
458                         public override bool NeedsTemporary {
459                                 get { return true; }
460                         }
461
462                         protected FieldInfo GetField (EmitContext ec)
463                         {
464                                 if ((ec.CurrentBlock != null) &&
465                                     (ec.CurrentBlock.Toplevel != Scope.ScopeBlock.Toplevel))
466                                         return Field.FieldBuilder;
467                                 else
468                                         return FieldInstance.FieldInfo;
469                         }
470
471                         public override void EmitInstance (EmitContext ec)
472                         {
473                                 if ((ec.CurrentAnonymousMethod != null) &&
474                                     (ec.CurrentAnonymousMethod.Scope == Scope)) {
475                                         ec.ig.Emit (OpCodes.Ldarg_0);
476                                         return;
477                                 }
478
479                                 Scope.EmitScopeInstance (ec);
480                         }
481
482                         public override void Emit (EmitContext ec)
483                         {
484                                 ec.ig.Emit (OpCodes.Ldfld, GetField (ec));
485                         }
486
487                         public override void EmitAssign (EmitContext ec)
488                         {
489                                 ec.ig.Emit (OpCodes.Stfld, GetField (ec));
490                         }
491
492                         public override void EmitAddressOf (EmitContext ec)
493                         {
494                                 ec.ig.Emit (OpCodes.Ldflda, GetField (ec));
495                         }
496                 }
497
498                 protected class CapturedParameter : CapturedVariable {
499                         public readonly Parameter Parameter;
500                         public readonly int Idx;
501
502                         public CapturedParameter (ScopeInfo scope, Parameter par, int idx)
503                                 : base (scope, par.Name, par.ParameterType)
504                         {
505                                 this.Parameter = par;
506                                 this.Idx = idx;
507                         }
508
509                         public override string ToString ()
510                         {
511                                 return String.Format ("{0} ({1}:{2}:{3})", GetType (), Field,
512                                                       Parameter.Name, Idx);
513                         }
514                 }
515
516                 protected class CapturedLocal : CapturedVariable {
517                         public readonly LocalInfo Local;
518
519                         public CapturedLocal (ScopeInfo scope, LocalInfo local)
520                                 : base (scope, local.Name, local.VariableType)
521                         {
522                                 this.Local = local;
523                         }
524
525                         public override string ToString ()
526                         {
527                                 return String.Format ("{0} ({1}:{2})", GetType (), Field,
528                                                       Local.Name);
529                         }
530                 }
531
532                 protected class CapturedThis : CapturedVariable {
533                         public CapturedThis (RootScopeInfo host)
534                                 : base (host, "<>THIS", host.ParentType)
535                         { }
536                 }
537
538                 protected class CapturedScope : CapturedVariable {
539                         public readonly ScopeInfo ChildScope;
540
541                         public CapturedScope (ScopeInfo root, ScopeInfo child)
542                                 : base (root, "scope" + child.ID)
543                         {
544                                 this.ChildScope = child;
545                         }
546
547                         public bool DefineMembers ()
548                         {
549                                 Type type = ChildScope.IsGeneric ?
550                                         ChildScope.CurrentType : ChildScope.TypeBuilder;
551                                 Report.Debug (128, "CAPTURED SCOPE DEFINE MEMBERS", this, Scope,
552                                               ChildScope, Name, type);
553                                 if (type == null)
554                                         throw new InternalErrorException ();
555                                 field = Scope.CaptureVariable (
556                                         Scope.MakeFieldName (Name), Scope.InflateType (type));
557                                 return true;
558                         }
559
560                         public override string ToString ()
561                         {
562                                 return String.Format ("CapturedScope ({1} captured in {0})",
563                                                       Scope, ChildScope);
564                         }
565                 }
566
567                 static void DoPath (StringBuilder sb, ScopeInfo start)
568                 {
569                         sb.Append ((start.ID).ToString ());
570                 }
571                 
572                 public override string ToString ()
573                 {
574                         StringBuilder sb = new StringBuilder ();
575                         
576                         sb.Append ("{");
577                         DoPath (sb, this);
578                         sb.Append ("}");
579
580                         return sb.ToString ();
581                 }
582
583                 protected class ScopeInitializer : ExpressionStatement
584                 {
585                         ScopeInfo scope;
586                         CapturedVariable captured_scope;
587                         LocalBuilder scope_instance;
588                         ConstructorInfo scope_ctor;
589
590                         bool initialized;
591
592                         public ScopeInitializer (ScopeInfo scope)
593                         {
594                                 this.scope = scope;
595                                 this.loc = scope.Location;
596                                 eclass = ExprClass.Value;
597                         }
598
599                         public ScopeInfo Scope {
600                                 get { return scope; }
601                         }
602
603                         public override Expression DoResolve (EmitContext ec)
604                         {
605                                 if (scope_ctor != null)
606                                         return this;
607
608                                 Report.Debug (64, "RESOLVE SCOPE INITIALIZER BASE", this, Scope,
609                                               ec, ec.CurrentBlock);
610
611                                 type = Scope.GetScopeType (ec);
612                                 if (type == null)
613                                         throw new InternalErrorException ();
614
615                                 if (!DoResolveInternal (ec))
616                                         throw new InternalErrorException ();
617
618                                 return this;
619                         }
620
621                         protected virtual bool DoResolveInternal (EmitContext ec)
622                         {
623                                 MethodGroupExpr mg = (MethodGroupExpr) Expression.MemberLookupFinal (
624                                         ec, ec.ContainerType, type, ".ctor", MemberTypes.Constructor,
625                                         AllBindingFlags | BindingFlags.DeclaredOnly, loc);
626                                 if (mg == null)
627                                         throw new InternalErrorException ();
628
629                                 scope_ctor = (ConstructorInfo) mg.Methods [0];
630
631                                 Report.Debug (128, "RESOLVE THE INIT", this, Scope, Scope.RootScope,
632                                               Scope.RootScope.GetType ());
633
634                                 ScopeInfo host = Scope.RootScope;
635                                 if ((Scope != host) && (Scope.RootScope is IteratorHost)) {
636                                         captured_scope = host.GetCapturedScope (Scope);
637                                         Type root = host.GetScopeType (ec);
638                                         FieldExpr fe = (FieldExpr) Expression.MemberLookup (
639                                                 type, root, captured_scope.Field.Name, loc);
640                                         if (fe == null)
641                                                 throw new InternalErrorException ();
642
643                                         fe.InstanceExpression = this;
644                                         captured_scope.FieldInstance = fe;
645
646                                         Report.Debug (128, "RESOLVE THE INIT #1", this,
647                                                       captured_scope, fe);
648                                 } else
649                                         scope_instance = ec.ig.DeclareLocal (type);
650
651                                 foreach (CapturedLocal local in Scope.locals.Values) {
652                                         FieldExpr fe = (FieldExpr) Expression.MemberLookup (
653                                                 ec.ContainerType, type, local.Field.Name, loc);
654                                         Report.Debug (64, "RESOLVE SCOPE INITIALIZER #2", this, Scope,
655                                                       Scope, ec, ec.ContainerType, type,
656                                                       local.Field, local.Field.Name, loc, fe);
657                                         if (fe == null)
658                                                 throw new InternalErrorException ();
659
660                                         fe.InstanceExpression = this;
661                                         local.FieldInstance = fe;
662                                 }
663
664                                 if (Scope.HostsParameters) {
665                                         foreach (CapturedParameter cp in Scope.captured_params.Values) {
666                                                 FieldExpr fe = (FieldExpr) Expression.MemberLookup (
667                                                         ec.ContainerType, type, cp.Field.Name, loc);
668                                                 if (fe == null)
669                                                         throw new InternalErrorException ();
670
671                                                 fe.InstanceExpression = this;
672                                                 cp.FieldInstance = fe;
673                                         }
674                                 }
675
676                                 foreach (CapturedScope scope in Scope.CapturedScopes) {
677                                         FieldExpr fe = (FieldExpr) Expression.MemberLookup (
678                                                 ec.ContainerType, type, scope.Field.Name, loc);
679                                         Report.Debug (64, "RESOLVE SCOPE INITIALIZER #3", this, Scope,
680                                                       scope, ec, ec.ContainerType, type,
681                                                       scope.Field, scope.Field.Name, loc, fe);
682                                         if (fe == null)
683                                                 throw new InternalErrorException ();
684
685                                         fe.InstanceExpression = this;
686                                         scope.FieldInstance = fe;
687                                 }
688
689                                 return true;
690                         }
691
692                         protected virtual void EmitParameterReference (EmitContext ec,
693                                                                        CapturedParameter cp)
694                         {
695                                 int extra = ec.MethodIsStatic ? 0 : 1;
696                                 ParameterReference.EmitLdArg (ec.ig, cp.Idx + extra);
697                         }
698
699                         static int next_id;
700                         int id = ++next_id;
701
702                         protected virtual void DoEmit (EmitContext ec)
703                         {
704                                 if ((ec.CurrentBlock != null) &&
705                                     (ec.CurrentBlock.Toplevel != Scope.ScopeBlock.Toplevel)) {
706                                         ec.ig.Emit (OpCodes.Ldarg_0);
707
708                                         if (ec.CurrentAnonymousMethod != null) {
709                                                 ScopeInfo host = ec.CurrentAnonymousMethod.Scope;
710                                                 Variable captured = host.GetCapturedScope (scope);
711                                                 Report.Debug (128, "EMIT SCOPE INSTANCE #2",
712                                                               ec.CurrentAnonymousMethod, host,
713                                                               scope, captured);
714                                                 if (captured != null)
715                                                         captured.Emit (ec);
716                                         }
717                                 } else if (scope_instance != null)
718                                         ec.ig.Emit (OpCodes.Ldloc, scope_instance);
719                                 else {
720                                         Report.Debug (128, "DO EMIT", this, Scope, ec,
721                                                       scope_instance, captured_scope);
722                                         captured_scope.EmitInstance (ec);
723                                         captured_scope.Emit (ec);
724                                 }
725                         }
726
727                         protected void DoEmitInstance (EmitContext ec)
728                         {
729                                 Report.Debug (128, "DO EMIT INSTANCE", this, Scope, ec,
730                                               scope_instance, captured_scope);
731
732                                 if (scope_instance != null)
733                                         ec.ig.Emit (OpCodes.Ldloc, scope_instance);
734                                 else
735                                         captured_scope.EmitInstance (ec);
736                         }
737
738                         protected virtual void EmitScopeConstructor (EmitContext ec)
739                         {
740                                 ec.ig.Emit (OpCodes.Newobj, scope_ctor);
741                         }
742
743                         public override void Emit (EmitContext ec)
744                         {
745                                 if (!initialized)
746                                         throw new InternalErrorException (
747                                                 "Scope {0} not initialized yet", scope);
748
749                                 DoEmit (ec);
750                         }
751
752                         public override void EmitStatement (EmitContext ec)
753                         {
754                                 if (initialized)
755                                         return;
756
757                                 DoEmitStatement (ec);
758                                 initialized = true;
759                         }
760
761                         protected virtual void DoEmitStatement (EmitContext ec)
762                         {
763                                 Report.Debug (128, "EMIT SCOPE INITIALIZER STATEMENT", this, id,
764                                               Scope, scope_instance, ec);
765
766                                 ec.ig.Emit (OpCodes.Nop);
767                                 ec.ig.Emit (OpCodes.Ldc_I4, id);
768                                 ec.ig.Emit (OpCodes.Pop);
769                                 ec.ig.Emit (OpCodes.Nop);
770
771                                 if (scope_instance == null)
772                                         ec.ig.Emit (OpCodes.Ldarg_0);
773                                 EmitScopeConstructor (ec);
774                                 if (scope_instance != null)
775                                         ec.ig.Emit (OpCodes.Stloc, scope_instance);
776                                 else
777                                         captured_scope.EmitAssign (ec);
778
779                                 if (Scope.HostsParameters) {
780                                         foreach (CapturedParameter cp in Scope.captured_params.Values) {
781                                                 Report.Debug (128, "EMIT SCOPE INIT #6", this,
782                                                               ec, ec.IsStatic, Scope, cp, cp.Field.Name);
783                                                 DoEmitInstance (ec);
784                                                 EmitParameterReference (ec, cp);
785                                                 ec.ig.Emit (OpCodes.Stfld, cp.FieldInstance.FieldInfo);
786                                         }
787                                 }
788
789                                 if (Scope is IteratorHost)
790                                         return;
791
792                                 foreach (CapturedScope scope in Scope.CapturedScopes) {
793                                         ScopeInfo child = scope.ChildScope;
794
795                                         Report.Debug (128, "EMIT SCOPE INIT #5", this, Scope,
796                                                       scope.Scope, scope.ChildScope);
797
798                                         ExpressionStatement init = child.GetScopeInitializer (ec);
799                                         init.EmitStatement (ec);
800
801                                         DoEmit (ec);
802                                         scope.ChildScope.EmitScopeInstance (ec);
803                                         scope.EmitAssign (ec);
804                                 }
805                         }
806                 }
807         }
808
809         public class RootScopeInfo : ScopeInfo
810         {
811                 public RootScopeInfo (ToplevelBlock toplevel, DeclSpace parent,
812                                       GenericMethod generic, Location loc)
813                         : base (toplevel, parent, generic, loc)
814                 {
815                         scopes = new ArrayList ();
816                 }
817
818                 TypeExpr parent_type;
819                 CapturedVariableField parent_link;
820                 CapturedThis this_variable;
821                 protected ArrayList scopes;
822
823                 public virtual bool IsIterator {
824                         get { return false; }
825                 }
826
827                 public RootScopeInfo ParentHost {
828                         get { return Parent.PartialContainer as RootScopeInfo; }
829                 }
830
831                 public Type ParentType {
832                         get { return parent_type.Type; }
833                 }
834
835                 public Field ParentLink {
836                         get { return parent_link; }
837                 }
838
839                 protected CapturedThis THIS {
840                         get { return this_variable; }
841                 }
842
843                 public Variable CaptureThis ()
844                 {
845                         if (ParentHost != null)
846                                 return ParentHost.CaptureThis ();
847
848                         CheckMembersDefined ();
849                         if (this_variable == null)
850                                 this_variable = new CapturedThis (this);
851                         return this_variable;
852                 }
853
854                 public void AddScope (ScopeInfo scope)
855                 {
856                         scopes.Add (scope);
857                 }
858
859                 bool linked;
860                 public void LinkScopes ()
861                 {
862                         Report.Debug (128, "LINK SCOPES", this, linked, scopes);
863
864                         if (linked)
865                                 return;
866
867                         linked = true;
868                         if (ParentHost != null)
869                                 ParentHost.LinkScopes ();
870
871                         foreach (ScopeInfo si in scopes) {
872                                 if (!si.Define ())
873                                         throw new InternalErrorException ();
874                                 if (si.DefineType () == null)
875                                         throw new InternalErrorException ();
876                                 if (!si.ResolveType ())
877                                         throw new InternalErrorException ();
878                         }
879
880                         foreach (ScopeInfo si in scopes) {
881                                 if (!si.ResolveMembers ())
882                                         throw new InternalErrorException ();
883                                 if (!si.DefineMembers ())
884                                         throw new InternalErrorException ();
885                         }
886                 }
887
888                 protected override ScopeInitializer CreateScopeInitializer ()
889                 {
890                         return new RootScopeInitializer (this);
891                 }
892
893                 protected override bool DefineNestedTypes ()
894                 {
895                         if (Parent.IsGeneric) {
896                                 parent_type = new ConstructedType (
897                                         Parent.TypeBuilder, Parent.TypeParameters, Location);
898                                 parent_type = parent_type.ResolveAsTypeTerminal (this, false);
899                                 if ((parent_type == null) || (parent_type.Type == null))
900                                         return false;
901                         } else {
902                                 parent_type = new TypeExpression (Parent.TypeBuilder, Location);
903                         }
904
905                         CompilerGeneratedClass parent = Parent.PartialContainer as CompilerGeneratedClass;
906                         if (parent != null)
907                                 parent_link = new CapturedVariableField (this, "<>parent", parent_type);
908
909                         return base.DefineNestedTypes ();
910                 }
911
912                 protected override bool DoDefineMembers ()
913                 {
914                         ArrayList args = new ArrayList ();
915                         if (this is IteratorHost)
916                                 args.Add (new Parameter (
917                                         TypeManager.int32_type, "$PC", Parameter.Modifier.NONE,
918                                         null, Location));
919
920                         Field pfield;
921                         if (Parent is CompilerGeneratedClass)
922                                 pfield = parent_link;
923                         else
924                                 pfield = this_variable !=  null ? this_variable.Field : null;
925                         if (pfield != null)
926                                 args.Add (new Parameter (
927                                         pfield.MemberType, "parent", Parameter.Modifier.NONE,
928                                         null, Location));
929
930                         Parameter[] ctor_params = new Parameter [args.Count];
931                         args.CopyTo (ctor_params, 0);
932                         Constructor ctor = new Constructor (
933                                 this, MemberName.Name, Modifiers.PUBLIC,
934                                 new Parameters (ctor_params),
935                                 new GeneratedBaseInitializer (Location),
936                                 Location);
937                         AddConstructor (ctor);
938
939                         ctor.Block = new ToplevelBlock (null, Location);
940                         ctor.Block.AddStatement (new TheCtor (this));
941
942                         return base.DoDefineMembers ();
943                 }
944
945                 protected virtual void EmitScopeConstructor (EmitContext ec)
946                 {
947                         int pos = (this is IteratorHost) ? 2 : 1;
948
949                         Field pfield;
950                         if (Parent is CompilerGeneratedClass)
951                                 pfield = parent_link;
952                         else
953                                 pfield = this_variable !=  null ? this_variable.Field : null;
954
955                         if (pfield != null) {
956                                 ec.ig.Emit (OpCodes.Ldarg_0);
957                                 ec.ig.Emit (OpCodes.Ldarg, pos);
958                                 ec.ig.Emit (OpCodes.Stfld, pfield.FieldBuilder);
959                                 pos++;
960                         }
961                 }
962
963                 protected class TheCtor : Statement
964                 {
965                         RootScopeInfo host;
966
967                         public TheCtor (RootScopeInfo host)
968                         {
969                                 this.host = host;
970                         }
971
972                         public override bool Resolve (EmitContext ec)
973                         {
974                                 return true;
975                         }
976
977                         protected override void DoEmit (EmitContext ec)
978                         {
979                                 host.EmitScopeConstructor (ec);
980                         }
981                 }
982
983                 protected class RootScopeInitializer : ScopeInitializer
984                 {
985                         RootScopeInfo host;
986
987                         public RootScopeInitializer (RootScopeInfo host)
988                                 : base (host)
989                         {
990                                 this.host = host;
991                         }
992
993                         public RootScopeInfo Host {
994                                 get { return host; }
995                         }
996
997                         protected override bool DoResolveInternal (EmitContext ec)
998                         {
999                                 Report.Debug (64, "RESOLVE ANONYMOUS METHOD HOST INITIALIZER",
1000                                               this, Host, Host.ParentType, loc);
1001
1002                                 if (Host.THIS != null) {
1003                                         FieldExpr fe = (FieldExpr) Expression.MemberLookup (
1004                                                 ec.ContainerType, type, Host.THIS.Field.Name, loc);
1005                                         if (fe == null)
1006                                                 throw new InternalErrorException ();
1007
1008                                         fe.InstanceExpression = this;
1009                                         Host.THIS.FieldInstance = fe;
1010                                 }
1011
1012                                 return base.DoResolveInternal (ec);
1013                         }
1014
1015                         protected virtual bool IsGetEnumerator {
1016                                 get { return false; }
1017                         }
1018
1019                         protected override void EmitScopeConstructor (EmitContext ec)
1020                         {
1021                                 if (host.THIS != null) {
1022                                         ec.ig.Emit (OpCodes.Ldarg_0);
1023                                         if (IsGetEnumerator)
1024                                                 ec.ig.Emit (OpCodes.Ldfld, host.THIS.Field.FieldBuilder);
1025                                         else if (host.THIS.Type.IsValueType)
1026                                                 Expression.LoadFromPtr (ec.ig, host.THIS.Type);
1027                                 } else if (host.ParentLink != null)
1028                                         ec.ig.Emit (OpCodes.Ldarg_0);
1029
1030                                 base.EmitScopeConstructor (ec);
1031                         }
1032                 }
1033
1034         }
1035
1036         public interface IAnonymousContainer
1037         {
1038                 Block Container {
1039                         get;
1040                 }
1041
1042                 GenericMethod GenericMethod {
1043                         get;
1044                 }
1045
1046                 RootScopeInfo RootScope {
1047                         get;
1048                 }
1049
1050                 bool IsIterator {
1051                         get;
1052                 }
1053         }
1054
1055         public interface IAnonymousHost
1056         {
1057                 //
1058                 // Invoked if a yield statement is found in the body
1059                 //
1060                 void SetYields ();
1061
1062                 //
1063                 // Invoked if an anonymous method is found in the body
1064                 //
1065                 void AddAnonymousMethod (AnonymousMethodExpression anonymous);
1066         }
1067
1068         public class AnonymousMethodExpression : Expression, IAnonymousContainer, IAnonymousHost
1069         {
1070                 public readonly AnonymousMethodExpression Parent;
1071                 public readonly TypeContainer Host;
1072                 public Parameters Parameters;
1073
1074                 public ToplevelBlock Block;
1075                 protected AnonymousMethod anonymous;
1076
1077                 protected Block container;
1078                 protected readonly GenericMethod generic;
1079
1080                 public Block Container {
1081                         get { return container; }
1082                 }
1083
1084                 public GenericMethod GenericMethod {
1085                         get { return generic; }
1086                 }
1087
1088                 public AnonymousMethod AnonymousMethod {
1089                         get { return anonymous; }
1090                 }
1091
1092                 public RootScopeInfo RootScope {
1093                         get { return root_scope; }
1094                 }
1095
1096                 public AnonymousMethodExpression (AnonymousMethodExpression parent,
1097                                                   GenericMethod generic, TypeContainer host,
1098                                                   Parameters parameters, Block container,
1099                                                   Location loc)
1100                 {
1101                         this.Parent = parent;
1102                         this.generic = parent != null ? null : generic;
1103                         this.Host = host;
1104                         this.Parameters = parameters;
1105                         this.container = container;
1106                         this.loc = loc;
1107
1108                         Report.Debug (64, "NEW ANONYMOUS METHOD EXPRESSION", this, parent, host,
1109                                       container, loc);
1110
1111                         if (parent != null)
1112                                 parent.AddAnonymousMethod (this);
1113                 }
1114
1115                 ArrayList children;
1116                 RootScopeInfo root_scope;
1117
1118                 static int next_index;
1119
1120                 void IAnonymousHost.SetYields ()
1121                 {
1122                         throw new InvalidOperationException ();
1123                 }
1124
1125                 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
1126                 {
1127                         if (children == null)
1128                                 children = new ArrayList ();
1129                         children.Add (anonymous);
1130                 }
1131
1132                 public bool CreateAnonymousHelpers ()
1133                 {
1134                         Report.Debug (64, "ANONYMOUS METHOD EXPRESSION CREATE ROOT SCOPE",
1135                                       this, Host, container, loc);
1136
1137                         if (container != null)
1138                                 root_scope = container.Toplevel.CreateRootScope (Host);
1139
1140                         if (children != null) {
1141                                 foreach (AnonymousMethodExpression child in children) {
1142                                         if (!child.CreateAnonymousHelpers ())
1143                                                 return false;
1144                                 }
1145                         }
1146
1147                         return true;
1148                 }
1149
1150                 public override string ExprClassName {
1151                         get {
1152                                 return "anonymous method";
1153                         }
1154                 }
1155
1156                 void Error_ParameterMismatch (Type t)
1157                 {
1158                         Report.Error (1661, loc, "Anonymous method could not be converted to delegate `" +
1159                                       "{0}' since there is a parameter mismatch",
1160                                       TypeManager.CSharpName (t));
1161                 }
1162
1163                 public virtual bool ImplicitStandardConversionExists (Type delegate_type)
1164                 {
1165                         if (Parameters == null)
1166                                 return true;
1167
1168                         MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
1169                                 Host.TypeBuilder, delegate_type, loc);
1170                         MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
1171                         ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
1172
1173                         if (Parameters.Count != invoke_pd.Count)
1174                                 return false;
1175
1176                         for (int i = 0; i < Parameters.Count; ++i) {
1177                                 if (invoke_pd.ParameterType (i) != Parameters.ParameterType (i))
1178                                         return false;
1179                         }
1180                         return true;
1181                 }
1182
1183                 protected Expression CompatibleChecks (EmitContext ec, Type delegate_type)
1184                 {
1185                         if (!ec.IsAnonymousMethodAllowed) {
1186                                 Report.Error (1706, loc,
1187                                               "Anonymous methods are not allowed in the " +
1188                                               "attribute declaration");
1189                                 return null;
1190                         }
1191                         
1192                         if (!TypeManager.IsDelegateType (delegate_type)){
1193                                 Report.Error (1660, loc,
1194                                               "Cannot convert anonymous method block to type " +
1195                                               "`{0}' because it is not a delegate type",
1196                                               TypeManager.CSharpName (delegate_type));
1197                                 return null;
1198                         }
1199                         return this;
1200                 }
1201
1202                 protected bool VerifyExplicitParameterCompatibility (Type delegate_type, ParameterData invoke_pd)
1203                 {
1204                         if (Parameters.Count != invoke_pd.Count) {
1205                                 Report.SymbolRelatedToPreviousError (delegate_type);
1206                                 Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
1207                                               TypeManager.CSharpName (delegate_type), Parameters.Count.ToString ());
1208                                 Error_ParameterMismatch (delegate_type);
1209                                 return false;
1210                         }
1211                         
1212                         for (int i = 0; i < Parameters.Count; ++i) {
1213                                 Parameter.Modifier p_mod = invoke_pd.ParameterModifier (i);
1214                                 if (Parameters.ParameterModifier (i) != p_mod && p_mod != Parameter.Modifier.PARAMS) {
1215                                         if (p_mod == Parameter.Modifier.NONE)
1216                                                 Report.Error (1677, loc, "Parameter `{0}' should not be declared with the `{1}' keyword",
1217                                                               (i + 1).ToString (), Parameter.GetModifierSignature (Parameters.ParameterModifier (i)));
1218                                         else
1219                                                 Report.Error (1676, loc, "Parameter `{0}' must be declared with the `{1}' keyword",
1220                                                               (i+1).ToString (), Parameter.GetModifierSignature (p_mod));
1221                                         Error_ParameterMismatch (delegate_type);
1222                                         return false;
1223                                 }
1224
1225                                 // We assume that generic parameters are always inflated
1226                                 if (TypeManager.IsGenericParameter (invoke_pd.Types[i]))
1227                                         continue;
1228                                 
1229                                 if (invoke_pd.ParameterType (i) != Parameters.ParameterType (i)) {
1230                                         Report.Error (1678, loc, "Parameter `{0}' is declared as type `{1}' but should be `{2}'",
1231                                                       (i+1).ToString (),
1232                                                       TypeManager.CSharpName (Parameters.ParameterType (i)),
1233                                                       TypeManager.CSharpName (invoke_pd.ParameterType (i)));
1234                                         Error_ParameterMismatch (delegate_type);
1235                                         return false;
1236                                 }
1237                         }
1238                         return true;
1239                 }
1240                 
1241                 //
1242                 // Returns true if this anonymous method can be implicitly
1243                 // converted to the delegate type `delegate_type'
1244                 //
1245                 public virtual Expression Compatible (EmitContext ec, Type delegate_type)
1246                 {
1247                         if (anonymous != null)
1248                                 return anonymous.AnonymousDelegate;
1249
1250                         if (CompatibleChecks (ec, delegate_type) == null)
1251                                 return null;
1252
1253                         
1254                         //
1255                         // At this point its the first time we know the return type that is 
1256                         // needed for the anonymous method.  We create the method here.
1257                         //
1258
1259                         MethodGroupExpr invoke_mg = Delegate.GetInvokeMethod (
1260                                 ec.ContainerType, delegate_type, loc);
1261                         MethodInfo invoke_mb = (MethodInfo) invoke_mg.Methods [0];
1262                         ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
1263
1264 #if GMCS_SOURCE
1265                         Type[] infered_arguments = null;
1266                         if (TypeManager.IsGenericType (delegate_type)) {
1267
1268                                 Type[] g_arguments = delegate_type.GetGenericArguments ();
1269                                 infered_arguments = new Type[g_arguments.Length];
1270                                 for (int i = 0; i < g_arguments.Length; ++i) {
1271                                         infered_arguments [i] = g_arguments[i];
1272                                 }
1273
1274                                 for (int i = 0; i < invoke_pd.Count; ++i) {
1275                                         if (!invoke_pd.Types[i].IsGenericParameter)
1276                                                 continue;
1277
1278                                         infered_arguments [invoke_pd.Types[i].GenericParameterPosition] = Parameters.Types[i];
1279                                 }
1280                         }
1281
1282                         int return_type_pos = -1;
1283                         if (TypeManager.IsGenericParameter (invoke_mb.ReturnType)) {
1284                                 ec.InferReturnType = true;
1285                                 return_type_pos = invoke_mb.ReturnType.GenericParameterPosition;
1286                         }
1287 #endif
1288
1289                         Parameters parameters;
1290                         if (Parameters == null) {
1291                                 //
1292                                 // We provide a set of inaccessible parameters
1293                                 //
1294                                 Parameter [] fixedpars = new Parameter [invoke_pd.Count];
1295                                                                 
1296                                 for (int i = 0; i < invoke_pd.Count; i++) {
1297                                         Parameter.Modifier i_mod = invoke_pd.ParameterModifier (i);
1298                                         if ((i_mod & Parameter.Modifier.OUTMASK) != 0) {
1299                                                 Report.Error (1688, loc, "Cannot convert anonymous " +
1300                                                               "method block without a parameter list " +
1301                                                               "to delegate type `{0}' because it has " +
1302                                                               "one or more `out' parameters.",
1303                                                               TypeManager.CSharpName (delegate_type));
1304                                                 return null;
1305                                         }
1306                                         fixedpars [i] = new Parameter (
1307                                                 invoke_pd.ParameterType (i), "+" + (++next_index),
1308                                                 invoke_pd.ParameterModifier (i), null, loc);
1309                                 }
1310                                                                 
1311                                 parameters = new Parameters (fixedpars);
1312                                 if (!parameters.Resolve (ec))
1313                                         return null;
1314                         } else {
1315                                 if (!VerifyExplicitParameterCompatibility (delegate_type, invoke_pd))
1316                                         return null;
1317
1318                                 parameters = Parameters;
1319                         }
1320
1321                         //
1322                         // Second: the return type of the delegate must be compatible with 
1323                         // the anonymous type.   Instead of doing a pass to examine the block
1324                         // we satisfy the rule by setting the return type on the EmitContext
1325                         // to be the delegate type return type.
1326                         //
1327
1328                         //MethodBuilder builder = method_data.MethodBuilder;
1329                         //ILGenerator ig = builder.GetILGenerator ();
1330
1331                         Report.Debug (64, "COMPATIBLE", this, Parent, GenericMethod, Host,
1332                                       Container, Block, invoke_mb.ReturnType, delegate_type,
1333                                       TypeManager.IsGenericType (delegate_type), loc);
1334
1335                         anonymous = new AnonymousMethod (
1336                                 Parent != null ? Parent.AnonymousMethod : null, RootScope, Host,
1337                                 GenericMethod, parameters, Container, Block, invoke_mb.ReturnType,
1338                                 delegate_type, loc);
1339
1340                         if (!anonymous.Resolve (ec))
1341                                 return null;
1342
1343 #if GMCS_SOURCE
1344                         if (return_type_pos != -1) {
1345                                 if (infered_arguments == null)
1346                                         infered_arguments = new Type [delegate_type.GetGenericArguments ().Length];
1347
1348                                 infered_arguments [return_type_pos] = anonymous.ReturnType;
1349                         }
1350
1351                         if (infered_arguments != null & TypeManager.IsGenericType (delegate_type)) {
1352                                 anonymous.AnonymousDelegate.Type = delegate_type.GetGenericTypeDefinition ().MakeGenericType (infered_arguments);
1353                                 anonymous.DelegateType = anonymous.AnonymousDelegate.Type;
1354                         }
1355 #endif
1356                         return anonymous.AnonymousDelegate;
1357                 }
1358
1359                 public override Expression DoResolve (EmitContext ec)
1360                 {
1361                         //
1362                         // Set class type, set type
1363                         //
1364
1365                         eclass = ExprClass.Value;
1366
1367                         //
1368                         // This hack means `The type is not accessible
1369                         // anywhere', we depend on special conversion
1370                         // rules.
1371                         // 
1372                         type = TypeManager.anonymous_method_type;
1373
1374                         if ((Parameters != null) && !Parameters.Resolve (ec))
1375                                 return null;
1376
1377                         return this;
1378                 }
1379
1380                 public override void Emit (EmitContext ec)
1381                 {
1382                         // nothing, as we only exist to not do anything.
1383                 }
1384
1385                 public bool IsIterator {
1386                         get { return false; }
1387                 }
1388
1389                 protected override void CloneTo (CloneContext clonectx, Expression t)
1390                 {
1391                         AnonymousMethodExpression target = (AnonymousMethodExpression) t;
1392
1393                         target.Block = (ToplevelBlock) clonectx.LookupBlock (Block);
1394                         target.container = clonectx.LookupBlock (Block);
1395                         target.Parameters = Parameters.Clone ();
1396                 }
1397         }
1398
1399         public abstract class AnonymousContainer : IAnonymousContainer
1400         {
1401                 public readonly Location Location;
1402
1403                 public Parameters Parameters;
1404
1405                 //
1406                 // The block that makes up the body for the anonymous mehtod
1407                 //
1408                 public readonly ToplevelBlock Block;
1409
1410                 public readonly int ModFlags;
1411                 public Type ReturnType;
1412                 public readonly DeclSpace Host;
1413
1414                 //
1415                 // The implicit method we create
1416                 //
1417                 protected Method method;
1418                 protected EmitContext aec;
1419
1420                 // The emit context for the anonymous method
1421                 protected bool unreachable;
1422                 protected readonly Block container;
1423                 protected readonly GenericMethod generic;
1424
1425                 //
1426                 // Points to our container anonymous method if its present
1427                 //
1428                 public readonly AnonymousContainer ContainerAnonymousMethod;
1429
1430                 protected AnonymousContainer (AnonymousContainer parent, DeclSpace host,
1431                                               GenericMethod generic, Parameters parameters,
1432                                               Block container, ToplevelBlock block,
1433                                               Type return_type, int mod, Location loc)
1434                 {
1435                         this.ContainerAnonymousMethod = parent;
1436                         this.ReturnType = return_type;
1437                         this.ModFlags = mod;
1438                         this.Host = host;
1439
1440                         this.container = container;
1441                         this.generic = parent != null ? null : generic;
1442                         this.Parameters = parameters;
1443                         this.Block = block;
1444                         this.Location = loc;
1445
1446                         block.AnonymousContainer = this;
1447                 }
1448
1449                 public Method Method {
1450                         get { return method; }
1451                 }
1452
1453                 public abstract RootScopeInfo RootScope {
1454                         get;
1455                 }
1456
1457                 public abstract ScopeInfo Scope {
1458                         get;
1459                 }
1460
1461                 public abstract string GetSignatureForError ();
1462
1463                 public virtual bool ResolveNoDefine (EmitContext ec)
1464                 {
1465                         Report.Debug (64, "RESOLVE ANONYMOUS METHOD", this, Location, ec,
1466                                       RootScope, Parameters, ec.IsStatic);
1467
1468                         if (ReturnType != null) {
1469                                 TypeExpr return_type_expr;
1470                                 if (RootScope != null)
1471                                         return_type_expr = RootScope.InflateType (ReturnType);
1472                                 else
1473                                         return_type_expr = new TypeExpression (ReturnType, Location);
1474                                 return_type_expr = return_type_expr.ResolveAsTypeTerminal (ec, false);
1475                                 if ((return_type_expr == null) || (return_type_expr.Type == null))
1476                                         return false;
1477                                 ReturnType = return_type_expr.Type;
1478                         }
1479
1480                         if (RootScope != null)
1481                                 Parameters = RootScope.InflateParameters (Parameters);
1482
1483                         aec = new EmitContext (
1484                                 ec.ResolveContext, ec.TypeContainer,
1485                                 RootScope != null ? RootScope : Host, Location, null, ReturnType,
1486                                 /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
1487                                 (ec.InUnsafe ? Modifiers.UNSAFE : 0), /* No constructor */ false);
1488
1489                         aec.CurrentAnonymousMethod = this;
1490                         aec.IsFieldInitializer = ec.IsFieldInitializer;
1491                         aec.IsStatic = ec.IsStatic;
1492                         aec.InferReturnType = ec.InferReturnType;
1493
1494                         Report.Debug (64, "RESOLVE ANONYMOUS METHOD #1", this, Location, ec, aec,
1495                                       RootScope, Parameters, Block);
1496
1497                         bool unreachable;
1498                         if (!aec.ResolveTopBlock (ec, Block, Parameters, null, out unreachable))
1499                                 return false;
1500
1501                         return true;
1502                 }
1503
1504                 public virtual bool Resolve (EmitContext ec)
1505                 {
1506                         if (!ResolveNoDefine (ec))
1507                                 return false;
1508                         
1509                         Report.Debug (64, "RESOLVE ANONYMOUS METHOD #3", this, ec, aec, Block);
1510
1511                         if (aec.InferReturnType)
1512                                 ReturnType = aec.ReturnType;
1513
1514                         method = DoCreateMethodHost (ec);
1515
1516                         if (Scope != null)
1517                                 return true;
1518
1519                         if (!method.ResolveMembers ())
1520                                 return false;
1521                         return method.Define ();
1522                 }
1523
1524                 protected abstract Method DoCreateMethodHost (EmitContext ec);
1525
1526                 public Block Container {
1527                         get { return container; }
1528                 }
1529
1530                 public GenericMethod GenericMethod {
1531                         get { return generic; }
1532                 }
1533
1534                 public abstract bool IsIterator {
1535                         get;
1536                 }
1537
1538                 protected class AnonymousMethodMethod : Method
1539                 {
1540                         public readonly AnonymousContainer AnonymousMethod;
1541                         public readonly ScopeInfo Scope;
1542
1543                         public AnonymousMethodMethod (AnonymousContainer am, ScopeInfo scope,
1544                                                       GenericMethod generic, TypeExpr return_type,
1545                                                       int mod, MemberName name, Parameters parameters)
1546                                 : base (scope != null ? scope : am.Host,
1547                                         generic, return_type, mod | Modifiers.COMPILER_GENERATED, false, name, parameters, null)
1548                         {
1549                                 this.AnonymousMethod = am;
1550                                 this.Scope = scope;
1551
1552                                 if (scope != null) {
1553                                         scope.CheckMembersDefined ();
1554                                         scope.AddMethod (this);
1555                                 } else {
1556                                         ModFlags |= Modifiers.STATIC;
1557                                         am.Host.PartialContainer.AddMethod (this);
1558                                 }
1559                                 Block = am.Block;
1560                         }
1561
1562                         public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
1563                         {
1564                                 EmitContext aec = AnonymousMethod.aec;
1565                                 aec.ig = ig;
1566                                 aec.MethodIsStatic = Scope == null;
1567                                 return aec;
1568                         }
1569                 }
1570         }
1571
1572         public class AnonymousMethod : AnonymousContainer
1573         {
1574                 public Type DelegateType;
1575
1576                 //
1577                 // The value return by the Compatible call, this ensure that
1578                 // the code works even if invoked more than once (Resolve called
1579                 // more than once, due to the way Convert.ImplicitConversion works
1580                 //
1581                 Expression anonymous_delegate;
1582                 RootScopeInfo root_scope;
1583                 ScopeInfo scope;
1584
1585                 public AnonymousMethod (AnonymousMethod parent, RootScopeInfo root_scope,
1586                                         DeclSpace host, GenericMethod generic,
1587                                         Parameters parameters, Block container,
1588                                         ToplevelBlock block, Type return_type, Type delegate_type,
1589                                         Location loc)
1590                         : base (parent, host, generic, parameters, container, block,
1591                                 return_type, 0, loc)
1592                 {
1593                         this.DelegateType = delegate_type;
1594                         this.root_scope = root_scope;
1595                 }
1596
1597                 public override RootScopeInfo RootScope {
1598                         get { return root_scope; }
1599                 }
1600
1601                 public override ScopeInfo Scope {
1602                         get { return scope; }
1603                 }
1604
1605                 public override bool IsIterator {
1606                         get { return false; }
1607                 }
1608
1609                 public Expression AnonymousDelegate {
1610                         get { return anonymous_delegate; }
1611                 }
1612
1613                 public override string GetSignatureForError ()
1614                 {
1615                         return TypeManager.CSharpName (DelegateType);
1616                 }
1617
1618                 //
1619                 // Creates the host for the anonymous method
1620                 //
1621                 protected override Method DoCreateMethodHost (EmitContext ec)
1622                 {
1623                         string name = CompilerGeneratedClass.MakeName ("AnonymousMethod");
1624                         MemberName member_name;
1625
1626                         Report.Debug (128, "CREATE METHOD HOST #0", RootScope);
1627
1628                         Block b;
1629                         scope = RootScope;
1630
1631                         Report.Debug (128, "CREATE METHOD HOST #1", this, Block, Block.ScopeInfo,
1632                                       RootScope, Location);
1633
1634                         for (b = Block.Parent; b != null; b = b.Parent) {
1635                                 Report.Debug (128, "CREATE METHOD HOST #2", this, Block,
1636                                               b, b.ScopeInfo);
1637                                 if (b.ScopeInfo != null) {
1638                                         scope = b.ScopeInfo;
1639                                         break;
1640                                 }
1641                         }
1642
1643                         if (scope != null)
1644                                 scope.CheckMembersDefined ();
1645
1646                         ArrayList scopes = new ArrayList ();
1647                         if (b != null) {
1648                                 for (b = b.Parent; b != null; b = b.Parent) {
1649                                         if (b.ScopeInfo != null)
1650                                                 scopes.Add (b.ScopeInfo);
1651                                 }
1652                         }
1653
1654                         Report.Debug (128, "CREATE METHOD HOST #1", this, scope, scopes);
1655
1656                         foreach (ScopeInfo si in scopes)
1657                                 scope.CaptureScope (si);
1658
1659                         Report.Debug (128, "CREATE METHOD HOST", this, Block, container,
1660                                       RootScope, scope, scopes, Location,
1661                                       ContainerAnonymousMethod);
1662
1663                         GenericMethod generic_method = null;
1664 #if GMCS_SOURCE
1665                         if (TypeManager.IsGenericType (DelegateType)) {
1666                                 TypeArguments args = new TypeArguments (Location);
1667
1668                                 Type dt = DelegateType.GetGenericTypeDefinition ();
1669
1670                                 Type[] tparam = TypeManager.GetTypeArguments (dt);
1671                                 for (int i = 0; i < tparam.Length; i++)
1672                                         args.Add (new SimpleName (tparam [i].Name, Location));
1673
1674                                 member_name = new MemberName (name, args, Location);
1675
1676                                 Report.Debug (128, "CREATE METHOD HOST #5", this, DelegateType,
1677                                               TypeManager.GetTypeArguments (DelegateType),
1678                                               dt, tparam, args);
1679
1680                                 generic_method = new GenericMethod (
1681                                         Host.NamespaceEntry, scope, member_name,
1682                                         new TypeExpression (ReturnType, Location), Parameters);
1683
1684                                 generic_method.SetParameterInfo (null);
1685                         } else
1686 #endif
1687                                 member_name = new MemberName (name, Location);
1688
1689                         return new AnonymousMethodMethod (
1690                                 this, scope, generic_method, new TypeExpression (ReturnType, Location),
1691                                 Modifiers.INTERNAL, member_name, Parameters);
1692                 }
1693
1694                 bool ResolveAnonymousDelegate (EmitContext ec)
1695                 {
1696                         // If we are inferring the return type, set it to the discovered value.
1697                         if (DelegateType == null){
1698                                 DelegateType = aec.ReturnType;
1699                                 
1700                                 // The special value pointing to our internal type means it failed.
1701                                 if (DelegateType == typeof (AnonymousDelegate))
1702                                         return false;
1703                         }
1704
1705                         anonymous_delegate = new AnonymousDelegate (
1706                                 this, DelegateType, Location).Resolve (ec);
1707                         if (anonymous_delegate == null)
1708                                 return false;
1709                         return true;
1710                 }
1711
1712                 public override bool Resolve (EmitContext ec)
1713                 {
1714                         if (!base.Resolve (ec))
1715                                 return false;
1716
1717                         return ResolveAnonymousDelegate (ec);
1718                 }
1719
1720                 public override bool ResolveNoDefine (EmitContext ec)
1721                 {
1722                         if (!base.ResolveNoDefine (ec))
1723                                 return false;
1724
1725                         return ResolveAnonymousDelegate (ec);
1726                         return true;
1727                 }
1728
1729                 public MethodInfo GetMethodBuilder (EmitContext ec)
1730                 {
1731                         MethodInfo builder = method.MethodBuilder;
1732                         if ((Scope != null) && Scope.IsGeneric) {
1733                                 Type scope_type = Scope.GetScopeType (ec);
1734                                 if (scope_type == null)
1735                                         throw new InternalErrorException ();
1736
1737                                 MethodGroupExpr mg = (MethodGroupExpr) Expression.MemberLookup (
1738                                         ec.ContainerType, scope_type, builder.Name, Location);
1739
1740                                 if (mg == null)
1741                                         throw new InternalErrorException ();
1742                                 builder = (MethodInfo) mg.Methods [0];
1743                         }
1744
1745 #if GMCS_SOURCE
1746                         if (!DelegateType.IsGenericType)
1747                                 return builder;
1748
1749                         Type[] targs = TypeManager.GetTypeArguments (DelegateType);
1750                         return builder.MakeGenericMethod (targs);
1751 #else
1752                         return builder;
1753 #endif
1754                 }
1755
1756                 public static void Error_AddressOfCapturedVar (string name, Location loc)
1757                 {
1758                         Report.Error (1686, loc,
1759                                       "Local variable `{0}' or its members cannot have their " +
1760                                       "address taken and be used inside an anonymous method block",
1761                                       name);
1762                 }
1763         }
1764
1765         //
1766         // This will emit the code for the delegate, as well delegate creation on the host
1767         //
1768         public class AnonymousDelegate : DelegateCreation {
1769                 AnonymousMethod am;
1770
1771                 //
1772                 // if target_type is null, this means that we do not know the type
1773                 // for this delegate, and we want to infer it from the various 
1774                 // returns (implicit and explicit) from the body of this anonymous
1775                 // method.
1776                 //
1777                 // for example, the lambda: x => 1
1778                 //
1779                 public AnonymousDelegate (AnonymousMethod am, Type target_type, Location l)
1780                 {
1781                         type = target_type;
1782                         loc = l;
1783                         this.am = am;
1784                 }
1785
1786                 public override Expression DoResolve (EmitContext ec)
1787                 {
1788                         eclass = ExprClass.Value;
1789
1790                         //
1791                         // If we are inferencing
1792                         //
1793                         if (type == null){
1794                                 type = ec.ReturnType;
1795
1796                                 // No type was infered
1797                                 if (type == null)
1798                                         return null;
1799                         }
1800
1801                         return this;
1802                 }
1803                 
1804                 public override void Emit (EmitContext ec)
1805                 {
1806                         //ec.ig.Emit (OpCodes.Ldstr, "EMIT ANONYMOUS DELEGATE");
1807                         //ec.ig.Emit (OpCodes.Pop);
1808
1809                         //
1810                         // Now emit the delegate creation.
1811                         //
1812                         if ((am.Method.ModFlags & Modifiers.STATIC) == 0) {
1813                                 Report.Debug (128, "EMIT ANONYMOUS DELEGATE", this, am, am.Scope, loc);
1814                                 delegate_instance_expression = am.Scope.GetScopeInitializer (ec);
1815
1816                                 if (delegate_instance_expression == null)
1817                                         throw new InternalErrorException ();
1818                         }
1819
1820                         Expression ml = Expression.MemberLookup (
1821                                 ec.ContainerType, type, ".ctor", MemberTypes.Constructor,
1822                                 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
1823                                 loc);
1824
1825                         constructor_method = ((MethodGroupExpr) ml).Methods [0];
1826 #if MS_COMPATIBLE
1827                         if (type.IsGenericType)
1828                                 constructor_method = TypeBuilder.GetConstructor (type, (ConstructorInfo)constructor_method);
1829 #endif
1830                         
1831                         delegate_method = am.GetMethodBuilder (ec);
1832                         base.Emit (ec);
1833
1834                         //ec.ig.Emit (OpCodes.Ldstr, "EMIT ANONYMOUS DELEGATE DONE");
1835                         //ec.ig.Emit (OpCodes.Pop);
1836
1837                         Report.Debug (128, "EMIT ANONYMOUS DELEGATE DONE", this, am, am.Scope, loc);
1838                 }
1839         }
1840         
1841         public class AnonymousClass : CompilerGeneratedClass
1842         {
1843                 public AnonymousClass (TypeContainer parent, Location loc)
1844                         : base (parent, null, 0, loc)
1845                 {
1846                 }
1847         }
1848 }