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