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