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