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