2008-02-18 Zoltan Varga <vargaz@gmail.com>
[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                                         TypeManager.DropGenericTypeArguments (target_type) != TypeManager.expression_type)
906                                         return false;
907                                 
908                                 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
909                                 return ame.ImplicitStandardConversionExists (target_type);
910                         }
911
912                         return false;
913                 }
914
915                 /// <summary>
916                 ///  Finds "most encompassed type" according to the spec (13.4.2)
917                 ///  amongst the methods in the MethodGroupExpr
918                 /// </summary>
919                 static Type FindMostEncompassedType (ArrayList types)
920                 {
921                         Type best = null;
922
923                         if (types.Count == 0)
924                                 return null;
925
926                         if (types.Count == 1)
927                                 return (Type) types [0];
928
929                         EmptyExpression expr = EmptyExpression.Grab ();
930
931                         foreach (Type t in types) {
932                                 if (best == null) {
933                                         best = t;
934                                         continue;
935                                 }
936
937                                 expr.SetType (t);
938                                 if (ImplicitStandardConversionExists (expr, best))
939                                         best = t;
940                         }
941
942                         expr.SetType (best);
943                         foreach (Type t in types) {
944                                 if (best == t)
945                                         continue;
946                                 if (!ImplicitStandardConversionExists (expr, t)) {
947                                         best = null;
948                                         break;
949                                 }
950                         }
951
952                         EmptyExpression.Release (expr);
953
954                         return best;
955                 }
956
957                 /// <summary>
958                 ///  Finds "most encompassing type" according to the spec (13.4.2)
959                 ///  amongst the types in the given set
960                 /// </summary>
961                 static Type FindMostEncompassingType (ArrayList types)
962                 {
963                         Type best = null;
964
965                         if (types.Count == 0)
966                                 return null;
967
968                         if (types.Count == 1)
969                                 return (Type) types [0];
970
971                         EmptyExpression expr = EmptyExpression.Grab ();
972
973                         foreach (Type t in types) {
974                                 if (best == null) {
975                                         best = t;
976                                         continue;
977                                 }
978
979                                 expr.SetType (best);
980                                 if (ImplicitStandardConversionExists (expr, t))
981                                         best = t;
982                         }
983
984                         foreach (Type t in types) {
985                                 if (best == t)
986                                         continue;
987                                 expr.SetType (t);
988                                 if (!ImplicitStandardConversionExists (expr, best)) {
989                                         best = null;
990                                         break;
991                                 }
992                         }
993
994                         EmptyExpression.Release (expr);
995
996                         return best;
997                 }
998
999                 /// <summary>
1000                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
1001                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
1002                 ///   for explicit and implicit conversion operators.
1003                 /// </summary>
1004                 static public Type FindMostSpecificSource (IList list,
1005                                                            Expression source, bool apply_explicit_conv_rules)
1006                 {
1007                         ArrayList src_types_set = new ArrayList ();
1008
1009                         //
1010                         // If any operator converts from S then Sx = S
1011                         //
1012                         Type source_type = source.Type;
1013                         foreach (MethodBase mb in list){
1014                                 ParameterData pd = TypeManager.GetParameterData (mb);
1015                                 Type param_type = pd.ParameterType (0);
1016
1017                                 if (param_type == source_type)
1018                                         return param_type;
1019
1020                                 src_types_set.Add (param_type);
1021                         }
1022
1023                         //
1024                         // Explicit Conv rules
1025                         //
1026                         if (apply_explicit_conv_rules) {
1027                                 ArrayList candidate_set = new ArrayList ();
1028
1029                                 foreach (Type param_type in src_types_set){
1030                                         if (ImplicitStandardConversionExists (source, param_type))
1031                                                 candidate_set.Add (param_type);
1032                                 }
1033
1034                                 if (candidate_set.Count != 0)
1035                                         return FindMostEncompassedType (candidate_set);
1036                         }
1037
1038                         //
1039                         // Final case
1040                         //
1041                         if (apply_explicit_conv_rules)
1042                                 return FindMostEncompassingType (src_types_set);
1043                         else
1044                                 return FindMostEncompassedType (src_types_set);
1045                 }
1046
1047                 /// <summary>
1048                 ///  Finds the most specific target Tx according to section 13.4.4
1049                 /// </summary>
1050                 static public Type FindMostSpecificTarget (IList list,
1051                                                            Type target, bool apply_explicit_conv_rules)
1052                 {
1053                         ArrayList tgt_types_set = new ArrayList ();
1054
1055                         //
1056                         // If any operator converts to T then Tx = T
1057                         //
1058                         foreach (MethodInfo mi in list){
1059                                 Type ret_type = mi.ReturnType;
1060                                 if (ret_type == target)
1061                                         return ret_type;
1062
1063                                 tgt_types_set.Add (ret_type);
1064                         }
1065
1066                         //
1067                         // Explicit conv rules
1068                         //
1069                         if (apply_explicit_conv_rules) {
1070                                 ArrayList candidate_set = new ArrayList ();
1071
1072                                 EmptyExpression expr = EmptyExpression.Grab ();
1073
1074                                 foreach (Type ret_type in tgt_types_set){
1075                                         expr.SetType (ret_type);
1076
1077                                         if (ImplicitStandardConversionExists (expr, target))
1078                                                 candidate_set.Add (ret_type);
1079                                 }
1080
1081                                 EmptyExpression.Release (expr);
1082
1083                                 if (candidate_set.Count != 0)
1084                                         return FindMostEncompassingType (candidate_set);
1085                         }
1086
1087                         //
1088                         // Okay, final case !
1089                         //
1090                         if (apply_explicit_conv_rules)
1091                                 return FindMostEncompassedType (tgt_types_set);
1092                         else
1093                                 return FindMostEncompassingType (tgt_types_set);
1094                 }
1095
1096                 /// <summary>
1097                 ///  User-defined Implicit conversions
1098                 /// </summary>
1099                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1100                                                                  Type target, Location loc)
1101                 {
1102                         return UserDefinedConversion (ec, source, target, loc, false);
1103                 }
1104
1105                 /// <summary>
1106                 ///  User-defined Explicit conversions
1107                 /// </summary>
1108                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1109                                                                  Type target, Location loc)
1110                 {
1111                         return UserDefinedConversion (ec, source, target, loc, true);
1112                 }
1113
1114                 static void AddConversionOperators (ArrayList list,
1115                                                     Expression source, Type target_type,
1116                                                     bool look_for_explicit,
1117                                                     MethodGroupExpr mg)
1118                 {
1119                         if (mg == null)
1120                                 return;
1121
1122                         Type source_type = source.Type;
1123                         EmptyExpression expr = EmptyExpression.Grab ();
1124                         foreach (MethodInfo m in mg.Methods) {
1125                                 ParameterData pd = TypeManager.GetParameterData (m);
1126                                 Type return_type = m.ReturnType;
1127                                 Type arg_type = pd.ParameterType (0);
1128
1129                                 if (source_type != arg_type) {
1130                                         if (!ImplicitStandardConversionExists (source, arg_type)) {
1131                                                 if (!look_for_explicit)
1132                                                         continue;
1133                                                 expr.SetType (arg_type);
1134                                                 if (!ImplicitStandardConversionExists (expr, source_type))
1135                                                         continue;
1136                                         }
1137                                 }
1138
1139                                 if (target_type != return_type) {
1140                                         expr.SetType (return_type);
1141                                         if (!ImplicitStandardConversionExists (expr, target_type)) {
1142                                                 if (!look_for_explicit)
1143                                                         continue;
1144                                                 expr.SetType (target_type);
1145                                                 if (!ImplicitStandardConversionExists (expr, return_type))
1146                                                         continue;
1147                                         }
1148                                 }
1149
1150                                 list.Add (m);
1151                         }
1152
1153                         EmptyExpression.Release (expr);
1154                 }
1155
1156                 /// <summary>
1157                 ///   Compute the user-defined conversion operator from source_type to target_type.
1158                 ///   `look_for_explicit' controls whether we should also include the list of explicit operators
1159                 /// </summary>
1160                 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1161                 {
1162                         ArrayList ops = new ArrayList (4);
1163
1164                         Type source_type = source.Type;
1165
1166                         if (source_type != TypeManager.decimal_type) {
1167                                 AddConversionOperators (ops, source, target_type, look_for_explicit,
1168                                         Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1169                                 if (look_for_explicit) {
1170                                         AddConversionOperators (ops, source, target_type, look_for_explicit,
1171                                                 Expression.MethodLookup (
1172                                                         container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1173                                 }
1174                         }
1175
1176                         if (target_type != TypeManager.decimal_type) {
1177                                 AddConversionOperators (ops, source, target_type, look_for_explicit,
1178                                         Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1179                                 if (look_for_explicit) {
1180                                         AddConversionOperators (ops, source, target_type, look_for_explicit,
1181                                                 Expression.MethodLookup (
1182                                                         container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1183                                 }
1184                         }
1185
1186                         if (ops.Count == 0)
1187                                 return null;
1188
1189                         Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1190                         if (most_specific_source == null)
1191                                 return null;
1192
1193                         Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1194                         if (most_specific_target == null)
1195                                 return null;
1196
1197                         MethodInfo method = null;
1198
1199                         foreach (MethodInfo m in ops) {
1200                                 if (m.ReturnType != most_specific_target)
1201                                         continue;
1202                                 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1203                                         continue;
1204                                 // Ambiguous: more than one conversion operator satisfies the signature.
1205                                 if (method != null)
1206                                         return null;
1207                                 method = m;
1208                         }
1209
1210                         return method;
1211                 }
1212
1213                 static DoubleHash explicit_conv = new DoubleHash (100);
1214                 static DoubleHash implicit_conv = new DoubleHash (100);
1215
1216                 /// <summary>
1217                 ///   User-defined conversions
1218                 /// </summary>
1219                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1220                                                                 Type target, Location loc,
1221                                                                 bool look_for_explicit)
1222                 {
1223                         Type source_type = source.Type;
1224                         MethodInfo method = null;
1225
1226                         object o;
1227                         DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1228
1229                         if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1230                                 method = (MethodInfo) o;
1231                         } else {
1232                                 method = GetConversionOperator (null, source, target, look_for_explicit);
1233                                 if (!(source is Constant))
1234                                         hash.Insert (source_type, target, method);
1235                         }
1236
1237                         if (method == null)
1238                                 return null;
1239
1240                         Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1241
1242                         //
1243                         // This will do the conversion to the best match that we
1244                         // found.  Now we need to perform an implict standard conversion
1245                         // if the best match was not the type that we were requested
1246                         // by target.
1247                         //
1248                         if (look_for_explicit)
1249                                 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1250                         else
1251                                 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1252
1253                         if (source == null)
1254                                 return null;
1255
1256                         Expression e;
1257                         e =  new UserCast (method, source, loc);
1258                         if (e.Type != target){
1259                                 if (!look_for_explicit)
1260                                         e = ImplicitConversionStandard (ec, e, target, loc);
1261                                 else
1262                                         e = ExplicitConversionStandard (ec, e, target, loc);
1263                         }
1264
1265                         return e;
1266                 }
1267
1268                 /// <summary>
1269                 ///   Converts implicitly the resolved expression `expr' into the
1270                 ///   `target_type'.  It returns a new expression that can be used
1271                 ///   in a context that expects a `target_type'.
1272                 /// </summary>
1273                 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1274                                                              Type target_type, Location loc)
1275                 {
1276                         Expression e;
1277
1278                         if (target_type == null)
1279                                 throw new Exception ("Target type is null");
1280
1281                         e = ImplicitConversionStandard (ec, expr, target_type, loc);
1282                         if (e != null)
1283                                 return e;
1284
1285                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1286                         if (e != null)
1287                                 return e;
1288
1289                         return null;
1290                 }
1291
1292
1293                 /// <summary>
1294                 ///   Attempts to apply the `Standard Implicit
1295                 ///   Conversion' rules to the expression `expr' into
1296                 ///   the `target_type'.  It returns a new expression
1297                 ///   that can be used in a context that expects a
1298                 ///   `target_type'.
1299                 ///
1300                 ///   This is different from `ImplicitConversion' in that the
1301                 ///   user defined implicit conversions are excluded.
1302                 /// </summary>
1303                 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1304                                                                      Type target_type, Location loc)
1305                 {
1306                         Type expr_type = expr.Type;
1307                         Expression e;
1308                         
1309 #if GMCS_SOURCE
1310                         if (TypeManager.IsNullableType (target_type)) {
1311                                 //
1312                                 // From null to any nullable type
1313                                 //
1314                                 if (expr_type == TypeManager.null_type)
1315                                         return new Nullable.Null (target_type, loc);
1316                                 
1317                                 Type target = TypeManager.GetTypeArguments (target_type) [0];
1318
1319                                 if (TypeManager.IsNullableType (expr.Type)) {
1320                                         e = new Nullable.LiftedConversion (
1321                                                 expr, target_type, false, false, loc).Resolve (ec);
1322                                         if (e != null)
1323                                                 return e;
1324                                 } else {
1325                                         e = ImplicitConversion (ec, expr, target, loc);
1326                                         if (e != null)
1327                                                 return Nullable.Wrap.Create (e, ec);
1328                                 }
1329                         }
1330 #endif
1331                         if (expr.eclass == ExprClass.MethodGroup){
1332                                 if (!TypeManager.IsDelegateType (target_type)){
1333                                         return null;
1334                                 }
1335
1336                                 //
1337                                 // Only allow anonymous method conversions on post ISO_1
1338                                 //
1339                                 if (RootContext.Version != LanguageVersion.ISO_1){
1340                                         MethodGroupExpr mg = expr as MethodGroupExpr;
1341                                         if (mg != null)
1342                                                 return ImplicitDelegateCreation.Create (
1343                                                         ec, mg, target_type, loc);
1344                                 }
1345                         }
1346
1347                         if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1348                                 return expr;
1349
1350                         //
1351                         // Attempt to do the implicit constant expression conversions
1352                         //
1353                         Constant c = expr as Constant;
1354                         if (c != null) {
1355                                 //
1356                                 // If `target_type' is an interface and the type of `ic' implements the interface
1357                                 // e.g. target_type is IComparable, IConvertible, IFormattable
1358                                 //
1359                                 if (c.Type == TypeManager.int32_type && target_type.IsInterface && target_type.IsAssignableFrom (c.Type))
1360                                         return new BoxedCast (c, target_type);
1361
1362                                 try {
1363                                         c = c.ConvertImplicitly (target_type);
1364                                 } catch {
1365                                         Console.WriteLine ("Conversion error happened in line {0}", loc);
1366                                         throw;
1367                                 }
1368                                 if (c != null)
1369                                         return c;
1370                         }
1371
1372                         e = ImplicitNumericConversion (expr, target_type);
1373                         if (e != null)
1374                                 return e;
1375
1376                         e = ImplicitReferenceConversion (expr, target_type);
1377                         if (e != null)
1378                                 return e;
1379
1380                         if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1381                                 IntLiteral i = (IntLiteral) expr;
1382
1383                                 if (i.Value == 0)
1384                                         return new EnumConstant ((Constant) expr, target_type);
1385                         }
1386
1387                         if (ec.InUnsafe) {
1388                                 if (expr_type.IsPointer){
1389                                         if (target_type == TypeManager.void_ptr_type)
1390                                                 return EmptyCast.Create (expr, target_type);
1391
1392                                         //
1393                                         // yep, comparing pointer types cant be done with
1394                                         // t1 == t2, we have to compare their element types.
1395                                         //
1396                                         if (target_type.IsPointer){
1397                                                 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1398                                                         return expr;
1399
1400                                                 //return null;
1401                                         }
1402                                 }
1403
1404                                 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1405                                         return EmptyCast.Create (NullPointer.Null, target_type);
1406                         }
1407
1408                         if (expr_type == TypeManager.anonymous_method_type){
1409                                 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1410                                 Expression am = ame.Compatible (ec, target_type);
1411                                 if (am != null)
1412                                         return am.DoResolve (ec);
1413                         }
1414
1415                         return null;
1416                 }
1417
1418                 /// <summary>
1419                 ///   Attempts to implicitly convert `source' into `target_type', using
1420                 ///   ImplicitConversion.  If there is no implicit conversion, then
1421                 ///   an error is signaled
1422                 /// </summary>
1423                 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1424                                                                      Type target_type, Location loc)
1425                 {
1426                         Expression e = ImplicitConversion (ec, source, target_type, loc);
1427                         if (e != null)
1428                                 return e;
1429
1430                         source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1431                         return null;
1432                 }
1433
1434                 /// <summary>
1435                 ///   Performs the explicit numeric conversions
1436                 ///
1437                 /// There are a few conversions that are not part of the C# standard,
1438                 /// they were interim hacks in the C# compiler that were supposed to
1439                 /// become explicit operators in the UIntPtr class and IntPtr class,
1440                 /// but for historical reasons it did not happen, so the C# compiler
1441                 /// ended up with these special hacks.
1442                 ///
1443                 /// See bug 59800 for details.
1444                 ///
1445                 /// The conversion are:
1446                 ///   UIntPtr->SByte
1447                 ///   UIntPtr->Int16
1448                 ///   UIntPtr->Int32
1449                 ///   IntPtr->UInt64
1450                 ///   UInt64->IntPtr
1451                 ///   SByte->UIntPtr
1452                 ///   Int16->UIntPtr
1453                 ///
1454                 /// </summary>
1455                 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1456                 {
1457                         Type expr_type = expr.Type;
1458                         Type real_target_type = target_type;
1459
1460                         if (expr_type == TypeManager.sbyte_type){
1461                                 //
1462                                 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1463                                 //
1464                                 if (real_target_type == TypeManager.byte_type)
1465                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1466                                 if (real_target_type == TypeManager.ushort_type)
1467                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1468                                 if (real_target_type == TypeManager.uint32_type)
1469                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1470                                 if (real_target_type == TypeManager.uint64_type)
1471                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1472                                 if (real_target_type == TypeManager.char_type)
1473                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1474
1475                                 // One of the built-in conversions that belonged in the class library
1476                                 if (real_target_type == TypeManager.uintptr_type){
1477                                         Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1478
1479                                         return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1480                                 }
1481                         } else if (expr_type == TypeManager.byte_type){
1482                                 //
1483                                 // From byte to sbyte and char
1484                                 //
1485                                 if (real_target_type == TypeManager.sbyte_type)
1486                                         return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1487                                 if (real_target_type == TypeManager.char_type)
1488                                         return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1489                         } else if (expr_type == TypeManager.short_type){
1490                                 //
1491                                 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1492                                 //
1493                                 if (real_target_type == TypeManager.sbyte_type)
1494                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1495                                 if (real_target_type == TypeManager.byte_type)
1496                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1497                                 if (real_target_type == TypeManager.ushort_type)
1498                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1499                                 if (real_target_type == TypeManager.uint32_type)
1500                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1501                                 if (real_target_type == TypeManager.uint64_type)
1502                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1503                                 if (real_target_type == TypeManager.char_type)
1504                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1505
1506                                 // One of the built-in conversions that belonged in the class library
1507                                 if (real_target_type == TypeManager.uintptr_type){
1508                                         Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1509
1510                                         return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1511                                 }
1512                         } else if (expr_type == TypeManager.ushort_type){
1513                                 //
1514                                 // From ushort to sbyte, byte, short, char
1515                                 //
1516                                 if (real_target_type == TypeManager.sbyte_type)
1517                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1518                                 if (real_target_type == TypeManager.byte_type)
1519                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1520                                 if (real_target_type == TypeManager.short_type)
1521                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1522                                 if (real_target_type == TypeManager.char_type)
1523                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1524                         } else if (expr_type == TypeManager.int32_type){
1525                                 //
1526                                 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1527                                 //
1528                                 if (real_target_type == TypeManager.sbyte_type)
1529                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1530                                 if (real_target_type == TypeManager.byte_type)
1531                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1532                                 if (real_target_type == TypeManager.short_type)
1533                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1534                                 if (real_target_type == TypeManager.ushort_type)
1535                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1536                                 if (real_target_type == TypeManager.uint32_type)
1537                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1538                                 if (real_target_type == TypeManager.uint64_type)
1539                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1540                                 if (real_target_type == TypeManager.char_type)
1541                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1542
1543                                 // One of the built-in conversions that belonged in the class library
1544                                 if (real_target_type == TypeManager.uintptr_type){
1545                                         Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1546
1547                                         return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1548                                 }
1549                         } else if (expr_type == TypeManager.uint32_type){
1550                                 //
1551                                 // From uint to sbyte, byte, short, ushort, int, char
1552                                 //
1553                                 if (real_target_type == TypeManager.sbyte_type)
1554                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1555                                 if (real_target_type == TypeManager.byte_type)
1556                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1557                                 if (real_target_type == TypeManager.short_type)
1558                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1559                                 if (real_target_type == TypeManager.ushort_type)
1560                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1561                                 if (real_target_type == TypeManager.int32_type)
1562                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1563                                 if (real_target_type == TypeManager.char_type)
1564                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1565                         } else if (expr_type == TypeManager.int64_type){
1566                                 //
1567                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1568                                 //
1569                                 if (real_target_type == TypeManager.sbyte_type)
1570                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1571                                 if (real_target_type == TypeManager.byte_type)
1572                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1573                                 if (real_target_type == TypeManager.short_type)
1574                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1575                                 if (real_target_type == TypeManager.ushort_type)
1576                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1577                                 if (real_target_type == TypeManager.int32_type)
1578                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1579                                 if (real_target_type == TypeManager.uint32_type)
1580                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1581                                 if (real_target_type == TypeManager.uint64_type)
1582                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1583                                 if (real_target_type == TypeManager.char_type)
1584                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1585                         } else if (expr_type == TypeManager.uint64_type){
1586                                 //
1587                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1588                                 //
1589                                 if (real_target_type == TypeManager.sbyte_type)
1590                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1591                                 if (real_target_type == TypeManager.byte_type)
1592                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1593                                 if (real_target_type == TypeManager.short_type)
1594                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1595                                 if (real_target_type == TypeManager.ushort_type)
1596                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1597                                 if (real_target_type == TypeManager.int32_type)
1598                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1599                                 if (real_target_type == TypeManager.uint32_type)
1600                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1601                                 if (real_target_type == TypeManager.int64_type)
1602                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1603                                 if (real_target_type == TypeManager.char_type)
1604                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1605
1606                                 // One of the built-in conversions that belonged in the class library
1607                                 if (real_target_type == TypeManager.intptr_type){
1608                                         return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1609                                                                  TypeManager.intptr_type, true);
1610                                 }
1611                         } else if (expr_type == TypeManager.char_type){
1612                                 //
1613                                 // From char to sbyte, byte, short
1614                                 //
1615                                 if (real_target_type == TypeManager.sbyte_type)
1616                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1617                                 if (real_target_type == TypeManager.byte_type)
1618                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1619                                 if (real_target_type == TypeManager.short_type)
1620                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1621                         } else if (expr_type == TypeManager.float_type){
1622                                 //
1623                                 // From float to sbyte, byte, short,
1624                                 // ushort, int, uint, long, ulong, char
1625                                 // or decimal
1626                                 //
1627                                 if (real_target_type == TypeManager.sbyte_type)
1628                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1629                                 if (real_target_type == TypeManager.byte_type)
1630                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1631                                 if (real_target_type == TypeManager.short_type)
1632                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1633                                 if (real_target_type == TypeManager.ushort_type)
1634                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1635                                 if (real_target_type == TypeManager.int32_type)
1636                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1637                                 if (real_target_type == TypeManager.uint32_type)
1638                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1639                                 if (real_target_type == TypeManager.int64_type)
1640                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1641                                 if (real_target_type == TypeManager.uint64_type)
1642                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1643                                 if (real_target_type == TypeManager.char_type)
1644                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1645                                 if (real_target_type == TypeManager.decimal_type)
1646                                         return new CastToDecimal (expr, true);
1647                         } else if (expr_type == TypeManager.double_type){
1648                                 //
1649                                 // From double to sbyte, byte, short,
1650                                 // ushort, int, uint, long, ulong,
1651                                 // char, float or decimal
1652                                 //
1653                                 if (real_target_type == TypeManager.sbyte_type)
1654                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1655                                 if (real_target_type == TypeManager.byte_type)
1656                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1657                                 if (real_target_type == TypeManager.short_type)
1658                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1659                                 if (real_target_type == TypeManager.ushort_type)
1660                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1661                                 if (real_target_type == TypeManager.int32_type)
1662                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1663                                 if (real_target_type == TypeManager.uint32_type)
1664                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1665                                 if (real_target_type == TypeManager.int64_type)
1666                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1667                                 if (real_target_type == TypeManager.uint64_type)
1668                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1669                                 if (real_target_type == TypeManager.char_type)
1670                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1671                                 if (real_target_type == TypeManager.float_type)
1672                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1673                                 if (real_target_type == TypeManager.decimal_type)
1674                                         return new CastToDecimal (expr, true);
1675                         } else if (expr_type == TypeManager.uintptr_type){
1676                                 //
1677                                 // Various built-in conversions that belonged in the class library
1678                                 //
1679                                 // from uintptr to sbyte, short, int32
1680                                 //
1681                                 if (real_target_type == TypeManager.sbyte_type){
1682                                         Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1683                                         return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1684                                 }
1685                                 if (real_target_type == TypeManager.short_type){
1686                                         Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1687                                         return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1688                                 }
1689                                 if (real_target_type == TypeManager.int32_type){
1690                                         return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1691                                                               TypeManager.int32_type);
1692                                 }
1693                         } else if (expr_type == TypeManager.intptr_type){
1694                                 if (real_target_type == TypeManager.uint64_type){
1695                                         return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1696                                                               TypeManager.uint64_type);
1697                                 }
1698                         } else if (expr_type == TypeManager.decimal_type) {
1699                                 return new CastFromDecimal (expr, target_type).Resolve ();
1700                         }
1701                         return null;
1702                 }
1703
1704                 /// <summary>
1705                 ///  Returns whether an explicit reference conversion can be performed
1706                 ///  from source_type to target_type
1707                 /// </summary>
1708                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1709                 {
1710                         bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1711                         bool target_is_value_type = target_type.IsValueType;
1712
1713                         if (source_type == target_type)
1714                                 return true;
1715
1716                         //
1717                         // From generic parameter to any type
1718                         //
1719                         if (TypeManager.IsGenericParameter (source_type))
1720                                 return ExplicitTypeParameterConversionExists (source_type, target_type);
1721
1722                         //
1723                         // From object to a generic parameter
1724                         //
1725                         if (source_type == TypeManager.object_type && target_is_type_param)
1726                                 return true;
1727
1728                         //
1729                         // From object to any reference type
1730                         //
1731                         if (source_type == TypeManager.object_type && !target_is_value_type)
1732                                 return true;
1733
1734                         //
1735                         // From any class S to any class-type T, provided S is a base class of T
1736                         //
1737                         if (TypeManager.IsSubclassOf (target_type, source_type))
1738                                 return true;
1739
1740                         //
1741                         // From any interface type S to any interface T provided S is not derived from T
1742                         //
1743                         if (source_type.IsInterface && target_type.IsInterface){
1744                                 if (!TypeManager.IsSubclassOf (target_type, source_type))
1745                                         return true;
1746                         }
1747
1748                         //
1749                         // From any class type S to any interface T, provided S is not sealed
1750                         // and provided S does not implement T.
1751                         //
1752                         if (target_type.IsInterface && !source_type.IsSealed &&
1753                             !TypeManager.ImplementsInterface (source_type, target_type))
1754                                 return true;
1755
1756                         //
1757                         // From any interface-type S to to any class type T, provided T is not
1758                         // sealed, or provided T implements S.
1759                         //
1760                         if (source_type.IsInterface &&
1761                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1762                                 return true;
1763
1764
1765                         if (source_type.IsInterface && IList_To_Array (source_type, target_type))
1766                                 return true;
1767
1768                         // From an array type S with an element type Se to an array type T with an
1769                         // element type Te provided all the following are true:
1770                         //     * S and T differe only in element type, in other words, S and T
1771                         //       have the same number of dimensions.
1772                         //     * Both Se and Te are reference types
1773                         //     * An explicit referenc conversions exist from Se to Te
1774                         //
1775                         if (source_type.IsArray && target_type.IsArray) {
1776                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1777
1778                                         Type source_element_type = TypeManager.GetElementType (source_type);
1779                                         Type target_element_type = TypeManager.GetElementType (target_type);
1780
1781                                         if (TypeManager.IsGenericParameter (source_element_type) ||
1782                                             (!source_element_type.IsValueType && !target_element_type.IsValueType))
1783                                                 if (ExplicitReferenceConversionExists (source_element_type,
1784                                                                                        target_element_type))
1785                                                         return true;
1786                                 }
1787                         }
1788
1789
1790                         // From System.Array to any array-type
1791                         if (source_type == TypeManager.array_type &&
1792                             target_type.IsArray){
1793                                 return true;
1794                         }
1795
1796                         //
1797                         // From System delegate to any delegate-type
1798                         //
1799                         if (source_type == TypeManager.delegate_type &&
1800                             TypeManager.IsDelegateType (target_type))
1801                                 return true;
1802
1803                         //
1804                         // From ICloneable to Array or Delegate types
1805                         //
1806                         if (source_type == TypeManager.icloneable_type &&
1807                             (target_type == TypeManager.array_type ||
1808                              target_type == TypeManager.delegate_type))
1809                                 return true;
1810
1811                         return false;
1812                 }
1813
1814                 /// <summary>
1815                 ///   Implements Explicit Reference conversions
1816                 /// </summary>
1817                 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1818                 {
1819                         Type source_type = source.Type;
1820                         bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1821                         bool target_is_value_type = target_type.IsValueType;
1822
1823                         //
1824                         // From object to a generic parameter
1825                         //
1826                         if (source_type == TypeManager.object_type && target_is_type_param)
1827                                 return new UnboxCast (source, target_type);
1828
1829                         //
1830                         // Explicit type parameter conversion.
1831                         //
1832
1833                         if (TypeManager.IsGenericParameter (source_type))
1834                                 return ExplicitTypeParameterConversion (source, target_type);
1835
1836                         //
1837                         // From object to any reference type
1838                         //
1839                         if (source_type == TypeManager.object_type && !target_is_value_type)
1840                                 return new ClassCast (source, target_type);
1841
1842                         //
1843                         // Unboxing conversion.
1844                         //
1845                         if (((source_type == TypeManager.enum_type &&
1846                                 !(source is EmptyCast)) ||
1847                                 source_type == TypeManager.value_type) && target_is_value_type)
1848                                 return new UnboxCast (source, target_type);
1849
1850                         //
1851                         // From any class S to any class-type T, provided S is a base class of T
1852                         //
1853                         if (TypeManager.IsSubclassOf (target_type, source_type))
1854                                 return new ClassCast (source, target_type);
1855
1856                         //
1857                         // From any interface type S to any interface T provided S is not derived from T
1858                         //
1859                         if (source_type.IsInterface && target_type.IsInterface){
1860                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1861                                         return null;
1862                                 else
1863                                         return new ClassCast (source, target_type);
1864                         }
1865
1866                         //
1867                         // From any class type S to any interface T, provides S is not sealed
1868                         // and provided S does not implement T.
1869                         //
1870                         if (target_type.IsInterface && !source_type.IsSealed) {
1871                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1872                                         return null;
1873                                 else
1874                                         return new ClassCast (source, target_type);
1875
1876                         }
1877
1878                         //
1879                         // From any interface-type S to to any class type T, provided T is not
1880                         // sealed, or provided T implements S.
1881                         //
1882                         if (source_type.IsInterface) {
1883                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1884                                         if (target_type.IsClass)
1885                                                 return new ClassCast (source, target_type);
1886                                         else
1887                                                 return new UnboxCast (source, target_type);
1888                                 }
1889
1890                                 //
1891                                 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1892                                 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1893                                 //
1894                                 if (IList_To_Array (source_type, target_type))
1895                                         return new ClassCast (source, target_type);
1896
1897                                 return null;
1898                         }
1899
1900                         // From an array type S with an element type Se to an array type T with an
1901                         // element type Te provided all the following are true:
1902                         //     * S and T differe only in element type, in other words, S and T
1903                         //       have the same number of dimensions.
1904                         //     * Both Se and Te are reference types
1905                         //     * An explicit referenc conversions exist from Se to Te
1906                         //
1907                         if (source_type.IsArray && target_type.IsArray) {
1908                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1909
1910                                         Type source_element_type = TypeManager.GetElementType (source_type);
1911                                         Type target_element_type = TypeManager.GetElementType (target_type);
1912
1913                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1914                                                 if (ExplicitReferenceConversionExists (source_element_type,
1915                                                                                        target_element_type))
1916                                                         return new ClassCast (source, target_type);
1917                                 }
1918                         }
1919
1920
1921                         // From System.Array to any array-type
1922                         if (source_type == TypeManager.array_type &&
1923                             target_type.IsArray) {
1924                                 return new ClassCast (source, target_type);
1925                         }
1926
1927                         //
1928                         // From System delegate to any delegate-type
1929                         //
1930                         if (source_type == TypeManager.delegate_type &&
1931                             TypeManager.IsDelegateType (target_type))
1932                                 return new ClassCast (source, target_type);
1933
1934                         //
1935                         // From ICloneable to Array or Delegate types
1936                         //
1937                         if (source_type == TypeManager.icloneable_type &&
1938                             (target_type == TypeManager.array_type ||
1939                              target_type == TypeManager.delegate_type))
1940                                 return new ClassCast (source, target_type);
1941
1942                         return null;
1943                 }
1944
1945                 /// <summary>
1946                 ///   Performs an explicit conversion of the expression `expr' whose
1947                 ///   type is expr.Type to `target_type'.
1948                 /// </summary>
1949                 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1950                                                                  Type target_type, Location loc)
1951                 {
1952                         Type expr_type = expr.Type;
1953
1954                         // Explicit conversion includes implicit conversion and it used for enum underlying types too
1955                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1956                         if (ne != null)
1957                                 return ne;
1958
1959                         //
1960                         // Unboxing conversions; only object types can be convertible to enum
1961                         //
1962                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
1963                                 return new UnboxCast (expr, target_type);
1964
1965                         if (TypeManager.IsEnumType (expr_type)) {
1966                                 Expression underlying = EmptyCast.Create (expr, TypeManager.EnumToUnderlying (expr_type));
1967                                 expr = ExplicitConversionCore (ec, underlying, target_type, loc);
1968                                 if (expr != null)
1969                                         return expr;
1970
1971                                 return ExplicitUserConversion (ec, underlying, target_type, loc);                               
1972                         }
1973
1974                         if (TypeManager.IsEnumType (target_type)){
1975                                 if (expr_type == TypeManager.enum_type)
1976                                         return new UnboxCast (expr, target_type);
1977
1978                                 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1979                                 if (ce != null)
1980                                         return EmptyCast.Create (ce, target_type);
1981                                 
1982                                 //
1983                                 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1984                                 //
1985                 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1986                                         ne = ExplicitUserConversion (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1987                                         if (ne != null)
1988                                                 return ExplicitConversionCore (ec, ne, target_type, loc);
1989                 }
1990                                 
1991                                 return null;
1992                         }
1993
1994                         ne = ExplicitNumericConversion (expr, target_type);
1995                         if (ne != null)
1996                                 return ne;
1997
1998                         //
1999                         // Skip the ExplicitReferenceConversion because we can not convert
2000                         // from Null to a ValueType, and ExplicitReference wont check against
2001                         // null literal explicitly
2002                         //
2003                         if (expr_type != TypeManager.null_type){
2004                                 ne = ExplicitReferenceConversion (expr, target_type);
2005                                 if (ne != null)
2006                                         return ne;
2007                         }
2008
2009                         if (ec.InUnsafe){
2010                                 ne = ExplicitUnsafe (expr, target_type);
2011                                 if (ne != null)
2012                                         return ne;
2013                         }
2014                         
2015                         return null;
2016                 }
2017
2018                 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
2019                 {
2020                         Type expr_type = expr.Type;
2021
2022                         if (target_type.IsPointer){
2023                                 if (expr_type.IsPointer)
2024                                         return EmptyCast.Create (expr, target_type);
2025
2026                                 if (expr_type == TypeManager.sbyte_type ||
2027                                         expr_type == TypeManager.short_type ||
2028                                         expr_type == TypeManager.int32_type ||
2029                                         expr_type == TypeManager.int64_type)
2030                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
2031
2032                                 if (expr_type == TypeManager.ushort_type ||
2033                                         expr_type == TypeManager.uint32_type ||
2034                                         expr_type == TypeManager.uint64_type ||
2035                                         expr_type == TypeManager.byte_type)
2036                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2037                         }
2038
2039                         if (expr_type.IsPointer){
2040                                 if (target_type == TypeManager.sbyte_type)
2041                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2042                                 else if (target_type == TypeManager.byte_type)
2043                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2044                                 else if (target_type == TypeManager.short_type)
2045                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2046                                 else if (target_type == TypeManager.ushort_type)
2047                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2048                                 else if (target_type == TypeManager.int32_type)
2049                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2050                                 else if (target_type == TypeManager.uint32_type)
2051                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2052                                 else if (target_type == TypeManager.uint64_type)
2053                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2054                                 else if (target_type == TypeManager.int64_type){
2055                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2056                                 }
2057                         }
2058                         return null;
2059                 }
2060
2061                 /// <summary>
2062                 ///   Same as ExplicitConversion, only it doesn't include user defined conversions
2063                 /// </summary>
2064                 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2065                                                                      Type target_type, Location l)
2066                 {
2067                         int errors = Report.Errors;
2068                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2069                         if (Report.Errors > errors)
2070                                 return null;
2071
2072                         if (ne != null)
2073                                 return ne;
2074
2075                         ne = ExplicitNumericConversion (expr, target_type);
2076                         if (ne != null)
2077                                 return ne;
2078
2079                         ne = ExplicitReferenceConversion (expr, target_type);
2080                         if (ne != null)
2081                                 return ne;
2082
2083                         if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2084                                 return EmptyCast.Create (expr, target_type);
2085
2086                         expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
2087                         return null;
2088                 }
2089
2090                 /// <summary>
2091                 ///   Performs an explicit conversion of the expression `expr' whose
2092                 ///   type is expr.Type to `target_type'.
2093                 /// </summary>
2094                 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2095                         Type target_type, Location loc)
2096                 {
2097                         Expression e;
2098 #if GMCS_SOURCE
2099                         Type expr_type = expr.Type;
2100                         if (TypeManager.IsNullableType (target_type)) {
2101                                 if (TypeManager.IsNullableType (expr_type)) {
2102                                         e = new Nullable.LiftedConversion (
2103                                                 expr, target_type, false, true, loc).Resolve (ec);
2104                                         if (e != null)
2105                                                 return e;
2106                                 } else if (expr_type == TypeManager.object_type) {
2107                                         return new UnboxCast (expr, target_type);
2108                                 } else {
2109                                         Type target = TypeManager.GetTypeArguments (target_type) [0];
2110
2111                                         e = ExplicitConversionCore (ec, expr, target, loc);
2112                                         if (e != null)
2113                                                 return Nullable.Wrap.Create (e, ec);
2114                                 }
2115                         } else if (TypeManager.IsNullableType (expr_type)) {
2116                                 e = ExplicitConversion (ec, Nullable.Unwrap.Create (expr, ec), target_type, loc);
2117                                 if (e != null)
2118                                         e = EmptyCast.Create (e, target_type);
2119                                 return e;
2120                         }
2121 #endif
2122                         e = ExplicitConversionCore (ec, expr, target_type, loc);
2123                         if (e != null)
2124                                 return e;
2125                         
2126                         e = ExplicitUserConversion (ec, expr, target_type, loc);
2127                         if (e != null)
2128                                 return e;                       
2129
2130                         expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);
2131                         return null;
2132                 }
2133         }
2134 }