2001-12-22 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                 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 AllBindingsFlags =
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, AllBindingsFlags, 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 c;
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 (object) ((uint) v);
1821                                 } else if (target_type == TypeManager.char_type){
1822                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1823                                                 return (object) ((char) v);
1824                                 } else if (target_type == TypeManager.byte_type){
1825                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1826                                                 return (object) ((byte) v);
1827                                 } else if (target_type == TypeManager.sbyte_type){
1828                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
1829                                                 return (object) ((sbyte) v);
1830                                 } else if (target_type == TypeManager.short_type){
1831                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1832                                                 return (object) ((short) v);
1833                                 } else if (target_type == TypeManager.ushort_type){
1834                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1835                                                 return (object) ((ushort) v);
1836                                 } else if (target_type == TypeManager.int64_type)
1837                                         return (object) ((long) v);
1838                                 else if (target_type == TypeManager.uint64_type){
1839                                         if (v > 0)
1840                                                 return (object) ((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 (object) ((int) v);
1850                                 } else if (target_type == TypeManager.char_type){
1851                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1852                                                 return (object) ((char) v);
1853                                 } else if (target_type == TypeManager.byte_type){
1854                                         if (v <= Byte.MaxValue)
1855                                                 return (object) ((byte) v);
1856                                 } else if (target_type == TypeManager.sbyte_type){
1857                                         if (v <= SByte.MaxValue)
1858                                                 return (object) ((sbyte) v);
1859                                 } else if (target_type == TypeManager.short_type){
1860                                         if (v <= UInt16.MaxValue)
1861                                                 return (object) ((short) v);
1862                                 } else if (target_type == TypeManager.ushort_type){
1863                                         if (v <= UInt16.MaxValue)
1864                                                 return (object) ((ushort) v);
1865                                 } else if (target_type == TypeManager.int64_type)
1866                                         return (object) ((long) v);
1867                                 else if (target_type == TypeManager.uint64_type)
1868                                         return (object) ((ulong) v);
1869                                 s = v.ToString ();
1870                         } else if (c is LongLiteral){ 
1871                                 long v = ((LongLiteral) c).Value;
1872
1873                                 if (target_type == TypeManager.int32_type){
1874                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1875                                                 return (object) ((int) v);
1876                                 } else if (target_type == TypeManager.uint32_type){
1877                                         if (v >= 0 && v <= UInt32.MaxValue)
1878                                                 return (object) ((uint) v);
1879                                 } else if (target_type == TypeManager.char_type){
1880                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1881                                                 return (object) ((char) v);
1882                                 } else if (target_type == TypeManager.byte_type){
1883                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1884                                                 return (object) ((byte) v);
1885                                 } else if (target_type == TypeManager.sbyte_type){
1886                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
1887                                                 return (object) ((sbyte) v);
1888                                 } else if (target_type == TypeManager.short_type){
1889                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1890                                                 return (object) ((short) v);
1891                                 } else if (target_type == TypeManager.ushort_type){
1892                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1893                                                 return (object) ((ushort) v);
1894                                 } else if (target_type == TypeManager.uint64_type){
1895                                         if (v > 0)
1896                                                 return (object) ((ulong) v);
1897                                 }
1898                                 s = v.ToString ();
1899                         } else if (c is ULongLiteral){
1900                                 ulong v = ((ULongLiteral) c).Value;
1901
1902                                 if (target_type == TypeManager.int32_type){
1903                                         if (v <= Int32.MaxValue)
1904                                                 return (object) ((int) v);
1905                                 } else if (target_type == TypeManager.uint32_type){
1906                                         if (v <= UInt32.MaxValue)
1907                                                 return (object) ((uint) v);
1908                                 } else if (target_type == TypeManager.char_type){
1909                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1910                                                 return (object) ((char) v);
1911                                 } else if (target_type == TypeManager.byte_type){
1912                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1913                                                 return (object) ((byte) v);
1914                                 } else if (target_type == TypeManager.sbyte_type){
1915                                         if (v <= (int) SByte.MaxValue)
1916                                                 return (object) ((sbyte) v);
1917                                 } else if (target_type == TypeManager.short_type){
1918                                         if (v <= UInt16.MaxValue)
1919                                                 return (object) ((short) v);
1920                                 } else if (target_type == TypeManager.ushort_type){
1921                                         if (v <= UInt16.MaxValue)
1922                                                 return (object) ((ushort) v);
1923                                 } else if (target_type == TypeManager.int64_type){
1924                                         if (v <= Int64.MaxValue)
1925                                                 return (object) ((long) v);
1926                                 }
1927                                 s = v.ToString ();
1928                         } else if (c is ByteConstant){
1929                                 throw new Exception ("Implement me");
1930                         } else if (c is SByteConstant){
1931                                 throw new Exception ("Implement me");
1932                         } else if (c is ShortConstant){
1933                                 throw new Exception ("Implement me");
1934                         } else if (c is UShortConstant){
1935                                 throw new Exception ("Implement me");
1936                         }
1937                         
1938                         error31 (loc, s, target_type);
1939                         return null;
1940                 }               
1941                         
1942         }
1943
1944         /// <summary>
1945         ///   This is just a base class for expressions that can
1946         ///   appear on statements (invocations, object creation,
1947         ///   assignments, post/pre increment and decrement).  The idea
1948         ///   being that they would support an extra Emition interface that
1949         ///   does not leave a result on the stack.
1950         /// </summary>
1951         public abstract class ExpressionStatement : Expression {
1952
1953                 /// <summary>
1954                 ///   Requests the expression to be emitted in a `statement'
1955                 ///   context.  This means that no new value is left on the
1956                 ///   stack after invoking this method (constrasted with
1957                 ///   Emit that will always leave a value on the stack).
1958                 /// </summary>
1959                 public abstract void EmitStatement (EmitContext ec);
1960         }
1961
1962         /// <summary>
1963         ///   This kind of cast is used to encapsulate the child
1964         ///   whose type is child.Type into an expression that is
1965         ///   reported to return "return_type".  This is used to encapsulate
1966         ///   expressions which have compatible types, but need to be dealt
1967         ///   at higher levels with.
1968         ///
1969         ///   For example, a "byte" expression could be encapsulated in one
1970         ///   of these as an "unsigned int".  The type for the expression
1971         ///   would be "unsigned int".
1972         ///
1973         /// </summary>
1974         public class EmptyCast : Expression {
1975                 protected Expression child;
1976
1977                 public EmptyCast (Expression child, Type return_type)
1978                 {
1979                         eclass = child.eclass;
1980                         type = return_type;
1981                         this.child = child;
1982                 }
1983
1984                 public override Expression DoResolve (EmitContext ec)
1985                 {
1986                         // This should never be invoked, we are born in fully
1987                         // initialized state.
1988
1989                         return this;
1990                 }
1991
1992                 public override void Emit (EmitContext ec)
1993                 {
1994                         child.Emit (ec);
1995                 }
1996
1997                 public Expression Child {
1998                         get {
1999                                 return child;
2000                         }
2001                 }
2002
2003         }
2004
2005         /// <summary>
2006         ///  This class is used to wrap literals which belong inside Enums
2007         /// </summary>
2008         public class EnumConstant : Constant {
2009                 public Constant Child;
2010
2011                 public EnumConstant (Constant child, Type enum_type)
2012                 {
2013                         eclass = child.eclass;
2014                         this.Child = child;
2015                         type = enum_type;
2016                 }
2017                 
2018                 public override Expression DoResolve (EmitContext ec)
2019                 {
2020                         // This should never be invoked, we are born in fully
2021                         // initialized state.
2022
2023                         return this;
2024                 }
2025
2026                 public override void Emit (EmitContext ec)
2027                 {
2028                         Child.Emit (ec);
2029                 }
2030
2031                 public override object GetValue ()
2032                 {
2033                         return Child.GetValue ();
2034                 }
2035
2036                 //
2037                 // Converts from one of the valid underlying types for an enumeration
2038                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2039                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2040                 //
2041                 public Constant WidenToCompilerConstant ()
2042                 {
2043                         Type t = Child.Type.UnderlyingSystemType;
2044                         object v = ((Constant) Child).GetValue ();;
2045                         
2046                         if (t == TypeManager.int32_type)
2047                                 return new IntConstant ((int) v);
2048                         if (t == TypeManager.uint32_type)
2049                                 return new UIntConstant ((uint) v);
2050                         if (t == TypeManager.int64_type)
2051                                 return new LongConstant ((long) v);
2052                         if (t == TypeManager.uint64_type)
2053                                 return new ULongConstant ((ulong) v);
2054                         if (t == TypeManager.short_type)
2055                                 return new ShortConstant ((short) v);
2056                         if (t == TypeManager.ushort_type)
2057                                 return new UShortConstant ((ushort) v);
2058                         if (t == TypeManager.byte_type)
2059                                 return new ByteConstant ((byte) v);
2060                         if (t == TypeManager.sbyte_type)
2061                                 return new SByteConstant ((sbyte) v);
2062
2063                         throw new Exception ("Invalid enumeration underlying type: " + t);
2064                 }
2065
2066                 //
2067                 // Extracts the value in the enumeration on its native representation
2068                 //
2069                 public object GetPlainValue ()
2070                 {
2071                         Type t = Child.Type.UnderlyingSystemType;
2072                         object v = ((Constant) Child).GetValue ();;
2073                         
2074                         if (t == TypeManager.int32_type)
2075                                 return (int) v;
2076                         if (t == TypeManager.uint32_type)
2077                                 return (uint) v;
2078                         if (t == TypeManager.int64_type)
2079                                 return (long) v;
2080                         if (t == TypeManager.uint64_type)
2081                                 return (ulong) v;
2082                         if (t == TypeManager.short_type)
2083                                 return (short) v;
2084                         if (t == TypeManager.ushort_type)
2085                                 return (ushort) v;
2086                         if (t == TypeManager.byte_type)
2087                                 return (byte) v;
2088                         if (t == TypeManager.sbyte_type)
2089                                 return (sbyte) v;
2090
2091                         return null;
2092                 }
2093                 
2094                 public override string AsString ()
2095                 {
2096                         return Child.AsString ();
2097                 }
2098         }
2099
2100         /// <summary>
2101         ///   This kind of cast is used to encapsulate Value Types in objects.
2102         ///
2103         ///   The effect of it is to box the value type emitted by the previous
2104         ///   operation.
2105         /// </summary>
2106         public class BoxedCast : EmptyCast {
2107
2108                 public BoxedCast (Expression expr)
2109                         : base (expr, TypeManager.object_type)
2110                 {
2111                 }
2112
2113                 public override Expression DoResolve (EmitContext ec)
2114                 {
2115                         // This should never be invoked, we are born in fully
2116                         // initialized state.
2117
2118                         return this;
2119                 }
2120
2121                 public override void Emit (EmitContext ec)
2122                 {
2123                         base.Emit (ec);
2124                         ec.ig.Emit (OpCodes.Box, child.Type);
2125                 }
2126         }
2127
2128         public class UnboxCast : EmptyCast {
2129                 public UnboxCast (Expression expr, Type return_type)
2130                         : base (expr, return_type)
2131                 {
2132                 }
2133
2134                 public override Expression DoResolve (EmitContext ec)
2135                 {
2136                         // This should never be invoked, we are born in fully
2137                         // initialized state.
2138
2139                         return this;
2140                 }
2141
2142                 public override void Emit (EmitContext ec)
2143                 {
2144                         Type t = type;
2145                         ILGenerator ig = ec.ig;
2146                         
2147                         base.Emit (ec);
2148                         ig.Emit (OpCodes.Unbox, t);
2149
2150                         //
2151                         // Load the object from the pointer
2152                         //
2153                         if (t == TypeManager.int32_type)
2154                                 ig.Emit (OpCodes.Ldind_I4);
2155                         else if (t == TypeManager.uint32_type)
2156                                 ig.Emit (OpCodes.Ldind_U4);
2157                         else if (t == TypeManager.short_type)
2158                                 ig.Emit (OpCodes.Ldind_I2);
2159                         else if (t == TypeManager.ushort_type)
2160                                 ig.Emit (OpCodes.Ldind_U2);
2161                         else if (t == TypeManager.char_type)
2162                                 ig.Emit (OpCodes.Ldind_U2);
2163                         else if (t == TypeManager.byte_type)
2164                                 ig.Emit (OpCodes.Ldind_U1);
2165                         else if (t == TypeManager.sbyte_type)
2166                                 ig.Emit (OpCodes.Ldind_I1);
2167                         else if (t == TypeManager.uint64_type)
2168                                 ig.Emit (OpCodes.Ldind_I8);
2169                         else if (t == TypeManager.int64_type)
2170                                 ig.Emit (OpCodes.Ldind_I8);
2171                         else if (t == TypeManager.float_type)
2172                                 ig.Emit (OpCodes.Ldind_R4);
2173                         else if (t == TypeManager.double_type)
2174                                 ig.Emit (OpCodes.Ldind_R8);
2175                         else if (t == TypeManager.bool_type)
2176                                 ig.Emit (OpCodes.Ldind_I1);
2177                         else if (t == TypeManager.intptr_type)
2178                                 ig.Emit (OpCodes.Ldind_I);
2179                         else 
2180                                 ig.Emit (OpCodes.Ldobj, t);
2181                 }
2182         }
2183         
2184         /// <summary>
2185         ///   This is used to perform explicit numeric conversions.
2186         ///
2187         ///   Explicit numeric conversions might trigger exceptions in a checked
2188         ///   context, so they should generate the conv.ovf opcodes instead of
2189         ///   conv opcodes.
2190         /// </summary>
2191         public class ConvCast : EmptyCast {
2192                 public enum Mode : byte {
2193                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2194                         U1_I1, U1_CH,
2195                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2196                         U2_I1, U2_U1, U2_I2, U2_CH,
2197                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2198                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2199                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2200                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2201                         CH_I1, CH_U1, CH_I2,
2202                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2203                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2204                 }
2205
2206                 Mode mode;
2207                 
2208                 public ConvCast (Expression child, Type return_type, Mode m)
2209                         : base (child, return_type)
2210                 {
2211                         mode = m;
2212                 }
2213
2214                 public override Expression DoResolve (EmitContext ec)
2215                 {
2216                         // This should never be invoked, we are born in fully
2217                         // initialized state.
2218
2219                         return this;
2220                 }
2221
2222                 public override void Emit (EmitContext ec)
2223                 {
2224                         ILGenerator ig = ec.ig;
2225                         
2226                         base.Emit (ec);
2227
2228                         if (ec.CheckState){
2229                                 switch (mode){
2230                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2231                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2232                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2233                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2234                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2235
2236                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2237                                 case Mode.U1_CH: /* nothing */ break;
2238
2239                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2240                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2241                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2242                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2243                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2244                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2245
2246                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2247                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2248                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2249                                 case Mode.U2_CH: /* nothing */ break;
2250
2251                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2252                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2253                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2254                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2255                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2256                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2257                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2258
2259                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2260                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2261                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2262                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2263                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2264                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2265
2266                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2267                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2268                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2269                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2270                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2271                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2272                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2273                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2274
2275                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2276                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2277                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2278                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2279                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2280                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2281                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2282                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2283
2284                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2285                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2286                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2287
2288                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2289                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2290                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2291                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2292                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2293                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2294                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2295                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2296                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2297
2298                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2299                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2300                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2301                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2302                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2303                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2304                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2305                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2306                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2307                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2308                                 }
2309                         } else {
2310                                 switch (mode){
2311                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2312                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2313                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2314                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2315                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2316
2317                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2318                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2319
2320                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2321                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2322                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2323                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2324                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2325                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2326
2327                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2328                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2329                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2330                                 case Mode.U2_CH: /* nothing */ break;
2331
2332                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2333                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2334                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2335                                 case Mode.I4_U4: /* nothing */ break;
2336                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2337                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2338                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2339
2340                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2341                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2342                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2343                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2344                                 case Mode.U4_I4: /* nothing */ break;
2345                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2346
2347                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2348                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2349                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2350                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2351                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2352                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2353                                 case Mode.I8_U8: /* nothing */ break;
2354                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2355
2356                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2357                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2358                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2359                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2360                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2361                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2362                                 case Mode.U8_I8: /* nothing */ break;
2363                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2364
2365                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2366                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2367                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2368
2369                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2370                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2371                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2372                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2373                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2374                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2375                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2376                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2377                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2378
2379                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2380                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2381                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2382                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2383                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2384                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2385                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2386                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2387                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2388                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2389                                 }
2390                         }
2391                 }
2392         }
2393         
2394         public class OpcodeCast : EmptyCast {
2395                 OpCode op, op2;
2396                 bool second_valid;
2397                 
2398                 public OpcodeCast (Expression child, Type return_type, OpCode op)
2399                         : base (child, return_type)
2400                         
2401                 {
2402                         this.op = op;
2403                         second_valid = false;
2404                 }
2405
2406                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2407                         : base (child, return_type)
2408                         
2409                 {
2410                         this.op = op;
2411                         this.op2 = op2;
2412                         second_valid = true;
2413                 }
2414
2415                 public override Expression DoResolve (EmitContext ec)
2416                 {
2417                         // This should never be invoked, we are born in fully
2418                         // initialized state.
2419
2420                         return this;
2421                 }
2422
2423                 public override void Emit (EmitContext ec)
2424                 {
2425                         base.Emit (ec);
2426                         ec.ig.Emit (op);
2427
2428                         if (second_valid)
2429                                 ec.ig.Emit (op2);
2430                 }                       
2431         }
2432
2433         /// <summary>
2434         ///   This kind of cast is used to encapsulate a child and cast it
2435         ///   to the class requested
2436         /// </summary>
2437         public class ClassCast : EmptyCast {
2438                 public ClassCast (Expression child, Type return_type)
2439                         : base (child, return_type)
2440                         
2441                 {
2442                 }
2443
2444                 public override Expression DoResolve (EmitContext ec)
2445                 {
2446                         // This should never be invoked, we are born in fully
2447                         // initialized state.
2448
2449                         return this;
2450                 }
2451
2452                 public override void Emit (EmitContext ec)
2453                 {
2454                         base.Emit (ec);
2455
2456                         ec.ig.Emit (OpCodes.Castclass, type);
2457                 }                       
2458                 
2459         }
2460         
2461         /// <summary>
2462         ///   SimpleName expressions are initially formed of a single
2463         ///   word and it only happens at the beginning of the expression.
2464         /// </summary>
2465         ///
2466         /// <remarks>
2467         ///   The expression will try to be bound to a Field, a Method
2468         ///   group or a Property.  If those fail we pass the name to our
2469         ///   caller and the SimpleName is compounded to perform a type
2470         ///   lookup.  The idea behind this process is that we want to avoid
2471         ///   creating a namespace map from the assemblies, as that requires
2472         ///   the GetExportedTypes function to be called and a hashtable to
2473         ///   be constructed which reduces startup time.  If later we find
2474         ///   that this is slower, we should create a `NamespaceExpr' expression
2475         ///   that fully participates in the resolution process. 
2476         ///   
2477         ///   For example `System.Console.WriteLine' is decomposed into
2478         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2479         ///   
2480         ///   The first SimpleName wont produce a match on its own, so it will
2481         ///   be turned into:
2482         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
2483         ///   
2484         ///   System.Console will produce a TypeExpr match.
2485         ///   
2486         ///   The downside of this is that we might be hitting `LookupType' too many
2487         ///   times with this scheme.
2488         /// </remarks>
2489         public class SimpleName : Expression {
2490                 public readonly string Name;
2491                 public readonly Location Location;
2492                 
2493                 public SimpleName (string name, Location l)
2494                 {
2495                         Name = name;
2496                         Location = l;
2497                 }
2498
2499                 public static void Error120 (Location l, string name)
2500                 {
2501                         Report.Error (
2502                                 120, l,
2503                                 "An object reference is required " +
2504                                 "for the non-static field `"+name+"'");
2505                 }
2506                 
2507                 //
2508                 // Checks whether we are trying to access an instance
2509                 // property, method or field from a static body.
2510                 //
2511                 Expression MemberStaticCheck (Expression e)
2512                 {
2513                         if (e is FieldExpr){
2514                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2515                                 
2516                                 if (!fi.IsStatic){
2517                                         Error120 (Location, Name);
2518                                         return null;
2519                                 }
2520                         } else if (e is MethodGroupExpr){
2521                                 MethodGroupExpr mg = (MethodGroupExpr) e;
2522
2523                                 if (!mg.RemoveInstanceMethods ()){
2524                                         Error120 (Location, mg.Methods [0].Name);
2525                                         return null;
2526                                 }
2527                                 return e;
2528                         } else if (e is PropertyExpr){
2529                                 if (!((PropertyExpr) e).IsStatic){
2530                                         Error120 (Location, Name);
2531                                         return null;
2532                                 }
2533                         } else if (e is EventExpr) {
2534                                 if (!((EventExpr) e).IsStatic) {
2535                                         Error120 (Location, Name);
2536                                         return null;
2537                                 }
2538                         }
2539
2540                         return e;
2541                 }
2542                 
2543                 // <remarks>
2544                 //   7.5.2: Simple Names. 
2545                 //
2546                 //   Local Variables and Parameters are handled at
2547                 //   parse time, so they never occur as SimpleNames.
2548                 // </remarks>
2549                 public override Expression DoResolve (EmitContext ec)
2550                 {
2551                         Expression e;
2552
2553                         //
2554                         // Stage 1: Performed by the parser (binding to locals or parameters).
2555                         //
2556
2557                         //
2558                         // Stage 2: Lookup members
2559                         //
2560                         e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
2561                         if (e == null) {
2562                                 //
2563                                 // Stage 3: Lookup symbol in the various namespaces. 
2564                                 // 
2565                                 DeclSpace ds = ec.TypeContainer;
2566                                 Type t;
2567                                 string alias_value;
2568
2569                                 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2570                                         return new TypeExpr (t);
2571
2572                                 //
2573                                 // Stage 3 part b: Lookup up if we are an alias to a type
2574                                 // or a namespace.
2575                                 //
2576                                 // Since we are cheating: we only do the Alias lookup for
2577                                 // namespaces if the name does not include any dots in it
2578                                 //
2579
2580                                 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2581                                         // System.Console.WriteLine (Name + " --> " + alias_value);
2582                                         if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2583                                             != null)
2584                                                 return new TypeExpr (t);
2585
2586                                         // we have alias value, but it isn't Type, so try if it's namespace
2587                                         return new SimpleName (alias_value, Location);
2588                                 }
2589
2590                                 // No match, maybe our parent can compose us
2591                                 // into something meaningful.
2592                                 //
2593                                 return this;
2594                         }
2595
2596                         // Step 2, continues here.
2597                         if (e is TypeExpr)
2598                                 return e;
2599
2600                         if (e is FieldExpr){
2601                                 FieldExpr fe = (FieldExpr) e;
2602                                 
2603                                 if (!fe.FieldInfo.IsStatic){
2604                                         This t = new This (Location.Null);
2605
2606                                         fe.InstanceExpression = t.DoResolve (ec);
2607                                 }
2608
2609                                 FieldInfo fi = fe.FieldInfo;
2610                                 
2611                                 if (fi is FieldBuilder) {
2612                                         Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
2613                                         
2614                                         if (c != null) {
2615                                                 object o = c.LookupConstantValue (ec);
2616                                                 object real_value = ((Constant)c.Expr).GetValue ();
2617                                                 return Constantify (real_value, fi.FieldType);
2618                                         }
2619                                 }
2620                         }                               
2621
2622                         if (ec.IsStatic)
2623                                 return MemberStaticCheck (e);
2624                         else
2625                                 return e;
2626                 }
2627
2628                 public override void Emit (EmitContext ec)
2629                 {
2630                         //
2631                         // If this is ever reached, then we failed to
2632                         // find the name as a namespace
2633                         //
2634
2635                         Error (103, Location, "The name `" + Name +
2636                                "' does not exist in the class `" +
2637                                ec.TypeContainer.Name + "'");
2638                 }
2639         }
2640         
2641         /// <summary>
2642         ///   Fully resolved expression that evaluates to a type
2643         /// </summary>
2644         public class TypeExpr : Expression {
2645                 public TypeExpr (Type t)
2646                 {
2647                         Type = t;
2648                         eclass = ExprClass.Type;
2649                 }
2650
2651                 override public Expression DoResolve (EmitContext ec)
2652                 {
2653                         return this;
2654                 }
2655
2656                 override public void Emit (EmitContext ec)
2657                 {
2658                         throw new Exception ("Implement me");
2659                 }
2660         }
2661
2662         /// <summary>
2663         ///   MethodGroup Expression.
2664         ///  
2665         ///   This is a fully resolved expression that evaluates to a type
2666         /// </summary>
2667         public class MethodGroupExpr : Expression {
2668                 public MethodBase [] Methods;
2669                 Expression instance_expression = null;
2670                 
2671                 public MethodGroupExpr (MemberInfo [] mi)
2672                 {
2673                         Methods = new MethodBase [mi.Length];
2674                         mi.CopyTo (Methods, 0);
2675                         eclass = ExprClass.MethodGroup;
2676                 }
2677
2678                 public MethodGroupExpr (ArrayList l)
2679                 {
2680                         Methods = new MethodBase [l.Count];
2681
2682                         l.CopyTo (Methods, 0);
2683                         eclass = ExprClass.MethodGroup;
2684                 }
2685                 
2686                 //
2687                 // `A method group may have associated an instance expression' 
2688                 // 
2689                 public Expression InstanceExpression {
2690                         get {
2691                                 return instance_expression;
2692                         }
2693
2694                         set {
2695                                 instance_expression = value;
2696                         }
2697                 }
2698                 
2699                 override public Expression DoResolve (EmitContext ec)
2700                 {
2701                         return this;
2702                 }
2703
2704                 override public void Emit (EmitContext ec)
2705                 {
2706                         throw new Exception ("This should never be reached");
2707                 }
2708
2709                 bool RemoveMethods (bool keep_static)
2710                 {
2711                         ArrayList smethods = new ArrayList ();
2712                         int top = Methods.Length;
2713                         int i;
2714                         
2715                         for (i = 0; i < top; i++){
2716                                 MethodBase mb = Methods [i];
2717
2718                                 if (mb.IsStatic == keep_static)
2719                                         smethods.Add (mb);
2720                         }
2721
2722                         if (smethods.Count == 0)
2723                                 return false;
2724
2725                         Methods = new MethodBase [smethods.Count];
2726                         smethods.CopyTo (Methods, 0);
2727
2728                         return true;
2729                 }
2730                 
2731                 /// <summary>
2732                 ///   Removes any instance methods from the MethodGroup, returns
2733                 ///   false if the resulting set is empty.
2734                 /// </summary>
2735                 public bool RemoveInstanceMethods ()
2736                 {
2737                         return RemoveMethods (true);
2738                 }
2739
2740                 /// <summary>
2741                 ///   Removes any static methods from the MethodGroup, returns
2742                 ///   false if the resulting set is empty.
2743                 /// </summary>
2744                 public bool RemoveStaticMethods ()
2745                 {
2746                         return RemoveMethods (false);
2747                 }
2748         }
2749
2750         /// <summary>
2751         ///   Fully resolved expression that evaluates to a Field
2752         /// </summary>
2753         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
2754                 public readonly FieldInfo FieldInfo;
2755                 public Expression InstanceExpression;
2756                 Location loc;
2757                 
2758                 public FieldExpr (FieldInfo fi, Location l)
2759                 {
2760                         FieldInfo = fi;
2761                         eclass = ExprClass.Variable;
2762                         type = fi.FieldType;
2763                         loc = l;
2764                 }
2765
2766                 override public Expression DoResolve (EmitContext ec)
2767                 {
2768                         if (!FieldInfo.IsStatic){
2769                                 if (InstanceExpression == null){
2770                                         throw new Exception ("non-static FieldExpr without instance var\n" +
2771                                                              "You have to assign the Instance variable\n" +
2772                                                              "Of the FieldExpr to set this\n");
2773                                 }
2774
2775                                 InstanceExpression = InstanceExpression.Resolve (ec);
2776                                 if (InstanceExpression == null)
2777                                         return null;
2778                                 
2779                         }
2780
2781                         return this;
2782                 }
2783
2784                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2785                 {
2786                         Expression e = DoResolve (ec);
2787
2788                         if (e == null)
2789                                 return null;
2790                         
2791                         if (!FieldInfo.IsInitOnly)
2792                                 return this;
2793
2794                         //
2795                         // InitOnly fields can only be assigned in constructors
2796                         //
2797
2798                         if (ec.IsConstructor)
2799                                 return this;
2800
2801                         Report.Error (191, loc,
2802                                       "Readonly field can not be assigned outside " +
2803                                       "of constructor or variable initializer");
2804                         
2805                         return null;
2806                 }
2807
2808                 override public void Emit (EmitContext ec)
2809                 {
2810                         ILGenerator ig = ec.ig;
2811
2812                         if (FieldInfo.IsStatic)
2813                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2814                         else {
2815                                 InstanceExpression.Emit (ec);
2816                                 
2817                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
2818                         }
2819                 }
2820
2821                 public void EmitAssign (EmitContext ec, Expression source)
2822                 {
2823                         bool is_static = FieldInfo.IsStatic;
2824
2825                         if (!is_static){
2826                                 Expression instance = InstanceExpression;
2827
2828                                 if (instance.Type.IsValueType){
2829                                         if (instance is IMemoryLocation){
2830                                                 IMemoryLocation ml = (IMemoryLocation) instance;
2831
2832                                                 ml.AddressOf (ec);
2833                                         } else
2834                                                 throw new Exception ("The " + instance + " of type " + Type+
2835                                                                      "represents a ValueType and does not " +
2836                                                                      "implement IMemoryLocation");
2837                                 } else
2838                                         instance.Emit (ec);
2839                         }
2840                         source.Emit (ec);
2841                         
2842                         if (is_static)
2843                                 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
2844                         else {
2845                                 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
2846                         }
2847                 }
2848                 
2849                 public void AddressOf (EmitContext ec)
2850                 {
2851                         if (FieldInfo.IsStatic)
2852                                 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
2853                         else {
2854                                 InstanceExpression.Emit (ec);
2855                                 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
2856                         }
2857                 }
2858         }
2859         
2860         /// <summary>
2861         ///   Expression that evaluates to a Property.  The Assign class
2862         ///   might set the `Value' expression if we are in an assignment.
2863         ///
2864         ///   This is not an LValue because we need to re-write the expression, we
2865         ///   can not take data from the stack and store it.  
2866         /// </summary>
2867         public class PropertyExpr : ExpressionStatement, IAssignMethod {
2868                 public readonly PropertyInfo PropertyInfo;
2869                 public readonly bool IsStatic;
2870                 MethodInfo [] Accessors;
2871                 Location loc;
2872                 
2873                 Expression instance_expr;
2874                 
2875                 public PropertyExpr (PropertyInfo pi, Location l)
2876                 {
2877                         PropertyInfo = pi;
2878                         eclass = ExprClass.PropertyAccess;
2879                         IsStatic = false;
2880                         loc = l;
2881                         Accessors = TypeManager.GetAccessors (pi);
2882
2883                         if (Accessors != null)
2884                                 for (int i = 0; i < Accessors.Length; i++){
2885                                         if (Accessors [i] != null)
2886                                                 if (Accessors [i].IsStatic)
2887                                                         IsStatic = true;
2888                                 }
2889                         else
2890                                 Accessors = new MethodInfo [2];
2891                         
2892                         type = pi.PropertyType;
2893                 }
2894
2895                 //
2896                 // The instance expression associated with this expression
2897                 //
2898                 public Expression InstanceExpression {
2899                         set {
2900                                 instance_expr = value;
2901                         }
2902
2903                         get {
2904                                 return instance_expr;
2905                         }
2906                 }
2907
2908                 public bool VerifyAssignable ()
2909                 {
2910                         if (!PropertyInfo.CanWrite){
2911                                 Report.Error (200, loc, 
2912                                               "The property `" + PropertyInfo.Name +
2913                                               "' can not be assigned to, as it has not set accessor");
2914                                 return false;
2915                         }
2916
2917                         return true;
2918                 }
2919
2920                 override public Expression DoResolve (EmitContext ec)
2921                 {
2922                         if (!PropertyInfo.CanRead){
2923                                 Report.Error (154, loc, 
2924                                               "The property `" + PropertyInfo.Name +
2925                                               "' can not be used in " +
2926                                               "this context because it lacks a get accessor");
2927                                 return null;
2928                         }
2929
2930                         type = PropertyInfo.PropertyType;
2931
2932                         return this;
2933                 }
2934
2935                 override public void Emit (EmitContext ec)
2936                 {
2937                         Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
2938                         
2939                 }
2940
2941                 //
2942                 // Implements the IAssignMethod interface for assignments
2943                 //
2944                 public void EmitAssign (EmitContext ec, Expression source)
2945                 {
2946                         Argument arg = new Argument (source, Argument.AType.Expression);
2947                         ArrayList args = new ArrayList ();
2948
2949                         args.Add (arg);
2950                         Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
2951                 }
2952
2953                 override public void EmitStatement (EmitContext ec)
2954                 {
2955                         Emit (ec);
2956                         ec.ig.Emit (OpCodes.Pop);
2957                 }
2958         }
2959
2960         /// <summary>
2961         ///   Fully resolved expression that evaluates to an Event
2962         /// </summary>
2963         public class EventExpr : Expression, IAssignMethod {
2964                 public readonly EventInfo EventInfo;
2965                 Location loc;
2966                 public Expression InstanceExpression;
2967
2968                 public readonly bool IsStatic;
2969
2970                 MethodInfo add_accessor, remove_accessor;
2971                 
2972                 public EventExpr (EventInfo ei, Location loc)
2973                 {
2974                         EventInfo = ei;
2975                         this.loc = loc;
2976                         eclass = ExprClass.EventAccess;
2977
2978                         add_accessor = TypeManager.GetAddMethod (ei);
2979                         remove_accessor = TypeManager.GetRemoveMethod (ei);
2980                         
2981                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
2982                                         IsStatic = true;
2983
2984                         if (EventInfo is MyEventBuilder)
2985                                 type = ((MyEventBuilder) EventInfo).EventType;
2986                         else
2987                                 type = EventInfo.EventHandlerType;
2988                 }
2989
2990                 override public Expression DoResolve (EmitContext ec)
2991                 {
2992                         // We are born fully resolved
2993                         return this;
2994                 }
2995
2996                 override public void Emit (EmitContext ec)
2997                 {
2998                         // FIXME : Implement
2999                 }
3000
3001                 public void EmitAssign (EmitContext ec, Expression source)
3002                 {
3003                         Expression handler = ((Binary) source).Right;
3004                         
3005                         Argument arg = new Argument (handler, Argument.AType.Expression);
3006                         ArrayList args = new ArrayList ();
3007                                 
3008                         args.Add (arg);
3009                         
3010                         if (((Binary) source).Oper == Binary.Operator.Addition)
3011                                 Invocation.EmitCall (ec, IsStatic, InstanceExpression, add_accessor, args);
3012                         else
3013                                 Invocation.EmitCall (ec, IsStatic, InstanceExpression, remove_accessor, args);
3014                 }
3015         }
3016 }