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