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