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