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