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