2008-04-19 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / ecore.cs
1 //
2 // ecore.cs: Core of the Expression representation for the intermediate tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Marek Safar (marek.safar@seznam.cz)
7 //
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
10 //
11 //
12
13 namespace Mono.CSharp {
14         using System;
15         using System.Collections;
16         using System.Diagnostics;
17         using System.Reflection;
18         using System.Reflection.Emit;
19         using System.Text;
20
21         /// <remarks>
22         ///   The ExprClass class contains the is used to pass the 
23         ///   classification of an expression (value, variable, namespace,
24         ///   type, method group, property access, event access, indexer access,
25         ///   nothing).
26         /// </remarks>
27         public enum ExprClass : byte {
28                 Invalid,
29                 
30                 Value,
31                 Variable,
32                 Namespace,
33                 Type,
34                 MethodGroup,
35                 PropertyAccess,
36                 EventAccess,
37                 IndexerAccess,
38                 Nothing, 
39         }
40
41         /// <remarks>
42         ///   This is used to tell Resolve in which types of expressions we're
43         ///   interested.
44         /// </remarks>
45         [Flags]
46         public enum ResolveFlags {
47                 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
48                 VariableOrValue         = 1,
49
50                 // Returns a type expression.
51                 Type                    = 2,
52
53                 // Returns a method group.
54                 MethodGroup             = 4,
55
56                 // Mask of all the expression class flags.
57                 MaskExprClass           = 7,
58
59                 // Disable control flow analysis while resolving the expression.
60                 // This is used when resolving the instance expression of a field expression.
61                 DisableFlowAnalysis     = 8,
62
63                 // Set if this is resolving the first part of a MemberAccess.
64                 Intermediate            = 16,
65
66                 // Disable control flow analysis _of struct_ while resolving the expression.
67                 // This is used when resolving the instance expression of a field expression.
68                 DisableStructFlowAnalysis       = 32,
69
70         }
71
72         //
73         // This is just as a hint to AddressOf of what will be done with the
74         // address.
75         [Flags]
76         public enum AddressOp {
77                 Store = 1,
78                 Load  = 2,
79                 LoadStore = 3
80         };
81         
82         /// <summary>
83         ///   This interface is implemented by variables
84         /// </summary>
85         public interface IMemoryLocation {
86                 /// <summary>
87                 ///   The AddressOf method should generate code that loads
88                 ///   the address of the object and leaves it on the stack.
89                 ///
90                 ///   The `mode' argument is used to notify the expression
91                 ///   of whether this will be used to read from the address or
92                 ///   write to the address.
93                 ///
94                 ///   This is just a hint that can be used to provide good error
95                 ///   reporting, and should have no other side effects. 
96                 /// </summary>
97                 void AddressOf (EmitContext ec, AddressOp mode);
98         }
99
100         /// <summary>
101         ///   This interface is implemented by variables
102         /// </summary>
103         public interface IVariable {
104                 VariableInfo VariableInfo {
105                         get;
106                 }
107
108                 bool VerifyFixed ();
109         }
110
111         /// <remarks>
112         ///   Base class for expressions
113         /// </remarks>
114         public abstract class Expression {
115                 public ExprClass eclass;
116                 protected Type type;
117                 protected Location loc;
118                 
119                 public Type Type {
120                         get { return type; }
121                         set { type = value; }
122                 }
123
124                 public virtual Location Location {
125                         get { return loc; }
126                 }
127
128                 /// <summary>
129                 ///   Utility wrapper routine for Error, just to beautify the code
130                 /// </summary>
131                 public void Error (int error, string s)
132                 {
133                         Report.Error (error, loc, s);
134                 }
135
136                 // Not nice but we have broken hierarchy.
137                 public virtual void CheckMarshalByRefAccess (EmitContext ec)
138                 {
139                 }
140
141                 public virtual bool GetAttributableValue (Type value_type, out object value)
142                 {
143                         Attribute.Error_AttributeArgumentNotValid (loc);
144                         value = null;
145                         return false;
146                 }
147
148                 public virtual string GetSignatureForError ()
149                 {
150                         return TypeManager.CSharpName (type);
151                 }
152
153                 public static bool IsAccessorAccessible (Type invocation_type, MethodInfo mi, out bool must_do_cs1540_check)
154                 {
155                         MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
156
157                         must_do_cs1540_check = false; // by default we do not check for this
158
159                         if (ma == MethodAttributes.Public)
160                                 return true;
161                         
162                         //
163                         // If only accessible to the current class or children
164                         //
165                         if (ma == MethodAttributes.Private)
166                                 return TypeManager.IsPrivateAccessible (invocation_type, mi.DeclaringType) ||
167                                         TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType);
168
169                         if (TypeManager.IsThisOrFriendAssembly (mi.DeclaringType.Assembly)) {
170                                 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem)
171                                         return true;
172                         } else {
173                                 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamANDAssem)
174                                         return false;
175                         }
176
177                         // Family and FamANDAssem require that we derive.
178                         // FamORAssem requires that we derive if in different assemblies.
179                         if (!TypeManager.IsNestedFamilyAccessible (invocation_type, mi.DeclaringType))
180                                 return false;
181
182                         if (!TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType))
183                                 must_do_cs1540_check = true;
184
185                         return true;
186                 }
187
188                 public virtual bool IsNull {
189                         get {
190                                 return false;
191                         }
192                 }
193
194                 /// <summary>
195                 ///   Performs semantic analysis on the Expression
196                 /// </summary>
197                 ///
198                 /// <remarks>
199                 ///   The Resolve method is invoked to perform the semantic analysis
200                 ///   on the node.
201                 ///
202                 ///   The return value is an expression (it can be the
203                 ///   same expression in some cases) or a new
204                 ///   expression that better represents this node.
205                 ///   
206                 ///   For example, optimizations of Unary (LiteralInt)
207                 ///   would return a new LiteralInt with a negated
208                 ///   value.
209                 ///   
210                 ///   If there is an error during semantic analysis,
211                 ///   then an error should be reported (using Report)
212                 ///   and a null value should be returned.
213                 ///   
214                 ///   There are two side effects expected from calling
215                 ///   Resolve(): the the field variable "eclass" should
216                 ///   be set to any value of the enumeration
217                 ///   `ExprClass' and the type variable should be set
218                 ///   to a valid type (this is the type of the
219                 ///   expression).
220                 /// </remarks>
221                 public abstract Expression DoResolve (EmitContext ec);
222
223                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
224                 {
225                         return null;
226                 }
227
228                 //
229                 // This is used if the expression should be resolved as a type or namespace name.
230                 // the default implementation fails.   
231                 //
232                 public virtual FullNamedExpression ResolveAsTypeStep (IResolveContext rc,  bool silent)
233                 {
234                         if (!silent) {
235                                 Expression e = this;
236                                 EmitContext ec = rc as EmitContext;
237                                 if (ec != null)
238                                         e = e.Resolve (ec);
239                                 if (e != null)
240                                         e.Error_UnexpectedKind (ResolveFlags.Type, loc);
241                         }
242                         return null;
243                 }
244
245                 //
246                 // C# 3.0 introduced contextual keywords (var) which behaves like a type if type with
247                 // same name exists or as a keyword when no type was found
248                 // 
249                 public virtual TypeExpr ResolveAsContextualType (IResolveContext rc, bool silent)
250                 {
251                         return ResolveAsTypeTerminal (rc, silent);
252                 }               
253                 
254                 //
255                 // This is used to resolve the expression as a type, a null
256                 // value will be returned if the expression is not a type
257                 // reference
258                 //
259                 public virtual TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
260                 {
261                         TypeExpr te = ResolveAsBaseTerminal (ec, silent);
262                         if (te == null)
263                                 return null;
264
265                         if (!silent) { // && !(te is TypeParameterExpr)) {
266                                 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (te.Type);
267                                 if (obsolete_attr != null && !ec.IsInObsoleteScope) {
268                                         AttributeTester.Report_ObsoleteMessage (obsolete_attr, te.GetSignatureForError (), Location);
269                                 }
270                         }
271
272                         // Constrains don't need to be checked for overrides
273                         GenericMethod gm = ec.GenericDeclContainer as GenericMethod;
274                         if (gm != null && (gm.ModFlags & Modifiers.OVERRIDE) != 0) {
275                                 te.loc = loc;
276                                 return te;
277                         }
278
279                         ConstructedType ct = te as ConstructedType;
280                         if ((ct != null) && !ct.CheckConstraints (ec))
281                                 return null;
282
283                         return te;
284                 }
285
286                 public TypeExpr ResolveAsBaseTerminal (IResolveContext ec, bool silent)
287                 {
288                         int errors = Report.Errors;
289
290                         FullNamedExpression fne = ResolveAsTypeStep (ec, silent);
291
292                         if (fne == null)
293                                 return null;
294
295                         if (fne.eclass != ExprClass.Type) {
296                                 if (!silent && errors == Report.Errors)
297                                         fne.Error_UnexpectedKind (null, "type", loc);
298                                 return null;
299                         }
300
301                         TypeExpr te = fne as TypeExpr;
302
303                         if (!te.CheckAccessLevel (ec.DeclContainer)) {
304                                 Report.SymbolRelatedToPreviousError (te.Type);
305                                 ErrorIsInaccesible (loc, TypeManager.CSharpName (te.Type));
306                                 return null;
307                         }
308
309                         te.loc = loc;
310                         return te;
311                 }
312
313                 public static void ErrorIsInaccesible (Location loc, string name)
314                 {
315                         Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", name);
316                 }
317
318                 protected static void Error_CannotAccessProtected (Location loc, MemberInfo m, Type qualifier, Type container)
319                 {
320                         Report.Error (1540, loc, "Cannot access protected member `{0}' via a qualifier of type `{1}'."
321                                 + " The qualifier must be of type `{2}' or derived from it", 
322                                 TypeManager.GetFullNameSignature (m),
323                                 TypeManager.CSharpName (qualifier),
324                                 TypeManager.CSharpName (container));
325
326                 }
327
328                 public static void Error_InvalidExpressionStatement (Location loc)
329                 {
330                         Report.Error (201, loc, "Only assignment, call, increment, decrement, and new object " +
331                                        "expressions can be used as a statement");
332                 }
333                 
334                 public void Error_InvalidExpressionStatement ()
335                 {
336                         Error_InvalidExpressionStatement (loc);
337                 }
338
339                 protected void Error_CannotAssign (string to, string roContext)
340                 {
341                         Report.Error (1656, loc, "Cannot assign to `{0}' because it is a `{1}'",
342                                 to, roContext);
343                 }
344
345                 public static void Error_VoidInvalidInTheContext (Location loc)
346                 {
347                         Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
348                 }
349
350                 public virtual void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
351                 {
352                         // The error was already reported as CS1660
353                         if (type == TypeManager.anonymous_method_type)
354                                 return;
355
356                         if (TypeManager.IsGenericParameter (Type) && TypeManager.IsGenericParameter (target) && type.Name == target.Name) {
357 #if GMCS_SOURCE
358                                 string sig1 = type.DeclaringMethod == null ?
359                                         TypeManager.CSharpName (type.DeclaringType) :
360                                         TypeManager.CSharpSignature (type.DeclaringMethod);
361                                 string sig2 = target.DeclaringMethod == null ?
362                                         TypeManager.CSharpName (target.DeclaringType) :
363                                         TypeManager.CSharpSignature (target.DeclaringMethod);
364                                 Report.ExtraInformation (loc,
365                                         String.Format (
366                                                 "The generic parameter `{0}' of `{1}' cannot be converted to the generic parameter `{0}' of `{2}' (in the previous ",
367                                                 Type.Name, sig1, sig2));
368 #endif
369                         } else if (Type.FullName == target.FullName){
370                                 Report.ExtraInformation (loc,
371                                         String.Format (
372                                         "The type `{0}' has two conflicting definitions, one comes from `{1}' and the other from `{2}' (in the previous ",
373                                         Type.FullName, Type.Assembly.FullName, target.Assembly.FullName));
374                         }
375
376                         if (expl) {
377                                 Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
378                                         TypeManager.CSharpName (type), TypeManager.CSharpName (target));
379                                 return;
380                         }
381                         
382                         Expression e = (this is EnumConstant) ? ((EnumConstant)this).Child : this;
383                         bool b = Convert.ExplicitNumericConversion (e, target) != null;
384
385                         if (b ||
386                             Convert.ExplicitReferenceConversionExists (Type, target) ||
387                             Convert.ExplicitUnsafe (e, target) != null ||
388                             (ec != null && Convert.ExplicitUserConversion (ec, this, target, Location.Null) != null))
389                         {
390                                 Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. " +
391                                               "An explicit conversion exists (are you missing a cast?)",
392                                         TypeManager.CSharpName (Type), TypeManager.CSharpName (target));
393                                 return;
394                         }
395
396                         if (Type != TypeManager.string_type && this is Constant && !(this is EmptyConstantCast)) {
397                                 Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
398                                         ((Constant)(this)).GetValue ().ToString (), TypeManager.CSharpName (target));
399                                 return;
400                         }
401
402                         Report.Error (29, loc, "Cannot implicitly convert type `{0}' to `{1}'",
403                                 TypeManager.CSharpName (type),
404                                 TypeManager.CSharpName (target));
405                 }
406
407                 protected void Error_VariableIsUsedBeforeItIsDeclared (string name)
408                 {
409                         Report.Error (841, loc, "The variable `{0}' cannot be used before it is declared",
410                                 name);
411                 }
412
413                 protected virtual void Error_TypeDoesNotContainDefinition (Type type, string name)
414                 {
415                         Error_TypeDoesNotContainDefinition (loc, type, name);
416                 }
417
418                 public static void Error_TypeDoesNotContainDefinition (Location loc, Type type, string name)
419                 {
420                         Report.SymbolRelatedToPreviousError (type);
421                         Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
422                                 TypeManager.CSharpName (type), name);
423                 }
424
425                 protected static void Error_ValueAssignment (Location loc)
426                 {
427                         Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
428                 }
429
430                 ResolveFlags ExprClassToResolveFlags
431                 {
432                         get {
433                                 switch (eclass) {
434                                         case ExprClass.Type:
435                                         case ExprClass.Namespace:
436                                                 return ResolveFlags.Type;
437
438                                         case ExprClass.MethodGroup:
439                                                 return ResolveFlags.MethodGroup;
440
441                                         case ExprClass.Value:
442                                         case ExprClass.Variable:
443                                         case ExprClass.PropertyAccess:
444                                         case ExprClass.EventAccess:
445                                         case ExprClass.IndexerAccess:
446                                                 return ResolveFlags.VariableOrValue;
447
448                                         default:
449                                                 throw new InternalErrorException (loc.ToString () + " " +  GetType () + " ExprClass is Invalid after resolve");
450                                 }
451                         }
452                 }
453                
454                 /// <summary>
455                 ///   Resolves an expression and performs semantic analysis on it.
456                 /// </summary>
457                 ///
458                 /// <remarks>
459                 ///   Currently Resolve wraps DoResolve to perform sanity
460                 ///   checking and assertion checking on what we expect from Resolve.
461                 /// </remarks>
462                 public Expression Resolve (EmitContext ec, ResolveFlags flags)
463                 {
464                         if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) 
465                                 return ResolveAsTypeStep (ec, false);
466
467                         bool do_flow_analysis = ec.DoFlowAnalysis;
468                         bool omit_struct_analysis = ec.OmitStructFlowAnalysis;
469                         if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
470                                 do_flow_analysis = false;
471                         if ((flags & ResolveFlags.DisableStructFlowAnalysis) != 0)
472                                 omit_struct_analysis = true;
473
474                         Expression e;
475                         using (ec.WithFlowAnalysis (do_flow_analysis, omit_struct_analysis)) {
476                                 if (this is SimpleName) {
477                                         bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
478                                         e = ((SimpleName) this).DoResolve (ec, intermediate);
479                                 } else {
480                                         e = DoResolve (ec);
481                                 }
482                         }
483
484                         if (e == null)
485                                 return null;
486
487                         if ((flags & e.ExprClassToResolveFlags) == 0) {
488                                 e.Error_UnexpectedKind (flags, loc);
489                                 return null;
490                         }
491
492                         if (e.type == null && !(e is Namespace)) {
493                                 throw new Exception (
494                                         "Expression " + e.GetType () +
495                                         " did not set its type after Resolve\n" +
496                                         "called from: " + this.GetType ());
497                         }
498
499                         return e;
500                 }
501
502                 /// <summary>
503                 ///   Resolves an expression and performs semantic analysis on it.
504                 /// </summary>
505                 public Expression Resolve (EmitContext ec)
506                 {
507                         Expression e = Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
508
509                         if (e != null && e.eclass == ExprClass.MethodGroup && RootContext.Version == LanguageVersion.ISO_1) {
510                                 ((MethodGroupExpr) e).ReportUsageError ();
511                                 return null;
512                         }
513                         return e;
514                 }
515
516                 public Constant ResolveAsConstant (EmitContext ec, MemberCore mc)
517                 {
518                         Expression e = Resolve (ec);
519                         if (e == null)
520                                 return null;
521
522                         Constant c = e as Constant;
523                         if (c != null)
524                                 return c;
525
526                         Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError ());
527                         return null;
528                 }
529
530                 /// <summary>
531                 ///   Resolves an expression for LValue assignment
532                 /// </summary>
533                 ///
534                 /// <remarks>
535                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
536                 ///   checking and assertion checking on what we expect from Resolve
537                 /// </remarks>
538                 public Expression ResolveLValue (EmitContext ec, Expression right_side, Location loc)
539                 {
540                         int errors = Report.Errors;
541                         bool out_access = right_side == EmptyExpression.OutAccess;
542
543                         Expression e = DoResolveLValue (ec, right_side);
544
545                         if (e != null && out_access && !(e is IMemoryLocation)) {
546                                 // FIXME: There's no problem with correctness, the 'Expr = null' handles that.
547                                 //        Enabling this 'throw' will "only" result in deleting useless code elsewhere,
548
549                                 //throw new InternalErrorException ("ResolveLValue didn't return an IMemoryLocation: " +
550                                 //                                e.GetType () + " " + e.GetSignatureForError ());
551                                 e = null;
552                         }
553
554                         if (e == null) {
555                                 if (errors == Report.Errors) {
556                                         if (out_access)
557                                                 Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
558                                         else
559                                                 Error_ValueAssignment (loc);
560                                 }
561                                 return null;
562                         }
563
564                         if (e.eclass == ExprClass.Invalid)
565                                 throw new Exception ("Expression " + e + " ExprClass is Invalid after resolve");
566
567                         if (e.eclass == ExprClass.MethodGroup) {
568                                 ((MethodGroupExpr) e).ReportUsageError ();
569                                 return null;
570                         }
571
572                         if ((e.type == null) && !(e is ConstructedType))
573                                 throw new Exception ("Expression " + e + " did not set its type after Resolve");
574
575                         return e;
576                 }
577
578                 /// <summary>
579                 ///   Emits the code for the expression
580                 /// </summary>
581                 ///
582                 /// <remarks>
583                 ///   The Emit method is invoked to generate the code
584                 ///   for the expression.  
585                 /// </remarks>
586                 public abstract void Emit (EmitContext ec);
587
588                 public virtual void EmitBranchable (EmitContext ec, Label target, bool on_true)
589                 {
590                         Emit (ec);
591                         ec.ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
592                 }
593
594                 public virtual void EmitSideEffect (EmitContext ec)
595                 {
596                         Emit (ec);
597                         ec.ig.Emit (OpCodes.Pop);
598                 }
599
600                 /// <summary>
601                 ///   Protected constructor.  Only derivate types should
602                 ///   be able to be created
603                 /// </summary>
604
605                 protected Expression ()
606                 {
607                         eclass = ExprClass.Invalid;
608                         type = null;
609                 }
610
611                 /// <summary>
612                 ///   Returns a fully formed expression after a MemberLookup
613                 /// </summary>
614                 /// 
615                 public static Expression ExprClassFromMemberInfo (Type container_type, MemberInfo mi, Location loc)
616                 {
617                         if (mi is EventInfo)
618                                 return new EventExpr ((EventInfo) mi, loc);
619                         else if (mi is FieldInfo) {
620                                 FieldInfo fi = (FieldInfo) mi;
621                                 if (fi.IsLiteral || (fi.IsInitOnly && fi.FieldType == TypeManager.decimal_type))
622                                         return new ConstantExpr (fi, loc);
623                                 return new FieldExpr (fi, loc);
624                         } else if (mi is PropertyInfo)
625                                 return new PropertyExpr (container_type, (PropertyInfo) mi, loc);
626                         else if (mi is Type) {
627                                 return new TypeExpression ((System.Type) mi, loc);
628                         }
629
630                         return null;
631                 }
632
633                 protected static ArrayList almost_matched_members = new ArrayList (4);
634
635                 //
636                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
637                 //
638                 // This code could use some optimizations, but we need to do some
639                 // measurements.  For example, we could use a delegate to `flag' when
640                 // something can not any longer be a method-group (because it is something
641                 // else).
642                 //
643                 // Return values:
644                 //     If the return value is an Array, then it is an array of
645                 //     MethodBases
646                 //   
647                 //     If the return value is an MemberInfo, it is anything, but a Method
648                 //
649                 //     null on error.
650                 //
651                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
652                 // the arguments here and have MemberLookup return only the methods that
653                 // match the argument count/type, unlike we are doing now (we delay this
654                 // decision).
655                 //
656                 // This is so we can catch correctly attempts to invoke instance methods
657                 // from a static body (scan for error 120 in ResolveSimpleName).
658                 //
659                 //
660                 // FIXME: Potential optimization, have a static ArrayList
661                 //
662
663                 public static Expression MemberLookup (Type container_type, Type queried_type, string name,
664                                                        MemberTypes mt, BindingFlags bf, Location loc)
665                 {
666                         return MemberLookup (container_type, null, queried_type, name, mt, bf, loc);
667                 }
668
669                 //
670                 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
671                 // `qualifier_type' or null to lookup members in the current class.
672                 //
673
674                 public static Expression MemberLookup (Type container_type,
675                                                        Type qualifier_type, Type queried_type,
676                                                        string name, MemberTypes mt,
677                                                        BindingFlags bf, Location loc)
678                 {
679                         almost_matched_members.Clear ();
680
681                         MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
682                                                                      queried_type, mt, bf, name, almost_matched_members);
683
684                         if (mi == null)
685                                 return null;
686
687                         if (mi.Length > 1) {
688                                 bool is_interface = qualifier_type != null && qualifier_type.IsInterface;
689                                 ArrayList methods = new ArrayList (2);
690                                 ArrayList non_methods = null;
691
692                                 foreach (MemberInfo m in mi) {
693                                         if (m is MethodBase) {
694                                                 methods.Add (m);
695                                                 continue;
696                                         }
697
698                                         if (non_methods == null) {
699                                                 non_methods = new ArrayList (2);
700                                                 non_methods.Add (m);
701                                                 continue;
702                                         }
703
704                                         foreach (MemberInfo n_m in non_methods) {
705                                                 if (m.DeclaringType.IsInterface && TypeManager.ImplementsInterface (m.DeclaringType, n_m.DeclaringType))
706                                                         continue;
707
708                                                 Report.SymbolRelatedToPreviousError (m);
709                                                 Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
710                                                         TypeManager.GetFullNameSignature (m), TypeManager.GetFullNameSignature (n_m));
711                                                 return null;
712                                         }
713                                 }
714
715                                 if (methods.Count == 0)
716                                         return ExprClassFromMemberInfo (container_type, (MemberInfo)non_methods [0], loc);
717
718                                 if (non_methods != null) {
719                                         MethodBase method = (MethodBase) methods [0];
720                                         MemberInfo non_method = (MemberInfo) non_methods [0];
721                                         if (method.DeclaringType == non_method.DeclaringType) {
722                                                 // Cannot happen with C# code, but is valid in IL
723                                                 Report.SymbolRelatedToPreviousError (method);
724                                                 Report.SymbolRelatedToPreviousError (non_method);
725                                                 Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
726                                                               TypeManager.GetFullNameSignature (non_method),
727                                                               TypeManager.CSharpSignature (method));
728                                                 return null;
729                                         }
730
731                                         if (is_interface) {
732                                                 Report.SymbolRelatedToPreviousError (method);
733                                                 Report.SymbolRelatedToPreviousError (non_method);
734                                                 Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and non-method `{1}'. Using method `{0}'",
735                                                                 TypeManager.CSharpSignature (method), TypeManager.GetFullNameSignature (non_method));
736                                         }
737                                 }
738
739                                 return new MethodGroupExpr (methods, queried_type, loc);
740                         }
741
742                         if (mi [0] is MethodBase)
743                                 return new MethodGroupExpr (mi, queried_type, loc);
744
745                         return ExprClassFromMemberInfo (container_type, mi [0], loc);
746                 }
747
748                 public const MemberTypes AllMemberTypes =
749                         MemberTypes.Constructor |
750                         MemberTypes.Event       |
751                         MemberTypes.Field       |
752                         MemberTypes.Method      |
753                         MemberTypes.NestedType  |
754                         MemberTypes.Property;
755                 
756                 public const BindingFlags AllBindingFlags =
757                         BindingFlags.Public |
758                         BindingFlags.Static |
759                         BindingFlags.Instance;
760
761                 public static Expression MemberLookup (Type container_type, Type queried_type,
762                                                        string name, Location loc)
763                 {
764                         return MemberLookup (container_type, null, queried_type, name,
765                                              AllMemberTypes, AllBindingFlags, loc);
766                 }
767
768                 public static Expression MemberLookup (Type container_type, Type qualifier_type,
769                                                        Type queried_type, string name, Location loc)
770                 {
771                         return MemberLookup (container_type, qualifier_type, queried_type,
772                                              name, AllMemberTypes, AllBindingFlags, loc);
773                 }
774
775                 public static MethodGroupExpr MethodLookup (Type container_type, Type queried_type,
776                                                        string name, Location loc)
777                 {
778                         return (MethodGroupExpr)MemberLookup (container_type, null, queried_type, name,
779                                              MemberTypes.Method, AllBindingFlags, loc);
780                 }
781
782                 /// <summary>
783                 ///   This is a wrapper for MemberLookup that is not used to "probe", but
784                 ///   to find a final definition.  If the final definition is not found, we
785                 ///   look for private members and display a useful debugging message if we
786                 ///   find it.
787                 /// </summary>
788                 protected Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
789                                                             Type queried_type, string name,
790                                                             MemberTypes mt, BindingFlags bf,
791                                                             Location loc)
792                 {
793                         Expression e;
794
795                         int errors = Report.Errors;
796
797                         e = MemberLookup (ec.ContainerType, qualifier_type, queried_type, name, mt, bf, loc);
798
799                         if (e != null || errors != Report.Errors)
800                                 return e;
801
802                         // No errors were reported by MemberLookup, but there was an error.
803                         return Error_MemberLookupFailed (ec.ContainerType, qualifier_type, queried_type,
804                                         name, null, mt, bf);
805                 }
806
807                 protected virtual Expression Error_MemberLookupFailed (Type container_type, Type qualifier_type,
808                                                        Type queried_type, string name, string class_name,
809                                                            MemberTypes mt, BindingFlags bf)
810                 {
811                         if (almost_matched_members.Count != 0) {
812                                 for (int i = 0; i < almost_matched_members.Count; ++i) {
813                                         MemberInfo m = (MemberInfo) almost_matched_members [i];
814                                         for (int j = 0; j < i; ++j) {
815                                                 if (m == almost_matched_members [j]) {
816                                                         m = null;
817                                                         break;
818                                                 }
819                                         }
820                                         if (m == null)
821                                                 continue;
822                                         
823                                         Type declaring_type = m.DeclaringType;
824                                         
825                                         Report.SymbolRelatedToPreviousError (m);
826                                         if (qualifier_type == null) {
827                                                 Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
828                                                               TypeManager.CSharpName (m.DeclaringType),
829                                                               TypeManager.CSharpName (container_type));
830                                                 
831                                         } else if (qualifier_type != container_type &&
832                                                    TypeManager.IsNestedFamilyAccessible (container_type, declaring_type)) {
833                                                 // Although a derived class can access protected members of
834                                                 // its base class it cannot do so through an instance of the
835                                                 // base class (CS1540).  If the qualifier_type is a base of the
836                                                 // ec.ContainerType and the lookup succeeds with the latter one,
837                                                 // then we are in this situation.
838                                                 Error_CannotAccessProtected (loc, m, qualifier_type, container_type);
839                                         } else {
840                                                 ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m));
841                                         }
842                                 }
843                                 almost_matched_members.Clear ();
844                                 return null;
845                         }
846
847                         MemberInfo[] lookup = null;
848                         if (queried_type == null) {
849                                 class_name = "global::";
850                         } else {
851                                 lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
852                                         mt, (bf & ~BindingFlags.Public) | BindingFlags.NonPublic,
853                                         name, null);
854
855                                 if (lookup != null) {
856                                         Report.SymbolRelatedToPreviousError (lookup [0]);
857                                         ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (lookup [0]));
858                                         return Error_MemberLookupFailed (lookup);
859                                 }
860
861                                 lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
862                                         AllMemberTypes, AllBindingFlags | BindingFlags.NonPublic,
863                                         name, null);
864                         }
865
866                         if (lookup == null) {
867                                 if (class_name != null) {
868                                         Report.Error (103, loc, "The name `{0}' does not exist in the current context",
869                                                 name);
870                                 } else {
871                                         Error_TypeDoesNotContainDefinition (queried_type, name);
872                                 }
873                                 return null;
874                         }
875
876                         if (TypeManager.MemberLookup (queried_type, null, queried_type,
877                                                       AllMemberTypes, AllBindingFlags |
878                                                       BindingFlags.NonPublic, name, null) == null) {
879                                 if ((lookup.Length == 1) && (lookup [0] is Type)) {
880                                         Type t = (Type) lookup [0];
881
882                                         Report.Error (305, loc,
883                                                       "Using the generic type `{0}' " +
884                                                       "requires {1} type arguments",
885                                                       TypeManager.CSharpName (t),
886                                                       TypeManager.GetNumberOfTypeArguments (t).ToString ());
887                                         return null;
888                                 }
889                         }
890
891                         return Error_MemberLookupFailed (lookup);
892                 }
893
894                 protected virtual Expression Error_MemberLookupFailed (MemberInfo[] members)
895                 {
896                         for (int i = 0; i < members.Length; ++i) {
897                                 if (!(members [i] is MethodBase))
898                                         return null;
899                         }
900
901                         // By default propagate the closest candidates upwards
902                         return new MethodGroupExpr (members, type, loc);
903                 }
904
905                 protected virtual void Error_NegativeArrayIndex (Location loc)
906                 {
907                         throw new NotImplementedException ();
908                 }
909
910                 /// <summary>
911                 ///   Returns an expression that can be used to invoke operator true
912                 ///   on the expression if it exists.
913                 /// </summary>
914                 static public Expression GetOperatorTrue (EmitContext ec, Expression e, Location loc)
915                 {
916                         return GetOperatorTrueOrFalse (ec, e, true, loc);
917                 }
918
919                 /// <summary>
920                 ///   Returns an expression that can be used to invoke operator false
921                 ///   on the expression if it exists.
922                 /// </summary>
923                 static public Expression GetOperatorFalse (EmitContext ec, Expression e, Location loc)
924                 {
925                         return GetOperatorTrueOrFalse (ec, e, false, loc);
926                 }
927
928                 static Expression GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
929                 {
930                         MethodGroupExpr operator_group;
931                         operator_group = MethodLookup (ec.ContainerType, e.Type, is_true ? "op_True" : "op_False", loc) as MethodGroupExpr;
932                         if (operator_group == null)
933                                 return null;
934
935                         ArrayList arguments = new ArrayList (1);
936                         arguments.Add (new Argument (e, Argument.AType.Expression));
937                         operator_group = operator_group.OverloadResolve (
938                                 ec, ref arguments, false, loc);
939
940                         if (operator_group == null)
941                                 return null;
942
943                         return new UserOperatorCall (operator_group, arguments, null, loc);
944                 }
945
946                 /// <summary>
947                 ///   Resolves the expression `e' into a boolean expression: either through
948                 ///   an implicit conversion, or through an `operator true' invocation
949                 /// </summary>
950                 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
951                 {
952                         e = e.Resolve (ec);
953                         if (e == null)
954                                 return null;
955
956                         if (e.Type == TypeManager.bool_type)
957                                 return e;
958
959                         Expression converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, Location.Null);
960
961                         if (converted != null)
962                                 return converted;
963
964                         //
965                         // If no implicit conversion to bool exists, try using `operator true'
966                         //
967                         converted = Expression.GetOperatorTrue (ec, e, loc);
968                         if (converted == null){
969                                 e.Error_ValueCannotBeConverted (ec, loc, TypeManager.bool_type, false);
970                                 return null;
971                         }
972                         return converted;
973                 }
974                 
975                 public virtual string ExprClassName
976                 {
977                         get {
978                                 switch (eclass){
979                                         case ExprClass.Invalid:
980                                                 return "Invalid";
981                                         case ExprClass.Value:
982                                                 return "value";
983                                         case ExprClass.Variable:
984                                                 return "variable";
985                                         case ExprClass.Namespace:
986                                                 return "namespace";
987                                         case ExprClass.Type:
988                                                 return "type";
989                                         case ExprClass.MethodGroup:
990                                                 return "method group";
991                                         case ExprClass.PropertyAccess:
992                                                 return "property access";
993                                         case ExprClass.EventAccess:
994                                                 return "event access";
995                                         case ExprClass.IndexerAccess:
996                                                 return "indexer access";
997                                         case ExprClass.Nothing:
998                                                 return "null";
999                                 }
1000                                 throw new Exception ("Should not happen");
1001                         }
1002                 }
1003                 
1004                 /// <summary>
1005                 ///   Reports that we were expecting `expr' to be of class `expected'
1006                 /// </summary>
1007                 public void Error_UnexpectedKind (DeclSpace ds, string expected, Location loc)
1008                 {
1009                         Error_UnexpectedKind (ds, expected, ExprClassName, loc);
1010                 }
1011
1012                 public void Error_UnexpectedKind (DeclSpace ds, string expected, string was, Location loc)
1013                 {
1014                         string name = GetSignatureForError ();
1015                         if (ds != null)
1016                                 name = ds.GetSignatureForError () + '.' + name;
1017
1018                         Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected",
1019                               name, was, expected);
1020                 }
1021
1022                 public void Error_UnexpectedKind (ResolveFlags flags, Location loc)
1023                 {
1024                         string [] valid = new string [4];
1025                         int count = 0;
1026
1027                         if ((flags & ResolveFlags.VariableOrValue) != 0) {
1028                                 valid [count++] = "variable";
1029                                 valid [count++] = "value";
1030                         }
1031
1032                         if ((flags & ResolveFlags.Type) != 0)
1033                                 valid [count++] = "type";
1034
1035                         if ((flags & ResolveFlags.MethodGroup) != 0)
1036                                 valid [count++] = "method group";
1037
1038                         if (count == 0)
1039                                 valid [count++] = "unknown";
1040
1041                         StringBuilder sb = new StringBuilder (valid [0]);
1042                         for (int i = 1; i < count - 1; i++) {
1043                                 sb.Append ("', `");
1044                                 sb.Append (valid [i]);
1045                         }
1046                         if (count > 1) {
1047                                 sb.Append ("' or `");
1048                                 sb.Append (valid [count - 1]);
1049                         }
1050
1051                         Report.Error (119, loc, 
1052                                 "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName, sb.ToString ());
1053                 }
1054                 
1055                 public static void UnsafeError (Location loc)
1056                 {
1057                         Report.Error (214, loc, "Pointers and fixed size buffers may only be used in an unsafe context");
1058                 }
1059
1060                 //
1061                 // Load the object from the pointer.  
1062                 //
1063                 public static void LoadFromPtr (ILGenerator ig, Type t)
1064                 {
1065                         if (t == TypeManager.int32_type)
1066                                 ig.Emit (OpCodes.Ldind_I4);
1067                         else if (t == TypeManager.uint32_type)
1068                                 ig.Emit (OpCodes.Ldind_U4);
1069                         else if (t == TypeManager.short_type)
1070                                 ig.Emit (OpCodes.Ldind_I2);
1071                         else if (t == TypeManager.ushort_type)
1072                                 ig.Emit (OpCodes.Ldind_U2);
1073                         else if (t == TypeManager.char_type)
1074                                 ig.Emit (OpCodes.Ldind_U2);
1075                         else if (t == TypeManager.byte_type)
1076                                 ig.Emit (OpCodes.Ldind_U1);
1077                         else if (t == TypeManager.sbyte_type)
1078                                 ig.Emit (OpCodes.Ldind_I1);
1079                         else if (t == TypeManager.uint64_type)
1080                                 ig.Emit (OpCodes.Ldind_I8);
1081                         else if (t == TypeManager.int64_type)
1082                                 ig.Emit (OpCodes.Ldind_I8);
1083                         else if (t == TypeManager.float_type)
1084                                 ig.Emit (OpCodes.Ldind_R4);
1085                         else if (t == TypeManager.double_type)
1086                                 ig.Emit (OpCodes.Ldind_R8);
1087                         else if (t == TypeManager.bool_type)
1088                                 ig.Emit (OpCodes.Ldind_I1);
1089                         else if (t == TypeManager.intptr_type)
1090                                 ig.Emit (OpCodes.Ldind_I);
1091                         else if (TypeManager.IsEnumType (t)) {
1092                                 if (t == TypeManager.enum_type)
1093                                         ig.Emit (OpCodes.Ldind_Ref);
1094                                 else
1095                                         LoadFromPtr (ig, TypeManager.GetEnumUnderlyingType (t));
1096                         } else if (t.IsValueType || TypeManager.IsGenericParameter (t))
1097                                 ig.Emit (OpCodes.Ldobj, t);
1098                         else if (t.IsPointer)
1099                                 ig.Emit (OpCodes.Ldind_I);
1100                         else
1101                                 ig.Emit (OpCodes.Ldind_Ref);
1102                 }
1103
1104                 //
1105                 // The stack contains the pointer and the value of type `type'
1106                 //
1107                 public static void StoreFromPtr (ILGenerator ig, Type type)
1108                 {
1109                         if (TypeManager.IsEnumType (type))
1110                                 type = TypeManager.GetEnumUnderlyingType (type);
1111                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1112                                 ig.Emit (OpCodes.Stind_I4);
1113                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1114                                 ig.Emit (OpCodes.Stind_I8);
1115                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1116                                  type == TypeManager.ushort_type)
1117                                 ig.Emit (OpCodes.Stind_I2);
1118                         else if (type == TypeManager.float_type)
1119                                 ig.Emit (OpCodes.Stind_R4);
1120                         else if (type == TypeManager.double_type)
1121                                 ig.Emit (OpCodes.Stind_R8);
1122                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1123                                  type == TypeManager.bool_type)
1124                                 ig.Emit (OpCodes.Stind_I1);
1125                         else if (type == TypeManager.intptr_type)
1126                                 ig.Emit (OpCodes.Stind_I);
1127                         else if (type.IsValueType || TypeManager.IsGenericParameter (type))
1128                                 ig.Emit (OpCodes.Stobj, type);
1129                         else
1130                                 ig.Emit (OpCodes.Stind_Ref);
1131                 }
1132                 
1133                 //
1134                 // Returns the size of type `t' if known, otherwise, 0
1135                 //
1136                 public static int GetTypeSize (Type t)
1137                 {
1138                         t = TypeManager.TypeToCoreType (t);
1139                         if (t == TypeManager.int32_type ||
1140                             t == TypeManager.uint32_type ||
1141                             t == TypeManager.float_type)
1142                                 return 4;
1143                         else if (t == TypeManager.int64_type ||
1144                                  t == TypeManager.uint64_type ||
1145                                  t == TypeManager.double_type)
1146                                 return 8;
1147                         else if (t == TypeManager.byte_type ||
1148                                  t == TypeManager.sbyte_type ||
1149                                  t == TypeManager.bool_type)    
1150                                 return 1;
1151                         else if (t == TypeManager.short_type ||
1152                                  t == TypeManager.char_type ||
1153                                  t == TypeManager.ushort_type)
1154                                 return 2;
1155                         else if (t == TypeManager.decimal_type)
1156                                 return 16;
1157                         else
1158                                 return 0;
1159                 }
1160
1161                 protected void Error_CannotCallAbstractBase (string name)
1162                 {
1163                         Report.Error (205, loc, "Cannot call an abstract base member `{0}'", name);
1164                 }
1165                 
1166                 protected void Error_CannotModifyIntermediateExpressionValue (EmitContext ec)
1167                 {
1168                         Report.SymbolRelatedToPreviousError (type);
1169                         if (ec.CurrentInitializerVariable != null) {
1170                                 Report.Error (1918, loc, "Members of a value type property `{0}' cannot be assigned with an object initializer",
1171                                         GetSignatureForError ());
1172                         } else {
1173                                 Report.Error (1612, loc, "Cannot modify a value type return value of `{0}'. Consider storing the value in a temporary variable",
1174                                         GetSignatureForError ());
1175                         }
1176                 }
1177
1178                 //
1179                 // Converts `source' to an int, uint, long or ulong.
1180                 //
1181                 public Expression ConvertExpressionToArrayIndex (EmitContext ec, Expression source)
1182                 {
1183                         Expression converted;
1184                         
1185                         using (ec.With (EmitContext.Flags.CheckState, true)) {
1186                                 converted = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, source.loc);
1187                                 if (converted == null)
1188                                         converted = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, source.loc);
1189                                 if (converted == null)
1190                                         converted = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, source.loc);
1191                                 if (converted == null)
1192                                         converted = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, source.loc);
1193
1194                                 if (converted == null) {
1195                                         source.Error_ValueCannotBeConverted (ec, source.loc, TypeManager.int32_type, false);
1196                                         return null;
1197                                 }
1198                         }
1199
1200                         //
1201                         // Only positive constants are allowed at compile time
1202                         //
1203                         Constant c = converted as Constant;
1204                         if (c != null) {
1205                                 if (c.IsNegative) {
1206                                         Error_NegativeArrayIndex (source.loc);
1207                                 }
1208                                 return c;
1209                         }
1210
1211                         return new ArrayIndexCast (converted).Resolve (ec);
1212                 }
1213
1214                 //
1215                 // Derived classes implement this method by cloning the fields that
1216                 // could become altered during the Resolve stage
1217                 //
1218                 // Only expressions that are created for the parser need to implement
1219                 // this.
1220                 //
1221                 protected virtual void CloneTo (CloneContext clonectx, Expression target)
1222                 {
1223                         throw new NotImplementedException (
1224                                 String.Format (
1225                                         "CloneTo not implemented for expression {0}", this.GetType ()));
1226                 }
1227
1228                 //
1229                 // Clones an expression created by the parser.
1230                 //
1231                 // We only support expressions created by the parser so far, not
1232                 // expressions that have been resolved (many more classes would need
1233                 // to implement CloneTo).
1234                 //
1235                 // This infrastructure is here merely for Lambda expressions which
1236                 // compile the same code using different type values for the same
1237                 // arguments to find the correct overload
1238                 //
1239                 public Expression Clone (CloneContext clonectx)
1240                 {
1241                         Expression cloned = (Expression) MemberwiseClone ();
1242                         CloneTo (clonectx, cloned);
1243
1244                         return cloned;
1245                 }
1246
1247                 public virtual Expression CreateExpressionTree (EmitContext ec)
1248                 {
1249                         throw new NotImplementedException (
1250                                 "Expression tree conversion not implemented for " + GetType ());
1251                 }
1252
1253                 protected Expression CreateExpressionFactoryCall (string name, ArrayList args)
1254                 {
1255                         return CreateExpressionFactoryCall (name, null, args, loc);
1256                 }
1257
1258                 protected Expression CreateExpressionFactoryCall (string name, TypeArguments typeArguments, ArrayList args)
1259                 {
1260                         return CreateExpressionFactoryCall (name, typeArguments, args, loc);
1261                 }
1262
1263                 public static Expression CreateExpressionFactoryCall (string name, TypeArguments typeArguments, ArrayList args, Location loc)
1264                 {
1265                         return new Invocation (new MemberAccess (CreateExpressionTypeExpression (loc), name, typeArguments, loc), args);
1266                 }
1267
1268                 protected static TypeExpr CreateExpressionTypeExpression (Location loc)
1269                 {
1270                         TypeExpr texpr = TypeManager.expression_type_expr;
1271                         if (texpr == null) {
1272                                 Type t = TypeManager.CoreLookupType ("System.Linq.Expressions", "Expression", Kind.Class, true);
1273                                 if (t == null)
1274                                         return null;
1275
1276                                 TypeManager.expression_type_expr = texpr = new TypeExpression (t, Location.Null);
1277                         }
1278
1279                         return texpr;
1280                 }
1281         }
1282
1283         /// <summary>
1284         ///   This is just a base class for expressions that can
1285         ///   appear on statements (invocations, object creation,
1286         ///   assignments, post/pre increment and decrement).  The idea
1287         ///   being that they would support an extra Emition interface that
1288         ///   does not leave a result on the stack.
1289         /// </summary>
1290         public abstract class ExpressionStatement : Expression {
1291
1292                 public virtual ExpressionStatement ResolveStatement (EmitContext ec)
1293                 {
1294                         Expression e = Resolve (ec);
1295                         if (e == null)
1296                                 return null;
1297
1298                         ExpressionStatement es = e as ExpressionStatement;
1299                         if (es == null)
1300                                 Error_InvalidExpressionStatement ();
1301
1302                         return es;
1303                 }
1304
1305                 /// <summary>
1306                 ///   Requests the expression to be emitted in a `statement'
1307                 ///   context.  This means that no new value is left on the
1308                 ///   stack after invoking this method (constrasted with
1309                 ///   Emit that will always leave a value on the stack).
1310                 /// </summary>
1311                 public abstract void EmitStatement (EmitContext ec);
1312
1313                 public override void EmitSideEffect (EmitContext ec)
1314                 {
1315                         EmitStatement (ec);
1316                 }
1317         }
1318
1319         /// <summary>
1320         ///   This kind of cast is used to encapsulate the child
1321         ///   whose type is child.Type into an expression that is
1322         ///   reported to return "return_type".  This is used to encapsulate
1323         ///   expressions which have compatible types, but need to be dealt
1324         ///   at higher levels with.
1325         ///
1326         ///   For example, a "byte" expression could be encapsulated in one
1327         ///   of these as an "unsigned int".  The type for the expression
1328         ///   would be "unsigned int".
1329         ///
1330         /// </summary>
1331         public abstract class TypeCast : Expression
1332         {
1333                 protected Expression child;
1334
1335                 protected TypeCast (Expression child, Type return_type)
1336                 {
1337                         eclass = child.eclass;
1338                         loc = child.Location;
1339                         type = return_type;
1340                         this.child = child;
1341                 }
1342
1343                 public override Expression CreateExpressionTree (EmitContext ec)
1344                 {
1345                         ArrayList args = new ArrayList (2);
1346                         args.Add (new Argument (child.CreateExpressionTree (ec)));
1347                         args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
1348                         return CreateExpressionFactoryCall (ec.CheckState ? "ConvertChecked" : "Convert", args);
1349                 }
1350
1351                 public override Expression DoResolve (EmitContext ec)
1352                 {
1353                         // This should never be invoked, we are born in fully
1354                         // initialized state.
1355
1356                         return this;
1357                 }
1358
1359                 public override void Emit (EmitContext ec)
1360                 {
1361                         child.Emit (ec);
1362                 }
1363
1364                 public override bool GetAttributableValue (Type value_type, out object value)
1365                 {
1366                         return child.GetAttributableValue (value_type, out value);
1367                 }
1368
1369                 protected override void CloneTo (CloneContext clonectx, Expression t)
1370                 {
1371                         TypeCast target = (TypeCast) t;
1372
1373                         target.child = child.Clone (clonectx);
1374                 }
1375         }
1376
1377         public class EmptyCast : TypeCast {
1378                 EmptyCast (Expression child, Type target_type)
1379                         : base (child, target_type)
1380                 {
1381                 }
1382                 
1383                 public static Expression Create (Expression child, Type type)
1384                 {
1385                         Constant c = child as Constant;
1386                         if (c != null)
1387                                 return new EmptyConstantCast (c, type);
1388
1389                         return new EmptyCast (child, type);
1390                 }
1391
1392                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1393                 {
1394                         child.EmitBranchable (ec, label, on_true);
1395                 }
1396
1397                 public override void EmitSideEffect (EmitContext ec)
1398                 {
1399                         child.EmitSideEffect (ec);
1400                 }
1401
1402         }
1403
1404         /// <summary>
1405         ///    Performs a cast using an operator (op_Explicit or op_Implicit)
1406         /// </summary>
1407         public class OperatorCast : TypeCast {
1408                 MethodInfo conversion_operator;
1409                 bool find_explicit;
1410                         
1411                 public OperatorCast (Expression child, Type target_type) : this (child, target_type, false) {}
1412
1413                 public OperatorCast (Expression child, Type target_type, bool find_explicit)
1414                         : base (child, target_type)
1415                 {
1416                         this.find_explicit = find_explicit;
1417                 }
1418
1419                 // Returns the implicit operator that converts from
1420                 // 'child.Type' to our target type (type)
1421                 MethodInfo GetConversionOperator (bool find_explicit)
1422                 {
1423                         string operator_name = find_explicit ? "op_Explicit" : "op_Implicit";
1424
1425                         MemberInfo [] mi;
1426
1427                         mi = TypeManager.MemberLookup (child.Type, child.Type, child.Type, MemberTypes.Method,
1428                                 BindingFlags.Static | BindingFlags.Public, operator_name, null);
1429
1430                         if (mi == null){
1431                                 mi = TypeManager.MemberLookup (type, type, type, MemberTypes.Method,
1432                                                                BindingFlags.Static | BindingFlags.Public, operator_name, null);
1433                         }
1434                         
1435                         foreach (MethodInfo oper in mi) {
1436                                 ParameterData pd = TypeManager.GetParameterData (oper);
1437
1438                                 if (pd.ParameterType (0) == child.Type && TypeManager.TypeToCoreType (oper.ReturnType) == type)
1439                                         return oper;
1440                         }
1441
1442                         return null;
1443
1444
1445                 }
1446                 public override void Emit (EmitContext ec)
1447                 {
1448                         ILGenerator ig = ec.ig;
1449
1450                         child.Emit (ec);
1451                         conversion_operator = GetConversionOperator (find_explicit);
1452
1453                         if (conversion_operator == null)
1454                                 throw new InternalErrorException ("Outer conversion routine is out of sync");
1455
1456                         ig.Emit (OpCodes.Call, conversion_operator);
1457                 }
1458                 
1459         }
1460         
1461         /// <summary>
1462         ///     This is a numeric cast to a Decimal
1463         /// </summary>
1464         public class CastToDecimal : TypeCast {
1465                 MethodInfo conversion_operator;
1466
1467                 public CastToDecimal (Expression child)
1468                         : this (child, false)
1469                 {
1470                 }
1471
1472                 public CastToDecimal (Expression child, bool find_explicit)
1473                         : base (child, TypeManager.decimal_type)
1474                 {
1475                         conversion_operator = GetConversionOperator (find_explicit);
1476
1477                         if (conversion_operator == null)
1478                                 throw new InternalErrorException ("Outer conversion routine is out of sync");
1479                 }
1480
1481                 // Returns the implicit operator that converts from
1482                 // 'child.Type' to System.Decimal.
1483                 MethodInfo GetConversionOperator (bool find_explicit)
1484                 {
1485                         string operator_name = find_explicit ? "op_Explicit" : "op_Implicit";
1486                         
1487                         MemberInfo [] mi = TypeManager.MemberLookup (type, type, type, MemberTypes.Method,
1488                                 BindingFlags.Static | BindingFlags.Public, operator_name, null);
1489
1490                         foreach (MethodInfo oper in mi) {
1491                                 ParameterData pd = TypeManager.GetParameterData (oper);
1492
1493                                 if (pd.ParameterType (0) == child.Type && TypeManager.TypeToCoreType (oper.ReturnType) == type)
1494                                         return oper;
1495                         }
1496
1497                         return null;
1498                 }
1499                 public override void Emit (EmitContext ec)
1500                 {
1501                         ILGenerator ig = ec.ig;
1502                         child.Emit (ec);
1503
1504                         ig.Emit (OpCodes.Call, conversion_operator);
1505                 }
1506         }
1507
1508         /// <summary>
1509         ///     This is an explicit numeric cast from a Decimal
1510         /// </summary>
1511         public class CastFromDecimal : TypeCast
1512         {
1513                 static IDictionary operators;
1514
1515                 public CastFromDecimal (Expression child, Type return_type)
1516                         : base (child, return_type)
1517                 {
1518                         if (child.Type != TypeManager.decimal_type)
1519                                 throw new InternalErrorException (
1520                                         "The expected type is Decimal, instead it is " + child.Type.FullName);
1521                 }
1522
1523                 // Returns the explicit operator that converts from an
1524                 // express of type System.Decimal to 'type'.
1525                 public Expression Resolve ()
1526                 {
1527                         if (operators == null) {
1528                                  MemberInfo[] all_oper = TypeManager.MemberLookup (TypeManager.decimal_type,
1529                                         TypeManager.decimal_type, TypeManager.decimal_type, MemberTypes.Method,
1530                                         BindingFlags.Static | BindingFlags.Public, "op_Explicit", null);
1531
1532                                 operators = new System.Collections.Specialized.HybridDictionary ();
1533                                 foreach (MethodInfo oper in all_oper) {
1534                                         ParameterData pd = TypeManager.GetParameterData (oper);
1535                                         if (pd.ParameterType (0) == TypeManager.decimal_type)
1536                                                 operators.Add (TypeManager.TypeToCoreType (oper.ReturnType), oper);
1537                                 }
1538                         }
1539
1540                         return operators.Contains (type) ? this : null;
1541                 }
1542
1543                 public override void Emit (EmitContext ec)
1544                 {
1545                         ILGenerator ig = ec.ig;
1546                         child.Emit (ec);
1547
1548                         ig.Emit (OpCodes.Call, (MethodInfo)operators [type]);
1549                 }
1550         }
1551
1552         
1553         //
1554         // Constant specialization of EmptyCast.
1555         // We need to special case this since an empty cast of
1556         // a constant is still a constant. 
1557         //
1558         public class EmptyConstantCast : Constant
1559         {
1560                 public readonly Constant child;
1561
1562                 public EmptyConstantCast(Constant child, Type type)
1563                         : base (child.Location)
1564                 {
1565                         eclass = child.eclass;
1566                         this.child = child;
1567                         this.type = type;
1568                 }
1569
1570                 public override string AsString ()
1571                 {
1572                         return child.AsString ();
1573                 }
1574
1575                 public override object GetValue ()
1576                 {
1577                         return child.GetValue ();
1578                 }
1579
1580                 public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
1581                 {
1582                         // FIXME: check that 'type' can be converted to 'target_type' first
1583                         return child.ConvertExplicitly (in_checked_context, target_type);
1584                 }
1585
1586                 public override Expression CreateExpressionTree (EmitContext ec)
1587                 {
1588                         ArrayList args = new ArrayList (2);
1589                         args.Add (new Argument (child.CreateExpressionTree (ec)));
1590                         args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
1591                         return CreateExpressionFactoryCall ("Convert", args);
1592                 }
1593
1594                 public override Constant Increment ()
1595                 {
1596                         return child.Increment ();
1597                 }
1598
1599                 public override bool IsDefaultValue {
1600                         get { return child.IsDefaultValue; }
1601                 }
1602
1603                 public override bool IsNegative {
1604                         get { return child.IsNegative; }
1605                 }
1606
1607                 public override bool IsNull {
1608                         get { return child.IsNull; }
1609                 }
1610
1611                 public override bool IsZeroInteger {
1612                         get { return child.IsZeroInteger; }
1613                 }
1614                 
1615                 public override void Emit (EmitContext ec)
1616                 {
1617                         child.Emit (ec);
1618                 }
1619
1620                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1621                 {
1622                         child.EmitBranchable (ec, label, on_true);
1623                 }
1624
1625                 public override void EmitSideEffect (EmitContext ec)
1626                 {
1627                         child.EmitSideEffect (ec);
1628                 }
1629
1630                 public override Constant ConvertImplicitly (Type target_type)
1631                 {
1632                         // FIXME: Do we need to check user conversions?
1633                         if (!Convert.ImplicitStandardConversionExists (this, target_type))
1634                                 return null;
1635                         return child.ConvertImplicitly (target_type);
1636                 }
1637         }
1638
1639
1640         /// <summary>
1641         ///  This class is used to wrap literals which belong inside Enums
1642         /// </summary>
1643         public class EnumConstant : Constant {
1644                 public Constant Child;
1645
1646                 public EnumConstant (Constant child, Type enum_type):
1647                         base (child.Location)
1648                 {
1649                         eclass = child.eclass;
1650                         this.Child = child;
1651                         type = enum_type;
1652                 }
1653                 
1654                 public override Expression DoResolve (EmitContext ec)
1655                 {
1656                         // This should never be invoked, we are born in fully
1657                         // initialized state.
1658
1659                         return this;
1660                 }
1661
1662                 public override void Emit (EmitContext ec)
1663                 {
1664                         Child.Emit (ec);
1665                 }
1666
1667                 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1668                 {
1669                         Child.EmitBranchable (ec, label, on_true);
1670                 }
1671
1672                 public override void EmitSideEffect (EmitContext ec)
1673                 {
1674                         Child.EmitSideEffect (ec);
1675                 }
1676
1677                 public override bool GetAttributableValue (Type value_type, out object value)
1678                 {
1679                         value = GetTypedValue ();
1680                         return true;
1681                 }
1682
1683                 public override string GetSignatureForError()
1684                 {
1685                         return TypeManager.CSharpName (Type);
1686                 }
1687
1688                 public override object GetValue ()
1689                 {
1690                         return Child.GetValue ();
1691                 }
1692
1693                 public override object GetTypedValue ()
1694                 {
1695                         // FIXME: runtime is not ready to work with just emited enums
1696                         if (!RootContext.StdLib) {
1697                                 return Child.GetValue ();
1698                         }
1699
1700                         return System.Enum.ToObject (type, Child.GetValue ());
1701                 }
1702                 
1703                 public override string AsString ()
1704                 {
1705                         return TypeManager.CSharpEnumValue (type, Child.GetValue ());
1706                 }
1707
1708                 public override Constant Increment()
1709                 {
1710                         return new EnumConstant (Child.Increment (), type);
1711                 }
1712
1713                 public override bool IsDefaultValue {
1714                         get {
1715                                 return Child.IsDefaultValue;
1716                         }
1717                 }
1718
1719                 public override bool IsZeroInteger {
1720                         get { return Child.IsZeroInteger; }
1721                 }
1722
1723                 public override bool IsNegative {
1724                         get {
1725                                 return Child.IsNegative;
1726                         }
1727                 }
1728
1729                 public override Constant ConvertExplicitly(bool in_checked_context, Type target_type)
1730                 {
1731                         if (Child.Type == target_type)
1732                                 return Child;
1733
1734                         return Child.ConvertExplicitly (in_checked_context, target_type);
1735                 }
1736
1737                 public override Constant ConvertImplicitly (Type type)
1738                 {
1739                         Type this_type = TypeManager.DropGenericTypeArguments (Type);
1740                         type = TypeManager.DropGenericTypeArguments (type);
1741
1742                         if (this_type == type) {
1743                                 // This is workaround of mono bug. It can be removed when the latest corlib spreads enough
1744                                 if (TypeManager.IsEnumType (type.UnderlyingSystemType))
1745                                         return this;
1746
1747                                 Type child_type = TypeManager.DropGenericTypeArguments (Child.Type);
1748                                 if (type.UnderlyingSystemType != child_type)
1749                                         Child = Child.ConvertImplicitly (type.UnderlyingSystemType);
1750                                 return this;
1751                         }
1752
1753                         if (!Convert.ImplicitStandardConversionExists (this, type)){
1754                                 return null;
1755                         }
1756
1757                         return Child.ConvertImplicitly(type);
1758                 }
1759
1760         }
1761
1762         /// <summary>
1763         ///   This kind of cast is used to encapsulate Value Types in objects.
1764         ///
1765         ///   The effect of it is to box the value type emitted by the previous
1766         ///   operation.
1767         /// </summary>
1768         public class BoxedCast : TypeCast {
1769
1770                 public BoxedCast (Expression expr, Type target_type)
1771                         : base (expr, target_type)
1772                 {
1773                         eclass = ExprClass.Value;
1774                 }
1775                 
1776                 public override Expression DoResolve (EmitContext ec)
1777                 {
1778                         // This should never be invoked, we are born in fully
1779                         // initialized state.
1780
1781                         return this;
1782                 }
1783
1784                 public override void Emit (EmitContext ec)
1785                 {
1786                         base.Emit (ec);
1787                         
1788                         ec.ig.Emit (OpCodes.Box, child.Type);
1789                 }
1790
1791                 public override void EmitSideEffect (EmitContext ec)
1792                 {
1793                         // boxing is side-effectful, since it involves runtime checks, except when boxing to Object or ValueType
1794                         // so, we need to emit the box+pop instructions in most cases
1795                         if (child.Type.IsValueType &&
1796                             (type == TypeManager.object_type || type == TypeManager.value_type))
1797                                 child.EmitSideEffect (ec);
1798                         else
1799                                 base.EmitSideEffect (ec);
1800                 }
1801         }
1802
1803         public class UnboxCast : TypeCast {
1804                 public UnboxCast (Expression expr, Type return_type)
1805                         : base (expr, return_type)
1806                 {
1807                 }
1808
1809                 public override Expression DoResolve (EmitContext ec)
1810                 {
1811                         // This should never be invoked, we are born in fully
1812                         // initialized state.
1813
1814                         return this;
1815                 }
1816
1817                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1818                 {
1819                         if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess)
1820                                 Report.Error (445, loc, "Cannot modify the result of an unboxing conversion");
1821                         return base.DoResolveLValue (ec, right_side);
1822                 }
1823
1824                 public override void Emit (EmitContext ec)
1825                 {
1826                         Type t = type;
1827                         ILGenerator ig = ec.ig;
1828                         
1829                         base.Emit (ec);
1830 #if GMCS_SOURCE
1831                         if (t.IsGenericParameter || t.IsGenericType && t.IsValueType)
1832                                 ig.Emit (OpCodes.Unbox_Any, t);
1833                         else
1834 #endif
1835                         {
1836                                 ig.Emit (OpCodes.Unbox, t);
1837
1838                                 LoadFromPtr (ig, t);
1839                         }
1840                 }
1841         }
1842         
1843         /// <summary>
1844         ///   This is used to perform explicit numeric conversions.
1845         ///
1846         ///   Explicit numeric conversions might trigger exceptions in a checked
1847         ///   context, so they should generate the conv.ovf opcodes instead of
1848         ///   conv opcodes.
1849         /// </summary>
1850         public class ConvCast : TypeCast {
1851                 public enum Mode : byte {
1852                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1853                         U1_I1, U1_CH,
1854                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1855                         U2_I1, U2_U1, U2_I2, U2_CH,
1856                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1857                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1858                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1859                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1860                         CH_I1, CH_U1, CH_I2,
1861                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1862                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1863                 }
1864
1865                 Mode mode;
1866                 
1867                 public ConvCast (Expression child, Type return_type, Mode m)
1868                         : base (child, return_type)
1869                 {
1870                         mode = m;
1871                 }
1872
1873                 public override Expression DoResolve (EmitContext ec)
1874                 {
1875                         // This should never be invoked, we are born in fully
1876                         // initialized state.
1877
1878                         return this;
1879                 }
1880
1881                 public override string ToString ()
1882                 {
1883                         return String.Format ("ConvCast ({0}, {1})", mode, child);
1884                 }
1885                 
1886                 public override void Emit (EmitContext ec)
1887                 {
1888                         ILGenerator ig = ec.ig;
1889                         
1890                         base.Emit (ec);
1891
1892                         if (ec.CheckState){
1893                                 switch (mode){
1894                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1895                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1896                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1897                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1898                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1899
1900                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1901                                 case Mode.U1_CH: /* nothing */ break;
1902
1903                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1904                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1905                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1906                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1907                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1908                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1909
1910                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1911                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1912                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1913                                 case Mode.U2_CH: /* nothing */ break;
1914
1915                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1916                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1917                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1918                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1919                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1920                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1921                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1922
1923                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1924                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1925                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1926                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1927                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1928                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1929
1930                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1931                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1932                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1933                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1934                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1935                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1936                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1937                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1938
1939                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1940                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1941                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1942                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1943                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1944                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1945                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1946                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1947
1948                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1949                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1950                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1951
1952                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1953                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1954                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1955                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1956                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1957                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1958                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1959                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1960                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1961
1962                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1963                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1964                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1965                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1966                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1967                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1968                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1969                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1970                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1971                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1972                                 }
1973                         } else {
1974                                 switch (mode){
1975                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1976                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1977                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1978                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1979                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1980
1981                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1982                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1983
1984                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1985                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1986                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1987                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1988                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1989                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1990
1991                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1992                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1993                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1994                                 case Mode.U2_CH: /* nothing */ break;
1995
1996                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1997                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1998                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1999                                 case Mode.I4_U4: /* nothing */ break;
2000                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2001                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2002                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2003
2004                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2005                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2006                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2007                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2008                                 case Mode.U4_I4: /* nothing */ break;
2009                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2010
2011                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2012                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2013                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2014                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2015                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2016                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2017                                 case Mode.I8_U8: /* nothing */ break;
2018                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2019
2020                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2021                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2022                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2023                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2024                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2025                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2026                                 case Mode.U8_I8: /* nothing */ break;
2027                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2028
2029                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2030                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2031                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2032
2033                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2034                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2035                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2036                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2037                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2038                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2039                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2040                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2041                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2042
2043                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2044                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2045                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2046                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2047                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2048                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2049                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2050                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2051                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2052                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2053                                 }
2054                         }
2055                 }
2056         }
2057         
2058         public class OpcodeCast : TypeCast {
2059                 OpCode op, op2;
2060                 bool second_valid;
2061                 
2062                 public OpcodeCast (Expression child, Type return_type, OpCode op)
2063                         : base (child, return_type)
2064                         
2065                 {
2066                         this.op = op;
2067                         second_valid = false;
2068                 }
2069
2070                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2071                         : base (child, return_type)
2072                         
2073                 {
2074                         this.op = op;
2075                         this.op2 = op2;
2076                         second_valid = true;
2077                 }
2078
2079                 public override Expression DoResolve (EmitContext ec)
2080                 {
2081                         // This should never be invoked, we are born in fully
2082                         // initialized state.
2083
2084                         return this;
2085                 }
2086
2087                 public override void Emit (EmitContext ec)
2088                 {
2089                         base.Emit (ec);
2090                         ec.ig.Emit (op);
2091
2092                         if (second_valid)
2093                                 ec.ig.Emit (op2);
2094                 }
2095
2096                 public Type UnderlyingType {
2097                         get { return child.Type; }
2098                 }
2099         }
2100
2101         /// <summary>
2102         ///   This kind of cast is used to encapsulate a child and cast it
2103         ///   to the class requested
2104         /// </summary>
2105         public class ClassCast : TypeCast {
2106                 public ClassCast (Expression child, Type return_type)
2107                         : base (child, return_type)
2108                         
2109                 {
2110                 }
2111
2112                 public override Expression DoResolve (EmitContext ec)
2113                 {
2114                         // This should never be invoked, we are born in fully
2115                         // initialized state.
2116
2117                         return this;
2118                 }
2119
2120                 public override void Emit (EmitContext ec)
2121                 {
2122                         base.Emit (ec);
2123
2124                         if (TypeManager.IsGenericParameter (child.Type))
2125                                 ec.ig.Emit (OpCodes.Box, child.Type);
2126
2127 #if GMCS_SOURCE
2128                         if (type.IsGenericParameter)
2129                                 ec.ig.Emit (OpCodes.Unbox_Any, type);
2130                         else
2131 #endif
2132                                 ec.ig.Emit (OpCodes.Castclass, type);
2133                 }
2134         }
2135
2136         //
2137         // Used when resolved expression has different representations for
2138         // expression trees and emit phase
2139         //
2140         public class ReducedExpression : Expression
2141         {
2142                 class ReducedConstantExpression : Constant
2143                 {
2144                         readonly Constant expr;
2145                         readonly Expression orig_expr;
2146
2147                         public ReducedConstantExpression (Constant expr, Expression orig_expr)
2148                                 : base (expr.Location)
2149                         {
2150                                 this.expr = expr;
2151                                 this.orig_expr = orig_expr;
2152                                 eclass = expr.eclass;
2153                                 type = expr.Type;
2154                         }
2155
2156                         public override string AsString ()
2157                         {
2158                                 return expr.AsString ();
2159                         }
2160
2161                         public override Expression CreateExpressionTree (EmitContext ec)
2162                         {
2163                                 return orig_expr.CreateExpressionTree (ec);
2164                         }
2165
2166                         public override object GetValue ()
2167                         {
2168                                 return expr.GetValue ();
2169                         }
2170
2171                         public override Constant ConvertExplicitly (bool in_checked_context, Type target_type)
2172                         {
2173                                 throw new NotImplementedException ();
2174                         }
2175
2176                         public override Expression DoResolve (EmitContext ec)
2177                         {
2178                                 return this;
2179                         }
2180
2181                         public override Constant Increment ()
2182                         {
2183                                 throw new NotImplementedException ();
2184                         }
2185
2186                         public override bool IsDefaultValue {
2187                                 get {
2188                                         return expr.IsDefaultValue;
2189                                 }
2190                         }
2191
2192                         public override bool IsNegative {
2193                                 get {
2194                                         return expr.IsNegative;
2195                                 }
2196                         }
2197
2198                         public override void Emit (EmitContext ec)
2199                         {
2200                                 expr.Emit (ec);
2201                         }
2202                 }
2203
2204                 readonly Expression expr, orig_expr;
2205
2206                 private ReducedExpression (Expression expr, Expression orig_expr)
2207                 {
2208                         this.expr = expr;
2209                         this.orig_expr = orig_expr;
2210                         this.loc = orig_expr.Location;
2211                 }
2212
2213                 public static Expression Create (Constant expr, Expression original_expr)
2214                 {
2215                         return new ReducedConstantExpression (expr, original_expr);
2216                 }
2217
2218                 public static Expression Create (Expression expr, Expression original_expr)
2219                 {
2220                         Constant c = expr as Constant;
2221                         if (c != null)
2222                                 return Create (c, original_expr);
2223
2224                         return new ReducedExpression (expr, original_expr);
2225                 }
2226
2227                 public override Expression CreateExpressionTree (EmitContext ec)
2228                 {
2229                         return orig_expr.CreateExpressionTree (ec);
2230                 }
2231
2232                 public override Expression DoResolve (EmitContext ec)
2233                 {
2234                         eclass = expr.eclass;
2235                         type = expr.Type;
2236                         return this;
2237                 }
2238
2239                 public override void Emit (EmitContext ec)
2240                 {
2241                         expr.Emit (ec);
2242                 }
2243
2244                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2245                 {
2246                         expr.EmitBranchable (ec, target, on_true);
2247                 }
2248         }
2249
2250         //
2251         // Unresolved type name expressions
2252         //
2253         public abstract class ATypeNameExpression : FullNamedExpression
2254         {
2255                 public readonly string Name;
2256                 protected TypeArguments targs;
2257
2258                 protected ATypeNameExpression (string name, Location l)
2259                 {
2260                         Name = name;
2261                         loc = l;
2262                 }
2263
2264                 protected ATypeNameExpression (string name, TypeArguments targs, Location l)
2265                 {
2266                         Name = name;
2267                         this.targs = targs;
2268                         loc = l;
2269                 }
2270
2271                 public override string GetSignatureForError ()
2272                 {
2273                         if (targs != null) {
2274                                 return TypeManager.RemoveGenericArity (Name) + "<" +
2275                                         targs.GetSignatureForError () + ">";
2276                         }
2277
2278                         return Name;
2279                 }
2280         }
2281         
2282         /// <summary>
2283         ///   SimpleName expressions are formed of a single word and only happen at the beginning 
2284         ///   of a dotted-name.
2285         /// </summary>
2286         public class SimpleName : ATypeNameExpression {
2287                 bool in_transit;
2288
2289                 public SimpleName (string name, Location l)
2290                         : base (name, l)
2291                 {
2292                 }
2293
2294                 public SimpleName (string name, TypeArguments args, Location l)
2295                         : base (name, args, l)
2296                 {
2297                 }
2298
2299                 public SimpleName (string name, TypeParameter[] type_params, Location l)
2300                         : base (name, l)
2301                 {
2302                         targs = new TypeArguments (l);
2303                         foreach (TypeParameter type_param in type_params)
2304                                 targs.Add (new TypeParameterExpr (type_param, l));
2305                 }
2306
2307                 public static string RemoveGenericArity (string name)
2308                 {
2309                         int start = 0;
2310                         StringBuilder sb = null;
2311                         do {
2312                                 int pos = name.IndexOf ('`', start);
2313                                 if (pos < 0) {
2314                                         if (start == 0)
2315                                                 return name;
2316
2317                                         sb.Append (name.Substring (start));
2318                                         break;
2319                                 }
2320
2321                                 if (sb == null)
2322                                         sb = new StringBuilder ();
2323                                 sb.Append (name.Substring (start, pos-start));
2324
2325                                 pos++;
2326                                 while ((pos < name.Length) && Char.IsNumber (name [pos]))
2327                                         pos++;
2328
2329                                 start = pos;
2330                         } while (start < name.Length);
2331
2332                         return sb.ToString ();
2333                 }
2334
2335                 public SimpleName GetMethodGroup ()
2336                 {
2337                         return new SimpleName (RemoveGenericArity (Name), targs, loc);
2338                 }
2339
2340                 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
2341                 {
2342                         if (ec.IsInFieldInitializer)
2343                                 Report.Error (236, l,
2344                                         "A field initializer cannot reference the nonstatic field, method, or property `{0}'",
2345                                         name);
2346                         else
2347                                 Report.Error (
2348                                         120, l, "`{0}': An object reference is required for the nonstatic field, method or property",
2349                                         name);
2350                 }
2351
2352                 public bool IdenticalNameAndTypeName (EmitContext ec, Expression resolved_to, Location loc)
2353                 {
2354                         return resolved_to != null && resolved_to.Type != null && 
2355                                 resolved_to.Type.Name == Name &&
2356                                 (ec.DeclContainer.LookupNamespaceOrType (Name, loc, /* ignore_cs0104 = */ true) != null);
2357                 }
2358
2359                 public override Expression DoResolve (EmitContext ec)
2360                 {
2361                         return SimpleNameResolve (ec, null, false);
2362                 }
2363
2364                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
2365                 {
2366                         return SimpleNameResolve (ec, right_side, false);
2367                 }
2368                 
2369
2370                 public Expression DoResolve (EmitContext ec, bool intermediate)
2371                 {
2372                         return SimpleNameResolve (ec, null, intermediate);
2373                 }
2374
2375                 static bool IsNestedChild (Type t, Type parent)
2376                 {
2377                         while (parent != null) {
2378                                 if (TypeManager.IsNestedChildOf (t, TypeManager.DropGenericTypeArguments (parent)))
2379                                         return true;
2380
2381                                 parent = parent.BaseType;
2382                         }
2383
2384                         return false;
2385                 }
2386
2387                 FullNamedExpression ResolveNested (IResolveContext ec, Type t)
2388                 {
2389                         if (!TypeManager.IsGenericTypeDefinition (t) && !TypeManager.IsGenericType (t))
2390                                 return null;
2391
2392                         DeclSpace ds = ec.DeclContainer;
2393                         while (ds != null && !IsNestedChild (t, ds.TypeBuilder))
2394                                 ds = ds.Parent;
2395
2396                         if (ds == null)
2397                                 return null;
2398
2399                         Type[] gen_params = TypeManager.GetTypeArguments (t);
2400
2401                         int arg_count = targs != null ? targs.Count : 0;
2402
2403                         for (; (ds != null) && ds.IsGeneric; ds = ds.Parent) {
2404                                 if (arg_count + ds.CountTypeParameters == gen_params.Length) {
2405                                         TypeArguments new_args = new TypeArguments (loc);
2406                                         foreach (TypeParameter param in ds.TypeParameters)
2407                                                 new_args.Add (new TypeParameterExpr (param, loc));
2408
2409                                         if (targs != null)
2410                                                 new_args.Add (targs);
2411
2412                                         return new ConstructedType (t, new_args, loc);
2413                                 }
2414                         }
2415
2416                         return null;
2417                 }
2418
2419                 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
2420                 {
2421                         FullNamedExpression fne = ec.GenericDeclContainer.LookupGeneric (Name, loc);
2422                         if (fne != null)
2423                                 return fne.ResolveAsTypeStep (ec, silent);
2424
2425                         int errors = Report.Errors;
2426                         fne = ec.DeclContainer.LookupNamespaceOrType (Name, loc, /*ignore_cs0104=*/ false);
2427
2428                         if (fne != null) {
2429                                 if (fne.Type == null)
2430                                         return fne;
2431
2432                                 FullNamedExpression nested = ResolveNested (ec, fne.Type);
2433                                 if (nested != null)
2434                                         return nested.ResolveAsTypeStep (ec, false);
2435
2436                                 if (targs != null) {
2437                                         ConstructedType ct = new ConstructedType (fne, targs, loc);
2438                                         return ct.ResolveAsTypeStep (ec, false);
2439                                 }
2440
2441                                 return fne;
2442                         }
2443
2444                         if (silent || errors != Report.Errors)
2445                                 return null;
2446
2447                         Error_TypeOrNamespaceNotFound (ec);
2448                         return null;
2449                 }
2450
2451                 protected virtual void Error_TypeOrNamespaceNotFound (IResolveContext ec)
2452                 {
2453                         MemberCore mc = ec.DeclContainer.GetDefinition (Name);
2454                         if (mc != null) {
2455                                 Error_UnexpectedKind (ec.DeclContainer, "type", GetMemberType (mc), loc);
2456                                 return;
2457                         }
2458
2459                         string ns = ec.DeclContainer.NamespaceEntry.NS.Name;
2460                         string fullname = (ns.Length > 0) ? ns + "." + Name : Name;
2461                         foreach (Assembly a in RootNamespace.Global.Assemblies) {
2462                                 Type type = a.GetType (fullname);
2463                                 if (type != null) {
2464                                         Report.SymbolRelatedToPreviousError (type);
2465                                         Expression.ErrorIsInaccesible (loc, TypeManager.CSharpName (type));
2466                                         return;
2467                                 }
2468                         }
2469
2470                         Type t = ec.DeclContainer.LookupAnyGeneric (Name);
2471                         if (t != null) {
2472                                 Namespace.Error_InvalidNumberOfTypeArguments (t, loc);
2473                                 return;
2474                         }
2475
2476                         if (targs != null) {
2477                                 FullNamedExpression retval = ec.DeclContainer.LookupNamespaceOrType (SimpleName.RemoveGenericArity (Name), loc, true);
2478                                 if (retval != null) {
2479                                         Namespace.Error_TypeArgumentsCannotBeUsed (retval.Type, loc);
2480                                         return;
2481                                 }
2482                         }
2483                                                 
2484                         NamespaceEntry.Error_NamespaceNotFound (loc, Name);
2485                 }
2486
2487                 // TODO: I am still not convinced about this. If someone else will need it
2488                 // implement this as virtual property in MemberCore hierarchy
2489                 public static string GetMemberType (MemberCore mc)
2490                 {
2491                         if (mc is Property)
2492                                 return "property";
2493                         if (mc is Indexer)
2494                                 return "indexer";
2495                         if (mc is FieldBase)
2496                                 return "field";
2497                         if (mc is MethodCore)
2498                                 return "method";
2499                         if (mc is EnumMember)
2500                                 return "enum";
2501                         if (mc is Event)
2502                                 return "event";
2503
2504                         return "type";
2505                 }
2506
2507                 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool intermediate)
2508                 {
2509                         if (in_transit)
2510                                 return null;
2511
2512                         in_transit = true;
2513                         Expression e = DoSimpleNameResolve (ec, right_side, intermediate);
2514                         in_transit = false;
2515
2516                         if (e == null)
2517                                 return null;
2518
2519                         if (ec.CurrentBlock == null || ec.CurrentBlock.CheckInvariantMeaningInBlock (Name, e, Location))
2520                                 return e;
2521
2522                         return null;
2523                 }
2524
2525                 /// <remarks>
2526                 ///   7.5.2: Simple Names. 
2527                 ///
2528                 ///   Local Variables and Parameters are handled at
2529                 ///   parse time, so they never occur as SimpleNames.
2530                 ///
2531                 ///   The `intermediate' flag is used by MemberAccess only
2532                 ///   and it is used to inform us that it is ok for us to 
2533                 ///   avoid the static check, because MemberAccess might end
2534                 ///   up resolving the Name as a Type name and the access as
2535                 ///   a static type access.
2536                 ///
2537                 ///   ie: Type Type; .... { Type.GetType (""); }
2538                 ///
2539                 ///   Type is both an instance variable and a Type;  Type.GetType
2540                 ///   is the static method not an instance method of type.
2541                 /// </remarks>
2542                 Expression DoSimpleNameResolve (EmitContext ec, Expression right_side, bool intermediate)
2543                 {
2544                         Expression e = null;
2545
2546                         //
2547                         // Stage 1: Performed by the parser (binding to locals or parameters).
2548                         //
2549                         Block current_block = ec.CurrentBlock;
2550                         if (current_block != null){
2551                                 LocalInfo vi = current_block.GetLocalInfo (Name);
2552                                 if (vi != null){
2553                                         if (targs != null) {
2554                                                 Report.Error (307, loc,
2555                                                               "The variable `{0}' cannot be used with type arguments",
2556                                                               Name);
2557                                                 return null;
2558                                         }
2559
2560                                         LocalVariableReference var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2561                                         if (right_side != null) {
2562                                                 return var.ResolveLValue (ec, right_side, loc);
2563                                         } else {
2564                                                 ResolveFlags rf = ResolveFlags.VariableOrValue;
2565                                                 if (intermediate)
2566                                                         rf |= ResolveFlags.DisableFlowAnalysis;
2567                                                 return var.Resolve (ec, rf);
2568                                         }
2569                                 }
2570
2571                                 ParameterReference pref = current_block.Toplevel.GetParameterReference (Name, loc);
2572                                 if (pref != null) {
2573                                         if (targs != null) {
2574                                                 Report.Error (307, loc,
2575                                                               "The variable `{0}' cannot be used with type arguments",
2576                                                               Name);
2577                                                 return null;
2578                                         }
2579
2580                                         if (right_side != null)
2581                                                 return pref.ResolveLValue (ec, right_side, loc);
2582                                         else
2583                                                 return pref.Resolve (ec);
2584                                 }
2585
2586                                 Expression expr = current_block.Toplevel.GetTransparentIdentifier (Name);
2587                                 if (expr != null) {
2588                                         if (right_side != null)
2589                                                 return expr.ResolveLValue (ec, right_side, loc);
2590                                         return expr.Resolve (ec);
2591                                 }
2592                         }
2593                         
2594                         //
2595                         // Stage 2: Lookup members 
2596                         //
2597
2598                         Type almost_matched_type = null;
2599                         ArrayList almost_matched = null;
2600                         for (DeclSpace lookup_ds = ec.DeclContainer; lookup_ds != null; lookup_ds = lookup_ds.Parent) {
2601                                 // either RootDeclSpace or GenericMethod
2602                                 if (lookup_ds.TypeBuilder == null)
2603                                         continue;
2604
2605                                 e = MemberLookup (ec.ContainerType, lookup_ds.TypeBuilder, Name, loc);
2606                                 if (e != null) {
2607                                         if (e is PropertyExpr) {
2608                                                 // since TypeManager.MemberLookup doesn't know if we're doing a lvalue access or not,
2609                                                 // it doesn't know which accessor to check permissions against
2610                                                 if (((PropertyExpr) e).IsAccessibleFrom (ec.ContainerType, right_side != null))
2611                                                         break;
2612                                         } else if (e is EventExpr) {
2613                                                 if (((EventExpr) e).IsAccessibleFrom (ec.ContainerType))
2614                                                         break;
2615                                         } else {
2616                                                 break;
2617                                         }
2618                                         e = null;
2619                                 }
2620
2621                                 if (almost_matched == null && almost_matched_members.Count > 0) {
2622                                         almost_matched_type = lookup_ds.TypeBuilder;
2623                                         almost_matched = (ArrayList) almost_matched_members.Clone ();
2624                                 }
2625                         }
2626
2627                         if (e == null) {
2628                                 if (almost_matched == null && almost_matched_members.Count > 0) {
2629                                         almost_matched_type = ec.ContainerType;
2630                                         almost_matched = (ArrayList) almost_matched_members.Clone ();
2631                                 }
2632                                 e = ResolveAsTypeStep (ec, true);
2633                         }
2634
2635                         if (e == null) {
2636                                 if (current_block != null) {
2637                                         IKnownVariable ikv = current_block.Explicit.GetKnownVariable (Name);
2638                                         if (ikv != null) {
2639                                                 LocalInfo li = ikv as LocalInfo;
2640                                                 // Supress CS0219 warning
2641                                                 if (li != null)
2642                                                         li.Used = true;
2643
2644                                                 Error_VariableIsUsedBeforeItIsDeclared (Name);
2645                                                 return null;
2646                                         }
2647                                 }
2648
2649                                 if (almost_matched != null)
2650                                         almost_matched_members = almost_matched;
2651                                 if (almost_matched_type == null)
2652                                         almost_matched_type = ec.ContainerType;
2653                                 Error_MemberLookupFailed (ec.ContainerType, null, almost_matched_type, Name,
2654                                         ec.DeclContainer.Name, AllMemberTypes, AllBindingFlags);
2655                                 return null;
2656                         }
2657
2658                         if (e is TypeExpr) {
2659                                 if (targs == null)
2660                                         return e;
2661
2662                                 ConstructedType ct = new ConstructedType (
2663                                         e.Type, targs, loc);
2664                                 return ct.ResolveAsTypeStep (ec, false);
2665                         }
2666
2667                         if (e is MemberExpr) {
2668                                 MemberExpr me = (MemberExpr) e;
2669
2670                                 Expression left;
2671                                 if (me.IsInstance) {
2672                                         if (ec.IsStatic || ec.IsInFieldInitializer) {
2673                                                 //
2674                                                 // Note that an MemberExpr can be both IsInstance and IsStatic.
2675                                                 // An unresolved MethodGroupExpr can contain both kinds of methods
2676                                                 // and each predicate is true if the MethodGroupExpr contains
2677                                                 // at least one of that kind of method.
2678                                                 //
2679
2680                                                 if (!me.IsStatic &&
2681                                                     (!intermediate || !IdenticalNameAndTypeName (ec, me, loc))) {
2682                                                         Error_ObjectRefRequired (ec, loc, me.GetSignatureForError ());
2683                                                         return null;
2684                                                 }
2685
2686                                                 //
2687                                                 // Pass the buck to MemberAccess and Invocation.
2688                                                 //
2689                                                 left = EmptyExpression.Null;
2690                                         } else {
2691                                                 left = ec.GetThis (loc);
2692                                         }
2693                                 } else {
2694                                         left = new TypeExpression (ec.ContainerType, loc);
2695                                 }
2696
2697                                 me = me.ResolveMemberAccess (ec, left, loc, null);
2698                                 if (me == null)
2699                                         return null;
2700
2701                                 if (targs != null) {
2702                                         targs.Resolve (ec);
2703                                         me.SetTypeArguments (targs);
2704                                 }
2705
2706                                 if (!me.IsStatic && (me.InstanceExpression != null) &&
2707                                     TypeManager.IsNestedFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2708                                     me.InstanceExpression.Type != me.DeclaringType &&
2709                                     !TypeManager.IsFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2710                                     (!intermediate || !IdenticalNameAndTypeName (ec, e, loc))) {
2711                                         Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
2712                                                 TypeManager.CSharpName (me.DeclaringType), TypeManager.CSharpName (me.InstanceExpression.Type));
2713                                         return null;
2714                                 }
2715
2716                                 return (right_side != null)
2717                                         ? me.DoResolveLValue (ec, right_side)
2718                                         : me.DoResolve (ec);
2719                         }
2720
2721                         return e;
2722                 }
2723                 
2724                 protected override void CloneTo (CloneContext clonectx, Expression target)
2725                 {
2726                         // CloneTo: Nothing, we do not keep any state on this expression
2727                 }
2728         }
2729
2730         /// <summary>
2731         ///   Represents a namespace or a type.  The name of the class was inspired by
2732         ///   section 10.8.1 (Fully Qualified Names).
2733         /// </summary>
2734         public abstract class FullNamedExpression : Expression {
2735                 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
2736                 {
2737                         return this;
2738                 }
2739
2740                 public override void Emit (EmitContext ec)
2741                 {
2742                         throw new InternalErrorException ("FullNamedExpression `{0}' found in resolved tree",
2743                                 GetSignatureForError ());
2744                 }
2745         }
2746         
2747         /// <summary>
2748         ///   Expression that evaluates to a type
2749         /// </summary>
2750         public abstract class TypeExpr : FullNamedExpression {
2751                 override public FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
2752                 {
2753                         TypeExpr t = DoResolveAsTypeStep (ec);
2754                         if (t == null)
2755                                 return null;
2756
2757                         eclass = ExprClass.Type;
2758                         return t;
2759                 }
2760
2761                 override public Expression DoResolve (EmitContext ec)
2762                 {
2763                         return ResolveAsTypeTerminal (ec, false);
2764                 }
2765
2766                 public virtual bool CheckAccessLevel (DeclSpace ds)
2767                 {
2768                         return ds.CheckAccessLevel (Type);
2769                 }
2770
2771                 public virtual bool AsAccessible (DeclSpace ds)
2772                 {
2773                         return ds.IsAccessibleAs (Type);
2774                 }
2775
2776                 public virtual bool IsClass {
2777                         get { return Type.IsClass; }
2778                 }
2779
2780                 public virtual bool IsValueType {
2781                         get { return Type.IsValueType; }
2782                 }
2783
2784                 public virtual bool IsInterface {
2785                         get { return Type.IsInterface; }
2786                 }
2787
2788                 public virtual bool IsSealed {
2789                         get { return Type.IsSealed; }
2790                 }
2791
2792                 public virtual bool CanInheritFrom ()
2793                 {
2794                         if (Type == TypeManager.enum_type ||
2795                             (Type == TypeManager.value_type && RootContext.StdLib) ||
2796                             Type == TypeManager.multicast_delegate_type ||
2797                             Type == TypeManager.delegate_type ||
2798                             Type == TypeManager.array_type)
2799                                 return false;
2800
2801                         return true;
2802                 }
2803
2804                 protected abstract TypeExpr DoResolveAsTypeStep (IResolveContext ec);
2805
2806                 public override bool Equals (object obj)
2807                 {
2808                         TypeExpr tobj = obj as TypeExpr;
2809                         if (tobj == null)
2810                                 return false;
2811
2812                         return Type == tobj.Type;
2813                 }
2814
2815                 public override int GetHashCode ()
2816                 {
2817                         return Type.GetHashCode ();
2818                 }
2819         }
2820
2821         /// <summary>
2822         ///   Fully resolved Expression that already evaluated to a type
2823         /// </summary>
2824         public class TypeExpression : TypeExpr {
2825                 public TypeExpression (Type t, Location l)
2826                 {
2827                         Type = t;
2828                         eclass = ExprClass.Type;
2829                         loc = l;
2830                 }
2831
2832                 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
2833                 {
2834                         return this;
2835                 }
2836
2837                 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
2838                 {
2839                         return this;
2840                 }
2841         }
2842
2843         /// <summary>
2844         ///   Used to create types from a fully qualified name.  These are just used
2845         ///   by the parser to setup the core types.  A TypeLookupExpression is always
2846         ///   classified as a type.
2847         /// </summary>
2848         public sealed class TypeLookupExpression : TypeExpr {
2849                 readonly string name;
2850                 
2851                 public TypeLookupExpression (string name)
2852                 {
2853                         this.name = name;
2854                         eclass = ExprClass.Type;
2855                 }
2856
2857                 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
2858                 {
2859                         // It's null for corlib compilation only
2860                         if (type == null)
2861                                 return DoResolveAsTypeStep (ec);
2862
2863                         return this;
2864                 }
2865
2866                 private class UnexpectedType
2867                 {
2868                 }
2869
2870                 // This performes recursive type lookup, providing support for generic types.
2871                 // For example, given the type:
2872                 //
2873                 // System.Collections.Generic.KeyValuePair`2[[System.Int32],[System.String]]
2874                 //
2875                 // The types will be checked in the following order:
2876                 //                                                                             _
2877                 // System                                                                       |
2878                 // System.Collections                                                           |
2879                 // System.Collections.Generic                                                   |
2880                 //                        _                                                     |
2881                 //     System              | recursive call 1                                   |
2882                 //     System.Int32       _|                                                    | main method call
2883                 //                        _                                                     |
2884                 //     System              | recursive call 2                                   |
2885                 //     System.String      _|                                                    |
2886                 //                                                                              |
2887                 // System.Collections.Generic.KeyValuePair`2[[System.Int32],[System.String]]   _|
2888                 //
2889                 private Type TypeLookup (IResolveContext ec, string name)
2890                 {
2891                         int index = 0;
2892                         int dot = 0;
2893                         bool done = false;
2894                         FullNamedExpression resolved = null;
2895                         Type type = null;
2896                         Type recursive_type = null;
2897                         while (index < name.Length) {
2898                                 if (name[index] == '[') {
2899                                         int open = index;
2900                                         int braces = 1;
2901                                         do {
2902                                                 index++;
2903                                                 if (name[index] == '[')
2904                                                         braces++;
2905                                                 else if (name[index] == ']')
2906                                                         braces--;
2907                                         } while (braces > 0);
2908                                         recursive_type = TypeLookup (ec, name.Substring (open + 1, index - open - 1));
2909                                         if (recursive_type == null || (recursive_type == typeof(UnexpectedType)))
2910                                                 return recursive_type;
2911                                 }
2912                                 else {
2913                                         if (name[index] == ',')
2914                                                 done = true;
2915                                         else if ((name[index] == '.' && !done) || (index == name.Length && name[0] != '[')) {
2916                                                 string substring = name.Substring(dot, index - dot);
2917
2918                                                 if (resolved == null)
2919                                                         resolved = RootNamespace.Global.Lookup (ec.DeclContainer, substring, Location.Null);
2920                                                 else if (resolved is Namespace)
2921                                                     resolved = (resolved as Namespace).Lookup (ec.DeclContainer, substring, Location.Null);
2922                                                 else if (type != null)
2923                                                         type = TypeManager.GetNestedType (type, substring);
2924                                                 else
2925                                                         return null;
2926
2927                                                 if (resolved == null)
2928                                                         return null;
2929                                                 else if (type == null && resolved is TypeExpr)
2930                                                         type = resolved.Type;
2931
2932                                                 dot = index + 1;
2933                                         }
2934                                 }
2935                                 index++;
2936                         }
2937                         if (name[0] != '[') {
2938                                 string substring = name.Substring(dot, index - dot);
2939
2940                                 if (type != null)
2941                                         return TypeManager.GetNestedType (type, substring);
2942                                 
2943                                 if (resolved != null) {
2944                                         resolved = (resolved as Namespace).Lookup (ec.DeclContainer, substring, Location.Null);
2945                                         if (resolved is TypeExpr)
2946                                                 return resolved.Type;
2947                                         
2948                                         if (resolved == null)
2949                                                 return null;
2950                                         
2951                                         resolved.Error_UnexpectedKind (ec.DeclContainer, "type", loc);
2952                                         return typeof (UnexpectedType);
2953                                 }
2954                                 else
2955                                         return null;
2956                         }
2957                         else
2958                                 return recursive_type;
2959                         }
2960
2961                 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
2962                 {
2963                         Type t = TypeLookup (ec, name);
2964                         if (t == null) {
2965                                 NamespaceEntry.Error_NamespaceNotFound (loc, name);
2966                                 return null;
2967                         }
2968                         if (t == typeof(UnexpectedType))
2969                                 return null;
2970                         type = t;
2971                         return this;
2972                 }
2973
2974                 protected override void CloneTo (CloneContext clonectx, Expression target)
2975                 {
2976                         // CloneTo: Nothing, we do not keep any state on this expression
2977                 }
2978
2979                 public override string GetSignatureForError ()
2980                 {
2981                         if (type == null)
2982                                 return TypeManager.CSharpName (name);
2983
2984                         return base.GetSignatureForError ();
2985                 }
2986         }
2987
2988         /// <summary>
2989         ///   Represents an "unbound generic type", ie. typeof (Foo<>).
2990         ///   See 14.5.11.
2991         /// </summary>
2992         public class UnboundTypeExpression : TypeExpr
2993         {
2994                 MemberName name;
2995
2996                 public UnboundTypeExpression (MemberName name, Location l)
2997                 {
2998                         this.name = name;
2999                         loc = l;
3000                 }
3001
3002                 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
3003                 {
3004                         Expression expr;
3005                         if (name.Left != null) {
3006                                 Expression lexpr = name.Left.GetTypeExpression ();
3007                                 expr = new MemberAccess (lexpr, name.Basename);
3008                         } else {
3009                                 expr = new SimpleName (name.Basename, loc);
3010                         }
3011
3012                         FullNamedExpression fne = expr.ResolveAsTypeStep (ec, false);
3013                         if (fne == null)
3014                                 return null;
3015
3016                         type = fne.Type;
3017                         return new TypeExpression (type, loc);
3018                 }
3019         }
3020
3021         public class TypeAliasExpression : TypeExpr {
3022                 FullNamedExpression alias;
3023                 TypeExpr texpr;
3024                 TypeArguments args;
3025
3026                 public TypeAliasExpression (FullNamedExpression alias, TypeArguments args, Location l)
3027                 {
3028                         this.alias = alias;
3029                         this.args = args;
3030                         loc = l;
3031
3032                         eclass = ExprClass.Type;
3033                 }
3034
3035                 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
3036                 {
3037                         texpr = alias.ResolveAsTypeTerminal (ec, false);
3038                         if (texpr == null)
3039                                 return null;
3040
3041                         Type type = texpr.Type;
3042                         int num_args = TypeManager.GetNumberOfTypeArguments (type);
3043
3044                         if (args != null) {
3045                                 if (num_args == 0) {
3046                                         Report.Error (308, loc,
3047                                                       "The non-generic type `{0}' cannot " +
3048                                                       "be used with type arguments.",
3049                                                       TypeManager.CSharpName (type));
3050                                         return null;
3051                                 }
3052
3053                                 ConstructedType ctype = new ConstructedType (type, args, loc);
3054                                 return ctype.ResolveAsTypeTerminal (ec, false);
3055                         } else if (num_args > 0) {
3056                                 Report.Error (305, loc,
3057                                               "Using the generic type `{0}' " +
3058                                               "requires {1} type arguments",
3059                                               TypeManager.CSharpName (type), num_args.ToString ());
3060                                 return null;
3061                         }
3062
3063                         return texpr;
3064                 }
3065
3066                 public override bool CheckAccessLevel (DeclSpace ds)
3067                 {
3068                         return texpr.CheckAccessLevel (ds);
3069                 }
3070
3071                 public override bool AsAccessible (DeclSpace ds)
3072                 {
3073                         return texpr.AsAccessible (ds);
3074                 }
3075
3076                 public override bool IsClass {
3077                         get { return texpr.IsClass; }
3078                 }
3079
3080                 public override bool IsValueType {
3081                         get { return texpr.IsValueType; }
3082                 }
3083
3084                 public override bool IsInterface {
3085                         get { return texpr.IsInterface; }
3086                 }
3087
3088                 public override bool IsSealed {
3089                         get { return texpr.IsSealed; }
3090                 }
3091         }
3092
3093         /// <summary>
3094         ///   This class denotes an expression which evaluates to a member
3095         ///   of a struct or a class.
3096         /// </summary>
3097         public abstract class MemberExpr : Expression
3098         {
3099                 protected bool is_base;
3100
3101                 /// <summary>
3102                 ///   The name of this member.
3103                 /// </summary>
3104                 public abstract string Name {
3105                         get;
3106                 }
3107
3108                 //
3109                 // When base.member is used
3110                 //
3111                 public bool IsBase {
3112                         get { return is_base; }
3113                         set { is_base = value; }
3114                 }
3115
3116                 /// <summary>
3117                 ///   Whether this is an instance member.
3118                 /// </summary>
3119                 public abstract bool IsInstance {
3120                         get;
3121                 }
3122
3123                 /// <summary>
3124                 ///   Whether this is a static member.
3125                 /// </summary>
3126                 public abstract bool IsStatic {
3127                         get;
3128                 }
3129
3130                 /// <summary>
3131                 ///   The type which declares this member.
3132                 /// </summary>
3133                 public abstract Type DeclaringType {
3134                         get;
3135                 }
3136
3137                 /// <summary>
3138                 ///   The instance expression associated with this member, if it's a
3139                 ///   non-static member.
3140                 /// </summary>
3141                 public Expression InstanceExpression;
3142
3143                 public static void error176 (Location loc, string name)
3144                 {
3145                         Report.Error (176, loc, "Static member `{0}' cannot be accessed " +
3146                                       "with an instance reference, qualify it with a type name instead", name);
3147                 }
3148
3149                 // TODO: possible optimalization
3150                 // Cache resolved constant result in FieldBuilder <-> expression map
3151                 public virtual MemberExpr ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
3152                                                                SimpleName original)
3153                 {
3154                         //
3155                         // Precondition:
3156                         //   original == null || original.Resolve (...) ==> left
3157                         //
3158
3159                         if (left is TypeExpr) {
3160                                 left = left.ResolveAsTypeTerminal (ec, true);
3161                                 if (left == null)
3162                                         return null;
3163
3164                                 if (!IsStatic) {
3165                                         SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
3166                                         return null;
3167                                 }
3168
3169                                 return this;
3170                         }
3171                                 
3172                         if (!IsInstance) {
3173                                 if (original != null && original.IdenticalNameAndTypeName (ec, left, loc))
3174                                         return this;
3175
3176                                 return ResolveExtensionMemberAccess (left);
3177                         }
3178
3179                         InstanceExpression = left;
3180                         return this;
3181                 }
3182
3183                 protected virtual MemberExpr ResolveExtensionMemberAccess (Expression left)
3184                 {
3185                         error176 (loc, GetSignatureForError ());
3186                         return this;
3187                 }
3188
3189                 protected void EmitInstance (EmitContext ec, bool prepare_for_load)
3190                 {
3191                         if (IsStatic)
3192                                 return;
3193
3194                         if (InstanceExpression == EmptyExpression.Null) {
3195                                 SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
3196                                 return;
3197                         }
3198                                 
3199                         if (InstanceExpression.Type.IsValueType) {
3200                                 if (InstanceExpression is IMemoryLocation) {
3201                                         ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
3202                                 } else {
3203                                         LocalTemporary t = new LocalTemporary (InstanceExpression.Type);
3204                                         InstanceExpression.Emit (ec);
3205                                         t.Store (ec);
3206                                         t.AddressOf (ec, AddressOp.Store);
3207                                 }
3208                         } else
3209                                 InstanceExpression.Emit (ec);
3210
3211                         if (prepare_for_load)
3212                                 ec.ig.Emit (OpCodes.Dup);
3213                 }
3214
3215                 public virtual void SetTypeArguments (TypeArguments ta)
3216                 {
3217                         // TODO: need to get correct member type
3218                         Report.Error (307, loc, "The property `{0}' cannot be used with type arguments",
3219                                 GetSignatureForError ());
3220                 }
3221         }
3222
3223         /// 
3224         /// Represents group of extension methods
3225         /// 
3226         public class ExtensionMethodGroupExpr : MethodGroupExpr
3227         {
3228                 readonly NamespaceEntry namespace_entry;
3229                 public Expression ExtensionExpression;
3230                 Argument extension_argument;
3231
3232                 public ExtensionMethodGroupExpr (ArrayList list, NamespaceEntry n, Type extensionType, Location l)
3233                         : base (list, extensionType, l)
3234                 {
3235                         this.namespace_entry = n;
3236                 }
3237
3238                 public override bool IsStatic {
3239                         get { return true; }
3240                 }
3241
3242                 public bool IsTopLevel {
3243                         get { return namespace_entry == null; }
3244                 }
3245
3246                 public override void EmitArguments (EmitContext ec, ArrayList arguments)
3247                 {
3248                         if (arguments == null)
3249                                 arguments = new ArrayList (1);                  
3250                         arguments.Insert (0, extension_argument);
3251                         base.EmitArguments (ec, arguments);
3252                 }
3253
3254                 public override void EmitCall (EmitContext ec, ArrayList arguments)
3255                 {
3256                         if (arguments == null)
3257                                 arguments = new ArrayList (1);
3258                         arguments.Insert (0, extension_argument);
3259                         base.EmitCall (ec, arguments);
3260                 }
3261
3262                 public override MethodGroupExpr OverloadResolve (EmitContext ec, ref ArrayList arguments, bool may_fail, Location loc)
3263                 {
3264                         if (arguments == null)
3265                                 arguments = new ArrayList (1);
3266
3267                         arguments.Insert (0, new Argument (ExtensionExpression));
3268                         MethodGroupExpr mg = ResolveOverloadExtensions (ec, arguments, namespace_entry, loc);
3269
3270                         // Store resolved argument and restore original arguments
3271                         if (mg != null)
3272                                 ((ExtensionMethodGroupExpr)mg).extension_argument = (Argument)arguments [0];
3273                         arguments.RemoveAt (0);
3274
3275                         return mg;
3276                 }
3277
3278                 MethodGroupExpr ResolveOverloadExtensions (EmitContext ec, ArrayList arguments, NamespaceEntry ns, Location loc)
3279                 {
3280                         // Use normal resolve rules
3281                         MethodGroupExpr mg = base.OverloadResolve (ec, ref arguments, ns != null, loc);
3282                         if (mg != null)
3283                                 return mg;
3284
3285                         if (ns == null)
3286                                 return null;
3287
3288                         // Search continues
3289                         ExtensionMethodGroupExpr e = ns.LookupExtensionMethod (type, null, Name, loc);
3290                         if (e == null)
3291                                 return base.OverloadResolve (ec, ref arguments, false, loc);
3292
3293                         e.ExtensionExpression = ExtensionExpression;
3294                         return e.ResolveOverloadExtensions (ec, arguments, e.namespace_entry, loc);
3295                 }               
3296         }
3297
3298         /// <summary>
3299         ///   MethodGroupExpr represents a group of method candidates which
3300         ///   can be resolved to the best method overload
3301         /// </summary>
3302         public class MethodGroupExpr : MemberExpr
3303         {
3304                 public interface IErrorHandler
3305                 {
3306                         bool NoExactMatch (EmitContext ec, MethodBase method);
3307                 }
3308
3309                 public IErrorHandler CustomErrorHandler;                
3310                 public MethodBase [] Methods;
3311                 MethodBase best_candidate;
3312                 // TODO: make private
3313                 public TypeArguments type_arguments;
3314                 bool identical_type_name;
3315                 Type delegate_type;
3316                 
3317                 public MethodGroupExpr (MemberInfo [] mi, Type type, Location l)
3318                         : this (type, l)
3319                 {
3320                         Methods = new MethodBase [mi.Length];
3321                         mi.CopyTo (Methods, 0);
3322                 }
3323
3324                 public MethodGroupExpr (ArrayList list, Type type, Location l)
3325                         : this (type, l)
3326                 {
3327                         try {
3328                                 Methods = (MethodBase[])list.ToArray (typeof (MethodBase));
3329                         } catch {
3330                                 foreach (MemberInfo m in list){
3331                                         if (!(m is MethodBase)){
3332                                                 Console.WriteLine ("Name " + m.Name);
3333                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3334                                         }
3335                                 }
3336                                 throw;
3337                         }
3338
3339
3340                 }
3341
3342                 protected MethodGroupExpr (Type type, Location loc)
3343                 {
3344                         this.loc = loc;
3345                         eclass = ExprClass.MethodGroup;
3346                         this.type = type;
3347                 }
3348
3349                 public override Type DeclaringType {
3350                         get {
3351                                 //
3352                                 // We assume that the top-level type is in the end
3353                                 //
3354                                 return Methods [Methods.Length - 1].DeclaringType;
3355                                 //return Methods [0].DeclaringType;
3356                         }
3357                 }
3358
3359                 public Type DelegateType {
3360                         set {
3361                                 delegate_type = value;
3362                         }
3363                 }
3364
3365                 public bool IdenticalTypeName {
3366                         get {
3367                                 return identical_type_name;
3368                         }
3369
3370                         set {
3371                                 identical_type_name = value;
3372                         }
3373                 }
3374
3375                 public override string GetSignatureForError ()
3376                 {
3377                         if (best_candidate != null)
3378                                 return TypeManager.CSharpSignature (best_candidate);
3379                         
3380                         return TypeManager.CSharpSignature (Methods [0]);
3381                 }
3382
3383                 public override string Name {
3384                         get {
3385                                 return Methods [0].Name;
3386                         }
3387                 }
3388
3389                 public override bool IsInstance {
3390                         get {
3391                                 if (best_candidate != null)
3392                                         return !best_candidate.IsStatic;
3393
3394                                 foreach (MethodBase mb in Methods)
3395                                         if (!mb.IsStatic)
3396                                                 return true;
3397
3398                                 return false;
3399                         }
3400                 }
3401
3402                 public override bool IsStatic {
3403                         get {
3404                                 if (best_candidate != null)
3405                                         return best_candidate.IsStatic;
3406
3407                                 foreach (MethodBase mb in Methods)
3408                                         if (mb.IsStatic)
3409                                                 return true;
3410
3411                                 return false;
3412                         }
3413                 }
3414                 
3415                 public static explicit operator ConstructorInfo (MethodGroupExpr mg)
3416                 {
3417                         return (ConstructorInfo)mg.best_candidate;
3418                 }
3419
3420                 public static explicit operator MethodInfo (MethodGroupExpr mg)
3421                 {
3422                         return (MethodInfo)mg.best_candidate;
3423                 }
3424
3425                 //
3426                 //  7.4.3.3  Better conversion from expression
3427                 //  Returns :   1    if a->p is better,
3428                 //              2    if a->q is better,
3429                 //              0 if neither is better
3430                 //
3431                 static int BetterExpressionConversion (EmitContext ec, Argument a, Type p, Type q)
3432                 {
3433                         Type argument_type = TypeManager.TypeToCoreType (a.Type);
3434                         if (argument_type == TypeManager.anonymous_method_type && RootContext.Version > LanguageVersion.ISO_2) {
3435                                 //
3436                                 // Uwrap delegate from Expression<T>
3437                                 //
3438                                 if (TypeManager.DropGenericTypeArguments (p) == TypeManager.expression_type) {
3439                                         p = TypeManager.GetTypeArguments (p) [0];
3440                                         q = TypeManager.GetTypeArguments (q) [0];
3441                                 }
3442                                 p = Delegate.GetInvokeMethod (null, p).ReturnType;
3443                                 q = Delegate.GetInvokeMethod (null, q).ReturnType;
3444                         } else {
3445                                 if (argument_type == p)
3446                                         return 1;
3447
3448                                 if (argument_type == q)
3449                                         return 2;
3450                         }
3451
3452                         return BetterTypeConversion (ec, p, q);
3453                 }
3454
3455                 //
3456                 // 7.4.3.4  Better conversion from type
3457                 //
3458                 public static int BetterTypeConversion (EmitContext ec, Type p, Type q)
3459                 {
3460                         if (p == null || q == null)
3461                                 throw new InternalErrorException ("BetterTypeConversion got a null conversion");
3462
3463                         if (p == TypeManager.int32_type) {
3464                                 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3465                                         return 1;
3466                         } else if (p == TypeManager.int64_type) {
3467                                 if (q == TypeManager.uint64_type)
3468                                         return 1;
3469                         } else if (p == TypeManager.sbyte_type) {
3470                                 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3471                                         q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3472                                         return 1;
3473                         } else if (p == TypeManager.short_type) {
3474                                 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3475                                         q == TypeManager.uint64_type)
3476                                         return 1;
3477                         }
3478
3479                         if (q == TypeManager.int32_type) {
3480                                 if (p == TypeManager.uint32_type || p == TypeManager.uint64_type)
3481                                         return 2;
3482                         } if (q == TypeManager.int64_type) {
3483                                 if (p == TypeManager.uint64_type)
3484                                         return 2;
3485                         } else if (q == TypeManager.sbyte_type) {
3486                                 if (p == TypeManager.byte_type || p == TypeManager.ushort_type ||
3487                                         p == TypeManager.uint32_type || p == TypeManager.uint64_type)
3488                                         return 2;
3489                         } if (q == TypeManager.short_type) {
3490                                 if (p == TypeManager.ushort_type || p == TypeManager.uint32_type ||
3491                                         p == TypeManager.uint64_type)
3492                                         return 2;
3493                         }
3494
3495                         // TODO: this is expensive
3496                         Expression p_tmp = new EmptyExpression (p);
3497                         Expression q_tmp = new EmptyExpression (q);
3498
3499                         bool p_to_q = Convert.ImplicitConversionExists (ec, p_tmp, q);
3500                         bool q_to_p = Convert.ImplicitConversionExists (ec, q_tmp, p);
3501
3502                         if (p_to_q && !q_to_p)
3503                                 return 1;
3504
3505                         if (q_to_p && !p_to_q)
3506                                 return 2;
3507
3508                         return 0;
3509                 }
3510
3511                 /// <summary>
3512                 ///   Determines "Better function" between candidate
3513                 ///   and the current best match
3514                 /// </summary>
3515                 /// <remarks>
3516                 ///    Returns a boolean indicating :
3517                 ///     false if candidate ain't better
3518                 ///     true  if candidate is better than the current best match
3519                 /// </remarks>
3520                 static bool BetterFunction (EmitContext ec, ArrayList args, int argument_count,
3521                         MethodBase candidate, bool candidate_params,
3522                         MethodBase best, bool best_params)
3523                 {
3524                         ParameterData candidate_pd = TypeManager.GetParameterData (candidate);
3525                         ParameterData best_pd = TypeManager.GetParameterData (best);
3526                 
3527                         bool better_at_least_one = false;
3528                         bool same = true;
3529                         for (int j = 0, c_idx = 0, b_idx = 0; j < argument_count; ++j, ++c_idx, ++b_idx) 
3530                         {
3531                                 Argument a = (Argument) args [j];
3532
3533                                 Type ct = TypeManager.TypeToCoreType (candidate_pd.ParameterType (c_idx));
3534                                 Type bt = TypeManager.TypeToCoreType (best_pd.ParameterType (b_idx));
3535
3536                                 if (candidate_params && candidate_pd.ParameterModifier (c_idx) == Parameter.Modifier.PARAMS) 
3537                                 {
3538                                         ct = TypeManager.GetElementType (ct);
3539                                         --c_idx;
3540                                 }
3541
3542                                 if (best_params && best_pd.ParameterModifier (b_idx) == Parameter.Modifier.PARAMS) 
3543                                 {
3544                                         bt = TypeManager.GetElementType (bt);
3545                                         --b_idx;
3546                                 }
3547
3548                                 if (ct.Equals (bt))
3549                                         continue;
3550
3551                                 same = false;
3552                                 int result = BetterExpressionConversion (ec, a, ct, bt);
3553
3554                                 // for each argument, the conversion to 'ct' should be no worse than 
3555                                 // the conversion to 'bt'.
3556                                 if (result == 2)
3557                                         return false;
3558
3559                                 // for at least one argument, the conversion to 'ct' should be better than 
3560                                 // the conversion to 'bt'.
3561                                 if (result != 0)
3562                                         better_at_least_one = true;
3563                         }
3564
3565                         if (better_at_least_one)
3566                                 return true;
3567
3568                         //
3569                         // This handles the case
3570                         //
3571                         //   Add (float f1, float f2, float f3);
3572                         //   Add (params decimal [] foo);
3573                         //
3574                         // The call Add (3, 4, 5) should be ambiguous.  Without this check, the
3575                         // first candidate would've chosen as better.
3576                         //
3577                         if (!same)
3578                                 return false;
3579
3580                         //
3581                         // The two methods have equal parameter types.  Now apply tie-breaking rules
3582                         //
3583                         if (TypeManager.IsGenericMethod (best) && !TypeManager.IsGenericMethod (candidate))
3584                                 return true;
3585                         if (!TypeManager.IsGenericMethod (best) && TypeManager.IsGenericMethod (candidate))
3586                                 return false;
3587
3588                         //
3589                         // This handles the following cases:
3590                         //
3591                         //   Trim () is better than Trim (params char[] chars)
3592                         //   Concat (string s1, string s2, string s3) is better than
3593                         //     Concat (string s1, params string [] srest)
3594                         //   Foo (int, params int [] rest) is better than Foo (params int [] rest)
3595                         //
3596                         if (!candidate_params && best_params)
3597                                 return true;
3598                         if (candidate_params && !best_params)
3599                                 return false;
3600
3601                         int candidate_param_count = candidate_pd.Count;
3602                         int best_param_count = best_pd.Count;
3603
3604                         if (candidate_param_count != best_param_count)
3605                                 // can only happen if (candidate_params && best_params)
3606                                 return candidate_param_count > best_param_count;
3607
3608                         //
3609                         // now, both methods have the same number of parameters, and the parameters have the same types
3610                         // Pick the "more specific" signature
3611                         //
3612
3613                         MethodBase orig_candidate = TypeManager.DropGenericMethodArguments (candidate);
3614                         MethodBase orig_best = TypeManager.DropGenericMethodArguments (best);
3615
3616                         ParameterData orig_candidate_pd = TypeManager.GetParameterData (orig_candidate);
3617                         ParameterData orig_best_pd = TypeManager.GetParameterData (orig_best);
3618
3619                         bool specific_at_least_once = false;
3620                         for (int j = 0; j < candidate_param_count; ++j) 
3621                         {
3622                                 Type ct = TypeManager.TypeToCoreType (orig_candidate_pd.ParameterType (j));
3623                                 Type bt = TypeManager.TypeToCoreType (orig_best_pd.ParameterType (j));
3624                                 if (ct.Equals (bt))
3625                                         continue;
3626                                 Type specific = MoreSpecific (ct, bt);
3627                                 if (specific == bt)
3628                                         return false;
3629                                 if (specific == ct)
3630                                         specific_at_least_once = true;
3631                         }
3632
3633                         if (specific_at_least_once)
3634                                 return true;
3635
3636                         // FIXME: handle lifted operators
3637                         // ...
3638
3639                         return false;
3640                 }
3641
3642                 protected override MemberExpr ResolveExtensionMemberAccess (Expression left)
3643                 {
3644                         if (!IsStatic)
3645                                 return base.ResolveExtensionMemberAccess (left);
3646
3647                         //
3648                         // When left side is an expression and at least one candidate method is 
3649                         // static, it can be extension method
3650                         //
3651                         InstanceExpression = left;
3652                         return this;
3653                 }
3654
3655                 public override MemberExpr ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
3656                                                                 SimpleName original)
3657                 {
3658                         if (!(left is TypeExpr) &&
3659                             original != null && original.IdenticalNameAndTypeName (ec, left, loc))
3660                                 IdenticalTypeName = true;
3661
3662                         return base.ResolveMemberAccess (ec, left, loc, original);
3663                 }
3664
3665                 public override Expression CreateExpressionTree (EmitContext ec)
3666                 {
3667                         Type t = best_candidate.IsConstructor ?
3668                                 typeof (ConstructorInfo) : typeof (MethodInfo);
3669
3670                         return new Cast (new TypeExpression (t, loc), new TypeOfMethod (best_candidate, loc));
3671                 }
3672                 
3673                 override public Expression DoResolve (EmitContext ec)
3674                 {
3675                         if (InstanceExpression != null) {
3676                                 InstanceExpression = InstanceExpression.DoResolve (ec);
3677                                 if (InstanceExpression == null)
3678                                         return null;
3679                         }
3680
3681                         return this;
3682                 }
3683
3684                 public void ReportUsageError ()
3685                 {
3686                         Report.Error (654, loc, "Method `" + DeclaringType + "." +
3687                                       Name + "()' is referenced without parentheses");
3688                 }
3689
3690                 override public void Emit (EmitContext ec)
3691                 {
3692                         ReportUsageError ();
3693                 }
3694                 
3695                 public virtual void EmitArguments (EmitContext ec, ArrayList arguments)
3696                 {
3697                         Invocation.EmitArguments (ec, arguments, false, null);  
3698                 }
3699                 
3700                 public virtual void EmitCall (EmitContext ec, ArrayList arguments)
3701                 {
3702                         Invocation.EmitCall (ec, IsBase, InstanceExpression, best_candidate, arguments, loc);                   
3703                 }
3704
3705                 protected virtual void Error_InvalidArguments (EmitContext ec, Location loc, int idx, MethodBase method,
3706                                                                                                         Argument a, ParameterData expected_par, Type paramType)
3707                 {
3708                         if (a is CollectionElementInitializer.ElementInitializerArgument) {
3709                                 Report.SymbolRelatedToPreviousError (method);
3710                                 if ((expected_par.ParameterModifier (idx) & Parameter.Modifier.ISBYREF) != 0) {
3711                                         Report.Error (1954, loc, "The best overloaded collection initalizer method `{0}' cannot have 'ref', or `out' modifier",
3712                                                 TypeManager.CSharpSignature (method));
3713                                         return;
3714                                 }
3715                                 Report.Error (1950, loc, "The best overloaded collection initalizer method `{0}' has some invalid arguments",
3716                                           TypeManager.CSharpSignature (method));
3717                         } else if (delegate_type == null) {
3718                                 Report.SymbolRelatedToPreviousError (method);
3719                                 Report.Error (1502, loc, "The best overloaded method match for `{0}' has some invalid arguments",
3720                                                   TypeManager.CSharpSignature (method));
3721                         } else
3722                                 Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
3723                                         TypeManager.CSharpName (delegate_type));
3724
3725                         Parameter.Modifier mod = expected_par.ParameterModifier (idx);
3726
3727                         string index = (idx + 1).ToString ();
3728                         if (((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) ^
3729                                 (a.Modifier & (Parameter.Modifier.REF | Parameter.Modifier.OUT))) != 0) {
3730                                 if ((mod & Parameter.Modifier.ISBYREF) == 0)
3731                                         Report.Error (1615, loc, "Argument `{0}' should not be passed with the `{1}' keyword",
3732                                                 index, Parameter.GetModifierSignature (a.Modifier));
3733                                 else
3734                                         Report.Error (1620, loc, "Argument `{0}' must be passed with the `{1}' keyword",
3735                                                 index, Parameter.GetModifierSignature (mod));
3736                         } else {
3737                                 string p1 = a.GetSignatureForError ();
3738                                 string p2 = TypeManager.CSharpName (paramType);
3739
3740                                 if (p1 == p2) {
3741                                         Report.ExtraInformation (loc, "(equally named types possibly from different assemblies in previous ");
3742                                         Report.SymbolRelatedToPreviousError (a.Expr.Type);
3743                                         Report.SymbolRelatedToPreviousError (paramType);
3744                                 }
3745                                 Report.Error (1503, loc, "Argument {0}: Cannot convert type `{1}' to `{2}'", index, p1, p2);
3746                         }
3747                 }
3748
3749                 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
3750                 {
3751                         Report.Error (428, loc, "Cannot convert method group `{0}' to non-delegate type `{1}'. Consider using parentheses to invoke the method",
3752                                 Name, TypeManager.CSharpName (target));
3753                 }
3754                 
3755                 protected virtual int GetApplicableParametersCount (MethodBase method, ParameterData parameters)
3756                 {
3757                         return parameters.Count;
3758                 }               
3759
3760                 public static bool IsAncestralType (Type first_type, Type second_type)
3761                 {
3762                         return first_type != second_type &&
3763                                 (TypeManager.IsSubclassOf (second_type, first_type) ||
3764                                 TypeManager.ImplementsInterface (second_type, first_type));
3765                 }
3766
3767                 ///
3768                 /// Determines if the candidate method is applicable (section 14.4.2.1)
3769                 /// to the given set of arguments
3770                 /// A return value rates candidate method compatibility,
3771                 /// 0 = the best, int.MaxValue = the worst
3772                 ///
3773                 public int IsApplicable (EmitContext ec,
3774                                                  ArrayList arguments, int arg_count, ref MethodBase method, ref bool params_expanded_form)
3775                 {
3776                         MethodBase candidate = method;
3777
3778                         ParameterData pd = TypeManager.GetParameterData (candidate);
3779                         int param_count = GetApplicableParametersCount (candidate, pd);
3780
3781                         if (arg_count != param_count) {
3782                                 if (!pd.HasParams)
3783                                         return int.MaxValue - 10000 + Math.Abs (arg_count - param_count);
3784                                 if (arg_count < param_count - 1)
3785                                         return int.MaxValue - 10000 + Math.Abs (arg_count - param_count);
3786                         }
3787
3788 #if GMCS_SOURCE
3789                         //
3790                         // 1. Handle generic method using type arguments when specified or type inference
3791                         //
3792                         if (TypeManager.IsGenericMethod (candidate)) {
3793                                 if (type_arguments != null) {
3794                                         Type [] g_args = candidate.GetGenericArguments ();
3795                                         if (g_args.Length != type_arguments.Count)
3796                                                 return int.MaxValue - 20000 + Math.Abs (type_arguments.Count - g_args.Length);
3797
3798                                         // TODO: Don't create new method, create Parameters only
3799                                         method = ((MethodInfo) candidate).MakeGenericMethod (type_arguments.Arguments);
3800                                         candidate = method;
3801                                         pd = TypeManager.GetParameterData (candidate);
3802                                 } else {
3803                                         int score = TypeManager.InferTypeArguments (ec, arguments, ref candidate);
3804                                         if (score != 0)
3805                                                 return score - 20000;
3806
3807                                         if (TypeManager.IsGenericMethodDefinition (candidate))
3808                                                 throw new InternalErrorException ("A generic method `{0}' definition took part in overload resolution",
3809                                                         TypeManager.CSharpSignature (candidate));
3810
3811                                         pd = TypeManager.GetParameterData (candidate);
3812                                 }
3813                         } else {
3814                                 if (type_arguments != null)
3815                                         return int.MaxValue - 15000;
3816                         }
3817 #endif                  
3818
3819                         //
3820                         // 2. Each argument has to be implicitly convertible to method parameter
3821                         //
3822                         method = candidate;
3823                         Parameter.Modifier p_mod = 0;
3824                         Type pt = null;
3825                         for (int i = 0; i < arg_count; i++) {
3826                                 Argument a = (Argument) arguments [i];
3827                                 Parameter.Modifier a_mod = a.Modifier &
3828                                         ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
3829
3830                                 if (p_mod != Parameter.Modifier.PARAMS) {
3831                                         p_mod = pd.ParameterModifier (i) & ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
3832
3833                                         if (p_mod == Parameter.Modifier.ARGLIST) {
3834                                                 if (a.Type == TypeManager.runtime_argument_handle_type)
3835                                                         continue;
3836
3837                                                 p_mod = 0;
3838                                         }
3839
3840                                         pt = pd.ParameterType (i);
3841                                 } else {
3842                                         params_expanded_form = true;
3843                                 }
3844
3845                                 int score = 1;
3846                                 if (!params_expanded_form)
3847                                         score = IsArgumentCompatible (ec, a_mod, a, p_mod & ~Parameter.Modifier.PARAMS, pt);
3848
3849                                 if (score != 0 && (p_mod & Parameter.Modifier.PARAMS) != 0) {
3850                                         // It can be applicable in expanded form
3851                                         score = IsArgumentCompatible (ec, a_mod, a, 0, pt.GetElementType ());
3852                                         if (score == 0)
3853                                                 params_expanded_form = true;
3854                                 }
3855
3856                                 if (score != 0) {
3857                                         if (params_expanded_form)
3858                                                 ++score;
3859                                         return (arg_count - i) * 2 + score;
3860                                 }
3861                         }
3862                         
3863                         if (arg_count != param_count)
3864                                 params_expanded_form = true;                    
3865                         
3866                         return 0;
3867                 }
3868
3869                 int IsArgumentCompatible (EmitContext ec, Parameter.Modifier arg_mod, Argument argument, Parameter.Modifier param_mod, Type parameter)
3870                 {
3871                         //
3872                         // Types have to be identical when ref or out modifer is used 
3873                         //
3874                         if (arg_mod != 0 || param_mod != 0) {
3875                                 if (TypeManager.HasElementType (parameter))
3876                                         parameter = parameter.GetElementType ();
3877
3878                                 Type a_type = argument.Type;
3879                                 if (TypeManager.HasElementType (a_type))
3880                                         a_type = a_type.GetElementType ();
3881
3882                                 if (a_type != parameter)
3883                                         return 2;
3884
3885                                 return 0;
3886                         }
3887
3888                         // FIXME: Kill this abomination (EmitContext.TempEc)
3889                         EmitContext prevec = EmitContext.TempEc;
3890                         EmitContext.TempEc = ec;
3891                         try {
3892                                 if (delegate_type != null ?
3893                                         !Delegate.IsTypeCovariant (argument.Expr, parameter) :
3894                                         !Convert.ImplicitConversionExists (ec, argument.Expr, parameter))
3895                                         return 2;
3896
3897                                 if (arg_mod != param_mod)
3898                                         return 1;
3899
3900                         } finally {
3901                                 EmitContext.TempEc = prevec;
3902                         }
3903
3904                         return 0;
3905                 }
3906
3907                 public static bool IsOverride (MethodBase cand_method, MethodBase base_method)
3908                 {
3909                         if (!IsAncestralType (base_method.DeclaringType, cand_method.DeclaringType))
3910                                 return false;
3911
3912                         ParameterData cand_pd = TypeManager.GetParameterData (cand_method);
3913                         ParameterData base_pd = TypeManager.GetParameterData (base_method);
3914                 
3915                         if (cand_pd.Count != base_pd.Count)
3916                                 return false;
3917
3918                         for (int j = 0; j < cand_pd.Count; ++j) 
3919                         {
3920                                 Parameter.Modifier cm = cand_pd.ParameterModifier (j);
3921                                 Parameter.Modifier bm = base_pd.ParameterModifier (j);
3922                                 Type ct = TypeManager.TypeToCoreType (cand_pd.ParameterType (j));
3923                                 Type bt = TypeManager.TypeToCoreType (base_pd.ParameterType (j));
3924
3925                                 if (cm != bm || ct != bt)
3926                                         return false;
3927                         }
3928
3929                         return true;
3930                 }
3931                 
3932                 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2, Location loc)
3933                 {
3934                         MemberInfo [] miset;
3935                         MethodGroupExpr union;
3936
3937                         if (mg1 == null) {
3938                                 if (mg2 == null)
3939                                         return null;
3940                                 return (MethodGroupExpr) mg2;
3941                         } else {
3942                                 if (mg2 == null)
3943                                         return (MethodGroupExpr) mg1;
3944                         }
3945                         
3946                         MethodGroupExpr left_set = null, right_set = null;
3947                         int length1 = 0, length2 = 0;
3948                         
3949                         left_set = (MethodGroupExpr) mg1;
3950                         length1 = left_set.Methods.Length;
3951                         
3952                         right_set = (MethodGroupExpr) mg2;
3953                         length2 = right_set.Methods.Length;
3954                         
3955                         ArrayList common = new ArrayList ();
3956
3957                         foreach (MethodBase r in right_set.Methods){
3958                                 if (TypeManager.ArrayContainsMethod (left_set.Methods, r))
3959                                         common.Add (r);
3960                         }
3961
3962                         miset = new MemberInfo [length1 + length2 - common.Count];
3963                         left_set.Methods.CopyTo (miset, 0);
3964                         
3965                         int k = length1;
3966
3967                         foreach (MethodBase r in right_set.Methods) {
3968                                 if (!common.Contains (r))
3969                                         miset [k++] = r;
3970                         }
3971
3972                         union = new MethodGroupExpr (miset, mg1.Type, loc);
3973                         
3974                         return union;
3975                 }               
3976
3977                 static Type MoreSpecific (Type p, Type q)
3978                 {
3979                         if (TypeManager.IsGenericParameter (p) && !TypeManager.IsGenericParameter (q))
3980                                 return q;
3981                         if (!TypeManager.IsGenericParameter (p) && TypeManager.IsGenericParameter (q))
3982                                 return p;
3983
3984                         if (TypeManager.HasElementType (p)) 
3985                         {
3986                                 Type pe = TypeManager.GetElementType (p);
3987                                 Type qe = TypeManager.GetElementType (q);
3988                                 Type specific = MoreSpecific (pe, qe);
3989                                 if (specific == pe)
3990                                         return p;
3991                                 if (specific == qe)
3992                                         return q;
3993                         } 
3994                         else if (TypeManager.IsGenericType (p)) 
3995                         {
3996                                 Type[] pargs = TypeManager.GetTypeArguments (p);
3997                                 Type[] qargs = TypeManager.GetTypeArguments (q);
3998
3999                                 bool p_specific_at_least_once = false;
4000                                 bool q_specific_at_least_once = false;
4001
4002                                 for (int i = 0; i < pargs.Length; i++) 
4003                                 {
4004                                         Type specific = MoreSpecific (pargs [i], qargs [i]);
4005                                         if (specific == pargs [i])
4006                                                 p_specific_at_least_once = true;
4007                                         if (specific == qargs [i])
4008                                                 q_specific_at_least_once = true;
4009                                 }
4010
4011                                 if (p_specific_at_least_once && !q_specific_at_least_once)
4012                                         return p;
4013                                 if (!p_specific_at_least_once && q_specific_at_least_once)
4014                                         return q;
4015                         }
4016
4017                         return null;
4018                 }
4019
4020                 /// <summary>
4021                 ///   Find the Applicable Function Members (7.4.2.1)
4022                 ///
4023                 ///   me: Method Group expression with the members to select.
4024                 ///       it might contain constructors or methods (or anything
4025                 ///       that maps to a method).
4026                 ///
4027                 ///   Arguments: ArrayList containing resolved Argument objects.
4028                 ///
4029                 ///   loc: The location if we want an error to be reported, or a Null
4030                 ///        location for "probing" purposes.
4031                 ///
4032                 ///   Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4033                 ///            that is the best match of me on Arguments.
4034                 ///
4035                 /// </summary>
4036                 public virtual MethodGroupExpr OverloadResolve (EmitContext ec, ref ArrayList Arguments,
4037                         bool may_fail, Location loc)
4038                 {
4039                         bool method_params = false;
4040                         Type applicable_type = null;
4041                         int arg_count = 0;
4042                         ArrayList candidates = new ArrayList (2);
4043                         ArrayList candidate_overrides = null;
4044
4045                         //
4046                         // Used to keep a map between the candidate
4047                         // and whether it is being considered in its
4048                         // normal or expanded form
4049                         //
4050                         // false is normal form, true is expanded form
4051                         //
4052                         Hashtable candidate_to_form = null;
4053
4054                         if (Arguments != null)
4055                                 arg_count = Arguments.Count;
4056
4057                         if (RootContext.Version == LanguageVersion.ISO_1 && Name == "Invoke" && TypeManager.IsDelegateType (DeclaringType)) {
4058                                 if (!may_fail)
4059                                         Report.Error (1533, loc, "Invoke cannot be called directly on a delegate");
4060                                 return null;
4061                         }
4062
4063                         int nmethods = Methods.Length;
4064
4065                         if (!IsBase) {
4066                                 //
4067                                 // Methods marked 'override' don't take part in 'applicable_type'
4068                                 // computation, nor in the actual overload resolution.
4069                                 // However, they still need to be emitted instead of a base virtual method.
4070                                 // So, we salt them away into the 'candidate_overrides' array.
4071                                 //
4072                                 // In case of reflected methods, we replace each overriding method with
4073                                 // its corresponding base virtual method.  This is to improve compatibility
4074                                 // with non-C# libraries which change the visibility of overrides (#75636)
4075                                 //
4076                                 int j = 0;
4077                                 for (int i = 0; i < Methods.Length; ++i) {
4078                                         MethodBase m = Methods [i];
4079                                         if (TypeManager.IsOverride (m)) {
4080                                                 if (candidate_overrides == null)
4081                                                         candidate_overrides = new ArrayList ();
4082                                                 candidate_overrides.Add (m);
4083                                                 m = TypeManager.TryGetBaseDefinition (m);
4084                                         }
4085                                         if (m != null)
4086                                                 Methods [j++] = m;
4087                                 }
4088                                 nmethods = j;
4089                         }
4090
4091                         //
4092                         // Enable message recording, it's used mainly by lambda expressions
4093                         //
4094                         Report.IMessageRecorder msg_recorder = new Report.MessageRecorder ();
4095                         Report.IMessageRecorder prev_recorder = Report.SetMessageRecorder (msg_recorder);
4096
4097                         //
4098                         // First we construct the set of applicable methods
4099                         //
4100                         bool is_sorted = true;
4101                         int best_candidate_rate = int.MaxValue;
4102                         for (int i = 0; i < nmethods; i++) {
4103                                 Type decl_type = Methods [i].DeclaringType;
4104
4105                                 //
4106                                 // If we have already found an applicable method
4107                                 // we eliminate all base types (Section 14.5.5.1)
4108                                 //
4109                                 if (applicable_type != null && IsAncestralType (decl_type, applicable_type))
4110                                         continue;
4111
4112                                 //
4113                                 // Check if candidate is applicable (section 14.4.2.1)
4114                                 //
4115                                 bool params_expanded_form = false;
4116                                 int candidate_rate = IsApplicable (ec, Arguments, arg_count, ref Methods [i], ref params_expanded_form);
4117
4118                                 if (candidate_rate < best_candidate_rate) {
4119                                         best_candidate_rate = candidate_rate;
4120                                         best_candidate = Methods [i];
4121                                 }
4122                                 
4123                                 if (params_expanded_form) {
4124                                         if (candidate_to_form == null)
4125                                                 candidate_to_form = new PtrHashtable ();
4126                                         MethodBase candidate = Methods [i];
4127                                         candidate_to_form [candidate] = candidate;
4128                                 }
4129
4130                                 if (candidate_rate != 0) {
4131                                         if (msg_recorder != null)
4132                                                 msg_recorder.EndSession ();
4133                                         continue;
4134                                 }
4135
4136                                 msg_recorder = null;
4137                                 candidates.Add (Methods [i]);
4138
4139                                 if (applicable_type == null)
4140                                         applicable_type = decl_type;
4141                                 else if (applicable_type != decl_type) {
4142                                         is_sorted = false;
4143                                         if (IsAncestralType (applicable_type, decl_type))
4144                                                 applicable_type = decl_type;
4145                                 }
4146                         }
4147
4148                         Report.SetMessageRecorder (prev_recorder);
4149                         if (msg_recorder != null && msg_recorder.PrintMessages ())
4150                                 return null;
4151                         
4152                         int candidate_top = candidates.Count;
4153
4154                         if (applicable_type == null) {
4155                                 //
4156                                 // When we found a top level method which does not match and it's 
4157                                 // not an extension method. We start extension methods lookup from here
4158                                 //
4159                                 if (InstanceExpression != null) {
4160                                         ExtensionMethodGroupExpr ex_method_lookup = ec.TypeContainer.LookupExtensionMethod (type, Name, loc);
4161                                         if (ex_method_lookup != null) {
4162                                                 ex_method_lookup.ExtensionExpression = InstanceExpression;
4163                                                 ex_method_lookup.SetTypeArguments (type_arguments);
4164                                                 return ex_method_lookup.OverloadResolve (ec, ref Arguments, may_fail, loc);
4165                                         }
4166                                 }
4167                                 
4168                                 if (may_fail)
4169                                         return null;
4170
4171                                 //
4172                                 // Okay so we have failed to find exact match so we
4173                                 // return error info about the closest match
4174                                 //
4175                                 if (best_candidate != null) {
4176                                         if (CustomErrorHandler != null) {
4177                                                 if (CustomErrorHandler.NoExactMatch (ec, best_candidate))
4178                                                         return null;
4179                                         }
4180
4181                                         ParameterData pd = TypeManager.GetParameterData (best_candidate);
4182                                         bool cand_params = candidate_to_form != null && candidate_to_form.Contains (best_candidate);
4183                                         if (arg_count == pd.Count || pd.HasParams) {
4184                                                 if (TypeManager.IsGenericMethodDefinition (best_candidate)) {
4185                                                         if (type_arguments == null) {
4186                                                                 Report.Error (411, loc,
4187                                                                         "The type arguments for method `{0}' cannot be inferred from " +
4188                                                                         "the usage. Try specifying the type arguments explicitly",
4189                                                                         TypeManager.CSharpSignature (best_candidate));
4190                                                                 return null;
4191                                                         }
4192                                                                 
4193                                                         Type [] g_args = TypeManager.GetGenericArguments (best_candidate);
4194                                                         if (type_arguments.Count != g_args.Length) {
4195                                                                 Report.SymbolRelatedToPreviousError (best_candidate);
4196                                                                 Report.Error (305, loc, "Using the generic method `{0}' requires `{1}' type argument(s)",
4197                                                                         TypeManager.CSharpSignature (best_candidate),
4198                                                                         g_args.Length.ToString ());
4199                                                                 return null;
4200                                                         }
4201                                                 } else {
4202                                                         if (type_arguments != null && !TypeManager.IsGenericMethod (best_candidate)) {
4203                                                                 Namespace.Error_TypeArgumentsCannotBeUsed (best_candidate, loc);
4204                                                                 return null;
4205                                                         }
4206                                                 }
4207                                                 
4208                                                 if (!VerifyArgumentsCompat (ec, ref Arguments, arg_count, best_candidate, cand_params, may_fail, loc))
4209                                                         return null;
4210                                         }
4211                                 }
4212
4213                                 if (almost_matched_members.Count != 0) {
4214                                         Error_MemberLookupFailed (ec.ContainerType, type, type, ".ctor",
4215                                         null, MemberTypes.Constructor, AllBindingFlags);
4216                                         return null;
4217                                 }
4218                                 
4219                                 //
4220                                 // We failed to find any method with correct argument count
4221                                 //
4222                                 if (Name == ConstructorInfo.ConstructorName) {
4223                                         Report.SymbolRelatedToPreviousError (type);
4224                                         Report.Error (1729, loc,
4225                                                 "The type `{0}' does not contain a constructor that takes `{1}' arguments",
4226                                                 TypeManager.CSharpName (type), arg_count);
4227                                 } else {
4228                                         Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
4229                                                 Name, arg_count.ToString ());
4230                                 }
4231                                 
4232                                 return null;
4233                         }
4234
4235                         if (!is_sorted) {
4236                                 //
4237                                 // At this point, applicable_type is _one_ of the most derived types
4238                                 // in the set of types containing the methods in this MethodGroup.
4239                                 // Filter the candidates so that they only contain methods from the
4240                                 // most derived types.
4241                                 //
4242
4243                                 int finalized = 0; // Number of finalized candidates
4244
4245                                 do {
4246                                         // Invariant: applicable_type is a most derived type
4247                                         
4248                                         // We'll try to complete Section 14.5.5.1 for 'applicable_type' by 
4249                                         // eliminating all it's base types.  At the same time, we'll also move
4250                                         // every unrelated type to the end of the array, and pick the next
4251                                         // 'applicable_type'.
4252
4253                                         Type next_applicable_type = null;
4254                                         int j = finalized; // where to put the next finalized candidate
4255                                         int k = finalized; // where to put the next undiscarded candidate
4256                                         for (int i = finalized; i < candidate_top; ++i) {
4257                                                 MethodBase candidate = (MethodBase) candidates [i];
4258                                                 Type decl_type = candidate.DeclaringType;
4259
4260                                                 if (decl_type == applicable_type) {
4261                                                         candidates [k++] = candidates [j];
4262                                                         candidates [j++] = candidates [i];
4263                                                         continue;
4264                                                 }
4265
4266                                                 if (IsAncestralType (decl_type, applicable_type))
4267                                                         continue;
4268
4269                                                 if (next_applicable_type != null &&
4270                                                         IsAncestralType (decl_type, next_applicable_type))
4271                                                         continue;
4272
4273                                                 candidates [k++] = candidates [i];
4274
4275                                                 if (next_applicable_type == null ||
4276                                                         IsAncestralType (next_applicable_type, decl_type))
4277                                                         next_applicable_type = decl_type;
4278                                         }
4279
4280                                         applicable_type = next_applicable_type;
4281                                         finalized = j;
4282                                         candidate_top = k;
4283                                 } while (applicable_type != null);
4284                         }
4285
4286                         //
4287                         // Now we actually find the best method
4288                         //
4289
4290                         best_candidate = (MethodBase) candidates [0];
4291                         if (delegate_type == null)
4292                                 method_params = candidate_to_form != null && candidate_to_form.Contains (best_candidate);
4293
4294                         for (int ix = 1; ix < candidate_top; ix++) {
4295                                 MethodBase candidate = (MethodBase) candidates [ix];
4296
4297                                 if (candidate == best_candidate)
4298                                         continue;
4299
4300                                 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
4301
4302                                 if (BetterFunction (ec, Arguments, arg_count, 
4303                                         candidate, cand_params,
4304                                         best_candidate, method_params)) {
4305                                         best_candidate = candidate;
4306                                         method_params = cand_params;
4307                                 }
4308                         }
4309                         //
4310                         // Now check that there are no ambiguities i.e the selected method
4311                         // should be better than all the others
4312                         //
4313                         MethodBase ambiguous = null;
4314                         for (int ix = 1; ix < candidate_top; ix++) {
4315                                 MethodBase candidate = (MethodBase) candidates [ix];
4316
4317                                 if (candidate == best_candidate)
4318                                         continue;
4319
4320                                 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
4321                                 if (!BetterFunction (ec, Arguments, arg_count,
4322                                         best_candidate, method_params,
4323                                         candidate, cand_params)) 
4324                                 {
4325                                         if (!may_fail)
4326                                                 Report.SymbolRelatedToPreviousError (candidate);
4327                                         ambiguous = candidate;
4328                                 }
4329                         }
4330
4331                         if (ambiguous != null) {
4332                                 Report.SymbolRelatedToPreviousError (best_candidate);
4333                                 Report.Error (121, loc, "The call is ambiguous between the following methods or properties: `{0}' and `{1}'",
4334                                         TypeManager.CSharpSignature (ambiguous), TypeManager.CSharpSignature (best_candidate));
4335                                 return this;
4336                         }
4337
4338                         //
4339                         // If the method is a virtual function, pick an override closer to the LHS type.
4340                         //
4341                         if (!IsBase && best_candidate.IsVirtual) {
4342                                 if (TypeManager.IsOverride (best_candidate))
4343                                         throw new InternalErrorException (
4344                                                 "Should not happen.  An 'override' method took part in overload resolution: " + best_candidate);
4345
4346                                 if (candidate_overrides != null) {
4347                                         Type[] gen_args = null;
4348                                         bool gen_override = false;
4349                                         if (TypeManager.IsGenericMethod (best_candidate))
4350                                                 gen_args = TypeManager.GetGenericArguments (best_candidate);
4351
4352                                         foreach (MethodBase candidate in candidate_overrides) {
4353                                                 if (TypeManager.IsGenericMethod (candidate)) {
4354                                                         if (gen_args == null)
4355                                                                 continue;
4356
4357                                                         if (gen_args.Length != TypeManager.GetGenericArguments (candidate).Length)
4358                                                                 continue;
4359                                                 } else {
4360                                                         if (gen_args != null)
4361                                                                 continue;
4362                                                 }
4363                                                 
4364                                                 if (IsOverride (candidate, best_candidate)) {
4365                                                         gen_override = true;
4366                                                         best_candidate = candidate;
4367                                                 }
4368                                         }
4369
4370                                         if (gen_override && gen_args != null) {
4371 #if GMCS_SOURCE
4372                                                 best_candidate = ((MethodInfo) best_candidate).MakeGenericMethod (gen_args);
4373 #endif                                          
4374                                         }
4375                                 }
4376                         }
4377
4378                         //
4379                         // And now check if the arguments are all
4380                         // compatible, perform conversions if
4381                         // necessary etc. and return if everything is
4382                         // all right
4383                         //
4384                         if (!VerifyArgumentsCompat (ec, ref Arguments, arg_count, best_candidate,
4385                                 method_params, may_fail, loc))
4386                                 return null;
4387
4388                         if (best_candidate == null)
4389                                 return null;
4390
4391                         MethodBase the_method = TypeManager.DropGenericMethodArguments (best_candidate);
4392 #if GMCS_SOURCE
4393                         if (the_method.IsGenericMethodDefinition &&
4394                             !ConstraintChecker.CheckConstraints (ec, the_method, best_candidate, loc))
4395                                 return null;
4396 #endif
4397
4398                         IMethodData data = TypeManager.GetMethod (the_method);
4399                         if (data != null)
4400                                 data.SetMemberIsUsed ();
4401
4402                         return this;
4403                 }
4404                 
4405                 public override void SetTypeArguments (TypeArguments ta)
4406                 {
4407                         type_arguments = ta;
4408                 }
4409
4410                 public bool VerifyArgumentsCompat (EmitContext ec, ref ArrayList arguments,
4411                                                           int arg_count, MethodBase method,
4412                                                           bool chose_params_expanded,
4413                                                           bool may_fail, Location loc)
4414                 {
4415                         ParameterData pd = TypeManager.GetParameterData (method);
4416
4417                         int errors = Report.Errors;
4418                         Parameter.Modifier p_mod = 0;
4419                         Type pt = null;
4420                         int a_idx = 0, a_pos = 0;
4421                         Argument a = null;
4422                         ArrayList params_initializers = null;
4423
4424                         for (; a_idx < arg_count; a_idx++, ++a_pos) {
4425                                 a = (Argument) arguments [a_idx];
4426                                 if (p_mod != Parameter.Modifier.PARAMS) {
4427                                         p_mod = pd.ParameterModifier (a_idx);
4428                                         pt = pd.ParameterType (a_idx);
4429
4430                                         if (p_mod == Parameter.Modifier.ARGLIST) {
4431                                                 if (a.Type != TypeManager.runtime_argument_handle_type)
4432                                                         break;
4433                                                 continue;
4434                                         }
4435
4436                                         if (pt.IsPointer && !ec.InUnsafe) {
4437                                                 if (may_fail)
4438                                                         return false;
4439
4440                                                 UnsafeError (loc);
4441                                         }
4442
4443                                         if (p_mod == Parameter.Modifier.PARAMS) {
4444                                                 if (chose_params_expanded) {
4445                                                         params_initializers = new ArrayList (arg_count - a_idx);
4446                                                         pt = TypeManager.GetElementType (pt);
4447                                                 }
4448                                         } else if (p_mod != 0) {
4449                                                 pt = TypeManager.GetElementType (pt);
4450                                         }
4451                                 }
4452
4453                                 //
4454                                 // Types have to be identical when ref or out modifer is used 
4455                                 //
4456                                 if (a.Modifier != 0 || (p_mod & ~Parameter.Modifier.PARAMS) != 0) {
4457                                         if ((p_mod & ~Parameter.Modifier.PARAMS) != a.Modifier)
4458                                                 break;
4459
4460                                         if (!TypeManager.IsEqual (a.Expr.Type, pt))
4461                                                 break;
4462
4463                                         continue;
4464                                 }
4465                 
4466                                 Expression conv;
4467                                 if (TypeManager.IsEqual (a.Type, pt)) {
4468                                         conv = a.Expr;
4469                                 } else {
4470                                         conv = Convert.ImplicitConversion (ec, a.Expr, pt, loc);
4471                                         if (conv == null)
4472                                                 break;
4473                                 }
4474
4475                                 //
4476                                 // Convert params arguments to an array initializer
4477                                 //
4478                                 if (params_initializers != null) {
4479                                         params_initializers.Add (conv);
4480                                         arguments.RemoveAt (a_idx--);
4481                                         --arg_count;
4482                                         continue;
4483                                 }
4484                                 
4485                                 // Update the argument with the implicit conversion
4486                                 a.Expr = conv;
4487                         }
4488
4489                         //
4490                         // Fill not provided arguments required by params modifier
4491                         //
4492                         if (params_initializers == null && pd.HasParams && arg_count < pd.Count && a_idx + 1 == pd.Count) {
4493                                 if (arguments == null)
4494                                         arguments = new ArrayList (1);
4495
4496                                 pt = pd.Types [GetApplicableParametersCount (method, pd) - 1];
4497                                 pt = TypeManager.GetElementType (pt);
4498                                 params_initializers = new ArrayList (0);
4499                         }
4500
4501                         if (a_idx == arg_count) {
4502                                 //
4503                                 // Append an array argument with all params arguments
4504                                 //
4505                                 if (params_initializers != null) {
4506                                         arguments.Add (new Argument (
4507                                                 new ArrayCreation (new TypeExpression (pt, loc), "[]",
4508                                                 params_initializers, loc).Resolve (ec)));
4509                                 }
4510                                 return true;
4511                         }
4512
4513                         if (!may_fail && Report.Errors == errors) {
4514                                 if (CustomErrorHandler != null)
4515                                         CustomErrorHandler.NoExactMatch (ec, best_candidate);
4516                                 else
4517                                         Error_InvalidArguments (ec, loc, a_pos, method, a, pd, pt);
4518                         }
4519                         return false;
4520                 }
4521         }
4522
4523         public class ConstantExpr : MemberExpr
4524         {
4525                 FieldInfo constant;
4526
4527                 public ConstantExpr (FieldInfo constant, Location loc)
4528                 {
4529                         this.constant = constant;
4530                         this.loc = loc;
4531                 }
4532
4533                 public override string Name {
4534                         get { throw new NotImplementedException (); }
4535                 }
4536
4537                 public override bool IsInstance {
4538                         get { return !IsStatic; }
4539                 }
4540
4541                 public override bool IsStatic {
4542                         get { return constant.IsStatic; }
4543                 }
4544
4545                 public override Type DeclaringType {
4546                         get { return constant.DeclaringType; }
4547                 }
4548
4549                 public override MemberExpr ResolveMemberAccess (EmitContext ec, Expression left, Location loc, SimpleName original)
4550                 {
4551                         constant = TypeManager.GetGenericFieldDefinition (constant);
4552
4553                         IConstant ic = TypeManager.GetConstant (constant);
4554                         if (ic == null) {
4555                                 if (constant.IsLiteral) {
4556                                         ic = new ExternalConstant (constant);
4557                                 } else {
4558                                         ic = ExternalConstant.CreateDecimal (constant);
4559                                         // HACK: decimal field was not resolved as constant
4560                                         if (ic == null)
4561                                                 return new FieldExpr (constant, loc).ResolveMemberAccess (ec, left, loc, original);
4562                                 }
4563                                 TypeManager.RegisterConstant (constant, ic);
4564                         }
4565
4566                         return base.ResolveMemberAccess (ec, left, loc, original);
4567                 }
4568
4569                 public override Expression CreateExpressionTree (EmitContext ec)
4570                 {
4571                         throw new NotSupportedException ();
4572                 }
4573
4574                 public override Expression DoResolve (EmitContext ec)
4575                 {
4576                         IConstant ic = TypeManager.GetConstant (constant);
4577                         if (ic.ResolveValue ()) {
4578                                 if (!ec.IsInObsoleteScope)
4579                                         ic.CheckObsoleteness (loc);
4580                         }
4581
4582                         return ic.CreateConstantReference (loc);
4583                 }
4584
4585                 public override void Emit (EmitContext ec)
4586                 {
4587                         throw new NotSupportedException ();
4588                 }
4589
4590                 public override string GetSignatureForError ()
4591                 {
4592                         return TypeManager.GetFullNameSignature (constant);
4593                 }
4594         }
4595
4596         /// <summary>
4597         ///   Fully resolved expression that evaluates to a Field
4598         /// </summary>
4599         public class FieldExpr : MemberExpr, IAssignMethod, IMemoryLocation, IVariable {
4600                 public readonly FieldInfo FieldInfo;
4601                 VariableInfo variable_info;
4602                 
4603                 LocalTemporary temp;
4604                 bool prepared;
4605                 bool in_initializer;
4606
4607                 public FieldExpr (FieldInfo fi, Location l, bool in_initializer):
4608                         this (fi, l)
4609                 {
4610                         this.in_initializer = in_initializer;
4611                 }
4612                 
4613                 public FieldExpr (FieldInfo fi, Location l)
4614                 {
4615                         FieldInfo = fi;
4616                         eclass = ExprClass.Variable;
4617                         type = TypeManager.TypeToCoreType (fi.FieldType);
4618                         loc = l;
4619                 }
4620
4621                 public override string Name {
4622                         get {
4623                                 return FieldInfo.Name;
4624                         }
4625                 }
4626
4627                 public override bool IsInstance {
4628                         get {
4629                                 return !FieldInfo.IsStatic;
4630                         }
4631                 }
4632
4633                 public override bool IsStatic {
4634                         get {
4635                                 return FieldInfo.IsStatic;
4636                         }
4637                 }
4638
4639                 public override Type DeclaringType {
4640                         get {
4641                                 return FieldInfo.DeclaringType;
4642                         }
4643                 }
4644
4645                 public override string GetSignatureForError ()
4646                 {
4647                         return TypeManager.GetFullNameSignature (FieldInfo);
4648                 }
4649
4650                 public VariableInfo VariableInfo {
4651                         get {
4652                                 return variable_info;
4653                         }
4654                 }
4655
4656                 public override MemberExpr ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
4657                                                                 SimpleName original)
4658                 {
4659                         FieldInfo fi = TypeManager.GetGenericFieldDefinition (FieldInfo);
4660                         Type t = fi.FieldType;
4661
4662                         if (t.IsPointer && !ec.InUnsafe) {
4663                                 UnsafeError (loc);
4664                         }
4665
4666                         return base.ResolveMemberAccess (ec, left, loc, original);
4667                 }
4668
4669                 public override Expression CreateExpressionTree (EmitContext ec)
4670                 {
4671                         Expression instance;
4672                         if (InstanceExpression == null) {
4673                                 instance = new NullLiteral (loc);
4674                         } else {
4675                                 instance = InstanceExpression.CreateExpressionTree (ec);
4676                         }
4677
4678                         ArrayList args = new ArrayList (2);
4679                         args.Add (new Argument (instance));
4680                         args.Add (new Argument (CreateTypeOfExpression ()));
4681                         return CreateExpressionFactoryCall ("Field", args);
4682                 }
4683
4684                 public Expression CreateTypeOfExpression ()
4685                 {
4686                         return new TypeOfField (FieldInfo, loc);
4687                 }
4688
4689                 override public Expression DoResolve (EmitContext ec)
4690                 {
4691                         return DoResolve (ec, false, false);
4692                 }
4693
4694                 Expression DoResolve (EmitContext ec, bool lvalue_instance, bool out_access)
4695                 {
4696                         if (!FieldInfo.IsStatic){
4697                                 if (InstanceExpression == null){
4698                                         //
4699                                         // This can happen when referencing an instance field using
4700                                         // a fully qualified type expression: TypeName.InstanceField = xxx
4701                                         // 
4702                                         SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
4703                                         return null;
4704                                 }
4705
4706                                 // Resolve the field's instance expression while flow analysis is turned
4707                                 // off: when accessing a field "a.b", we must check whether the field
4708                                 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4709
4710                                 if (lvalue_instance) {
4711                                         using (ec.With (EmitContext.Flags.DoFlowAnalysis, false)) {
4712                                                 Expression right_side =
4713                                                         out_access ? EmptyExpression.LValueMemberOutAccess : EmptyExpression.LValueMemberAccess;
4714                                                 InstanceExpression = InstanceExpression.ResolveLValue (ec, right_side, loc);
4715                                         }
4716                                 } else {
4717                                         ResolveFlags rf = ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis;
4718                                         InstanceExpression = InstanceExpression.Resolve (ec, rf);
4719                                 }
4720
4721                                 if (InstanceExpression == null)
4722                                         return null;
4723
4724                                 using (ec.Set (EmitContext.Flags.OmitStructFlowAnalysis)) {
4725                                         InstanceExpression.CheckMarshalByRefAccess (ec);
4726                                 }
4727                         }
4728
4729                         if (!in_initializer && !ec.IsInFieldInitializer) {
4730                                 ObsoleteAttribute oa;
4731                                 FieldBase f = TypeManager.GetField (FieldInfo);
4732                                 if (f != null) {
4733                                         if (!ec.IsInObsoleteScope)
4734                                                 f.CheckObsoleteness (loc);
4735                                 
4736                                         // To be sure that type is external because we do not register generated fields
4737                                 } else if (!(FieldInfo.DeclaringType is TypeBuilder)) {                                
4738                                         oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo);
4739                                         if (oa != null)
4740                                                 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc);
4741                                 }
4742                         }
4743
4744                         AnonymousContainer am = ec.CurrentAnonymousMethod;
4745                         if (am != null){
4746                                 if (!FieldInfo.IsStatic){
4747                                         if (!am.IsIterator && (ec.TypeContainer is Struct)){
4748                                                 Report.Error (1673, loc,
4749                                                 "Anonymous methods inside structs cannot access instance members of `{0}'. Consider copying `{0}' to a local variable outside the anonymous method and using the local instead",
4750                                                         "this");
4751                                                 return null;
4752                                         }
4753                                 }
4754                         }
4755
4756                         IFixedBuffer fb = AttributeTester.GetFixedBuffer (FieldInfo);
4757                         if (fb != null) {
4758                                 if (!ec.InFixedInitializer && ec.ContainerType.IsValueType) {
4759                                         Report.Error (1666, loc, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
4760                                 }
4761
4762                                 if (InstanceExpression.eclass != ExprClass.Variable) {
4763                                         Report.SymbolRelatedToPreviousError (FieldInfo);
4764                                         Report.Error (1708, loc, "`{0}': Fixed size buffers can only be accessed through locals or fields",
4765                                                 TypeManager.GetFullNameSignature (FieldInfo));
4766                                 }
4767                                 
4768                                 return new FixedBufferPtr (this, fb.ElementType, loc).Resolve (ec);
4769                         }
4770
4771                         // If the instance expression is a local variable or parameter.
4772                         IVariable var = InstanceExpression as IVariable;
4773                         if ((var == null) || (var.VariableInfo == null))
4774                                 return this;
4775
4776                         VariableInfo vi = var.VariableInfo;
4777                         if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
4778                                 return null;
4779
4780                         variable_info = vi.GetSubStruct (FieldInfo.Name);
4781                         return this;
4782                 }
4783
4784                 static readonly int [] codes = {
4785                         191,    // instance, write access
4786                         192,    // instance, out access
4787                         198,    // static, write access
4788                         199,    // static, out access
4789                         1648,   // member of value instance, write access
4790                         1649,   // member of value instance, out access
4791                         1650,   // member of value static, write access
4792                         1651    // member of value static, out access
4793                 };
4794
4795                 static readonly string [] msgs = {
4796                         /*0191*/ "A readonly field `{0}' cannot be assigned to (except in a constructor or a variable initializer)",
4797                         /*0192*/ "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
4798                         /*0198*/ "A static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
4799                         /*0199*/ "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
4800                         /*1648*/ "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)",
4801                         /*1649*/ "Members of readonly field `{0}' cannot be passed ref or out (except in a constructor)",
4802                         /*1650*/ "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
4803                         /*1651*/ "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)"
4804                 };
4805
4806                 // The return value is always null.  Returning a value simplifies calling code.
4807                 Expression Report_AssignToReadonly (Expression right_side)
4808                 {
4809                         int i = 0;
4810                         if (right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess)
4811                                 i += 1;
4812                         if (IsStatic)
4813                                 i += 2;
4814                         if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess)
4815                                 i += 4;
4816                         Report.Error (codes [i], loc, msgs [i], GetSignatureForError ());
4817
4818                         return null;
4819                 }
4820                 
4821                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4822                 {
4823                         IVariable var = InstanceExpression as IVariable;
4824                         if ((var != null) && (var.VariableInfo != null))
4825                                 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
4826
4827                         bool lvalue_instance = !FieldInfo.IsStatic && FieldInfo.DeclaringType.IsValueType;
4828                         bool out_access = right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess;
4829
4830                         Expression e = DoResolve (ec, lvalue_instance, out_access);
4831
4832                         if (e == null)
4833                                 return null;
4834
4835                         FieldBase fb = TypeManager.GetField (FieldInfo);
4836                         if (fb != null)
4837                                 fb.SetAssigned ();
4838
4839                         if (FieldInfo.IsInitOnly) {
4840                                 // InitOnly fields can only be assigned in constructors or initializers
4841                                 if (!ec.IsInFieldInitializer && !ec.IsConstructor)
4842                                         return Report_AssignToReadonly (right_side);
4843
4844                                 if (ec.IsConstructor) {
4845                                         Type ctype = ec.TypeContainer.CurrentType;
4846                                         if (ctype == null)
4847                                                 ctype = ec.ContainerType;
4848
4849                                         // InitOnly fields cannot be assigned-to in a different constructor from their declaring type
4850                                         if (!TypeManager.IsEqual (ctype, FieldInfo.DeclaringType))
4851                                                 return Report_AssignToReadonly (right_side);
4852                                         // static InitOnly fields cannot be assigned-to in an instance constructor
4853                                         if (IsStatic && !ec.IsStatic)
4854                                                 return Report_AssignToReadonly (right_side);
4855                                         // instance constructors can't modify InitOnly fields of other instances of the same type
4856                                         if (!IsStatic && !(InstanceExpression is This))
4857                                                 return Report_AssignToReadonly (right_side);
4858                                 }
4859                         }
4860
4861                         if (right_side == EmptyExpression.OutAccess &&
4862                             !IsStatic && !(InstanceExpression is This) && TypeManager.mbr_type != null && TypeManager.IsSubclassOf (DeclaringType, TypeManager.mbr_type)) {
4863                                 Report.SymbolRelatedToPreviousError (DeclaringType);
4864                                 Report.Warning (197, 1, loc,
4865                                                 "Passing `{0}' as ref or out or taking its address may cause a runtime exception because it is a field of a marshal-by-reference class",
4866                                                 GetSignatureForError ());
4867                         }
4868
4869                         return this;
4870                 }
4871
4872                 bool is_marshal_by_ref ()
4873                 {
4874                         return !IsStatic && Type.IsValueType && TypeManager.mbr_type != null && TypeManager.IsSubclassOf (DeclaringType, TypeManager.mbr_type);
4875                 }
4876
4877                 public override void CheckMarshalByRefAccess (EmitContext ec)
4878                 {
4879                         if (is_marshal_by_ref () && !(InstanceExpression is This)) {
4880                                 Report.SymbolRelatedToPreviousError (DeclaringType);
4881                                 Report.Warning (1690, 1, loc, "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class",
4882                                                 GetSignatureForError ());
4883                         }
4884                 }
4885
4886                 public bool VerifyFixed ()
4887                 {
4888                         IVariable variable = InstanceExpression as IVariable;
4889                         // A variable of the form V.I is fixed when V is a fixed variable of a struct type.
4890                         // We defer the InstanceExpression check after the variable check to avoid a 
4891                         // separate null check on InstanceExpression.
4892                         return variable != null && InstanceExpression.Type.IsValueType && variable.VerifyFixed ();
4893                 }
4894
4895                 public override int GetHashCode ()
4896                 {
4897                         return FieldInfo.GetHashCode ();
4898                 }
4899
4900                 public override bool Equals (object obj)
4901                 {
4902                         FieldExpr fe = obj as FieldExpr;
4903                         if (fe == null)
4904                                 return false;
4905
4906                         if (FieldInfo != fe.FieldInfo)
4907                                 return false;
4908
4909                         if (InstanceExpression == null || fe.InstanceExpression == null)
4910                                 return true;
4911
4912                         return InstanceExpression.Equals (fe.InstanceExpression);
4913                 }
4914                 
4915                 public void Emit (EmitContext ec, bool leave_copy)
4916                 {
4917                         ILGenerator ig = ec.ig;
4918                         bool is_volatile = false;
4919
4920                         FieldBase f = TypeManager.GetField (FieldInfo);
4921                         if (f != null){
4922                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4923                                         is_volatile = true;
4924
4925                                 f.SetMemberIsUsed ();
4926                         }
4927                         
4928                         if (FieldInfo.IsStatic){
4929                                 if (is_volatile)
4930                                         ig.Emit (OpCodes.Volatile);
4931                                 
4932                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4933                         } else {
4934                                 if (!prepared)
4935                                         EmitInstance (ec, false);
4936
4937                                 IFixedBuffer ff = AttributeTester.GetFixedBuffer (FieldInfo);
4938                                 if (ff != null) {
4939                                         ig.Emit (OpCodes.Ldflda, FieldInfo);
4940                                         ig.Emit (OpCodes.Ldflda, ff.Element);
4941                                 } else {
4942                                         if (is_volatile)
4943                                                 ig.Emit (OpCodes.Volatile);
4944
4945                                         ig.Emit (OpCodes.Ldfld, FieldInfo);
4946                                 }
4947                         }
4948
4949                         if (leave_copy) {
4950                                 ec.ig.Emit (OpCodes.Dup);
4951                                 if (!FieldInfo.IsStatic) {
4952                                         temp = new LocalTemporary (this.Type);
4953                                         temp.Store (ec);
4954                                 }
4955                         }
4956                 }
4957
4958                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
4959                 {
4960                         FieldAttributes fa = FieldInfo.Attributes;
4961                         bool is_static = (fa & FieldAttributes.Static) != 0;
4962                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4963                         ILGenerator ig = ec.ig;
4964
4965                         if (is_readonly && !ec.IsConstructor){
4966                                 Report_AssignToReadonly (source);
4967                                 return;
4968                         }
4969
4970                         //
4971                         // String concatenation creates a new string instance 
4972                         //
4973                         prepared = prepare_for_load && !(source is StringConcat);
4974                         EmitInstance (ec, prepared);
4975
4976                         source.Emit (ec);                       
4977                         if (leave_copy) {
4978                                 ec.ig.Emit (OpCodes.Dup);
4979                                 if (!FieldInfo.IsStatic) {
4980                                         temp = new LocalTemporary (this.Type);
4981                                         temp.Store (ec);
4982                                 }
4983                         }
4984
4985                         FieldBase f = TypeManager.GetField (FieldInfo);
4986                         if (f != null){
4987                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4988                                         ig.Emit (OpCodes.Volatile);
4989                                         
4990                                 f.SetAssigned ();
4991                         }
4992
4993                         if (is_static)
4994                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
4995                         else 
4996                                 ig.Emit (OpCodes.Stfld, FieldInfo);
4997                         
4998                         if (temp != null) {
4999                                 temp.Emit (ec);
5000                                 temp.Release (ec);
5001                         }
5002                 }
5003
5004                 public override void Emit (EmitContext ec)
5005                 {
5006                         Emit (ec, false);
5007                 }
5008
5009                 public override void EmitSideEffect (EmitContext ec)
5010                 {
5011                         FieldBase f = TypeManager.GetField (FieldInfo);
5012                         bool is_volatile = f != null && (f.ModFlags & Modifiers.VOLATILE) != 0;
5013
5014                         if (is_volatile || is_marshal_by_ref ())
5015                                 base.EmitSideEffect (ec);
5016                 }
5017
5018                 public void AddressOf (EmitContext ec, AddressOp mode)
5019                 {
5020                         ILGenerator ig = ec.ig;
5021
5022                         FieldBase f = TypeManager.GetField (FieldInfo);
5023                         if (f != null){
5024                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
5025                                         Report.Warning (420, 1, loc, "`{0}': A volatile field references will not be treated as volatile", 
5026                                                         f.GetSignatureForError ());
5027                                 }
5028                                         
5029                                 if ((mode & AddressOp.Store) != 0)
5030                                         f.SetAssigned ();
5031                                 if ((mode & AddressOp.Load) != 0)
5032                                         f.SetMemberIsUsed ();
5033                         }
5034
5035                         //
5036                         // Handle initonly fields specially: make a copy and then
5037                         // get the address of the copy.
5038                         //
5039                         bool need_copy;
5040                         if (FieldInfo.IsInitOnly){
5041                                 need_copy = true;
5042                                 if (ec.IsConstructor){
5043                                         if (FieldInfo.IsStatic){
5044                                                 if (ec.IsStatic)
5045                                                         need_copy = false;
5046                                         } else
5047                                                 need_copy = false;
5048                                 }
5049                         } else
5050                                 need_copy = false;
5051                         
5052                         if (need_copy){
5053                                 LocalBuilder local;
5054                                 Emit (ec);
5055                                 local = ig.DeclareLocal (type);
5056                                 ig.Emit (OpCodes.Stloc, local);
5057                                 ig.Emit (OpCodes.Ldloca, local);
5058                                 return;
5059                         }
5060
5061
5062                         if (FieldInfo.IsStatic){
5063                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
5064                         } else {
5065                                 if (!prepared)
5066                                         EmitInstance (ec, false);
5067                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
5068                         }
5069                 }
5070         }
5071
5072         
5073         /// <summary>
5074         ///   Expression that evaluates to a Property.  The Assign class
5075         ///   might set the `Value' expression if we are in an assignment.
5076         ///
5077         ///   This is not an LValue because we need to re-write the expression, we
5078         ///   can not take data from the stack and store it.  
5079         /// </summary>
5080         public class PropertyExpr : MemberExpr, IAssignMethod {
5081                 public readonly PropertyInfo PropertyInfo;
5082                 MethodInfo getter, setter;
5083                 bool is_static;
5084
5085                 bool resolved;
5086                 
5087                 LocalTemporary temp;
5088                 bool prepared;
5089
5090                 public PropertyExpr (Type container_type, PropertyInfo pi, Location l)
5091                 {
5092                         PropertyInfo = pi;
5093                         eclass = ExprClass.PropertyAccess;
5094                         is_static = false;
5095                         loc = l;
5096
5097                         type = TypeManager.TypeToCoreType (pi.PropertyType);
5098
5099                         ResolveAccessors (container_type);
5100                 }
5101
5102                 public override string Name {
5103                         get {
5104                                 return PropertyInfo.Name;
5105                         }
5106                 }
5107
5108                 public override bool IsInstance {
5109                         get {
5110                                 return !is_static;
5111                         }
5112                 }
5113
5114                 public override bool IsStatic {
5115                         get {
5116                                 return is_static;
5117                         }
5118                 }
5119
5120                 public override Expression CreateExpressionTree (EmitContext ec)
5121                 {
5122                         if (IsSingleDimensionalArrayLength ()) {
5123                                 ArrayList args = new ArrayList (1);
5124                                 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
5125                                 return CreateExpressionFactoryCall ("ArrayLength", args);
5126                         }
5127
5128                         // TODO: it's waiting for PropertyExpr refactoring
5129                         //ArrayList args = new ArrayList (2);
5130                         //args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
5131                         //args.Add (getter expression);
5132                         //return CreateExpressionFactoryCall ("Property", args);
5133                         return base.CreateExpressionTree (ec);
5134                 }
5135
5136                 public Expression CreateSetterTypeOfExpression ()
5137                 {
5138                         return new Cast (new TypeExpression (typeof (MethodInfo), loc), new TypeOfMethod (setter, loc));
5139                 }
5140
5141                 public override Type DeclaringType {
5142                         get {
5143                                 return PropertyInfo.DeclaringType;
5144                         }
5145                 }
5146
5147                 public override string GetSignatureForError ()
5148                 {
5149                         return TypeManager.GetFullNameSignature (PropertyInfo);
5150                 }
5151
5152                 void FindAccessors (Type invocation_type)
5153                 {
5154                         const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
5155                                 BindingFlags.Static | BindingFlags.Instance |
5156                                 BindingFlags.DeclaredOnly;
5157
5158                         Type current = PropertyInfo.DeclaringType;
5159                         for (; current != null; current = current.BaseType) {
5160                                 MemberInfo[] group = TypeManager.MemberLookup (
5161                                         invocation_type, invocation_type, current,
5162                                         MemberTypes.Property, flags, PropertyInfo.Name, null);
5163
5164                                 if (group == null)
5165                                         continue;
5166
5167                                 if (group.Length != 1)
5168                                         // Oooops, can this ever happen ?
5169                                         return;
5170
5171                                 PropertyInfo pi = (PropertyInfo) group [0];
5172
5173                                 if (getter == null)
5174                                         getter = pi.GetGetMethod (true);
5175
5176                                 if (setter == null)
5177                                         setter = pi.GetSetMethod (true);
5178
5179                                 MethodInfo accessor = getter != null ? getter : setter;
5180
5181                                 if (!accessor.IsVirtual)
5182                                         return;
5183                         }
5184                 }
5185
5186                 //
5187                 // We also perform the permission checking here, as the PropertyInfo does not
5188                 // hold the information for the accessibility of its setter/getter
5189                 //
5190                 // TODO: Refactor to use some kind of cache together with GetPropertyFromAccessor
5191                 void ResolveAccessors (Type container_type)
5192                 {
5193                         FindAccessors (container_type);
5194
5195                         if (getter != null) {
5196                                 MethodBase the_getter = TypeManager.DropGenericMethodArguments (getter);
5197                                 IMethodData md = TypeManager.GetMethod (the_getter);
5198                                 if (md != null)
5199                                         md.SetMemberIsUsed ();
5200
5201                                 is_static = getter.IsStatic;
5202                         }
5203
5204                         if (setter != null) {
5205                                 MethodBase the_setter = TypeManager.DropGenericMethodArguments (setter);
5206                                 IMethodData md = TypeManager.GetMethod (the_setter);
5207                                 if (md != null)
5208                                         md.SetMemberIsUsed ();
5209
5210                                 is_static = setter.IsStatic;
5211                         }
5212                 }
5213
5214                 bool InstanceResolve (EmitContext ec, bool lvalue_instance, bool must_do_cs1540_check)
5215                 {
5216                         if (is_static) {
5217                                 InstanceExpression = null;
5218                                 return true;
5219                         }
5220
5221                         if (InstanceExpression == null) {
5222                                 SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
5223                                 return false;
5224                         }
5225
5226                         InstanceExpression = InstanceExpression.DoResolve (ec);
5227                         if (lvalue_instance && InstanceExpression != null)
5228                                 InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
5229
5230                         if (InstanceExpression == null)
5231                                 return false;
5232
5233                         InstanceExpression.CheckMarshalByRefAccess (ec);
5234
5235                         if (must_do_cs1540_check && (InstanceExpression != EmptyExpression.Null) &&
5236                             !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.ContainerType) &&
5237                             !TypeManager.IsNestedChildOf (ec.ContainerType, InstanceExpression.Type) &&
5238                             !TypeManager.IsSubclassOf (InstanceExpression.Type, ec.ContainerType)) {
5239                                 Report.SymbolRelatedToPreviousError (PropertyInfo);
5240                                 Error_CannotAccessProtected (loc, PropertyInfo, InstanceExpression.Type, ec.ContainerType);
5241                                 return false;
5242                         }
5243
5244                         return true;
5245                 }
5246
5247                 void Error_PropertyNotFound (MethodInfo mi, bool getter)
5248                 {
5249                         // TODO: correctly we should compare arguments but it will lead to bigger changes
5250                         if (mi is MethodBuilder) {
5251                                 Error_TypeDoesNotContainDefinition (loc, PropertyInfo.DeclaringType, Name);
5252                                 return;
5253                         }
5254                         
5255                         StringBuilder sig = new StringBuilder (TypeManager.CSharpName (mi.DeclaringType));
5256                         sig.Append ('.');
5257                         ParameterData iparams = TypeManager.GetParameterData (mi);
5258                         sig.Append (getter ? "get_" : "set_");
5259                         sig.Append (Name);
5260                         sig.Append (iparams.GetSignatureForError ());
5261
5262                         Report.SymbolRelatedToPreviousError (mi);
5263                         Report.Error (1546, loc, "Property `{0}' is not supported by the C# language. Try to call the accessor method `{1}' directly",
5264                                 Name, sig.ToString ());
5265                 }
5266
5267                 public bool IsAccessibleFrom (Type invocation_type, bool lvalue)
5268                 {
5269                         bool dummy;
5270                         MethodInfo accessor = lvalue ? setter : getter;
5271                         if (accessor == null && lvalue)
5272                                 accessor = getter;
5273                         return accessor != null && IsAccessorAccessible (invocation_type, accessor, out dummy);
5274                 }
5275
5276                 bool IsSingleDimensionalArrayLength ()
5277                 {
5278                         if (DeclaringType != TypeManager.array_type || getter == null || Name != "Length")
5279                                 return false;
5280
5281                         string t_name = InstanceExpression.Type.Name;
5282                         int t_name_len = t_name.Length;
5283                         return t_name_len > 2 && t_name [t_name_len - 2] == '[' && t_name [t_name_len - 3] != ']';
5284                 }
5285
5286                 override public Expression DoResolve (EmitContext ec)
5287                 {
5288                         if (resolved)
5289                                 return this;
5290
5291                         if (getter != null){
5292                                 if (TypeManager.GetParameterData (getter).Count != 0){
5293                                         Error_PropertyNotFound (getter, true);
5294                                         return null;
5295                                 }
5296                         }
5297
5298                         if (getter == null){
5299                                 //
5300                                 // The following condition happens if the PropertyExpr was
5301                                 // created, but is invalid (ie, the property is inaccessible),
5302                                 // and we did not want to embed the knowledge about this in
5303                                 // the caller routine.  This only avoids double error reporting.
5304                                 //
5305                                 if (setter == null)
5306                                         return null;
5307
5308                                 if (InstanceExpression != EmptyExpression.Null) {
5309                                         Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
5310                                                 TypeManager.GetFullNameSignature (PropertyInfo));
5311                                         return null;
5312                                 }
5313                         } 
5314
5315                         bool must_do_cs1540_check = false;
5316                         if (getter != null &&
5317                             !IsAccessorAccessible (ec.ContainerType, getter, out must_do_cs1540_check)) {
5318                                 PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter) as PropertyBase.PropertyMethod;
5319                                 if (pm != null && pm.HasCustomAccessModifier) {
5320                                         Report.SymbolRelatedToPreviousError (pm);
5321                                         Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible",
5322                                                 TypeManager.CSharpSignature (getter));
5323                                 }
5324                                 else {
5325                                         Report.SymbolRelatedToPreviousError (getter);
5326                                         ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter));
5327                                 }
5328                                 return null;
5329                         }
5330                         
5331                         if (!InstanceResolve (ec, false, must_do_cs1540_check))
5332                                 return null;
5333
5334                         //
5335                         // Only base will allow this invocation to happen.
5336                         //
5337                         if (IsBase && getter.IsAbstract) {
5338                                 Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (PropertyInfo));
5339                                 return null;
5340                         }
5341
5342                         if (PropertyInfo.PropertyType.IsPointer && !ec.InUnsafe){
5343                                 UnsafeError (loc);
5344                                 return null;
5345                         }
5346
5347                         resolved = true;
5348
5349                         return this;
5350                 }
5351
5352                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
5353                 {
5354                         if (right_side == EmptyExpression.OutAccess) {
5355                                 if (ec.CurrentBlock.Toplevel.GetTransparentIdentifier (PropertyInfo.Name) != null) {
5356                                         Report.Error (1939, loc, "A range variable `{0}' may not be passes as `ref' or `out' parameter",
5357                                             PropertyInfo.Name);
5358                                 } else {
5359                                         Report.Error (206, loc, "A property or indexer `{0}' may not be passed as `ref' or `out' parameter",
5360                                               GetSignatureForError ());
5361                                 }
5362                                 return null;
5363                         }
5364
5365                         if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
5366                                 Error_CannotModifyIntermediateExpressionValue (ec);
5367                         }
5368
5369                         if (setter == null){
5370                                 //
5371                                 // The following condition happens if the PropertyExpr was
5372                                 // created, but is invalid (ie, the property is inaccessible),
5373                                 // and we did not want to embed the knowledge about this in
5374                                 // the caller routine.  This only avoids double error reporting.
5375                                 //
5376                                 if (getter == null)
5377                                         return null;
5378                                 Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read only)",
5379                                               GetSignatureForError ());
5380                                 return null;
5381                         }
5382
5383                         if (TypeManager.GetParameterData (setter).Count != 1){
5384                                 Error_PropertyNotFound (setter, false);
5385                                 return null;
5386                         }
5387
5388                         bool must_do_cs1540_check;
5389                         if (!IsAccessorAccessible (ec.ContainerType, setter, out must_do_cs1540_check)) {
5390                                 PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter) as PropertyBase.PropertyMethod;
5391                                 if (pm != null && pm.HasCustomAccessModifier) {
5392                                         Report.SymbolRelatedToPreviousError (pm);
5393                                         Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible",
5394                                                 TypeManager.CSharpSignature (setter));
5395                                 }
5396                                 else {
5397                                         Report.SymbolRelatedToPreviousError (setter);
5398                                         ErrorIsInaccesible (loc, TypeManager.CSharpSignature (setter));
5399                                 }
5400                                 return null;
5401                         }
5402                         
5403                         if (!InstanceResolve (ec, PropertyInfo.DeclaringType.IsValueType, must_do_cs1540_check))
5404                                 return null;
5405                         
5406                         //
5407                         // Only base will allow this invocation to happen.
5408                         //
5409                         if (IsBase && setter.IsAbstract){
5410                                 Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (PropertyInfo));
5411                                 return null;
5412                         }
5413
5414                         return this;
5415                 }
5416                 
5417                 public override void Emit (EmitContext ec)
5418                 {
5419                         Emit (ec, false);
5420                 }
5421                 
5422                 public void Emit (EmitContext ec, bool leave_copy)
5423                 {
5424                         //
5425                         // Special case: length of single dimension array property is turned into ldlen
5426                         //
5427                         if (IsSingleDimensionalArrayLength ()) {
5428                                 if (!prepared)
5429                                         EmitInstance (ec, false);
5430                                 ec.ig.Emit (OpCodes.Ldlen);
5431                                 ec.ig.Emit (OpCodes.Conv_I4);
5432                                 return;
5433                         }
5434
5435                         Invocation.EmitCall (ec, IsBase, InstanceExpression, getter, null, loc, prepared, false);
5436                         
5437                         if (leave_copy) {
5438                                 ec.ig.Emit (OpCodes.Dup);
5439                                 if (!is_static) {
5440                                         temp = new LocalTemporary (this.Type);
5441                                         temp.Store (ec);
5442                                 }
5443                         }
5444                 }
5445
5446                 //
5447                 // Implements the IAssignMethod interface for assignments
5448                 //
5449                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
5450                 {
5451                         Expression my_source = source;
5452
5453                         if (prepare_for_load) {
5454                                 if (source is StringConcat)
5455                                         EmitInstance (ec, false);
5456                                 else
5457                                         prepared = true;                                        
5458
5459                                 source.Emit (ec);
5460                                 
5461                                 prepared = true;
5462                                 if (leave_copy) {
5463                                         ec.ig.Emit (OpCodes.Dup);
5464                                         if (!is_static) {
5465                                                 temp = new LocalTemporary (this.Type);
5466                                                 temp.Store (ec);
5467                                         }
5468                                 }
5469                         } else if (leave_copy) {
5470                                 source.Emit (ec);
5471                                 temp = new LocalTemporary (this.Type);
5472                                 temp.Store (ec);
5473                                 my_source = temp;
5474                         }
5475
5476                         ArrayList args = new ArrayList (1);
5477                         args.Add (new Argument (my_source, Argument.AType.Expression));
5478                         
5479                         Invocation.EmitCall (ec, IsBase, InstanceExpression, setter, args, loc, false, prepared);
5480                         
5481                         if (temp != null) {
5482                                 temp.Emit (ec);
5483                                 temp.Release (ec);
5484                         }
5485                 }
5486         }
5487
5488         /// <summary>
5489         ///   Fully resolved expression that evaluates to an Event
5490         /// </summary>
5491         public class EventExpr : MemberExpr {
5492                 public readonly EventInfo EventInfo;
5493
5494                 bool is_static;
5495                 MethodInfo add_accessor, remove_accessor;
5496
5497                 public EventExpr (EventInfo ei, Location loc)
5498                 {
5499                         EventInfo = ei;
5500                         this.loc = loc;
5501                         eclass = ExprClass.EventAccess;
5502
5503                         add_accessor = TypeManager.GetAddMethod (ei);
5504                         remove_accessor = TypeManager.GetRemoveMethod (ei);
5505                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
5506                                 is_static = true;
5507
5508                         if (EventInfo is MyEventBuilder){
5509                                 MyEventBuilder eb = (MyEventBuilder) EventInfo;
5510                                 type = eb.EventType;
5511                                 eb.SetUsed ();
5512                         } else
5513                                 type = EventInfo.EventHandlerType;
5514                 }
5515
5516                 public override string Name {
5517                         get {
5518                                 return EventInfo.Name;
5519                         }
5520                 }
5521
5522                 public override bool IsInstance {
5523                         get {
5524                                 return !is_static;
5525                         }
5526                 }
5527
5528                 public override bool IsStatic {
5529                         get {
5530                                 return is_static;
5531                         }
5532                 }
5533
5534                 public override Type DeclaringType {
5535                         get {
5536                                 return EventInfo.DeclaringType;
5537                         }
5538                 }
5539                 
5540                 void Error_AssignmentEventOnly ()
5541                 {
5542                         Report.Error (79, loc, "The event `{0}' can only appear on the left hand side of `+=' or `-=' operator",
5543                                 GetSignatureForError ());
5544                 }
5545
5546                 public override MemberExpr ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
5547                                                                 SimpleName original)
5548                 {
5549                         //
5550                         // If the event is local to this class, we transform ourselves into a FieldExpr
5551                         //
5552
5553                         if (EventInfo.DeclaringType == ec.ContainerType ||
5554                             TypeManager.IsNestedChildOf(ec.ContainerType, EventInfo.DeclaringType)) {
5555                                 EventField mi = TypeManager.GetEventField (EventInfo);
5556
5557                                 if (mi != null) {
5558                                         if (!ec.IsInObsoleteScope)
5559                                                 mi.CheckObsoleteness (loc);
5560
5561                                         if ((mi.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0 && !ec.IsInCompoundAssignment)
5562                                                 Error_AssignmentEventOnly ();
5563                                         
5564                                         FieldExpr ml = new FieldExpr (mi.FieldBuilder, loc);
5565
5566                                         InstanceExpression = null;
5567                                 
5568                                         return ml.ResolveMemberAccess (ec, left, loc, original);
5569                                 }
5570                         }
5571                         
5572                         if (left is This && !ec.IsInCompoundAssignment)                 
5573                                 Error_AssignmentEventOnly ();
5574
5575                         return base.ResolveMemberAccess (ec, left, loc, original);
5576                 }
5577
5578
5579                 bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
5580                 {
5581                         if (is_static) {
5582                                 InstanceExpression = null;
5583                                 return true;
5584                         }
5585
5586                         if (InstanceExpression == null) {
5587                                 SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
5588                                 return false;
5589                         }
5590
5591                         InstanceExpression = InstanceExpression.DoResolve (ec);
5592                         if (InstanceExpression == null)
5593                                 return false;
5594
5595                         if (IsBase && add_accessor.IsAbstract) {
5596                                 Error_CannotCallAbstractBase(TypeManager.CSharpSignature(add_accessor));
5597                                 return false;
5598                         }
5599
5600                         //
5601                         // This is using the same mechanism as the CS1540 check in PropertyExpr.
5602                         // However, in the Event case, we reported a CS0122 instead.
5603                         //
5604                         if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
5605                             InstanceExpression.Type != ec.ContainerType &&
5606                             TypeManager.IsSubclassOf (ec.ContainerType, InstanceExpression.Type)) {
5607                                 Report.SymbolRelatedToPreviousError (EventInfo);
5608                                 ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
5609                                 return false;
5610                         }
5611
5612                         return true;
5613                 }
5614
5615                 public bool IsAccessibleFrom (Type invocation_type)
5616                 {
5617                         bool dummy;
5618                         return IsAccessorAccessible (invocation_type, add_accessor, out dummy) &&
5619                                 IsAccessorAccessible (invocation_type, remove_accessor, out dummy);
5620                 }
5621
5622                 public override Expression CreateExpressionTree (EmitContext ec)
5623                 {
5624                         throw new NotSupportedException ();
5625                 }
5626
5627                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5628                 {
5629                         return DoResolve (ec);
5630                 }
5631
5632                 public override Expression DoResolve (EmitContext ec)
5633                 {
5634                         bool must_do_cs1540_check;
5635                         if (!(IsAccessorAccessible (ec.ContainerType, add_accessor, out must_do_cs1540_check) &&
5636                               IsAccessorAccessible (ec.ContainerType, remove_accessor, out must_do_cs1540_check))) {
5637                                 Report.SymbolRelatedToPreviousError (EventInfo);
5638                                 ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
5639                                 return null;
5640                         }
5641
5642                         if (!InstanceResolve (ec, must_do_cs1540_check))
5643                                 return null;
5644                         
5645                         return this;
5646                 }               
5647
5648                 public override void Emit (EmitContext ec)
5649                 {
5650                         Report.Error (70, loc, "The event `{0}' can only appear on the left hand side of += or -= "+
5651                                       "(except on the defining type)", GetSignatureForError ());
5652                 }
5653
5654                 public override string GetSignatureForError ()
5655                 {
5656                         return TypeManager.CSharpSignature (EventInfo);
5657                 }
5658
5659                 public void EmitAddOrRemove (EmitContext ec, Expression source)
5660                 {
5661                         BinaryDelegate source_del = source as BinaryDelegate;
5662                         if (source_del == null) {
5663                                 Emit (ec);
5664                                 return;
5665                         }
5666                         Expression handler = source_del.Right;
5667                         
5668                         Argument arg = new Argument (handler, Argument.AType.Expression);
5669                         ArrayList args = new ArrayList ();
5670                                 
5671                         args.Add (arg);
5672                         
5673                         if (source_del.IsAddition)
5674                                 Invocation.EmitCall (
5675                                         ec, IsBase, InstanceExpression, add_accessor, args, loc);
5676                         else
5677                                 Invocation.EmitCall (
5678                                         ec, IsBase, InstanceExpression, remove_accessor, args, loc);
5679                 }
5680         }
5681
5682         public class TemporaryVariable : Expression, IMemoryLocation
5683         {
5684                 LocalInfo li;
5685                 Variable var;
5686                 
5687                 public TemporaryVariable (Type type, Location loc)
5688                 {
5689                         this.type = type;
5690                         this.loc = loc;
5691                         eclass = ExprClass.Value;
5692                 }
5693                 
5694                 public override Expression DoResolve (EmitContext ec)
5695                 {
5696                         if (li != null)
5697                                 return this;
5698                         
5699                         TypeExpr te = new TypeExpression (type, loc);
5700                         li = ec.CurrentBlock.AddTemporaryVariable (te, loc);
5701                         if (!li.Resolve (ec))
5702                                 return null;
5703
5704                         if (ec.MustCaptureVariable (li)) {
5705                                 ScopeInfo scope = li.Block.CreateScopeInfo ();
5706                                 var = scope.AddLocal (li);
5707                                 type = var.Type;
5708                         }
5709                         
5710                         return this;
5711                 }
5712
5713                 public Variable Variable {
5714                         get { return var != null ? var : li.Variable; }
5715                 }
5716                 
5717                 public override void Emit (EmitContext ec)
5718                 {
5719                         Variable.EmitInstance (ec);
5720                         Variable.Emit (ec);
5721                 }
5722                 
5723                 public void EmitLoadAddress (EmitContext ec)
5724                 {
5725                         Variable.EmitInstance (ec);
5726                         Variable.EmitAddressOf (ec);
5727                 }
5728                 
5729                 public void Store (EmitContext ec, Expression right_side)
5730                 {
5731                         Variable.EmitInstance (ec);
5732                         right_side.Emit (ec);
5733                         Variable.EmitAssign (ec);
5734                 }
5735                 
5736                 public void EmitThis (EmitContext ec)
5737                 {
5738                         Variable.EmitInstance (ec);
5739                 }
5740                 
5741                 public void EmitStore (EmitContext ec)
5742                 {
5743                         Variable.EmitAssign (ec);
5744                 }
5745                 
5746                 public void AddressOf (EmitContext ec, AddressOp mode)
5747                 {
5748                         EmitLoadAddress (ec);
5749                 }
5750         }
5751
5752         /// 
5753         /// Handles `var' contextual keyword; var becomes a keyword only
5754         /// if no type called var exists in a variable scope
5755         /// 
5756         public class VarExpr : SimpleName
5757         {
5758                 // Used for error reporting only
5759                 ArrayList initializer;
5760
5761                 public VarExpr (Location loc)
5762                         : base ("var", loc)
5763                 {
5764                 }
5765
5766                 public ArrayList VariableInitializer {
5767                         set {
5768                                 this.initializer = value;
5769                         }
5770                 }
5771
5772                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5773                 {
5774                         if (type != null)
5775                                 throw new InternalErrorException ("An implicitly typed local variable could not be redefined");
5776                         
5777                         type = right_side.Type;
5778                         if (type == TypeManager.null_type || type == TypeManager.void_type || type == TypeManager.anonymous_method_type) {
5779                                 Report.Error (815, loc, "An implicitly typed local variable declaration cannot be initialized with `{0}'",
5780                                               right_side.GetSignatureForError ());
5781                                 return null;
5782                         }
5783
5784                         eclass = ExprClass.Variable;
5785                         return this;
5786                 }
5787
5788                 protected override void Error_TypeOrNamespaceNotFound (IResolveContext ec)
5789                 {
5790                         Report.Error (825, loc, "The contextual keyword `var' may only appear within a local variable declaration");
5791                 }
5792
5793                 public override TypeExpr ResolveAsContextualType (IResolveContext rc, bool silent)
5794                 {
5795                         TypeExpr te = base.ResolveAsContextualType (rc, true);
5796                         if (te != null)
5797                                 return te;
5798
5799                         if (initializer == null)
5800                                 return null;
5801                         
5802                         if (initializer.Count > 1) {
5803                                 Location loc = ((Mono.CSharp.CSharpParser.VariableDeclaration)initializer [1]).Location;
5804                                 Report.Error (819, loc, "An implicitly typed local variable declaration cannot include multiple declarators");
5805                                 initializer = null;
5806                                 return null;
5807                         }
5808                                 
5809                         Expression variable_initializer = ((Mono.CSharp.CSharpParser.VariableDeclaration)initializer [0]).expression_or_array_initializer;
5810                         if (variable_initializer == null) {
5811                                 Report.Error (818, loc, "An implicitly typed local variable declarator must include an initializer");
5812                                 return null;
5813                         }
5814                         
5815                         return null;
5816                 }
5817         }
5818 }