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