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