Cleanup repl from most of static stuff and fix many hidden issues
[mono.git] / mcs / mcs / anonymous.cs
1 //
2 // anonymous.cs: Support for anonymous methods and types
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximain.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2003-2008 Novell, Inc.
10 //
11
12 using System;
13 using System.Collections.Generic;
14
15 #if STATIC
16 using IKVM.Reflection;
17 using IKVM.Reflection.Emit;
18 #else
19 using System.Reflection;
20 using System.Reflection.Emit;
21 #endif
22
23 namespace Mono.CSharp {
24
25         public abstract class CompilerGeneratedClass : Class
26         {
27                 protected CompilerGeneratedClass (DeclSpace parent, MemberName name, Modifiers mod)
28                         : base (parent.NamespaceEntry, parent, name, mod | Modifiers.COMPILER_GENERATED, null)
29                 {
30                 }
31
32                 protected void CheckMembersDefined ()
33                 {
34                         if (HasMembersDefined)
35                                 throw new InternalErrorException ("Helper class already defined!");
36                 }
37
38                 protected static MemberName MakeMemberName (MemberBase host, string name, int unique_id, TypeParameter[] tparams, Location loc)
39                 {
40                         string host_name = host == null ? null : host.Name;
41                         string tname = MakeName (host_name, "c", name, unique_id);
42                         TypeArguments args = null;
43                         if (tparams != null) {
44                                 args = new TypeArguments ();
45                                 foreach (TypeParameter tparam in tparams)
46                                         args.Add (new TypeParameterName (tparam.Name, null, loc));
47                         }
48
49                         return new MemberName (tname, args, loc);
50                 }
51
52                 public static string MakeName (string host, string typePrefix, string name, int id)
53                 {
54                         return "<" + host + ">" + typePrefix + "__" + name + id.ToString ("X");
55                 }
56         }
57
58         public class HoistedStoreyClass : CompilerGeneratedClass
59         {
60                 public sealed class HoistedField : Field
61                 {
62                         public HoistedField (HoistedStoreyClass parent, FullNamedExpression type, Modifiers mod, string name,
63                                   Attributes attrs, Location loc)
64                                 : base (parent, type, mod, new MemberName (name, loc), attrs)
65                         {
66                         }
67
68                         protected override bool ResolveMemberType ()
69                         {
70                                 if (!base.ResolveMemberType ())
71                                         return false;
72
73                                 HoistedStoreyClass parent = ((HoistedStoreyClass) Parent).GetGenericStorey ();
74                                 if (parent != null && parent.Mutator != null)
75                                         member_type = parent.Mutator.Mutate (MemberType);
76
77                                 return true;
78                         }
79                 }
80
81                 protected TypeParameterMutator mutator;
82
83                 public HoistedStoreyClass (DeclSpace parent, MemberName name, TypeParameter[] tparams, Modifiers mod)
84                         : base (parent, name, mod | Modifiers.PRIVATE)
85                 {
86                         if (tparams != null) {
87                                 type_params = new TypeParameter[tparams.Length];
88                                 var src = new TypeParameterSpec[tparams.Length];
89                                 var dst = new TypeParameterSpec[tparams.Length];
90
91                                 for (int i = 0; i < type_params.Length; ++i) {
92                                         type_params[i] = tparams[i].CreateHoistedCopy (this, spec);
93
94                                         src[i] = tparams[i].Type;
95                                         dst[i] = type_params[i].Type;
96                                 }
97
98                                 // A copy is not enough, inflate any type parameter constraints
99                                 // using a new type parameters
100                                 var inflator = new TypeParameterInflator (this, null, src, dst);
101                                 for (int i = 0; i < type_params.Length; ++i) {
102                                         src[i].InflateConstraints (inflator, dst[i]);
103                                 }
104                         }
105                 }
106
107                 #region Properties
108
109                 public TypeParameterMutator Mutator {
110                         get {
111                                 return mutator;
112                         }
113                         set {
114                                 mutator = value;
115                         }
116                 }
117
118                 #endregion
119
120                 public HoistedStoreyClass GetGenericStorey ()
121                 {
122                         DeclSpace storey = this;
123                         while (storey != null && storey.CurrentTypeParameters == null)
124                                 storey = storey.Parent;
125
126                         return storey as HoistedStoreyClass;
127                 }
128         }
129
130
131         //
132         // Anonymous method storey is created when an anonymous method uses
133         // variable or parameter from outer scope. They are then hoisted to
134         // anonymous method storey (captured)
135         //
136         public class AnonymousMethodStorey : HoistedStoreyClass
137         {
138                 struct StoreyFieldPair
139                 {
140                         public readonly AnonymousMethodStorey Storey;
141                         public readonly Field Field;
142
143                         public StoreyFieldPair (AnonymousMethodStorey storey, Field field)
144                         {
145                                 this.Storey = storey;
146                                 this.Field = field;
147                         }
148                 }
149
150                 //
151                 // Needed to delay hoisted _this_ initialization. When an anonymous
152                 // method is used inside ctor and _this_ is hoisted, base ctor has to
153                 // be called first, otherwise _this_ will be initialized with 
154                 // uninitialized value.
155                 //
156                 sealed class ThisInitializer : Statement
157                 {
158                         readonly HoistedThis hoisted_this;
159
160                         public ThisInitializer (HoistedThis hoisted_this)
161                         {
162                                 this.hoisted_this = hoisted_this;
163                         }
164
165                         protected override void DoEmit (EmitContext ec)
166                         {
167                                 hoisted_this.EmitHoistingAssignment (ec);
168                         }
169
170                         protected override void CloneTo (CloneContext clonectx, Statement target)
171                         {
172                                 // Nothing to clone
173                         }
174                 }
175
176                 // Unique storey ID
177                 public readonly int ID;
178                 static int unique_id;
179
180                 public readonly Block OriginalSourceBlock;
181
182                 // A list of StoreyFieldPair with local field keeping parent storey instance
183                 List<StoreyFieldPair> used_parent_storeys;
184                 List<ExplicitBlock> children_references;
185
186                 // A list of hoisted parameters
187                 protected List<HoistedParameter> hoisted_params;
188                 protected List<HoistedVariable> hoisted_locals;
189
190                 // Hoisted this
191                 protected HoistedThis hoisted_this;
192
193                 // Local variable which holds this storey instance
194                 public LocalTemporary Instance;
195
196                 public AnonymousMethodStorey (Block block, TypeContainer parent, MemberBase host, TypeParameter[] tparams, string name)
197                         : base (parent, MakeMemberName (host, name, unique_id, tparams, block.StartLocation),
198                                 tparams, Modifiers.SEALED)
199                 {
200                         Parent = parent;
201                         OriginalSourceBlock = block;
202                         ID = unique_id++;
203                 }
204
205                 public void AddCapturedThisField (EmitContext ec)
206                 {
207                         TypeExpr type_expr = new TypeExpression (ec.CurrentType, Location);
208                         Field f = AddCompilerGeneratedField ("<>f__this", type_expr);
209                         f.Define ();
210                         hoisted_this = new HoistedThis (this, f);
211
212                         // Inflated type instance has to be updated manually
213                         if (Instance.Type is InflatedTypeSpec) {
214                                 var inflator = new TypeParameterInflator (this, Instance.Type, TypeParameterSpec.EmptyTypes, TypeSpec.EmptyTypes);
215                                 Instance.Type.MemberCache.AddMember (f.Spec.InflateMember (inflator));
216
217                                 inflator = new TypeParameterInflator (this, f.Parent.CurrentType, TypeParameterSpec.EmptyTypes, TypeSpec.EmptyTypes);
218                                 f.Parent.CurrentType.MemberCache.AddMember (f.Spec.InflateMember (inflator));
219                         }
220                 }
221
222                 public Field AddCapturedVariable (string name, TypeSpec type)
223                 {
224                         CheckMembersDefined ();
225
226                         FullNamedExpression field_type = new TypeExpression (type, Location);
227                         if (!IsGeneric)
228                                 return AddCompilerGeneratedField (name, field_type);
229
230                         const Modifiers mod = Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED;
231                         Field f = new HoistedField (this, field_type, mod, name, null, Location);
232                         AddField (f);
233                         return f;
234                 }
235
236                 protected Field AddCompilerGeneratedField (string name, FullNamedExpression type)
237                 {
238                         const Modifiers mod = Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED;
239                         Field f = new Field (this, type, mod, new MemberName (name, Location), null);
240                         AddField (f);
241                         return f;
242                 }
243
244                 //
245                 // Creates a link between hoisted variable block and the anonymous method storey
246                 //
247                 // An anonymous method can reference variables from any outer block, but they are
248                 // hoisted in their own ExplicitBlock. When more than one block is referenced we
249                 // need to create another link between those variable storeys
250                 //
251                 public void AddReferenceFromChildrenBlock (ExplicitBlock block)
252                 {
253                         if (children_references == null)
254                                 children_references = new List<ExplicitBlock> ();
255
256                         if (!children_references.Contains (block))
257                                 children_references.Add (block);
258                 }
259
260                 public void AddParentStoreyReference (EmitContext ec, AnonymousMethodStorey storey)
261                 {
262                         CheckMembersDefined ();
263
264                         if (used_parent_storeys == null)
265                                 used_parent_storeys = new List<StoreyFieldPair> ();
266                         else if (used_parent_storeys.Exists (i => i.Storey == storey))
267                                 return;
268
269                         TypeExpr type_expr = storey.CreateStoreyTypeExpression (ec);
270                         Field f = AddCompilerGeneratedField ("<>f__ref$" + storey.ID, type_expr);
271                         used_parent_storeys.Add (new StoreyFieldPair (storey, f));
272                 }
273
274                 public void CaptureLocalVariable (ResolveContext ec, LocalVariable local_info)
275                 {
276                         ec.CurrentBlock.Explicit.HasCapturedVariable = true;
277                         if (ec.CurrentBlock.Explicit != local_info.Block.Explicit)
278                                 AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
279
280                         if (local_info.HoistedVariant != null)
281                                 return;
282
283                         HoistedVariable var = new HoistedLocalVariable (this, local_info, GetVariableMangledName (local_info));
284                         local_info.HoistedVariant = var;
285
286                         if (hoisted_locals == null)
287                                 hoisted_locals = new List<HoistedVariable> ();
288
289                         hoisted_locals.Add (var);
290                 }
291
292                 public void CaptureParameter (ResolveContext ec, ParameterReference param_ref)
293                 {
294                         ec.CurrentBlock.Explicit.HasCapturedVariable = true;
295                         AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
296
297                         if (param_ref.GetHoistedVariable (ec) != null)
298                                 return;
299
300                         if (hoisted_params == null)
301                                 hoisted_params = new List<HoistedParameter> (2);
302
303                         var expr = new HoistedParameter (this, param_ref);
304                         param_ref.Parameter.HoistedVariant = expr;
305                         hoisted_params.Add (expr);
306                 }
307
308                 TypeExpr CreateStoreyTypeExpression (EmitContext ec)
309                 {
310                         //
311                         // Create an instance of storey type
312                         //
313                         TypeExpr storey_type_expr;
314                         if (CurrentTypeParameters != null) {
315                                 //
316                                 // Use current method type parameter (MVAR) for top level storey only. All
317                                 // nested storeys use class type parameter (VAR)
318                                 //
319                                 TypeParameter[] tparams = ec.CurrentAnonymousMethod != null && ec.CurrentAnonymousMethod.Storey != null ?
320                                         ec.CurrentAnonymousMethod.Storey.TypeParameters :
321                                         ec.CurrentTypeParameters;
322
323                                 TypeArguments targs = new TypeArguments ();
324
325                                 //
326                                 // Use type parameter name instead of resolved type parameter
327                                 // specification to resolve to correctly nested type parameters
328                                 //
329                                 for (int i = 0; i < tparams.Length; ++i)
330                                         targs.Add (new SimpleName (tparams [i].Name, Location)); //  new TypeParameterExpr (tparams[i], Location));
331
332                                 storey_type_expr = new GenericTypeExpr (Definition, targs, Location);
333                         } else {
334                                 storey_type_expr = new TypeExpression (CurrentType, Location);
335                         }
336
337                         return storey_type_expr;
338                 }
339
340                 public void SetNestedStoryParent (AnonymousMethodStorey parentStorey)
341                 {
342                         Parent = parentStorey;
343                         type_params = null;
344                         spec.IsGeneric = false;
345                         spec.DeclaringType = parentStorey.CurrentType;
346                         MemberName.TypeArguments = null;
347                 }
348
349                 protected override bool DoResolveTypeParameters ()
350                 {
351                         // Although any storey can have type parameters they are all clones of method type
352                         // parameters therefore have to mutate MVAR references in any of cloned constraints
353                         if (type_params != null) {
354                                 for (int i = 0; i < type_params.Length; ++i) {
355                                         var spec = type_params[i].Type;
356                                         spec.BaseType = mutator.Mutate (spec.BaseType);
357                                         if (spec.InterfacesDefined != null) {
358                                                 var mutated = new TypeSpec[spec.InterfacesDefined.Length];
359                                                 for (int ii = 0; ii < mutated.Length; ++ii) {
360                                                         mutated[ii] = mutator.Mutate (spec.InterfacesDefined[ii]);
361                                                 }
362
363                                                 spec.InterfacesDefined = mutated;
364                                         }
365
366                                         if (spec.TypeArguments != null) {
367                                                 spec.TypeArguments = mutator.Mutate (spec.TypeArguments);
368                                         }
369                                 }
370                         }
371
372                         //
373                         // Update parent cache as we most likely passed the point
374                         // where the cache was constructed
375                         //
376                         Parent.CurrentType.MemberCache.AddMember (this.spec);
377
378                         return true;
379                 }
380
381                 //
382                 // Initializes all hoisted variables
383                 //
384                 public void EmitStoreyInstantiation (EmitContext ec, ExplicitBlock block)
385                 {
386                         // There can be only one instance variable for each storey type
387                         if (Instance != null)
388                                 throw new InternalErrorException ();
389
390                         SymbolWriter.OpenCompilerGeneratedBlock (ec);
391
392                         //
393                         // Create an instance of a storey
394                         //
395                         var storey_type_expr = CreateStoreyTypeExpression (ec);
396
397                         ResolveContext rc = new ResolveContext (ec.MemberContext);
398                         rc.CurrentBlock = block;
399                         Expression e = new New (storey_type_expr, null, Location).Resolve (rc);
400                         e.Emit (ec);
401
402                         Instance = new LocalTemporary (storey_type_expr.Type);
403                         Instance.Store (ec);
404
405                         EmitHoistedFieldsInitialization (rc, ec);
406
407                         SymbolWriter.DefineScopeVariable (ID, Instance.Builder);
408                         SymbolWriter.CloseCompilerGeneratedBlock (ec);
409                 }
410
411                 void EmitHoistedFieldsInitialization (ResolveContext rc, EmitContext ec)
412                 {
413                         //
414                         // Initialize all storey reference fields by using local or hoisted variables
415                         //
416                         if (used_parent_storeys != null) {
417                                 foreach (StoreyFieldPair sf in used_parent_storeys) {
418                                         //
419                                         // Get instance expression of storey field
420                                         //
421                                         Expression instace_expr = GetStoreyInstanceExpression (ec);
422                                         var fs = sf.Field.Spec;
423                                         if (TypeManager.IsGenericType (instace_expr.Type))
424                                                 fs = MemberCache.GetMember (instace_expr.Type, fs);
425
426                                         FieldExpr f_set_expr = new FieldExpr (fs, Location);
427                                         f_set_expr.InstanceExpression = instace_expr;
428
429                                         SimpleAssign a = new SimpleAssign (f_set_expr, sf.Storey.GetStoreyInstanceExpression (ec));
430                                         if (a.Resolve (rc) != null)
431                                                 a.EmitStatement (ec);
432                                 }
433                         }
434
435                         //
436                         // Define hoisted `this' in top-level storey only 
437                         //
438                         if (OriginalSourceBlock.Explicit.HasCapturedThis && !(Parent is AnonymousMethodStorey)) {
439                                 AddCapturedThisField (ec);
440                                 rc.CurrentBlock.AddScopeStatement (new ThisInitializer (hoisted_this));
441                         }
442
443                         //
444                         // Setting currect anonymous method to null blocks any further variable hoisting
445                         //
446                         AnonymousExpression ae = ec.CurrentAnonymousMethod;
447                         ec.CurrentAnonymousMethod = null;
448
449                         if (hoisted_params != null) {
450                                 EmitHoistedParameters (ec, hoisted_params);
451                         }
452
453                         ec.CurrentAnonymousMethod = ae;
454                 }
455
456                 protected virtual void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
457                 {
458                         foreach (HoistedParameter hp in hoisted) {
459                                 hp.EmitHoistingAssignment (ec);
460                         }
461                 }
462
463                 public override void EmitType ()
464                 {
465                         SymbolWriter.DefineAnonymousScope (ID);
466
467                         if (hoisted_this != null)
468                                 hoisted_this.EmitSymbolInfo ();
469
470                         if (hoisted_locals != null) {
471                                 foreach (HoistedVariable local in hoisted_locals)
472                                         local.EmitSymbolInfo ();
473                         }
474
475                         if (hoisted_params != null) {
476                                 foreach (HoistedParameter param in hoisted_params)
477                                         param.EmitSymbolInfo ();
478                         }
479
480                         if (used_parent_storeys != null) {
481                                 foreach (StoreyFieldPair sf in used_parent_storeys) {
482                                         SymbolWriter.DefineCapturedScope (ID, sf.Storey.ID, sf.Field.Name);
483                                 }
484                         }
485
486                         base.EmitType ();
487                 }
488
489                 //
490                 // Returns a field which holds referenced storey instance
491                 //
492                 Field GetReferencedStoreyField (AnonymousMethodStorey storey)
493                 {
494                         if (used_parent_storeys == null)
495                                 return null;
496
497                         foreach (StoreyFieldPair sf in used_parent_storeys) {
498                                 if (sf.Storey == storey)
499                                         return sf.Field;
500                         }
501
502                         return null;
503                 }
504
505                 //
506                 // Creates storey instance expression regardless of currect IP
507                 //
508                 public Expression GetStoreyInstanceExpression (EmitContext ec)
509                 {
510                         AnonymousExpression am = ec.CurrentAnonymousMethod;
511
512                         //
513                         // Access from original block -> storey
514                         //
515                         if (am == null)
516                                 return Instance;
517
518                         //
519                         // Access from anonymous method implemented as a static -> storey
520                         //
521                         if (am.Storey == null)
522                                 return Instance;
523
524                         Field f = am.Storey.GetReferencedStoreyField (this);
525                         if (f == null) {
526                                 if (am.Storey == this) {
527                                         //
528                                         // Access inside of same storey (S -> S)
529                                         //
530                                         return new CompilerGeneratedThis (CurrentType, Location);
531                                 }
532                                 //
533                                 // External field access
534                                 //
535                                 return Instance;
536                         }
537
538                         //
539                         // Storey was cached to local field
540                         //
541                         FieldExpr f_ind = new FieldExpr (f, Location);
542                         f_ind.InstanceExpression = new CompilerGeneratedThis (CurrentType, Location);
543                         return f_ind;
544                 }
545
546                 protected virtual string GetVariableMangledName (LocalVariable local_info)
547                 {
548                         //
549                         // No need to mangle anonymous method hoisted variables cause they
550                         // are hoisted in their own scopes
551                         //
552                         return local_info.Name;
553                 }
554
555                 public HoistedThis HoistedThis {
556                         get { return hoisted_this; }
557                 }
558
559                 public IList<ExplicitBlock> ReferencesFromChildrenBlock {
560                         get { return children_references; }
561                 }
562
563                 public static void Reset ()
564                 {
565                         unique_id = 0;
566                 }               
567         }
568
569         public abstract class HoistedVariable
570         {
571                 //
572                 // Hoisted version of variable references used in expression
573                 // tree has to be delayed until we know its location. The variable
574                 // doesn't know its location until all stories are calculated
575                 //
576                 class ExpressionTreeVariableReference : Expression
577                 {
578                         readonly HoistedVariable hv;
579
580                         public ExpressionTreeVariableReference (HoistedVariable hv)
581                         {
582                                 this.hv = hv;
583                         }
584
585                         public override Expression CreateExpressionTree (ResolveContext ec)
586                         {
587                                 return hv.CreateExpressionTree ();
588                         }
589
590                         protected override Expression DoResolve (ResolveContext ec)
591                         {
592                                 eclass = ExprClass.Value;
593                                 type = ec.Module.PredefinedTypes.Expression.Resolve (Location);
594                                 return this;
595                         }
596
597                         public override void Emit (EmitContext ec)
598                         {
599                                 ResolveContext rc = new ResolveContext (ec.MemberContext);
600                                 Expression e = hv.GetFieldExpression (ec).CreateExpressionTree (rc);
601                                 // This should never fail
602                                 e = e.Resolve (rc);
603                                 if (e != null)
604                                         e.Emit (ec);
605                         }
606                 }
607         
608                 protected readonly AnonymousMethodStorey storey;
609                 protected Field field;
610                 Dictionary<AnonymousExpression, FieldExpr> cached_inner_access; // TODO: Hashtable is too heavyweight
611                 FieldExpr cached_outer_access;
612
613                 protected HoistedVariable (AnonymousMethodStorey storey, string name, TypeSpec type)
614                         : this (storey, storey.AddCapturedVariable (name, type))
615                 {
616                 }
617
618                 protected HoistedVariable (AnonymousMethodStorey storey, Field field)
619                 {
620                         this.storey = storey;
621                         this.field = field;
622                 }
623
624                 public void AddressOf (EmitContext ec, AddressOp mode)
625                 {
626                         GetFieldExpression (ec).AddressOf (ec, mode);
627                 }
628
629                 public Expression CreateExpressionTree ()
630                 {
631                         return new ExpressionTreeVariableReference (this);
632                 }
633
634                 public void Emit (EmitContext ec)
635                 {
636                         GetFieldExpression (ec).Emit (ec);
637                 }
638
639                 //
640                 // Creates field access expression for hoisted variable
641                 //
642                 protected virtual FieldExpr GetFieldExpression (EmitContext ec)
643                 {
644                         if (ec.CurrentAnonymousMethod == null || ec.CurrentAnonymousMethod.Storey == null) {
645                                 if (cached_outer_access != null)
646                                         return cached_outer_access;
647
648                                 //
649                                 // When setting top-level hoisted variable in generic storey
650                                 // change storey generic types to method generic types (VAR -> MVAR)
651                                 //
652                                 if (storey.Instance.Type.IsGenericOrParentIsGeneric) {
653                                         var fs = MemberCache.GetMember (storey.Instance.Type, field.Spec);
654                                         cached_outer_access = new FieldExpr (fs, field.Location);
655                                 } else {
656                                         cached_outer_access = new FieldExpr (field, field.Location);
657                                 }
658
659                                 cached_outer_access.InstanceExpression = storey.GetStoreyInstanceExpression (ec);
660                                 return cached_outer_access;
661                         }
662
663                         FieldExpr inner_access;
664                         if (cached_inner_access != null) {
665                                 if (!cached_inner_access.TryGetValue (ec.CurrentAnonymousMethod, out inner_access))
666                                         inner_access = null;
667                         } else {
668                                 inner_access = null;
669                                 cached_inner_access = new Dictionary<AnonymousExpression, FieldExpr> (4);
670                         }
671
672                         if (inner_access == null) {
673                                 if (field.Parent.IsGeneric) {
674                                         var fs = MemberCache.GetMember (field.Parent.CurrentType, field.Spec);
675                                         inner_access = new FieldExpr (fs, field.Location);
676                                 } else {
677                                         inner_access = new FieldExpr (field, field.Location);
678                                 }
679
680                                 inner_access.InstanceExpression = storey.GetStoreyInstanceExpression (ec);
681                                 cached_inner_access.Add (ec.CurrentAnonymousMethod, inner_access);
682                         }
683
684                         return inner_access;
685                 }
686
687                 public abstract void EmitSymbolInfo ();
688
689                 public void Emit (EmitContext ec, bool leave_copy)
690                 {
691                         GetFieldExpression (ec).Emit (ec, leave_copy);
692                 }
693
694                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
695                 {
696                         GetFieldExpression (ec).EmitAssign (ec, source, leave_copy, false);
697                 }
698         }
699
700         public class HoistedParameter : HoistedVariable
701         {
702                 sealed class HoistedFieldAssign : Assign
703                 {
704                         public HoistedFieldAssign (Expression target, Expression source)
705                                 : base (target, source, source.Location)
706                         {
707                         }
708
709                         protected override Expression ResolveConversions (ResolveContext ec)
710                         {
711                                 //
712                                 // Implicit conversion check fails for hoisted type arguments
713                                 // as they are of different types (!!0 x !0)
714                                 //
715                                 return this;
716                         }
717                 }
718
719                 readonly ParameterReference parameter;
720
721                 public HoistedParameter (AnonymousMethodStorey scope, ParameterReference par)
722                         : base (scope, par.Name, par.Type)
723                 {
724                         this.parameter = par;
725                 }
726
727                 public HoistedParameter (HoistedParameter hp, string name)
728                         : base (hp.storey, name, hp.parameter.Type)
729                 {
730                         this.parameter = hp.parameter;
731                 }
732
733                 public void EmitHoistingAssignment (EmitContext ec)
734                 {
735                         //
736                         // Remove hoisted redirection to emit assignment from original parameter
737                         //
738                         HoistedVariable temp = parameter.Parameter.HoistedVariant;
739                         parameter.Parameter.HoistedVariant = null;
740
741                         Assign a = new HoistedFieldAssign (GetFieldExpression (ec), parameter);
742                         if (a.Resolve (new ResolveContext (ec.MemberContext)) != null)
743                                 a.EmitStatement (ec);
744
745                         parameter.Parameter.HoistedVariant = temp;
746                 }
747
748                 public override void EmitSymbolInfo ()
749                 {
750                         SymbolWriter.DefineCapturedParameter (storey.ID, field.Name, field.Name);
751                 }
752
753                 public Field Field {
754                         get { return field; }
755                 }
756         }
757
758         class HoistedLocalVariable : HoistedVariable
759         {
760                 readonly string name;
761
762                 public HoistedLocalVariable (AnonymousMethodStorey scope, LocalVariable local, string name)
763                         : base (scope, name, local.Type)
764                 {
765                         this.name = local.Name;
766                 }
767
768                 public override void EmitSymbolInfo ()
769                 {
770                         SymbolWriter.DefineCapturedLocal (storey.ID, name, field.Name);
771                 }
772         }
773
774         public class HoistedThis : HoistedVariable
775         {
776                 public HoistedThis (AnonymousMethodStorey storey, Field field)
777                         : base (storey, field)
778                 {
779                 }
780
781                 public void EmitHoistingAssignment (EmitContext ec)
782                 {
783                         SimpleAssign a = new SimpleAssign (GetFieldExpression (ec), new CompilerGeneratedThis (ec.CurrentType, field.Location));
784                         if (a.Resolve (new ResolveContext (ec.MemberContext)) != null)
785                                 a.EmitStatement (ec);
786                 }
787
788                 public override void EmitSymbolInfo ()
789                 {
790                         SymbolWriter.DefineCapturedThis (storey.ID, field.Name);
791                 }
792
793                 public Field Field {
794                         get { return field; }
795                 }
796         }
797
798         //
799         // Anonymous method expression as created by parser
800         //
801         public class AnonymousMethodExpression : Expression
802         {
803                 //
804                 // Special conversion for nested expression tree lambdas
805                 //
806                 class Quote : ShimExpression
807                 {
808                         public Quote (Expression expr)
809                                 : base (expr)
810                         {
811                         }
812
813                         public override Expression CreateExpressionTree (ResolveContext ec)
814                         {
815                                 var args = new Arguments (1);
816                                 args.Add (new Argument (expr.CreateExpressionTree (ec)));
817                                 return CreateExpressionFactoryCall (ec, "Quote", args);
818                         }
819
820                         protected override Expression DoResolve (ResolveContext rc)
821                         {
822                                 expr = expr.Resolve (rc);
823                                 if (expr == null)
824                                         return null;
825
826                                 eclass = expr.eclass;
827                                 type = expr.Type;
828                                 return this;
829                         }
830                 }
831
832                 Dictionary<TypeSpec, Expression> compatibles;
833                 public ParametersBlock Block;
834
835                 public AnonymousMethodExpression (Location loc)
836                 {
837                         this.loc = loc;
838                         this.compatibles = new Dictionary<TypeSpec, Expression> ();
839                 }
840
841                 public override string ExprClassName {
842                         get {
843                                 return "anonymous method";
844                         }
845                 }
846
847                 public virtual bool HasExplicitParameters {
848                         get {
849                                 return Parameters != ParametersCompiled.Undefined;
850                         }
851                 }
852                 
853                 public ParametersCompiled Parameters {
854                         get { return Block.Parameters; }
855                 }
856
857                 //
858                 // Returns true if the body of lambda expression can be implicitly
859                 // converted to the delegate of type `delegate_type'
860                 //
861                 public bool ImplicitStandardConversionExists (ResolveContext ec, TypeSpec delegate_type)
862                 {
863                         using (ec.With (ResolveContext.Options.InferReturnType, false)) {
864                                 using (ec.Set (ResolveContext.Options.ProbingMode)) {
865                                         return Compatible (ec, delegate_type) != null;
866                                 }
867                         }
868                 }
869
870                 TypeSpec CompatibleChecks (ResolveContext ec, TypeSpec delegate_type)
871                 {
872                         if (delegate_type.IsDelegate)
873                                 return delegate_type;
874
875                         if (delegate_type.IsGeneric && delegate_type.GetDefinition () == TypeManager.expression_type) {
876                                 delegate_type = delegate_type.TypeArguments [0];
877                                 if (delegate_type.IsDelegate)
878                                         return delegate_type;
879
880                                 ec.Report.Error (835, loc, "Cannot convert `{0}' to an expression tree of non-delegate type `{1}'",
881                                         GetSignatureForError (), TypeManager.CSharpName (delegate_type));
882                                 return null;
883                         }
884
885                         ec.Report.Error (1660, loc, "Cannot convert `{0}' to non-delegate type `{1}'",
886                                       GetSignatureForError (), TypeManager.CSharpName (delegate_type));
887                         return null;
888                 }
889
890                 protected bool VerifyExplicitParameters (ResolveContext ec, TypeSpec delegate_type, AParametersCollection parameters)
891                 {
892                         if (VerifyParameterCompatibility (ec, delegate_type, parameters, ec.IsInProbingMode))
893                                 return true;
894
895                         if (!ec.IsInProbingMode)
896                                 ec.Report.Error (1661, loc,
897                                         "Cannot convert `{0}' to delegate type `{1}' since there is a parameter mismatch",
898                                         GetSignatureForError (), TypeManager.CSharpName (delegate_type));
899
900                         return false;
901                 }
902
903                 protected bool VerifyParameterCompatibility (ResolveContext ec, TypeSpec delegate_type, AParametersCollection invoke_pd, bool ignore_errors)
904                 {
905                         if (Parameters.Count != invoke_pd.Count) {
906                                 if (ignore_errors)
907                                         return false;
908                                 
909                                 ec.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
910                                               TypeManager.CSharpName (delegate_type), Parameters.Count.ToString ());
911                                 return false;
912                         }
913
914                         bool has_implicit_parameters = !HasExplicitParameters;
915                         bool error = false;
916
917                         for (int i = 0; i < Parameters.Count; ++i) {
918                                 Parameter.Modifier p_mod = invoke_pd.FixedParameters [i].ModFlags;
919                                 if (Parameters.FixedParameters [i].ModFlags != p_mod && p_mod != Parameter.Modifier.PARAMS) {
920                                         if (ignore_errors)
921                                                 return false;
922                                         
923                                         if (p_mod == Parameter.Modifier.NONE)
924                                                 ec.Report.Error (1677, loc, "Parameter `{0}' should not be declared with the `{1}' keyword",
925                                                               (i + 1).ToString (), Parameter.GetModifierSignature (Parameters.FixedParameters [i].ModFlags));
926                                         else
927                                                 ec.Report.Error (1676, loc, "Parameter `{0}' must be declared with the `{1}' keyword",
928                                                               (i+1).ToString (), Parameter.GetModifierSignature (p_mod));
929                                         error = true;
930                                 }
931
932                                 if (has_implicit_parameters)
933                                         continue;
934
935                                 TypeSpec type = invoke_pd.Types [i];
936                                 
937                                 // We assume that generic parameters are always inflated
938                                 if (TypeManager.IsGenericParameter (type))
939                                         continue;
940                                 
941                                 if (TypeManager.HasElementType (type) && TypeManager.IsGenericParameter (TypeManager.GetElementType (type)))
942                                         continue;
943                                 
944                                 if (!TypeSpecComparer.IsEqual (invoke_pd.Types [i], Parameters.Types [i])) {
945                                         if (ignore_errors)
946                                                 return false;
947                                         
948                                         ec.Report.Error (1678, loc, "Parameter `{0}' is declared as type `{1}' but should be `{2}'",
949                                                       (i+1).ToString (),
950                                                       TypeManager.CSharpName (Parameters.Types [i]),
951                                                       TypeManager.CSharpName (invoke_pd.Types [i]));
952                                         error = true;
953                                 }
954                         }
955
956                         return !error;
957                 }
958
959                 //
960                 // Infers type arguments based on explicit arguments
961                 //
962                 public bool ExplicitTypeInference (ResolveContext ec, TypeInferenceContext type_inference, TypeSpec delegate_type)
963                 {
964                         if (!HasExplicitParameters)
965                                 return false;
966
967                         if (!delegate_type.IsDelegate) {
968                                 if (delegate_type.GetDefinition () != TypeManager.expression_type)
969                                         return false;
970
971                                 delegate_type = TypeManager.GetTypeArguments (delegate_type) [0];
972                                 if (!delegate_type.IsDelegate)
973                                         return false;
974                         }
975                         
976                         AParametersCollection d_params = Delegate.GetParameters (delegate_type);
977                         if (d_params.Count != Parameters.Count)
978                                 return false;
979
980                         for (int i = 0; i < Parameters.Count; ++i) {
981                                 TypeSpec itype = d_params.Types [i];
982                                 if (!TypeManager.IsGenericParameter (itype)) {
983                                         if (!TypeManager.HasElementType (itype))
984                                                 continue;
985                                         
986                                         if (!TypeManager.IsGenericParameter (TypeManager.GetElementType (itype)))
987                                             continue;
988                                 }
989                                 type_inference.ExactInference (Parameters.Types [i], itype);
990                         }
991                         return true;
992                 }
993
994                 public TypeSpec InferReturnType (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegate_type)
995                 {
996                         Expression expr;
997                         AnonymousExpression am;
998
999                         if (compatibles.TryGetValue (delegate_type, out expr)) {
1000                                 am = expr as AnonymousExpression;
1001                                 return am == null ? null : am.ReturnType;
1002                         }
1003
1004                         using (ec.Set (ResolveContext.Options.ProbingMode | ResolveContext.Options.InferReturnType)) {
1005                                 am = CompatibleMethodBody (ec, tic, InternalType.Arglist, delegate_type);
1006                                 if (am != null)
1007                                         am = am.Compatible (ec);
1008                         }
1009
1010                         if (am == null)
1011                                 return null;
1012
1013 //                      compatibles.Add (delegate_type, am);
1014                         return am.ReturnType;
1015                 }
1016
1017                 //
1018                 // Returns AnonymousMethod container if this anonymous method
1019                 // expression can be implicitly converted to the delegate type `delegate_type'
1020                 //
1021                 public Expression Compatible (ResolveContext ec, TypeSpec type)
1022                 {
1023                         Expression am;
1024                         if (compatibles.TryGetValue (type, out am))
1025                                 return am;
1026
1027                         TypeSpec delegate_type = CompatibleChecks (ec, type);
1028                         if (delegate_type == null)
1029                                 return null;
1030
1031                         //
1032                         // At this point its the first time we know the return type that is 
1033                         // needed for the anonymous method.  We create the method here.
1034                         //
1035
1036                         var invoke_mb = Delegate.GetInvokeMethod (delegate_type);
1037                         TypeSpec return_type = invoke_mb.ReturnType;
1038
1039                         //
1040                         // Second: the return type of the delegate must be compatible with 
1041                         // the anonymous type.   Instead of doing a pass to examine the block
1042                         // we satisfy the rule by setting the return type on the EmitContext
1043                         // to be the delegate type return type.
1044                         //
1045
1046                         var body = CompatibleMethodBody (ec, null, return_type, delegate_type);
1047                         if (body == null)
1048                                 return null;
1049
1050                         bool etree_conversion = delegate_type != type;
1051
1052                         try {
1053                                 if (etree_conversion) {
1054                                         if (ec.HasSet (ResolveContext.Options.ExpressionTreeConversion)) {
1055                                                 //
1056                                                 // Nested expression tree lambda use same scope as parent
1057                                                 // lambda, this also means no variable capturing between this
1058                                                 // and parent scope
1059                                                 //
1060                                                 am = body.Compatible (ec, ec.CurrentAnonymousMethod);
1061
1062                                                 //
1063                                                 // Quote nested expression tree
1064                                                 //
1065                                                 if (am != null)
1066                                                         am = new Quote (am);
1067                                         } else {
1068                                                 int errors = ec.Report.Errors;
1069
1070                                                 using (ec.Set (ResolveContext.Options.ExpressionTreeConversion)) {
1071                                                         am = body.Compatible (ec);
1072                                                 }
1073
1074                                                 //
1075                                                 // Rewrite expressions into expression tree when targeting Expression<T>
1076                                                 //
1077                                                 if (am != null && errors == ec.Report.Errors)
1078                                                         am = CreateExpressionTree (ec, delegate_type);
1079                                         }
1080                                 } else {
1081                                         am = body.Compatible (ec);
1082                                 }
1083                         } catch (CompletionResult) {
1084                                 throw;
1085                         } catch (Exception e) {
1086                                 throw new InternalErrorException (e, loc);
1087                         }
1088
1089                         if (!ec.IsInProbingMode) {
1090                                 compatibles.Add (type, am ?? EmptyExpression.Null);
1091                         }
1092
1093                         return am;
1094                 }
1095
1096                 protected virtual Expression CreateExpressionTree (ResolveContext ec, TypeSpec delegate_type)
1097                 {
1098                         return CreateExpressionTree (ec);
1099                 }
1100
1101                 public override Expression CreateExpressionTree (ResolveContext ec)
1102                 {
1103                         ec.Report.Error (1946, loc, "An anonymous method cannot be converted to an expression tree");
1104                         return null;
1105                 }
1106
1107                 protected virtual ParametersCompiled ResolveParameters (ResolveContext ec, TypeInferenceContext tic, TypeSpec delegate_type)
1108                 {
1109                         var delegate_parameters = Delegate.GetParameters (delegate_type);
1110
1111                         if (Parameters == ParametersCompiled.Undefined) {
1112                                 //
1113                                 // We provide a set of inaccessible parameters
1114                                 //
1115                                 Parameter[] fixedpars = new Parameter[delegate_parameters.Count];
1116
1117                                 for (int i = 0; i < delegate_parameters.Count; i++) {
1118                                         Parameter.Modifier i_mod = delegate_parameters.FixedParameters [i].ModFlags;
1119                                         if (i_mod == Parameter.Modifier.OUT) {
1120                                                 if (!ec.IsInProbingMode) {
1121                                                         ec.Report.Error (1688, loc,
1122                                                                 "Cannot convert anonymous method block without a parameter list to delegate type `{0}' because it has one or more `out' parameters",
1123                                                                 delegate_type.GetSignatureForError ());
1124                                                 }
1125
1126                                                 return null;
1127                                         }
1128                                         fixedpars[i] = new Parameter (
1129                                                 new TypeExpression (delegate_parameters.Types [i], loc), null,
1130                                                 delegate_parameters.FixedParameters [i].ModFlags, null, loc);
1131                                 }
1132
1133                                 return ParametersCompiled.CreateFullyResolved (fixedpars, delegate_parameters.Types);
1134                         }
1135
1136                         if (!VerifyExplicitParameters (ec, delegate_type, delegate_parameters)) {
1137                                 return null;
1138                         }
1139
1140                         return Parameters;
1141                 }
1142
1143                 protected override Expression DoResolve (ResolveContext ec)
1144                 {
1145                         if (ec.HasSet (ResolveContext.Options.ConstantScope)) {
1146                                 ec.Report.Error (1706, loc, "Anonymous methods and lambda expressions cannot be used in the current context");
1147                                 return null;
1148                         }
1149
1150                         //
1151                         // Set class type, set type
1152                         //
1153
1154                         eclass = ExprClass.Value;
1155
1156                         //
1157                         // This hack means `The type is not accessible
1158                         // anywhere', we depend on special conversion
1159                         // rules.
1160                         // 
1161                         type = InternalType.AnonymousMethod;
1162
1163                         if (!DoResolveParameters (ec))
1164                                 return null;
1165
1166                         // FIXME: The emitted code isn't very careful about reachability
1167                         // so, ensure we have a 'ret' at the end
1168                         BlockContext bc = ec as BlockContext;
1169                         if (bc != null && bc.CurrentBranching != null && bc.CurrentBranching.CurrentUsageVector.IsUnreachable)
1170                                 bc.NeedReturnLabel ();
1171
1172                         return this;
1173                 }
1174
1175                 protected virtual bool DoResolveParameters (ResolveContext rc)
1176                 {
1177                         return Parameters.Resolve (rc);
1178                 }
1179
1180                 public override void Emit (EmitContext ec)
1181                 {
1182                         // nothing, as we only exist to not do anything.
1183                 }
1184
1185                 public static void Error_AddressOfCapturedVar (ResolveContext ec, IVariableReference var, Location loc)
1186                 {
1187                         ec.Report.Error (1686, loc,
1188                                 "Local variable or parameter `{0}' cannot have their address taken and be used inside an anonymous method or lambda expression",
1189                                 var.Name);
1190                 }
1191
1192                 public override string GetSignatureForError ()
1193                 {
1194                         return ExprClassName;
1195                 }
1196
1197                 AnonymousMethodBody CompatibleMethodBody (ResolveContext ec, TypeInferenceContext tic, TypeSpec return_type, TypeSpec delegate_type)
1198                 {
1199                         ParametersCompiled p = ResolveParameters (ec, tic, delegate_type);
1200                         if (p == null)
1201                                 return null;
1202
1203                         ParametersBlock b = ec.IsInProbingMode ? (ParametersBlock) Block.PerformClone () : Block;
1204
1205                         return CompatibleMethodFactory (return_type, delegate_type, p, b);
1206
1207                 }
1208
1209                 protected virtual AnonymousMethodBody CompatibleMethodFactory (TypeSpec return_type, TypeSpec delegate_type, ParametersCompiled p, ParametersBlock b)
1210                 {
1211                         return new AnonymousMethodBody (p, b, return_type, delegate_type, loc);
1212                 }
1213
1214                 protected override void CloneTo (CloneContext clonectx, Expression t)
1215                 {
1216                         AnonymousMethodExpression target = (AnonymousMethodExpression) t;
1217
1218                         target.Block = (ParametersBlock) clonectx.LookupBlock (Block);
1219                 }
1220         }
1221
1222         //
1223         // Abstract expression for any block which requires variables hoisting
1224         //
1225         public abstract class AnonymousExpression : Expression
1226         {
1227                 protected class AnonymousMethodMethod : Method
1228                 {
1229                         public readonly AnonymousExpression AnonymousMethod;
1230                         public readonly AnonymousMethodStorey Storey;
1231                         readonly string RealName;
1232
1233                         public AnonymousMethodMethod (DeclSpace parent, AnonymousExpression am, AnonymousMethodStorey storey,
1234                                                           GenericMethod generic, TypeExpr return_type,
1235                                                           Modifiers mod, string real_name, MemberName name,
1236                                                           ParametersCompiled parameters)
1237                                 : base (parent, generic, return_type, mod | Modifiers.COMPILER_GENERATED,
1238                                                 name, parameters, null)
1239                         {
1240                                 this.AnonymousMethod = am;
1241                                 this.Storey = storey;
1242                                 this.RealName = real_name;
1243
1244                                 Parent.PartialContainer.AddMethod (this);
1245                                 Block = new ToplevelBlock (am.block, parameters);
1246                         }
1247
1248                         public override EmitContext CreateEmitContext (ILGenerator ig)
1249                         {
1250                                 EmitContext ec = new EmitContext (this, ig, ReturnType);
1251                                 ec.CurrentAnonymousMethod = AnonymousMethod;
1252                                 if (AnonymousMethod.return_label != null) {
1253                                         ec.HasReturnLabel = true;
1254                                         ec.ReturnLabel = (Label) AnonymousMethod.return_label;
1255                                 }
1256
1257                                 return ec;
1258                         }
1259
1260                         protected override void DefineTypeParameters ()
1261                         {
1262                                 // Type parameters were cloned
1263                         }
1264
1265                         protected override bool ResolveMemberType ()
1266                         {
1267                                 if (!base.ResolveMemberType ())
1268                                         return false;
1269
1270                                 if (Storey != null && Storey.Mutator != null) {
1271                                         if (!parameters.IsEmpty) {
1272                                                 var mutated = Storey.Mutator.Mutate (parameters.Types);
1273                                                 if (mutated != parameters.Types)
1274                                                         parameters = ParametersCompiled.CreateFullyResolved ((Parameter[]) parameters.FixedParameters, mutated);
1275                                         }
1276
1277                                         member_type = Storey.Mutator.Mutate (member_type);
1278                                 }
1279
1280                                 return true;
1281                         }
1282
1283                         public override void Emit ()
1284                         {
1285                                 if (MethodBuilder == null) {
1286                                         Define ();
1287                                 }
1288
1289                                 base.Emit ();
1290                         }
1291
1292                         public override void EmitExtraSymbolInfo (SourceMethod source)
1293                         {
1294                                 source.SetRealMethodName (RealName);
1295                         }
1296                 }
1297
1298                 protected ParametersBlock block;
1299
1300                 public TypeSpec ReturnType;
1301
1302                 object return_label;
1303
1304                 protected AnonymousExpression (ParametersBlock block, TypeSpec return_type, Location loc)
1305                 {
1306                         this.ReturnType = return_type;
1307                         this.block = block;
1308                         this.loc = loc;
1309                 }
1310
1311                 public abstract string ContainerType { get; }
1312                 public abstract bool IsIterator { get; }
1313                 public abstract AnonymousMethodStorey Storey { get; }
1314
1315                 public AnonymousExpression Compatible (ResolveContext ec)
1316                 {
1317                         return Compatible (ec, this);
1318                 }
1319
1320                 public AnonymousExpression Compatible (ResolveContext ec, AnonymousExpression ae)
1321                 {
1322                         if (block.Resolved)
1323                                 return this;
1324
1325                         // TODO: Implement clone
1326                         BlockContext aec = new BlockContext (ec, block, ReturnType);
1327                         aec.CurrentAnonymousMethod = ae;
1328
1329                         ResolveContext.Options flags = 0;
1330
1331                         var am = this as AnonymousMethodBody;
1332
1333                         if (ec.HasSet (ResolveContext.Options.InferReturnType) && am != null) {
1334                                 am.ReturnTypeInference = new TypeInferenceContext ();
1335                         }
1336
1337                         if (ec.IsInProbingMode)
1338                                 flags |= ResolveContext.Options.ProbingMode;
1339
1340                         if (ec.HasSet (ResolveContext.Options.FieldInitializerScope))
1341                                 flags |= ResolveContext.Options.FieldInitializerScope;
1342
1343                         if (ec.HasSet (ResolveContext.Options.ExpressionTreeConversion))
1344                                 flags |= ResolveContext.Options.ExpressionTreeConversion;
1345
1346                         aec.Set (flags);
1347
1348                         var errors = ec.Report.Errors;
1349
1350                         bool res = Block.Resolve (ec.CurrentBranching, aec, null);
1351
1352                         if (aec.HasReturnLabel)
1353                                 return_label = aec.ReturnLabel;
1354
1355                         if (am != null && am.ReturnTypeInference != null) {
1356                                 am.ReturnTypeInference.FixAllTypes (ec);
1357                                 ReturnType = am.ReturnTypeInference.InferredTypeArguments [0];
1358                                 am.ReturnTypeInference = null;
1359                         }
1360
1361                         if (res && errors != ec.Report.Errors)
1362                                 return null;
1363
1364                         return res ? this : null;
1365                 }
1366
1367                 public void SetHasThisAccess ()
1368                 {
1369                         ExplicitBlock b = block;
1370                         do {
1371                                 if (b.HasCapturedThis)
1372                                         return;
1373
1374                                 b.HasCapturedThis = true;
1375                                 b = b.Parent == null ? null : b.Parent.Explicit;
1376                         } while (b != null);
1377                 }
1378
1379                 //
1380                 // The block that makes up the body for the anonymous method
1381                 //
1382                 public ParametersBlock Block {
1383                         get {
1384                                 return block;
1385                         }
1386                 }
1387
1388         }
1389
1390         public class AnonymousMethodBody : AnonymousExpression
1391         {
1392                 protected readonly ParametersCompiled parameters;
1393                 AnonymousMethodStorey storey;
1394
1395                 AnonymousMethodMethod method;
1396                 Field am_cache;
1397                 string block_name;
1398                 TypeInferenceContext return_inference;
1399
1400                 static int unique_id;
1401
1402                 public AnonymousMethodBody (ParametersCompiled parameters,
1403                                         ParametersBlock block, TypeSpec return_type, TypeSpec delegate_type,
1404                                         Location loc)
1405                         : base (block, return_type, loc)
1406                 {
1407                         this.type = delegate_type;
1408                         this.parameters = parameters;
1409                 }
1410
1411                 #region Properties
1412
1413                 public override string ContainerType {
1414                         get { return "anonymous method"; }
1415                 }
1416
1417                 public override bool IsIterator {
1418                         get { return false; }
1419                 }
1420
1421                 public TypeInferenceContext ReturnTypeInference {
1422                         get {
1423                                 return return_inference;
1424                         }
1425                         set {
1426                                 return_inference = value;
1427                         }
1428                 }
1429
1430                 public override AnonymousMethodStorey Storey {
1431                         get { return storey; }
1432                 }
1433
1434                 #endregion
1435
1436                 public override Expression CreateExpressionTree (ResolveContext ec)
1437                 {
1438                         ec.Report.Error (1945, loc, "An expression tree cannot contain an anonymous method expression");
1439                         return null;
1440                 }
1441
1442                 bool Define (ResolveContext ec)
1443                 {
1444                         if (!Block.Resolved && Compatible (ec) == null)
1445                                 return false;
1446
1447                         if (block_name == null) {
1448                                 MemberCore mc = (MemberCore) ec.MemberContext;
1449                                 block_name = mc.MemberName.Basename;
1450                         }
1451
1452                         return true;
1453                 }
1454
1455                 //
1456                 // Creates a host for the anonymous method
1457                 //
1458                 AnonymousMethodMethod DoCreateMethodHost (EmitContext ec)
1459                 {
1460                         //
1461                         // Anonymous method body can be converted to
1462                         //
1463                         // 1, an instance method in current scope when only `this' is hoisted
1464                         // 2, a static method in current scope when neither `this' nor any variable is hoisted
1465                         // 3, an instance method in compiler generated storey when any hoisted variable exists
1466                         //
1467
1468                         Modifiers modifiers;
1469                         if (Block.HasCapturedVariable || Block.HasCapturedThis) {
1470                                 storey = FindBestMethodStorey ();
1471                                 modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
1472                         } else {
1473                                 if (ec.CurrentAnonymousMethod != null)
1474                                         storey = ec.CurrentAnonymousMethod.Storey;
1475
1476                                 modifiers = Modifiers.STATIC | Modifiers.PRIVATE;
1477                         }
1478
1479                         TypeContainer parent = storey != null ? storey : ec.CurrentTypeDefinition.Parent.PartialContainer;
1480
1481                         MemberCore mc = ec.MemberContext as MemberCore;
1482                         string name = CompilerGeneratedClass.MakeName (parent != storey ? block_name : null,
1483                                 "m", null, unique_id++);
1484
1485                         MemberName member_name;
1486                         GenericMethod generic_method;
1487                         if (storey == null && mc.MemberName.TypeArguments != null) {
1488                                 member_name = new MemberName (name, mc.MemberName.TypeArguments.Clone (), Location);
1489
1490                                 var hoisted_tparams = ec.CurrentTypeParameters;
1491                                 var type_params = new TypeParameter[hoisted_tparams.Length];
1492                                 for (int i = 0; i < type_params.Length; ++i) {
1493                                         type_params[i] = hoisted_tparams[i].CreateHoistedCopy (parent, null);
1494                                 }
1495
1496                                 generic_method = new GenericMethod (parent.NamespaceEntry, parent, member_name, type_params,
1497                                         new TypeExpression (ReturnType, Location), parameters);
1498                         } else {
1499                                 member_name = new MemberName (name, Location);
1500                                 generic_method = null;
1501                         }
1502
1503                         string real_name = String.Format (
1504                                 "{0}~{1}{2}", mc.GetSignatureForError (), GetSignatureForError (),
1505                                 parameters.GetSignatureForError ());
1506
1507                         return new AnonymousMethodMethod (parent,
1508                                 this, storey, generic_method, new TypeExpression (ReturnType, Location), modifiers,
1509                                 real_name, member_name, parameters);
1510                 }
1511
1512                 protected override Expression DoResolve (ResolveContext ec)
1513                 {
1514                         if (!Define (ec))
1515                                 return null;
1516
1517                         eclass = ExprClass.Value;
1518                         return this;
1519                 }
1520
1521                 public override void Emit (EmitContext ec)
1522                 {
1523                         //
1524                         // Use same anonymous method implementation for scenarios where same
1525                         // code is used from multiple blocks, e.g. field initializers
1526                         //
1527                         if (method == null) {
1528                                 //
1529                                 // Delay an anonymous method definition to avoid emitting unused code
1530                                 // for unreachable blocks or expression trees
1531                                 //
1532                                 method = DoCreateMethodHost (ec);
1533                                 method.Define ();
1534                         }
1535
1536                         bool is_static = (method.ModFlags & Modifiers.STATIC) != 0;
1537                         if (is_static && am_cache == null) {
1538                                 //
1539                                 // Creates a field cache to store delegate instance if it's not generic
1540                                 //
1541                                 if (!method.MemberName.IsGeneric) {
1542                                         TypeContainer parent = method.Parent.PartialContainer;
1543                                         int id = parent.Fields == null ? 0 : parent.Fields.Count;
1544                                         var cache_type = storey != null && storey.Mutator != null ? storey.Mutator.Mutate (type) : type;
1545
1546                                         am_cache = new Field (parent, new TypeExpression (cache_type, loc),
1547                                                 Modifiers.STATIC | Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED,
1548                                                 new MemberName (CompilerGeneratedClass.MakeName (null, "f", "am$cache", id), loc), null);
1549                                         am_cache.Define ();
1550                                         parent.AddField (am_cache);
1551                                 } else {
1552                                         // TODO: Implement caching of generated generic static methods
1553                                         //
1554                                         // Idea:
1555                                         //
1556                                         // Some extra class is needed to capture variable generic type
1557                                         // arguments. Maybe we could re-use anonymous types, with a unique
1558                                         // anonymous method id, but they are quite heavy.
1559                                         //
1560                                         // Consider : "() => typeof(T);"
1561                                         //
1562                                         // We need something like
1563                                         // static class Wrap<Tn, Tm, DelegateType> {
1564                                         //              public static DelegateType cache;
1565                                         // }
1566                                         //
1567                                         // We then specialize local variable to capture all generic parameters
1568                                         // and delegate type, e.g. "Wrap<Ta, Tb, DelegateTypeInst> cache;"
1569                                         //
1570                                 }
1571                         }
1572
1573                         Label l_initialized = ec.DefineLabel ();
1574
1575                         if (am_cache != null) {
1576                                 ec.Emit (OpCodes.Ldsfld, am_cache.Spec);
1577                                 ec.Emit (OpCodes.Brtrue_S, l_initialized);
1578                         }
1579
1580                         //
1581                         // Load method delegate implementation
1582                         //
1583
1584                         if (is_static) {
1585                                 ec.Emit (OpCodes.Ldnull);
1586                         } else if (storey != null) {
1587                                 Expression e = storey.GetStoreyInstanceExpression (ec).Resolve (new ResolveContext (ec.MemberContext));
1588                                 if (e != null)
1589                                         e.Emit (ec);
1590                         } else {
1591                                 ec.Emit (OpCodes.Ldarg_0);
1592                         }
1593
1594                         var delegate_method = method.Spec;
1595                         if (storey != null && storey.MemberName.IsGeneric) {
1596                                 TypeSpec t = storey.Instance.Type;
1597
1598                                 //
1599                                 // Mutate anonymous method instance type if we are in nested
1600                                 // hoisted generic anonymous method storey
1601                                 //
1602                                 if (ec.CurrentAnonymousMethod != null &&
1603                                         ec.CurrentAnonymousMethod.Storey != null &&
1604                                         ec.CurrentAnonymousMethod.Storey.Mutator != null) {
1605                                         t = storey.Mutator.Mutate (t);
1606                                 }
1607
1608                                 ec.Emit (OpCodes.Ldftn, TypeBuilder.GetMethod (t.GetMetaInfo (), (MethodInfo) delegate_method.GetMetaInfo ()));
1609                         } else {
1610                                 if (delegate_method.IsGeneric)
1611                                         delegate_method = delegate_method.MakeGenericMethod (ec.MemberContext, method.TypeParameters);
1612
1613                                 ec.Emit (OpCodes.Ldftn, delegate_method);
1614                         }
1615
1616                         var constructor_method = Delegate.GetConstructor (type);
1617                         ec.Emit (OpCodes.Newobj, constructor_method);
1618
1619                         if (am_cache != null) {
1620                                 ec.Emit (OpCodes.Stsfld, am_cache.Spec);
1621                                 ec.MarkLabel (l_initialized);
1622                                 ec.Emit (OpCodes.Ldsfld, am_cache.Spec);
1623                         }
1624                 }
1625
1626                 //
1627                 // Look for the best storey for this anonymous method
1628                 //
1629                 AnonymousMethodStorey FindBestMethodStorey ()
1630                 {
1631                         //
1632                         // Use the nearest parent block which has a storey
1633                         //
1634                         for (Block b = Block.Parent; b != null; b = b.Parent) {
1635                                 AnonymousMethodStorey s = b.Explicit.AnonymousMethodStorey;
1636                                 if (s != null)
1637                                         return s;
1638                         }
1639                                         
1640                         return null;
1641                 }
1642
1643                 public override string GetSignatureForError ()
1644                 {
1645                         return TypeManager.CSharpName (type);
1646                 }
1647
1648                 public static void Reset ()
1649                 {
1650                         unique_id = 0;
1651                 }
1652         }
1653
1654         //
1655         // Anonymous type container
1656         //
1657         public class AnonymousTypeClass : CompilerGeneratedClass
1658         {
1659                 // TODO: Merge with AnonymousTypeParameter
1660                 public class GeneratedParameter : Parameter
1661                 {
1662                         public GeneratedParameter (FullNamedExpression type, AnonymousTypeParameter p)
1663                                 : base (type, p.Name, Modifier.NONE, null, p.Location)
1664                         {
1665                         }
1666                 }
1667
1668                 static int types_counter;
1669                 public const string ClassNamePrefix = "<>__AnonType";
1670                 public const string SignatureForError = "anonymous type";
1671                 
1672                 readonly IList<AnonymousTypeParameter> parameters;
1673
1674                 private AnonymousTypeClass (DeclSpace parent, MemberName name, IList<AnonymousTypeParameter> parameters, Location loc)
1675                         : base (parent, name, (parent.Module.Evaluator != null ? Modifiers.PUBLIC : 0) | Modifiers.SEALED)
1676                 {
1677                         this.parameters = parameters;
1678                 }
1679
1680                 public static AnonymousTypeClass Create (TypeContainer parent, IList<AnonymousTypeParameter> parameters, Location loc)
1681                 {
1682                         string name = ClassNamePrefix + types_counter++;
1683
1684                         ParametersCompiled all_parameters;
1685                         TypeParameterName[] t_params;
1686                         SimpleName[] t_args;
1687
1688                         if (parameters.Count == 0) {
1689                                 all_parameters = ParametersCompiled.EmptyReadOnlyParameters;
1690                                 t_params = new TypeParameterName[0];
1691                                 t_args = null;
1692                         } else {
1693                                 t_args = new SimpleName[parameters.Count];
1694                                 t_params = new TypeParameterName[parameters.Count];
1695                                 Parameter[] ctor_params = new Parameter[parameters.Count];
1696                                 for (int i = 0; i < parameters.Count; ++i) {
1697                                         AnonymousTypeParameter p = parameters[i];
1698
1699                                         t_args[i] = new SimpleName ("<" + p.Name + ">__T", p.Location);
1700                                         t_params[i] = new TypeParameterName (t_args[i].Name, null, p.Location);
1701                                         ctor_params[i] = new GeneratedParameter (t_args[i], p);
1702                                 }
1703
1704                                 all_parameters = new ParametersCompiled (ctor_params);
1705                         }
1706
1707                         //
1708                         // Create generic anonymous type host with generic arguments
1709                         // named upon properties names
1710                         //
1711                         AnonymousTypeClass a_type = new AnonymousTypeClass (parent.NamespaceEntry.SlaveDeclSpace,
1712                                 new MemberName (name, new TypeArguments (t_params), loc), parameters, loc);
1713
1714                         if (parameters.Count > 0)
1715                                 a_type.SetParameterInfo (null);
1716
1717                         Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
1718                                 null, all_parameters, null, loc);
1719                         c.Block = new ToplevelBlock (parent.Module.Compiler, c.ParameterInfo, loc);
1720
1721                         // 
1722                         // Create fields and contructor body with field initialization
1723                         //
1724                         bool error = false;
1725                         for (int i = 0; i < parameters.Count; ++i) {
1726                                 AnonymousTypeParameter p = parameters [i];
1727
1728                                 Field f = new Field (a_type, t_args [i], Modifiers.PRIVATE | Modifiers.READONLY,
1729                                         new MemberName ("<" + p.Name + ">", p.Location), null);
1730
1731                                 if (!a_type.AddField (f)) {
1732                                         error = true;
1733                                         continue;
1734                                 }
1735
1736                                 c.Block.AddStatement (new StatementExpression (
1737                                         new SimpleAssign (new MemberAccess (new This (p.Location), f.Name),
1738                                                 c.Block.GetParameterReference (i, p.Location))));
1739
1740                                 ToplevelBlock get_block = new ToplevelBlock (parent.Module.Compiler, p.Location);
1741                                 get_block.AddStatement (new Return (
1742                                         new MemberAccess (new This (p.Location), f.Name), p.Location));
1743
1744                                 Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC,
1745                                         new MemberName (p.Name, p.Location), null);
1746                                 prop.Get = new Property.GetMethod (prop, 0, null, p.Location);
1747                                 prop.Get.Block = get_block;
1748                                 a_type.AddProperty (prop);
1749                         }
1750
1751                         if (error)
1752                                 return null;
1753
1754                         a_type.AddConstructor (c);
1755                         return a_type;
1756                 }
1757                 
1758                 public static void Reset ()
1759                 {
1760                         types_counter = 0;
1761                 }
1762
1763                 protected override bool AddToContainer (MemberCore symbol, string name)
1764                 {
1765                         MemberCore mc = GetDefinition (name);
1766
1767                         if (mc == null) {
1768                                 defined_names.Add (name, symbol);
1769                                 return true;
1770                         }
1771
1772                         // A conflict between anonymous type members will be reported
1773                         if (symbol is TypeParameter) {
1774                                 Report.SymbolRelatedToPreviousError (symbol);
1775                                 return false;
1776                         }
1777
1778                         // Ignore other conflicts
1779                         return true;
1780                 }
1781
1782                 protected override bool DoDefineMembers ()
1783                 {
1784                         if (!base.DoDefineMembers ())
1785                                 return false;
1786
1787                         Location loc = Location;
1788
1789                         var equals_parameters = ParametersCompiled.CreateFullyResolved (
1790                                 new Parameter (new TypeExpression (TypeManager.object_type, loc), "obj", 0, null, loc), TypeManager.object_type);
1791
1792                         Method equals = new Method (this, null, new TypeExpression (TypeManager.bool_type, loc),
1793                                 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, new MemberName ("Equals", loc),
1794                                 equals_parameters, null);
1795
1796                         equals_parameters[0].Resolve (equals, 0);
1797
1798                         Method tostring = new Method (this, null, new TypeExpression (TypeManager.string_type, loc),
1799                                 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, new MemberName ("ToString", loc),
1800                                 Mono.CSharp.ParametersCompiled.EmptyReadOnlyParameters, null);
1801
1802                         ToplevelBlock equals_block = new ToplevelBlock (Compiler, equals.ParameterInfo, loc);
1803
1804                         TypeExpr current_type;
1805                         if (type_params != null) {
1806                                 var targs = new TypeArguments ();
1807                                 foreach (var type_param in type_params)
1808                                         targs.Add (new TypeParameterExpr (type_param, type_param.Location));
1809
1810                                 current_type = new GenericTypeExpr (Definition, targs, loc);
1811                         } else {
1812                                 current_type = new TypeExpression (Definition, loc);
1813                         }
1814
1815                         var li_other = LocalVariable.CreateCompilerGenerated (CurrentType, equals_block, loc);
1816                         equals_block.AddStatement (new BlockVariableDeclaration (new TypeExpression (li_other.Type, loc), li_other));
1817                         var other_variable = new LocalVariableReference (li_other, loc);
1818
1819                         MemberAccess system_collections_generic = new MemberAccess (new MemberAccess (
1820                                 new QualifiedAliasMember ("global", "System", loc), "Collections", loc), "Generic", loc);
1821
1822                         Expression rs_equals = null;
1823                         Expression string_concat = new StringConstant ("{", loc);
1824                         Expression rs_hashcode = new IntConstant (-2128831035, loc);
1825                         for (int i = 0; i < parameters.Count; ++i) {
1826                                 var p = parameters [i];
1827                                 var f = Fields [i];
1828
1829                                 MemberAccess equality_comparer = new MemberAccess (new MemberAccess (
1830                                         system_collections_generic, "EqualityComparer",
1831                                                 new TypeArguments (new SimpleName (CurrentTypeParameters [i].Name, loc)), loc),
1832                                                 "Default", loc);
1833
1834                                 Arguments arguments_equal = new Arguments (2);
1835                                 arguments_equal.Add (new Argument (new MemberAccess (new This (f.Location), f.Name)));
1836                                 arguments_equal.Add (new Argument (new MemberAccess (other_variable, f.Name)));
1837
1838                                 Expression field_equal = new Invocation (new MemberAccess (equality_comparer,
1839                                         "Equals", loc), arguments_equal);
1840
1841                                 Arguments arguments_hashcode = new Arguments (1);
1842                                 arguments_hashcode.Add (new Argument (new MemberAccess (new This (f.Location), f.Name)));
1843                                 Expression field_hashcode = new Invocation (new MemberAccess (equality_comparer,
1844                                         "GetHashCode", loc), arguments_hashcode);
1845
1846                                 IntConstant FNV_prime = new IntConstant (16777619, loc);                                
1847                                 rs_hashcode = new Binary (Binary.Operator.Multiply,
1848                                         new Binary (Binary.Operator.ExclusiveOr, rs_hashcode, field_hashcode, loc),
1849                                         FNV_prime, loc);
1850
1851                                 Expression field_to_string = new Conditional (new BooleanExpression (new Binary (Binary.Operator.Inequality,
1852                                         new MemberAccess (new This (f.Location), f.Name), new NullLiteral (loc), loc)),
1853                                         new Invocation (new MemberAccess (
1854                                                 new MemberAccess (new This (f.Location), f.Name), "ToString"), null),
1855                                         new StringConstant (string.Empty, loc), loc);
1856
1857                                 if (rs_equals == null) {
1858                                         rs_equals = field_equal;
1859                                         string_concat = new Binary (Binary.Operator.Addition,
1860                                                 string_concat,
1861                                                 new Binary (Binary.Operator.Addition,
1862                                                         new StringConstant (" " + p.Name + " = ", loc),
1863                                                         field_to_string,
1864                                                         loc),
1865                                                 loc);
1866                                         continue;
1867                                 }
1868
1869                                 //
1870                                 // Implementation of ToString () body using string concatenation
1871                                 //                              
1872                                 string_concat = new Binary (Binary.Operator.Addition,
1873                                         new Binary (Binary.Operator.Addition,
1874                                                 string_concat,
1875                                                 new StringConstant (", " + p.Name + " = ", loc),
1876                                                 loc),
1877                                         field_to_string,
1878                                         loc);
1879
1880                                 rs_equals = new Binary (Binary.Operator.LogicalAnd, rs_equals, field_equal, loc);
1881                         }
1882
1883                         string_concat = new Binary (Binary.Operator.Addition,
1884                                 string_concat,
1885                                 new StringConstant (" }", loc),
1886                                 loc);
1887
1888                         //
1889                         // Equals (object obj) override
1890                         //              
1891                         var other_variable_assign = new TemporaryVariableReference (li_other, loc);
1892                         equals_block.AddStatement (new StatementExpression (
1893                                 new SimpleAssign (other_variable_assign,
1894                                         new As (equals_block.GetParameterReference (0, loc),
1895                                                 current_type, loc), loc)));
1896
1897                         Expression equals_test = new Binary (Binary.Operator.Inequality, other_variable, new NullLiteral (loc), loc);
1898                         if (rs_equals != null)
1899                                 equals_test = new Binary (Binary.Operator.LogicalAnd, equals_test, rs_equals, loc);
1900                         equals_block.AddStatement (new Return (equals_test, loc));
1901
1902                         equals.Block = equals_block;
1903                         equals.Define ();
1904                         AddMethod (equals);
1905
1906                         //
1907                         // GetHashCode () override
1908                         //
1909                         Method hashcode = new Method (this, null, new TypeExpression (TypeManager.int32_type, loc),
1910                                 Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN,
1911                                 new MemberName ("GetHashCode", loc),
1912                                 Mono.CSharp.ParametersCompiled.EmptyReadOnlyParameters, null);
1913
1914                         //
1915                         // Modified FNV with good avalanche behavior and uniform
1916                         // distribution with larger hash sizes.
1917                         //
1918                         // const int FNV_prime = 16777619;
1919                         // int hash = (int) 2166136261;
1920                         // foreach (int d in data)
1921                         //     hash = (hash ^ d) * FNV_prime;
1922                         // hash += hash << 13;
1923                         // hash ^= hash >> 7;
1924                         // hash += hash << 3;
1925                         // hash ^= hash >> 17;
1926                         // hash += hash << 5;
1927
1928                         ToplevelBlock hashcode_top = new ToplevelBlock (Compiler, loc);
1929                         Block hashcode_block = new Block (hashcode_top, loc, loc);
1930                         hashcode_top.AddStatement (new Unchecked (hashcode_block, loc));
1931
1932                         var li_hash = LocalVariable.CreateCompilerGenerated (TypeManager.int32_type, hashcode_top, loc);
1933                         hashcode_block.AddStatement (new BlockVariableDeclaration (new TypeExpression (li_hash.Type, loc), li_hash));
1934                         LocalVariableReference hash_variable_assign = new LocalVariableReference (li_hash, loc);
1935                         hashcode_block.AddStatement (new StatementExpression (
1936                                 new SimpleAssign (hash_variable_assign, rs_hashcode)));
1937
1938                         var hash_variable = new LocalVariableReference (li_hash, loc);
1939                         hashcode_block.AddStatement (new StatementExpression (
1940                                 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1941                                         new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (13, loc), loc), loc)));
1942                         hashcode_block.AddStatement (new StatementExpression (
1943                                 new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
1944                                         new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (7, loc), loc), loc)));
1945                         hashcode_block.AddStatement (new StatementExpression (
1946                                 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1947                                         new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (3, loc), loc), loc)));
1948                         hashcode_block.AddStatement (new StatementExpression (
1949                                 new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
1950                                         new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (17, loc), loc), loc)));
1951                         hashcode_block.AddStatement (new StatementExpression (
1952                                 new CompoundAssign (Binary.Operator.Addition, hash_variable,
1953                                         new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (5, loc), loc), loc)));
1954
1955                         hashcode_block.AddStatement (new Return (hash_variable, loc));
1956                         hashcode.Block = hashcode_top;
1957                         hashcode.Define ();
1958                         AddMethod (hashcode);
1959
1960                         //
1961                         // ToString () override
1962                         //
1963
1964                         ToplevelBlock tostring_block = new ToplevelBlock (Compiler, loc);
1965                         tostring_block.AddStatement (new Return (string_concat, loc));
1966                         tostring.Block = tostring_block;
1967                         tostring.Define ();
1968                         AddMethod (tostring);
1969
1970                         return true;
1971                 }
1972
1973                 public override string GetSignatureForError ()
1974                 {
1975                         return SignatureForError;
1976                 }
1977
1978                 public IList<AnonymousTypeParameter> Parameters {
1979                         get {
1980                                 return parameters;
1981                         }
1982                 }
1983         }
1984 }