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