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