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