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