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