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