2004-09-24 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / gmcs / convert.cs
1 //
2 // conversion.cs: various routines for implementing conversions.
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Ravi Pratap (ravi@ximian.com)
7 //
8 // (C) 2001, 2002, 2003 Ximian, Inc.
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
18         //
19         // A container class for all the conversion operations
20         //
21         public class Convert {
22                 static void Error_CannotConvertType (Location loc, Type source, Type target)
23                 {
24                         Report.Error (30, loc, "Cannot convert type '" +
25                                       TypeManager.CSharpName (source) + "' to '" +
26                                       TypeManager.CSharpName (target) + "'");
27                 }
28
29                 static Expression TypeParameter_to_Null (Expression expr, Type target_type,
30                                                          Location loc)
31                 {
32                         if (!TypeParameter_to_Null (target_type)) {
33                                 Report.Error (403, loc, "Cannot convert null to the type " +
34                                               "parameter `{0}' becaues it could be a value " +
35                                               "type.  Consider using `default ({0})' instead.",
36                                               target_type);
37                                 return null;
38                         }
39
40                         return new NullCast (expr, target_type);
41                 }
42
43                 static bool TypeParameter_to_Null (Type target_type)
44                 {
45                         if ((target_type.BaseType == null) ||
46                             (target_type.BaseType == TypeManager.value_type) ||
47                             target_type.BaseType.IsValueType)
48                                 return false;
49
50                         return true;
51                 }
52
53                 static EmptyExpression MyEmptyExpr;
54                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
55                 {
56                         Type expr_type = expr.Type;
57
58                         if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
59                                 // if we are a method group, emit a warning
60
61                                 expr.Emit (null);
62                         }
63
64                         if (expr_type == TypeManager.void_type)
65                                 return null;
66
67                         //
68                         // notice that it is possible to write "ValueType v = 1", the ValueType here
69                         // is an abstract class, and not really a value type, so we apply the same rules.
70                         //
71                         if (target_type == TypeManager.object_type) {
72                                 //
73                                 // A pointer type cannot be converted to object
74                                 // 
75                                 if (expr_type.IsPointer)
76                                         return null;
77
78                                 if (TypeManager.IsValueType (expr_type))
79                                         return new BoxedCast (expr);
80                                 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
81                                         return new EmptyCast (expr, target_type);
82
83                                 return null;
84                         } else if (target_type == TypeManager.value_type) {
85                                 if (TypeManager.IsValueType (expr_type))
86                                         return new BoxedCast (expr);
87                                 if (expr is NullLiteral)
88                                         return new NullCast (expr, target_type);
89
90                                 return null;
91                         } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
92                                 //
93                                 // Special case: enumeration to System.Enum.
94                                 // System.Enum is not a value type, it is a class, so we need
95                                 // a boxing conversion
96                                 //
97                                 if (expr_type.IsEnum || expr_type.IsGenericParameter)
98                                         return new BoxedCast (expr);
99
100                                 return new EmptyCast (expr, target_type);
101                         }
102
103                         // This code is kind of mirrored inside ImplicitStandardConversionExists
104                         // with the small distinction that we only probe there
105                         //
106                         // Always ensure that the code here and there is in sync
107
108                         // from the null type to any reference-type.
109                         if (expr is NullLiteral){
110                                 if (target_type.IsPointer)
111                                         return NullPointer.Null;
112                                         
113                                 if (!target_type.IsValueType)
114                                         return new NullCast (expr, target_type);
115                         }
116
117                         // from any class-type S to any interface-type T.
118                         if (target_type.IsInterface) {
119                                 if (target_type != TypeManager.iconvertible_type &&
120                                     expr_type.IsValueType && (expr is Constant) &&
121                                     !(expr is IntLiteral || expr is BoolLiteral ||
122                                       expr is FloatLiteral || expr is DoubleLiteral ||
123                                       expr is LongLiteral || expr is CharLiteral ||
124                                       expr is StringLiteral || expr is DecimalLiteral ||
125                                       expr is UIntLiteral || expr is ULongLiteral)) {
126                                         return null;
127                                 }
128
129                                 if (TypeManager.ImplementsInterface (expr_type, target_type)){
130                                         if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
131                                                 return new BoxedCast (expr, target_type);
132                                         else
133                                                 return new EmptyCast (expr, target_type);
134                                 }
135                         }
136
137                         // from any interface type S to interface-type T.
138                         if (expr_type.IsInterface && target_type.IsInterface) {
139                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
140                                         return new EmptyCast (expr, target_type);
141                                 else
142                                         return null;
143                         }
144                                 
145                         // from an array-type S to an array-type of type T
146                         if (expr_type.IsArray && target_type.IsArray) {
147                                 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
148
149                                         Type expr_element_type = TypeManager.GetElementType (expr_type);
150
151                                         if (MyEmptyExpr == null)
152                                                 MyEmptyExpr = new EmptyExpression ();
153                                                 
154                                         MyEmptyExpr.SetType (expr_element_type);
155                                         Type target_element_type = TypeManager.GetElementType (target_type);
156
157                                         if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
158                                                 if (ImplicitStandardConversionExists (MyEmptyExpr,
159                                                                                       target_element_type))
160                                                         return new EmptyCast (expr, target_type);
161                                 }
162                         }
163                                 
164                         // from an array-type to System.Array
165                         if (expr_type.IsArray && target_type == TypeManager.array_type)
166                                 return new EmptyCast (expr, target_type);
167                                 
168                         // from any delegate type to System.Delegate
169                         if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
170                             target_type == TypeManager.delegate_type)
171                                 return new EmptyCast (expr, target_type);
172                                         
173                         // from any array-type or delegate type into System.ICloneable.
174                         if (expr_type.IsArray ||
175                             expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
176                                 if (target_type == TypeManager.icloneable_type)
177                                         return new EmptyCast (expr, target_type);
178
179                         // from a generic type definition to a generic instance.
180                         if (TypeManager.IsEqualGenericType (expr_type, target_type))
181                                 return new EmptyCast (expr, target_type);
182
183                         if (expr_type.IsGenericParameter) {
184                                 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
185
186                                 // We're converting from a type parameter which is known to be a reference type.
187                                 if ((gc != null) && gc.IsReferenceType) {
188                                         if (gc.HasClassConstraint &&
189                                             TypeManager.IsSubclassOf (gc.ClassConstraint, target_type))
190                                                 return new EmptyCast (expr, target_type);
191
192                                         foreach (Type t in gc.InterfaceConstraints) {
193                                                 if (TypeManager.IsSubclassOf (t, target_type))
194                                                         return new EmptyCast (expr, target_type);
195                                         }
196                                 }
197                         }
198                                 
199                         return null;
200                 }
201
202                 //
203                 // Tests whether an implicit reference conversion exists between expr_type
204                 // and target_type
205                 //
206                 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
207                 {
208                         Type expr_type = expr.Type;
209
210                         //
211                         // This is the boxed case.
212                         //
213                         if (target_type == TypeManager.object_type) {
214                                 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
215                                     expr_type.IsInterface || expr_type == TypeManager.enum_type)
216                                         return true;
217
218                                 return false;
219                         } else if (TypeManager.IsSubclassOf (expr_type, target_type))
220                                 return true;
221
222                         // Please remember that all code below actually comes
223                         // from ImplicitReferenceConversion so make sure code remains in sync
224                                 
225                         // from any class-type S to any interface-type T.
226                         if (target_type.IsInterface) {
227                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
228                                         return true;
229                         }
230                                 
231                         // from any interface type S to interface-type T.
232                         if (expr_type.IsInterface && target_type.IsInterface)
233                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
234                                         return true;
235                                 
236                         // from an array-type S to an array-type of type T
237                         if (expr_type.IsArray && target_type.IsArray) {
238                                 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
239                                                 
240                                         Type expr_element_type = expr_type.GetElementType ();
241
242                                         if (MyEmptyExpr == null)
243                                                 MyEmptyExpr = new EmptyExpression ();
244                                                 
245                                         MyEmptyExpr.SetType (expr_element_type);
246                                         Type target_element_type = TypeManager.GetElementType (target_type);
247                                                 
248                                         if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
249                                                 if (ImplicitStandardConversionExists (MyEmptyExpr,
250                                                                                       target_element_type))
251                                                         return true;
252                                 }
253                         }
254                                 
255                         // from an array-type to System.Array
256                         if (expr_type.IsArray && (target_type == TypeManager.array_type))
257                                 return true;
258                                 
259                         // from any delegate type to System.Delegate
260                         if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
261                             target_type == TypeManager.delegate_type)
262                                 if (target_type.IsAssignableFrom (expr_type))
263                                         return true;
264                                         
265                         // from any array-type or delegate type into System.ICloneable.
266                         if (expr_type.IsArray ||
267                             expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
268                                 if (target_type == TypeManager.icloneable_type)
269                                         return true;
270                                 
271                         // from the null type to any reference-type.
272                         if (expr is NullLiteral && !target_type.IsValueType && !TypeManager.IsEnumType (target_type))
273                                 return true;
274
275                         // from a generic type definition to a generic instance.
276                         if (TypeManager.IsEqualGenericType (expr_type, target_type))
277                                 return true;
278
279                         if (expr_type.IsGenericParameter) {
280                                 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
281
282                                 // We're converting from a type parameter which is known to be a reference type.
283                                 if ((gc != null) && gc.IsReferenceType) {
284                                         if (gc.HasClassConstraint &&
285                                             TypeManager.IsSubclassOf (gc.ClassConstraint, target_type))
286                                                 return true;
287
288                                         foreach (Type t in gc.InterfaceConstraints) {
289                                                 if (TypeManager.IsSubclassOf (t, target_type))
290                                                         return true;
291                                         }
292                                 }
293                         }
294
295                         return false;
296                 }
297
298                 /// <summary>
299                 ///   Implicit Numeric Conversions.
300                 ///
301                 ///   expr is the expression to convert, returns a new expression of type
302                 ///   target_type or null if an implicit conversion is not possible.
303                 /// </summary>
304                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
305                                                                     Type target_type, Location loc)
306                 {
307                         Type expr_type = expr.Type;
308
309                         //
310                         // Attempt to do the implicit constant expression conversions
311
312                         if (expr is Constant){
313                                 if (expr is IntConstant){
314                                         Expression e;
315                                         
316                                         e = TryImplicitIntConversion (target_type, (IntConstant) expr);
317                                         
318                                         if (e != null)
319                                                 return e;
320                                 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
321                                         //
322                                         // Try the implicit constant expression conversion
323                                         // from long to ulong, instead of a nice routine,
324                                         // we just inline it
325                                         //
326                                         long v = ((LongConstant) expr).Value;
327                                         if (v >= 0)
328                                                 return new ULongConstant ((ulong) v);
329                                 } 
330                         }
331                         
332                         Type real_target_type = target_type;
333
334                         if (expr_type == TypeManager.sbyte_type){
335                                 //
336                                 // From sbyte to short, int, long, float, double.
337                                 //
338                                 if (real_target_type == TypeManager.int32_type)
339                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
340                                 if (real_target_type == TypeManager.int64_type)
341                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
342                                 if (real_target_type == TypeManager.double_type)
343                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
344                                 if (real_target_type == TypeManager.float_type)
345                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
346                                 if (real_target_type == TypeManager.short_type)
347                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
348                         } else if (expr_type == TypeManager.byte_type){
349                                 //
350                                 // From byte to short, ushort, int, uint, long, ulong, float, double
351                                 // 
352                                 if ((real_target_type == TypeManager.short_type) ||
353                                     (real_target_type == TypeManager.ushort_type) ||
354                                     (real_target_type == TypeManager.int32_type) ||
355                                     (real_target_type == TypeManager.uint32_type))
356                                         return new EmptyCast (expr, target_type);
357
358                                 if (real_target_type == TypeManager.uint64_type)
359                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
360                                 if (real_target_type == TypeManager.int64_type)
361                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
362                                 if (real_target_type == TypeManager.float_type)
363                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
364                                 if (real_target_type == TypeManager.double_type)
365                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
366                         } else if (expr_type == TypeManager.short_type){
367                                 //
368                                 // From short to int, long, float, double
369                                 // 
370                                 if (real_target_type == TypeManager.int32_type)
371                                         return new EmptyCast (expr, target_type);
372                                 if (real_target_type == TypeManager.int64_type)
373                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
374                                 if (real_target_type == TypeManager.double_type)
375                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
376                                 if (real_target_type == TypeManager.float_type)
377                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
378                         } else if (expr_type == TypeManager.ushort_type){
379                                 //
380                                 // From ushort to int, uint, long, ulong, float, double
381                                 //
382                                 if (real_target_type == TypeManager.uint32_type)
383                                         return new EmptyCast (expr, target_type);
384
385                                 if (real_target_type == TypeManager.uint64_type)
386                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
387                                 if (real_target_type == TypeManager.int32_type)
388                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
389                                 if (real_target_type == TypeManager.int64_type)
390                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
391                                 if (real_target_type == TypeManager.double_type)
392                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
393                                 if (real_target_type == TypeManager.float_type)
394                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
395                         } else if (expr_type == TypeManager.int32_type){
396                                 //
397                                 // From int to long, float, double
398                                 //
399                                 if (real_target_type == TypeManager.int64_type)
400                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
401                                 if (real_target_type == TypeManager.double_type)
402                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
403                                 if (real_target_type == TypeManager.float_type)
404                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
405                         } else if (expr_type == TypeManager.uint32_type){
406                                 //
407                                 // From uint to long, ulong, float, double
408                                 //
409                                 if (real_target_type == TypeManager.int64_type)
410                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
411                                 if (real_target_type == TypeManager.uint64_type)
412                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
413                                 if (real_target_type == TypeManager.double_type)
414                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
415                                                                OpCodes.Conv_R8);
416                                 if (real_target_type == TypeManager.float_type)
417                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
418                                                                OpCodes.Conv_R4);
419                         } else if (expr_type == TypeManager.int64_type){
420                                 //
421                                 // From long/ulong to float, double
422                                 //
423                                 if (real_target_type == TypeManager.double_type)
424                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
425                                 if (real_target_type == TypeManager.float_type)
426                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
427                         } else if (expr_type == TypeManager.uint64_type){
428                                 //
429                                 // From ulong to float, double
430                                 //
431                                 if (real_target_type == TypeManager.double_type)
432                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
433                                                                OpCodes.Conv_R8);
434                                 if (real_target_type == TypeManager.float_type)
435                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
436                                                                OpCodes.Conv_R4);        
437                         } else if (expr_type == TypeManager.char_type){
438                                 //
439                                 // From char to ushort, int, uint, long, ulong, float, double
440                                 // 
441                                 if ((real_target_type == TypeManager.ushort_type) ||
442                                     (real_target_type == TypeManager.int32_type) ||
443                                     (real_target_type == TypeManager.uint32_type))
444                                         return new EmptyCast (expr, target_type);
445                                 if (real_target_type == TypeManager.uint64_type)
446                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
447                                 if (real_target_type == TypeManager.int64_type)
448                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
449                                 if (real_target_type == TypeManager.float_type)
450                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
451                                 if (real_target_type == TypeManager.double_type)
452                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
453                         } else if (expr_type == TypeManager.float_type){
454                                 //
455                                 // float to double
456                                 //
457                                 if (real_target_type == TypeManager.double_type)
458                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
459                         }
460
461                         return null;
462                 }
463
464
465                 /// <summary>
466                 ///  Same as ImplicitStandardConversionExists except that it also looks at
467                 ///  implicit user defined conversions - needed for overload resolution
468                 /// </summary>
469                 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
470                 {
471                         if ((expr is NullLiteral) && target_type.IsGenericParameter)
472                                 return TypeParameter_to_Null (target_type);
473
474                         if (ImplicitStandardConversionExists (expr, target_type))
475                                 return true;
476
477                         Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
478
479                         if (dummy != null)
480                                 return true;
481
482                         return false;
483                 }
484
485                 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
486                 {
487                         Expression dummy = ImplicitUserConversion (
488                                 ec, new EmptyExpression (source), target, Location.Null);
489                         return dummy != null;
490                 }
491
492                 /// <summary>
493                 ///  Determines if a standard implicit conversion exists from
494                 ///  expr_type to target_type
495                 /// </summary>
496                 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
497                 {
498                         Type expr_type = expr.Type;
499
500                         if (expr_type == TypeManager.void_type)
501                                 return false;
502
503                         //Console.WriteLine ("Expr is {0}", expr);
504                         //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
505                         if (expr_type.Equals (target_type))
506                                 return true;
507
508                         // First numeric conversions 
509
510                         if (expr_type == TypeManager.sbyte_type){
511                                 //
512                                 // From sbyte to short, int, long, float, double.
513                                 //
514                                 if ((target_type == TypeManager.int32_type) || 
515                                     (target_type == TypeManager.int64_type) ||
516                                     (target_type == TypeManager.double_type) ||
517                                     (target_type == TypeManager.float_type)  ||
518                                     (target_type == TypeManager.short_type) ||
519                                     (target_type == TypeManager.decimal_type))
520                                         return true;
521                                 
522                         } else if (expr_type == TypeManager.byte_type){
523                                 //
524                                 // From byte to short, ushort, int, uint, long, ulong, float, double
525                                 // 
526                                 if ((target_type == TypeManager.short_type) ||
527                                     (target_type == TypeManager.ushort_type) ||
528                                     (target_type == TypeManager.int32_type) ||
529                                     (target_type == TypeManager.uint32_type) ||
530                                     (target_type == TypeManager.uint64_type) ||
531                                     (target_type == TypeManager.int64_type) ||
532                                     (target_type == TypeManager.float_type) ||
533                                     (target_type == TypeManager.double_type) ||
534                                     (target_type == TypeManager.decimal_type))
535                                         return true;
536         
537                         } else if (expr_type == TypeManager.short_type){
538                                 //
539                                 // From short to int, long, float, double
540                                 // 
541                                 if ((target_type == TypeManager.int32_type) ||
542                                     (target_type == TypeManager.int64_type) ||
543                                     (target_type == TypeManager.double_type) ||
544                                     (target_type == TypeManager.float_type) ||
545                                     (target_type == TypeManager.decimal_type))
546                                         return true;
547                                         
548                         } else if (expr_type == TypeManager.ushort_type){
549                                 //
550                                 // From ushort to int, uint, long, ulong, float, double
551                                 //
552                                 if ((target_type == TypeManager.uint32_type) ||
553                                     (target_type == TypeManager.uint64_type) ||
554                                     (target_type == TypeManager.int32_type) ||
555                                     (target_type == TypeManager.int64_type) ||
556                                     (target_type == TypeManager.double_type) ||
557                                     (target_type == TypeManager.float_type) ||
558                                     (target_type == TypeManager.decimal_type))
559                                         return true;
560                                     
561                         } else if (expr_type == TypeManager.int32_type){
562                                 //
563                                 // From int to long, float, double
564                                 //
565                                 if ((target_type == TypeManager.int64_type) ||
566                                     (target_type == TypeManager.double_type) ||
567                                     (target_type == TypeManager.float_type) ||
568                                     (target_type == TypeManager.decimal_type))
569                                         return true;
570                                         
571                         } else if (expr_type == TypeManager.uint32_type){
572                                 //
573                                 // From uint to long, ulong, float, double
574                                 //
575                                 if ((target_type == TypeManager.int64_type) ||
576                                     (target_type == TypeManager.uint64_type) ||
577                                     (target_type == TypeManager.double_type) ||
578                                     (target_type == TypeManager.float_type) ||
579                                     (target_type == TypeManager.decimal_type))
580                                         return true;
581                                         
582                         } else if ((expr_type == TypeManager.uint64_type) ||
583                                    (expr_type == TypeManager.int64_type)) {
584                                 //
585                                 // From long/ulong to float, double
586                                 //
587                                 if ((target_type == TypeManager.double_type) ||
588                                     (target_type == TypeManager.float_type) ||
589                                     (target_type == TypeManager.decimal_type))
590                                         return true;
591                                     
592                         } else if (expr_type == TypeManager.char_type){
593                                 //
594                                 // From char to ushort, int, uint, long, ulong, float, double
595                                 // 
596                                 if ((target_type == TypeManager.ushort_type) ||
597                                     (target_type == TypeManager.int32_type) ||
598                                     (target_type == TypeManager.uint32_type) ||
599                                     (target_type == TypeManager.uint64_type) ||
600                                     (target_type == TypeManager.int64_type) ||
601                                     (target_type == TypeManager.float_type) ||
602                                     (target_type == TypeManager.double_type) ||
603                                     (target_type == TypeManager.decimal_type))
604                                         return true;
605
606                         } else if (expr_type == TypeManager.float_type){
607                                 //
608                                 // float to double
609                                 //
610                                 if (target_type == TypeManager.double_type)
611                                         return true;
612                         }       
613                         
614                         if (ImplicitReferenceConversionExists (expr, target_type))
615                                 return true;
616
617                         //
618                         // Implicit Constant Expression Conversions
619                         //
620                         if (expr is IntConstant){
621                                 int value = ((IntConstant) expr).Value;
622
623                                 if (target_type == TypeManager.sbyte_type){
624                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
625                                                 return true;
626                                 } else if (target_type == TypeManager.byte_type){
627                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
628                                                 return true;
629                                 } else if (target_type == TypeManager.short_type){
630                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
631                                                 return true;
632                                 } else if (target_type == TypeManager.ushort_type){
633                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
634                                                 return true;
635                                 } else if (target_type == TypeManager.uint32_type){
636                                         if (value >= 0)
637                                                 return true;
638                                 } else if (target_type == TypeManager.uint64_type){
639                                          //
640                                          // we can optimize this case: a positive int32
641                                          // always fits on a uint64.  But we need an opcode
642                                          // to do it.
643                                          //
644                                         if (value >= 0)
645                                                 return true;
646                                 }
647                                 
648                                 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
649                                         return true;
650                         }
651
652                         if (expr is LongConstant && target_type == TypeManager.uint64_type){
653                                 //
654                                 // Try the implicit constant expression conversion
655                                 // from long to ulong, instead of a nice routine,
656                                 // we just inline it
657                                 //
658                                 long v = ((LongConstant) expr).Value;
659                                 if (v > 0)
660                                         return true;
661                         }
662                         
663                         if ((target_type == TypeManager.enum_type ||
664                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
665                              expr is IntLiteral){
666                                 IntLiteral i = (IntLiteral) expr;
667
668                                 if (i.Value == 0)
669                                         return true;
670                         }
671
672                         if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
673                                 return true;
674
675                         return false;
676                 }
677
678                 //
679                 // Used internally by FindMostEncompassedType, this is used
680                 // to avoid creating lots of objects in the tight loop inside
681                 // FindMostEncompassedType
682                 //
683                 static EmptyExpression priv_fmet_param;
684                 
685                 /// <summary>
686                 ///  Finds "most encompassed type" according to the spec (13.4.2)
687                 ///  amongst the methods in the MethodGroupExpr
688                 /// </summary>
689                 static Type FindMostEncompassedType (ArrayList types)
690                 {
691                         Type best = null;
692
693                         if (priv_fmet_param == null)
694                                 priv_fmet_param = new EmptyExpression ();
695
696                         foreach (Type t in types){
697                                 priv_fmet_param.SetType (t);
698                                 
699                                 if (best == null) {
700                                         best = t;
701                                         continue;
702                                 }
703                                 
704                                 if (ImplicitStandardConversionExists (priv_fmet_param, best))
705                                         best = t;
706                         }
707
708                         return best;
709                 }
710
711                 //
712                 // Used internally by FindMostEncompassingType, this is used
713                 // to avoid creating lots of objects in the tight loop inside
714                 // FindMostEncompassingType
715                 //
716                 static EmptyExpression priv_fmee_ret;
717                 
718                 /// <summary>
719                 ///  Finds "most encompassing type" according to the spec (13.4.2)
720                 ///  amongst the types in the given set
721                 /// </summary>
722                 static Type FindMostEncompassingType (ArrayList types)
723                 {
724                         Type best = null;
725
726                         if (priv_fmee_ret == null)
727                                 priv_fmee_ret = new EmptyExpression ();
728
729                         foreach (Type t in types){
730                                 priv_fmee_ret.SetType (best);
731
732                                 if (best == null) {
733                                         best = t;
734                                         continue;
735                                 }
736
737                                 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
738                                         best = t;
739                         }
740                         
741                         return best;
742                 }
743
744                 //
745                 // Used to avoid creating too many objects
746                 //
747                 static EmptyExpression priv_fms_expr;
748                 
749                 /// <summary>
750                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
751                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
752                 ///   for explicit and implicit conversion operators.
753                 /// </summary>
754                 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
755                                                            bool apply_explicit_conv_rules,
756                                                            Location loc)
757                 {
758                         ArrayList src_types_set = new ArrayList ();
759                         
760                         if (priv_fms_expr == null)
761                                 priv_fms_expr = new EmptyExpression ();
762
763                         //
764                         // If any operator converts from S then Sx = S
765                         //
766                         Type source_type = source.Type;
767                         foreach (MethodBase mb in me.Methods){
768                                 ParameterData pd = Invocation.GetParameterData (mb);
769                                 Type param_type = pd.ParameterType (0);
770
771                                 if (param_type == source_type)
772                                         return param_type;
773
774                                 if (apply_explicit_conv_rules) {
775                                         //
776                                         // From the spec :
777                                         // Find the set of applicable user-defined conversion operators, U.  This set
778                                         // consists of the
779                                         // user-defined implicit or explicit conversion operators declared by
780                                         // the classes or structs in D that convert from a type encompassing
781                                         // or encompassed by S to a type encompassing or encompassed by T
782                                         //
783                                         priv_fms_expr.SetType (param_type);
784                                         if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
785                                                 src_types_set.Add (param_type);
786                                         else {
787                                                 if (ImplicitStandardConversionExists (source, param_type))
788                                                         src_types_set.Add (param_type);
789                                         }
790                                 } else {
791                                         //
792                                         // Only if S is encompassed by param_type
793                                         //
794                                         if (ImplicitStandardConversionExists (source, param_type))
795                                                 src_types_set.Add (param_type);
796                                 }
797                         }
798                         
799                         //
800                         // Explicit Conv rules
801                         //
802                         if (apply_explicit_conv_rules) {
803                                 ArrayList candidate_set = new ArrayList ();
804
805                                 foreach (Type param_type in src_types_set){
806                                         if (ImplicitStandardConversionExists (source, param_type))
807                                                 candidate_set.Add (param_type);
808                                 }
809
810                                 if (candidate_set.Count != 0)
811                                         return FindMostEncompassedType (candidate_set);
812                         }
813
814                         //
815                         // Final case
816                         //
817                         if (apply_explicit_conv_rules)
818                                 return FindMostEncompassingType (src_types_set);
819                         else
820                                 return FindMostEncompassedType (src_types_set);
821                 }
822
823                 //
824                 // Useful in avoiding proliferation of objects
825                 //
826                 static EmptyExpression priv_fmt_expr;
827                 
828                 /// <summary>
829                 ///  Finds the most specific target Tx according to section 13.4.4
830                 /// </summary>
831                 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
832                                                            bool apply_explicit_conv_rules,
833                                                            Location loc)
834                 {
835                         ArrayList tgt_types_set = new ArrayList ();
836                         
837                         if (priv_fmt_expr == null)
838                                 priv_fmt_expr = new EmptyExpression ();
839                         
840                         //
841                         // If any operator converts to T then Tx = T
842                         //
843                         foreach (MethodInfo mi in me.Methods){
844                                 Type ret_type = mi.ReturnType;
845
846                                 if (ret_type == target)
847                                         return ret_type;
848
849                                 if (apply_explicit_conv_rules) {
850                                         //
851                                         // From the spec :
852                                         // Find the set of applicable user-defined conversion operators, U.
853                                         //
854                                         // This set consists of the
855                                         // user-defined implicit or explicit conversion operators declared by
856                                         // the classes or structs in D that convert from a type encompassing
857                                         // or encompassed by S to a type encompassing or encompassed by T
858                                         //
859                                         priv_fms_expr.SetType (ret_type);
860                                         if (ImplicitStandardConversionExists (priv_fms_expr, target))
861                                                 tgt_types_set.Add (ret_type);
862                                         else {
863                                                 priv_fms_expr.SetType (target);
864                                                 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
865                                                         tgt_types_set.Add (ret_type);
866                                         }
867                                 } else {
868                                         //
869                                         // Only if T is encompassed by param_type
870                                         //
871                                         priv_fms_expr.SetType (ret_type);
872                                         if (ImplicitStandardConversionExists (priv_fms_expr, target))
873                                                 tgt_types_set.Add (ret_type);
874                                 }
875                         }
876
877                         //
878                         // Explicit conv rules
879                         //
880                         if (apply_explicit_conv_rules) {
881                                 ArrayList candidate_set = new ArrayList ();
882
883                                 foreach (Type ret_type in tgt_types_set){
884                                         priv_fmt_expr.SetType (ret_type);
885                                         
886                                         if (ImplicitStandardConversionExists (priv_fmt_expr, target))
887                                                 candidate_set.Add (ret_type);
888                                 }
889
890                                 if (candidate_set.Count != 0)
891                                         return FindMostEncompassingType (candidate_set);
892                         }
893                         
894                         //
895                         // Okay, final case !
896                         //
897                         if (apply_explicit_conv_rules)
898                                 return FindMostEncompassedType (tgt_types_set);
899                         else 
900                                 return FindMostEncompassingType (tgt_types_set);
901                 }
902                 
903                 /// <summary>
904                 ///  User-defined Implicit conversions
905                 /// </summary>
906                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
907                                                                  Type target, Location loc)
908                 {
909                         return UserDefinedConversion (ec, source, target, loc, false);
910                 }
911
912                 /// <summary>
913                 ///  User-defined Explicit conversions
914                 /// </summary>
915                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
916                                                                  Type target, Location loc)
917                 {
918                         return UserDefinedConversion (ec, source, target, loc, true);
919                 }
920
921                 static DoubleHash explicit_conv = new DoubleHash (100);
922                 static DoubleHash implicit_conv = new DoubleHash (100);
923                 /// <summary>
924                 ///   Computes the MethodGroup for the user-defined conversion
925                 ///   operators from source_type to target_type.  `look_for_explicit'
926                 ///   controls whether we should also include the list of explicit
927                 ///   operators
928                 /// </summary>
929                 static MethodGroupExpr GetConversionOperators (EmitContext ec,
930                                                                Type source_type, Type target_type,
931                                                                Location loc, bool look_for_explicit)
932                 {
933                         Expression mg1 = null, mg2 = null;
934                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
935                         string op_name;
936
937                         op_name = "op_Implicit";
938
939                         MethodGroupExpr union3;
940                         object r;
941                         if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
942                                 return (MethodGroupExpr) r;
943
944                         mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
945                         if (source_type.BaseType != null)
946                                 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
947
948                         if (mg1 == null)
949                                 union3 = (MethodGroupExpr) mg2;
950                         else if (mg2 == null)
951                                 union3 = (MethodGroupExpr) mg1;
952                         else
953                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
954
955                         mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
956                         if (mg1 != null){
957                                 if (union3 != null)
958                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
959                                 else
960                                         union3 = (MethodGroupExpr) mg1;
961                         }
962
963                         if (target_type.BaseType != null)
964                                 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
965                         
966                         if (mg1 != null){
967                                 if (union3 != null)
968                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
969                                 else
970                                         union3 = (MethodGroupExpr) mg1;
971                         }
972
973                         MethodGroupExpr union4 = null;
974
975                         if (look_for_explicit) {
976                                 op_name = "op_Explicit";
977
978                                 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
979                                 if (source_type.BaseType != null)
980                                         mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
981                                 
982                                 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
983                                 if (target_type.BaseType != null)
984                                         mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
985                                 
986                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
987                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
988
989                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
990                         }
991                         
992                         MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
993                         (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
994                         return ret;
995                 }
996                 
997                 /// <summary>
998                 ///   User-defined conversions
999                 /// </summary>
1000                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1001                                                                 Type target, Location loc,
1002                                                                 bool look_for_explicit)
1003                 {
1004                         MethodGroupExpr union;
1005                         Type source_type = source.Type;
1006                         MethodBase method = null;
1007
1008                         union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1009                         if (union == null)
1010                                 return null;
1011                         
1012                         Type most_specific_source, most_specific_target;
1013
1014                         most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1015                         if (most_specific_source == null)
1016                                 return null;
1017
1018                         most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1019                         if (most_specific_target == null) 
1020                                 return null;
1021
1022                         int count = 0;
1023
1024                         
1025                         foreach (MethodBase mb in union.Methods){
1026                                 ParameterData pd = Invocation.GetParameterData (mb);
1027                                 MethodInfo mi = (MethodInfo) mb;
1028                                 
1029                                 if (pd.ParameterType (0) == most_specific_source &&
1030                                     mi.ReturnType == most_specific_target) {
1031                                         method = mb;
1032                                         count++;
1033                                 }
1034                         }
1035                         
1036                         if (method == null || count > 1)
1037                                 return null;
1038                         
1039                         
1040                         //
1041                         // This will do the conversion to the best match that we
1042                         // found.  Now we need to perform an implict standard conversion
1043                         // if the best match was not the type that we were requested
1044                         // by target.
1045                         //
1046                         if (look_for_explicit)
1047                                 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1048                         else
1049                                 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1050
1051                         if (source == null)
1052                                 return null;
1053
1054                         Expression e;
1055                         e =  new UserCast ((MethodInfo) method, source, loc);
1056                         if (e.Type != target){
1057                                 if (!look_for_explicit)
1058                                         e = ImplicitConversionStandard (ec, e, target, loc);
1059                                 else
1060                                         e = ExplicitConversionStandard (ec, e, target, loc);
1061                         }
1062
1063                         return e;
1064                 }
1065                 
1066                 /// <summary>
1067                 ///   Converts implicitly the resolved expression `expr' into the
1068                 ///   `target_type'.  It returns a new expression that can be used
1069                 ///   in a context that expects a `target_type'. 
1070                 /// </summary>
1071                 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1072                                                              Type target_type, Location loc)
1073                 {
1074                         Expression e;
1075
1076                         if (target_type == null)
1077                                 throw new Exception ("Target type is null");
1078
1079                         e = ImplicitConversionStandard (ec, expr, target_type, loc);
1080                         if (e != null)
1081                                 return e;
1082
1083                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1084                         if (e != null)
1085                                 return e;
1086
1087                         return null;
1088                 }
1089
1090                 
1091                 /// <summary>
1092                 ///   Attempts to apply the `Standard Implicit
1093                 ///   Conversion' rules to the expression `expr' into
1094                 ///   the `target_type'.  It returns a new expression
1095                 ///   that can be used in a context that expects a
1096                 ///   `target_type'.
1097                 ///
1098                 ///   This is different from `ImplicitConversion' in that the
1099                 ///   user defined implicit conversions are excluded. 
1100                 /// </summary>
1101                 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1102                                                                      Type target_type, Location loc)
1103                 {
1104                         Type expr_type = expr.Type;
1105                         Expression e;
1106
1107                         if ((expr is NullLiteral) && target_type.IsGenericParameter)
1108                                 return TypeParameter_to_Null (expr, target_type, loc);
1109
1110                         if (expr.eclass == ExprClass.MethodGroup){
1111                                 if (!TypeManager.IsDelegateType (target_type)){
1112                                         Report.Error (428, loc,
1113                                                       String.Format (
1114                                                            "Cannot convert method group to `{0}', since it is not a delegate",
1115                                                            TypeManager.CSharpName (target_type)));
1116                                         return null;
1117                                 }
1118
1119                                 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1120                         }
1121
1122                         if (expr_type.Equals (target_type) && !(expr is NullLiteral))
1123                                 return expr;
1124
1125                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1126                         if (e != null)
1127                                 return e;
1128
1129                         e = ImplicitReferenceConversion (expr, target_type);
1130                         if (e != null)
1131                                 return e;
1132                         
1133                         if ((target_type == TypeManager.enum_type ||
1134                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
1135                             expr is IntLiteral){
1136                                 IntLiteral i = (IntLiteral) expr;
1137
1138                                 if (i.Value == 0)
1139                                         return new EnumConstant ((Constant) expr, target_type);
1140                         }
1141
1142                         if (ec.InUnsafe) {
1143                                 if (expr_type.IsPointer){
1144                                         if (target_type == TypeManager.void_ptr_type)
1145                                                 return new EmptyCast (expr, target_type);
1146
1147                                         //
1148                                         // yep, comparing pointer types cant be done with
1149                                         // t1 == t2, we have to compare their element types.
1150                                         //
1151                                         if (target_type.IsPointer){
1152                                                 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1153                                                         return expr;
1154                                         }
1155                                 }
1156                                 
1157                                 if (target_type.IsPointer) {
1158                                         if (expr is NullLiteral)
1159                                                 return new EmptyCast (expr, target_type);
1160
1161                                         if (expr_type == TypeManager.void_ptr_type)
1162                                                 return new EmptyCast (expr, target_type);
1163                                 }
1164                         }
1165
1166                         return null;
1167                 }
1168
1169                 /// <summary>
1170                 ///   Attemps to perform an implict constant conversion of the IntConstant
1171                 ///   into a different data type using casts (See Implicit Constant
1172                 ///   Expression Conversions)
1173                 /// </summary>
1174                 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1175                 {
1176                         int value = ic.Value;
1177
1178                         if (target_type == TypeManager.sbyte_type){
1179                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1180                                         return new SByteConstant ((sbyte) value);
1181                         } else if (target_type == TypeManager.byte_type){
1182                                 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1183                                         return new ByteConstant ((byte) value);
1184                         } else if (target_type == TypeManager.short_type){
1185                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1186                                         return new ShortConstant ((short) value);
1187                         } else if (target_type == TypeManager.ushort_type){
1188                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1189                                         return new UShortConstant ((ushort) value);
1190                         } else if (target_type == TypeManager.uint32_type){
1191                                 if (value >= 0)
1192                                         return new UIntConstant ((uint) value);
1193                         } else if (target_type == TypeManager.uint64_type){
1194                                 //
1195                                 // we can optimize this case: a positive int32
1196                                 // always fits on a uint64.  But we need an opcode
1197                                 // to do it.
1198                                 //
1199                                 if (value >= 0)
1200                                         return new ULongConstant ((ulong) value);
1201                         } else if (target_type == TypeManager.double_type)
1202                                 return new DoubleConstant ((double) value);
1203                         else if (target_type == TypeManager.float_type)
1204                                 return new FloatConstant ((float) value);
1205                         
1206                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1207                                 Type underlying = TypeManager.EnumToUnderlying (target_type);
1208                                 Constant e = (Constant) ic;
1209                                 
1210                                 //
1211                                 // Possibly, we need to create a different 0 literal before passing
1212                                 // to EnumConstant
1213                                 //n
1214                                 if (underlying == TypeManager.int64_type)
1215                                         e = new LongLiteral (0);
1216                                 else if (underlying == TypeManager.uint64_type)
1217                                         e = new ULongLiteral (0);
1218
1219                                 return new EnumConstant (e, target_type);
1220                         }
1221                         return null;
1222                 }
1223
1224                 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1225                 {
1226                         string msg = "Cannot convert implicitly from `"+
1227                                 TypeManager.CSharpName (source) + "' to `" +
1228                                 TypeManager.CSharpName (target) + "'";
1229
1230                         Report.Error (29, loc, msg);
1231                 }
1232
1233                 /// <summary>
1234                 ///   Attemptes to implicityly convert `target' into `type', using
1235                 ///   ImplicitConversion.  If there is no implicit conversion, then
1236                 ///   an error is signaled
1237                 /// </summary>
1238                 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1239                                                                      Type target_type, Location loc)
1240                 {
1241                         Expression e;
1242
1243                         int errors = Report.Errors;
1244                         e = ImplicitConversion (ec, source, target_type, loc);
1245                         if (Report.Errors > errors)
1246                                 return null;
1247                         if (e != null)
1248                                 return e;
1249
1250                         if (source is DoubleLiteral && target_type == TypeManager.float_type){
1251                                 Report.Error (664, loc,
1252                                               "Double literal cannot be implicitly converted to " +
1253                                               "float type, use F suffix to create a float literal");
1254                         }
1255
1256                         if (source is Constant){
1257                                 Constant c = (Constant) source;
1258
1259                                 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1260                                 return null;
1261                         }
1262                         
1263                         Error_CannotImplicitConversion (loc, source.Type, target_type);
1264
1265                         return null;
1266                 }
1267
1268                 /// <summary>
1269                 ///   Performs the explicit numeric conversions
1270                 /// </summary>
1271                 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1272                 {
1273                         Type expr_type = expr.Type;
1274
1275                         //
1276                         // If we have an enumeration, extract the underlying type,
1277                         // use this during the comparison, but wrap around the original
1278                         // target_type
1279                         //
1280                         Type real_target_type = target_type;
1281
1282                         if (TypeManager.IsEnumType (real_target_type))
1283                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1284
1285                         if (ImplicitStandardConversionExists (expr, real_target_type)){
1286                                 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1287
1288                                 if (real_target_type != target_type)
1289                                         return new EmptyCast (ce, target_type);
1290                                 return ce;
1291                         }
1292                         
1293                         if (expr_type == TypeManager.sbyte_type){
1294                                 //
1295                                 // From sbyte to byte, ushort, uint, ulong, char
1296                                 //
1297                                 if (real_target_type == TypeManager.byte_type)
1298                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1299                                 if (real_target_type == TypeManager.ushort_type)
1300                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1301                                 if (real_target_type == TypeManager.uint32_type)
1302                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1303                                 if (real_target_type == TypeManager.uint64_type)
1304                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1305                                 if (real_target_type == TypeManager.char_type)
1306                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1307                         } else if (expr_type == TypeManager.byte_type){
1308                                 //
1309                                 // From byte to sbyte and char
1310                                 //
1311                                 if (real_target_type == TypeManager.sbyte_type)
1312                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1313                                 if (real_target_type == TypeManager.char_type)
1314                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1315                         } else if (expr_type == TypeManager.short_type){
1316                                 //
1317                                 // From short to sbyte, byte, ushort, uint, ulong, char
1318                                 //
1319                                 if (real_target_type == TypeManager.sbyte_type)
1320                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1321                                 if (real_target_type == TypeManager.byte_type)
1322                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1323                                 if (real_target_type == TypeManager.ushort_type)
1324                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1325                                 if (real_target_type == TypeManager.uint32_type)
1326                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1327                                 if (real_target_type == TypeManager.uint64_type)
1328                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1329                                 if (real_target_type == TypeManager.char_type)
1330                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1331                         } else if (expr_type == TypeManager.ushort_type){
1332                                 //
1333                                 // From ushort to sbyte, byte, short, char
1334                                 //
1335                                 if (real_target_type == TypeManager.sbyte_type)
1336                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1337                                 if (real_target_type == TypeManager.byte_type)
1338                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1339                                 if (real_target_type == TypeManager.short_type)
1340                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1341                                 if (real_target_type == TypeManager.char_type)
1342                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1343                         } else if (expr_type == TypeManager.int32_type){
1344                                 //
1345                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1346                                 //
1347                                 if (real_target_type == TypeManager.sbyte_type)
1348                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1349                                 if (real_target_type == TypeManager.byte_type)
1350                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1351                                 if (real_target_type == TypeManager.short_type)
1352                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1353                                 if (real_target_type == TypeManager.ushort_type)
1354                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1355                                 if (real_target_type == TypeManager.uint32_type)
1356                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1357                                 if (real_target_type == TypeManager.uint64_type)
1358                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1359                                 if (real_target_type == TypeManager.char_type)
1360                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1361                         } else if (expr_type == TypeManager.uint32_type){
1362                                 //
1363                                 // From uint to sbyte, byte, short, ushort, int, char
1364                                 //
1365                                 if (real_target_type == TypeManager.sbyte_type)
1366                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1367                                 if (real_target_type == TypeManager.byte_type)
1368                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1369                                 if (real_target_type == TypeManager.short_type)
1370                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1371                                 if (real_target_type == TypeManager.ushort_type)
1372                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1373                                 if (real_target_type == TypeManager.int32_type)
1374                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1375                                 if (real_target_type == TypeManager.char_type)
1376                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1377                         } else if (expr_type == TypeManager.int64_type){
1378                                 //
1379                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1380                                 //
1381                                 if (real_target_type == TypeManager.sbyte_type)
1382                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1383                                 if (real_target_type == TypeManager.byte_type)
1384                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1385                                 if (real_target_type == TypeManager.short_type)
1386                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1387                                 if (real_target_type == TypeManager.ushort_type)
1388                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1389                                 if (real_target_type == TypeManager.int32_type)
1390                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1391                                 if (real_target_type == TypeManager.uint32_type)
1392                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1393                                 if (real_target_type == TypeManager.uint64_type)
1394                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1395                                 if (real_target_type == TypeManager.char_type)
1396                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1397                         } else if (expr_type == TypeManager.uint64_type){
1398                                 //
1399                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1400                                 //
1401                                 if (real_target_type == TypeManager.sbyte_type)
1402                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1403                                 if (real_target_type == TypeManager.byte_type)
1404                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1405                                 if (real_target_type == TypeManager.short_type)
1406                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1407                                 if (real_target_type == TypeManager.ushort_type)
1408                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1409                                 if (real_target_type == TypeManager.int32_type)
1410                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1411                                 if (real_target_type == TypeManager.uint32_type)
1412                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1413                                 if (real_target_type == TypeManager.int64_type)
1414                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1415                                 if (real_target_type == TypeManager.char_type)
1416                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1417                         } else if (expr_type == TypeManager.char_type){
1418                                 //
1419                                 // From char to sbyte, byte, short
1420                                 //
1421                                 if (real_target_type == TypeManager.sbyte_type)
1422                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1423                                 if (real_target_type == TypeManager.byte_type)
1424                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1425                                 if (real_target_type == TypeManager.short_type)
1426                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1427                         } else if (expr_type == TypeManager.float_type){
1428                                 //
1429                                 // From float to sbyte, byte, short,
1430                                 // ushort, int, uint, long, ulong, char
1431                                 // or decimal
1432                                 //
1433                                 if (real_target_type == TypeManager.sbyte_type)
1434                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1435                                 if (real_target_type == TypeManager.byte_type)
1436                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1437                                 if (real_target_type == TypeManager.short_type)
1438                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1439                                 if (real_target_type == TypeManager.ushort_type)
1440                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1441                                 if (real_target_type == TypeManager.int32_type)
1442                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1443                                 if (real_target_type == TypeManager.uint32_type)
1444                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1445                                 if (real_target_type == TypeManager.int64_type)
1446                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1447                                 if (real_target_type == TypeManager.uint64_type)
1448                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1449                                 if (real_target_type == TypeManager.char_type)
1450                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1451                         } else if (expr_type == TypeManager.double_type){
1452                                 //
1453                                 // From double to byte, byte, short,
1454                                 // ushort, int, uint, long, ulong,
1455                                 // char, float or decimal
1456                                 //
1457                                 if (real_target_type == TypeManager.sbyte_type)
1458                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1459                                 if (real_target_type == TypeManager.byte_type)
1460                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1461                                 if (real_target_type == TypeManager.short_type)
1462                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1463                                 if (real_target_type == TypeManager.ushort_type)
1464                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1465                                 if (real_target_type == TypeManager.int32_type)
1466                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1467                                 if (real_target_type == TypeManager.uint32_type)
1468                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1469                                 if (real_target_type == TypeManager.int64_type)
1470                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1471                                 if (real_target_type == TypeManager.uint64_type)
1472                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1473                                 if (real_target_type == TypeManager.char_type)
1474                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1475                                 if (real_target_type == TypeManager.float_type)
1476                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1477                         } 
1478
1479                         // decimal is taken care of by the op_Explicit methods.
1480
1481                         return null;
1482                 }
1483
1484                 /// <summary>
1485                 ///  Returns whether an explicit reference conversion can be performed
1486                 ///  from source_type to target_type
1487                 /// </summary>
1488                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1489                 {
1490                         bool target_is_type_param = target_type.IsGenericParameter;
1491                         bool target_is_value_type = target_type.IsValueType;
1492                         
1493                         if (source_type == target_type)
1494                                 return true;
1495
1496                         //
1497                         // From object to a generic parameter
1498                         //
1499                         if (source_type == TypeManager.object_type && target_is_type_param)
1500                                 return true;
1501
1502                         //
1503                         // From object to any reference type
1504                         //
1505                         if (source_type == TypeManager.object_type && !target_is_value_type)
1506                                 return true;
1507                                         
1508                         //
1509                         // From any class S to any class-type T, provided S is a base class of T
1510                         //
1511                         if (TypeManager.IsSubclassOf (target_type, source_type))
1512                                 return true;
1513
1514                         //
1515                         // From any interface type S to any interface T provided S is not derived from T
1516                         //
1517                         if (source_type.IsInterface && target_type.IsInterface){
1518                                 if (!TypeManager.IsSubclassOf (target_type, source_type))
1519                                         return true;
1520                         }
1521                             
1522                         //
1523                         // From any class type S to any interface T, provided S is not sealed
1524                         // and provided S does not implement T.
1525                         //
1526                         if (target_type.IsInterface && !source_type.IsSealed &&
1527                             !TypeManager.ImplementsInterface (source_type, target_type))
1528                                 return true;
1529
1530                         //
1531                         // From any interface-type S to to any class type T, provided T is not
1532                         // sealed, or provided T implements S.
1533                         //
1534                         if (source_type.IsInterface &&
1535                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1536                                 return true;
1537                         
1538                         
1539                         // From an array type S with an element type Se to an array type T with an 
1540                         // element type Te provided all the following are true:
1541                         //     * S and T differe only in element type, in other words, S and T
1542                         //       have the same number of dimensions.
1543                         //     * Both Se and Te are reference types
1544                         //     * An explicit referenc conversions exist from Se to Te
1545                         //
1546                         if (source_type.IsArray && target_type.IsArray) {
1547                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1548                                         
1549                                         Type source_element_type = TypeManager.GetElementType (source_type);
1550                                         Type target_element_type = TypeManager.GetElementType (target_type);
1551                                         
1552                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1553                                                 if (ExplicitReferenceConversionExists (source_element_type,
1554                                                                                        target_element_type))
1555                                                         return true;
1556                                 }
1557                         }
1558                         
1559
1560                         // From System.Array to any array-type
1561                         if (source_type == TypeManager.array_type &&
1562                             target_type.IsArray){
1563                                 return true;
1564                         }
1565
1566                         //
1567                         // From System delegate to any delegate-type
1568                         //
1569                         if (source_type == TypeManager.delegate_type &&
1570                             TypeManager.IsDelegateType (target_type))
1571                                 return true;
1572
1573                         //
1574                         // From ICloneable to Array or Delegate types
1575                         //
1576                         if (source_type == TypeManager.icloneable_type &&
1577                             (target_type == TypeManager.array_type ||
1578                              target_type == TypeManager.delegate_type))
1579                                 return true;
1580                         
1581                         return false;
1582                 }
1583
1584                 /// <summary>
1585                 ///   Implements Explicit Reference conversions
1586                 /// </summary>
1587                 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1588                 {
1589                         Type source_type = source.Type;
1590                         bool target_is_type_param = target_type.IsGenericParameter;
1591                         bool target_is_value_type = target_type.IsValueType;
1592
1593                         //
1594                         // From object to a generic parameter
1595                         //
1596                         if (source_type == TypeManager.object_type && target_is_type_param)
1597                                 return new UnboxCast (source, target_type);
1598
1599                         //
1600                         // From object to any reference type
1601                         //
1602                         if (source_type == TypeManager.object_type && !target_is_value_type)
1603                                 return new ClassCast (source, target_type);
1604
1605                         //
1606                         // Unboxing conversion.
1607                         //
1608                         if (((source_type == TypeManager.enum_type &&
1609                                 !(source is EmptyCast)) ||
1610                                 source_type == TypeManager.value_type) && target_is_value_type)
1611                                 return new UnboxCast (source, target_type);
1612
1613                         //
1614                         // From any class S to any class-type T, provided S is a base class of T
1615                         //
1616                         if (TypeManager.IsSubclassOf (target_type, source_type))
1617                                 return new ClassCast (source, target_type);
1618
1619                         //
1620                         // From any interface type S to any interface T provided S is not derived from T
1621                         //
1622                         if (source_type.IsInterface && target_type.IsInterface){
1623                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1624                                         return null;
1625                                 else
1626                                         return new ClassCast (source, target_type);
1627                         }
1628
1629                         //
1630                         // From any class type S to any interface T, provides S is not sealed
1631                         // and provided S does not implement T.
1632                         //
1633                         if (target_type.IsInterface && !source_type.IsSealed) {
1634                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1635                                         return null;
1636                                 else
1637                                         return new ClassCast (source, target_type);
1638                                 
1639                         }
1640
1641                         //
1642                         // From any interface-type S to to any class type T, provided T is not
1643                         // sealed, or provided T implements S.
1644                         //
1645                         if (source_type.IsInterface) {
1646                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1647                                         if (target_type.IsClass)
1648                                                 return new ClassCast (source, target_type);
1649                                         else
1650                                                 return new UnboxCast (source, target_type);
1651                                 }
1652
1653                                 return null;
1654                         }
1655                         
1656                         // From an array type S with an element type Se to an array type T with an 
1657                         // element type Te provided all the following are true:
1658                         //     * S and T differe only in element type, in other words, S and T
1659                         //       have the same number of dimensions.
1660                         //     * Both Se and Te are reference types
1661                         //     * An explicit referenc conversions exist from Se to Te
1662                         //
1663                         if (source_type.IsArray && target_type.IsArray) {
1664                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1665                                         
1666                                         Type source_element_type = TypeManager.GetElementType (source_type);
1667                                         Type target_element_type = TypeManager.GetElementType (target_type);
1668                                         
1669                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1670                                                 if (ExplicitReferenceConversionExists (source_element_type,
1671                                                                                        target_element_type))
1672                                                         return new ClassCast (source, target_type);
1673                                 }
1674                         }
1675                         
1676
1677                         // From System.Array to any array-type
1678                         if (source_type == TypeManager.array_type &&
1679                             target_type.IsArray) {
1680                                 return new ClassCast (source, target_type);
1681                         }
1682
1683                         //
1684                         // From System delegate to any delegate-type
1685                         //
1686                         if (source_type == TypeManager.delegate_type &&
1687                             TypeManager.IsDelegateType (target_type))
1688                                 return new ClassCast (source, target_type);
1689
1690                         //
1691                         // From ICloneable to Array or Delegate types
1692                         //
1693                         if (source_type == TypeManager.icloneable_type &&
1694                             (target_type == TypeManager.array_type ||
1695                              target_type == TypeManager.delegate_type))
1696                                 return new ClassCast (source, target_type);
1697                         
1698                         return null;
1699                 }
1700                 
1701                 /// <summary>
1702                 ///   Performs an explicit conversion of the expression `expr' whose
1703                 ///   type is expr.Type to `target_type'.
1704                 /// </summary>
1705                 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1706                                                              Type target_type, Location loc)
1707                 {
1708                         Type expr_type = expr.Type;
1709                         Type original_expr_type = expr_type;
1710
1711                         if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1712                                 if (target_type == TypeManager.enum_type ||
1713                                     target_type == TypeManager.object_type) {
1714                                         if (expr is EnumConstant)
1715                                                 expr = ((EnumConstant) expr).Child;
1716                                         // We really need all these casts here .... :-(
1717                                         expr = new BoxedCast (new EmptyCast (expr, expr_type));
1718                                         return new EmptyCast (expr, target_type);
1719                                 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1720                                            target_type.IsSubclassOf (TypeManager.enum_type))
1721                                         return new UnboxCast (expr, target_type);
1722
1723                                 //
1724                                 // Notice that we have kept the expr_type unmodified, which is only
1725                                 // used later on to 
1726                                 if (expr is EnumConstant)
1727                                         expr = ((EnumConstant) expr).Child;
1728                                 else
1729                                         expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1730                                 expr_type = expr.Type;
1731                         }
1732
1733                         int errors = Report.Errors;
1734                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1735                         if (Report.Errors > errors)
1736                                 return null;
1737
1738                         if (ne != null)
1739                                 return ne;
1740
1741                         ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1742                         if (ne != null)
1743                                 return ne;
1744
1745                         //
1746                         // Unboxing conversion.
1747                         //
1748                         if (expr_type == TypeManager.object_type && target_type.IsValueType){
1749                                 if (expr is NullLiteral){
1750                                         //
1751                                         // Skip the ExplicitReferenceConversion because we can not convert
1752                                         // from Null to a ValueType, and ExplicitReference wont check against
1753                                         // null literal explicitly
1754                                         //
1755                                         goto skip_explicit;
1756                                 }
1757                                 return new UnboxCast (expr, target_type);
1758
1759                         }
1760
1761                         ne = ExplicitReferenceConversion (expr, target_type);
1762                         if (ne != null)
1763                                 return ne;
1764
1765                 skip_explicit:
1766                         if (ec.InUnsafe){
1767                                 if (target_type.IsPointer){
1768                                         if (expr_type.IsPointer)
1769                                                 return new EmptyCast (expr, target_type);
1770                                         
1771                                         if (expr_type == TypeManager.sbyte_type ||
1772                                             expr_type == TypeManager.byte_type ||
1773                                             expr_type == TypeManager.short_type ||
1774                                             expr_type == TypeManager.ushort_type ||
1775                                             expr_type == TypeManager.int32_type ||
1776                                             expr_type == TypeManager.uint32_type ||
1777                                             expr_type == TypeManager.uint64_type ||
1778                                             expr_type == TypeManager.int64_type)
1779                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1780                                 }
1781                                 if (expr_type.IsPointer){
1782                                         Expression e = null;
1783                                         
1784                                         if (target_type == TypeManager.sbyte_type)
1785                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1786                                         else if (target_type == TypeManager.byte_type)
1787                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1788                                         else if (target_type == TypeManager.short_type)
1789                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1790                                         else if (target_type == TypeManager.ushort_type)
1791                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1792                                         else if (target_type == TypeManager.int32_type)
1793                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1794                                         else if (target_type == TypeManager.uint32_type)
1795                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1796                                         else if (target_type == TypeManager.uint64_type)
1797                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1798                                         else if (target_type == TypeManager.int64_type){
1799                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1800                                         }
1801
1802                                         if (e != null){
1803                                                 Expression ci, ce;
1804
1805                                                 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1806
1807                                                 if (ci != null)
1808                                                         return ci;
1809
1810                                                 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1811                                                 if (ce != null)
1812                                                         return ce;
1813                                                 //
1814                                                 // We should always be able to go from an uint32
1815                                                 // implicitly or explicitly to the other integral
1816                                                 // types
1817                                                 //
1818                                                 throw new Exception ("Internal compiler error");
1819                                         }
1820                                 }
1821                         }
1822                         
1823                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1824                         if (ne != null)
1825                                 return ne;
1826
1827                         if (expr is NullLiteral){
1828                                 Report.Error (37, loc, "Cannot convert null to value type `" +
1829                                               TypeManager.CSharpName (target_type) + "'");
1830                                 return null;
1831                         }
1832                                 
1833                         Error_CannotConvertType (loc, original_expr_type, target_type);
1834                         return null;
1835                 }
1836
1837                 /// <summary>
1838                 ///   Same as ExplicitConversion, only it doesn't include user defined conversions
1839                 /// </summary>
1840                 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1841                                                                      Type target_type, Location l)
1842                 {
1843                         int errors = Report.Errors;
1844                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1845                         if (Report.Errors > errors)
1846                                 return null;
1847
1848                         if (ne != null)
1849                                 return ne;
1850
1851                         ne = ExplicitNumericConversion (ec, expr, target_type, l);
1852                         if (ne != null)
1853                                 return ne;
1854
1855                         ne = ExplicitReferenceConversion (expr, target_type);
1856                         if (ne != null)
1857                                 return ne;
1858
1859                         Error_CannotConvertType (l, expr.Type, target_type);
1860                         return null;
1861                 }
1862         }
1863 }