Reverted everything until 24 hours ago.
[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, loc);
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, loc);
335                                         return null;
336                                 }
337                                 break;
338
339                         case ExprClass.MethodGroup:
340                                 if (RootContext.Version == LanguageVersion.ISO_1){
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, loc);
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 = System.Enum.GetUnderlyingType (real_type);
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, Location loc)
839                 {
840                         string kind = "Unknown";
841                         
842                         kind = ExprClassName (eclass);
843
844                         Report.Error (118, loc, "Expression denotes a `" + kind +
845                                "' where a `" + expected + "' was expected");
846                 }
847
848                 public void Error_UnexpectedKind (ResolveFlags flags, Location loc)
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                 public static void Error_NegativeArrayIndex (Location loc)
1271                 {
1272                         Report.Error (248, loc, "Cannot create an 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                 public override bool IsNegative {
1436                         get {
1437                                 return false;
1438                         }
1439                 }
1440         }
1441
1442
1443         /// <summary>
1444         ///  This class is used to wrap literals which belong inside Enums
1445         /// </summary>
1446         public class EnumConstant : Constant {
1447                 public Constant Child;
1448
1449                 public EnumConstant (Constant child, Type enum_type)
1450                 {
1451                         eclass = child.eclass;
1452                         this.Child = child;
1453                         type = enum_type;
1454                 }
1455                 
1456                 public override Expression DoResolve (EmitContext ec)
1457                 {
1458                         // This should never be invoked, we are born in fully
1459                         // initialized state.
1460
1461                         return this;
1462                 }
1463
1464                 public override void Emit (EmitContext ec)
1465                 {
1466                         Child.Emit (ec);
1467                 }
1468
1469                 public override object GetValue ()
1470                 {
1471                         return Child.GetValue ();
1472                 }
1473
1474                 public object GetValueAsEnumType ()
1475                 {
1476                         return System.Enum.ToObject (type, Child.GetValue ());
1477                 }
1478
1479                 //
1480                 // Converts from one of the valid underlying types for an enumeration
1481                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1482                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1483                 //
1484                 public Constant WidenToCompilerConstant ()
1485                 {
1486                         Type t = TypeManager.EnumToUnderlying (Child.Type);
1487                         object v = ((Constant) Child).GetValue ();;
1488                         
1489                         if (t == TypeManager.int32_type)
1490                                 return new IntConstant ((int) v);
1491                         if (t == TypeManager.uint32_type)
1492                                 return new UIntConstant ((uint) v);
1493                         if (t == TypeManager.int64_type)
1494                                 return new LongConstant ((long) v);
1495                         if (t == TypeManager.uint64_type)
1496                                 return new ULongConstant ((ulong) v);
1497                         if (t == TypeManager.short_type)
1498                                 return new ShortConstant ((short) v);
1499                         if (t == TypeManager.ushort_type)
1500                                 return new UShortConstant ((ushort) v);
1501                         if (t == TypeManager.byte_type)
1502                                 return new ByteConstant ((byte) v);
1503                         if (t == TypeManager.sbyte_type)
1504                                 return new SByteConstant ((sbyte) v);
1505
1506                         throw new Exception ("Invalid enumeration underlying type: " + t);
1507                 }
1508
1509                 //
1510                 // Extracts the value in the enumeration on its native representation
1511                 //
1512                 public object GetPlainValue ()
1513                 {
1514                         Type t = TypeManager.EnumToUnderlying (Child.Type);
1515                         object v = ((Constant) Child).GetValue ();;
1516                         
1517                         if (t == TypeManager.int32_type)
1518                                 return (int) v;
1519                         if (t == TypeManager.uint32_type)
1520                                 return (uint) v;
1521                         if (t == TypeManager.int64_type)
1522                                 return (long) v;
1523                         if (t == TypeManager.uint64_type)
1524                                 return (ulong) v;
1525                         if (t == TypeManager.short_type)
1526                                 return (short) v;
1527                         if (t == TypeManager.ushort_type)
1528                                 return (ushort) v;
1529                         if (t == TypeManager.byte_type)
1530                                 return (byte) v;
1531                         if (t == TypeManager.sbyte_type)
1532                                 return (sbyte) v;
1533
1534                         return null;
1535                 }
1536                 
1537                 public override string AsString ()
1538                 {
1539                         return Child.AsString ();
1540                 }
1541
1542                 public override DoubleConstant ConvertToDouble ()
1543                 {
1544                         return Child.ConvertToDouble ();
1545                 }
1546
1547                 public override FloatConstant ConvertToFloat ()
1548                 {
1549                         return Child.ConvertToFloat ();
1550                 }
1551
1552                 public override ULongConstant ConvertToULong ()
1553                 {
1554                         return Child.ConvertToULong ();
1555                 }
1556
1557                 public override LongConstant ConvertToLong ()
1558                 {
1559                         return Child.ConvertToLong ();
1560                 }
1561
1562                 public override UIntConstant ConvertToUInt ()
1563                 {
1564                         return Child.ConvertToUInt ();
1565                 }
1566
1567                 public override IntConstant ConvertToInt ()
1568                 {
1569                         return Child.ConvertToInt ();
1570                 }
1571                 
1572                 public override bool IsZeroInteger {
1573                         get { return Child.IsZeroInteger; }
1574                 }
1575
1576                 public override bool IsNegative {
1577                         get {
1578                                 return Child.IsNegative;
1579                         }
1580                 }
1581         }
1582
1583         /// <summary>
1584         ///   This kind of cast is used to encapsulate Value Types in objects.
1585         ///
1586         ///   The effect of it is to box the value type emitted by the previous
1587         ///   operation.
1588         /// </summary>
1589         public class BoxedCast : EmptyCast {
1590
1591                 public BoxedCast (Expression expr)
1592                         : base (expr, TypeManager.object_type) 
1593                 {
1594                         eclass = ExprClass.Value;
1595                 }
1596
1597                 public BoxedCast (Expression expr, Type target_type)
1598                         : base (expr, target_type)
1599                 {
1600                         eclass = ExprClass.Value;
1601                 }
1602                 
1603                 public override Expression DoResolve (EmitContext ec)
1604                 {
1605                         // This should never be invoked, we are born in fully
1606                         // initialized state.
1607
1608                         return this;
1609                 }
1610
1611                 public override void Emit (EmitContext ec)
1612                 {
1613                         base.Emit (ec);
1614                         
1615                         ec.ig.Emit (OpCodes.Box, child.Type);
1616                 }
1617         }
1618
1619         public class UnboxCast : EmptyCast {
1620                 public UnboxCast (Expression expr, Type return_type)
1621                         : base (expr, return_type)
1622                 {
1623                 }
1624
1625                 public override Expression DoResolve (EmitContext ec)
1626                 {
1627                         // This should never be invoked, we are born in fully
1628                         // initialized state.
1629
1630                         return this;
1631                 }
1632
1633                 public override void Emit (EmitContext ec)
1634                 {
1635                         Type t = type;
1636                         ILGenerator ig = ec.ig;
1637                         
1638                         base.Emit (ec);
1639                         if (t.IsGenericParameter)
1640                                 ig.Emit (OpCodes.Unbox_Any, t);
1641                         else {
1642                                 ig.Emit (OpCodes.Unbox, t);
1643
1644                                 LoadFromPtr (ig, t);
1645                         }
1646                 }
1647         }
1648         
1649         /// <summary>
1650         ///   This is used to perform explicit numeric conversions.
1651         ///
1652         ///   Explicit numeric conversions might trigger exceptions in a checked
1653         ///   context, so they should generate the conv.ovf opcodes instead of
1654         ///   conv opcodes.
1655         /// </summary>
1656         public class ConvCast : EmptyCast {
1657                 public enum Mode : byte {
1658                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1659                         U1_I1, U1_CH,
1660                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1661                         U2_I1, U2_U1, U2_I2, U2_CH,
1662                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1663                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1664                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1665                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1666                         CH_I1, CH_U1, CH_I2,
1667                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1668                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1669                 }
1670
1671                 Mode mode;
1672                 bool checked_state;
1673                 
1674                 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
1675                         : base (child, return_type)
1676                 {
1677                         checked_state = ec.CheckState;
1678                         mode = m;
1679                 }
1680
1681                 public override Expression DoResolve (EmitContext ec)
1682                 {
1683                         // This should never be invoked, we are born in fully
1684                         // initialized state.
1685
1686                         return this;
1687                 }
1688
1689                 public override string ToString ()
1690                 {
1691                         return String.Format ("ConvCast ({0}, {1})", mode, child);
1692                 }
1693                 
1694                 public override void Emit (EmitContext ec)
1695                 {
1696                         ILGenerator ig = ec.ig;
1697                         
1698                         base.Emit (ec);
1699
1700                         if (checked_state){
1701                                 switch (mode){
1702                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1703                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1704                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1705                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1706                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1707
1708                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1709                                 case Mode.U1_CH: /* nothing */ break;
1710
1711                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1712                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1713                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1714                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1715                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1716                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1717
1718                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1719                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1720                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1721                                 case Mode.U2_CH: /* nothing */ break;
1722
1723                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1724                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1725                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1726                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1727                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1728                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1729                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1730
1731                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1732                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1733                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1734                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1735                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1736                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1737
1738                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1739                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1740                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1741                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1742                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1743                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1744                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1745                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1746
1747                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1748                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1749                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1750                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1751                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1752                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1753                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1754                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1755
1756                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1757                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1758                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1759
1760                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1761                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1762                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1763                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1764                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1765                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1766                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1767                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1768                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1769
1770                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1771                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1772                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1773                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1774                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1775                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1776                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1777                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1778                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1779                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1780                                 }
1781                         } else {
1782                                 switch (mode){
1783                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1784                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1785                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1786                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1787                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1788
1789                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1790                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1791
1792                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1793                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1794                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1795                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1796                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1797                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1798
1799                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1800                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1801                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1802                                 case Mode.U2_CH: /* nothing */ break;
1803
1804                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1805                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1806                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1807                                 case Mode.I4_U4: /* nothing */ break;
1808                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1809                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1810                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1811
1812                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1813                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1814                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1815                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1816                                 case Mode.U4_I4: /* nothing */ break;
1817                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1818
1819                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1820                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1821                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1822                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1823                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1824                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1825                                 case Mode.I8_U8: /* nothing */ break;
1826                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1827
1828                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1829                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1830                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1831                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1832                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1833                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1834                                 case Mode.U8_I8: /* nothing */ break;
1835                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1836
1837                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1838                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1839                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1840
1841                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1842                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1843                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1844                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1845                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1846                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1847                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1848                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1849                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1850
1851                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1852                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1853                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1854                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1855                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1856                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1857                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1858                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1859                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1860                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1861                                 }
1862                         }
1863                 }
1864         }
1865         
1866         public class OpcodeCast : EmptyCast {
1867                 OpCode op, op2;
1868                 bool second_valid;
1869                 
1870                 public OpcodeCast (Expression child, Type return_type, OpCode op)
1871                         : base (child, return_type)
1872                         
1873                 {
1874                         this.op = op;
1875                         second_valid = false;
1876                 }
1877
1878                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1879                         : base (child, return_type)
1880                         
1881                 {
1882                         this.op = op;
1883                         this.op2 = op2;
1884                         second_valid = true;
1885                 }
1886
1887                 public override Expression DoResolve (EmitContext ec)
1888                 {
1889                         // This should never be invoked, we are born in fully
1890                         // initialized state.
1891
1892                         return this;
1893                 }
1894
1895                 public override void Emit (EmitContext ec)
1896                 {
1897                         base.Emit (ec);
1898                         ec.ig.Emit (op);
1899
1900                         if (second_valid)
1901                                 ec.ig.Emit (op2);
1902                 }                       
1903         }
1904
1905         /// <summary>
1906         ///   This kind of cast is used to encapsulate a child and cast it
1907         ///   to the class requested
1908         /// </summary>
1909         public class ClassCast : EmptyCast {
1910                 public ClassCast (Expression child, Type return_type)
1911                         : base (child, return_type)
1912                         
1913                 {
1914                 }
1915
1916                 public override Expression DoResolve (EmitContext ec)
1917                 {
1918                         // This should never be invoked, we are born in fully
1919                         // initialized state.
1920
1921                         return this;
1922                 }
1923
1924                 public override void Emit (EmitContext ec)
1925                 {
1926                         base.Emit (ec);
1927
1928                         if (child.Type.IsGenericParameter)
1929                                 ec.ig.Emit (OpCodes.Box, child.Type);
1930
1931                         if (type.IsGenericParameter)
1932                                 ec.ig.Emit (OpCodes.Unbox_Any, type);
1933                         else
1934                                 ec.ig.Emit (OpCodes.Castclass, type);
1935                 }
1936         }
1937         
1938         /// <summary>
1939         ///   SimpleName expressions are initially formed of a single
1940         ///   word and it only happens at the beginning of the expression.
1941         /// </summary>
1942         ///
1943         /// <remarks>
1944         ///   The expression will try to be bound to a Field, a Method
1945         ///   group or a Property.  If those fail we pass the name to our
1946         ///   caller and the SimpleName is compounded to perform a type
1947         ///   lookup.  The idea behind this process is that we want to avoid
1948         ///   creating a namespace map from the assemblies, as that requires
1949         ///   the GetExportedTypes function to be called and a hashtable to
1950         ///   be constructed which reduces startup time.  If later we find
1951         ///   that this is slower, we should create a `NamespaceExpr' expression
1952         ///   that fully participates in the resolution process. 
1953         ///   
1954         ///   For example `System.Console.WriteLine' is decomposed into
1955         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1956         ///   
1957         ///   The first SimpleName wont produce a match on its own, so it will
1958         ///   be turned into:
1959         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
1960         ///   
1961         ///   System.Console will produce a TypeExpr match.
1962         ///   
1963         ///   The downside of this is that we might be hitting `LookupType' too many
1964         ///   times with this scheme.
1965         /// </remarks>
1966         public class SimpleName : Expression {
1967                 public string Name;
1968                 public readonly TypeArguments Arguments;
1969
1970                 //
1971                 // If true, then we are a simple name, not composed with a ".
1972                 //
1973                 bool is_base;
1974
1975                 public SimpleName (string a, string b, Location l)
1976                 {
1977                         Name = String.Concat (a, ".", b);
1978                         loc = l;
1979                         is_base = false;
1980                 }
1981                 
1982                 public SimpleName (string name, Location l)
1983                 {
1984                         Name = name;
1985                         loc = l;
1986                         is_base = true;
1987                 }
1988
1989                 public SimpleName (string name, TypeArguments args, Location l)
1990                 {
1991                         Name = name;
1992                         Arguments = args;
1993                         loc = l;
1994                         is_base = true;
1995                 }
1996
1997                 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
1998                 {
1999                         if (ec.IsFieldInitializer)
2000                                 Report.Error (
2001                                         236, l,
2002                                         "A field initializer cannot reference the non-static field, " +
2003                                         "method or property `"+name+"'");
2004                         else
2005                                 Report.Error (
2006                                         120, l,
2007                                         "An object reference is required " +
2008                                         "for the non-static field `"+name+"'");
2009                 }
2010                 
2011                 //
2012                 // Checks whether we are trying to access an instance
2013                 // property, method or field from a static body.
2014                 //
2015                 Expression MemberStaticCheck (EmitContext ec, Expression e)
2016                 {
2017                         if (e is IMemberExpr){
2018                                 IMemberExpr member = (IMemberExpr) e;
2019                                 
2020                                 if (!member.IsStatic){
2021                                         Error_ObjectRefRequired (ec, loc, Name);
2022                                         return null;
2023                                 }
2024                         }
2025
2026                         return e;
2027                 }
2028                 
2029                 public override Expression DoResolve (EmitContext ec)
2030                 {
2031                         return SimpleNameResolve (ec, null, false, false);
2032                 }
2033
2034                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
2035                 {
2036                         return SimpleNameResolve (ec, right_side, false, false);
2037                 }
2038                 
2039
2040                 public Expression DoResolveAllowStatic (EmitContext ec, bool intermediate)
2041                 {
2042                         return SimpleNameResolve (ec, null, true, intermediate);
2043                 }
2044
2045                 public override Expression ResolveAsTypeStep (EmitContext ec)
2046                 {
2047                         DeclSpace ds = ec.DeclSpace;
2048                         NamespaceEntry ns = ds.NamespaceEntry;
2049                         TypeExpr t;
2050                         IAlias alias_value;
2051
2052                         //
2053                         // Since we are cheating: we only do the Alias lookup for
2054                         // namespaces if the name does not include any dots in it
2055                         //
2056                         if (ns != null && is_base)
2057                                 alias_value = ns.LookupAlias (Name);
2058                         else
2059                                 alias_value = null;
2060
2061                         TypeParameterExpr generic_type = ds.LookupGeneric (Name, loc);
2062                         if (generic_type != null)
2063                                 return generic_type.ResolveAsTypeTerminal (ec);
2064
2065                         if (ec.ResolvingTypeTree){
2066                                 int errors = Report.Errors;
2067                                 Type dt = ds.FindType (loc, Name);
2068                                 
2069                                 if (Report.Errors != errors)
2070                                         return null;
2071                                 
2072                                 if (dt != null)
2073                                         return new TypeExpression (dt, loc);
2074
2075                                 if (alias_value != null){
2076                                         if (alias_value.IsType)
2077                                                 return alias_value.Type;
2078                                         if ((t = RootContext.LookupType (ds, alias_value.Name, true, loc)) != null)
2079                                                 return t;
2080                                 }
2081                         }
2082
2083                         //
2084                         // First, the using aliases
2085                         //
2086                         if (alias_value != null){
2087                                 if (alias_value.IsType)
2088                                         return alias_value.Type;
2089                                 if ((t = RootContext.LookupType (ds, alias_value.Name, true, loc)) != null)
2090                                         return t;
2091                                 
2092                                 // we have alias value, but it isn't Type, so try if it's namespace
2093                                 return new SimpleName (alias_value.Name, loc);
2094                         }
2095
2096                         //
2097                         // Stage 2: Lookup up if we are an alias to a type
2098                         // or a namespace.
2099                         //
2100
2101                         if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
2102                                 return t;
2103                                 
2104                         // No match, maybe our parent can compose us
2105                         // into something meaningful.
2106                         return this;
2107                 }
2108
2109                 Expression SimpleNameResolve (EmitContext ec, Expression right_side,
2110                                               bool allow_static, bool intermediate)
2111                 {
2112                         Expression e = DoSimpleNameResolve (ec, right_side, allow_static, intermediate);
2113                         if (e == null)
2114                                 return null;
2115
2116                         Block current_block = ec.CurrentBlock;
2117                         if (current_block != null){
2118                                 //LocalInfo vi = current_block.GetLocalInfo (Name);
2119                                 if (is_base &&
2120                                     current_block.IsVariableNameUsedInChildBlock(Name)) {
2121                                         Report.Error (135, Location,
2122                                                       "'{0}' has a different meaning in a " +
2123                                                       "child block", Name);
2124                                         return null;
2125                                 }
2126                         }
2127
2128                         if (e.Type != null && e.Type.IsPointer && !ec.InUnsafe) {
2129                                 UnsafeError (loc);
2130                                 return null;
2131                         }
2132
2133                         return e;
2134                 }
2135
2136                 /// <remarks>
2137                 ///   7.5.2: Simple Names. 
2138                 ///
2139                 ///   Local Variables and Parameters are handled at
2140                 ///   parse time, so they never occur as SimpleNames.
2141                 ///
2142                 ///   The `allow_static' flag is used by MemberAccess only
2143                 ///   and it is used to inform us that it is ok for us to 
2144                 ///   avoid the static check, because MemberAccess might end
2145                 ///   up resolving the Name as a Type name and the access as
2146                 ///   a static type access.
2147                 ///
2148                 ///   ie: Type Type; .... { Type.GetType (""); }
2149                 ///
2150                 ///   Type is both an instance variable and a Type;  Type.GetType
2151                 ///   is the static method not an instance method of type.
2152                 /// </remarks>
2153                 Expression DoSimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static, bool intermediate)
2154                 {
2155                         Expression e = null;
2156
2157                         //
2158                         // Stage 1: Performed by the parser (binding to locals or parameters).
2159                         //
2160                         Block current_block = ec.CurrentBlock;
2161                         if (current_block != null){
2162                                 LocalInfo vi = current_block.GetLocalInfo (Name);
2163                                 if (vi != null){
2164                                         Expression var;
2165                                         
2166                                         var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2167                                         
2168                                         if (right_side != null)
2169                                                 return var.ResolveLValue (ec, right_side);
2170                                         else
2171                                                 return var.Resolve (ec);
2172                                 }
2173
2174                                 int idx = -1;
2175                                 Parameter par = null;
2176                                 Parameters pars = current_block.Parameters;
2177                                 if (pars != null)
2178                                         par = pars.GetParameterByName (Name, out idx);
2179
2180                                 if (par != null) {
2181                                         ParameterReference param;
2182                                         
2183                                         param = new ParameterReference (pars, current_block, idx, Name, loc);
2184
2185                                         if (right_side != null)
2186                                                 return param.ResolveLValue (ec, right_side);
2187                                         else
2188                                                 return param.Resolve (ec);
2189                                 }
2190                         }
2191                         
2192                         //
2193                         // Stage 2: Lookup members 
2194                         //
2195
2196                         DeclSpace lookup_ds = ec.DeclSpace;
2197                         do {
2198                                 if (lookup_ds.TypeBuilder == null)
2199                                         break;
2200
2201                                 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
2202                                 if (e != null)
2203                                         break;
2204
2205                                 lookup_ds =lookup_ds.Parent;
2206                         } while (lookup_ds != null);
2207
2208                         if (e == null && ec.ContainerType != null)
2209                                 e = MemberLookup (ec, ec.ContainerType, Name, loc);
2210
2211                         if (e == null) {
2212                                 //
2213                                 // Since we are cheating (is_base is our hint
2214                                 // that we are the beginning of the name): we
2215                                 // only do the Alias lookup for namespaces if
2216                                 // the name does not include any dots in it
2217                                 //
2218                                 NamespaceEntry ns = ec.DeclSpace.NamespaceEntry;
2219                                 if (is_base && ns != null){
2220                                         IAlias alias_value = ns.LookupAlias (Name);
2221                                         if (alias_value != null){
2222                                                 if (alias_value.IsType)
2223                                                         return alias_value.Type;
2224
2225                                                 Name = alias_value.Name;
2226                                                 Type t;
2227
2228                                                 if ((t = TypeManager.LookupType (Name)) != null)
2229                                                         return new TypeExpression (t, loc);
2230                                         
2231                                                 // No match, maybe our parent can compose us
2232                                                 // into something meaningful.
2233                                                 return this;
2234                                         }
2235                                 }
2236
2237                                 return ResolveAsTypeStep (ec);
2238                         }
2239
2240                         if (e is TypeExpr)
2241                                 return e;
2242
2243                         if (e is IMemberExpr) {
2244                                 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
2245                                 if (e == null)
2246                                         return null;
2247
2248                                 IMemberExpr me = e as IMemberExpr;
2249                                 if (me == null)
2250                                         return e;
2251
2252                                 if (Arguments != null) {
2253                                         MethodGroupExpr mg = me as MethodGroupExpr;
2254                                         if (mg == null)
2255                                                 return null;
2256
2257                                         return mg.ResolveGeneric (ec, Arguments);
2258                                 }
2259
2260                                 // This fails if ResolveMemberAccess() was unable to decide whether
2261                                 // it's a field or a type of the same name.
2262                                 
2263                                 if (!me.IsStatic && (me.InstanceExpression == null))
2264                                         return e;
2265
2266                                 if (!me.IsStatic &&
2267                                     TypeManager.IsNestedFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2268                                     me.InstanceExpression.Type != me.DeclaringType &&
2269                                     !TypeManager.IsFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2270                                     (!intermediate || !MemberAccess.IdenticalNameAndTypeName (ec, this, e, loc))) {
2271                                         Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
2272                                                "outer type `" + me.DeclaringType + "' via nested type `" +
2273                                                me.InstanceExpression.Type + "'");
2274                                         return null;
2275                                 }
2276
2277                                 return (right_side != null)
2278                                         ? e.DoResolveLValue (ec, right_side)
2279                                         : e.DoResolve (ec);
2280                         }
2281
2282                         if (ec.IsStatic || ec.IsFieldInitializer){
2283                                 if (allow_static)
2284                                         return e;
2285
2286                                 return MemberStaticCheck (ec, e);
2287                         } else
2288                                 return e;
2289                 }
2290                 
2291                 public override void Emit (EmitContext ec)
2292                 {
2293                         //
2294                         // If this is ever reached, then we failed to
2295                         // find the name as a namespace
2296                         //
2297
2298                         Error (103, "The name `" + Name +
2299                                "' does not exist in the class `" +
2300                                ec.DeclSpace.Name + "'");
2301                 }
2302
2303                 public override string ToString ()
2304                 {
2305                         return Name;
2306                 }
2307         }
2308         
2309         /// <summary>
2310         ///   Fully resolved expression that evaluates to a type
2311         /// </summary>
2312         public abstract class TypeExpr : Expression, IAlias {
2313                 override public Expression ResolveAsTypeStep (EmitContext ec)
2314                 {
2315                         TypeExpr t = DoResolveAsTypeStep (ec);
2316                         if (t == null)
2317                                 return null;
2318
2319                         eclass = ExprClass.Type;
2320                         return t;
2321                 }
2322
2323                 override public Expression DoResolve (EmitContext ec)
2324                 {
2325                         return ResolveAsTypeTerminal (ec);
2326                 }
2327
2328                 override public void Emit (EmitContext ec)
2329                 {
2330                         throw new Exception ("Should never be called");
2331                 }
2332
2333                 public virtual bool CheckAccessLevel (DeclSpace ds)
2334                 {
2335                         return ds.CheckAccessLevel (Type);
2336                 }
2337
2338                 public virtual bool AsAccessible (DeclSpace ds, int flags)
2339                 {
2340                         return ds.AsAccessible (Type, flags);
2341                 }
2342
2343                 public virtual bool IsClass {
2344                         get { return Type.IsClass; }
2345                 }
2346
2347                 public virtual bool IsValueType {
2348                         get { return Type.IsValueType; }
2349                 }
2350
2351                 public virtual bool IsInterface {
2352                         get { return Type.IsInterface; }
2353                 }
2354
2355                 public virtual bool IsSealed {
2356                         get { return Type.IsSealed; }
2357                 }
2358
2359                 public virtual bool CanInheritFrom ()
2360                 {
2361                         if (Type == TypeManager.enum_type ||
2362                             (Type == TypeManager.value_type && RootContext.StdLib) ||
2363                             Type == TypeManager.multicast_delegate_type ||
2364                             Type == TypeManager.delegate_type ||
2365                             Type == TypeManager.array_type)
2366                                 return false;
2367
2368                         return true;
2369                 }
2370
2371                 public virtual bool IsAttribute {
2372                         get {
2373                                 return Type == TypeManager.attribute_type ||
2374                                         Type.IsSubclassOf (TypeManager.attribute_type);
2375                         }
2376                 }
2377
2378                 public abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
2379
2380                 public virtual Type ResolveType (EmitContext ec)
2381                 {
2382                         TypeExpr t = ResolveAsTypeTerminal (ec);
2383                         if (t == null)
2384                                 return null;
2385
2386                         return t.Type;
2387                 }
2388
2389                 public abstract string Name {
2390                         get;
2391                 }
2392
2393                 public override bool Equals (object obj)
2394                 {
2395                         TypeExpr tobj = obj as TypeExpr;
2396                         if (tobj == null)
2397                                 return false;
2398
2399                         return Type == tobj.Type;
2400                 }
2401
2402                 public override int GetHashCode ()
2403                 {
2404                         return Type.GetHashCode ();
2405                 }
2406                 
2407                 public override string ToString ()
2408                 {
2409                         return Name;
2410                 }
2411
2412                 bool IAlias.IsType {
2413                         get { return true; }
2414                 }
2415
2416                 TypeExpr IAlias.Type {
2417                         get {
2418                                 return this;
2419                         }
2420                 }
2421         }
2422
2423         public class TypeExpression : TypeExpr, IAlias {
2424                 public TypeExpression (Type t, Location l)
2425                 {
2426                         Type = t;
2427                         eclass = ExprClass.Type;
2428                         loc = l;
2429                 }
2430
2431                 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2432                 {
2433                         return this;
2434                 }
2435
2436                 public override string Name {
2437                         get {
2438                                 return Type.ToString ();
2439                         }
2440                 }
2441
2442                 string IAlias.Name {
2443                         get {
2444                                 return Type.FullName != null ? Type.FullName : Type.Name;
2445                         }
2446                 }
2447         }
2448
2449         /// <summary>
2450         ///   Used to create types from a fully qualified name.  These are just used
2451         ///   by the parser to setup the core types.  A TypeLookupExpression is always
2452         ///   classified as a type.
2453         /// </summary>
2454         public class TypeLookupExpression : TypeExpr {
2455                 string name;
2456                 
2457                 public TypeLookupExpression (string name)
2458                 {
2459                         this.name = name;
2460                 }
2461
2462                 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2463                 {
2464                         if (type == null) {
2465                                 TypeExpr texpr = RootContext.LookupType (
2466                                         ec.DeclSpace, name, false, Location.Null);
2467                                 if (texpr == null)
2468                                         return null;
2469
2470                                 type = texpr.ResolveType (ec);
2471                                 if (type == null)
2472                                         return null;
2473                         }
2474
2475                         return this;
2476                 }
2477
2478                 public override string Name {
2479                         get {
2480                                 return name;
2481                         }
2482                 }
2483         }
2484
2485         /// <summary>
2486         ///   Represents an "unbound generic type", ie. typeof (Foo<>).
2487         ///   See 14.5.11.
2488         /// </summary>
2489         public class UnboundTypeExpression : TypeLookupExpression {
2490                 public UnboundTypeExpression (string name)
2491                         : base (name)
2492                 { }
2493         }
2494
2495         public class TypeAliasExpression : TypeExpr, IAlias {
2496                 TypeExpr texpr;
2497                 TypeArguments args;
2498                 string name;
2499
2500                 public TypeAliasExpression (TypeExpr texpr, TypeArguments args, Location l)
2501                 {
2502                         this.texpr = texpr;
2503                         this.args = args;
2504                         loc = texpr.Location;
2505
2506                         eclass = ExprClass.Type;
2507                         if (args != null)
2508                                 name = texpr.Name + "<" + args.ToString () + ">";
2509                         else
2510                                 name = texpr.Name;
2511                 }
2512
2513                 public override string Name {
2514                         get { return name; }
2515                 }
2516
2517                 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2518                 {
2519                         Type type = texpr.ResolveType (ec);
2520                         if (type == null)
2521                                 return null;
2522
2523                         int num_args = TypeManager.GetNumberOfTypeArguments (type);
2524
2525                         if (args != null) {
2526                                 if (num_args == 0) {
2527                                         Report.Error (308, loc,
2528                                                       "The non-generic type `{0}' cannot " +
2529                                                       "be used with type arguments.",
2530                                                       TypeManager.CSharpName (type));
2531                                         return null;
2532                                 }
2533
2534                                 ConstructedType ctype = new ConstructedType (type, args, loc);
2535                                 return ctype.ResolveAsTypeTerminal (ec);
2536                         } else if (num_args > 0) {
2537                                 Report.Error (305, loc,
2538                                               "Using the generic type `{0}' " +
2539                                               "requires {1} type arguments",
2540                                               TypeManager.GetFullName (type), num_args);
2541                                 return null;
2542                         }
2543
2544                         return new TypeExpression (type, loc);
2545                 }
2546
2547                 public override Type ResolveType (EmitContext ec)
2548                 {
2549                         TypeExpr t = ResolveAsTypeTerminal (ec);
2550                         if (t == null)
2551                                 return null;
2552
2553                         type = t.ResolveType (ec);
2554                         return type;
2555                 }
2556
2557                 public override bool CheckAccessLevel (DeclSpace ds)
2558                 {
2559                         return texpr.CheckAccessLevel (ds);
2560                 }
2561
2562                 public override bool AsAccessible (DeclSpace ds, int flags)
2563                 {
2564                         return texpr.AsAccessible (ds, flags);
2565                 }
2566
2567                 public override bool IsClass {
2568                         get { return texpr.IsClass; }
2569                 }
2570
2571                 public override bool IsValueType {
2572                         get { return texpr.IsValueType; }
2573                 }
2574
2575                 public override bool IsInterface {
2576                         get { return texpr.IsInterface; }
2577                 }
2578
2579                 public override bool IsSealed {
2580                         get { return texpr.IsSealed; }
2581                 }
2582
2583                 public override bool IsAttribute {
2584                         get { return texpr.IsAttribute; }
2585                 }
2586         }
2587
2588         /// <summary>
2589         ///   MethodGroup Expression.
2590         ///  
2591         ///   This is a fully resolved expression that evaluates to a type
2592         /// </summary>
2593         public class MethodGroupExpr : Expression, IMemberExpr {
2594                 public MethodBase [] Methods;
2595                 Expression instance_expression = null;
2596                 bool is_explicit_impl = false;
2597                 bool has_type_arguments = false;
2598                 bool identical_type_name = false;
2599                 bool is_base;
2600                 
2601                 public MethodGroupExpr (MemberInfo [] mi, Location l)
2602                 {
2603                         Methods = new MethodBase [mi.Length];
2604                         mi.CopyTo (Methods, 0);
2605                         eclass = ExprClass.MethodGroup;
2606                         type = TypeManager.object_type;
2607                         loc = l;
2608                 }
2609
2610                 public MethodGroupExpr (ArrayList list, Location l)
2611                 {
2612                         Methods = new MethodBase [list.Count];
2613
2614                         try {
2615                                 list.CopyTo (Methods, 0);
2616                         } catch {
2617                                 foreach (MemberInfo m in list){
2618                                         if (!(m is MethodBase)){
2619                                                 Console.WriteLine ("Name " + m.Name);
2620                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2621                                         }
2622                                 }
2623                                 throw;
2624                         }
2625
2626                         loc = l;
2627                         eclass = ExprClass.MethodGroup;
2628                         type = TypeManager.object_type;
2629                 }
2630
2631                 public Type DeclaringType {
2632                         get {
2633                                 //
2634                                 // We assume that the top-level type is in the end
2635                                 //
2636                                 return Methods [Methods.Length - 1].DeclaringType;
2637                                 //return Methods [0].DeclaringType;
2638                         }
2639                 }
2640                 
2641                 //
2642                 // `A method group may have associated an instance expression' 
2643                 // 
2644                 public Expression InstanceExpression {
2645                         get {
2646                                 return instance_expression;
2647                         }
2648
2649                         set {
2650                                 instance_expression = value;
2651                         }
2652                 }
2653
2654                 public bool IsExplicitImpl {
2655                         get {
2656                                 return is_explicit_impl;
2657                         }
2658
2659                         set {
2660                                 is_explicit_impl = value;
2661                         }
2662                 }
2663
2664                 public bool HasTypeArguments {
2665                         get {
2666                                 return has_type_arguments;
2667                         }
2668
2669                         set {
2670                                 has_type_arguments = value;
2671                         }
2672                 }
2673
2674                 public bool IdenticalTypeName {
2675                         get {
2676                                 return identical_type_name;
2677                         }
2678
2679                         set {
2680                                 identical_type_name = value;
2681                         }
2682                 }
2683
2684                 public bool IsBase {
2685                         get {
2686                                 return is_base;
2687                         }
2688                         set {
2689                                 is_base = value;
2690                         }
2691                 }
2692
2693                 public string Name {
2694                         get {
2695                                 //return Methods [0].Name;
2696                                 return Methods [Methods.Length - 1].Name;
2697                         }
2698                 }
2699
2700                 public bool IsInstance {
2701                         get {
2702                                 foreach (MethodBase mb in Methods)
2703                                         if (!mb.IsStatic)
2704                                                 return true;
2705
2706                                 return false;
2707                         }
2708                 }
2709
2710                 public bool IsStatic {
2711                         get {
2712                                 foreach (MethodBase mb in Methods)
2713                                         if (mb.IsStatic)
2714                                                 return true;
2715
2716                                 return false;
2717                         }
2718                 }
2719                 
2720                 override public Expression DoResolve (EmitContext ec)
2721                 {
2722                         if (!IsInstance)
2723                                 instance_expression = null;
2724
2725                         if (instance_expression != null) {
2726                                 instance_expression = instance_expression.DoResolve (ec);
2727                                 if (instance_expression == null)
2728                                         return null;
2729                         }
2730
2731                         return this;
2732                 }
2733
2734                 public void ReportUsageError ()
2735                 {
2736                         Report.Error (654, loc, "Method `" + DeclaringType + "." +
2737                                       Name + "()' is referenced without parentheses");
2738                 }
2739
2740                 override public void Emit (EmitContext ec)
2741                 {
2742                         ReportUsageError ();
2743                 }
2744
2745                 bool RemoveMethods (bool keep_static)
2746                 {
2747                         ArrayList smethods = new ArrayList ();
2748
2749                         foreach (MethodBase mb in Methods){
2750                                 if (mb.IsStatic == keep_static)
2751                                         smethods.Add (mb);
2752                         }
2753
2754                         if (smethods.Count == 0)
2755                                 return false;
2756
2757                         Methods = new MethodBase [smethods.Count];
2758                         smethods.CopyTo (Methods, 0);
2759
2760                         return true;
2761                 }
2762                 
2763                 /// <summary>
2764                 ///   Removes any instance methods from the MethodGroup, returns
2765                 ///   false if the resulting set is empty.
2766                 /// </summary>
2767                 public bool RemoveInstanceMethods ()
2768                 {
2769                         return RemoveMethods (true);
2770                 }
2771
2772                 /// <summary>
2773                 ///   Removes any static methods from the MethodGroup, returns
2774                 ///   false if the resulting set is empty.
2775                 /// </summary>
2776                 public bool RemoveStaticMethods ()
2777                 {
2778                         return RemoveMethods (false);
2779                 }
2780
2781                 public Expression ResolveGeneric (EmitContext ec, TypeArguments args)
2782                 {
2783                         if (args.Resolve (ec) == false)
2784                                 return null;
2785
2786                         Type[] atypes = args.Arguments;
2787
2788                         int first_count = 0;
2789                         MethodInfo first = null;
2790
2791                         ArrayList list = new ArrayList ();
2792                         foreach (MethodBase mb in Methods) {
2793                                 MethodInfo mi = mb as MethodInfo;
2794                                 if ((mi == null) || !mi.HasGenericParameters)
2795                                         continue;
2796
2797                                 Type[] gen_params = mi.GetGenericArguments ();
2798
2799                                 if (first == null) {
2800                                         first = mi;
2801                                         first_count = gen_params.Length;
2802                                 }
2803
2804                                 if (gen_params.Length != atypes.Length)
2805                                         continue;
2806
2807                                 list.Add (mi.BindGenericParameters (atypes));
2808                         }
2809
2810                         if (list.Count > 0) {
2811                                 MethodGroupExpr new_mg = new MethodGroupExpr (list, Location);
2812                                 new_mg.InstanceExpression = InstanceExpression;
2813                                 new_mg.HasTypeArguments = true;
2814                                 return new_mg;
2815                         }
2816
2817                         if (first != null)
2818                                 Report.Error (
2819                                         305, loc, "Using the generic method `{0}' " +
2820                                         "requires {1} type arguments", Name,
2821                                         first_count);
2822                         else
2823                                 Report.Error (
2824                                         308, loc, "The non-generic method `{0}' " +
2825                                         "cannot be used with type arguments", Name);
2826
2827                         return null;
2828                 }
2829         }
2830
2831         /// <summary>
2832         ///   Fully resolved expression that evaluates to a Field
2833         /// </summary>
2834         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr, IVariable {
2835                 public readonly FieldInfo FieldInfo;
2836                 Expression instance_expr;
2837                 VariableInfo variable_info;
2838                 
2839                 LocalTemporary temp;
2840                 bool prepared;
2841                 bool is_field_initializer;
2842                 
2843                 public FieldExpr (FieldInfo fi, Location l)
2844                 {
2845                         FieldInfo = fi;
2846                         eclass = ExprClass.Variable;
2847                         type = TypeManager.TypeToCoreType (fi.FieldType);
2848                         loc = l;
2849                 }
2850
2851                 public string Name {
2852                         get {
2853                                 return FieldInfo.Name;
2854                         }
2855                 }
2856
2857                 public bool IsInstance {
2858                         get {
2859                                 return !FieldInfo.IsStatic;
2860                         }
2861                 }
2862
2863                 public bool IsStatic {
2864                         get {
2865                                 return FieldInfo.IsStatic;
2866                         }
2867                 }
2868
2869                 public Type DeclaringType {
2870                         get {
2871                                 return FieldInfo.DeclaringType;
2872                         }
2873                 }
2874
2875                 public Expression InstanceExpression {
2876                         get {
2877                                 return instance_expr;
2878                         }
2879
2880                         set {
2881                                 instance_expr = value;
2882                         }
2883                 }
2884
2885                 public bool IsFieldInitializer {
2886                         get {
2887                                 return is_field_initializer;
2888                         }
2889
2890                         set {
2891                                 is_field_initializer = value;
2892                         }
2893                 }
2894
2895                 public VariableInfo VariableInfo {
2896                         get {
2897                                 return variable_info;
2898                         }
2899                 }
2900
2901                 override public Expression DoResolve (EmitContext ec)
2902                 {
2903                         if (!FieldInfo.IsStatic){
2904                                 if (instance_expr == null){
2905                                         //
2906                                         // This can happen when referencing an instance field using
2907                                         // a fully qualified type expression: TypeName.InstanceField = xxx
2908                                         // 
2909                                         SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
2910                                         return null;
2911                                 }
2912
2913                                 // Resolve the field's instance expression while flow analysis is turned
2914                                 // off: when accessing a field "a.b", we must check whether the field
2915                                 // "a.b" is initialized, not whether the whole struct "a" is initialized.
2916                                 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
2917                                                                        ResolveFlags.DisableFlowAnalysis);
2918                                 if (instance_expr == null)
2919                                         return null;
2920                         }
2921
2922                         ObsoleteAttribute oa;
2923                         FieldBase f = TypeManager.GetField (FieldInfo);
2924                         if (f != null) {
2925                                 oa = f.GetObsoleteAttribute (f.Parent);
2926                                 if (oa != null)
2927                                         AttributeTester.Report_ObsoleteMessage (oa, f.GetSignatureForError (), loc);
2928                                 // To be sure that type is external because we do not register generated fields
2929                         } else if (!(FieldInfo.DeclaringType is TypeBuilder)) {                                
2930                                 oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo);
2931                                 if (oa != null)
2932                                         AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc);
2933                         }
2934
2935                         // If the instance expression is a local variable or parameter.
2936                         IVariable var = instance_expr as IVariable;
2937                         if ((var == null) || (var.VariableInfo == null))
2938                                 return this;
2939
2940                         VariableInfo vi = var.VariableInfo;
2941                         if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
2942                                 return null;
2943
2944                         variable_info = vi.GetSubStruct (FieldInfo.Name);
2945                         return this;
2946                 }
2947
2948                 void Report_AssignToReadonly (bool is_instance)
2949                 {
2950                         string msg;
2951                         
2952                         if (is_instance)
2953                                 msg = "Readonly field can not be assigned outside " +
2954                                 "of constructor or variable initializer";
2955                         else
2956                                 msg = "A static readonly field can only be assigned in " +
2957                                 "a static constructor";
2958
2959                         Report.Error (is_instance ? 191 : 198, loc, msg);
2960                 }
2961                 
2962                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2963                 {
2964                         IVariable var = instance_expr as IVariable;
2965                         if ((var != null) && (var.VariableInfo != null))
2966                                 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
2967
2968                         Expression e = DoResolve (ec);
2969
2970                         if (e == null)
2971                                 return null;
2972
2973                         if (!FieldInfo.IsStatic && (instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation))) {
2974                                 // FIXME: Provide better error reporting.
2975                                 Error (1612, "Cannot modify expression because it is not a variable.");
2976                                 return null;
2977                         }
2978
2979                         if (!FieldInfo.IsInitOnly)
2980                                 return this;
2981
2982                         FieldBase fb = TypeManager.GetField (FieldInfo);
2983                         if (fb != null)
2984                                 fb.SetAssigned ();
2985
2986                         //
2987                         // InitOnly fields can only be assigned in constructors
2988                         //
2989
2990                         if (ec.IsConstructor){
2991                                 if (IsStatic && !ec.IsStatic)
2992                                         Report_AssignToReadonly (false);
2993
2994                                 Type ctype;
2995                                 if (!is_field_initializer &&
2996                                     (ec.TypeContainer.CurrentType != null))
2997                                         ctype = ec.TypeContainer.CurrentType.ResolveType (ec);
2998                                 else
2999                                         ctype = ec.ContainerType;
3000
3001                                 if (TypeManager.IsEqual (ctype, FieldInfo.DeclaringType))
3002                                         return this;
3003                         }
3004
3005                         Report_AssignToReadonly (!IsStatic);
3006                         
3007                         return null;
3008                 }
3009
3010                 public bool VerifyFixed (bool is_expression)
3011                 {
3012                         IVariable variable = instance_expr as IVariable;
3013                         if ((variable == null) || !variable.VerifyFixed (true))
3014                                 return false;
3015
3016                         return true;
3017                 }
3018                 
3019                 public void Emit (EmitContext ec, bool leave_copy)
3020                 {
3021                         ILGenerator ig = ec.ig;
3022                         bool is_volatile = false;
3023
3024                         if (FieldInfo is FieldBuilder){
3025                                 FieldBase f = TypeManager.GetField (FieldInfo);
3026                                 if (f != null){
3027                                         if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3028                                                 is_volatile = true;
3029                                         
3030                                         f.status |= Field.Status.USED;
3031                                 }
3032                         } 
3033                         
3034                         if (FieldInfo.IsStatic){
3035                                 if (is_volatile)
3036                                         ig.Emit (OpCodes.Volatile);
3037                                 
3038                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3039                         } else {
3040                                 if (!prepared)
3041                                         EmitInstance (ec);
3042
3043                                 if (is_volatile)
3044                                         ig.Emit (OpCodes.Volatile);
3045
3046                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
3047                         }
3048
3049                         if (leave_copy) {
3050                                 ec.ig.Emit (OpCodes.Dup);
3051                                 if (!FieldInfo.IsStatic) {
3052                                         temp = new LocalTemporary (ec, this.Type);
3053                                         temp.Store (ec);
3054                                 }
3055                         }
3056                 }
3057
3058                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3059                 {
3060                         FieldAttributes fa = FieldInfo.Attributes;
3061                         bool is_static = (fa & FieldAttributes.Static) != 0;
3062                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3063                         ILGenerator ig = ec.ig;
3064                         prepared = prepare_for_load;
3065
3066                         if (is_readonly && !ec.IsConstructor){
3067                                 Report_AssignToReadonly (!is_static);
3068                                 return;
3069                         }
3070
3071                         if (!is_static) {
3072                                 EmitInstance (ec);
3073                                 if (prepare_for_load)
3074                                         ig.Emit (OpCodes.Dup);
3075                         }
3076
3077                         source.Emit (ec);
3078                         if (leave_copy) {
3079                                 ec.ig.Emit (OpCodes.Dup);
3080                                 if (!FieldInfo.IsStatic) {
3081                                         temp = new LocalTemporary (ec, this.Type);
3082                                         temp.Store (ec);
3083                                 }
3084                         }
3085
3086                         if (FieldInfo is FieldBuilder){
3087                                 FieldBase f = TypeManager.GetField (FieldInfo);
3088                                 if (f != null){
3089                                         if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3090                                                 ig.Emit (OpCodes.Volatile);
3091                                         
3092                                         f.status |= Field.Status.ASSIGNED;
3093                                 }
3094                         } 
3095
3096                         if (is_static)
3097                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3098                         else 
3099                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3100                         
3101                         if (temp != null)
3102                                 temp.Emit (ec);
3103                 }
3104
3105                 void EmitInstance (EmitContext ec)
3106                 {
3107                         if (instance_expr.Type.IsValueType) {
3108                                 if (instance_expr is IMemoryLocation) {
3109                                         ((IMemoryLocation) instance_expr).AddressOf (ec, AddressOp.LoadStore);
3110                                 } else {
3111                                         LocalTemporary t = new LocalTemporary (ec, instance_expr.Type);
3112                                         instance_expr.Emit (ec);
3113                                         t.Store (ec);
3114                                         t.AddressOf (ec, AddressOp.Store);
3115                                 }
3116                         } else
3117                                 instance_expr.Emit (ec);
3118                 }
3119
3120                 public override void Emit (EmitContext ec)
3121                 {
3122                         Emit (ec, false);
3123                 }
3124                 
3125                 public void AddressOf (EmitContext ec, AddressOp mode)
3126                 {
3127                         ILGenerator ig = ec.ig;
3128                         
3129                         if (FieldInfo is FieldBuilder){
3130                                 FieldBase f = TypeManager.GetField (FieldInfo);
3131                                 if (f != null){
3132                                         if ((f.ModFlags & Modifiers.VOLATILE) != 0){
3133                                                 Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
3134                                                 return;
3135                                         }
3136                                         
3137                                         if ((mode & AddressOp.Store) != 0)
3138                                                 f.status |= Field.Status.ASSIGNED;
3139                                         if ((mode & AddressOp.Load) != 0)
3140                                                 f.status |= Field.Status.USED;
3141                                 }
3142                         } 
3143
3144                         //
3145                         // Handle initonly fields specially: make a copy and then
3146                         // get the address of the copy.
3147                         //
3148                         bool need_copy;
3149                         if (FieldInfo.IsInitOnly){
3150                                 need_copy = true;
3151                                 if (ec.IsConstructor){
3152                                         if (FieldInfo.IsStatic){
3153                                                 if (ec.IsStatic)
3154                                                         need_copy = false;
3155                                         } else
3156                                                 need_copy = false;
3157                                 }
3158                         } else
3159                                 need_copy = false;
3160                         
3161                         if (need_copy){
3162                                 LocalBuilder local;
3163                                 Emit (ec);
3164                                 local = ig.DeclareLocal (type);
3165                                 ig.Emit (OpCodes.Stloc, local);
3166                                 ig.Emit (OpCodes.Ldloca, local);
3167                                 return;
3168                         }
3169
3170
3171                         if (FieldInfo.IsStatic){
3172                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3173                         } else {
3174                                 EmitInstance (ec);
3175                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
3176                         }
3177                 }
3178         }
3179
3180         //
3181         // A FieldExpr whose address can not be taken
3182         //
3183         public class FieldExprNoAddress : FieldExpr, IMemoryLocation {
3184                 public FieldExprNoAddress (FieldInfo fi, Location loc) : base (fi, loc)
3185                 {
3186                 }
3187                 
3188                 public new void AddressOf (EmitContext ec, AddressOp mode)
3189                 {
3190                         Report.Error (-215, "Report this: Taking the address of a remapped parameter not supported");
3191                 }
3192         }
3193         
3194         /// <summary>
3195         ///   Expression that evaluates to a Property.  The Assign class
3196         ///   might set the `Value' expression if we are in an assignment.
3197         ///
3198         ///   This is not an LValue because we need to re-write the expression, we
3199         ///   can not take data from the stack and store it.  
3200         /// </summary>
3201         public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
3202                 public readonly PropertyInfo PropertyInfo;
3203
3204                 //
3205                 // This is set externally by the  `BaseAccess' class
3206                 //
3207                 public bool IsBase;
3208                 MethodInfo getter, setter;
3209                 bool is_static;
3210                 bool must_do_cs1540_check;
3211                 
3212                 Expression instance_expr;
3213                 LocalTemporary temp;
3214                 bool prepared;
3215
3216                 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
3217                 {
3218                         PropertyInfo = pi;
3219                         eclass = ExprClass.PropertyAccess;
3220                         is_static = false;
3221                         loc = l;
3222
3223                         type = TypeManager.TypeToCoreType (pi.PropertyType);
3224
3225                         ResolveAccessors (ec);
3226                 }
3227
3228                 public string Name {
3229                         get {
3230                                 return PropertyInfo.Name;
3231                         }
3232                 }
3233
3234                 public bool IsInstance {
3235                         get {
3236                                 return !is_static;
3237                         }
3238                 }
3239
3240                 public bool IsStatic {
3241                         get {
3242                                 return is_static;
3243                         }
3244                 }
3245                 
3246                 public Type DeclaringType {
3247                         get {
3248                                 return PropertyInfo.DeclaringType;
3249                         }
3250                 }
3251
3252                 //
3253                 // The instance expression associated with this expression
3254                 //
3255                 public Expression InstanceExpression {
3256                         set {
3257                                 instance_expr = value;
3258                         }
3259
3260                         get {
3261                                 return instance_expr;
3262                         }
3263                 }
3264
3265                 public bool VerifyAssignable ()
3266                 {
3267                         if (setter == null) {
3268                                 Report.Error (200, loc, 
3269                                               "The property `" + PropertyInfo.Name +
3270                                               "' can not be assigned to, as it has not set accessor");
3271                                 return false;
3272                         }
3273
3274                         return true;
3275                 }
3276
3277                 void FindAccessors (Type invocation_type)
3278                 {
3279                         BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
3280                                 BindingFlags.Static | BindingFlags.Instance |
3281                                 BindingFlags.DeclaredOnly;
3282
3283                         Type current = PropertyInfo.DeclaringType;
3284                         for (; current != null; current = current.BaseType) {
3285                                 MemberInfo[] group = TypeManager.MemberLookup (
3286                                         invocation_type, invocation_type, current,
3287                                         MemberTypes.Property, flags, PropertyInfo.Name, null);
3288
3289                                 if (group == null)
3290                                         continue;
3291
3292                                 if (group.Length != 1)
3293                                         // Oooops, can this ever happen ?
3294                                         return;
3295
3296                                 PropertyInfo pi = (PropertyInfo) group [0];
3297
3298                                 if (getter == null)
3299                                         getter = pi.GetGetMethod (true);;
3300
3301                                 if (setter == null)
3302                                         setter = pi.GetSetMethod (true);;
3303
3304                                 MethodInfo accessor = getter != null ? getter : setter;
3305
3306                                 if (!accessor.IsVirtual)
3307                                         return;
3308                         }
3309                 }
3310
3311                 bool IsAccessorAccessible (Type invocation_type, MethodInfo mi)
3312                 {
3313                         MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
3314
3315                         //
3316                         // If only accessible to the current class or children
3317                         //
3318                         if (ma == MethodAttributes.Private) {
3319                                 Type declaring_type = mi.DeclaringType;
3320
3321                                 if (invocation_type != declaring_type)
3322                                         return TypeManager.IsNestedFamilyAccessible (invocation_type, declaring_type);
3323
3324                                 return true;
3325                         }
3326                         //
3327                         // FamAndAssem requires that we not only derivate, but we are on the
3328                         // same assembly.  
3329                         //
3330                         if (ma == MethodAttributes.FamANDAssem){
3331                                 return (mi.DeclaringType.Assembly != invocation_type.Assembly);
3332                         }
3333
3334                         // Assembly and FamORAssem succeed if we're in the same assembly.
3335                         if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
3336                                 if (mi.DeclaringType.Assembly == invocation_type.Assembly)
3337                                         return true;
3338                         }
3339
3340                         // We already know that we aren't in the same assembly.
3341                         if (ma == MethodAttributes.Assembly)
3342                                 return false;
3343
3344                         // Family and FamANDAssem require that we derive.
3345                         if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem) || (ma == MethodAttributes.FamORAssem)){
3346                                 if (!TypeManager.IsNestedFamilyAccessible (invocation_type, mi.DeclaringType))
3347                                         return false;
3348                                 else {
3349                                         if (!TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType))
3350                                                 must_do_cs1540_check = true;
3351
3352                                         return true;
3353                                 }
3354                         }
3355
3356                         return true;
3357                 }
3358
3359                 //
3360                 // We also perform the permission checking here, as the PropertyInfo does not
3361                 // hold the information for the accessibility of its setter/getter
3362                 //
3363                 void ResolveAccessors (EmitContext ec)
3364                 {
3365                         FindAccessors (ec.ContainerType);
3366
3367                         if (setter != null && !IsAccessorAccessible (ec.ContainerType, setter) ||
3368                             getter != null && !IsAccessorAccessible (ec.ContainerType, getter)) {
3369                                 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", PropertyInfo.Name);
3370                         }
3371
3372                         is_static = getter != null ? getter.IsStatic : setter.IsStatic;
3373                 }
3374
3375                 bool InstanceResolve (EmitContext ec)
3376                 {
3377                         if ((instance_expr == null) && ec.IsStatic && !is_static) {
3378                                 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
3379                                 return false;
3380                         }
3381
3382                         if (instance_expr != null) {
3383                                 instance_expr = instance_expr.DoResolve (ec);
3384                                 if (instance_expr == null)
3385                                         return false;
3386                         }
3387
3388                         if (must_do_cs1540_check && (instance_expr != null)) {
3389                                 if ((instance_expr.Type != ec.ContainerType) &&
3390                                     ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
3391                                         Report.Error (1540, loc, "Cannot access protected member `" +
3392                                                       PropertyInfo.DeclaringType + "." + PropertyInfo.Name + 
3393                                                       "' via a qualifier of type `" +
3394                                                       TypeManager.CSharpName (instance_expr.Type) +
3395                                                       "'; the qualifier must be of type `" +
3396                                                       TypeManager.CSharpName (ec.ContainerType) +
3397                                                       "' (or derived from it)");
3398                                         return false;
3399                                 }
3400                         }
3401
3402                         return true;
3403                 }
3404                 
3405                 override public Expression DoResolve (EmitContext ec)
3406                 {
3407                         if (getter != null){
3408                                 if (TypeManager.GetArgumentTypes (getter).Length != 0){
3409                                         Report.Error (
3410                                                 117, loc, "`{0}' does not contain a " +
3411                                                 "definition for `{1}'.", getter.DeclaringType,
3412                                                 Name);
3413                                         return null;
3414                                 }
3415                         }
3416
3417                         if (getter == null){
3418                                 //
3419                                 // The following condition happens if the PropertyExpr was
3420                                 // created, but is invalid (ie, the property is inaccessible),
3421                                 // and we did not want to embed the knowledge about this in
3422                                 // the caller routine.  This only avoids double error reporting.
3423                                 //
3424                                 if (setter == null)
3425                                         return null;
3426                                 
3427                                 Report.Error (154, loc, 
3428                                               "The property `" + PropertyInfo.Name +
3429                                               "' can not be used in " +
3430                                               "this context because it lacks a get accessor");
3431                                 return null;
3432                         } 
3433
3434                         if (!InstanceResolve (ec))
3435                                 return null;
3436
3437                         //
3438                         // Only base will allow this invocation to happen.
3439                         //
3440                         if (IsBase && getter.IsAbstract){
3441                                 Report.Error (205, loc, "Cannot call an abstract base property: " +
3442                                               PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3443                                 return null;
3444                         }
3445
3446                         return this;
3447                 }
3448
3449                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3450                 {
3451                         if (setter == null){
3452                                 //
3453                                 // The following condition happens if the PropertyExpr was
3454                                 // created, but is invalid (ie, the property is inaccessible),
3455                                 // and we did not want to embed the knowledge about this in
3456                                 // the caller routine.  This only avoids double error reporting.
3457                                 //
3458                                 if (getter == null)
3459                                         return null;
3460                                 
3461                                 Report.Error (154, loc, 
3462                                               "The property `" + PropertyInfo.Name +
3463                                               "' can not be used in " +
3464                                               "this context because it lacks a set accessor");
3465                                 return null;
3466                         }
3467
3468                         if (TypeManager.GetArgumentTypes (setter).Length != 1){
3469                                 Report.Error (
3470                                         117, loc, "`{0}' does not contain a " +
3471                                         "definition for `{1}'.", getter.DeclaringType,
3472                                         Name);
3473                                 return null;
3474                         }
3475
3476                         if (!InstanceResolve (ec))
3477                                 return null;
3478                         
3479                         //
3480                         // Only base will allow this invocation to happen.
3481                         //
3482                         if (IsBase && setter.IsAbstract){
3483                                 Report.Error (205, loc, "Cannot call an abstract base property: " +
3484                                               PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3485                                 return null;
3486                         }
3487
3488                         //
3489                         // Check that we are not making changes to a temporary memory location
3490                         //
3491                         if (instance_expr != null && instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation)) {
3492                                 // FIXME: Provide better error reporting.
3493                                 Error (1612, "Cannot modify expression because it is not a variable.");
3494                                 return null;
3495                         }
3496
3497                         return this;
3498                 }
3499
3500
3501                 
3502                 public override void Emit (EmitContext ec)
3503                 {
3504                         Emit (ec, false);
3505                 }
3506                 
3507                 void EmitInstance (EmitContext ec)
3508                 {
3509                         if (is_static)
3510                                 return;
3511
3512                         if (instance_expr.Type.IsValueType) {
3513                                 if (instance_expr is IMemoryLocation) {
3514                                         ((IMemoryLocation) instance_expr).AddressOf (ec, AddressOp.LoadStore);
3515                                 } else {
3516                                         LocalTemporary t = new LocalTemporary (ec, instance_expr.Type);
3517                                         instance_expr.Emit (ec);
3518                                         t.Store (ec);
3519                                         t.AddressOf (ec, AddressOp.Store);
3520                                 }
3521                         } else
3522                                 instance_expr.Emit (ec);
3523                         
3524                         if (prepared)
3525                                 ec.ig.Emit (OpCodes.Dup);
3526                 }
3527
3528                 
3529                 public void Emit (EmitContext ec, bool leave_copy)
3530                 {
3531                         if (!prepared)
3532                                 EmitInstance (ec);
3533                         
3534                         //
3535                         // Special case: length of single dimension array property is turned into ldlen
3536                         //
3537                         if ((getter == TypeManager.system_int_array_get_length) ||
3538                             (getter == TypeManager.int_array_get_length)){
3539                                 Type iet = instance_expr.Type;
3540
3541                                 //
3542                                 // System.Array.Length can be called, but the Type does not
3543                                 // support invoking GetArrayRank, so test for that case first
3544                                 //
3545                                 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)) {
3546                                         ec.ig.Emit (OpCodes.Ldlen);
3547                                         ec.ig.Emit (OpCodes.Conv_I4);
3548                                         return;
3549                                 }
3550                         }
3551
3552                         Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), getter, null, loc);
3553                         
3554                         if (!leave_copy)
3555                                 return;
3556                         
3557                         ec.ig.Emit (OpCodes.Dup);
3558                         if (!is_static) {
3559                                 temp = new LocalTemporary (ec, this.Type);
3560                                 temp.Store (ec);
3561                         }
3562                 }
3563
3564                 //
3565                 // Implements the IAssignMethod interface for assignments
3566                 //
3567                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3568                 {
3569                         prepared = prepare_for_load;
3570                         
3571                         EmitInstance (ec);
3572
3573                         source.Emit (ec);
3574                         if (leave_copy) {
3575                                 ec.ig.Emit (OpCodes.Dup);
3576                                 if (!is_static) {
3577                                         temp = new LocalTemporary (ec, this.Type);
3578                                         temp.Store (ec);
3579                                 }
3580                         }
3581                         
3582                         ArrayList args = new ArrayList (1);
3583                         args.Add (new Argument (new EmptyAddressOf (), Argument.AType.Expression));
3584                         
3585                         Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), setter, args, loc);
3586                         
3587                         if (temp != null)
3588                                 temp.Emit (ec);
3589                 }
3590
3591                 override public void EmitStatement (EmitContext ec)
3592                 {
3593                         Emit (ec);
3594                         ec.ig.Emit (OpCodes.Pop);
3595                 }
3596         }
3597
3598         /// <summary>
3599         ///   Fully resolved expression that evaluates to an Event
3600         /// </summary>
3601         public class EventExpr : Expression, IMemberExpr {
3602                 public readonly EventInfo EventInfo;
3603                 Expression instance_expr;
3604
3605                 bool is_static;
3606                 MethodInfo add_accessor, remove_accessor;
3607                 
3608                 public EventExpr (EventInfo ei, Location loc)
3609                 {
3610                         EventInfo = ei;
3611                         this.loc = loc;
3612                         eclass = ExprClass.EventAccess;
3613
3614                         add_accessor = TypeManager.GetAddMethod (ei);
3615                         remove_accessor = TypeManager.GetRemoveMethod (ei);
3616                         
3617                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
3618                                 is_static = true;
3619
3620                         if (EventInfo is MyEventBuilder){
3621                                 MyEventBuilder eb = (MyEventBuilder) EventInfo;
3622                                 type = eb.EventType;
3623                                 eb.SetUsed ();
3624                         } else
3625                                 type = EventInfo.EventHandlerType;
3626                 }
3627
3628                 public string Name {
3629                         get {
3630                                 return EventInfo.Name;
3631                         }
3632                 }
3633
3634                 public bool IsInstance {
3635                         get {
3636                                 return !is_static;
3637                         }
3638                 }
3639
3640                 public bool IsStatic {
3641                         get {
3642                                 return is_static;
3643                         }
3644                 }
3645
3646                 public Type DeclaringType {
3647                         get {
3648                                 return EventInfo.DeclaringType;
3649                         }
3650                 }
3651
3652                 public Expression InstanceExpression {
3653                         get {
3654                                 return instance_expr;
3655                         }
3656
3657                         set {
3658                                 instance_expr = value;
3659                         }
3660                 }
3661
3662                 public override Expression DoResolve (EmitContext ec)
3663                 {
3664                         if (instance_expr != null) {
3665                                 instance_expr = instance_expr.DoResolve (ec);
3666                                 if (instance_expr == null)
3667                                         return null;
3668                         }
3669
3670                         
3671                         return this;
3672                 }
3673
3674                 public override void Emit (EmitContext ec)
3675                 {
3676                         Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
3677                 }
3678
3679                 public void EmitAddOrRemove (EmitContext ec, Expression source)
3680                 {
3681                         BinaryDelegate source_del = (BinaryDelegate) source;
3682                         Expression handler = source_del.Right;
3683                         
3684                         Argument arg = new Argument (handler, Argument.AType.Expression);
3685                         ArrayList args = new ArrayList ();
3686                                 
3687                         args.Add (arg);
3688                         
3689                         if (source_del.IsAddition)
3690                                 Invocation.EmitCall (
3691                                         ec, false, IsStatic, instance_expr, add_accessor, args, loc);
3692                         else
3693                                 Invocation.EmitCall (
3694                                         ec, false, IsStatic, instance_expr, remove_accessor, args, loc);
3695                 }
3696         }
3697 }