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