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