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