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