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