make line-endings uniform
[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) {
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                 protected void Error_CannotAssign (string to, string roContext)
306                 {
307                         Report.Error (1656, loc, "Cannot assign to `{0}' because it is a `{1}'",
308                                 to, roContext);
309                 }
310
311                 public static void Error_VoidInvalidInTheContext (Location loc)
312                 {
313                         Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
314                 }
315
316                 public virtual void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
317                 {
318                         if (Type.FullName == target.FullName){
319                                 Report.ExtraInformation (loc,
320                                         String.Format (
321                                         "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
322                                         Type.FullName, Type.Assembly.FullName, target.Assembly.FullName));
323                                                          
324                         }
325
326                         if (expl) {
327                                 Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
328                                         GetSignatureForError (), TypeManager.CSharpName (target));
329                                 return;
330                         }
331                         
332                         Expression e = (this is EnumConstant) ? ((EnumConstant)this).Child : this;
333                         bool b = Convert.ExplicitNumericConversion (e, target) != null;
334
335                         if (b ||
336                             Convert.ExplicitReferenceConversionExists (Type, target) ||
337                             Convert.ExplicitUnsafe (e, target) != null ||
338                             (ec != null && Convert.UserDefinedConversion (ec, this, target, Location.Null, true) != null))
339                         {
340                                 Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. " +
341                                               "An explicit conversion exists (are you missing a cast?)",
342                                         TypeManager.CSharpName (Type), TypeManager.CSharpName (target));
343                                 return;
344                         }
345
346                         if (Type != TypeManager.string_type && this is Constant && !(this is EmptyConstantCast)) {
347                                 Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
348                                         ((Constant)(this)).GetValue ().ToString (), TypeManager.CSharpName (target));
349                                 return;
350                         }
351
352                         Report.Error (29, loc, "Cannot implicitly convert type {0} to `{1}'",
353                                 Type == TypeManager.anonymous_method_type ?
354                                 "anonymous method" : "`" + GetSignatureForError () + "'",
355                                 TypeManager.CSharpName (target));
356                 }
357
358                 protected static void Error_TypeDoesNotContainDefinition (Location loc, Type type, string name)
359                 {
360                         Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
361                                 TypeManager.CSharpName (type), name);
362                 }
363
364                 protected static void Error_ValueAssignment (Location loc)
365                 {
366                         Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
367                 }
368
369                 ResolveFlags ExprClassToResolveFlags
370                 {
371                         get {
372                                 switch (eclass) {
373                                         case ExprClass.Type:
374                                         case ExprClass.Namespace:
375                                                 return ResolveFlags.Type;
376
377                                         case ExprClass.MethodGroup:
378                                                 return ResolveFlags.MethodGroup;
379
380                                         case ExprClass.Value:
381                                         case ExprClass.Variable:
382                                         case ExprClass.PropertyAccess:
383                                         case ExprClass.EventAccess:
384                                         case ExprClass.IndexerAccess:
385                                                 return ResolveFlags.VariableOrValue;
386
387                                         default:
388                                                 throw new Exception ("Expression " + GetType () +
389                                                         " ExprClass is Invalid after resolve");
390                                 }
391                         }
392                 }
393                
394                 /// <summary>
395                 ///   Resolves an expression and performs semantic analysis on it.
396                 /// </summary>
397                 ///
398                 /// <remarks>
399                 ///   Currently Resolve wraps DoResolve to perform sanity
400                 ///   checking and assertion checking on what we expect from Resolve.
401                 /// </remarks>
402                 public Expression Resolve (EmitContext ec, ResolveFlags flags)
403                 {
404                         if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) 
405                                 return ResolveAsTypeStep (ec, false);
406
407                         bool do_flow_analysis = ec.DoFlowAnalysis;
408                         bool omit_struct_analysis = ec.OmitStructFlowAnalysis;
409                         if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
410                                 do_flow_analysis = false;
411                         if ((flags & ResolveFlags.DisableStructFlowAnalysis) != 0)
412                                 omit_struct_analysis = true;
413
414                         Expression e;
415                         using (ec.WithFlowAnalysis (do_flow_analysis, omit_struct_analysis)) {
416                                 if (this is SimpleName) {
417                                         bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
418                                         e = ((SimpleName) this).DoResolve (ec, intermediate);
419                                 } else {
420                                         e = DoResolve (ec);
421                                 }
422                         }
423
424                         if (e == null)
425                                 return null;
426
427                         if ((flags & e.ExprClassToResolveFlags) == 0) {
428                                 e.Error_UnexpectedKind (flags, loc);
429                                 return null;
430                         }
431
432                         if (e.type == null && !(e is Namespace)) {
433                                 throw new Exception (
434                                         "Expression " + e.GetType () +
435                                         " did not set its type after Resolve\n" +
436                                         "called from: " + this.GetType ());
437                         }
438
439                         return e;
440                 }
441
442                 /// <summary>
443                 ///   Resolves an expression and performs semantic analysis on it.
444                 /// </summary>
445                 public Expression Resolve (EmitContext ec)
446                 {
447                         Expression e = Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
448
449                         if (e != null && e.eclass == ExprClass.MethodGroup && RootContext.Version == LanguageVersion.ISO_1) {
450                                 ((MethodGroupExpr) e).ReportUsageError ();
451                                 return null;
452                         }
453                         return e;
454                 }
455
456                 public Constant ResolveAsConstant (EmitContext ec, MemberCore mc)
457                 {
458                         Expression e = Resolve (ec);
459                         if (e == null)
460                                 return null;
461
462                         Constant c = e as Constant;
463                         if (c != null)
464                                 return c;
465
466                         Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError ());
467                         return null;
468                 }
469
470                 /// <summary>
471                 ///   Resolves an expression for LValue assignment
472                 /// </summary>
473                 ///
474                 /// <remarks>
475                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
476                 ///   checking and assertion checking on what we expect from Resolve
477                 /// </remarks>
478                 public Expression ResolveLValue (EmitContext ec, Expression right_side, Location loc)
479                 {
480                         int errors = Report.Errors;
481                         bool out_access = right_side == EmptyExpression.OutAccess;
482
483                         Expression e = DoResolveLValue (ec, right_side);
484
485                         if (e != null && out_access && !(e is IMemoryLocation)) {
486                                 // FIXME: There's no problem with correctness, the 'Expr = null' handles that.
487                                 //        Enabling this 'throw' will "only" result in deleting useless code elsewhere,
488
489                                 //throw new InternalErrorException ("ResolveLValue didn't return an IMemoryLocation: " +
490                                 //                                e.GetType () + " " + e.GetSignatureForError ());
491                                 e = null;
492                         }
493
494                         if (e == null) {
495                                 if (errors == Report.Errors) {
496                                         if (out_access)
497                                                 Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
498                                         else
499                                                 Error_ValueAssignment (loc);
500                                 }
501                                 return null;
502                         }
503
504                         if (e.eclass == ExprClass.Invalid)
505                                 throw new Exception ("Expression " + e + " ExprClass is Invalid after resolve");
506
507                         if (e.eclass == ExprClass.MethodGroup) {
508                                 ((MethodGroupExpr) e).ReportUsageError ();
509                                 return null;
510                         }
511
512                         if ((e.type == null) && !(e is ConstructedType))
513                                 throw new Exception ("Expression " + e + " did not set its type after Resolve");
514
515                         return e;
516                 }
517
518                 /// <summary>
519                 ///   Emits the code for the expression
520                 /// </summary>
521                 ///
522                 /// <remarks>
523                 ///   The Emit method is invoked to generate the code
524                 ///   for the expression.  
525                 /// </remarks>
526                 public abstract void Emit (EmitContext ec);
527
528                 public virtual void EmitBranchable (EmitContext ec, Label target, bool onTrue)
529                 {
530                         Emit (ec);
531                         ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
532                 }
533
534                 /// <summary>
535                 ///   Protected constructor.  Only derivate types should
536                 ///   be able to be created
537                 /// </summary>
538
539                 protected Expression ()
540                 {
541                         eclass = ExprClass.Invalid;
542                         type = null;
543                 }
544
545                 /// <summary>
546                 ///   Returns a fully formed expression after a MemberLookup
547                 /// </summary>
548                 /// 
549                 public static Expression ExprClassFromMemberInfo (Type containerType, MemberInfo mi, Location loc)
550                 {
551                         if (mi is EventInfo)
552                                 return new EventExpr ((EventInfo) mi, loc);
553                         else if (mi is FieldInfo)
554                                 return new FieldExpr ((FieldInfo) mi, loc);
555                         else if (mi is PropertyInfo)
556                                 return new PropertyExpr (containerType, (PropertyInfo) mi, loc);
557                         else if (mi is Type){
558                                 return new TypeExpression ((System.Type) mi, loc);
559                         }
560
561                         return null;
562                 }
563
564                 protected static ArrayList almostMatchedMembers = new ArrayList (4);
565
566                 //
567                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
568                 //
569                 // This code could use some optimizations, but we need to do some
570                 // measurements.  For example, we could use a delegate to `flag' when
571                 // something can not any longer be a method-group (because it is something
572                 // else).
573                 //
574                 // Return values:
575                 //     If the return value is an Array, then it is an array of
576                 //     MethodBases
577                 //   
578                 //     If the return value is an MemberInfo, it is anything, but a Method
579                 //
580                 //     null on error.
581                 //
582                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
583                 // the arguments here and have MemberLookup return only the methods that
584                 // match the argument count/type, unlike we are doing now (we delay this
585                 // decision).
586                 //
587                 // This is so we can catch correctly attempts to invoke instance methods
588                 // from a static body (scan for error 120 in ResolveSimpleName).
589                 //
590                 //
591                 // FIXME: Potential optimization, have a static ArrayList
592                 //
593
594                 public static Expression MemberLookup (Type container_type, Type queried_type, string name,
595                                                        MemberTypes mt, BindingFlags bf, Location loc)
596                 {
597                         return MemberLookup (container_type, null, queried_type, name, mt, bf, loc);
598                 }
599
600                 //
601                 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
602                 // `qualifier_type' or null to lookup members in the current class.
603                 //
604
605                 public static Expression MemberLookup (Type container_type,
606                                                        Type qualifier_type, Type queried_type,
607                                                        string name, MemberTypes mt,
608                                                        BindingFlags bf, Location loc)
609                 {
610                         almostMatchedMembers.Clear ();
611
612                         MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
613                                                                      queried_type, mt, bf, name, almostMatchedMembers);
614
615                         if (mi == null)
616                                 return null;
617
618                         if (mi.Length > 1) {
619                                 bool is_interface = qualifier_type != null && qualifier_type.IsInterface;
620                                 MemberInfo non_method = null;
621                                 ArrayList methods = new ArrayList (2);
622
623                                 foreach (MemberInfo m in mi) {
624                                         if (m is MethodBase) {
625                                                 methods.Add (m);
626                                                 continue;
627                                         }
628
629                                         if (non_method == null) {
630                                                 non_method = m;
631                                                 continue;
632                                         }
633
634                                         Report.SymbolRelatedToPreviousError (m);
635                                         Report.SymbolRelatedToPreviousError (non_method);
636                                         Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
637                                                 TypeManager.GetFullNameSignature (m), TypeManager.GetFullNameSignature (non_method));
638                                         return null;
639                                 }
640
641                                 if (methods.Count == 0)
642                                         return null;
643
644                                 if (non_method != null) {
645                                         MethodBase method = (MethodBase) methods [0];
646
647                                         if (method.DeclaringType == non_method.DeclaringType) {
648                                                 // Cannot happen with C# code, but is valid in IL
649                                                 Report.SymbolRelatedToPreviousError (method);
650                                                 Report.SymbolRelatedToPreviousError (non_method);
651                                                 Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
652                                                               TypeManager.GetFullNameSignature (non_method),
653                                                               TypeManager.CSharpSignature (method));
654                                                 return null;
655                                         }
656
657                                         if (is_interface) {
658                                                 Report.SymbolRelatedToPreviousError (method);
659                                                 Report.SymbolRelatedToPreviousError (non_method);
660                                                 Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and non-method `{1}'. Using method `{0}'",
661                                                                 TypeManager.CSharpSignature (method), TypeManager.GetFullNameSignature (non_method));
662                                         }
663                                 }
664
665                                 return new MethodGroupExpr (methods, loc);
666                         }
667
668                         if (mi [0] is MethodBase)
669                                 return new MethodGroupExpr (mi, loc);
670
671                         return ExprClassFromMemberInfo (container_type, mi [0], loc);
672                 }
673
674                 public const MemberTypes AllMemberTypes =
675                         MemberTypes.Constructor |
676                         MemberTypes.Event       |
677                         MemberTypes.Field       |
678                         MemberTypes.Method      |
679                         MemberTypes.NestedType  |
680                         MemberTypes.Property;
681                 
682                 public const BindingFlags AllBindingFlags =
683                         BindingFlags.Public |
684                         BindingFlags.Static |
685                         BindingFlags.Instance;
686
687                 public static Expression MemberLookup (Type container_type, Type queried_type,
688                                                        string name, Location loc)
689                 {
690                         return MemberLookup (container_type, null, queried_type, name,
691                                              AllMemberTypes, AllBindingFlags, loc);
692                 }
693
694                 public static Expression MemberLookup (Type container_type, Type qualifier_type,
695                                                        Type queried_type, string name, Location loc)
696                 {
697                         return MemberLookup (container_type, qualifier_type, queried_type,
698                                              name, AllMemberTypes, AllBindingFlags, loc);
699                 }
700
701                 public static Expression MethodLookup (Type container_type, Type queried_type,
702                                                        string name, Location loc)
703                 {
704                         return MemberLookup (container_type, null, queried_type, name,
705                                              MemberTypes.Method, AllBindingFlags, loc);
706                 }
707
708                 /// <summary>
709                 ///   This is a wrapper for MemberLookup that is not used to "probe", but
710                 ///   to find a final definition.  If the final definition is not found, we
711                 ///   look for private members and display a useful debugging message if we
712                 ///   find it.
713                 /// </summary>
714                 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
715                                                             Type queried_type, string name, Location loc)
716                 {
717                         return MemberLookupFinal (ec, qualifier_type, queried_type, name,
718                                                   AllMemberTypes, AllBindingFlags, loc);
719                 }
720
721                 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
722                                                             Type queried_type, string name,
723                                                             MemberTypes mt, BindingFlags bf,
724                                                             Location loc)
725                 {
726                         Expression e;
727
728                         int errors = Report.Errors;
729
730                         e = MemberLookup (ec.ContainerType, qualifier_type, queried_type, name, mt, bf, loc);
731
732                         if (e == null && errors == Report.Errors)
733                                 // No errors were reported by MemberLookup, but there was an error.
734                                 MemberLookupFailed (ec.ContainerType, qualifier_type, queried_type, name, null, true, loc);
735
736                         return e;
737                 }
738
739                 public static void MemberLookupFailed (Type container_type, Type qualifier_type,
740                                                        Type queried_type, string name,
741                                                        string class_name, bool complain_if_none_found, 
742                                                        Location loc)
743                 {
744                         if (almostMatchedMembers.Count != 0) {
745                                 for (int i = 0; i < almostMatchedMembers.Count; ++i) {
746                                         MemberInfo m = (MemberInfo) almostMatchedMembers [i];
747                                         for (int j = 0; j < i; ++j) {
748                                                 if (m == almostMatchedMembers [j]) {
749                                                         m = null;
750                                                         break;
751                                                 }
752                                         }
753                                         if (m == null)
754                                                 continue;
755                                         
756                                         Type declaring_type = m.DeclaringType;
757                                         
758                                         Report.SymbolRelatedToPreviousError (m);
759                                         if (qualifier_type == null) {
760                                                 Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
761                                                               TypeManager.CSharpName (m.DeclaringType),
762                                                               TypeManager.CSharpName (container_type));
763                                                 
764                                         } else if (qualifier_type != container_type &&
765                                                    TypeManager.IsNestedFamilyAccessible (container_type, declaring_type)) {
766                                                 // Although a derived class can access protected members of
767                                                 // its base class it cannot do so through an instance of the
768                                                 // base class (CS1540).  If the qualifier_type is a base of the
769                                                 // ec.ContainerType and the lookup succeeds with the latter one,
770                                                 // then we are in this situation.
771                                                 Error_CannotAccessProtected (loc, m, qualifier_type, container_type);
772                                         } else {
773                                                 ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m));
774                                         }
775                                 }
776                                 almostMatchedMembers.Clear ();
777                                 return;
778                         }
779
780                         MemberInfo[] lookup = null;
781                         if (queried_type == null) {
782                                 class_name = "global::";
783                         } else {
784                                 lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
785                                         AllMemberTypes, AllBindingFlags |
786                                         BindingFlags.NonPublic, name, null);
787                         }
788
789                         if (lookup == null) {
790                                 if (!complain_if_none_found)
791                                         return;
792
793                                 if (class_name != null)
794                                         Report.Error (103, loc, "The name `{0}' does not exist in the context of `{1}'",
795                                                 name, class_name);
796                                 else
797                                         Error_TypeDoesNotContainDefinition (loc, queried_type, name);
798                                 return;
799                         }
800
801                         if (TypeManager.MemberLookup (queried_type, null, queried_type,
802                                                       AllMemberTypes, AllBindingFlags |
803                                                       BindingFlags.NonPublic, name, null) == null) {
804                                 if ((lookup.Length == 1) && (lookup [0] is Type)) {
805                                         Type t = (Type) lookup [0];
806
807                                         Report.Error (305, loc,
808                                                       "Using the generic type `{0}' " +
809                                                       "requires {1} type arguments",
810                                                       TypeManager.CSharpName (t),
811                                                       TypeManager.GetNumberOfTypeArguments (t).ToString ());
812                                         return;
813                                 }
814                         }
815
816                         MemberList ml = TypeManager.FindMembers (queried_type, MemberTypes.Constructor,
817                                                                  BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null);
818                         if (name == ".ctor" && ml.Count == 0)
819                         {
820                                 Report.Error (143, loc, "The type `{0}' has no constructors defined", TypeManager.CSharpName (queried_type));
821                                 return;
822                         }
823
824                         Report.SymbolRelatedToPreviousError (lookup [0]);
825                         ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (lookup [0]));
826                 }
827
828                 /// <summary>
829                 ///   Returns an expression that can be used to invoke operator true
830                 ///   on the expression if it exists.
831                 /// </summary>
832                 static public Expression GetOperatorTrue (EmitContext ec, Expression e, Location loc)
833                 {
834                         return GetOperatorTrueOrFalse (ec, e, true, loc);
835                 }
836
837                 /// <summary>
838                 ///   Returns an expression that can be used to invoke operator false
839                 ///   on the expression if it exists.
840                 /// </summary>
841                 static public Expression GetOperatorFalse (EmitContext ec, Expression e, Location loc)
842                 {
843                         return GetOperatorTrueOrFalse (ec, e, false, loc);
844                 }
845
846                 static Expression GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
847                 {
848                         MethodBase method;
849                         Expression operator_group;
850
851 #if GMCS_SOURCE
852                         if (TypeManager.IsNullableType (e.Type))
853                                 return new Nullable.OperatorTrueOrFalse (e, is_true, loc).Resolve (ec);
854 #endif
855
856                         operator_group = MethodLookup (ec.ContainerType, e.Type, is_true ? "op_True" : "op_False", loc);
857                         if (operator_group == null)
858                                 return null;
859
860                         ArrayList arguments = new ArrayList ();
861                         arguments.Add (new Argument (e, Argument.AType.Expression));
862                         method = Invocation.OverloadResolve (
863                                 ec, (MethodGroupExpr) operator_group, arguments, false, loc);
864
865                         if (method == null)
866                                 return null;
867
868                         return new StaticCallExpr ((MethodInfo) method, arguments, loc);
869                 }
870
871                 /// <summary>
872                 ///   Resolves the expression `e' into a boolean expression: either through
873                 ///   an implicit conversion, or through an `operator true' invocation
874                 /// </summary>
875                 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
876                 {
877                         e = e.Resolve (ec);
878                         if (e == null)
879                                 return null;
880
881                         if (e.Type == TypeManager.bool_type)
882                                 return e;
883
884                         Expression converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, Location.Null);
885
886                         if (converted != null)
887                                 return converted;
888
889                         //
890                         // If no implicit conversion to bool exists, try using `operator true'
891                         //
892                         converted = Expression.GetOperatorTrue (ec, e, loc);
893                         if (converted == null){
894                                 e.Error_ValueCannotBeConverted (ec, loc, TypeManager.bool_type, false);
895                                 return null;
896                         }
897                         return converted;
898                 }
899                 
900                 public virtual string ExprClassName
901                 {
902                         get {
903                                 switch (eclass){
904                                         case ExprClass.Invalid:
905                                                 return "Invalid";
906                                         case ExprClass.Value:
907                                                 return "value";
908                                         case ExprClass.Variable:
909                                                 return "variable";
910                                         case ExprClass.Namespace:
911                                                 return "namespace";
912                                         case ExprClass.Type:
913                                                 return "type";
914                                         case ExprClass.MethodGroup:
915                                                 return "method group";
916                                         case ExprClass.PropertyAccess:
917                                                 return "property access";
918                                         case ExprClass.EventAccess:
919                                                 return "event access";
920                                         case ExprClass.IndexerAccess:
921                                                 return "indexer access";
922                                         case ExprClass.Nothing:
923                                                 return "null";
924                                 }
925                                 throw new Exception ("Should not happen");
926                         }
927                 }
928                 
929                 /// <summary>
930                 ///   Reports that we were expecting `expr' to be of class `expected'
931                 /// </summary>
932                 public void Error_UnexpectedKind (DeclSpace ds, string expected, Location loc)
933                 {
934                         Error_UnexpectedKind (ds, expected, ExprClassName, loc);
935                 }
936
937                 public void Error_UnexpectedKind (DeclSpace ds, string expected, string was, Location loc)
938                 {
939                         string name = GetSignatureForError ();
940                         if (ds != null)
941                                 name = ds.GetSignatureForError () + '.' + name;
942
943                         Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected",
944                               name, was, expected);
945                 }
946
947                 public void Error_UnexpectedKind (ResolveFlags flags, Location loc)
948                 {
949                         string [] valid = new string [4];
950                         int count = 0;
951
952                         if ((flags & ResolveFlags.VariableOrValue) != 0) {
953                                 valid [count++] = "variable";
954                                 valid [count++] = "value";
955                         }
956
957                         if ((flags & ResolveFlags.Type) != 0)
958                                 valid [count++] = "type";
959
960                         if ((flags & ResolveFlags.MethodGroup) != 0)
961                                 valid [count++] = "method group";
962
963                         if (count == 0)
964                                 valid [count++] = "unknown";
965
966                         StringBuilder sb = new StringBuilder (valid [0]);
967                         for (int i = 1; i < count - 1; i++) {
968                                 sb.Append ("', `");
969                                 sb.Append (valid [i]);
970                         }
971                         if (count > 1) {
972                                 sb.Append ("' or `");
973                                 sb.Append (valid [count - 1]);
974                         }
975
976                         Report.Error (119, loc, 
977                                 "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName, sb.ToString ());
978                 }
979                 
980                 public static void UnsafeError (Location loc)
981                 {
982                         Report.Error (214, loc, "Pointers and fixed size buffers may only be used in an unsafe context");
983                 }
984
985                 //
986                 // Load the object from the pointer.  
987                 //
988                 public static void LoadFromPtr (ILGenerator ig, Type t)
989                 {
990                         if (t == TypeManager.int32_type)
991                                 ig.Emit (OpCodes.Ldind_I4);
992                         else if (t == TypeManager.uint32_type)
993                                 ig.Emit (OpCodes.Ldind_U4);
994                         else if (t == TypeManager.short_type)
995                                 ig.Emit (OpCodes.Ldind_I2);
996                         else if (t == TypeManager.ushort_type)
997                                 ig.Emit (OpCodes.Ldind_U2);
998                         else if (t == TypeManager.char_type)
999                                 ig.Emit (OpCodes.Ldind_U2);
1000                         else if (t == TypeManager.byte_type)
1001                                 ig.Emit (OpCodes.Ldind_U1);
1002                         else if (t == TypeManager.sbyte_type)
1003                                 ig.Emit (OpCodes.Ldind_I1);
1004                         else if (t == TypeManager.uint64_type)
1005                                 ig.Emit (OpCodes.Ldind_I8);
1006                         else if (t == TypeManager.int64_type)
1007                                 ig.Emit (OpCodes.Ldind_I8);
1008                         else if (t == TypeManager.float_type)
1009                                 ig.Emit (OpCodes.Ldind_R4);
1010                         else if (t == TypeManager.double_type)
1011                                 ig.Emit (OpCodes.Ldind_R8);
1012                         else if (t == TypeManager.bool_type)
1013                                 ig.Emit (OpCodes.Ldind_I1);
1014                         else if (t == TypeManager.intptr_type)
1015                                 ig.Emit (OpCodes.Ldind_I);
1016                         else if (TypeManager.IsEnumType (t)) {
1017                                 if (t == TypeManager.enum_type)
1018                                         ig.Emit (OpCodes.Ldind_Ref);
1019                                 else
1020                                         LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
1021                         } else if (t.IsValueType || TypeManager.IsGenericParameter (t))
1022                                 ig.Emit (OpCodes.Ldobj, t);
1023                         else if (t.IsPointer)
1024                                 ig.Emit (OpCodes.Ldind_I);
1025                         else
1026                                 ig.Emit (OpCodes.Ldind_Ref);
1027                 }
1028
1029                 //
1030                 // The stack contains the pointer and the value of type `type'
1031                 //
1032                 public static void StoreFromPtr (ILGenerator ig, Type type)
1033                 {
1034                         if (TypeManager.IsEnumType (type))
1035                                 type = TypeManager.EnumToUnderlying (type);
1036                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1037                                 ig.Emit (OpCodes.Stind_I4);
1038                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1039                                 ig.Emit (OpCodes.Stind_I8);
1040                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1041                                  type == TypeManager.ushort_type)
1042                                 ig.Emit (OpCodes.Stind_I2);
1043                         else if (type == TypeManager.float_type)
1044                                 ig.Emit (OpCodes.Stind_R4);
1045                         else if (type == TypeManager.double_type)
1046                                 ig.Emit (OpCodes.Stind_R8);
1047                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1048                                  type == TypeManager.bool_type)
1049                                 ig.Emit (OpCodes.Stind_I1);
1050                         else if (type == TypeManager.intptr_type)
1051                                 ig.Emit (OpCodes.Stind_I);
1052                         else if (type.IsValueType || TypeManager.IsGenericParameter (type))
1053                                 ig.Emit (OpCodes.Stobj, type);
1054                         else
1055                                 ig.Emit (OpCodes.Stind_Ref);
1056                 }
1057                 
1058                 //
1059                 // Returns the size of type `t' if known, otherwise, 0
1060                 //
1061                 public static int GetTypeSize (Type t)
1062                 {
1063                         t = TypeManager.TypeToCoreType (t);
1064                         if (t == TypeManager.int32_type ||
1065                             t == TypeManager.uint32_type ||
1066                             t == TypeManager.float_type)
1067                                 return 4;
1068                         else if (t == TypeManager.int64_type ||
1069                                  t == TypeManager.uint64_type ||
1070                                  t == TypeManager.double_type)
1071                                 return 8;
1072                         else if (t == TypeManager.byte_type ||
1073                                  t == TypeManager.sbyte_type ||
1074                                  t == TypeManager.bool_type)    
1075                                 return 1;
1076                         else if (t == TypeManager.short_type ||
1077                                  t == TypeManager.char_type ||
1078                                  t == TypeManager.ushort_type)
1079                                 return 2;
1080                         else if (t == TypeManager.decimal_type)
1081                                 return 16;
1082                         else
1083                                 return 0;
1084                 }
1085
1086                 public static void Error_NegativeArrayIndex (Location loc)
1087                 {
1088                         Report.Error (248, loc, "Cannot create an array with a negative size");
1089                 }
1090
1091                 protected void Error_CannotCallAbstractBase (string name)
1092                 {
1093                         Report.Error (205, loc, "Cannot call an abstract base member `{0}'", name);
1094                 }
1095                 
1096                 //
1097                 // Converts `source' to an int, uint, long or ulong.
1098                 //
1099                 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
1100                 {
1101                         Expression target;
1102                         
1103                         using (ec.With (EmitContext.Flags.CheckState, true)) {
1104                                 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
1105                                 if (target == null)
1106                                         target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
1107                                 if (target == null)
1108                                         target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
1109                                 if (target == null)
1110                                         target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
1111
1112                                 if (target == null) {
1113                                         source.Error_ValueCannotBeConverted (ec, loc, TypeManager.int32_type, false);
1114                                         return null;
1115                                 }
1116                         }
1117
1118                         //
1119                         // Only positive constants are allowed at compile time
1120                         //
1121                         if (target is Constant){
1122                                 if (target is IntConstant){
1123                                         if (((IntConstant) target).Value < 0){
1124                                                 Error_NegativeArrayIndex (loc);
1125                                                 return null;
1126                                         }
1127                                 }
1128
1129                                 if (target is LongConstant){
1130                                         if (((LongConstant) target).Value < 0){
1131                                                 Error_NegativeArrayIndex (loc);
1132                                                 return null;
1133                                         }
1134                                 }
1135                                 
1136                         }
1137
1138                         return target;
1139                 }
1140                 
1141         }
1142
1143         /// <summary>
1144         ///   This is just a base class for expressions that can
1145         ///   appear on statements (invocations, object creation,
1146         ///   assignments, post/pre increment and decrement).  The idea
1147         ///   being that they would support an extra Emition interface that
1148         ///   does not leave a result on the stack.
1149         /// </summary>
1150         public abstract class ExpressionStatement : Expression {
1151
1152                 public virtual ExpressionStatement ResolveStatement (EmitContext ec)
1153                 {
1154                         Expression e = Resolve (ec);
1155                         if (e == null)
1156                                 return null;
1157
1158                         ExpressionStatement es = e as ExpressionStatement;
1159                         if (es == null)
1160                                 Error (201, "Only assignment, call, increment, decrement and new object " +
1161                                        "expressions can be used as a statement");
1162
1163                         return es;
1164                 }
1165
1166                 /// <summary>
1167                 ///   Requests the expression to be emitted in a `statement'
1168                 ///   context.  This means that no new value is left on the
1169                 ///   stack after invoking this method (constrasted with
1170                 ///   Emit that will always leave a value on the stack).
1171                 /// </summary>
1172                 public abstract void EmitStatement (EmitContext ec);
1173         }
1174
1175         /// <summary>
1176         ///   This kind of cast is used to encapsulate the child
1177         ///   whose type is child.Type into an expression that is
1178         ///   reported to return "return_type".  This is used to encapsulate
1179         ///   expressions which have compatible types, but need to be dealt
1180         ///   at higher levels with.
1181         ///
1182         ///   For example, a "byte" expression could be encapsulated in one
1183         ///   of these as an "unsigned int".  The type for the expression
1184         ///   would be "unsigned int".
1185         ///
1186         /// </summary>
1187         public class EmptyCast : Expression {
1188                 protected readonly Expression child;
1189
1190                 public EmptyCast (Expression child, Type return_type)
1191                 {
1192                         eclass = child.eclass;
1193                         loc = child.Location;
1194                         type = return_type;
1195                         this.child = child;
1196                 }
1197
1198                 public override Expression DoResolve (EmitContext ec)
1199                 {
1200                         // This should never be invoked, we are born in fully
1201                         // initialized state.
1202
1203                         return this;
1204                 }
1205
1206                 public override void Emit (EmitContext ec)
1207                 {
1208                         child.Emit (ec);
1209                 }
1210
1211                 public override bool GetAttributableValue (Type valueType, out object value)
1212                 {
1213                         return child.GetAttributableValue (valueType, out value);
1214                 }
1215
1216         }
1217
1218         /// <summary>
1219         ///    Performs a cast using an operator (op_Explicit or op_Implicit)
1220         /// </summary>
1221         public class OperatorCast : EmptyCast {
1222                 MethodInfo conversion_operator;
1223                 bool find_explicit;
1224                         
1225                 public OperatorCast (Expression child, Type target_type) : this (child, target_type, false) {}
1226
1227                 public OperatorCast (Expression child, Type target_type, bool find_explicit)
1228                         : base (child, target_type)
1229                 {
1230                         this.find_explicit = find_explicit;
1231                 }
1232
1233                 // Returns the implicit operator that converts from
1234                 // 'child.Type' to our target type (type)
1235                 MethodInfo GetConversionOperator (bool find_explicit)
1236                 {
1237                         string operator_name = find_explicit ? "op_Explicit" : "op_Implicit";
1238
1239                         MemberInfo [] mi;
1240
1241                         mi = TypeManager.MemberLookup (child.Type, child.Type, child.Type, MemberTypes.Method,
1242                                 BindingFlags.Static | BindingFlags.Public, operator_name, null);
1243
1244                         if (mi == null){
1245                                 mi = TypeManager.MemberLookup (type, type, type, MemberTypes.Method,
1246                                                                BindingFlags.Static | BindingFlags.Public, operator_name, null);
1247                         }
1248                         
1249                         foreach (MethodInfo oper in mi) {
1250                                 ParameterData pd = TypeManager.GetParameterData (oper);
1251
1252                                 if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
1253                                         return oper;
1254                         }
1255
1256                         return null;
1257
1258
1259                 }
1260                 public override void Emit (EmitContext ec)
1261                 {
1262                         ILGenerator ig = ec.ig;
1263
1264                         child.Emit (ec);
1265                         conversion_operator = GetConversionOperator (find_explicit);
1266
1267                         if (conversion_operator == null)
1268                                 throw new InternalErrorException ("Outer conversion routine is out of sync");
1269
1270                         ig.Emit (OpCodes.Call, conversion_operator);
1271                 }
1272                 
1273         }
1274         
1275         /// <summary>
1276         ///     This is a numeric cast to a Decimal
1277         /// </summary>
1278         public class CastToDecimal : EmptyCast {
1279                 MethodInfo conversion_operator;
1280
1281                 public CastToDecimal (Expression child)
1282                         : this (child, false)
1283                 {
1284                 }
1285
1286                 public CastToDecimal (Expression child, bool find_explicit)
1287                         : base (child, TypeManager.decimal_type)
1288                 {
1289                         conversion_operator = GetConversionOperator (find_explicit);
1290
1291                         if (conversion_operator == null)
1292                                 throw new InternalErrorException ("Outer conversion routine is out of sync");
1293                 }
1294
1295                 // Returns the implicit operator that converts from
1296                 // 'child.Type' to System.Decimal.
1297                 MethodInfo GetConversionOperator (bool find_explicit)
1298                 {
1299                         string operator_name = find_explicit ? "op_Explicit" : "op_Implicit";
1300                         
1301                         MemberInfo [] mi = TypeManager.MemberLookup (type, type, type, MemberTypes.Method,
1302                                 BindingFlags.Static | BindingFlags.Public, operator_name, null);
1303
1304                         foreach (MethodInfo oper in mi) {
1305                                 ParameterData pd = TypeManager.GetParameterData (oper);
1306
1307                                 if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
1308                                         return oper;
1309                         }
1310
1311                         return null;
1312                 }
1313                 public override void Emit (EmitContext ec)
1314                 {
1315                         ILGenerator ig = ec.ig;
1316                         child.Emit (ec);
1317
1318                         ig.Emit (OpCodes.Call, conversion_operator);
1319                 }
1320         }
1321
1322         /// <summary>
1323         ///     This is an explicit numeric cast from a Decimal
1324         /// </summary>
1325         public class CastFromDecimal : EmptyCast
1326         {
1327                 static IDictionary operators;
1328
1329                 public CastFromDecimal (Expression child, Type return_type)
1330                         : base (child, return_type)
1331                 {
1332                         if (child.Type != TypeManager.decimal_type)
1333                                 throw new InternalErrorException (
1334                                         "The expected type is Decimal, instead it is " + child.Type.FullName);
1335                 }
1336
1337                 // Returns the explicit operator that converts from an
1338                 // express of type System.Decimal to 'type'.
1339                 public Expression Resolve ()
1340                 {
1341                         if (operators == null) {
1342                                  MemberInfo[] all_oper = TypeManager.MemberLookup (TypeManager.decimal_type,
1343                                         TypeManager.decimal_type, TypeManager.decimal_type, MemberTypes.Method,
1344                                         BindingFlags.Static | BindingFlags.Public, "op_Explicit", null);
1345
1346                                 operators = new System.Collections.Specialized.HybridDictionary ();
1347                                 foreach (MethodInfo oper in all_oper) {
1348                                         ParameterData pd = TypeManager.GetParameterData (oper);
1349                                         if (pd.ParameterType (0) == TypeManager.decimal_type)
1350                                                 operators.Add (oper.ReturnType, oper);
1351                                 }
1352                         }
1353
1354                         return operators.Contains (type) ? this : null;
1355                 }
1356
1357                 public override void Emit (EmitContext ec)
1358                 {
1359                         ILGenerator ig = ec.ig;
1360                         child.Emit (ec);
1361
1362                         ig.Emit (OpCodes.Call, (MethodInfo)operators [type]);
1363                 }
1364         }
1365
1366         
1367         //
1368         // Constant specialization of EmptyCast.
1369         // We need to special case this since an empty cast of
1370         // a constant is still a constant. 
1371         //
1372         public class EmptyConstantCast : Constant
1373         {
1374                 public readonly Constant child;
1375
1376                 public EmptyConstantCast(Constant child, Type type)
1377                         : base (child.Location)
1378                 {
1379                         eclass = child.eclass;
1380                         this.child = child;
1381                         this.type = type;
1382                 }
1383
1384                 public override string AsString ()
1385                 {
1386                         return child.AsString ();
1387                 }
1388
1389                 public override object GetValue ()
1390                 {
1391                         return child.GetValue ();
1392                 }
1393
1394                 public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
1395                 {
1396                         // FIXME: check that 'type' can be converted to 'target_type' first
1397                         return child.ConvertExplicitly (inCheckedContext, target_type);
1398                 }
1399
1400                 public override Constant Increment ()
1401                 {
1402                         return child.Increment ();
1403                 }
1404
1405                 public override bool IsDefaultValue {
1406                         get { return child.IsDefaultValue; }
1407                 }
1408
1409                 public override bool IsNegative {
1410                         get { return child.IsNegative; }
1411                 }
1412
1413                 public override void Emit (EmitContext ec)
1414                 {
1415                         child.Emit (ec);
1416                 }
1417
1418                 public override Constant ConvertImplicitly (Type target_type)
1419                 {
1420                         // FIXME: Do we need to check user conversions?
1421                         if (!Convert.ImplicitStandardConversionExists (this, target_type))
1422                                 return null;
1423                         return child.ConvertImplicitly (target_type);
1424                 }
1425         }
1426
1427
1428         /// <summary>
1429         ///  This class is used to wrap literals which belong inside Enums
1430         /// </summary>
1431         public class EnumConstant : Constant {
1432                 public Constant Child;
1433
1434                 public EnumConstant (Constant child, Type enum_type):
1435                         base (child.Location)
1436                 {
1437                         eclass = child.eclass;
1438                         this.Child = child;
1439                         type = enum_type;
1440                 }
1441                 
1442                 public override Expression DoResolve (EmitContext ec)
1443                 {
1444                         // This should never be invoked, we are born in fully
1445                         // initialized state.
1446
1447                         return this;
1448                 }
1449
1450                 public override void Emit (EmitContext ec)
1451                 {
1452                         Child.Emit (ec);
1453                 }
1454
1455                 public override bool GetAttributableValue (Type valueType, out object value)
1456                 {
1457                         value = GetTypedValue ();
1458                         return true;
1459                 }
1460
1461                 public override string GetSignatureForError()
1462                 {
1463                         return TypeManager.CSharpName (Type);
1464                 }
1465
1466                 public override object GetValue ()
1467                 {
1468                         return Child.GetValue ();
1469                 }
1470
1471                 public override object GetTypedValue ()
1472                 {
1473                         // FIXME: runtime is not ready to work with just emited enums
1474                         if (!RootContext.StdLib) {
1475                                 return Child.GetValue ();
1476                         }
1477
1478                         return System.Enum.ToObject (type, Child.GetValue ());
1479                 }
1480                 
1481                 public override string AsString ()
1482                 {
1483                         string value = System.Enum.GetName (type, Child.GetValue ());
1484                         return value == null ? "0" : value;
1485                 }
1486
1487                 public override Constant Increment()
1488                 {
1489                         return new EnumConstant (Child.Increment (), type);
1490                 }
1491
1492                 public override bool IsDefaultValue {
1493                         get {
1494                                 return Child.IsDefaultValue;
1495                         }
1496                 }
1497
1498                 public override bool IsZeroInteger {
1499                         get { return Child.IsZeroInteger; }
1500                 }
1501
1502                 public override bool IsNegative {
1503                         get {
1504                                 return Child.IsNegative;
1505                         }
1506                 }
1507
1508                 public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
1509                 {
1510                         if (Child.Type == target_type)
1511                                 return Child;
1512
1513                         return Child.ConvertExplicitly (inCheckedContext, target_type);
1514                 }
1515
1516                 public override Constant ConvertImplicitly (Type type)
1517                 {
1518                         if (Type == type) {
1519                                 // This is workaround of mono bug. It can be removed when the latest corlib spreads enough
1520                                 if (TypeManager.IsEnumType (type.UnderlyingSystemType))
1521                                         return this;
1522
1523                                 if (type.UnderlyingSystemType != Child.Type)
1524                                         Child = Child.ConvertImplicitly (type.UnderlyingSystemType);
1525                                 return this;
1526                         }
1527
1528                         if (!Convert.ImplicitStandardConversionExists (this, type)){
1529                                 return null;
1530                         }
1531
1532                         return Child.ConvertImplicitly(type);
1533                 }
1534
1535         }
1536
1537         /// <summary>
1538         ///   This kind of cast is used to encapsulate Value Types in objects.
1539         ///
1540         ///   The effect of it is to box the value type emitted by the previous
1541         ///   operation.
1542         /// </summary>
1543         public class BoxedCast : EmptyCast {
1544
1545                 public BoxedCast (Expression expr, Type target_type)
1546                         : base (expr, target_type)
1547                 {
1548                         eclass = ExprClass.Value;
1549                 }
1550                 
1551                 public override Expression DoResolve (EmitContext ec)
1552                 {
1553                         // This should never be invoked, we are born in fully
1554                         // initialized state.
1555
1556                         return this;
1557                 }
1558
1559                 public override void Emit (EmitContext ec)
1560                 {
1561                         base.Emit (ec);
1562                         
1563                         ec.ig.Emit (OpCodes.Box, child.Type);
1564                 }
1565         }
1566
1567         public class UnboxCast : EmptyCast {
1568                 public UnboxCast (Expression expr, Type return_type)
1569                         : base (expr, return_type)
1570                 {
1571                 }
1572
1573                 public override Expression DoResolve (EmitContext ec)
1574                 {
1575                         // This should never be invoked, we are born in fully
1576                         // initialized state.
1577
1578                         return this;
1579                 }
1580
1581                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1582                 {
1583                         if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess)
1584                                 Report.Error (445, loc, "Cannot modify the result of an unboxing conversion");
1585                         return base.DoResolveLValue (ec, right_side);
1586                 }
1587
1588                 public override void Emit (EmitContext ec)
1589                 {
1590                         Type t = type;
1591                         ILGenerator ig = ec.ig;
1592                         
1593                         base.Emit (ec);
1594 #if GMCS_SOURCE
1595                         if (t.IsGenericParameter || t.IsGenericType && t.IsValueType)
1596                                 ig.Emit (OpCodes.Unbox_Any, t);
1597                         else
1598 #endif
1599                         {
1600                                 ig.Emit (OpCodes.Unbox, t);
1601
1602                                 LoadFromPtr (ig, t);
1603                         }
1604                 }
1605         }
1606         
1607         /// <summary>
1608         ///   This is used to perform explicit numeric conversions.
1609         ///
1610         ///   Explicit numeric conversions might trigger exceptions in a checked
1611         ///   context, so they should generate the conv.ovf opcodes instead of
1612         ///   conv opcodes.
1613         /// </summary>
1614         public class ConvCast : EmptyCast {
1615                 public enum Mode : byte {
1616                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1617                         U1_I1, U1_CH,
1618                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1619                         U2_I1, U2_U1, U2_I2, U2_CH,
1620                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1621                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1622                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1623                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1624                         CH_I1, CH_U1, CH_I2,
1625                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1626                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1627                 }
1628
1629                 Mode mode;
1630                 
1631                 public ConvCast (Expression child, Type return_type, Mode m)
1632                         : base (child, return_type)
1633                 {
1634                         mode = m;
1635                 }
1636
1637                 public override Expression DoResolve (EmitContext ec)
1638                 {
1639                         // This should never be invoked, we are born in fully
1640                         // initialized state.
1641
1642                         return this;
1643                 }
1644
1645                 public override string ToString ()
1646                 {
1647                         return String.Format ("ConvCast ({0}, {1})", mode, child);
1648                 }
1649                 
1650                 public override void Emit (EmitContext ec)
1651                 {
1652                         ILGenerator ig = ec.ig;
1653                         
1654                         base.Emit (ec);
1655
1656                         if (ec.CheckState){
1657                                 switch (mode){
1658                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1659                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1660                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1661                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1662                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1663
1664                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1665                                 case Mode.U1_CH: /* nothing */ break;
1666
1667                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1668                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1669                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1670                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1671                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1672                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1673
1674                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1675                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1676                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1677                                 case Mode.U2_CH: /* nothing */ break;
1678
1679                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1680                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1681                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1682                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1683                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1684                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1685                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1686
1687                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1688                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1689                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1690                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1691                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1692                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1693
1694                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1695                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1696                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1697                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1698                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1699                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1700                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1701                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1702
1703                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1704                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1705                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1706                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1707                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1708                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1709                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1710                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1711
1712                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1713                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1714                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1715
1716                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1717                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1718                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1719                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1720                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1721                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1722                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1723                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1724                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1725
1726                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1727                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1728                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1729                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1730                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1731                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1732                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1733                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1734                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1735                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1736                                 }
1737                         } else {
1738                                 switch (mode){
1739                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1740                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1741                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1742                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1743                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1744
1745                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1746                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1747
1748                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1749                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1750                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1751                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1752                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1753                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1754
1755                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1756                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1757                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1758                                 case Mode.U2_CH: /* nothing */ break;
1759
1760                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1761                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1762                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1763                                 case Mode.I4_U4: /* nothing */ break;
1764                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1765                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1766                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1767
1768                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1769                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1770                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1771                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1772                                 case Mode.U4_I4: /* nothing */ break;
1773                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1774
1775                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1776                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1777                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1778                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1779                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1780                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1781                                 case Mode.I8_U8: /* nothing */ break;
1782                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1783
1784                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1785                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1786                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1787                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1788                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1789                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1790                                 case Mode.U8_I8: /* nothing */ break;
1791                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1792
1793                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1794                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1795                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1796
1797                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1798                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1799                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1800                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1801                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1802                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1803                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1804                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1805                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1806
1807                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1808                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1809                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1810                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1811                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1812                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1813                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1814                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1815                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1816                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1817                                 }
1818                         }
1819                 }
1820         }
1821         
1822         public class OpcodeCast : EmptyCast {
1823                 OpCode op, op2;
1824                 bool second_valid;
1825                 
1826                 public OpcodeCast (Expression child, Type return_type, OpCode op)
1827                         : base (child, return_type)
1828                         
1829                 {
1830                         this.op = op;
1831                         second_valid = false;
1832                 }
1833
1834                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1835                         : base (child, return_type)
1836                         
1837                 {
1838                         this.op = op;
1839                         this.op2 = op2;
1840                         second_valid = true;
1841                 }
1842
1843                 public override Expression DoResolve (EmitContext ec)
1844                 {
1845                         // This should never be invoked, we are born in fully
1846                         // initialized state.
1847
1848                         return this;
1849                 }
1850
1851                 public override void Emit (EmitContext ec)
1852                 {
1853                         base.Emit (ec);
1854                         ec.ig.Emit (op);
1855
1856                         if (second_valid)
1857                                 ec.ig.Emit (op2);
1858                 }                       
1859         }
1860
1861         /// <summary>
1862         ///   This kind of cast is used to encapsulate a child and cast it
1863         ///   to the class requested
1864         /// </summary>
1865         public class ClassCast : EmptyCast {
1866                 public ClassCast (Expression child, Type return_type)
1867                         : base (child, return_type)
1868                         
1869                 {
1870                 }
1871
1872                 public override Expression DoResolve (EmitContext ec)
1873                 {
1874                         // This should never be invoked, we are born in fully
1875                         // initialized state.
1876
1877                         return this;
1878                 }
1879
1880                 public override void Emit (EmitContext ec)
1881                 {
1882                         base.Emit (ec);
1883
1884                         if (TypeManager.IsGenericParameter (child.Type))
1885                                 ec.ig.Emit (OpCodes.Box, child.Type);
1886
1887 #if GMCS_SOURCE
1888                         if (type.IsGenericParameter)
1889                                 ec.ig.Emit (OpCodes.Unbox_Any, type);
1890                         else
1891 #endif
1892                                 ec.ig.Emit (OpCodes.Castclass, type);
1893                 }
1894         }
1895         
1896         /// <summary>
1897         ///   SimpleName expressions are formed of a single word and only happen at the beginning 
1898         ///   of a dotted-name.
1899         /// </summary>
1900         public class SimpleName : Expression {
1901                 public string Name;
1902                 public readonly TypeArguments Arguments;
1903                 bool in_transit;
1904
1905                 public SimpleName (string name, Location l)
1906                 {
1907                         Name = name;
1908                         loc = l;
1909                 }
1910
1911                 public SimpleName (string name, TypeArguments args, Location l)
1912                 {
1913                         Name = name;
1914                         Arguments = args;
1915                         loc = l;
1916                 }
1917
1918                 public SimpleName (string name, TypeParameter[] type_params, Location l)
1919                 {
1920                         Name = name;
1921                         loc = l;
1922
1923                         Arguments = new TypeArguments (l);
1924                         foreach (TypeParameter type_param in type_params)
1925                                 Arguments.Add (new TypeParameterExpr (type_param, l));
1926                 }
1927
1928                 public static string RemoveGenericArity (string name)
1929                 {
1930                         int start = 0;
1931                         StringBuilder sb = null;
1932                         do {
1933                                 int pos = name.IndexOf ('`', start);
1934                                 if (pos < 0) {
1935                                         if (start == 0)
1936                                                 return name;
1937
1938                                         sb.Append (name.Substring (start));
1939                                         break;
1940                                 }
1941
1942                                 if (sb == null)
1943                                         sb = new StringBuilder ();
1944                                 sb.Append (name.Substring (start, pos-start));
1945
1946                                 pos++;
1947                                 while ((pos < name.Length) && Char.IsNumber (name [pos]))
1948                                         pos++;
1949
1950                                 start = pos;
1951                         } while (start < name.Length);
1952
1953                         return sb.ToString ();
1954                 }
1955
1956                 public SimpleName GetMethodGroup ()
1957                 {
1958                         return new SimpleName (RemoveGenericArity (Name), Arguments, loc);
1959                 }
1960
1961                 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
1962                 {
1963                         if (ec.IsFieldInitializer)
1964                                 Report.Error (236, l,
1965                                         "A field initializer cannot reference the nonstatic field, method, or property `{0}'",
1966                                         name);
1967                         else
1968                                 Report.Error (
1969                                         120, l, "`{0}': An object reference is required for the nonstatic field, method or property",
1970                                         name);
1971                 }
1972
1973                 public bool IdenticalNameAndTypeName (EmitContext ec, Expression resolved_to, Location loc)
1974                 {
1975                         return resolved_to != null && resolved_to.Type != null && 
1976                                 resolved_to.Type.Name == Name &&
1977                                 (ec.DeclContainer.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
1978                 }
1979
1980                 public override Expression DoResolve (EmitContext ec)
1981                 {
1982                         return SimpleNameResolve (ec, null, false);
1983                 }
1984
1985                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1986                 {
1987                         return SimpleNameResolve (ec, right_side, false);
1988                 }
1989                 
1990
1991                 public Expression DoResolve (EmitContext ec, bool intermediate)
1992                 {
1993                         return SimpleNameResolve (ec, null, intermediate);
1994                 }
1995
1996                 private bool IsNestedChild (Type t, Type parent)
1997                 {
1998                         if (parent == null)
1999                                 return false;
2000
2001                         while (parent != null) {
2002                                 parent = TypeManager.DropGenericTypeArguments (parent);
2003                                 if (TypeManager.IsNestedChildOf (t, parent))
2004                                         return true;
2005
2006                                 parent = parent.BaseType;
2007                         }
2008
2009                         return false;
2010                 }
2011
2012                 FullNamedExpression ResolveNested (IResolveContext ec, Type t)
2013                 {
2014                         if (!TypeManager.IsGenericTypeDefinition (t))
2015                                 return null;
2016
2017                         DeclSpace ds = ec.DeclContainer;
2018                         while (ds != null) {
2019                                 if (IsNestedChild (t, ds.TypeBuilder))
2020                                         break;
2021
2022                                 ds = ds.Parent;
2023                         }
2024
2025                         if (ds == null)
2026                                 return null;
2027
2028                         Type[] gen_params = TypeManager.GetTypeArguments (t);
2029
2030                         int arg_count = Arguments != null ? Arguments.Count : 0;
2031
2032                         for (; (ds != null) && ds.IsGeneric; ds = ds.Parent) {
2033                                 if (arg_count + ds.CountTypeParameters == gen_params.Length) {
2034                                         TypeArguments new_args = new TypeArguments (loc);
2035                                         foreach (TypeParameter param in ds.TypeParameters)
2036                                                 new_args.Add (new TypeParameterExpr (param, loc));
2037
2038                                         if (Arguments != null)
2039                                                 new_args.Add (Arguments);
2040
2041                                         return new ConstructedType (t, new_args, loc);
2042                                 }
2043                         }
2044
2045                         return null;
2046                 }
2047
2048                 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
2049                 {
2050                         FullNamedExpression fne = ec.GenericDeclContainer.LookupGeneric (Name, loc);
2051                         if (fne != null)
2052                                 return fne.ResolveAsTypeStep (ec, silent);
2053
2054                         int errors = Report.Errors;
2055                         fne = ec.DeclContainer.LookupType (Name, loc, /*ignore_cs0104=*/ false);
2056
2057                         if (fne != null) {
2058                                 if (fne.Type == null)
2059                                         return fne;
2060
2061                                 FullNamedExpression nested = ResolveNested (ec, fne.Type);
2062                                 if (nested != null)
2063                                         return nested.ResolveAsTypeStep (ec, false);
2064
2065                                 if (Arguments != null) {
2066                                         ConstructedType ct = new ConstructedType (fne, Arguments, loc);
2067                                         return ct.ResolveAsTypeStep (ec, false);
2068                                 }
2069
2070                                 return fne;
2071                         }
2072
2073                         if (silent || errors != Report.Errors)
2074                                 return null;
2075
2076                         MemberCore mc = ec.DeclContainer.GetDefinition (Name);
2077                         if (mc != null) {
2078                                 Error_UnexpectedKind (ec.DeclContainer, "type", GetMemberType (mc), loc);
2079                                 return null;
2080                         }
2081
2082                         string ns = ec.DeclContainer.NamespaceEntry.NS.Name;
2083                         string fullname = (ns.Length > 0) ? ns + "." + Name : Name;
2084                         foreach (Assembly a in RootNamespace.Global.Assemblies) {
2085                                 Type type = a.GetType (fullname);
2086                                 if (type != null) {
2087                                         Report.SymbolRelatedToPreviousError (type);
2088                                         Expression.ErrorIsInaccesible (loc, fullname);
2089                                         return null;
2090                                 }
2091                         }
2092
2093                         Type t = ec.DeclContainer.NamespaceEntry.NS.LookForAnyGenericType (Name);
2094                         if (t != null) {
2095                                 Namespace.Error_InvalidNumberOfTypeArguments (t, loc);
2096                                 return null;
2097                         }
2098
2099                         NamespaceEntry.Error_NamespaceNotFound (loc, Name);
2100                         return null;
2101                 }
2102
2103                 // TODO: I am still not convinced about this. If someone else will need it
2104                 // implement this as virtual property in MemberCore hierarchy
2105                 public static string GetMemberType (MemberCore mc)
2106                 {
2107                         if (mc is Property)
2108                                 return "property";
2109                         if (mc is Indexer)
2110                                 return "indexer";
2111                         if (mc is FieldBase)
2112                                 return "field";
2113                         if (mc is MethodCore)
2114                                 return "method";
2115                         if (mc is EnumMember)
2116                                 return "enum";
2117                         if (mc is Event)
2118                                 return "event";
2119
2120                         return "type";
2121                 }
2122
2123                 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool intermediate)
2124                 {
2125                         if (in_transit)
2126                                 return null;
2127                         in_transit = true;
2128
2129                         Expression e = DoSimpleNameResolve (ec, right_side, intermediate);
2130                         if (e == null)
2131                                 return null;
2132
2133                         if (ec.CurrentBlock == null || ec.CurrentBlock.CheckInvariantMeaningInBlock (Name, e, Location))
2134                                 return e;
2135
2136                         return null;
2137                 }
2138
2139                 /// <remarks>
2140                 ///   7.5.2: Simple Names. 
2141                 ///
2142                 ///   Local Variables and Parameters are handled at
2143                 ///   parse time, so they never occur as SimpleNames.
2144                 ///
2145                 ///   The `intermediate' flag is used by MemberAccess only
2146                 ///   and it is used to inform us that it is ok for us to 
2147                 ///   avoid the static check, because MemberAccess might end
2148                 ///   up resolving the Name as a Type name and the access as
2149                 ///   a static type access.
2150                 ///
2151                 ///   ie: Type Type; .... { Type.GetType (""); }
2152                 ///
2153                 ///   Type is both an instance variable and a Type;  Type.GetType
2154                 ///   is the static method not an instance method of type.
2155                 /// </remarks>
2156                 Expression DoSimpleNameResolve (EmitContext ec, Expression right_side, bool intermediate)
2157                 {
2158                         Expression e = null;
2159
2160                         //
2161                         // Stage 1: Performed by the parser (binding to locals or parameters).
2162                         //
2163                         Block current_block = ec.CurrentBlock;
2164                         if (current_block != null){
2165                                 LocalInfo vi = current_block.GetLocalInfo (Name);
2166                                 if (vi != null){
2167                                         if (Arguments != null) {
2168                                                 Report.Error (307, loc,
2169                                                               "The variable `{0}' cannot be used with type arguments",
2170                                                               Name);
2171                                                 return null;
2172                                         }
2173
2174                                         LocalVariableReference var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2175                                         if (right_side != null) {
2176                                                 return var.ResolveLValue (ec, right_side, loc);
2177                                         } else {
2178                                                 ResolveFlags rf = ResolveFlags.VariableOrValue;
2179                                                 if (intermediate)
2180                                                         rf |= ResolveFlags.DisableFlowAnalysis;
2181                                                 return var.Resolve (ec, rf);
2182                                         }
2183                                 }
2184
2185                                 ParameterReference pref = current_block.Toplevel.GetParameterReference (Name, loc);
2186                                 if (pref != null) {
2187                                         if (Arguments != null) {
2188                                                 Report.Error (307, loc,
2189                                                               "The variable `{0}' cannot be used with type arguments",
2190                                                               Name);
2191                                                 return null;
2192                                         }
2193
2194                                         if (right_side != null)
2195                                                 return pref.ResolveLValue (ec, right_side, loc);
2196                                         else
2197                                                 return pref.Resolve (ec);
2198                                 }
2199                         }
2200                         
2201                         //
2202                         // Stage 2: Lookup members 
2203                         //
2204
2205                         DeclSpace lookup_ds = ec.DeclContainer;
2206                         Type almost_matched_type = null;
2207                         ArrayList almost_matched = null;
2208                         do {
2209                                 if (lookup_ds.TypeBuilder == null)
2210                                         break;
2211
2212                                 e = MemberLookup (ec.ContainerType, lookup_ds.TypeBuilder, Name, loc);
2213                                 if (e != null)
2214                                         break;
2215
2216                                 if (almost_matched == null && almostMatchedMembers.Count > 0) {
2217                                         almost_matched_type = lookup_ds.TypeBuilder;
2218                                         almost_matched = (ArrayList) almostMatchedMembers.Clone ();
2219                                 }
2220
2221                                 lookup_ds =lookup_ds.Parent;
2222                         } while (lookup_ds != null);
2223
2224                         if (e == null && ec.ContainerType != null)
2225                                 e = MemberLookup (ec.ContainerType, ec.ContainerType, Name, loc);
2226
2227                         if (e == null) {
2228                                 if (almost_matched == null && almostMatchedMembers.Count > 0) {
2229                                         almost_matched_type = ec.ContainerType;
2230                                         almost_matched = (ArrayList) almostMatchedMembers.Clone ();
2231                                 }
2232                                 e = ResolveAsTypeStep (ec, true);
2233                         }
2234
2235                         if (e == null) {
2236                                 if (almost_matched != null)
2237                                         almostMatchedMembers = almost_matched;
2238                                 if (almost_matched_type == null)
2239                                         almost_matched_type = ec.ContainerType;
2240                                 MemberLookupFailed (ec.ContainerType, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclContainer.Name, true, loc);
2241                                 return null;
2242                         }
2243
2244                         if (e is TypeExpr) {
2245                                 if (Arguments == null)
2246                                         return e;
2247
2248                                 ConstructedType ct = new ConstructedType (
2249                                         (FullNamedExpression) e, Arguments, loc);
2250                                 return ct.ResolveAsTypeStep (ec, false);
2251                         }
2252
2253                         if (e is MemberExpr) {
2254                                 MemberExpr me = (MemberExpr) e;
2255
2256                                 Expression left;
2257                                 if (me.IsInstance) {
2258                                         if (ec.IsStatic || ec.IsFieldInitializer) {
2259                                                 //
2260                                                 // Note that an MemberExpr can be both IsInstance and IsStatic.
2261                                                 // An unresolved MethodGroupExpr can contain both kinds of methods
2262                                                 // and each predicate is true if the MethodGroupExpr contains
2263                                                 // at least one of that kind of method.
2264                                                 //
2265
2266                                                 if (!me.IsStatic &&
2267                                                     (!intermediate || !IdenticalNameAndTypeName (ec, me, loc))) {
2268                                                         Error_ObjectRefRequired (ec, loc, me.GetSignatureForError ());
2269                                                         return EmptyExpression.Null;
2270                                                 }
2271
2272                                                 //
2273                                                 // Pass the buck to MemberAccess and Invocation.
2274                                                 //
2275                                                 left = EmptyExpression.Null;
2276                                         } else {
2277                                                 left = ec.GetThis (loc);
2278                                         }
2279                                 } else {
2280                                         left = new TypeExpression (ec.ContainerType, loc);
2281                                 }
2282
2283                                 e = me.ResolveMemberAccess (ec, left, loc, null);
2284                                 if (e == null)
2285                                         return null;
2286
2287                                 me = e as MemberExpr;
2288                                 if (me == null)
2289                                         return e;
2290
2291                                 if (Arguments != null) {
2292                                         MethodGroupExpr mg = me as MethodGroupExpr;
2293                                         if (mg == null)
2294                                                 return null;
2295
2296                                         return mg.ResolveGeneric (ec, Arguments);
2297                                 }
2298
2299                                 if (!me.IsStatic && (me.InstanceExpression != null) &&
2300                                     TypeManager.IsNestedFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2301                                     me.InstanceExpression.Type != me.DeclaringType &&
2302                                     !TypeManager.IsFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2303                                     (!intermediate || !IdenticalNameAndTypeName (ec, e, loc))) {
2304                                         Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
2305                                                 TypeManager.CSharpName (me.DeclaringType), TypeManager.CSharpName (me.InstanceExpression.Type));
2306                                         return null;
2307                                 }
2308
2309                                 return (right_side != null)
2310                                         ? me.DoResolveLValue (ec, right_side)
2311                                         : me.DoResolve (ec);
2312                         }
2313
2314                         return e;
2315                 }
2316                 
2317                 public override void Emit (EmitContext ec)
2318                 {
2319                         //
2320                         // If this is ever reached, then we failed to
2321                         // find the name as a namespace
2322                         //
2323
2324                         Error (103, "The name `" + Name +
2325                                "' does not exist in the class `" +
2326                                ec.DeclContainer.Name + "'");
2327                 }
2328
2329                 public override string ToString ()
2330                 {
2331                         return Name;
2332                 }
2333
2334                 public override string GetSignatureForError ()
2335                 {
2336                         return Name;
2337                 }
2338         }
2339
2340         /// <summary>
2341         ///   Represents a namespace or a type.  The name of the class was inspired by
2342         ///   section 10.8.1 (Fully Qualified Names).
2343         /// </summary>
2344         public abstract class FullNamedExpression : Expression {
2345                 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
2346                 {
2347                         return this;
2348                 }
2349
2350                 public abstract string FullName {
2351                         get;
2352                 }
2353         }
2354         
2355         /// <summary>
2356         ///   Expression that evaluates to a type
2357         /// </summary>
2358         public abstract class TypeExpr : FullNamedExpression {
2359                 override public FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
2360                 {
2361                         TypeExpr t = DoResolveAsTypeStep (ec);
2362                         if (t == null)
2363                                 return null;
2364
2365                         eclass = ExprClass.Type;
2366                         return t;
2367                 }
2368
2369                 override public Expression DoResolve (EmitContext ec)
2370                 {
2371                         return ResolveAsTypeTerminal (ec, false);
2372                 }
2373
2374                 override public void Emit (EmitContext ec)
2375                 {
2376                         throw new Exception ("Should never be called");
2377                 }
2378
2379                 public virtual bool CheckAccessLevel (DeclSpace ds)
2380                 {
2381                         return ds.CheckAccessLevel (Type);
2382                 }
2383
2384                 public virtual bool AsAccessible (DeclSpace ds, int flags)
2385                 {
2386                         return ds.AsAccessible (Type, flags);
2387                 }
2388
2389                 public virtual bool IsClass {
2390                         get { return Type.IsClass; }
2391                 }
2392
2393                 public virtual bool IsValueType {
2394                         get { return Type.IsValueType; }
2395                 }
2396
2397                 public virtual bool IsInterface {
2398                         get { return Type.IsInterface; }
2399                 }
2400
2401                 public virtual bool IsSealed {
2402                         get { return Type.IsSealed; }
2403                 }
2404
2405                 public virtual bool CanInheritFrom ()
2406                 {
2407                         if (Type == TypeManager.enum_type ||
2408                             (Type == TypeManager.value_type && RootContext.StdLib) ||
2409                             Type == TypeManager.multicast_delegate_type ||
2410                             Type == TypeManager.delegate_type ||
2411                             Type == TypeManager.array_type)
2412                                 return false;
2413
2414                         return true;
2415                 }
2416
2417                 protected abstract TypeExpr DoResolveAsTypeStep (IResolveContext ec);
2418
2419                 public abstract string Name {
2420                         get;
2421                 }
2422
2423                 public override bool Equals (object obj)
2424                 {
2425                         TypeExpr tobj = obj as TypeExpr;
2426                         if (tobj == null)
2427                                 return false;
2428
2429                         return Type == tobj.Type;
2430                 }
2431
2432                 public override int GetHashCode ()
2433                 {
2434                         return Type.GetHashCode ();
2435                 }
2436                 
2437                 public override string ToString ()
2438                 {
2439                         return Name;
2440                 }
2441         }
2442
2443         /// <summary>
2444         ///   Fully resolved Expression that already evaluated to a type
2445         /// </summary>
2446         public class TypeExpression : TypeExpr {
2447                 public TypeExpression (Type t, Location l)
2448                 {
2449                         Type = t;
2450                         eclass = ExprClass.Type;
2451                         loc = l;
2452                 }
2453
2454                 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
2455                 {
2456                         return this;
2457                 }
2458
2459                 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
2460                 {
2461                         return this;
2462                 }
2463
2464                 public override string Name {
2465                         get { return Type.ToString (); }
2466                 }
2467
2468                 public override string FullName {
2469                         get { return Type.FullName; }
2470                 }
2471         }
2472
2473         /// <summary>
2474         ///   Used to create types from a fully qualified name.  These are just used
2475         ///   by the parser to setup the core types.  A TypeLookupExpression is always
2476         ///   classified as a type.
2477         /// </summary>
2478         public sealed class TypeLookupExpression : TypeExpr {
2479                 readonly string name;
2480                 
2481                 public TypeLookupExpression (string name)
2482                 {
2483                         this.name = name;
2484                         eclass = ExprClass.Type;
2485                 }
2486
2487                 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
2488                 {
2489                         // It's null for corlib compilation only
2490                         if (type == null)
2491                                 return DoResolveAsTypeStep (ec);
2492
2493                         return this;
2494                 }
2495
2496                 static readonly char [] dot_array = { '.' };
2497                 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
2498                 {
2499                         // If name is of the form `N.I', first lookup `N', then search a member `I' in it.
2500                         string rest = null;
2501                         string lookup_name = name;
2502                         int pos = name.IndexOf ('.');
2503                         if (pos >= 0) {
2504                                 rest = name.Substring (pos + 1);
2505                                 lookup_name = name.Substring (0, pos);
2506                         }
2507
2508                         FullNamedExpression resolved = RootNamespace.Global.Lookup (ec.DeclContainer, lookup_name, Location.Null);
2509
2510                         if (resolved != null && rest != null) {
2511                                 // Now handle the rest of the the name.
2512                                 string [] elements = rest.Split (dot_array);
2513                                 string element;
2514                                 int count = elements.Length;
2515                                 int i = 0;
2516                                 while (i < count && resolved != null && resolved is Namespace) {
2517                                         Namespace ns = resolved as Namespace;
2518                                         element = elements [i++];
2519                                         lookup_name += "." + element;
2520                                         resolved = ns.Lookup (ec.DeclContainer, element, Location.Null);
2521                                 }
2522
2523                                 if (resolved != null && resolved is TypeExpr) {
2524                                         Type t = ((TypeExpr) resolved).Type;
2525                                         while (t != null) {
2526                                                 if (!ec.DeclContainer.CheckAccessLevel (t)) {
2527                                                         resolved = null;
2528                                                         lookup_name = t.FullName;
2529                                                         break;
2530                                                 }
2531                                                 if (i == count) {
2532                                                         type = t;
2533                                                         return this;
2534                                                 }
2535                                                 t = TypeManager.GetNestedType (t, elements [i++]);
2536                                         }
2537                                 }
2538                         }
2539
2540                         if (resolved == null) {
2541                                 NamespaceEntry.Error_NamespaceNotFound (loc, lookup_name);
2542                                 return null;
2543                         }
2544
2545                         if (!(resolved is TypeExpr)) {
2546                                 resolved.Error_UnexpectedKind (ec.DeclContainer, "type", loc);
2547                                 return null;
2548                         }
2549
2550                         type = resolved.Type;
2551                         return this;
2552                 }
2553
2554                 public override string Name {
2555                         get { return name; }
2556                 }
2557
2558                 public override string FullName {
2559                         get { return name; }
2560                 }
2561         }
2562
2563         /// <summary>
2564         ///   Represents an "unbound generic type", ie. typeof (Foo<>).
2565         ///   See 14.5.11.
2566         /// </summary>
2567         public class UnboundTypeExpression : TypeExpr
2568         {
2569                 MemberName name;
2570
2571                 public UnboundTypeExpression (MemberName name, Location l)
2572                 {
2573                         this.name = name;
2574                         loc = l;
2575                 }
2576
2577                 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
2578                 {
2579                         Expression expr;
2580                         if (name.Left != null) {
2581                                 Expression lexpr = name.Left.GetTypeExpression ();
2582                                 expr = new MemberAccess (lexpr, name.Basename);
2583                         } else {
2584                                 expr = new SimpleName (name.Basename, loc);
2585                         }
2586
2587                         FullNamedExpression fne = expr.ResolveAsTypeStep (ec, false);
2588                         if (fne == null)
2589                                 return null;
2590
2591                         type = fne.Type;
2592                         return new TypeExpression (type, loc);
2593                 }
2594
2595                 public override string Name {
2596                         get { return name.FullName; }
2597                 }
2598
2599                 public override string FullName {
2600                         get { return name.FullName; }
2601                 }
2602         }
2603
2604         public class TypeAliasExpression : TypeExpr {
2605                 FullNamedExpression alias;
2606                 TypeExpr texpr;
2607                 TypeArguments args;
2608                 string name;
2609
2610                 public TypeAliasExpression (FullNamedExpression alias, TypeArguments args, Location l)
2611                 {
2612                         this.alias = alias;
2613                         this.args = args;
2614                         loc = l;
2615
2616                         eclass = ExprClass.Type;
2617                         if (args != null)
2618                                 name = alias.FullName + "<" + args.ToString () + ">";
2619                         else
2620                                 name = alias.FullName;
2621                 }
2622
2623                 public override string Name {
2624                         get { return alias.FullName; }
2625                 }
2626
2627                 public override string FullName {
2628                         get { return name; }
2629                 }
2630
2631                 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
2632                 {
2633                         texpr = alias.ResolveAsTypeTerminal (ec, false);
2634                         if (texpr == null)
2635                                 return null;
2636
2637                         Type type = texpr.Type;
2638                         int num_args = TypeManager.GetNumberOfTypeArguments (type);
2639
2640                         if (args != null) {
2641                                 if (num_args == 0) {
2642                                         Report.Error (308, loc,
2643                                                       "The non-generic type `{0}' cannot " +
2644                                                       "be used with type arguments.",
2645                                                       TypeManager.CSharpName (type));
2646                                         return null;
2647                                 }
2648
2649                                 ConstructedType ctype = new ConstructedType (type, args, loc);
2650                                 return ctype.ResolveAsTypeTerminal (ec, false);
2651                         } else if (num_args > 0) {
2652                                 Report.Error (305, loc,
2653                                               "Using the generic type `{0}' " +
2654                                               "requires {1} type arguments",
2655                                               TypeManager.CSharpName (type), num_args.ToString ());
2656                                 return null;
2657                         }
2658
2659                         return texpr;
2660                 }
2661
2662                 public override bool CheckAccessLevel (DeclSpace ds)
2663                 {
2664                         return texpr.CheckAccessLevel (ds);
2665                 }
2666
2667                 public override bool AsAccessible (DeclSpace ds, int flags)
2668                 {
2669                         return texpr.AsAccessible (ds, flags);
2670                 }
2671
2672                 public override bool IsClass {
2673                         get { return texpr.IsClass; }
2674                 }
2675
2676                 public override bool IsValueType {
2677                         get { return texpr.IsValueType; }
2678                 }
2679
2680                 public override bool IsInterface {
2681                         get { return texpr.IsInterface; }
2682                 }
2683
2684                 public override bool IsSealed {
2685                         get { return texpr.IsSealed; }
2686                 }
2687         }
2688
2689         /// <summary>
2690         ///   This class denotes an expression which evaluates to a member
2691         ///   of a struct or a class.
2692         /// </summary>
2693         public abstract class MemberExpr : Expression
2694         {
2695                 /// <summary>
2696                 ///   The name of this member.
2697                 /// </summary>
2698                 public abstract string Name {
2699                         get;
2700                 }
2701
2702                 /// <summary>
2703                 ///   Whether this is an instance member.
2704                 /// </summary>
2705                 public abstract bool IsInstance {
2706                         get;
2707                 }
2708
2709                 /// <summary>
2710                 ///   Whether this is a static member.
2711                 /// </summary>
2712                 public abstract bool IsStatic {
2713                         get;
2714                 }
2715
2716                 /// <summary>
2717                 ///   The type which declares this member.
2718                 /// </summary>
2719                 public abstract Type DeclaringType {
2720                         get;
2721                 }
2722
2723                 /// <summary>
2724                 ///   The instance expression associated with this member, if it's a
2725                 ///   non-static member.
2726                 /// </summary>
2727                 public Expression InstanceExpression;
2728
2729                 public static void error176 (Location loc, string name)
2730                 {
2731                         Report.Error (176, loc, "Static member `{0}' cannot be accessed " +
2732                                       "with an instance reference, qualify it with a type name instead", name);
2733                 }
2734
2735                 // TODO: possible optimalization
2736                 // Cache resolved constant result in FieldBuilder <-> expression map
2737                 public virtual Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
2738                                                                SimpleName original)
2739                 {
2740                         //
2741                         // Precondition:
2742                         //   original == null || original.Resolve (...) ==> left
2743                         //
2744
2745                         if (left is TypeExpr) {
2746                                 if (!IsStatic) {
2747                                         SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
2748                                         return null;
2749                                 }
2750
2751                                 return this;
2752                         }
2753                                 
2754                         if (!IsInstance) {
2755                                 if (original != null && original.IdenticalNameAndTypeName (ec, left, loc))
2756                                         return this;
2757
2758                                 error176 (loc, GetSignatureForError ());
2759                                 return null;
2760                         }
2761
2762                         InstanceExpression = left;
2763
2764                         return this;
2765                 }
2766
2767                 protected void EmitInstance (EmitContext ec, bool prepare_for_load)
2768                 {
2769                         if (IsStatic)
2770                                 return;
2771
2772                         if (InstanceExpression == EmptyExpression.Null) {
2773                                 SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
2774                                 return;
2775                         }
2776                                 
2777                         if (InstanceExpression.Type.IsValueType) {
2778                                 if (InstanceExpression is IMemoryLocation) {
2779                                         ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
2780                                 } else {
2781                                         LocalTemporary t = new LocalTemporary (InstanceExpression.Type);
2782                                         InstanceExpression.Emit (ec);
2783                                         t.Store (ec);
2784                                         t.AddressOf (ec, AddressOp.Store);
2785                                 }
2786                         } else
2787                                 InstanceExpression.Emit (ec);
2788
2789                         if (prepare_for_load)
2790                                 ec.ig.Emit (OpCodes.Dup);
2791                 }
2792         }
2793
2794         /// <summary>
2795         ///   MethodGroup Expression.
2796         ///  
2797         ///   This is a fully resolved expression that evaluates to a type
2798         /// </summary>
2799         public class MethodGroupExpr : MemberExpr {
2800                 public MethodBase [] Methods;
2801                 bool has_type_arguments = false;
2802                 bool identical_type_name = false;
2803                 bool is_base;
2804                 
2805                 public MethodGroupExpr (MemberInfo [] mi, Location l)
2806                 {
2807                         Methods = new MethodBase [mi.Length];
2808                         mi.CopyTo (Methods, 0);
2809                         eclass = ExprClass.MethodGroup;
2810
2811                         // Set the type to something that will never be useful, which will
2812                         // trigger the proper conversions.
2813                         type = typeof (MethodGroupExpr);
2814                         loc = l;
2815                 }
2816
2817                 public MethodGroupExpr (ArrayList list, Location l)
2818                 {
2819                         Methods = new MethodBase [list.Count];
2820
2821                         try {
2822                                 list.CopyTo (Methods, 0);
2823                         } catch {
2824                                 foreach (MemberInfo m in list){
2825                                         if (!(m is MethodBase)){
2826                                                 Console.WriteLine ("Name " + m.Name);
2827                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2828                                         }
2829                                 }
2830                                 throw;
2831                         }
2832
2833                         loc = l;
2834                         eclass = ExprClass.MethodGroup;
2835                         type = TypeManager.object_type;
2836                 }
2837
2838                 public override Type DeclaringType {
2839                         get {
2840                                 //
2841                                 // We assume that the top-level type is in the end
2842                                 //
2843                                 return Methods [Methods.Length - 1].DeclaringType;
2844                                 //return Methods [0].DeclaringType;
2845                         }
2846                 }
2847
2848                 public bool HasTypeArguments {
2849                         get {
2850                                 return has_type_arguments;
2851                         }
2852
2853                         set {
2854                                 has_type_arguments = value;
2855                         }
2856                 }
2857
2858                 public bool IdenticalTypeName {
2859                         get {
2860                                 return identical_type_name;
2861                         }
2862
2863                         set {
2864                                 identical_type_name = value;
2865                         }
2866                 }
2867
2868                 public bool IsBase {
2869                         get {
2870                                 return is_base;
2871                         }
2872                         set {
2873                                 is_base = value;
2874                         }
2875                 }
2876
2877                 public override string GetSignatureForError ()
2878                 {
2879                         return TypeManager.CSharpSignature (Methods [0]);
2880                 }
2881
2882                 public override string Name {
2883                         get {
2884                                 return Methods [0].Name;
2885                         }
2886                 }
2887
2888                 public override bool IsInstance {
2889                         get {
2890                                 foreach (MethodBase mb in Methods)
2891                                         if (!mb.IsStatic)
2892                                                 return true;
2893
2894                                 return false;
2895                         }
2896                 }
2897
2898                 public override bool IsStatic {
2899                         get {
2900                                 foreach (MethodBase mb in Methods)
2901                                         if (mb.IsStatic)
2902                                                 return true;
2903
2904                                 return false;
2905                         }
2906                 }
2907
2908                 public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
2909                                                                 SimpleName original)
2910                 {
2911                         if (!(left is TypeExpr) &&
2912                             original != null && original.IdenticalNameAndTypeName (ec, left, loc))
2913                                 IdenticalTypeName = true;
2914
2915                         return base.ResolveMemberAccess (ec, left, loc, original);
2916                 }
2917                 
2918                 override public Expression DoResolve (EmitContext ec)
2919                 {
2920                         if (!IsInstance)
2921                                 InstanceExpression = null;
2922
2923                         if (InstanceExpression != null) {
2924                                 InstanceExpression = InstanceExpression.DoResolve (ec);
2925                                 if (InstanceExpression == null)
2926                                         return null;
2927                         }
2928
2929                         return this;
2930                 }
2931
2932                 public void ReportUsageError ()
2933                 {
2934                         Report.Error (654, loc, "Method `" + DeclaringType + "." +
2935                                       Name + "()' is referenced without parentheses");
2936                 }
2937
2938                 override public void Emit (EmitContext ec)
2939                 {
2940                         ReportUsageError ();
2941                 }
2942
2943                 bool RemoveMethods (bool keep_static)
2944                 {
2945                         ArrayList smethods = new ArrayList ();
2946
2947                         foreach (MethodBase mb in Methods){
2948                                 if (mb.IsStatic == keep_static)
2949                                         smethods.Add (mb);
2950                         }
2951
2952                         if (smethods.Count == 0)
2953                                 return false;
2954
2955                         Methods = new MethodBase [smethods.Count];
2956                         smethods.CopyTo (Methods, 0);
2957
2958                         return true;
2959                 }
2960                 
2961                 /// <summary>
2962                 ///   Removes any instance methods from the MethodGroup, returns
2963                 ///   false if the resulting set is empty.
2964                 /// </summary>
2965                 public bool RemoveInstanceMethods ()
2966                 {
2967                         return RemoveMethods (true);
2968                 }
2969
2970                 /// <summary>
2971                 ///   Removes any static methods from the MethodGroup, returns
2972                 ///   false if the resulting set is empty.
2973                 /// </summary>
2974                 public bool RemoveStaticMethods ()
2975                 {
2976                         return RemoveMethods (false);
2977                 }
2978
2979                 public Expression ResolveGeneric (EmitContext ec, TypeArguments args)
2980                 {
2981 #if GMCS_SOURCE
2982                         if (args.Resolve (ec) == false)
2983                                 return null;
2984
2985                         Type[] atypes = args.Arguments;
2986
2987                         int first_count = 0;
2988                         MethodInfo first = null;
2989
2990                         ArrayList list = new ArrayList ();
2991                         foreach (MethodBase mb in Methods) {
2992                                 MethodInfo mi = mb as MethodInfo;
2993                                 if ((mi == null) || !mi.IsGenericMethod)
2994                                         continue;
2995
2996                                 Type[] gen_params = mi.GetGenericArguments ();
2997
2998                                 if (first == null) {
2999                                         first = mi;
3000                                         first_count = gen_params.Length;
3001                                 }
3002
3003                                 if (gen_params.Length != atypes.Length)
3004                                         continue;
3005
3006                                 list.Add (mi.MakeGenericMethod (atypes));
3007                         }
3008
3009                         if (list.Count > 0) {
3010                                 MethodGroupExpr new_mg = new MethodGroupExpr (list, Location);
3011                                 new_mg.InstanceExpression = InstanceExpression;
3012                                 new_mg.HasTypeArguments = true;
3013                                 new_mg.IsBase = IsBase;
3014                                 return new_mg;
3015                         }
3016
3017                         if (first != null)
3018                                 Report.Error (
3019                                         305, loc, "Using the generic method `{0}' " +
3020                                         "requires {1} type arguments", Name,
3021                                         first_count.ToString ());
3022                         else
3023                                 Report.Error (
3024                                         308, loc, "The non-generic method `{0}' " +
3025                                         "cannot be used with type arguments", Name);
3026
3027                         return null;
3028 #else
3029                         throw new NotImplementedException ();
3030 #endif
3031                 }
3032         }
3033
3034         /// <summary>
3035         ///   Fully resolved expression that evaluates to a Field
3036         /// </summary>
3037         public class FieldExpr : MemberExpr, IAssignMethod, IMemoryLocation, IVariable {
3038                 public readonly FieldInfo FieldInfo;
3039                 VariableInfo variable_info;
3040                 
3041                 LocalTemporary temp;
3042                 bool prepared;
3043                 bool in_initializer;
3044
3045                 public FieldExpr (FieldInfo fi, Location l, bool in_initializer):
3046                         this (fi, l)
3047                 {
3048                         this.in_initializer = in_initializer;
3049                 }
3050                 
3051                 public FieldExpr (FieldInfo fi, Location l)
3052                 {
3053                         FieldInfo = fi;
3054                         eclass = ExprClass.Variable;
3055                         type = TypeManager.TypeToCoreType (fi.FieldType);
3056                         loc = l;
3057                 }
3058
3059                 public override string Name {
3060                         get {
3061                                 return FieldInfo.Name;
3062                         }
3063                 }
3064
3065                 public override bool IsInstance {
3066                         get {
3067                                 return !FieldInfo.IsStatic;
3068                         }
3069                 }
3070
3071                 public override bool IsStatic {
3072                         get {
3073                                 return FieldInfo.IsStatic;
3074                         }
3075                 }
3076
3077                 public override Type DeclaringType {
3078                         get {
3079                                 return FieldInfo.DeclaringType;
3080                         }
3081                 }
3082
3083                 public override string GetSignatureForError ()
3084                 {
3085                         return TypeManager.GetFullNameSignature (FieldInfo);
3086                 }
3087
3088                 public VariableInfo VariableInfo {
3089                         get {
3090                                 return variable_info;
3091                         }
3092                 }
3093
3094                 public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
3095                                                                 SimpleName original)
3096                 {
3097                         FieldInfo fi = TypeManager.GetGenericFieldDefinition (FieldInfo);
3098
3099                         Type t = fi.FieldType;
3100
3101                         if (fi.IsLiteral || (fi.IsInitOnly && t == TypeManager.decimal_type)) {
3102                                 IConstant ic = TypeManager.GetConstant (fi);
3103                                 if (ic == null) {
3104                                         if (fi.IsLiteral) {
3105                                                 ic = new ExternalConstant (fi);
3106                                         } else {
3107                                                 ic = ExternalConstant.CreateDecimal (fi);
3108                                                 if (ic == null) {
3109                                                         return base.ResolveMemberAccess (ec, left, loc, original);
3110                                                 }
3111                                         }
3112                                         TypeManager.RegisterConstant (fi, ic);
3113                                 }
3114
3115                                 bool left_is_type = left is TypeExpr;
3116                                 if (!left_is_type && (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) {
3117                                         Report.SymbolRelatedToPreviousError (FieldInfo);
3118                                         error176 (loc, TypeManager.GetFullNameSignature (FieldInfo));
3119                                         return null;
3120                                 }
3121
3122                                 if (ic.ResolveValue ()) {
3123                                         if (!ec.IsInObsoleteScope)
3124                                                 ic.CheckObsoleteness (loc);
3125                                 }
3126
3127                                 return ic.CreateConstantReference (loc);
3128                         }
3129                         
3130                         if (t.IsPointer && !ec.InUnsafe) {
3131                                 UnsafeError (loc);
3132                                 return null;
3133                         }
3134
3135                         return base.ResolveMemberAccess (ec, left, loc, original);
3136                 }
3137
3138                 override public Expression DoResolve (EmitContext ec)
3139                 {
3140                         return DoResolve (ec, false, false);
3141                 }
3142
3143                 Expression DoResolve (EmitContext ec, bool lvalue_instance, bool out_access)
3144                 {
3145                         if (!FieldInfo.IsStatic){
3146                                 if (InstanceExpression == null){
3147                                         //
3148                                         // This can happen when referencing an instance field using
3149                                         // a fully qualified type expression: TypeName.InstanceField = xxx
3150                                         // 
3151                                         SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
3152                                         return null;
3153                                 }
3154
3155                                 // Resolve the field's instance expression while flow analysis is turned
3156                                 // off: when accessing a field "a.b", we must check whether the field
3157                                 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3158
3159                                 if (lvalue_instance) {
3160                                         using (ec.With (EmitContext.Flags.DoFlowAnalysis, false)) {
3161                                                 Expression right_side =
3162                                                         out_access ? EmptyExpression.LValueMemberOutAccess : EmptyExpression.LValueMemberAccess;
3163                                                 InstanceExpression = InstanceExpression.ResolveLValue (ec, right_side, loc);
3164                                         }
3165                                 } else {
3166                                         ResolveFlags rf = ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis;
3167                                         InstanceExpression = InstanceExpression.Resolve (ec, rf);
3168                                 }
3169
3170                                 if (InstanceExpression == null)
3171                                         return null;
3172
3173                                 InstanceExpression.CheckMarshalByRefAccess ();
3174                         }
3175
3176                         if (!in_initializer && !ec.IsFieldInitializer) {
3177                                 ObsoleteAttribute oa;
3178                                 FieldBase f = TypeManager.GetField (FieldInfo);
3179                                 if (f != null) {
3180                                         if (!ec.IsInObsoleteScope)
3181                                                 f.CheckObsoleteness (loc);
3182                                 
3183                                         // To be sure that type is external because we do not register generated fields
3184                                 } else if (!(FieldInfo.DeclaringType is TypeBuilder)) {                                
3185                                         oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo);
3186                                         if (oa != null)
3187                                                 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc);
3188                                 }
3189                         }
3190
3191                         AnonymousContainer am = ec.CurrentAnonymousMethod;
3192                         if (am != null){
3193                                 if (!FieldInfo.IsStatic){
3194                                         if (!am.IsIterator && (ec.TypeContainer is Struct)){
3195                                                 Report.Error (1673, loc,
3196                                                 "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",
3197                                                         "this");
3198                                                 return null;
3199                                         }
3200                                 }
3201                         }
3202                         
3203                         // If the instance expression is a local variable or parameter.
3204                         IVariable var = InstanceExpression as IVariable;
3205                         if ((var == null) || (var.VariableInfo == null))
3206                                 return this;
3207
3208                         VariableInfo vi = var.VariableInfo;
3209                         if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
3210                                 return null;
3211
3212                         variable_info = vi.GetSubStruct (FieldInfo.Name);
3213                         return this;
3214                 }
3215
3216                 static readonly int [] codes = {
3217                         191,    // instance, write access
3218                         192,    // instance, out access
3219                         198,    // static, write access
3220                         199,    // static, out access
3221                         1648,   // member of value instance, write access
3222                         1649,   // member of value instance, out access
3223                         1650,   // member of value static, write access
3224                         1651    // member of value static, out access
3225                 };
3226
3227                 static readonly string [] msgs = {
3228                         /*0191*/ "A readonly field `{0}' cannot be assigned to (except in a constructor or a variable initializer)",
3229                         /*0192*/ "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
3230                         /*0198*/ "A static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
3231                         /*0199*/ "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
3232                         /*1648*/ "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)",
3233                         /*1649*/ "Members of readonly field `{0}' cannot be passed ref or out (except in a constructor)",
3234                         /*1650*/ "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
3235                         /*1651*/ "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)"
3236                 };
3237
3238                 // The return value is always null.  Returning a value simplifies calling code.
3239                 Expression Report_AssignToReadonly (Expression right_side)
3240                 {
3241                         int i = 0;
3242                         if (right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess)
3243                                 i += 1;
3244                         if (IsStatic)
3245                                 i += 2;
3246                         if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess)
3247                                 i += 4;
3248                         Report.Error (codes [i], loc, msgs [i], GetSignatureForError ());
3249
3250                         return null;
3251                 }
3252                 
3253                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3254                 {
3255                         IVariable var = InstanceExpression as IVariable;
3256                         if ((var != null) && (var.VariableInfo != null))
3257                                 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
3258
3259                         bool lvalue_instance = !FieldInfo.IsStatic && FieldInfo.DeclaringType.IsValueType;
3260                         bool out_access = right_side == EmptyExpression.OutAccess || right_side == EmptyExpression.LValueMemberOutAccess;
3261
3262                         Expression e = DoResolve (ec, lvalue_instance, out_access);
3263
3264                         if (e == null)
3265                                 return null;
3266
3267                         FieldBase fb = TypeManager.GetField (FieldInfo);
3268                         if (fb != null)
3269                                 fb.SetAssigned ();
3270
3271                         if (FieldInfo.IsInitOnly) {
3272                                 // InitOnly fields can only be assigned in constructors or initializers
3273                                 if (!ec.IsFieldInitializer && !ec.IsConstructor)
3274                                         return Report_AssignToReadonly (right_side);
3275
3276                                 if (ec.IsConstructor) {
3277                                         Type ctype = ec.TypeContainer.CurrentType;
3278                                         if (ctype == null)
3279                                                 ctype = ec.ContainerType;
3280
3281                                         // InitOnly fields cannot be assigned-to in a different constructor from their declaring type
3282                                         if (!TypeManager.IsEqual (ctype, FieldInfo.DeclaringType))
3283                                                 return Report_AssignToReadonly (right_side);
3284                                         // static InitOnly fields cannot be assigned-to in an instance constructor
3285                                         if (IsStatic && !ec.IsStatic)
3286                                                 return Report_AssignToReadonly (right_side);
3287                                         // instance constructors can't modify InitOnly fields of other instances of the same type
3288                                         if (!IsStatic && !(InstanceExpression is This))
3289                                                 return Report_AssignToReadonly (right_side);
3290                                 }
3291                         }
3292
3293                         if (right_side == EmptyExpression.OutAccess &&
3294                             !IsStatic && !(InstanceExpression is This) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) {
3295                                 Report.SymbolRelatedToPreviousError (DeclaringType);
3296                                 Report.Warning (197, 1, loc,
3297                                                 "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",
3298                                                 GetSignatureForError ());
3299                         }
3300
3301                         return this;
3302                 }
3303
3304                 public override void CheckMarshalByRefAccess ()
3305                 {
3306                         if (!IsStatic && Type.IsValueType && !(InstanceExpression is This) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) {
3307                                 Report.SymbolRelatedToPreviousError (DeclaringType);
3308                                 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",
3309                                                 GetSignatureForError ());
3310                         }
3311                 }
3312
3313                 public bool VerifyFixed ()
3314                 {
3315                         IVariable variable = InstanceExpression as IVariable;
3316                         // A variable of the form V.I is fixed when V is a fixed variable of a struct type.
3317                         // We defer the InstanceExpression check after the variable check to avoid a 
3318                         // separate null check on InstanceExpression.
3319                         return variable != null && InstanceExpression.Type.IsValueType && variable.VerifyFixed ();
3320                 }
3321
3322                 public override int GetHashCode ()
3323                 {
3324                         return FieldInfo.GetHashCode ();
3325                 }
3326
3327                 public override bool Equals (object obj)
3328                 {
3329                         FieldExpr fe = obj as FieldExpr;
3330                         if (fe == null)
3331                                 return false;
3332
3333                         if (FieldInfo != fe.FieldInfo)
3334                                 return false;
3335
3336                         if (InstanceExpression == null || fe.InstanceExpression == null)
3337                                 return true;
3338
3339                         return InstanceExpression.Equals (fe.InstanceExpression);
3340                 }
3341                 
3342                 public void Emit (EmitContext ec, bool leave_copy)
3343                 {
3344                         ILGenerator ig = ec.ig;
3345                         bool is_volatile = false;
3346
3347                         FieldBase f = TypeManager.GetField (FieldInfo);
3348                         if (f != null){
3349                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3350                                         is_volatile = true;
3351
3352                                 f.SetMemberIsUsed ();
3353                         }
3354                         
3355                         if (FieldInfo.IsStatic){
3356                                 if (is_volatile)
3357                                         ig.Emit (OpCodes.Volatile);
3358                                 
3359                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3360                         } else {
3361                                 if (!prepared)
3362                                         EmitInstance (ec, false);
3363
3364                                 if (is_volatile)
3365                                         ig.Emit (OpCodes.Volatile);
3366
3367                                 IFixedBuffer ff = AttributeTester.GetFixedBuffer (FieldInfo);
3368                                 if (ff != null)
3369                                 {
3370                                         ig.Emit (OpCodes.Ldflda, FieldInfo);
3371                                         ig.Emit (OpCodes.Ldflda, ff.Element);
3372                                 }
3373                                 else {
3374                                         ig.Emit (OpCodes.Ldfld, FieldInfo);
3375                                 }
3376                         }
3377
3378                         if (leave_copy) {
3379                                 ec.ig.Emit (OpCodes.Dup);
3380                                 if (!FieldInfo.IsStatic) {
3381                                         temp = new LocalTemporary (this.Type);
3382                                         temp.Store (ec);
3383                                 }
3384                         }
3385                 }
3386
3387                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3388                 {
3389                         FieldAttributes fa = FieldInfo.Attributes;
3390                         bool is_static = (fa & FieldAttributes.Static) != 0;
3391                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3392                         ILGenerator ig = ec.ig;
3393                         prepared = prepare_for_load;
3394
3395                         if (is_readonly && !ec.IsConstructor){
3396                                 Report_AssignToReadonly (source);
3397                                 return;
3398                         }
3399
3400                         EmitInstance (ec, prepare_for_load);
3401
3402                         source.Emit (ec);
3403                         if (leave_copy) {
3404                                 ec.ig.Emit (OpCodes.Dup);
3405                                 if (!FieldInfo.IsStatic) {
3406                                         temp = new LocalTemporary (this.Type);
3407                                         temp.Store (ec);
3408                                 }
3409                         }
3410
3411                         FieldBase f = TypeManager.GetField (FieldInfo);
3412                         if (f != null){
3413                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3414                                         ig.Emit (OpCodes.Volatile);
3415                                         
3416                                 f.SetAssigned ();
3417                         }
3418
3419                         if (is_static)
3420                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3421                         else 
3422                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3423                         
3424                         if (temp != null) {
3425                                 temp.Emit (ec);
3426                                 temp.Release (ec);
3427                         }
3428                 }
3429
3430                 public override void Emit (EmitContext ec)
3431                 {
3432                         Emit (ec, false);
3433                 }
3434
3435                 public void AddressOf (EmitContext ec, AddressOp mode)
3436                 {
3437                         ILGenerator ig = ec.ig;
3438
3439                         FieldBase f = TypeManager.GetField (FieldInfo);
3440                         if (f != null){
3441                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
3442                                         Report.Warning (420, 1, loc, "`{0}': A volatile fields cannot be passed using a ref or out parameter",
3443                                                         f.GetSignatureForError ());
3444                                         return;
3445                                 }
3446                                         
3447                                 if ((mode & AddressOp.Store) != 0)
3448                                         f.SetAssigned ();
3449                                 if ((mode & AddressOp.Load) != 0)
3450                                         f.SetMemberIsUsed ();
3451                         }
3452
3453                         //
3454                         // Handle initonly fields specially: make a copy and then
3455                         // get the address of the copy.
3456                         //
3457                         bool need_copy;
3458                         if (FieldInfo.IsInitOnly){
3459                                 need_copy = true;
3460                                 if (ec.IsConstructor){
3461                                         if (FieldInfo.IsStatic){
3462                                                 if (ec.IsStatic)
3463                                                         need_copy = false;
3464                                         } else
3465                                                 need_copy = false;
3466                                 }
3467                         } else
3468                                 need_copy = false;
3469                         
3470                         if (need_copy){
3471                                 LocalBuilder local;
3472                                 Emit (ec);
3473                                 local = ig.DeclareLocal (type);
3474                                 ig.Emit (OpCodes.Stloc, local);
3475                                 ig.Emit (OpCodes.Ldloca, local);
3476                                 return;
3477                         }
3478
3479
3480                         if (FieldInfo.IsStatic){
3481                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3482                         } else {
3483                                 if (!prepared)
3484                                         EmitInstance (ec, false);
3485                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
3486                         }
3487                 }
3488         }
3489
3490         //
3491         // A FieldExpr whose address can not be taken
3492         //
3493         public class FieldExprNoAddress : FieldExpr, IMemoryLocation {
3494                 public FieldExprNoAddress (FieldInfo fi, Location loc) : base (fi, loc)
3495                 {
3496                 }
3497                 
3498                 public new void AddressOf (EmitContext ec, AddressOp mode)
3499                 {
3500                         Report.Error (-215, "Report this: Taking the address of a remapped parameter not supported");
3501                 }
3502         }
3503         
3504         /// <summary>
3505         ///   Expression that evaluates to a Property.  The Assign class
3506         ///   might set the `Value' expression if we are in an assignment.
3507         ///
3508         ///   This is not an LValue because we need to re-write the expression, we
3509         ///   can not take data from the stack and store it.  
3510         /// </summary>
3511         public class PropertyExpr : MemberExpr, IAssignMethod {
3512                 public readonly PropertyInfo PropertyInfo;
3513
3514                 //
3515                 // This is set externally by the  `BaseAccess' class
3516                 //
3517                 public bool IsBase;
3518                 MethodInfo getter, setter;
3519                 bool is_static;
3520
3521                 bool resolved;
3522                 
3523                 LocalTemporary temp;
3524                 bool prepared;
3525
3526                 public PropertyExpr (Type containerType, PropertyInfo pi, Location l)
3527                 {
3528                         PropertyInfo = pi;
3529                         eclass = ExprClass.PropertyAccess;
3530                         is_static = false;
3531                         loc = l;
3532
3533                         type = TypeManager.TypeToCoreType (pi.PropertyType);
3534
3535                         ResolveAccessors (containerType);
3536                 }
3537
3538                 public override string Name {
3539                         get {
3540                                 return PropertyInfo.Name;
3541                         }
3542                 }
3543
3544                 public override bool IsInstance {
3545                         get {
3546                                 return !is_static;
3547                         }
3548                 }
3549
3550                 public override bool IsStatic {
3551                         get {
3552                                 return is_static;
3553                         }
3554                 }
3555                 
3556                 public override Type DeclaringType {
3557                         get {
3558                                 return PropertyInfo.DeclaringType;
3559                         }
3560                 }
3561
3562                 public override string GetSignatureForError ()
3563                 {
3564                         return TypeManager.GetFullNameSignature (PropertyInfo);
3565                 }
3566
3567                 void FindAccessors (Type invocation_type)
3568                 {
3569                         const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
3570                                 BindingFlags.Static | BindingFlags.Instance |
3571                                 BindingFlags.DeclaredOnly;
3572
3573                         Type current = PropertyInfo.DeclaringType;
3574                         for (; current != null; current = current.BaseType) {
3575                                 MemberInfo[] group = TypeManager.MemberLookup (
3576                                         invocation_type, invocation_type, current,
3577                                         MemberTypes.Property, flags, PropertyInfo.Name, null);
3578
3579                                 if (group == null)
3580                                         continue;
3581
3582                                 if (group.Length != 1)
3583                                         // Oooops, can this ever happen ?
3584                                         return;
3585
3586                                 PropertyInfo pi = (PropertyInfo) group [0];
3587
3588                                 if (getter == null)
3589                                         getter = pi.GetGetMethod (true);
3590
3591                                 if (setter == null)
3592                                         setter = pi.GetSetMethod (true);
3593
3594                                 MethodInfo accessor = getter != null ? getter : setter;
3595
3596                                 if (!accessor.IsVirtual)
3597                                         return;
3598                         }
3599                 }
3600
3601                 //
3602                 // We also perform the permission checking here, as the PropertyInfo does not
3603                 // hold the information for the accessibility of its setter/getter
3604                 //
3605                 // TODO: Refactor to use some kind of cache together with GetPropertyFromAccessor
3606                 void ResolveAccessors (Type containerType)
3607                 {
3608                         FindAccessors (containerType);
3609
3610                         if (getter != null) {
3611                                 MethodBase the_getter = TypeManager.DropGenericMethodArguments (getter);
3612                                 IMethodData md = TypeManager.GetMethod (the_getter);
3613                                 if (md != null)
3614                                         md.SetMemberIsUsed ();
3615
3616                                 is_static = getter.IsStatic;
3617                         }
3618
3619                         if (setter != null) {
3620                                 MethodBase the_setter = TypeManager.DropGenericMethodArguments (setter);
3621                                 IMethodData md = TypeManager.GetMethod (the_setter);
3622                                 if (md != null)
3623                                         md.SetMemberIsUsed ();
3624
3625                                 is_static = setter.IsStatic;
3626                         }
3627                 }
3628
3629                 bool InstanceResolve (EmitContext ec, bool lvalue_instance, bool must_do_cs1540_check)
3630                 {
3631                         if (is_static) {
3632                                 InstanceExpression = null;
3633                                 return true;
3634                         }
3635
3636                         if (InstanceExpression == null) {
3637                                 SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
3638                                 return false;
3639                         }
3640
3641                         InstanceExpression = InstanceExpression.DoResolve (ec);
3642                         if (lvalue_instance && InstanceExpression != null)
3643                                 InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
3644
3645                         if (InstanceExpression == null)
3646                                 return false;
3647
3648                         InstanceExpression.CheckMarshalByRefAccess ();
3649
3650                         if (must_do_cs1540_check && (InstanceExpression != EmptyExpression.Null) &&
3651                             !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.ContainerType) &&
3652                             !TypeManager.IsNestedChildOf (ec.ContainerType, InstanceExpression.Type) &&
3653                             !TypeManager.IsSubclassOf (InstanceExpression.Type, ec.ContainerType)) {
3654                                 Report.SymbolRelatedToPreviousError (PropertyInfo);
3655                                 Error_CannotAccessProtected (loc, PropertyInfo, InstanceExpression.Type, ec.ContainerType);
3656                                 return false;
3657                         }
3658
3659                         return true;
3660                 }
3661
3662                 void Error_PropertyNotFound (MethodInfo mi, bool getter)
3663                 {
3664                         // TODO: correctly we should compare arguments but it will lead to bigger changes
3665                         if (mi is MethodBuilder) {
3666                                 Error_TypeDoesNotContainDefinition (loc, PropertyInfo.DeclaringType, Name);
3667                                 return;
3668                         }
3669
3670                         StringBuilder sig = new StringBuilder (TypeManager.CSharpName (mi.DeclaringType));
3671                         sig.Append ('.');
3672                         ParameterData iparams = TypeManager.GetParameterData (mi);
3673                         sig.Append (getter ? "get_" : "set_");
3674                         sig.Append (Name);
3675                         sig.Append (iparams.GetSignatureForError ());
3676
3677                         Report.SymbolRelatedToPreviousError (mi);
3678                         Report.Error (1546, loc, "Property `{0}' is not supported by the C# language. Try to call the accessor method `{1}' directly",
3679                                 Name, sig.ToString ());
3680                 }
3681                 
3682                 override public Expression DoResolve (EmitContext ec)
3683                 {
3684                         if (resolved)
3685                                 return this;
3686
3687                         if (getter != null){
3688                                 if (TypeManager.GetParameterData (getter).Count != 0){
3689                                         Error_PropertyNotFound (getter, true);
3690                                         return null;
3691                                 }
3692                         }
3693
3694                         if (getter == null){
3695                                 //
3696                                 // The following condition happens if the PropertyExpr was
3697                                 // created, but is invalid (ie, the property is inaccessible),
3698                                 // and we did not want to embed the knowledge about this in
3699                                 // the caller routine.  This only avoids double error reporting.
3700                                 //
3701                                 if (setter == null)
3702                                         return null;
3703
3704                                 if (InstanceExpression != EmptyExpression.Null) {
3705                                         Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
3706                                                 TypeManager.GetFullNameSignature (PropertyInfo));
3707                                         return null;
3708                                 }
3709                         } 
3710
3711                         bool must_do_cs1540_check = false;
3712                         if (getter != null &&
3713                             !IsAccessorAccessible (ec.ContainerType, getter, out must_do_cs1540_check)) {
3714                                 PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter) as PropertyBase.PropertyMethod;
3715                                 if (pm != null && pm.HasCustomAccessModifier) {
3716                                         Report.SymbolRelatedToPreviousError (pm);
3717                                         Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible",
3718                                                 TypeManager.CSharpSignature (getter));
3719                                 }
3720                                 else {
3721                                         Report.SymbolRelatedToPreviousError (getter);
3722                                         ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter));
3723                                 }
3724                                 return null;
3725                         }
3726                         
3727                         if (!InstanceResolve (ec, false, must_do_cs1540_check))
3728                                 return null;
3729
3730                         //
3731                         // Only base will allow this invocation to happen.
3732                         //
3733                         if (IsBase && getter.IsAbstract) {
3734                                 Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (PropertyInfo));
3735                                 return null;
3736                         }
3737
3738                         if (PropertyInfo.PropertyType.IsPointer && !ec.InUnsafe){
3739                                 UnsafeError (loc);
3740                                 return null;
3741                         }
3742
3743                         resolved = true;
3744
3745                         return this;
3746                 }
3747
3748                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3749                 {
3750                         if (right_side == EmptyExpression.OutAccess) {
3751                                 Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
3752                                               GetSignatureForError ());
3753                                 return null;
3754                         }
3755
3756                         if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
3757                                 Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
3758                                               GetSignatureForError ());
3759                                 return null;
3760                         }
3761
3762                         if (setter == null){
3763                                 //
3764                                 // The following condition happens if the PropertyExpr was
3765                                 // created, but is invalid (ie, the property is inaccessible),
3766                                 // and we did not want to embed the knowledge about this in
3767                                 // the caller routine.  This only avoids double error reporting.
3768                                 //
3769                                 if (getter == null)
3770                                         return null;
3771                                 Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read only)",
3772                                               GetSignatureForError ());
3773                                 return null;
3774                         }
3775
3776                         if (TypeManager.GetParameterData (setter).Count != 1){
3777                                 Error_PropertyNotFound (setter, false);
3778                                 return null;
3779                         }
3780
3781                         bool must_do_cs1540_check;
3782                         if (!IsAccessorAccessible (ec.ContainerType, setter, out must_do_cs1540_check)) {
3783                                 PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter) as PropertyBase.PropertyMethod;
3784                                 if (pm != null && pm.HasCustomAccessModifier) {
3785                                         Report.SymbolRelatedToPreviousError (pm);
3786                                         Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible",
3787                                                 TypeManager.CSharpSignature (setter));
3788                                 }
3789                                 else {
3790                                         Report.SymbolRelatedToPreviousError (setter);
3791                                         ErrorIsInaccesible (loc, TypeManager.CSharpSignature (setter));
3792                                 }
3793                                 return null;
3794                         }
3795                         
3796                         if (!InstanceResolve (ec, PropertyInfo.DeclaringType.IsValueType, must_do_cs1540_check))
3797                                 return null;
3798                         
3799                         //
3800                         // Only base will allow this invocation to happen.
3801                         //
3802                         if (IsBase && setter.IsAbstract){
3803                                 Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (PropertyInfo));
3804                                 return null;
3805                         }
3806
3807                         return this;
3808                 }
3809                 
3810                 public override void Emit (EmitContext ec)
3811                 {
3812                         Emit (ec, false);
3813                 }
3814                 
3815                 public void Emit (EmitContext ec, bool leave_copy)
3816                 {
3817                         //
3818                         // Special case: length of single dimension array property is turned into ldlen
3819                         //
3820                         if ((getter == TypeManager.system_int_array_get_length) ||
3821                             (getter == TypeManager.int_array_get_length)){
3822                                 Type iet = InstanceExpression.Type;
3823
3824                                 //
3825                                 // System.Array.Length can be called, but the Type does not
3826                                 // support invoking GetArrayRank, so test for that case first
3827                                 //
3828                                 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)) {
3829                                         if (!prepared)
3830                                                 EmitInstance (ec, false);
3831                                         ec.ig.Emit (OpCodes.Ldlen);
3832                                         ec.ig.Emit (OpCodes.Conv_I4);
3833                                         return;
3834                                 }
3835                         }
3836
3837                         Invocation.EmitCall (ec, IsBase, IsStatic, InstanceExpression, getter, null, loc, prepared, false);
3838                         
3839                         if (leave_copy) {
3840                                 ec.ig.Emit (OpCodes.Dup);
3841                                 if (!is_static) {
3842                                         temp = new LocalTemporary (this.Type);
3843                                         temp.Store (ec);
3844                                 }
3845                         }
3846                 }
3847
3848                 //
3849                 // Implements the IAssignMethod interface for assignments
3850                 //
3851                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3852                 {
3853                         Expression my_source = source;
3854
3855                         prepared = prepare_for_load;
3856                         
3857                         if (prepared) {
3858                                 source.Emit (ec);
3859                                 if (leave_copy) {
3860                                         ec.ig.Emit (OpCodes.Dup);
3861                                         if (!is_static) {
3862                                                 temp = new LocalTemporary (this.Type);
3863                                                 temp.Store (ec);
3864                                         }
3865                                 }
3866                         } else if (leave_copy) {
3867                                 source.Emit (ec);
3868                                 if (!is_static) {
3869                                         temp = new LocalTemporary (this.Type);
3870                                         temp.Store (ec);
3871                                 }
3872                                 my_source = temp;
3873                         }
3874                         
3875                         ArrayList args = new ArrayList (1);
3876                         args.Add (new Argument (my_source, Argument.AType.Expression));
3877                         
3878                         Invocation.EmitCall (ec, IsBase, IsStatic, InstanceExpression, setter, args, loc, false, prepared);
3879                         
3880                         if (temp != null) {
3881                                 temp.Emit (ec);
3882                                 temp.Release (ec);
3883                         }
3884                 }
3885         }
3886
3887         /// <summary>
3888         ///   Fully resolved expression that evaluates to an Event
3889         /// </summary>
3890         public class EventExpr : MemberExpr {
3891                 public readonly EventInfo EventInfo;
3892
3893                 bool is_static;
3894                 MethodInfo add_accessor, remove_accessor;
3895
3896                 public EventExpr (EventInfo ei, Location loc)
3897                 {
3898                         EventInfo = ei;
3899                         this.loc = loc;
3900                         eclass = ExprClass.EventAccess;
3901
3902                         add_accessor = TypeManager.GetAddMethod (ei);
3903                         remove_accessor = TypeManager.GetRemoveMethod (ei);
3904                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
3905                                 is_static = true;
3906
3907                         if (EventInfo is MyEventBuilder){
3908                                 MyEventBuilder eb = (MyEventBuilder) EventInfo;
3909                                 type = eb.EventType;
3910                                 eb.SetUsed ();
3911                         } else
3912                                 type = EventInfo.EventHandlerType;
3913                 }
3914
3915                 public override string Name {
3916                         get {
3917                                 return EventInfo.Name;
3918                         }
3919                 }
3920
3921                 public override bool IsInstance {
3922                         get {
3923                                 return !is_static;
3924                         }
3925                 }
3926
3927                 public override bool IsStatic {
3928                         get {
3929                                 return is_static;
3930                         }
3931                 }
3932
3933                 public override Type DeclaringType {
3934                         get {
3935                                 return EventInfo.DeclaringType;
3936                         }
3937                 }
3938
3939                 public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
3940                                                                 SimpleName original)
3941                 {
3942                         //
3943                         // If the event is local to this class, we transform ourselves into a FieldExpr
3944                         //
3945
3946                         if (EventInfo.DeclaringType == ec.ContainerType ||
3947                             TypeManager.IsNestedChildOf(ec.ContainerType, EventInfo.DeclaringType)) {
3948                                 EventField mi = TypeManager.GetEventField (EventInfo);
3949
3950                                 if (mi != null) {
3951                                         if (!ec.IsInObsoleteScope)
3952                                                 mi.CheckObsoleteness (loc);
3953
3954                                         FieldExpr ml = new FieldExpr (mi.FieldBuilder, loc);
3955
3956                                         InstanceExpression = null;
3957                                 
3958                                         return ml.ResolveMemberAccess (ec, left, loc, original);
3959                                 }
3960                         }
3961
3962                         return base.ResolveMemberAccess (ec, left, loc, original);
3963                 }
3964
3965
3966                 bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
3967                 {
3968                         if (is_static) {
3969                                 InstanceExpression = null;
3970                                 return true;
3971                         }
3972
3973                         if (InstanceExpression == null) {
3974                                 SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
3975                                 return false;
3976                         }
3977
3978                         InstanceExpression = InstanceExpression.DoResolve (ec);
3979                         if (InstanceExpression == null)
3980                                 return false;
3981
3982                         //
3983                         // This is using the same mechanism as the CS1540 check in PropertyExpr.
3984                         // However, in the Event case, we reported a CS0122 instead.
3985                         //
3986                         if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
3987                             InstanceExpression.Type != ec.ContainerType &&
3988                             ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
3989                                 Report.SymbolRelatedToPreviousError (EventInfo);
3990                                 ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
3991                                 return false;
3992                         }
3993
3994                         return true;
3995                 }
3996
3997                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3998                 {
3999                         return DoResolve (ec);
4000                 }
4001
4002                 public override Expression DoResolve (EmitContext ec)
4003                 {
4004                         bool must_do_cs1540_check;
4005                         if (!(IsAccessorAccessible (ec.ContainerType, add_accessor, out must_do_cs1540_check) &&
4006                               IsAccessorAccessible (ec.ContainerType, remove_accessor, out must_do_cs1540_check))) {
4007                                 Report.SymbolRelatedToPreviousError (EventInfo);
4008                                 ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
4009                                 return null;
4010                         }
4011
4012                         if (!InstanceResolve (ec, must_do_cs1540_check))
4013                                 return null;
4014                         
4015                         return this;
4016                 }               
4017
4018                 public override void Emit (EmitContext ec)
4019                 {
4020                         if (InstanceExpression is This)
4021                                 Report.Error (79, loc, "The event `{0}' can only appear on the left hand side of += or -=", GetSignatureForError ());
4022                         else
4023                                 Report.Error (70, loc, "The event `{0}' can only appear on the left hand side of += or -= "+
4024                                               "(except on the defining type)", Name);
4025                 }
4026
4027                 public override string GetSignatureForError ()
4028                 {
4029                         return TypeManager.CSharpSignature (EventInfo);
4030                 }
4031
4032                 public void EmitAddOrRemove (EmitContext ec, Expression source)
4033                 {
4034                         BinaryDelegate source_del = source as BinaryDelegate;
4035                         if (source_del == null) {
4036                                 Emit (ec);
4037                                 return;
4038                         }
4039                         Expression handler = source_del.Right;
4040                         
4041                         Argument arg = new Argument (handler, Argument.AType.Expression);
4042                         ArrayList args = new ArrayList ();
4043                                 
4044                         args.Add (arg);
4045                         
4046                         if (source_del.IsAddition)
4047                                 Invocation.EmitCall (
4048                                         ec, false, IsStatic, InstanceExpression, add_accessor, args, loc);
4049                         else
4050                                 Invocation.EmitCall (
4051                                         ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);
4052                 }
4053         }
4054
4055         public class TemporaryVariable : Expression, IMemoryLocation
4056         {
4057                 LocalInfo li;
4058                 Variable var;
4059                 
4060                 public TemporaryVariable (Type type, Location loc)
4061                 {
4062                         this.type = type;
4063                         this.loc = loc;
4064                         eclass = ExprClass.Value;
4065                 }
4066                 
4067                 public override Expression DoResolve (EmitContext ec)
4068                 {
4069                         if (li != null)
4070                                 return this;
4071                         
4072                         TypeExpr te = new TypeExpression (type, loc);
4073                         li = ec.CurrentBlock.AddTemporaryVariable (te, loc);
4074                         if (!li.Resolve (ec))
4075                                 return null;
4076
4077                         if (ec.MustCaptureVariable (li)) {
4078                                 ScopeInfo scope = li.Block.CreateScopeInfo ();
4079                                 var = scope.AddLocal (li);
4080                                 type = var.Type;
4081                         }
4082                         
4083                         return this;
4084                 }
4085
4086                 public Variable Variable {
4087                         get { return var != null ? var : li.Variable; }
4088                 }
4089                 
4090                 public override void Emit (EmitContext ec)
4091                 {
4092                         Variable.EmitInstance (ec);
4093                         Variable.Emit (ec);
4094                 }
4095                 
4096                 public void EmitLoadAddress (EmitContext ec)
4097                 {
4098                         Variable.EmitInstance (ec);
4099                         Variable.EmitAddressOf (ec);
4100                 }
4101                 
4102                 public void Store (EmitContext ec, Expression right_side)
4103                 {
4104                         Variable.EmitInstance (ec);
4105                         right_side.Emit (ec);
4106                         Variable.EmitAssign (ec);
4107                 }
4108                 
4109                 public void EmitThis (EmitContext ec)
4110                 {
4111                         Variable.EmitInstance (ec);
4112                 }
4113                 
4114                 public void EmitStore (EmitContext ec)
4115                 {
4116                         Variable.EmitAssign (ec);
4117                 }
4118                 
4119                 public void AddressOf (EmitContext ec, AddressOp mode)
4120                 {
4121                         EmitLoadAddress (ec);
4122                 }
4123         }
4124         
4125 }