**** Merged from MCS ****
[mono.git] / mcs / gmcs / 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 //
7 // (C) 2001, 2002, 2003 Ximian, Inc.
8 //
9 //
10
11 namespace Mono.CSharp {
12         using System;
13         using System.Collections;
14         using System.Diagnostics;
15         using System.Reflection;
16         using System.Reflection.Emit;
17         using System.Text;
18
19         /// <remarks>
20         ///   The ExprClass class contains the is used to pass the 
21         ///   classification of an expression (value, variable, namespace,
22         ///   type, method group, property access, event access, indexer access,
23         ///   nothing).
24         /// </remarks>
25         public enum ExprClass : byte {
26                 Invalid,
27                 
28                 Value,
29                 Variable,
30                 Namespace,
31                 Type,
32                 MethodGroup,
33                 PropertyAccess,
34                 EventAccess,
35                 IndexerAccess,
36                 Nothing, 
37         }
38
39         /// <remarks>
40         ///   This is used to tell Resolve in which types of expressions we're
41         ///   interested.
42         /// </remarks>
43         [Flags]
44         public enum ResolveFlags {
45                 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
46                 VariableOrValue         = 1,
47
48                 // Returns a type expression.
49                 Type                    = 2,
50
51                 // Returns a method group.
52                 MethodGroup             = 4,
53
54                 // Allows SimpleNames to be returned.
55                 // This is used by MemberAccess to construct long names that can not be
56                 // partially resolved (namespace-qualified names for example).
57                 SimpleName              = 8,
58
59                 // Mask of all the expression class flags.
60                 MaskExprClass           = 15,
61
62                 // Disable control flow analysis while resolving the expression.
63                 // This is used when resolving the instance expression of a field expression.
64                 DisableFlowAnalysis     = 16,
65
66                 // Set if this is resolving the first part of a MemberAccess.
67                 Intermediate            = 32
68         }
69
70         //
71         // This is just as a hint to AddressOf of what will be done with the
72         // address.
73         [Flags]
74         public enum AddressOp {
75                 Store = 1,
76                 Load  = 2,
77                 LoadStore = 3
78         };
79         
80         /// <summary>
81         ///   This interface is implemented by variables
82         /// </summary>
83         public interface IMemoryLocation {
84                 /// <summary>
85                 ///   The AddressOf method should generate code that loads
86                 ///   the address of the object and leaves it on the stack.
87                 ///
88                 ///   The `mode' argument is used to notify the expression
89                 ///   of whether this will be used to read from the address or
90                 ///   write to the address.
91                 ///
92                 ///   This is just a hint that can be used to provide good error
93                 ///   reporting, and should have no other side effects. 
94                 /// </summary>
95                 void AddressOf (EmitContext ec, AddressOp mode);
96         }
97
98         /// <summary>
99         ///   We are either a namespace or a type.
100         ///   If we're a type, `IsType' is true and we may use `Type' to get
101         ///   a TypeExpr representing that type.
102         /// </summary>
103         public interface IAlias {
104                 bool IsType {
105                         get;
106                 }
107
108                 string Name {
109                         get;
110                 }
111
112                 TypeExpr Type {
113                         get;
114                 }
115         }
116
117         /// <summary>
118         ///   This interface is implemented by variables
119         /// </summary>
120         public interface IVariable {
121                 VariableInfo VariableInfo {
122                         get;
123                 }
124
125                 bool VerifyFixed (bool is_expression);
126         }
127
128         /// <summary>
129         ///   This interface denotes an expression which evaluates to a member
130         ///   of a struct or a class.
131         /// </summary>
132         public interface IMemberExpr
133         {
134                 /// <summary>
135                 ///   The name of this member.
136                 /// </summary>
137                 string Name {
138                         get;
139                 }
140
141                 /// <summary>
142                 ///   Whether this is an instance member.
143                 /// </summary>
144                 bool IsInstance {
145                         get;
146                 }
147
148                 /// <summary>
149                 ///   Whether this is a static member.
150                 /// </summary>
151                 bool IsStatic {
152                         get;
153                 }
154
155                 /// <summary>
156                 ///   The type which declares this member.
157                 /// </summary>
158                 Type DeclaringType {
159                         get;
160                 }
161
162                 /// <summary>
163                 ///   The instance expression associated with this member, if it's a
164                 ///   non-static member.
165                 /// </summary>
166                 Expression InstanceExpression {
167                         get; set;
168                 }
169         }
170
171         /// <remarks>
172         ///   Base class for expressions
173         /// </remarks>
174         public abstract class Expression {
175                 public ExprClass eclass;
176                 protected Type type;
177                 protected Location loc;
178                 
179                 public Type Type {
180                         get {
181                                 return type;
182                         }
183
184                         set {
185                                 type = value;
186                         }
187                 }
188
189                 public Location Location {
190                         get {
191                                 return loc;
192                         }
193                 }
194
195                 /// <summary>
196                 ///   Utility wrapper routine for Error, just to beautify the code
197                 /// </summary>
198                 public void Error (int error, string s)
199                 {
200                         if (!Location.IsNull (loc))
201                                 Report.Error (error, loc, s);
202                         else
203                                 Report.Error (error, s);
204                 }
205
206                 /// <summary>
207                 ///   Utility wrapper routine for Warning, just to beautify the code
208                 /// </summary>
209                 public void Warning (int code, string format, params object[] args)
210                 {
211                         Report.Warning (code, loc, format, args);
212                 }
213
214                 /// <summary>
215                 /// Tests presence of ObsoleteAttribute and report proper error
216                 /// </summary>
217                 protected void CheckObsoleteAttribute (Type type)
218                 {
219                         ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
220                         if (obsolete_attr == null)
221                                 return;
222
223                         AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc);
224                 }
225
226                 /// <summary>
227                 ///   Performs semantic analysis on the Expression
228                 /// </summary>
229                 ///
230                 /// <remarks>
231                 ///   The Resolve method is invoked to perform the semantic analysis
232                 ///   on the node.
233                 ///
234                 ///   The return value is an expression (it can be the
235                 ///   same expression in some cases) or a new
236                 ///   expression that better represents this node.
237                 ///   
238                 ///   For example, optimizations of Unary (LiteralInt)
239                 ///   would return a new LiteralInt with a negated
240                 ///   value.
241                 ///   
242                 ///   If there is an error during semantic analysis,
243                 ///   then an error should be reported (using Report)
244                 ///   and a null value should be returned.
245                 ///   
246                 ///   There are two side effects expected from calling
247                 ///   Resolve(): the the field variable "eclass" should
248                 ///   be set to any value of the enumeration
249                 ///   `ExprClass' and the type variable should be set
250                 ///   to a valid type (this is the type of the
251                 ///   expression).
252                 /// </remarks>
253                 public abstract Expression DoResolve (EmitContext ec);
254
255                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
256                 {
257                         return DoResolve (ec);
258                 }
259
260                 //
261                 // This is used if the expression should be resolved as a type.
262                 // the default implementation fails.   Use this method in
263                 // those participants in the SimpleName chain system.
264                 //
265                 public virtual Expression ResolveAsTypeStep (EmitContext ec)
266                 {
267                         return null;
268                 }
269
270                 //
271                 // This is used to resolve the expression as a type, a null
272                 // value will be returned if the expression is not a type
273                 // reference
274                 //
275                 public TypeExpr ResolveAsTypeTerminal (EmitContext ec)
276                 {
277                         return ResolveAsTypeStep (ec) as TypeExpr;
278                 }
279                
280                 /// <summary>
281                 ///   Resolves an expression and performs semantic analysis on it.
282                 /// </summary>
283                 ///
284                 /// <remarks>
285                 ///   Currently Resolve wraps DoResolve to perform sanity
286                 ///   checking and assertion checking on what we expect from Resolve.
287                 /// </remarks>
288                 public Expression Resolve (EmitContext ec, ResolveFlags flags)
289                 {
290                         if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) 
291                                 return ResolveAsTypeStep (ec);
292
293                         bool old_do_flow_analysis = ec.DoFlowAnalysis;
294                         if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
295                                 ec.DoFlowAnalysis = false;
296
297                         Expression e;
298                         bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
299                         if (this is SimpleName)
300                                 e = ((SimpleName) this).DoResolveAllowStatic (ec, intermediate);
301
302                         else 
303                                 e = DoResolve (ec);
304
305                         ec.DoFlowAnalysis = old_do_flow_analysis;
306
307                         if (e == null)
308                                 return null;
309
310                         if (e is SimpleName){
311                                 SimpleName s = (SimpleName) e;
312
313                                 if ((flags & ResolveFlags.SimpleName) == 0) {
314                                         MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
315                                                             ec.DeclSpace.Name, loc);
316                                         return null;
317                                 }
318
319                                 return s;
320                         }
321
322                         if ((e is TypeExpr) || (e is ComposedCast)) {
323                                 if ((flags & ResolveFlags.Type) == 0) {
324                                         e.Error_UnexpectedKind (flags);
325                                         return null;
326                                 }
327
328                                 return e;
329                         }
330
331                         switch (e.eclass) {
332                         case ExprClass.Type:
333                                 if ((flags & ResolveFlags.VariableOrValue) == 0) {
334                                         e.Error_UnexpectedKind (flags);
335                                         return null;
336                                 }
337                                 break;
338
339                         case ExprClass.MethodGroup:
340                                 if (!RootContext.V2){
341                                 if ((flags & ResolveFlags.MethodGroup) == 0) {
342                                         ((MethodGroupExpr) e).ReportUsageError ();
343                                         return null;
344                                 }
345                                 }
346                                 break;
347
348                         case ExprClass.Value:
349                         case ExprClass.Variable:
350                         case ExprClass.PropertyAccess:
351                         case ExprClass.EventAccess:
352                         case ExprClass.IndexerAccess:
353                                 if ((flags & ResolveFlags.VariableOrValue) == 0) {
354                                         Console.WriteLine ("I got: {0} and {1}", e.GetType (), e);
355                                         Console.WriteLine ("I am {0} and {1}", this.GetType (), this);
356                                         FieldInfo fi = ((FieldExpr) e).FieldInfo;
357                                         
358                                         Console.WriteLine ("{0} and {1}", fi.DeclaringType, fi.Name);
359                                         e.Error_UnexpectedKind (flags);
360                                         return null;
361                                 }
362                                 break;
363
364                         default:
365                                 throw new Exception ("Expression " + e.GetType () +
366                                                      " ExprClass is Invalid after resolve");
367                         }
368
369                         if (e.type == null)
370                                 throw new Exception (
371                                         "Expression " + e.GetType () +
372                                         " did not set its type after Resolve\n" +
373                                         "called from: " + this.GetType ());
374
375                         return e;
376                 }
377
378                 /// <summary>
379                 ///   Resolves an expression and performs semantic analysis on it.
380                 /// </summary>
381                 public Expression Resolve (EmitContext ec)
382                 {
383                         return Resolve (ec, ResolveFlags.VariableOrValue);
384                 }
385
386                 /// <summary>
387                 ///   Resolves an expression for LValue assignment
388                 /// </summary>
389                 ///
390                 /// <remarks>
391                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
392                 ///   checking and assertion checking on what we expect from Resolve
393                 /// </remarks>
394                 public Expression ResolveLValue (EmitContext ec, Expression right_side)
395                 {
396                         Expression e = DoResolveLValue (ec, right_side);
397
398                         if (e != null){
399                                 if (e is SimpleName){
400                                         SimpleName s = (SimpleName) e;
401                                         MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
402                                                             ec.DeclSpace.Name, loc);
403                                         return null;
404                                 }
405
406                                 if (e.eclass == ExprClass.Invalid)
407                                         throw new Exception ("Expression " + e +
408                                                              " ExprClass is Invalid after resolve");
409
410                                 if (e.eclass == ExprClass.MethodGroup) {
411                                         ((MethodGroupExpr) e).ReportUsageError ();
412                                         return null;
413                                 }
414
415                                 if ((e.type == null) && !(e is ConstructedType))
416                                         throw new Exception ("Expression " + e +
417                                                              " did not set its type after Resolve");
418                         }
419
420                         return e;
421                 }
422                 
423                 /// <summary>
424                 ///   Emits the code for the expression
425                 /// </summary>
426                 ///
427                 /// <remarks>
428                 ///   The Emit method is invoked to generate the code
429                 ///   for the expression.  
430                 /// </remarks>
431                 public abstract void Emit (EmitContext ec);
432
433                 public virtual void EmitBranchable (EmitContext ec, Label target, bool onTrue)
434                 {
435                         Emit (ec);
436                         ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
437                 }
438
439                 /// <summary>
440                 ///   Protected constructor.  Only derivate types should
441                 ///   be able to be created
442                 /// </summary>
443
444                 protected Expression ()
445                 {
446                         eclass = ExprClass.Invalid;
447                         type = null;
448                 }
449
450                 /// <summary>
451                 ///   Returns a literalized version of a literal FieldInfo
452                 /// </summary>
453                 ///
454                 /// <remarks>
455                 ///   The possible return values are:
456                 ///      IntConstant, UIntConstant
457                 ///      LongLiteral, ULongConstant
458                 ///      FloatConstant, DoubleConstant
459                 ///      StringConstant
460                 ///
461                 ///   The value returned is already resolved.
462                 /// </remarks>
463                 public static Constant Constantify (object v, Type t)
464                 {
465                         if (t == TypeManager.int32_type)
466                                 return new IntConstant ((int) v);
467                         else if (t == TypeManager.uint32_type)
468                                 return new UIntConstant ((uint) v);
469                         else if (t == TypeManager.int64_type)
470                                 return new LongConstant ((long) v);
471                         else if (t == TypeManager.uint64_type)
472                                 return new ULongConstant ((ulong) v);
473                         else if (t == TypeManager.float_type)
474                                 return new FloatConstant ((float) v);
475                         else if (t == TypeManager.double_type)
476                                 return new DoubleConstant ((double) v);
477                         else if (t == TypeManager.string_type)
478                                 return new StringConstant ((string) v);
479                         else if (t == TypeManager.short_type)
480                                 return new ShortConstant ((short)v);
481                         else if (t == TypeManager.ushort_type)
482                                 return new UShortConstant ((ushort)v);
483                         else if (t == TypeManager.sbyte_type)
484                                 return new SByteConstant (((sbyte)v));
485                         else if (t == TypeManager.byte_type)
486                                 return new ByteConstant ((byte)v);
487                         else if (t == TypeManager.char_type)
488                                 return new CharConstant ((char)v);
489                         else if (t == TypeManager.bool_type)
490                                 return new BoolConstant ((bool) v);
491                         else if (TypeManager.IsEnumType (t)){
492                                 Type real_type = TypeManager.TypeToCoreType (v.GetType ());
493                                 if (real_type == t)
494                                         real_type = real_type.UnderlyingSystemType;
495
496                                 Constant e = Constantify (v, real_type);
497
498                                 return new EnumConstant (e, t);
499                         } else
500                                 throw new Exception ("Unknown type for constant (" + t +
501                                                      "), details: " + v);
502                 }
503
504                 /// <summary>
505                 ///   Returns a fully formed expression after a MemberLookup
506                 /// </summary>
507                 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
508                 {
509                         if (mi is EventInfo)
510                                 return new EventExpr ((EventInfo) mi, loc);
511                         else if (mi is FieldInfo)
512                                 return new FieldExpr ((FieldInfo) mi, loc);
513                         else if (mi is PropertyInfo)
514                                 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
515                         else if (mi is Type){
516                                 return new TypeExpression ((System.Type) mi, loc);
517                         }
518
519                         return null;
520                 }
521
522
523                 private static ArrayList almostMatchedMembers = new ArrayList (4);
524
525                 //
526                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
527                 //
528                 // This code could use some optimizations, but we need to do some
529                 // measurements.  For example, we could use a delegate to `flag' when
530                 // something can not any longer be a method-group (because it is something
531                 // else).
532                 //
533                 // Return values:
534                 //     If the return value is an Array, then it is an array of
535                 //     MethodBases
536                 //   
537                 //     If the return value is an MemberInfo, it is anything, but a Method
538                 //
539                 //     null on error.
540                 //
541                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
542                 // the arguments here and have MemberLookup return only the methods that
543                 // match the argument count/type, unlike we are doing now (we delay this
544                 // decision).
545                 //
546                 // This is so we can catch correctly attempts to invoke instance methods
547                 // from a static body (scan for error 120 in ResolveSimpleName).
548                 //
549                 //
550                 // FIXME: Potential optimization, have a static ArrayList
551                 //
552
553                 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
554                                                        MemberTypes mt, BindingFlags bf, Location loc)
555                 {
556                         return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
557                 }
558
559                 //
560                 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
561                 // `qualifier_type' or null to lookup members in the current class.
562                 //
563
564                 public static Expression MemberLookup (EmitContext ec, Type container_type,
565                                                        Type qualifier_type, Type queried_type,
566                                                        string name, MemberTypes mt,
567                                                        BindingFlags bf, Location loc)
568                 {
569                         almostMatchedMembers.Clear ();
570
571                         MemberInfo [] mi = TypeManager.MemberLookup (
572                                 container_type, qualifier_type,queried_type, mt, bf, name,
573                                 almostMatchedMembers);
574
575                         if (mi == null)
576                                 return null;
577
578                         int count = mi.Length;
579
580                         if (mi [0] is MethodBase)
581                                 return new MethodGroupExpr (mi, loc);
582
583                         if (count > 1)
584                                 return null;
585
586                         return ExprClassFromMemberInfo (ec, mi [0], loc);
587                 }
588
589                 public const MemberTypes AllMemberTypes =
590                         MemberTypes.Constructor |
591                         MemberTypes.Event       |
592                         MemberTypes.Field       |
593                         MemberTypes.Method      |
594                         MemberTypes.NestedType  |
595                         MemberTypes.Property;
596                 
597                 public const BindingFlags AllBindingFlags =
598                         BindingFlags.Public |
599                         BindingFlags.Static |
600                         BindingFlags.Instance;
601
602                 public static Expression MemberLookup (EmitContext ec, Type queried_type,
603                                                        string name, Location loc)
604                 {
605                         return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
606                                              AllMemberTypes, AllBindingFlags, loc);
607                 }
608
609                 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
610                                                        Type queried_type, string name, Location loc)
611                 {
612                         return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
613                                              name, AllMemberTypes, AllBindingFlags, loc);
614                 }
615
616                 public static Expression MethodLookup (EmitContext ec, Type queried_type,
617                                                        string name, Location loc)
618                 {
619                         return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
620                                              MemberTypes.Method, AllBindingFlags, loc);
621                 }
622
623                 /// <summary>
624                 ///   This is a wrapper for MemberLookup that is not used to "probe", but
625                 ///   to find a final definition.  If the final definition is not found, we
626                 ///   look for private members and display a useful debugging message if we
627                 ///   find it.
628                 /// </summary>
629                 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
630                                                             Type queried_type, string name,
631                                                             Location loc)
632                 {
633                         return MemberLookupFinal (ec, qualifier_type, queried_type, name,
634                                                   AllMemberTypes, AllBindingFlags, loc);
635                 }
636
637                 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
638                                                             Type queried_type, string name,
639                                                             MemberTypes mt, BindingFlags bf,
640                                                             Location loc)
641                 {
642                         Expression e;
643
644                         int errors = Report.Errors;
645
646                         e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
647                                           name, mt, bf, loc);
648
649                         if (e == null && errors == Report.Errors)
650                                 // No errors were reported by MemberLookup, but there was an error.
651                                 MemberLookupFailed (ec, qualifier_type, queried_type, name,
652                                                     null, loc);
653
654                         return e;
655                 }
656
657                 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
658                                                        Type queried_type, string name,
659                                                        string class_name, Location loc)
660                 {
661                         if (almostMatchedMembers.Count != 0) {
662                                 if (qualifier_type == null) {
663                                         foreach (MemberInfo m in almostMatchedMembers)
664                                                 Report.Error (38, loc, 
665                                                               "Cannot access non-static member `{0}' via nested type `{1}'", 
666                                                               TypeManager.GetFullNameSignature (m),
667                                                               TypeManager.CSharpName (ec.ContainerType));
668                                         return;
669                                 }
670
671
672                                 if (qualifier_type != ec.ContainerType) {
673                                         // Although a derived class can access protected members of
674                                         // its base class it cannot do so through an instance of the
675                                         // base class (CS1540).  If the qualifier_type is a parent of the
676                                         // ec.ContainerType and the lookup succeeds with the latter one,
677                                         // then we are in this situation.
678                                         foreach (MemberInfo m in almostMatchedMembers)
679                                                 Report.Error (1540, loc, 
680                                                               "Cannot access protected member `{0}' via a qualifier of type `{1}';"
681                                                               + " the qualifier must be of type `{2}' (or derived from it)", 
682                                                               TypeManager.GetFullNameSignature (m),
683                                                               TypeManager.CSharpName (qualifier_type),
684                                                               TypeManager.CSharpName (ec.ContainerType));
685                                         return;
686                                 }
687                                 almostMatchedMembers.Clear ();
688                         }
689
690                         MemberInfo[] mi = TypeManager.MemberLookup (queried_type, null, queried_type,
691                                                                     AllMemberTypes, AllBindingFlags |
692                                                                     BindingFlags.NonPublic, name, null);
693
694                         if (mi == null) {
695                                 if (class_name != null)
696                                         Report.Error (103, loc, "The name `" + name + "' could not be " +
697                                                       "found in `" + class_name + "'");
698                                 else
699                                         Report.Error (
700                                                 117, loc, "`" + queried_type + "' does not contain a " +
701                                                 "definition for `" + name + "'");
702                                 return;
703                         }
704
705                         if (TypeManager.MemberLookup (queried_type, null, queried_type,
706                                                       AllMemberTypes, AllBindingFlags |
707                                                       BindingFlags.NonPublic, name, null) == null) {
708                                 if ((mi.Length == 1) && (mi [0] is Type)) {
709                                         Type t = (Type) mi [0];
710
711                                         Report.Error (305, loc,
712                                                       "Using the generic type `{0}' " +
713                                                       "requires {1} type arguments",
714                                                       TypeManager.GetFullName (t),
715                                                       TypeManager.GetNumberOfTypeArguments (t));
716                                         return;
717                                 }
718                         }
719
720
721
722                         if (qualifier_type != null)
723                                 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", TypeManager.CSharpName (qualifier_type) + "." + name);
724                         else if (name == ".ctor") {
725                                 Report.Error (143, loc, String.Format ("The type {0} has no constructors defined",
726                                                                        TypeManager.CSharpName (queried_type)));
727                         } else {
728                                 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", name);
729                 }
730                 }
731
732                 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
733                 {
734                         EventInfo ei = event_expr.EventInfo;
735
736                         return TypeManager.GetPrivateFieldOfEvent (ei);
737                 }
738                 
739                 /// <summary>
740                 ///   Returns an expression that can be used to invoke operator true
741                 ///   on the expression if it exists.
742                 /// </summary>
743                 static public StaticCallExpr GetOperatorTrue (EmitContext ec, Expression e, Location loc)
744                 {
745                         return GetOperatorTrueOrFalse (ec, e, true, loc);
746                 }
747
748                 /// <summary>
749                 ///   Returns an expression that can be used to invoke operator false
750                 ///   on the expression if it exists.
751                 /// </summary>
752                 static public StaticCallExpr GetOperatorFalse (EmitContext ec, Expression e, Location loc)
753                 {
754                         return GetOperatorTrueOrFalse (ec, e, false, loc);
755                 }
756
757                 static StaticCallExpr GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
758                 {
759                         MethodBase method;
760                         Expression operator_group;
761
762                         operator_group = MethodLookup (ec, e.Type, is_true ? "op_True" : "op_False", loc);
763                         if (operator_group == null)
764                                 return null;
765
766                         ArrayList arguments = new ArrayList ();
767                         arguments.Add (new Argument (e, Argument.AType.Expression));
768                         method = Invocation.OverloadResolve (
769                                 ec, (MethodGroupExpr) operator_group, arguments, false, loc);
770
771                         if (method == null)
772                                 return null;
773
774                         return new StaticCallExpr ((MethodInfo) method, arguments, loc);
775                 }
776
777                 /// <summary>
778                 ///   Resolves the expression `e' into a boolean expression: either through
779                 ///   an implicit conversion, or through an `operator true' invocation
780                 /// </summary>
781                 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
782                 {
783                         e = e.Resolve (ec);
784                         if (e == null)
785                                 return null;
786
787                         Expression converted = e;
788                         if (e.Type != TypeManager.bool_type)
789                                 converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, new Location (-1));
790
791                         //
792                         // If no implicit conversion to bool exists, try using `operator true'
793                         //
794                         if (converted == null){
795                                 Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
796                                 if (operator_true == null){
797                                         Report.Error (
798                                                 31, loc, "Can not convert the expression to a boolean");
799                                         return null;
800                                 }
801                                 e = operator_true;
802                         } else
803                                 e = converted;
804
805                         return e;
806                 }
807                 
808                 static string ExprClassName (ExprClass c)
809                 {
810                         switch (c){
811                         case ExprClass.Invalid:
812                                 return "Invalid";
813                         case ExprClass.Value:
814                                 return "value";
815                         case ExprClass.Variable:
816                                 return "variable";
817                         case ExprClass.Namespace:
818                                 return "namespace";
819                         case ExprClass.Type:
820                                 return "type";
821                         case ExprClass.MethodGroup:
822                                 return "method group";
823                         case ExprClass.PropertyAccess:
824                                 return "property access";
825                         case ExprClass.EventAccess:
826                                 return "event access";
827                         case ExprClass.IndexerAccess:
828                                 return "indexer access";
829                         case ExprClass.Nothing:
830                                 return "null";
831                         }
832                         throw new Exception ("Should not happen");
833                 }
834                 
835                 /// <summary>
836                 ///   Reports that we were expecting `expr' to be of class `expected'
837                 /// </summary>
838                 public void Error_UnexpectedKind (string expected)
839                 {
840                         string kind = "Unknown";
841                         
842                         kind = ExprClassName (eclass);
843
844                         Error (118, "Expression denotes a `" + kind +
845                                "' where a `" + expected + "' was expected");
846                 }
847
848                 public void Error_UnexpectedKind (ResolveFlags flags)
849                 {
850                         ArrayList valid = new ArrayList (10);
851
852                         if ((flags & ResolveFlags.VariableOrValue) != 0) {
853                                 valid.Add ("variable");
854                                 valid.Add ("value");
855                         }
856
857                         if ((flags & ResolveFlags.Type) != 0)
858                                 valid.Add ("type");
859
860                         if ((flags & ResolveFlags.MethodGroup) != 0)
861                                 valid.Add ("method group");
862
863                         if ((flags & ResolveFlags.SimpleName) != 0)
864                                 valid.Add ("simple name");
865
866                         if (valid.Count == 0)
867                                 valid.Add ("unknown");
868
869                         StringBuilder sb = new StringBuilder ();
870                         for (int i = 0; i < valid.Count; i++) {
871                                 if (i > 0)
872                                         sb.Append (", ");
873                                 else if (i == valid.Count)
874                                         sb.Append (" or ");
875                                 sb.Append (valid [i]);
876                         }
877
878                         string kind = ExprClassName (eclass);
879
880                         Error (119, "Expression denotes a `" + kind + "' where " +
881                                "a `" + sb.ToString () + "' was expected");
882                 }
883                 
884                 static public void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
885                 {
886                         Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
887                                       TypeManager.CSharpName (t));
888                 }
889
890                 public static void UnsafeError (Location loc)
891                 {
892                         Report.Error (214, loc, "Pointers may only be used in an unsafe context");
893                 }
894                 
895                 /// <summary>
896                 ///   Converts the IntConstant, UIntConstant, LongConstant or
897                 ///   ULongConstant into the integral target_type.   Notice
898                 ///   that we do not return an `Expression' we do return
899                 ///   a boxed integral type.
900                 ///
901                 ///   FIXME: Since I added the new constants, we need to
902                 ///   also support conversions from CharConstant, ByteConstant,
903                 ///   SByteConstant, UShortConstant, ShortConstant
904                 ///
905                 ///   This is used by the switch statement, so the domain
906                 ///   of work is restricted to the literals above, and the
907                 ///   targets are int32, uint32, char, byte, sbyte, ushort,
908                 ///   short, uint64 and int64
909                 /// </summary>
910                 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
911                 {
912                         if (!Convert.ImplicitStandardConversionExists (c, target_type)){
913                                 Convert.Error_CannotImplicitConversion (loc, c.Type, target_type);
914                                 return null;
915                         }
916                         
917                         string s = "";
918
919                         if (c.Type == target_type)
920                                 return ((Constant) c).GetValue ();
921
922                         //
923                         // Make into one of the literals we handle, we dont really care
924                         // about this value as we will just return a few limited types
925                         // 
926                         if (c is EnumConstant)
927                                 c = ((EnumConstant)c).WidenToCompilerConstant ();
928
929                         if (c is IntConstant){
930                                 int v = ((IntConstant) c).Value;
931                                 
932                                 if (target_type == TypeManager.uint32_type){
933                                         if (v >= 0)
934                                                 return (uint) v;
935                                 } else if (target_type == TypeManager.char_type){
936                                         if (v >= Char.MinValue && v <= Char.MaxValue)
937                                                 return (char) v;
938                                 } else if (target_type == TypeManager.byte_type){
939                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
940                                                 return (byte) v;
941                                 } else if (target_type == TypeManager.sbyte_type){
942                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
943                                                 return (sbyte) v;
944                                 } else if (target_type == TypeManager.short_type){
945                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
946                                                 return (short) v;
947                                 } else if (target_type == TypeManager.ushort_type){
948                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
949                                                 return (ushort) v;
950                                 } else if (target_type == TypeManager.int64_type)
951                                         return (long) v;
952                                 else if (target_type == TypeManager.uint64_type){
953                                         if (v > 0)
954                                                 return (ulong) v;
955                                 }
956
957                                 s = v.ToString ();
958                         } else if (c is UIntConstant){
959                                 uint v = ((UIntConstant) c).Value;
960
961                                 if (target_type == TypeManager.int32_type){
962                                         if (v <= Int32.MaxValue)
963                                                 return (int) v;
964                                 } else if (target_type == TypeManager.char_type){
965                                         if (v >= Char.MinValue && v <= Char.MaxValue)
966                                                 return (char) v;
967                                 } else if (target_type == TypeManager.byte_type){
968                                         if (v <= Byte.MaxValue)
969                                                 return (byte) v;
970                                 } else if (target_type == TypeManager.sbyte_type){
971                                         if (v <= SByte.MaxValue)
972                                                 return (sbyte) v;
973                                 } else if (target_type == TypeManager.short_type){
974                                         if (v <= UInt16.MaxValue)
975                                                 return (short) v;
976                                 } else if (target_type == TypeManager.ushort_type){
977                                         if (v <= UInt16.MaxValue)
978                                                 return (ushort) v;
979                                 } else if (target_type == TypeManager.int64_type)
980                                         return (long) v;
981                                 else if (target_type == TypeManager.uint64_type)
982                                         return (ulong) v;
983                                 s = v.ToString ();
984                         } else if (c is LongConstant){ 
985                                 long v = ((LongConstant) c).Value;
986
987                                 if (target_type == TypeManager.int32_type){
988                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
989                                                 return (int) v;
990                                 } else if (target_type == TypeManager.uint32_type){
991                                         if (v >= 0 && v <= UInt32.MaxValue)
992                                                 return (uint) v;
993                                 } else if (target_type == TypeManager.char_type){
994                                         if (v >= Char.MinValue && v <= Char.MaxValue)
995                                                 return (char) v;
996                                 } else if (target_type == TypeManager.byte_type){
997                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
998                                                 return (byte) v;
999                                 } else if (target_type == TypeManager.sbyte_type){
1000                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
1001                                                 return (sbyte) v;
1002                                 } else if (target_type == TypeManager.short_type){
1003                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1004                                                 return (short) v;
1005                                 } else if (target_type == TypeManager.ushort_type){
1006                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1007                                                 return (ushort) v;
1008                                 } else if (target_type == TypeManager.uint64_type){
1009                                         if (v > 0)
1010                                                 return (ulong) v;
1011                                 }
1012                                 s = v.ToString ();
1013                         } else if (c is ULongConstant){
1014                                 ulong v = ((ULongConstant) c).Value;
1015
1016                                 if (target_type == TypeManager.int32_type){
1017                                         if (v <= Int32.MaxValue)
1018                                                 return (int) v;
1019                                 } else if (target_type == TypeManager.uint32_type){
1020                                         if (v <= UInt32.MaxValue)
1021                                                 return (uint) v;
1022                                 } else if (target_type == TypeManager.char_type){
1023                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1024                                                 return (char) v;
1025                                 } else if (target_type == TypeManager.byte_type){
1026                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1027                                                 return (byte) v;
1028                                 } else if (target_type == TypeManager.sbyte_type){
1029                                         if (v <= (int) SByte.MaxValue)
1030                                                 return (sbyte) v;
1031                                 } else if (target_type == TypeManager.short_type){
1032                                         if (v <= UInt16.MaxValue)
1033                                                 return (short) v;
1034                                 } else if (target_type == TypeManager.ushort_type){
1035                                         if (v <= UInt16.MaxValue)
1036                                                 return (ushort) v;
1037                                 } else if (target_type == TypeManager.int64_type){
1038                                         if (v <= Int64.MaxValue)
1039                                                 return (long) v;
1040                                 }
1041                                 s = v.ToString ();
1042                         } else if (c is ByteConstant){
1043                                 byte v = ((ByteConstant) c).Value;
1044                                 
1045                                 if (target_type == TypeManager.int32_type)
1046                                         return (int) v;
1047                                 else if (target_type == TypeManager.uint32_type)
1048                                         return (uint) v;
1049                                 else if (target_type == TypeManager.char_type)
1050                                         return (char) v;
1051                                 else if (target_type == TypeManager.sbyte_type){
1052                                         if (v <= SByte.MaxValue)
1053                                                 return (sbyte) v;
1054                                 } else if (target_type == TypeManager.short_type)
1055                                         return (short) v;
1056                                 else if (target_type == TypeManager.ushort_type)
1057                                         return (ushort) v;
1058                                 else if (target_type == TypeManager.int64_type)
1059                                         return (long) v;
1060                                 else if (target_type == TypeManager.uint64_type)
1061                                         return (ulong) v;
1062                                 s = v.ToString ();
1063                         } else if (c is SByteConstant){
1064                                 sbyte v = ((SByteConstant) c).Value;
1065                                 
1066                                 if (target_type == TypeManager.int32_type)
1067                                         return (int) v;
1068                                 else if (target_type == TypeManager.uint32_type){
1069                                         if (v >= 0)
1070                                                 return (uint) v;
1071                                 } else if (target_type == TypeManager.char_type){
1072                                         if (v >= 0)
1073                                                 return (char) v;
1074                                 } else if (target_type == TypeManager.byte_type){
1075                                         if (v >= 0)
1076                                                 return (byte) v;
1077                                 } else if (target_type == TypeManager.short_type)
1078                                         return (short) v;
1079                                 else if (target_type == TypeManager.ushort_type){
1080                                         if (v >= 0)
1081                                                 return (ushort) v;
1082                                 } else if (target_type == TypeManager.int64_type)
1083                                         return (long) v;
1084                                 else if (target_type == TypeManager.uint64_type){
1085                                         if (v >= 0)
1086                                                 return (ulong) v;
1087                                 }
1088                                 s = v.ToString ();
1089                         } else if (c is ShortConstant){
1090                                 short v = ((ShortConstant) c).Value;
1091                                 
1092                                 if (target_type == TypeManager.int32_type){
1093                                         return (int) v;
1094                                 } else if (target_type == TypeManager.uint32_type){
1095                                         if (v >= 0)
1096                                                 return (uint) v;
1097                                 } else if (target_type == TypeManager.char_type){
1098                                         if (v >= 0)
1099                                                 return (char) v;
1100                                 } else if (target_type == TypeManager.byte_type){
1101                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1102                                                 return (byte) v;
1103                                 } else if (target_type == TypeManager.sbyte_type){
1104                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
1105                                                 return (sbyte) v;
1106                                 } else if (target_type == TypeManager.ushort_type){
1107                                         if (v >= 0)
1108                                                 return (ushort) v;
1109                                 } else if (target_type == TypeManager.int64_type)
1110                                         return (long) v;
1111                                 else if (target_type == TypeManager.uint64_type)
1112                                         return (ulong) v;
1113
1114                                 s = v.ToString ();
1115                         } else if (c is UShortConstant){
1116                                 ushort v = ((UShortConstant) c).Value;
1117                                 
1118                                 if (target_type == TypeManager.int32_type)
1119                                         return (int) v;
1120                                 else if (target_type == TypeManager.uint32_type)
1121                                         return (uint) v;
1122                                 else if (target_type == TypeManager.char_type){
1123                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1124                                                 return (char) v;
1125                                 } else if (target_type == TypeManager.byte_type){
1126                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1127                                                 return (byte) v;
1128                                 } else if (target_type == TypeManager.sbyte_type){
1129                                         if (v <= SByte.MaxValue)
1130                                                 return (byte) v;
1131                                 } else if (target_type == TypeManager.short_type){
1132                                         if (v <= Int16.MaxValue)
1133                                                 return (short) v;
1134                                 } else if (target_type == TypeManager.int64_type)
1135                                         return (long) v;
1136                                 else if (target_type == TypeManager.uint64_type)
1137                                         return (ulong) v;
1138
1139                                 s = v.ToString ();
1140                         } else if (c is CharConstant){
1141                                 char v = ((CharConstant) c).Value;
1142                                 
1143                                 if (target_type == TypeManager.int32_type)
1144                                         return (int) v;
1145                                 else if (target_type == TypeManager.uint32_type)
1146                                         return (uint) v;
1147                                 else if (target_type == TypeManager.byte_type){
1148                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1149                                                 return (byte) v;
1150                                 } else if (target_type == TypeManager.sbyte_type){
1151                                         if (v <= SByte.MaxValue)
1152                                                 return (sbyte) v;
1153                                 } else if (target_type == TypeManager.short_type){
1154                                         if (v <= Int16.MaxValue)
1155                                                 return (short) v;
1156                                 } else if (target_type == TypeManager.ushort_type)
1157                                         return (short) v;
1158                                 else if (target_type == TypeManager.int64_type)
1159                                         return (long) v;
1160                                 else if (target_type == TypeManager.uint64_type)
1161                                         return (ulong) v;
1162
1163                                 s = v.ToString ();
1164                         }
1165                         Error_ConstantValueCannotBeConverted (loc, s, target_type);
1166                         return null;
1167                 }
1168
1169                 //
1170                 // Load the object from the pointer.  
1171                 //
1172                 public static void LoadFromPtr (ILGenerator ig, Type t)
1173                 {
1174                         if (t == TypeManager.int32_type)
1175                                 ig.Emit (OpCodes.Ldind_I4);
1176                         else if (t == TypeManager.uint32_type)
1177                                 ig.Emit (OpCodes.Ldind_U4);
1178                         else if (t == TypeManager.short_type)
1179                                 ig.Emit (OpCodes.Ldind_I2);
1180                         else if (t == TypeManager.ushort_type)
1181                                 ig.Emit (OpCodes.Ldind_U2);
1182                         else if (t == TypeManager.char_type)
1183                                 ig.Emit (OpCodes.Ldind_U2);
1184                         else if (t == TypeManager.byte_type)
1185                                 ig.Emit (OpCodes.Ldind_U1);
1186                         else if (t == TypeManager.sbyte_type)
1187                                 ig.Emit (OpCodes.Ldind_I1);
1188                         else if (t == TypeManager.uint64_type)
1189                                 ig.Emit (OpCodes.Ldind_I8);
1190                         else if (t == TypeManager.int64_type)
1191                                 ig.Emit (OpCodes.Ldind_I8);
1192                         else if (t == TypeManager.float_type)
1193                                 ig.Emit (OpCodes.Ldind_R4);
1194                         else if (t == TypeManager.double_type)
1195                                 ig.Emit (OpCodes.Ldind_R8);
1196                         else if (t == TypeManager.bool_type)
1197                                 ig.Emit (OpCodes.Ldind_I1);
1198                         else if (t == TypeManager.intptr_type)
1199                                 ig.Emit (OpCodes.Ldind_I);
1200                         else if (TypeManager.IsEnumType (t)) {
1201                                 if (t == TypeManager.enum_type)
1202                                         ig.Emit (OpCodes.Ldind_Ref);
1203                                 else
1204                                         LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
1205                         } else if (t.IsValueType)
1206                                 ig.Emit (OpCodes.Ldobj, t);
1207                         else if (t.IsPointer)
1208                                 ig.Emit (OpCodes.Ldind_I);
1209                         else 
1210                                 ig.Emit (OpCodes.Ldind_Ref);
1211                 }
1212
1213                 //
1214                 // The stack contains the pointer and the value of type `type'
1215                 //
1216                 public static void StoreFromPtr (ILGenerator ig, Type type)
1217                 {
1218                         if (TypeManager.IsEnumType (type))
1219                                 type = TypeManager.EnumToUnderlying (type);
1220                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1221                                 ig.Emit (OpCodes.Stind_I4);
1222                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1223                                 ig.Emit (OpCodes.Stind_I8);
1224                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1225                                  type == TypeManager.ushort_type)
1226                                 ig.Emit (OpCodes.Stind_I2);
1227                         else if (type == TypeManager.float_type)
1228                                 ig.Emit (OpCodes.Stind_R4);
1229                         else if (type == TypeManager.double_type)
1230                                 ig.Emit (OpCodes.Stind_R8);
1231                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1232                                  type == TypeManager.bool_type)
1233                                 ig.Emit (OpCodes.Stind_I1);
1234                         else if (type == TypeManager.intptr_type)
1235                                 ig.Emit (OpCodes.Stind_I);
1236                         else if (type.IsValueType)
1237                                 ig.Emit (OpCodes.Stobj, type);
1238                         else
1239                                 ig.Emit (OpCodes.Stind_Ref);
1240                 }
1241                 
1242                 //
1243                 // Returns the size of type `t' if known, otherwise, 0
1244                 //
1245                 public static int GetTypeSize (Type t)
1246                 {
1247                         t = TypeManager.TypeToCoreType (t);
1248                         if (t == TypeManager.int32_type ||
1249                             t == TypeManager.uint32_type ||
1250                             t == TypeManager.float_type)
1251                                 return 4;
1252                         else if (t == TypeManager.int64_type ||
1253                                  t == TypeManager.uint64_type ||
1254                                  t == TypeManager.double_type)
1255                                 return 8;
1256                         else if (t == TypeManager.byte_type ||
1257                                  t == TypeManager.sbyte_type ||
1258                                  t == TypeManager.bool_type)    
1259                                 return 1;
1260                         else if (t == TypeManager.short_type ||
1261                                  t == TypeManager.char_type ||
1262                                  t == TypeManager.ushort_type)
1263                                 return 2;
1264                         else if (t == TypeManager.decimal_type)
1265                                 return 16;
1266                         else
1267                                 return 0;
1268                 }
1269
1270                 static void Error_NegativeArrayIndex (Location loc)
1271                 {
1272                         Report.Error (284, loc, "Can not create array with a negative size");
1273                 }
1274                 
1275                 //
1276                 // Converts `source' to an int, uint, long or ulong.
1277                 //
1278                 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
1279                 {
1280                         Expression target;
1281                         
1282                         bool old_checked = ec.CheckState;
1283                         ec.CheckState = true;
1284                         
1285                         target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
1286                         if (target == null){
1287                                 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
1288                                 if (target == null){
1289                                         target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
1290                                         if (target == null){
1291                                                 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
1292                                                 if (target == null)
1293                                                         Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
1294                                         }
1295                                 }
1296                         } 
1297                         ec.CheckState = old_checked;
1298
1299                         //
1300                         // Only positive constants are allowed at compile time
1301                         //
1302                         if (target is Constant){
1303                                 if (target is IntConstant){
1304                                         if (((IntConstant) target).Value < 0){
1305                                                 Error_NegativeArrayIndex (loc);
1306                                                 return null;
1307                                         }
1308                                 }
1309
1310                                 if (target is LongConstant){
1311                                         if (((LongConstant) target).Value < 0){
1312                                                 Error_NegativeArrayIndex (loc);
1313                                                 return null;
1314                                         }
1315                                 }
1316                                 
1317                         }
1318
1319                         return target;
1320                 }
1321                 
1322         }
1323
1324         /// <summary>
1325         ///   This is just a base class for expressions that can
1326         ///   appear on statements (invocations, object creation,
1327         ///   assignments, post/pre increment and decrement).  The idea
1328         ///   being that they would support an extra Emition interface that
1329         ///   does not leave a result on the stack.
1330         /// </summary>
1331         public abstract class ExpressionStatement : Expression {
1332
1333                 public virtual ExpressionStatement ResolveStatement (EmitContext ec)
1334                 {
1335                         Expression e = Resolve (ec);
1336                         if (e == null)
1337                                 return null;
1338
1339                         ExpressionStatement es = e as ExpressionStatement;
1340                         if (es == null)
1341                                 Error (201, "Only assignment, call, increment, decrement and new object " +
1342                                        "expressions can be used as a statement");
1343
1344                         return es;
1345                 }
1346
1347                 /// <summary>
1348                 ///   Requests the expression to be emitted in a `statement'
1349                 ///   context.  This means that no new value is left on the
1350                 ///   stack after invoking this method (constrasted with
1351                 ///   Emit that will always leave a value on the stack).
1352                 /// </summary>
1353                 public abstract void EmitStatement (EmitContext ec);
1354         }
1355
1356         /// <summary>
1357         ///   This kind of cast is used to encapsulate the child
1358         ///   whose type is child.Type into an expression that is
1359         ///   reported to return "return_type".  This is used to encapsulate
1360         ///   expressions which have compatible types, but need to be dealt
1361         ///   at higher levels with.
1362         ///
1363         ///   For example, a "byte" expression could be encapsulated in one
1364         ///   of these as an "unsigned int".  The type for the expression
1365         ///   would be "unsigned int".
1366         ///
1367         /// </summary>
1368         public class EmptyCast : Expression {
1369                 protected Expression child;
1370                 
1371                 public Expression Child {
1372                         get {
1373                                 return child;
1374                         }
1375                 }               
1376
1377                 public EmptyCast (Expression child, Type return_type)
1378                 {
1379                         eclass = child.eclass;
1380                         type = return_type;
1381                         this.child = child;
1382                 }
1383
1384                 public override Expression DoResolve (EmitContext ec)
1385                 {
1386                         // This should never be invoked, we are born in fully
1387                         // initialized state.
1388
1389                         return this;
1390                 }
1391
1392                 public override void Emit (EmitContext ec)
1393                 {
1394                         child.Emit (ec);
1395                 }
1396         }
1397
1398         //
1399         // We need to special case this since an empty cast of
1400         // a NullLiteral is still a Constant
1401         //
1402         public class NullCast : Constant {
1403                 protected Expression child;
1404                                 
1405                 public NullCast (Expression child, Type return_type)
1406                 {
1407                         eclass = child.eclass;
1408                         type = return_type;
1409                         this.child = child;
1410                 }
1411
1412                 override public string AsString ()
1413                 {
1414                         return "null";
1415                 }
1416
1417                 public override object GetValue ()
1418                 {
1419                         return null;
1420                 }
1421
1422                 public override Expression DoResolve (EmitContext ec)
1423                 {
1424                         // This should never be invoked, we are born in fully
1425                         // initialized state.
1426
1427                         return this;
1428                 }
1429
1430                 public override void Emit (EmitContext ec)
1431                 {
1432                         child.Emit (ec);
1433                 }
1434         }
1435
1436
1437         /// <summary>
1438         ///  This class is used to wrap literals which belong inside Enums
1439         /// </summary>
1440         public class EnumConstant : Constant {
1441                 public Constant Child;
1442
1443                 public EnumConstant (Constant child, Type enum_type)
1444                 {
1445                         eclass = child.eclass;
1446                         this.Child = child;
1447                         type = enum_type;
1448                 }
1449                 
1450                 public override Expression DoResolve (EmitContext ec)
1451                 {
1452                         // This should never be invoked, we are born in fully
1453                         // initialized state.
1454
1455                         return this;
1456                 }
1457
1458                 public override void Emit (EmitContext ec)
1459                 {
1460                         Child.Emit (ec);
1461                 }
1462
1463                 public override object GetValue ()
1464                 {
1465                         return Child.GetValue ();
1466                 }
1467
1468                 //
1469                 // Converts from one of the valid underlying types for an enumeration
1470                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1471                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1472                 //
1473                 public Constant WidenToCompilerConstant ()
1474                 {
1475                         Type t = TypeManager.EnumToUnderlying (Child.Type);
1476                         object v = ((Constant) Child).GetValue ();;
1477                         
1478                         if (t == TypeManager.int32_type)
1479                                 return new IntConstant ((int) v);
1480                         if (t == TypeManager.uint32_type)
1481                                 return new UIntConstant ((uint) v);
1482                         if (t == TypeManager.int64_type)
1483                                 return new LongConstant ((long) v);
1484                         if (t == TypeManager.uint64_type)
1485                                 return new ULongConstant ((ulong) v);
1486                         if (t == TypeManager.short_type)
1487                                 return new ShortConstant ((short) v);
1488                         if (t == TypeManager.ushort_type)
1489                                 return new UShortConstant ((ushort) v);
1490                         if (t == TypeManager.byte_type)
1491                                 return new ByteConstant ((byte) v);
1492                         if (t == TypeManager.sbyte_type)
1493                                 return new SByteConstant ((sbyte) v);
1494
1495                         throw new Exception ("Invalid enumeration underlying type: " + t);
1496                 }
1497
1498                 //
1499                 // Extracts the value in the enumeration on its native representation
1500                 //
1501                 public object GetPlainValue ()
1502                 {
1503                         Type t = TypeManager.EnumToUnderlying (Child.Type);
1504                         object v = ((Constant) Child).GetValue ();;
1505                         
1506                         if (t == TypeManager.int32_type)
1507                                 return (int) v;
1508                         if (t == TypeManager.uint32_type)
1509                                 return (uint) v;
1510                         if (t == TypeManager.int64_type)
1511                                 return (long) v;
1512                         if (t == TypeManager.uint64_type)
1513                                 return (ulong) v;
1514                         if (t == TypeManager.short_type)
1515                                 return (short) v;
1516                         if (t == TypeManager.ushort_type)
1517                                 return (ushort) v;
1518                         if (t == TypeManager.byte_type)
1519                                 return (byte) v;
1520                         if (t == TypeManager.sbyte_type)
1521                                 return (sbyte) v;
1522
1523                         return null;
1524                 }
1525                 
1526                 public override string AsString ()
1527                 {
1528                         return Child.AsString ();
1529                 }
1530
1531                 public override DoubleConstant ConvertToDouble ()
1532                 {
1533                         return Child.ConvertToDouble ();
1534                 }
1535
1536                 public override FloatConstant ConvertToFloat ()
1537                 {
1538                         return Child.ConvertToFloat ();
1539                 }
1540
1541                 public override ULongConstant ConvertToULong ()
1542                 {
1543                         return Child.ConvertToULong ();
1544                 }
1545
1546                 public override LongConstant ConvertToLong ()
1547                 {
1548                         return Child.ConvertToLong ();
1549                 }
1550
1551                 public override UIntConstant ConvertToUInt ()
1552                 {
1553                         return Child.ConvertToUInt ();
1554                 }
1555
1556                 public override IntConstant ConvertToInt ()
1557                 {
1558                         return Child.ConvertToInt ();
1559                 }
1560                 
1561                 public override bool IsZeroInteger {
1562                         get { return Child.IsZeroInteger; }
1563                 }
1564         }
1565
1566         /// <summary>
1567         ///   This kind of cast is used to encapsulate Value Types in objects.
1568         ///
1569         ///   The effect of it is to box the value type emitted by the previous
1570         ///   operation.
1571         /// </summary>
1572         public class BoxedCast : EmptyCast {
1573
1574                 public BoxedCast (Expression expr)
1575                         : base (expr, TypeManager.object_type) 
1576                 {
1577                         eclass = ExprClass.Value;
1578                 }
1579
1580                 public BoxedCast (Expression expr, Type target_type)
1581                         : base (expr, target_type)
1582                 {
1583                         eclass = ExprClass.Value;
1584                 }
1585                 
1586                 public override Expression DoResolve (EmitContext ec)
1587                 {
1588                         // This should never be invoked, we are born in fully
1589                         // initialized state.
1590
1591                         return this;
1592                 }
1593
1594                 public override void Emit (EmitContext ec)
1595                 {
1596                         base.Emit (ec);
1597                         
1598                         ec.ig.Emit (OpCodes.Box, child.Type);
1599                 }
1600         }
1601
1602         public class UnboxCast : EmptyCast {
1603                 public UnboxCast (Expression expr, Type return_type)
1604                         : base (expr, return_type)
1605                 {
1606                 }
1607
1608                 public override Expression DoResolve (EmitContext ec)
1609                 {
1610                         // This should never be invoked, we are born in fully
1611                         // initialized state.
1612
1613                         return this;
1614                 }
1615
1616                 public override void Emit (EmitContext ec)
1617                 {
1618                         Type t = type;
1619                         ILGenerator ig = ec.ig;
1620                         
1621                         base.Emit (ec);
1622                         if (t.IsGenericParameter)
1623                                 ig.Emit (OpCodes.Unbox_Any, t);
1624                         else {
1625                                 ig.Emit (OpCodes.Unbox, t);
1626
1627                                 LoadFromPtr (ig, t);
1628                         }
1629                 }
1630         }
1631         
1632         /// <summary>
1633         ///   This is used to perform explicit numeric conversions.
1634         ///
1635         ///   Explicit numeric conversions might trigger exceptions in a checked
1636         ///   context, so they should generate the conv.ovf opcodes instead of
1637         ///   conv opcodes.
1638         /// </summary>
1639         public class ConvCast : EmptyCast {
1640                 public enum Mode : byte {
1641                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1642                         U1_I1, U1_CH,
1643                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1644                         U2_I1, U2_U1, U2_I2, U2_CH,
1645                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1646                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1647                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1648                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1649                         CH_I1, CH_U1, CH_I2,
1650                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1651                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1652                 }
1653
1654                 Mode mode;
1655                 bool checked_state;
1656                 
1657                 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
1658                         : base (child, return_type)
1659                 {
1660                         checked_state = ec.CheckState;
1661                         mode = m;
1662                 }
1663
1664                 public override Expression DoResolve (EmitContext ec)
1665                 {
1666                         // This should never be invoked, we are born in fully
1667                         // initialized state.
1668
1669                         return this;
1670                 }
1671
1672                 public override string ToString ()
1673                 {
1674                         return String.Format ("ConvCast ({0}, {1})", mode, child);
1675                 }
1676                 
1677                 public override void Emit (EmitContext ec)
1678                 {
1679                         ILGenerator ig = ec.ig;
1680                         
1681                         base.Emit (ec);
1682
1683                         if (checked_state){
1684                                 switch (mode){
1685                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1686                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1687                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1688                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1689                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1690
1691                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1692                                 case Mode.U1_CH: /* nothing */ break;
1693
1694                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1695                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1696                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1697                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1698                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1699                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1700
1701                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1702                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1703                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1704                                 case Mode.U2_CH: /* nothing */ break;
1705
1706                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1707                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1708                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1709                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1710                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1711                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1712                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1713
1714                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1715                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1716                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1717                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1718                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1719                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1720
1721                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1722                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1723                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1724                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1725                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1726                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1727                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1728                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1729
1730                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1731                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1732                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1733                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1734                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1735                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1736                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1737                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1738
1739                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1740                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1741                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1742
1743                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1744                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1745                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1746                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1747                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1748                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1749                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1750                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1751                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1752
1753                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1754                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1755                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1756                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1757                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1758                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1759                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1760                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1761                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1762                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1763                                 }
1764                         } else {
1765                                 switch (mode){
1766                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1767                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1768                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1769                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1770                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1771
1772                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1773                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1774
1775                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1776                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1777                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1778                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1779                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1780                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1781
1782                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1783                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1784                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1785                                 case Mode.U2_CH: /* nothing */ break;
1786
1787                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1788                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1789                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1790                                 case Mode.I4_U4: /* nothing */ break;
1791                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1792                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1793                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1794
1795                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1796                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1797                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1798                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1799                                 case Mode.U4_I4: /* nothing */ break;
1800                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1801
1802                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1803                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1804                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1805                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1806                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1807                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1808                                 case Mode.I8_U8: /* nothing */ break;
1809                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1810
1811                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1812                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1813                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1814                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1815                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1816                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1817                                 case Mode.U8_I8: /* nothing */ break;
1818                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1819
1820                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1821                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1822                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1823
1824                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1825                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1826                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1827                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1828                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1829                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1830                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1831                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1832                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1833
1834                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1835                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1836                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1837                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1838                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1839                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1840                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1841                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1842                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1843                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1844                                 }
1845                         }
1846                 }
1847         }
1848         
1849         public class OpcodeCast : EmptyCast {
1850                 OpCode op, op2;
1851                 bool second_valid;
1852                 
1853                 public OpcodeCast (Expression child, Type return_type, OpCode op)
1854                         : base (child, return_type)
1855                         
1856                 {
1857                         this.op = op;
1858                         second_valid = false;
1859                 }
1860
1861                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1862                         : base (child, return_type)
1863                         
1864                 {
1865                         this.op = op;
1866                         this.op2 = op2;
1867                         second_valid = true;
1868                 }
1869
1870                 public override Expression DoResolve (EmitContext ec)
1871                 {
1872                         // This should never be invoked, we are born in fully
1873                         // initialized state.
1874
1875                         return this;
1876                 }
1877
1878                 public override void Emit (EmitContext ec)
1879                 {
1880                         base.Emit (ec);
1881                         ec.ig.Emit (op);
1882
1883                         if (second_valid)
1884                                 ec.ig.Emit (op2);
1885                 }                       
1886         }
1887
1888         /// <summary>
1889         ///   This kind of cast is used to encapsulate a child and cast it
1890         ///   to the class requested
1891         /// </summary>
1892         public class ClassCast : EmptyCast {
1893                 public ClassCast (Expression child, Type return_type)
1894                         : base (child, return_type)
1895                         
1896                 {
1897                 }
1898
1899                 public override Expression DoResolve (EmitContext ec)
1900                 {
1901                         // This should never be invoked, we are born in fully
1902                         // initialized state.
1903
1904                         return this;
1905                 }
1906
1907                 public override void Emit (EmitContext ec)
1908                 {
1909                         base.Emit (ec);
1910
1911                         if (child.Type.IsGenericParameter)
1912                                 ec.ig.Emit (OpCodes.Box, child.Type);
1913
1914                         if (type.IsGenericParameter)
1915                                 ec.ig.Emit (OpCodes.Unbox_Any, type);
1916                         else
1917                                 ec.ig.Emit (OpCodes.Castclass, type);
1918                 }
1919         }
1920         
1921         /// <summary>
1922         ///   SimpleName expressions are initially formed of a single
1923         ///   word and it only happens at the beginning of the expression.
1924         /// </summary>
1925         ///
1926         /// <remarks>
1927         ///   The expression will try to be bound to a Field, a Method
1928         ///   group or a Property.  If those fail we pass the name to our
1929         ///   caller and the SimpleName is compounded to perform a type
1930         ///   lookup.  The idea behind this process is that we want to avoid
1931         ///   creating a namespace map from the assemblies, as that requires
1932         ///   the GetExportedTypes function to be called and a hashtable to
1933         ///   be constructed which reduces startup time.  If later we find
1934         ///   that this is slower, we should create a `NamespaceExpr' expression
1935         ///   that fully participates in the resolution process. 
1936         ///   
1937         ///   For example `System.Console.WriteLine' is decomposed into
1938         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1939         ///   
1940         ///   The first SimpleName wont produce a match on its own, so it will
1941         ///   be turned into:
1942         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
1943         ///   
1944         ///   System.Console will produce a TypeExpr match.
1945         ///   
1946         ///   The downside of this is that we might be hitting `LookupType' too many
1947         ///   times with this scheme.
1948         /// </remarks>
1949         public class SimpleName : Expression {
1950                 public string Name;
1951                 public readonly TypeArguments Arguments;
1952
1953                 //
1954                 // If true, then we are a simple name, not composed with a ".
1955                 //
1956                 bool is_base;
1957
1958                 public SimpleName (string a, string b, Location l)
1959                 {
1960                         Name = String.Concat (a, ".", b);
1961                         loc = l;
1962                         is_base = false;
1963                 }
1964                 
1965                 public SimpleName (string name, Location l)
1966                 {
1967                         Name = name;
1968                         loc = l;
1969                         is_base = true;
1970                 }
1971
1972                 public SimpleName (string name, TypeArguments args, Location l)
1973                 {
1974                         Name = name;
1975                         Arguments = args;
1976                         loc = l;
1977                         is_base = true;
1978                 }
1979
1980                 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
1981                 {
1982                         if (ec.IsFieldInitializer)
1983                                 Report.Error (
1984                                         236, l,
1985                                         "A field initializer cannot reference the non-static field, " +
1986                                         "method or property `"+name+"'");
1987                         else
1988                                 Report.Error (
1989                                         120, l,
1990                                         "An object reference is required " +
1991                                         "for the non-static field `"+name+"'");
1992                 }
1993                 
1994                 //
1995                 // Checks whether we are trying to access an instance
1996                 // property, method or field from a static body.
1997                 //
1998                 Expression MemberStaticCheck (EmitContext ec, Expression e)
1999                 {
2000                         if (e is IMemberExpr){
2001                                 IMemberExpr member = (IMemberExpr) e;
2002                                 
2003                                 if (!member.IsStatic){
2004                                         Error_ObjectRefRequired (ec, loc, Name);
2005                                         return null;
2006                                 }
2007                         }
2008
2009                         return e;
2010                 }
2011                 
2012                 public override Expression DoResolve (EmitContext ec)
2013                 {
2014                         return SimpleNameResolve (ec, null, false, false);
2015                 }
2016
2017                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
2018                 {
2019                         return SimpleNameResolve (ec, right_side, false, false);
2020                 }
2021                 
2022
2023                 public Expression DoResolveAllowStatic (EmitContext ec, bool intermediate)
2024                 {
2025                         return SimpleNameResolve (ec, null, true, intermediate);
2026                 }
2027
2028                 public override Expression ResolveAsTypeStep (EmitContext ec)
2029                 {
2030                         DeclSpace ds = ec.DeclSpace;
2031                         NamespaceEntry ns = ds.NamespaceEntry;
2032                         TypeExpr t;
2033                         IAlias alias_value;
2034
2035                         //
2036                         // Since we are cheating: we only do the Alias lookup for
2037                         // namespaces if the name does not include any dots in it
2038                         //
2039                         if (ns != null && is_base)
2040                                 alias_value = ns.LookupAlias (Name);
2041                         else
2042                                 alias_value = null;
2043
2044                         TypeParameterExpr generic_type = ds.LookupGeneric (Name, loc);
2045                         if (generic_type != null)
2046                                 return generic_type.ResolveAsTypeTerminal (ec);
2047
2048                         if (ec.ResolvingTypeTree){
2049                                 int errors = Report.Errors;
2050                                 Type dt = ds.FindType (loc, Name);
2051                                 
2052                                 if (Report.Errors != errors)
2053                                         return null;
2054                                 
2055                                 if (dt != null)
2056                                         return new TypeExpression (dt, loc);
2057
2058                                 if (alias_value != null){
2059                                         if (alias_value.IsType)
2060                                                 return alias_value.Type;
2061                                         if ((t = RootContext.LookupType (ds, alias_value.Name, true, loc)) != null)
2062                                                 return t;
2063                                 }
2064                         }
2065
2066                         //
2067                         // First, the using aliases
2068                         //
2069                         if (alias_value != null){
2070                                 if (alias_value.IsType)
2071                                         return alias_value.Type;
2072                                 if ((t = RootContext.LookupType (ds, alias_value.Name, true, loc)) != null)
2073                                         return t;
2074                                 
2075                                 // we have alias value, but it isn't Type, so try if it's namespace
2076                                 return new SimpleName (alias_value.Name, loc);
2077                         }
2078
2079                         //
2080                         // Stage 2: Lookup up if we are an alias to a type
2081                         // or a namespace.
2082                         //
2083
2084                         if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
2085                                 return t;
2086                                 
2087                         // No match, maybe our parent can compose us
2088                         // into something meaningful.
2089                         return this;
2090                 }
2091
2092                 Expression SimpleNameResolve (EmitContext ec, Expression right_side,
2093                                               bool allow_static, bool intermediate)
2094                 {
2095                         Expression e = DoSimpleNameResolve (ec, right_side, allow_static, intermediate);
2096                         if (e == null)
2097                                 return null;
2098
2099                         Block current_block = ec.CurrentBlock;
2100                         if (current_block != null){
2101                                 LocalInfo vi = current_block.GetLocalInfo (Name);
2102                                 if (is_base &&
2103                                     current_block.IsVariableNameUsedInChildBlock(Name)) {
2104                                         Report.Error (135, Location,
2105                                                       "'{0}' has a different meaning in a " +
2106                                                       "child block", Name);
2107                                         return null;
2108                                 }
2109                         }
2110
2111                         if (e.Type != null && e.Type.IsPointer && !ec.InUnsafe) {
2112                                 UnsafeError (loc);
2113                                 return null;
2114                         }
2115
2116                         return e;
2117                 }
2118
2119                 /// <remarks>
2120                 ///   7.5.2: Simple Names. 
2121                 ///
2122                 ///   Local Variables and Parameters are handled at
2123                 ///   parse time, so they never occur as SimpleNames.
2124                 ///
2125                 ///   The `allow_static' flag is used by MemberAccess only
2126                 ///   and it is used to inform us that it is ok for us to 
2127                 ///   avoid the static check, because MemberAccess might end
2128                 ///   up resolving the Name as a Type name and the access as
2129                 ///   a static type access.
2130                 ///
2131                 ///   ie: Type Type; .... { Type.GetType (""); }
2132                 ///
2133                 ///   Type is both an instance variable and a Type;  Type.GetType
2134                 ///   is the static method not an instance method of type.
2135                 /// </remarks>
2136                 Expression DoSimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static, bool intermediate)
2137                 {
2138                         Expression e = null;
2139
2140                         //
2141                         // Stage 1: Performed by the parser (binding to locals or parameters).
2142                         //
2143                         Block current_block = ec.CurrentBlock;
2144                         if (current_block != null){
2145                                 LocalInfo vi = current_block.GetLocalInfo (Name);
2146                                 if (vi != null){
2147                                         Expression var;
2148                                         
2149                                         var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2150                                         
2151                                         if (right_side != null)
2152                                                 return var.ResolveLValue (ec, right_side);
2153                                         else
2154                                                 return var.Resolve (ec);
2155                                 }
2156
2157                                 int idx = -1;
2158                                 Parameter par = null;
2159                                 Parameters pars = current_block.Parameters;
2160                                 if (pars != null)
2161                                         par = pars.GetParameterByName (Name, out idx);
2162
2163                                 if (par != null) {
2164                                         ParameterReference param;
2165                                         
2166                                         param = new ParameterReference (pars, current_block, idx, Name, loc);
2167
2168                                         if (right_side != null)
2169                                                 return param.ResolveLValue (ec, right_side);
2170                                         else
2171                                                 return param.Resolve (ec);
2172                                 }
2173                         }
2174                         
2175                         //
2176                         // Stage 2: Lookup members 
2177                         //
2178
2179                         DeclSpace lookup_ds = ec.DeclSpace;
2180                         do {
2181                                 if (lookup_ds.TypeBuilder == null)
2182                                         break;
2183
2184                                 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
2185                                 if (e != null)
2186                                         break;
2187
2188                                 lookup_ds =lookup_ds.Parent;
2189                         } while (lookup_ds != null);
2190
2191                         if (e == null && ec.ContainerType != null)
2192                                 e = MemberLookup (ec, ec.ContainerType, Name, loc);
2193
2194                         if (e == null) {
2195                                 //
2196                                 // Since we are cheating (is_base is our hint
2197                                 // that we are the beginning of the name): we
2198                                 // only do the Alias lookup for namespaces if
2199                                 // the name does not include any dots in it
2200                                 //
2201                                 NamespaceEntry ns = ec.DeclSpace.NamespaceEntry;
2202                                 if (is_base && ns != null){
2203                                         IAlias alias_value = ns.LookupAlias (Name);
2204                                         if (alias_value != null){
2205                                                 if (alias_value.IsType)
2206                                                         return alias_value.Type;
2207
2208                                                 Name = alias_value.Name;
2209                                                 Type t;
2210
2211                                                 if ((t = TypeManager.LookupType (Name)) != null)
2212                                                         return new TypeExpression (t, loc);
2213                                         
2214                                                 // No match, maybe our parent can compose us
2215                                                 // into something meaningful.
2216                                                 return this;
2217                                         }
2218                                 }
2219
2220                                 return ResolveAsTypeStep (ec);
2221                         }
2222
2223                         if (e is TypeExpr)
2224                                 return e;
2225
2226                         if (e is IMemberExpr) {
2227                                 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
2228                                 if (e == null)
2229                                         return null;
2230
2231                                 IMemberExpr me = e as IMemberExpr;
2232                                 if (me == null)
2233                                         return e;
2234
2235                                 if (Arguments != null) {
2236                                         MethodGroupExpr mg = me as MethodGroupExpr;
2237                                         if (mg == null)
2238                                                 return null;
2239
2240                                         return mg.ResolveGeneric (ec, Arguments);
2241                                 }
2242
2243                                 // This fails if ResolveMemberAccess() was unable to decide whether
2244                                 // it's a field or a type of the same name.
2245                                 
2246                                 if (!me.IsStatic && (me.InstanceExpression == null))
2247                                         return e;
2248
2249                                 if (!me.IsStatic &&
2250                                     TypeManager.IsSubclassOrNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
2251                                     me.InstanceExpression.Type != me.DeclaringType &&
2252                                     !TypeManager.IsSubclassOf (me.InstanceExpression.Type, me.DeclaringType) &&
2253                                     (!intermediate || !MemberAccess.IdenticalNameAndTypeName (ec, this, e, loc))) {
2254                                         Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
2255                                                "outer type `" + me.DeclaringType + "' via nested type `" +
2256                                                me.InstanceExpression.Type + "'");
2257                                         return null;
2258                                 }
2259
2260                                 return (right_side != null)
2261                                         ? e.DoResolveLValue (ec, right_side)
2262                                         : e.DoResolve (ec);
2263                         }
2264
2265                         if (ec.IsStatic || ec.IsFieldInitializer){
2266                                 if (allow_static)
2267                                         return e;
2268
2269                                 return MemberStaticCheck (ec, e);
2270                         } else
2271                                 return e;
2272                 }
2273                 
2274                 public override void Emit (EmitContext ec)
2275                 {
2276                         //
2277                         // If this is ever reached, then we failed to
2278                         // find the name as a namespace
2279                         //
2280
2281                         Error (103, "The name `" + Name +
2282                                "' does not exist in the class `" +
2283                                ec.DeclSpace.Name + "'");
2284                 }
2285
2286                 public override string ToString ()
2287                 {
2288                         return Name;
2289                 }
2290         }
2291         
2292         /// <summary>
2293         ///   Fully resolved expression that evaluates to a type
2294         /// </summary>
2295         public abstract class TypeExpr : Expression, IAlias {
2296                 override public Expression ResolveAsTypeStep (EmitContext ec)
2297                 {
2298                         TypeExpr t = DoResolveAsTypeStep (ec);
2299                         if (t == null)
2300                                 return null;
2301
2302                         eclass = ExprClass.Type;
2303                         return t;
2304                 }
2305
2306                 override public Expression DoResolve (EmitContext ec)
2307                 {
2308                         return ResolveAsTypeTerminal (ec);
2309                 }
2310
2311                 override public void Emit (EmitContext ec)
2312                 {
2313                         throw new Exception ("Should never be called");
2314                 }
2315
2316                 public virtual bool CheckAccessLevel (DeclSpace ds)
2317                 {
2318                         return ds.CheckAccessLevel (Type);
2319                 }
2320
2321                 public virtual bool AsAccessible (DeclSpace ds, int flags)
2322                 {
2323                         return ds.AsAccessible (Type, flags);
2324                 }
2325
2326                 public virtual bool IsClass {
2327                         get { return Type.IsClass; }
2328                 }
2329
2330                 public virtual bool IsValueType {
2331                         get { return Type.IsValueType; }
2332                 }
2333
2334                 public virtual bool IsInterface {
2335                         get { return Type.IsInterface; }
2336                 }
2337
2338                 public virtual bool IsSealed {
2339                         get { return Type.IsSealed; }
2340                 }
2341
2342                 public virtual bool CanInheritFrom ()
2343                 {
2344                         if (Type == TypeManager.enum_type ||
2345                             (Type == TypeManager.value_type && RootContext.StdLib) ||
2346                             Type == TypeManager.multicast_delegate_type ||
2347                             Type == TypeManager.delegate_type ||
2348                             Type == TypeManager.array_type)
2349                                 return false;
2350
2351                         return true;
2352                 }
2353
2354                 public virtual bool IsAttribute {
2355                         get {
2356                                 return Type == TypeManager.attribute_type ||
2357                                         Type.IsSubclassOf (TypeManager.attribute_type);
2358                         }
2359                 }
2360
2361                 public abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
2362
2363                 public virtual Type ResolveType (EmitContext ec)
2364                 {
2365                         TypeExpr t = ResolveAsTypeTerminal (ec);
2366                         if (t == null)
2367                                 return null;
2368
2369                         return t.Type;
2370                 }
2371
2372                 public abstract string Name {
2373                         get;
2374                 }
2375
2376                 public override bool Equals (object obj)
2377                 {
2378                         TypeExpr tobj = obj as TypeExpr;
2379                         if (tobj == null)
2380                                 return false;
2381
2382                         return Type == tobj.Type;
2383                 }
2384
2385                 public override int GetHashCode ()
2386                 {
2387                         return Type.GetHashCode ();
2388                 }
2389                 
2390                 public override string ToString ()
2391                 {
2392                         return Name;
2393                 }
2394
2395                 bool IAlias.IsType {
2396                         get { return true; }
2397                 }
2398
2399                 TypeExpr IAlias.Type {
2400                         get {
2401                                 return this;
2402                         }
2403                 }
2404         }
2405
2406         public class TypeExpression : TypeExpr, IAlias {
2407                 public TypeExpression (Type t, Location l)
2408                 {
2409                         Type = t;
2410                         eclass = ExprClass.Type;
2411                         loc = l;
2412                 }
2413
2414                 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2415                 {
2416                         return this;
2417                 }
2418
2419                 public override string Name {
2420                         get {
2421                                 return Type.ToString ();
2422                         }
2423                 }
2424
2425                 string IAlias.Name {
2426                         get {
2427                                 return Type.FullName != null ? Type.FullName : Type.Name;
2428                         }
2429                 }
2430         }
2431
2432         /// <summary>
2433         ///   Used to create types from a fully qualified name.  These are just used
2434         ///   by the parser to setup the core types.  A TypeLookupExpression is always
2435         ///   classified as a type.
2436         /// </summary>
2437         public class TypeLookupExpression : TypeExpr {
2438                 string name;
2439                 
2440                 public TypeLookupExpression (string name)
2441                 {
2442                         this.name = name;
2443                 }
2444
2445                 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2446                 {
2447                         if (type == null) {
2448                                 TypeExpr texpr = RootContext.LookupType (
2449                                         ec.DeclSpace, name, false, Location.Null);
2450                                 if (texpr == null)
2451                                         return null;
2452
2453                                 type = texpr.ResolveType (ec);
2454                                 if (type == null)
2455                                         return null;
2456                         }
2457
2458                         return this;
2459                 }
2460
2461                 public override string Name {
2462                         get {
2463                                 return name;
2464                         }
2465                 }
2466         }
2467
2468         /// <summary>
2469         ///   Represents an "unbound generic type", ie. typeof (Foo<>).
2470         ///   See 14.5.11.
2471         /// </summary>
2472         public class UnboundTypeExpression : TypeLookupExpression {
2473                 public UnboundTypeExpression (string name)
2474                         : base (name)
2475                 { }
2476         }
2477
2478         public class TypeAliasExpression : TypeExpr, IAlias {
2479                 TypeExpr texpr;
2480                 TypeArguments args;
2481                 string name;
2482
2483                 public TypeAliasExpression (TypeExpr texpr, TypeArguments args, Location l)
2484                 {
2485                         this.texpr = texpr;
2486                         this.args = args;
2487                         loc = texpr.Location;
2488
2489                         eclass = ExprClass.Type;
2490                         if (args != null)
2491                                 name = texpr.Name + "<" + args.ToString () + ">";
2492                         else
2493                                 name = texpr.Name;
2494                 }
2495
2496                 public override string Name {
2497                         get { return name; }
2498                 }
2499
2500                 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2501                 {
2502                         Type type = texpr.ResolveType (ec);
2503                         if (type == null)
2504                                 return null;
2505
2506                         int num_args = TypeManager.GetNumberOfTypeArguments (type);
2507
2508                         if (args != null) {
2509                                 if (num_args == 0) {
2510                                         Report.Error (308, loc,
2511                                                       "The non-generic type `{0}' cannot " +
2512                                                       "be used with type arguments.",
2513                                                       TypeManager.CSharpName (type));
2514                                         return null;
2515                                 }
2516
2517                                 ConstructedType ctype = new ConstructedType (type, args, loc);
2518                                 return ctype.ResolveAsTypeTerminal (ec);
2519                         } else if (num_args > 0) {
2520                                 Report.Error (305, loc,
2521                                               "Using the generic type `{0}' " +
2522                                               "requires {1} type arguments",
2523                                               TypeManager.GetFullName (type), num_args);
2524                                 return null;
2525                         }
2526
2527                         return new TypeExpression (type, loc);
2528                 }
2529
2530                 public override Type ResolveType (EmitContext ec)
2531                 {
2532                         TypeExpr t = ResolveAsTypeTerminal (ec);
2533                         if (t == null)
2534                                 return null;
2535
2536                         type = t.ResolveType (ec);
2537                         return type;
2538                 }
2539
2540                 public override bool CheckAccessLevel (DeclSpace ds)
2541                 {
2542                         return texpr.CheckAccessLevel (ds);
2543                 }
2544
2545                 public override bool AsAccessible (DeclSpace ds, int flags)
2546                 {
2547                         return texpr.AsAccessible (ds, flags);
2548                 }
2549
2550                 public override bool IsClass {
2551                         get { return texpr.IsClass; }
2552                 }
2553
2554                 public override bool IsValueType {
2555                         get { return texpr.IsValueType; }
2556                 }
2557
2558                 public override bool IsInterface {
2559                         get { return texpr.IsInterface; }
2560                 }
2561
2562                 public override bool IsSealed {
2563                         get { return texpr.IsSealed; }
2564                 }
2565
2566                 public override bool IsAttribute {
2567                         get { return texpr.IsAttribute; }
2568                 }
2569         }
2570
2571         /// <summary>
2572         ///   MethodGroup Expression.
2573         ///  
2574         ///   This is a fully resolved expression that evaluates to a type
2575         /// </summary>
2576         public class MethodGroupExpr : Expression, IMemberExpr {
2577                 public MethodBase [] Methods;
2578                 Expression instance_expression = null;
2579                 bool is_explicit_impl = false;
2580                 bool has_type_arguments = false;
2581                 bool identical_type_name = false;
2582                 bool is_base;
2583                 
2584                 public MethodGroupExpr (MemberInfo [] mi, Location l)
2585                 {
2586                         Methods = new MethodBase [mi.Length];
2587                         mi.CopyTo (Methods, 0);
2588                         eclass = ExprClass.MethodGroup;
2589                         type = TypeManager.object_type;
2590                         loc = l;
2591                 }
2592
2593                 public MethodGroupExpr (ArrayList list, Location l)
2594                 {
2595                         Methods = new MethodBase [list.Count];
2596
2597                         try {
2598                                 list.CopyTo (Methods, 0);
2599                         } catch {
2600                                 foreach (MemberInfo m in list){
2601                                         if (!(m is MethodBase)){
2602                                                 Console.WriteLine ("Name " + m.Name);
2603                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2604                                         }
2605                                 }
2606                                 throw;
2607                         }
2608
2609                         loc = l;
2610                         eclass = ExprClass.MethodGroup;
2611                         type = TypeManager.object_type;
2612                 }
2613
2614                 public Type DeclaringType {
2615                         get {
2616                                 //
2617                                 // We assume that the top-level type is in the end
2618                                 //
2619                                 return Methods [Methods.Length - 1].DeclaringType;
2620                                 //return Methods [0].DeclaringType;
2621                         }
2622                 }
2623                 
2624                 //
2625                 // `A method group may have associated an instance expression' 
2626                 // 
2627                 public Expression InstanceExpression {
2628                         get {
2629                                 return instance_expression;
2630                         }
2631
2632                         set {
2633                                 instance_expression = value;
2634                         }
2635                 }
2636
2637                 public bool IsExplicitImpl {
2638                         get {
2639                                 return is_explicit_impl;
2640                         }
2641
2642                         set {
2643                                 is_explicit_impl = value;
2644                         }
2645                 }
2646
2647                 public bool HasTypeArguments {
2648                         get {
2649                                 return has_type_arguments;
2650                         }
2651
2652                         set {
2653                                 has_type_arguments = value;
2654                         }
2655                 }
2656
2657                 public bool IdenticalTypeName {
2658                         get {
2659                                 return identical_type_name;
2660                         }
2661
2662                         set {
2663                                 identical_type_name = value;
2664                         }
2665                 }
2666
2667                 public bool IsBase {
2668                         get {
2669                                 return is_base;
2670                         }
2671                         set {
2672                                 is_base = value;
2673                         }
2674                 }
2675
2676                 public string Name {
2677                         get {
2678                                 //return Methods [0].Name;
2679                                 return Methods [Methods.Length - 1].Name;
2680                         }
2681                 }
2682
2683                 public bool IsInstance {
2684                         get {
2685                                 foreach (MethodBase mb in Methods)
2686                                         if (!mb.IsStatic)
2687                                                 return true;
2688
2689                                 return false;
2690                         }
2691                 }
2692
2693                 public bool IsStatic {
2694                         get {
2695                                 foreach (MethodBase mb in Methods)
2696                                         if (mb.IsStatic)
2697                                                 return true;
2698
2699                                 return false;
2700                         }
2701                 }
2702                 
2703                 override public Expression DoResolve (EmitContext ec)
2704                 {
2705                         if (!IsInstance)
2706                                 instance_expression = null;
2707
2708                         if (instance_expression != null) {
2709                                 instance_expression = instance_expression.DoResolve (ec);
2710                                 if (instance_expression == null)
2711                                         return null;
2712                         }
2713
2714                         return this;
2715                 }
2716
2717                 public void ReportUsageError ()
2718                 {
2719                         Report.Error (654, loc, "Method `" + DeclaringType + "." +
2720                                       Name + "()' is referenced without parentheses");
2721                 }
2722
2723                 override public void Emit (EmitContext ec)
2724                 {
2725                         ReportUsageError ();
2726                 }
2727
2728                 bool RemoveMethods (bool keep_static)
2729                 {
2730                         ArrayList smethods = new ArrayList ();
2731
2732                         foreach (MethodBase mb in Methods){
2733                                 if (mb.IsStatic == keep_static)
2734                                         smethods.Add (mb);
2735                         }
2736
2737                         if (smethods.Count == 0)
2738                                 return false;
2739
2740                         Methods = new MethodBase [smethods.Count];
2741                         smethods.CopyTo (Methods, 0);
2742
2743                         return true;
2744                 }
2745                 
2746                 /// <summary>
2747                 ///   Removes any instance methods from the MethodGroup, returns
2748                 ///   false if the resulting set is empty.
2749                 /// </summary>
2750                 public bool RemoveInstanceMethods ()
2751                 {
2752                         return RemoveMethods (true);
2753                 }
2754
2755                 /// <summary>
2756                 ///   Removes any static methods from the MethodGroup, returns
2757                 ///   false if the resulting set is empty.
2758                 /// </summary>
2759                 public bool RemoveStaticMethods ()
2760                 {
2761                         return RemoveMethods (false);
2762                 }
2763
2764                 public Expression ResolveGeneric (EmitContext ec, TypeArguments args)
2765                 {
2766                         if (args.Resolve (ec) == false)
2767                                 return null;
2768
2769                         Type[] atypes = args.Arguments;
2770
2771                         int first_count = 0;
2772                         MethodInfo first = null;
2773
2774                         ArrayList list = new ArrayList ();
2775                         foreach (MethodBase mb in Methods) {
2776                                 MethodInfo mi = mb as MethodInfo;
2777                                 if ((mi == null) || !mi.HasGenericParameters)
2778                                         continue;
2779
2780                                 Type[] gen_params = mi.GetGenericArguments ();
2781
2782                                 if (first == null) {
2783                                         first = mi;
2784                                         first_count = gen_params.Length;
2785                                 }
2786
2787                                 if (gen_params.Length != atypes.Length)
2788                                         continue;
2789
2790                                 list.Add (mi.BindGenericParameters (atypes));
2791                         }
2792
2793                         if (list.Count > 0) {
2794                                 MethodGroupExpr new_mg = new MethodGroupExpr (list, Location);
2795                                 new_mg.InstanceExpression = InstanceExpression;
2796                                 new_mg.HasTypeArguments = true;
2797                                 return new_mg;
2798                         }
2799
2800                         if (first != null)
2801                                 Report.Error (
2802                                         305, loc, "Using the generic method `{0}' " +
2803                                         "requires {1} type arguments", Name,
2804                                         first_count);
2805                         else
2806                                 Report.Error (
2807                                         308, loc, "The non-generic method `{0}' " +
2808                                         "cannot be used with type arguments", Name);
2809
2810                         return null;
2811                 }
2812         }
2813
2814         /// <summary>
2815         ///   Fully resolved expression that evaluates to a Field
2816         /// </summary>
2817         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr, IVariable {
2818                 public readonly FieldInfo FieldInfo;
2819                 Expression instance_expr;
2820                 VariableInfo variable_info;
2821                 
2822                 LocalTemporary temp;
2823                 bool prepared;
2824                 bool is_field_initializer;
2825                 
2826                 public FieldExpr (FieldInfo fi, Location l)
2827                 {
2828                         FieldInfo = fi;
2829                         eclass = ExprClass.Variable;
2830                         type = TypeManager.TypeToCoreType (fi.FieldType);
2831                         loc = l;
2832                 }
2833
2834                 public string Name {
2835                         get {
2836                                 return FieldInfo.Name;
2837                         }
2838                 }
2839
2840                 public bool IsInstance {
2841                         get {
2842                                 return !FieldInfo.IsStatic;
2843                         }
2844                 }
2845
2846                 public bool IsStatic {
2847                         get {
2848                                 return FieldInfo.IsStatic;
2849                         }
2850                 }
2851
2852                 public Type DeclaringType {
2853                         get {
2854                                 return FieldInfo.DeclaringType;
2855                         }
2856                 }
2857
2858                 public Expression InstanceExpression {
2859                         get {
2860                                 return instance_expr;
2861                         }
2862
2863                         set {
2864                                 instance_expr = value;
2865                         }
2866                 }
2867
2868                 public bool IsFieldInitializer {
2869                         get {
2870                                 return is_field_initializer;
2871                         }
2872
2873                         set {
2874                                 is_field_initializer = value;
2875                         }
2876                 }
2877
2878                 public VariableInfo VariableInfo {
2879                         get {
2880                                 return variable_info;
2881                         }
2882                 }
2883
2884                 override public Expression DoResolve (EmitContext ec)
2885                 {
2886                         if (!FieldInfo.IsStatic){
2887                                 if (instance_expr == null){
2888                                         //
2889                                         // This can happen when referencing an instance field using
2890                                         // a fully qualified type expression: TypeName.InstanceField = xxx
2891                                         // 
2892                                         SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
2893                                         return null;
2894                                 }
2895
2896                                 // Resolve the field's instance expression while flow analysis is turned
2897                                 // off: when accessing a field "a.b", we must check whether the field
2898                                 // "a.b" is initialized, not whether the whole struct "a" is initialized.
2899                                 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
2900                                                                        ResolveFlags.DisableFlowAnalysis);
2901                                 if (instance_expr == null)
2902                                         return null;
2903                         }
2904
2905                         ObsoleteAttribute oa;
2906                         FieldBase f = TypeManager.GetField (FieldInfo);
2907                         if (f != null) {
2908                                 oa = f.GetObsoleteAttribute (ec.DeclSpace);
2909                                 if (oa != null)
2910                                         AttributeTester.Report_ObsoleteMessage (oa, f.GetSignatureForError (), loc);
2911                                 // To be sure that type is external because we do not register generated fields
2912                         } else if (!(FieldInfo.DeclaringType is TypeBuilder)) {                                
2913                                 oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo);
2914                                 if (oa != null)
2915                                         AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc);
2916                         }
2917
2918                         // If the instance expression is a local variable or parameter.
2919                         IVariable var = instance_expr as IVariable;
2920                         if ((var == null) || (var.VariableInfo == null))
2921                                 return this;
2922
2923                         VariableInfo vi = var.VariableInfo;
2924                         if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
2925                                 return null;
2926
2927                         variable_info = vi.GetSubStruct (FieldInfo.Name);
2928                         return this;
2929                 }
2930
2931                 void Report_AssignToReadonly (bool is_instance)
2932                 {
2933                         string msg;
2934                         
2935                         if (is_instance)
2936                                 msg = "Readonly field can not be assigned outside " +
2937                                 "of constructor or variable initializer";
2938                         else
2939                                 msg = "A static readonly field can only be assigned in " +
2940                                 "a static constructor";
2941
2942                         Report.Error (is_instance ? 191 : 198, loc, msg);
2943                 }
2944                 
2945                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2946                 {
2947                         IVariable var = instance_expr as IVariable;
2948                         if ((var != null) && (var.VariableInfo != null))
2949                                 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
2950
2951                         Expression e = DoResolve (ec);
2952
2953                         if (e == null)
2954                                 return null;
2955
2956                         if (!FieldInfo.IsStatic && (instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation))) {
2957                                 // FIXME: Provide better error reporting.
2958                                 Error (1612, "Cannot modify expression because it is not a variable.");
2959                                 return null;
2960                         }
2961
2962                         if (!FieldInfo.IsInitOnly)
2963                                 return this;
2964
2965                         FieldBase fb = TypeManager.GetField (FieldInfo);
2966                         if (fb != null)
2967                                 fb.SetAssigned ();
2968
2969                         //
2970                         // InitOnly fields can only be assigned in constructors
2971                         //
2972
2973                         if (ec.IsConstructor){
2974                                 if (IsStatic && !ec.IsStatic)
2975                                         Report_AssignToReadonly (false);
2976
2977                                 Type ctype;
2978                                 if (!is_field_initializer &&
2979                                     (ec.TypeContainer.CurrentType != null))
2980                                         ctype = ec.TypeContainer.CurrentType.ResolveType (ec);
2981                                 else
2982                                         ctype = ec.ContainerType;
2983
2984                                 if (TypeManager.IsEqual (ctype, FieldInfo.DeclaringType))
2985                                         return this;
2986                         }
2987
2988                         Report_AssignToReadonly (true);
2989                         
2990                         return null;
2991                 }
2992
2993                 public bool VerifyFixed (bool is_expression)
2994                 {
2995                         IVariable variable = instance_expr as IVariable;
2996                         if ((variable == null) || !variable.VerifyFixed (true))
2997                                 return false;
2998
2999                         return true;
3000                 }
3001                 
3002                 public void Emit (EmitContext ec, bool leave_copy)
3003                 {
3004                         ILGenerator ig = ec.ig;
3005                         bool is_volatile = false;
3006
3007                         if (FieldInfo is FieldBuilder){
3008                                 FieldBase f = TypeManager.GetField (FieldInfo);
3009                                 if (f != null){
3010                                         if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3011                                                 is_volatile = true;
3012                                         
3013                                         f.status |= Field.Status.USED;
3014                                 }
3015                         } 
3016                         
3017                         if (FieldInfo.IsStatic){
3018                                 if (is_volatile)
3019                                         ig.Emit (OpCodes.Volatile);
3020                                 
3021                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3022                         } else {
3023                                 if (!prepared)
3024                                         EmitInstance (ec);
3025
3026                                 if (is_volatile)
3027                                         ig.Emit (OpCodes.Volatile);
3028
3029                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
3030                         }
3031
3032                         if (leave_copy) {
3033                                 ec.ig.Emit (OpCodes.Dup);
3034                                 if (!FieldInfo.IsStatic) {
3035                                         temp = new LocalTemporary (ec, this.Type);
3036                                         temp.Store (ec);
3037                                 }
3038                         }
3039                 }
3040
3041                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3042                 {
3043                         FieldAttributes fa = FieldInfo.Attributes;
3044                         bool is_static = (fa & FieldAttributes.Static) != 0;
3045                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3046                         ILGenerator ig = ec.ig;
3047                         prepared = prepare_for_load;
3048
3049                         if (is_readonly && !ec.IsConstructor){
3050                                 Report_AssignToReadonly (!is_static);
3051                                 return;
3052                         }
3053
3054                         if (!is_static) {
3055                                 EmitInstance (ec);
3056                                 if (prepare_for_load)
3057                                         ig.Emit (OpCodes.Dup);
3058                         }
3059
3060                         source.Emit (ec);
3061                         if (leave_copy) {
3062                                 ec.ig.Emit (OpCodes.Dup);
3063                                 if (!FieldInfo.IsStatic) {
3064                                         temp = new LocalTemporary (ec, this.Type);
3065                                         temp.Store (ec);
3066                                 }
3067                         }
3068
3069                         if (FieldInfo is FieldBuilder){
3070                                 FieldBase f = TypeManager.GetField (FieldInfo);
3071                                 if (f != null){
3072                                         if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3073                                                 ig.Emit (OpCodes.Volatile);
3074                                         
3075                                         f.status |= Field.Status.ASSIGNED;
3076                                 }
3077                         } 
3078
3079                         if (is_static)
3080                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3081                         else 
3082                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3083                         
3084                         if (temp != null)
3085                                 temp.Emit (ec);
3086                 }
3087
3088                 void EmitInstance (EmitContext ec)
3089                 {
3090                         if (instance_expr.Type.IsValueType) {
3091                                 if (instance_expr is IMemoryLocation) {
3092                                         ((IMemoryLocation) instance_expr).AddressOf (ec, AddressOp.LoadStore);
3093                                 } else {
3094                                         LocalTemporary t = new LocalTemporary (ec, instance_expr.Type);
3095                                         instance_expr.Emit (ec);
3096                                         t.Store (ec);
3097                                         t.AddressOf (ec, AddressOp.Store);
3098                                 }
3099                         } else
3100                                 instance_expr.Emit (ec);
3101                 }
3102
3103                 public override void Emit (EmitContext ec)
3104                 {
3105                         Emit (ec, false);
3106                 }
3107                 
3108                 public void AddressOf (EmitContext ec, AddressOp mode)
3109                 {
3110                         ILGenerator ig = ec.ig;
3111                         
3112                         if (FieldInfo is FieldBuilder){
3113                                 FieldBase f = TypeManager.GetField (FieldInfo);
3114                                 if (f != null){
3115                                         if ((f.ModFlags & Modifiers.VOLATILE) != 0){
3116                                                 Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
3117                                                 return;
3118                                         }
3119                                         
3120                                         if ((mode & AddressOp.Store) != 0)
3121                                                 f.status |= Field.Status.ASSIGNED;
3122                                         if ((mode & AddressOp.Load) != 0)
3123                                                 f.status |= Field.Status.USED;
3124                                 }
3125                         } 
3126
3127                         //
3128                         // Handle initonly fields specially: make a copy and then
3129                         // get the address of the copy.
3130                         //
3131                         bool need_copy;
3132                         if (FieldInfo.IsInitOnly){
3133                                 need_copy = true;
3134                                 if (ec.IsConstructor){
3135                                         if (FieldInfo.IsStatic){
3136                                                 if (ec.IsStatic)
3137                                                         need_copy = false;
3138                                         } else
3139                                                 need_copy = false;
3140                                 }
3141                         } else
3142                                 need_copy = false;
3143                         
3144                         if (need_copy){
3145                                 LocalBuilder local;
3146                                 Emit (ec);
3147                                 local = ig.DeclareLocal (type);
3148                                 ig.Emit (OpCodes.Stloc, local);
3149                                 ig.Emit (OpCodes.Ldloca, local);
3150                                 return;
3151                         }
3152
3153
3154                         if (FieldInfo.IsStatic){
3155                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3156                         } else {
3157                                 EmitInstance (ec);
3158                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
3159                         }
3160                 }
3161         }
3162
3163         //
3164         // A FieldExpr whose address can not be taken
3165         //
3166         public class FieldExprNoAddress : FieldExpr, IMemoryLocation {
3167                 public FieldExprNoAddress (FieldInfo fi, Location loc) : base (fi, loc)
3168                 {
3169                 }
3170                 
3171                 public new void AddressOf (EmitContext ec, AddressOp mode)
3172                 {
3173                         Report.Error (-215, "Report this: Taking the address of a remapped parameter not supported");
3174                 }
3175         }
3176         
3177         /// <summary>
3178         ///   Expression that evaluates to a Property.  The Assign class
3179         ///   might set the `Value' expression if we are in an assignment.
3180         ///
3181         ///   This is not an LValue because we need to re-write the expression, we
3182         ///   can not take data from the stack and store it.  
3183         /// </summary>
3184         public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
3185                 public readonly PropertyInfo PropertyInfo;
3186
3187                 //
3188                 // This is set externally by the  `BaseAccess' class
3189                 //
3190                 public bool IsBase;
3191                 MethodInfo getter, setter;
3192                 bool is_static;
3193                 bool must_do_cs1540_check;
3194                 
3195                 Expression instance_expr;
3196                 LocalTemporary temp;
3197                 bool prepared;
3198
3199                 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
3200                 {
3201                         PropertyInfo = pi;
3202                         eclass = ExprClass.PropertyAccess;
3203                         is_static = false;
3204                         loc = l;
3205
3206                         type = TypeManager.TypeToCoreType (pi.PropertyType);
3207
3208                         ResolveAccessors (ec);
3209                 }
3210
3211                 public string Name {
3212                         get {
3213                                 return PropertyInfo.Name;
3214                         }
3215                 }
3216
3217                 public bool IsInstance {
3218                         get {
3219                                 return !is_static;
3220                         }
3221                 }
3222
3223                 public bool IsStatic {
3224                         get {
3225                                 return is_static;
3226                         }
3227                 }
3228                 
3229                 public Type DeclaringType {
3230                         get {
3231                                 return PropertyInfo.DeclaringType;
3232                         }
3233                 }
3234
3235                 //
3236                 // The instance expression associated with this expression
3237                 //
3238                 public Expression InstanceExpression {
3239                         set {
3240                                 instance_expr = value;
3241                         }
3242
3243                         get {
3244                                 return instance_expr;
3245                         }
3246                 }
3247
3248                 public bool VerifyAssignable ()
3249                 {
3250                         if (setter == null) {
3251                                 Report.Error (200, loc, 
3252                                               "The property `" + PropertyInfo.Name +
3253                                               "' can not be assigned to, as it has not set accessor");
3254                                 return false;
3255                         }
3256
3257                         return true;
3258                 }
3259
3260                 MethodInfo FindAccessor (Type invocation_type, bool is_set)
3261                 {
3262                         BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
3263                                 BindingFlags.Static | BindingFlags.Instance |
3264                                 BindingFlags.DeclaredOnly;
3265
3266                         Type current = PropertyInfo.DeclaringType;
3267                         for (; current != null; current = current.BaseType) {
3268                                 MemberInfo[] group = TypeManager.MemberLookup (
3269                                         invocation_type, invocation_type, current,
3270                                         MemberTypes.Property, flags, PropertyInfo.Name, null);
3271
3272                                 if (group == null)
3273                                         continue;
3274
3275                                 if (group.Length != 1)
3276                                         // Oooops, can this ever happen ?
3277                                         return null;
3278
3279                                 PropertyInfo pi = (PropertyInfo) group [0];
3280
3281                                 MethodInfo get = pi.GetGetMethod (true);
3282                                 MethodInfo set = pi.GetSetMethod (true);
3283
3284                                 if (is_set) {
3285                                         if (set != null)
3286                                                 return set;
3287                                 } else {
3288                                         if (get != null)
3289                                                 return get;
3290                                 }
3291
3292                                 MethodInfo accessor = get != null ? get : set;
3293                                 if (accessor == null)
3294                                         continue;
3295                                 if ((accessor.Attributes & MethodAttributes.NewSlot) != 0)
3296                                         break;
3297                         }
3298
3299                         return null;
3300                 }
3301
3302                 MethodInfo GetAccessor (Type invocation_type, bool is_set)
3303                 {
3304                         MethodInfo mi = FindAccessor (invocation_type, is_set);
3305                         if (mi == null)
3306                                 return null;
3307
3308                         MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
3309
3310                         //
3311                         // If only accessible to the current class or children
3312                         //
3313                         if (ma == MethodAttributes.Private) {
3314                                 Type declaring_type = mi.DeclaringType;
3315                                         
3316                                 if (invocation_type != declaring_type){
3317                                         if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
3318                                                 return mi;
3319                                         else
3320                                                 return null;
3321                                 } else
3322                                         return mi;
3323                         }
3324                         //
3325                         // FamAndAssem requires that we not only derivate, but we are on the
3326                         // same assembly.  
3327                         //
3328                         if (ma == MethodAttributes.FamANDAssem){
3329                                 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
3330                                         return null;
3331                                 else
3332                                         return mi;
3333                         }
3334
3335                         // Assembly and FamORAssem succeed if we're in the same assembly.
3336                         if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
3337                                 if (mi.DeclaringType.Assembly == invocation_type.Assembly)
3338                                         return mi;
3339                         }
3340
3341                         // We already know that we aren't in the same assembly.
3342                         if (ma == MethodAttributes.Assembly)
3343                                 return null;
3344
3345                         // Family and FamANDAssem require that we derive.
3346                         if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem) || (ma == MethodAttributes.FamORAssem)){
3347                                 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
3348                                         return null;
3349                                 else {
3350                                         if (!TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType))
3351                                                 must_do_cs1540_check = true;
3352
3353                                         return mi;
3354                                 }
3355                         }
3356
3357                         return mi;
3358                 }
3359
3360                 //
3361                 // We also perform the permission checking here, as the PropertyInfo does not
3362                 // hold the information for the accessibility of its setter/getter
3363                 //
3364                 void ResolveAccessors (EmitContext ec)
3365                 {
3366                         getter = GetAccessor (ec.ContainerType, false);
3367                         if ((getter != null) && getter.IsStatic)
3368                                 is_static = true;
3369
3370                         setter = GetAccessor (ec.ContainerType, true);
3371                         if ((setter != null) && setter.IsStatic)
3372                                 is_static = true;
3373
3374                         if (setter == null && getter == null){
3375                                 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", PropertyInfo.Name);
3376                         }
3377                 }
3378
3379                 bool InstanceResolve (EmitContext ec)
3380                 {
3381                         if ((instance_expr == null) && ec.IsStatic && !is_static) {
3382                                 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
3383                                 return false;
3384                         }
3385
3386                         if (instance_expr != null) {
3387                                 instance_expr = instance_expr.DoResolve (ec);
3388                                 if (instance_expr == null)
3389                                         return false;
3390                         }
3391
3392                         if (must_do_cs1540_check && (instance_expr != null)) {
3393                                 if ((instance_expr.Type != ec.ContainerType) &&
3394                                     ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
3395                                         Report.Error (1540, loc, "Cannot access protected member `" +
3396                                                       PropertyInfo.DeclaringType + "." + PropertyInfo.Name + 
3397                                                       "' via a qualifier of type `" +
3398                                                       TypeManager.CSharpName (instance_expr.Type) +
3399                                                       "'; the qualifier must be of type `" +
3400                                                       TypeManager.CSharpName (ec.ContainerType) +
3401                                                       "' (or derived from it)");
3402                                         return false;
3403                                 }
3404                         }
3405
3406                         return true;
3407                 }
3408                 
3409                 override public Expression DoResolve (EmitContext ec)
3410                 {
3411                         if (getter != null){
3412                                 if (TypeManager.GetArgumentTypes (getter).Length != 0){
3413                                         Report.Error (
3414                                                 117, loc, "`{0}' does not contain a " +
3415                                                 "definition for `{1}'.", getter.DeclaringType,
3416                                                 Name);
3417                                         return null;
3418                                 }
3419                         }
3420
3421                         if (getter == null){
3422                                 //
3423                                 // The following condition happens if the PropertyExpr was
3424                                 // created, but is invalid (ie, the property is inaccessible),
3425                                 // and we did not want to embed the knowledge about this in
3426                                 // the caller routine.  This only avoids double error reporting.
3427                                 //
3428                                 if (setter == null)
3429                                         return null;
3430                                 
3431                                 Report.Error (154, loc, 
3432                                               "The property `" + PropertyInfo.Name +
3433                                               "' can not be used in " +
3434                                               "this context because it lacks a get accessor");
3435                                 return null;
3436                         } 
3437
3438                         if (!InstanceResolve (ec))
3439                                 return null;
3440
3441                         //
3442                         // Only base will allow this invocation to happen.
3443                         //
3444                         if (IsBase && getter.IsAbstract){
3445                                 Report.Error (205, loc, "Cannot call an abstract base property: " +
3446                                               PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3447                                 return null;
3448                         }
3449
3450                         return this;
3451                 }
3452
3453                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3454                 {
3455                         if (setter == null){
3456                                 //
3457                                 // The following condition happens if the PropertyExpr was
3458                                 // created, but is invalid (ie, the property is inaccessible),
3459                                 // and we did not want to embed the knowledge about this in
3460                                 // the caller routine.  This only avoids double error reporting.
3461                                 //
3462                                 if (getter == null)
3463                                         return null;
3464                                 
3465                                 Report.Error (154, loc, 
3466                                               "The property `" + PropertyInfo.Name +
3467                                               "' can not be used in " +
3468                                               "this context because it lacks a set accessor");
3469                                 return null;
3470                         }
3471
3472                         if (TypeManager.GetArgumentTypes (setter).Length != 1){
3473                                 Report.Error (
3474                                         117, loc, "`{0}' does not contain a " +
3475                                         "definition for `{1}'.", getter.DeclaringType,
3476                                         Name);
3477                                 return null;
3478                         }
3479
3480                         if (!InstanceResolve (ec))
3481                                 return null;
3482                         
3483                         //
3484                         // Only base will allow this invocation to happen.
3485                         //
3486                         if (IsBase && setter.IsAbstract){
3487                                 Report.Error (205, loc, "Cannot call an abstract base property: " +
3488                                               PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3489                                 return null;
3490                         }
3491
3492                         //
3493                         // Check that we are not making changes to a temporary memory location
3494                         //
3495                         if (instance_expr != null && instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation)) {
3496                                 // FIXME: Provide better error reporting.
3497                                 Error (1612, "Cannot modify expression because it is not a variable.");
3498                                 return null;
3499                         }
3500
3501                         return this;
3502                 }
3503
3504
3505                 
3506                 public override void Emit (EmitContext ec)
3507                 {
3508                         Emit (ec, false);
3509                 }
3510                 
3511                 void EmitInstance (EmitContext ec)
3512                 {
3513                         if (is_static)
3514                                 return;
3515
3516                         if (instance_expr.Type.IsValueType) {
3517                                 if (instance_expr is IMemoryLocation) {
3518                                         ((IMemoryLocation) instance_expr).AddressOf (ec, AddressOp.LoadStore);
3519                                 } else {
3520                                         LocalTemporary t = new LocalTemporary (ec, instance_expr.Type);
3521                                         instance_expr.Emit (ec);
3522                                         t.Store (ec);
3523                                         t.AddressOf (ec, AddressOp.Store);
3524                                 }
3525                         } else
3526                                 instance_expr.Emit (ec);
3527                         
3528                         if (prepared)
3529                                 ec.ig.Emit (OpCodes.Dup);
3530                 }
3531
3532                 
3533                 public void Emit (EmitContext ec, bool leave_copy)
3534                 {
3535                         if (!prepared)
3536                                 EmitInstance (ec);
3537                         
3538                         //
3539                         // Special case: length of single dimension array property is turned into ldlen
3540                         //
3541                         if ((getter == TypeManager.system_int_array_get_length) ||
3542                             (getter == TypeManager.int_array_get_length)){
3543                                 Type iet = instance_expr.Type;
3544
3545                                 //
3546                                 // System.Array.Length can be called, but the Type does not
3547                                 // support invoking GetArrayRank, so test for that case first
3548                                 //
3549                                 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)) {
3550                                         ec.ig.Emit (OpCodes.Ldlen);
3551                                         ec.ig.Emit (OpCodes.Conv_I4);
3552                                         return;
3553                                 }
3554                         }
3555
3556                         Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), getter, null, loc);
3557                         
3558                         if (!leave_copy)
3559                                 return;
3560                         
3561                         ec.ig.Emit (OpCodes.Dup);
3562                         if (!is_static) {
3563                                 temp = new LocalTemporary (ec, this.Type);
3564                                 temp.Store (ec);
3565                         }
3566                 }
3567
3568                 //
3569                 // Implements the IAssignMethod interface for assignments
3570                 //
3571                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3572                 {
3573                         prepared = prepare_for_load;
3574                         
3575                         EmitInstance (ec);
3576
3577                         source.Emit (ec);
3578                         if (leave_copy) {
3579                                 ec.ig.Emit (OpCodes.Dup);
3580                                 if (!is_static) {
3581                                         temp = new LocalTemporary (ec, this.Type);
3582                                         temp.Store (ec);
3583                                 }
3584                         }
3585                         
3586                         ArrayList args = new ArrayList (1);
3587                         args.Add (new Argument (new EmptyAddressOf (), Argument.AType.Expression));
3588                         
3589                         Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), setter, args, loc);
3590                         
3591                         if (temp != null)
3592                                 temp.Emit (ec);
3593                 }
3594
3595                 override public void EmitStatement (EmitContext ec)
3596                 {
3597                         Emit (ec);
3598                         ec.ig.Emit (OpCodes.Pop);
3599                 }
3600         }
3601
3602         /// <summary>
3603         ///   Fully resolved expression that evaluates to an Event
3604         /// </summary>
3605         public class EventExpr : Expression, IMemberExpr {
3606                 public readonly EventInfo EventInfo;
3607                 Expression instance_expr;
3608
3609                 bool is_static;
3610                 MethodInfo add_accessor, remove_accessor;
3611                 
3612                 public EventExpr (EventInfo ei, Location loc)
3613                 {
3614                         EventInfo = ei;
3615                         this.loc = loc;
3616                         eclass = ExprClass.EventAccess;
3617
3618                         add_accessor = TypeManager.GetAddMethod (ei);
3619                         remove_accessor = TypeManager.GetRemoveMethod (ei);
3620                         
3621                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
3622                                 is_static = true;
3623
3624                         if (EventInfo is MyEventBuilder){
3625                                 MyEventBuilder eb = (MyEventBuilder) EventInfo;
3626                                 type = eb.EventType;
3627                                 eb.SetUsed ();
3628                         } else
3629                                 type = EventInfo.EventHandlerType;
3630                 }
3631
3632                 public string Name {
3633                         get {
3634                                 return EventInfo.Name;
3635                         }
3636                 }
3637
3638                 public bool IsInstance {
3639                         get {
3640                                 return !is_static;
3641                         }
3642                 }
3643
3644                 public bool IsStatic {
3645                         get {
3646                                 return is_static;
3647                         }
3648                 }
3649
3650                 public Type DeclaringType {
3651                         get {
3652                                 return EventInfo.DeclaringType;
3653                         }
3654                 }
3655
3656                 public Expression InstanceExpression {
3657                         get {
3658                                 return instance_expr;
3659                         }
3660
3661                         set {
3662                                 instance_expr = value;
3663                         }
3664                 }
3665
3666                 public override Expression DoResolve (EmitContext ec)
3667                 {
3668                         if (instance_expr != null) {
3669                                 instance_expr = instance_expr.DoResolve (ec);
3670                                 if (instance_expr == null)
3671                                         return null;
3672                         }
3673
3674                         
3675                         return this;
3676                 }
3677
3678                 public override void Emit (EmitContext ec)
3679                 {
3680                         Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
3681                 }
3682
3683                 public void EmitAddOrRemove (EmitContext ec, Expression source)
3684                 {
3685                         BinaryDelegate source_del = (BinaryDelegate) source;
3686                         Expression handler = source_del.Right;
3687                         
3688                         Argument arg = new Argument (handler, Argument.AType.Expression);
3689                         ArrayList args = new ArrayList ();
3690                                 
3691                         args.Add (arg);
3692                         
3693                         if (source_del.IsAddition)
3694                                 Invocation.EmitCall (
3695                                         ec, false, IsStatic, instance_expr, add_accessor, args, loc);
3696                         else
3697                                 Invocation.EmitCall (
3698                                         ec, false, IsStatic, instance_expr, remove_accessor, args, loc);
3699                 }
3700         }
3701 }