2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright 2001, 2002, 2003 Ximian, Inc.
10 // Copyright 2003-2008 Novell, Inc.
13 namespace Mono.CSharp {
15 using System.Collections;
16 using System.Diagnostics;
17 using System.Reflection;
18 using System.Reflection.Emit;
21 // A container class for all the conversion operations
23 public class Convert {
25 static bool TypeParameter_to_Null (Type target_type)
27 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
31 if (gc.HasReferenceTypeConstraint)
33 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
39 static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
41 ArrayList list = new ArrayList ();
42 list.Add (gc.EffectiveBaseClass);
43 foreach (Type t in gc.InterfaceConstraints) {
44 if (!TypeManager.IsGenericParameter (t))
47 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
49 list.Add (TypeParam_EffectiveBaseType (new_gc));
51 return FindMostEncompassedType (list);
56 // From a one-dimensional array-type S[] to System.Collections.IList<S> and base
57 // interfaces of this interface.
59 // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
60 // interfaces of this interface, provided there is an implicit reference conversion
63 static bool Array_To_IList (Type array, Type list)
66 if (!array.IsArray || (array.GetArrayRank () != 1) || !list.IsGenericType)
69 Type gt = list.GetGenericTypeDefinition ();
70 if ((gt != TypeManager.generic_ilist_type) &&
71 (gt != TypeManager.generic_icollection_type) &&
72 (gt != TypeManager.generic_ienumerable_type))
75 Type element_type = TypeManager.GetElementType (array);
76 Type arg_type = TypeManager.GetTypeArguments (list) [0];
78 if (element_type == arg_type)
81 if (MyEmptyExpr == null)
82 MyEmptyExpr = new EmptyExpression ();
83 MyEmptyExpr.SetType (TypeManager.GetElementType (array));
85 return ImplicitReferenceConversionCore (MyEmptyExpr, arg_type);
91 static bool IList_To_Array(Type list, Type array)
94 if (!list.IsGenericType || !array.IsArray || array.GetArrayRank() != 1)
97 Type gt = list.GetGenericTypeDefinition();
98 if (gt != TypeManager.generic_ilist_type &&
99 gt != TypeManager.generic_icollection_type &&
100 gt != TypeManager.generic_ienumerable_type)
103 Type arg_type = TypeManager.GetTypeArguments(list)[0];
104 Type element_type = TypeManager.GetElementType(array);
106 if (element_type == arg_type)
109 if (MyEmptyExpr == null)
110 MyEmptyExpr = new EmptyExpression();
111 MyEmptyExpr.SetType(element_type);
112 return ImplicitReferenceConversionExists(MyEmptyExpr, arg_type) || ExplicitReferenceConversionExists(element_type, arg_type);
118 static Expression ImplicitTypeParameterConversion (Expression expr,
122 Type expr_type = expr.Type;
124 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
127 if (target_type == TypeManager.object_type)
128 return new BoxedCast (expr, target_type);
133 // We're converting from a type parameter which is known to be a reference type.
134 Type base_type = TypeParam_EffectiveBaseType (gc);
136 if (TypeManager.IsSubclassOf (base_type, target_type))
137 return new ClassCast (expr, target_type);
139 if (target_type.IsInterface) {
140 if (TypeManager.ImplementsInterface (base_type, target_type))
141 return new ClassCast (expr, target_type);
143 foreach (Type t in gc.InterfaceConstraints) {
144 if (TypeManager.IsSubclassOf (t, target_type))
145 return new ClassCast (expr, target_type);
146 if (TypeManager.ImplementsInterface (t, target_type))
147 return new ClassCast (expr, target_type);
151 foreach (Type t in gc.InterfaceConstraints) {
152 if (!TypeManager.IsGenericParameter (t))
154 if (TypeManager.IsSubclassOf (t, target_type))
155 return new ClassCast (expr, target_type);
156 if (TypeManager.ImplementsInterface (t, target_type))
157 return new ClassCast (expr, target_type);
163 static bool ImplicitTypeParameterBoxingConversion (Type expr_type, Type target_type,
164 out bool use_class_cast)
167 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
170 use_class_cast = false;
171 return target_type == TypeManager.object_type;
174 use_class_cast = true;
176 if (!gc.HasReferenceTypeConstraint)
179 // We're converting from a type parameter which is known to be a reference type.
180 Type base_type = TypeParam_EffectiveBaseType (gc);
182 if (TypeManager.IsSubclassOf (base_type, target_type))
185 if (target_type.IsInterface) {
186 if (TypeManager.ImplementsInterface (base_type, target_type))
189 foreach (Type t in gc.InterfaceConstraints) {
190 if (TypeManager.IsSubclassOf (t, target_type))
192 if (TypeManager.ImplementsInterface (t, target_type))
197 foreach (Type t in gc.InterfaceConstraints) {
198 if (!TypeManager.IsGenericParameter (t))
200 if (TypeManager.IsSubclassOf (t, target_type))
202 if (TypeManager.ImplementsInterface (t, target_type))
207 use_class_cast = false;
211 static bool ExplicitTypeParameterConversionExists (Type source_type, Type target_type)
214 if (TypeManager.IsGenericParameter (target_type)) {
215 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
219 foreach (Type iface in gc.InterfaceConstraints) {
220 if (!TypeManager.IsGenericParameter (iface))
223 if (TypeManager.IsSubclassOf (source_type, iface))
228 return target_type.IsInterface;
231 static Expression ExplicitTypeParameterConversion (Expression source, Type target_type)
234 Type source_type = source.Type;
236 if (TypeManager.IsGenericParameter (target_type)) {
237 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
241 foreach (Type iface in gc.InterfaceConstraints) {
242 if (!TypeManager.IsGenericParameter (iface))
245 if (TypeManager.IsSubclassOf (source_type, iface))
246 return new ClassCast (source, target_type);
250 if (target_type.IsInterface)
251 return new ClassCast (source, target_type);
256 static EmptyExpression MyEmptyExpr;
257 static Expression ImplicitReferenceConversion (Expression expr, Type target_type, bool explicit_cast)
259 Type expr_type = expr.Type;
261 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
262 // if we are a method group, emit a warning
267 if (expr_type == TypeManager.void_type)
270 if (TypeManager.IsGenericParameter (expr_type))
271 return ImplicitTypeParameterConversion (expr, target_type);
274 // from the null type to any reference-type.
276 NullLiteral nl = expr as NullLiteral;
278 return nl.ConvertImplicitly(target_type);
281 if (ImplicitReferenceConversionCore (expr, target_type)) {
283 // Reduce implicit reference conversion to object
285 if (!explicit_cast && target_type == TypeManager.object_type)
288 return EmptyCast.Create (expr, target_type);
292 if (ImplicitBoxingConversionExists (expr, target_type, out use_class_cast)) {
294 return new ClassCast (expr, target_type);
296 return new BoxedCast (expr, target_type);
302 static public bool ImplicitReferenceConversionCore (Expression expr, Type target_type)
304 Type expr_type = expr.Type;
307 // notice that it is possible to write "ValueType v = 1", the ValueType here
308 // is an abstract class, and not really a value type, so we apply the same rules.
310 if (target_type == TypeManager.object_type) {
312 // A pointer type cannot be converted to object
314 if (expr_type.IsPointer)
317 if (TypeManager.IsValueType (expr_type))
319 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
320 return expr_type != TypeManager.anonymous_method_type;
324 } else if (target_type == TypeManager.value_type) {
325 return expr_type == TypeManager.enum_type;
326 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
328 // Special case: enumeration to System.Enum.
329 // System.Enum is not a value type, it is a class, so we need
330 // a boxing conversion
332 if (target_type == TypeManager.enum_type || TypeManager.IsGenericParameter (expr_type))
338 // This code is kind of mirrored inside ImplicitStandardConversionExists
339 // with the small distinction that we only probe there
341 // Always ensure that the code here and there is in sync
343 // from any class-type S to any interface-type T.
344 if (target_type.IsInterface) {
345 if (TypeManager.ImplementsInterface (expr_type, target_type)){
346 return !TypeManager.IsGenericParameter (expr_type) &&
347 !TypeManager.IsValueType (expr_type);
351 // from any interface type S to interface-type T.
352 if (expr_type.IsInterface && target_type.IsInterface) {
353 return TypeManager.ImplementsInterface (expr_type, target_type);
356 // from an array-type S to an array-type of type T
357 if (expr_type.IsArray && target_type.IsArray) {
358 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
360 Type expr_element_type = TypeManager.GetElementType (expr_type);
362 if (MyEmptyExpr == null)
363 MyEmptyExpr = new EmptyExpression ();
365 MyEmptyExpr.SetType (expr_element_type);
366 Type target_element_type = TypeManager.GetElementType (target_type);
368 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
369 return ImplicitStandardConversionExists (
370 MyEmptyExpr, target_element_type);
374 // from an array-type to System.Array
375 if (expr_type.IsArray && target_type == TypeManager.array_type)
378 // from an array-type of type T to IList<T>
379 if (expr_type.IsArray && Array_To_IList (expr_type, target_type))
382 // from any delegate type to System.Delegate
383 if (target_type == TypeManager.delegate_type &&
384 (expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)))
387 // from a generic type definition to a generic instance.
388 if (TypeManager.IsEqual (expr_type, target_type))
394 static public bool ImplicitBoxingConversionExists (Expression expr, Type target_type,
395 out bool use_class_cast)
397 Type expr_type = expr.Type;
398 use_class_cast = false;
401 // From any value-type to the type object.
403 if (target_type == TypeManager.object_type) {
405 // A pointer type cannot be converted to object
407 if (expr_type.IsPointer)
410 return TypeManager.IsValueType (expr_type);
414 // From any value-type to the type System.ValueType.
416 if (target_type == TypeManager.value_type)
417 return TypeManager.IsValueType (expr_type);
419 if (target_type == TypeManager.enum_type) {
421 // From any enum-type to the type System.Enum.
423 if (expr_type.IsEnum)
426 // From any nullable-type with an underlying enum-type to the type System.Enum
428 if (TypeManager.IsNullableType (expr_type))
429 return TypeManager.GetTypeArguments (expr_type) [0].IsEnum;
432 if (TypeManager.IsSubclassOf (expr_type, target_type)) {
434 // Don't box same type arguments
436 if (TypeManager.IsGenericParameter (expr_type) && expr_type != target_type)
442 // This code is kind of mirrored inside ImplicitStandardConversionExists
443 // with the small distinction that we only probe there
445 // Always ensure that the code here and there is in sync
447 // from any class-type S to any interface-type T.
448 if (target_type.IsInterface) {
449 if (TypeManager.ImplementsInterface (expr_type, target_type))
450 return TypeManager.IsGenericParameter (expr_type) ||
451 TypeManager.IsValueType (expr_type);
454 if (TypeManager.IsGenericParameter (expr_type))
455 return ImplicitTypeParameterBoxingConversion (
456 expr_type, target_type, out use_class_cast);
462 // Tests whether an implicit reference conversion exists between expr_type
465 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
467 if (target_type.IsValueType)
470 Type expr_type = expr.Type;
472 // from the null type to any reference-type.
473 if (expr_type == TypeManager.null_type){
474 return target_type != TypeManager.anonymous_method_type;
477 if (TypeManager.IsGenericParameter (expr_type))
478 return ImplicitTypeParameterConversion (expr, target_type) != null;
481 if (ImplicitReferenceConversionCore (expr, target_type) ||
482 ImplicitBoxingConversionExists (expr, target_type, out use_class_cast))
489 /// Implicit Numeric Conversions.
491 /// expr is the expression to convert, returns a new expression of type
492 /// target_type or null if an implicit conversion is not possible.
494 static public Expression ImplicitNumericConversion (Expression expr,
497 Type expr_type = expr.Type;
498 Type real_target_type = target_type;
500 if (expr_type == TypeManager.sbyte_type){
502 // From sbyte to short, int, long, float, double, decimal
504 if (real_target_type == TypeManager.int32_type)
505 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
506 if (real_target_type == TypeManager.int64_type)
507 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
508 if (real_target_type == TypeManager.double_type)
509 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
510 if (real_target_type == TypeManager.float_type)
511 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
512 if (real_target_type == TypeManager.short_type)
513 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
514 if (real_target_type == TypeManager.decimal_type)
515 return new CastToDecimal (expr);
516 } else if (expr_type == TypeManager.byte_type){
518 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
520 if (real_target_type == TypeManager.int32_type || real_target_type == TypeManager.uint32_type ||
521 real_target_type == TypeManager.short_type || real_target_type == TypeManager.ushort_type)
522 return EmptyCast.Create (expr, target_type);
524 if (real_target_type == TypeManager.uint64_type)
525 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
526 if (real_target_type == TypeManager.int64_type)
527 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
528 if (real_target_type == TypeManager.float_type)
529 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
530 if (real_target_type == TypeManager.double_type)
531 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
532 if (real_target_type == TypeManager.decimal_type)
533 return new CastToDecimal (expr);
535 } else if (expr_type == TypeManager.short_type){
537 // From short to int, long, float, double, decimal
539 if (real_target_type == TypeManager.int32_type)
540 return EmptyCast.Create (expr, target_type);
541 if (real_target_type == TypeManager.int64_type)
542 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
543 if (real_target_type == TypeManager.double_type)
544 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
545 if (real_target_type == TypeManager.float_type)
546 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
547 if (real_target_type == TypeManager.decimal_type)
548 return new CastToDecimal (expr);
550 } else if (expr_type == TypeManager.ushort_type){
552 // From ushort to int, uint, long, ulong, float, double, decimal
554 if (real_target_type == TypeManager.int32_type || real_target_type == TypeManager.uint32_type)
555 return EmptyCast.Create (expr, target_type);
557 if (real_target_type == TypeManager.uint64_type)
558 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
559 if (real_target_type == TypeManager.int64_type)
560 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
561 if (real_target_type == TypeManager.double_type)
562 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
563 if (real_target_type == TypeManager.float_type)
564 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
565 if (real_target_type == TypeManager.decimal_type)
566 return new CastToDecimal (expr);
567 } else if (expr_type == TypeManager.int32_type){
569 // From int to long, float, double, decimal
571 if (real_target_type == TypeManager.int64_type)
572 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
573 if (real_target_type == TypeManager.double_type)
574 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
575 if (real_target_type == TypeManager.float_type)
576 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
577 if (real_target_type == TypeManager.decimal_type)
578 return new CastToDecimal (expr);
579 } else if (expr_type == TypeManager.uint32_type){
581 // From uint to long, ulong, float, double, decimal
583 if (real_target_type == TypeManager.int64_type)
584 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
585 if (real_target_type == TypeManager.uint64_type)
586 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
587 if (real_target_type == TypeManager.double_type)
588 return new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
589 if (real_target_type == TypeManager.float_type)
590 return new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
591 if (real_target_type == TypeManager.decimal_type)
592 return new CastToDecimal (expr);
593 } else if (expr_type == TypeManager.int64_type){
595 // From long/ulong to float, double
597 if (real_target_type == TypeManager.double_type)
598 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
599 if (real_target_type == TypeManager.float_type)
600 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
601 if (real_target_type == TypeManager.decimal_type)
602 return new CastToDecimal (expr);
603 } else if (expr_type == TypeManager.uint64_type){
605 // From ulong to float, double
607 if (real_target_type == TypeManager.double_type)
608 return new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
609 if (real_target_type == TypeManager.float_type)
610 return new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
611 if (real_target_type == TypeManager.decimal_type)
612 return new CastToDecimal (expr);
613 } else if (expr_type == TypeManager.char_type){
615 // From char to ushort, int, uint, long, ulong, float, double, decimal
617 if ((real_target_type == TypeManager.ushort_type) ||
618 (real_target_type == TypeManager.int32_type) ||
619 (real_target_type == TypeManager.uint32_type))
620 return EmptyCast.Create (expr, target_type);
621 if (real_target_type == TypeManager.uint64_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
623 if (real_target_type == TypeManager.int64_type)
624 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
625 if (real_target_type == TypeManager.float_type)
626 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
627 if (real_target_type == TypeManager.double_type)
628 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
629 if (real_target_type == TypeManager.decimal_type)
630 return new CastToDecimal (expr);
631 } else if (expr_type == TypeManager.float_type){
635 if (real_target_type == TypeManager.double_type)
636 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
643 /// Same as ImplicitStandardConversionExists except that it also looks at
644 /// implicit user defined conversions - needed for overload resolution
646 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
649 if (expr is NullLiteral) {
650 if (TypeManager.IsGenericParameter (target_type))
651 return TypeParameter_to_Null (target_type);
653 if (TypeManager.IsNullableType (target_type))
657 if (ImplicitStandardConversionExists (expr, target_type))
660 if (expr.Type == TypeManager.anonymous_method_type) {
661 if (!TypeManager.IsDelegateType (target_type) &&
662 TypeManager.DropGenericTypeArguments (target_type) != TypeManager.expression_type)
665 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
666 return ame.ImplicitStandardConversionExists (ec, target_type);
669 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
672 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
674 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
678 /// Determines if a standard implicit conversion exists from
679 /// expr_type to target_type
681 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
683 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
685 Type expr_type = expr.Type;
687 if (TypeManager.IsNullableType (target_type)) {
688 // if implicit standard conversion S -> T exists, S -> T? and S? -> T? also exists
689 target_type = TypeManager.GetTypeArguments (target_type) [0];
692 if (TypeManager.IsNullableType (expr_type)) {
693 EmptyExpression new_expr = EmptyExpression.Grab ();
694 new_expr.SetType (TypeManager.GetTypeArguments (expr_type) [0]);
695 bool retval = ImplicitStandardConversionExists (new_expr, target_type);
696 EmptyExpression.Release (new_expr);
701 if (expr_type == TypeManager.void_type)
704 //Console.WriteLine ("Expr is {0}", expr);
705 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
706 if (TypeManager.IsEqual (expr_type, target_type))
710 // First numeric conversions
712 if (expr_type == TypeManager.sbyte_type){
714 // From sbyte to short, int, long, float, double, decimal
716 if ((target_type == TypeManager.int32_type) ||
717 (target_type == TypeManager.int64_type) ||
718 (target_type == TypeManager.double_type) ||
719 (target_type == TypeManager.float_type) ||
720 (target_type == TypeManager.short_type) ||
721 (target_type == TypeManager.decimal_type))
724 } else if (expr_type == TypeManager.byte_type){
726 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
728 if ((target_type == TypeManager.short_type) ||
729 (target_type == TypeManager.ushort_type) ||
730 (target_type == TypeManager.int32_type) ||
731 (target_type == TypeManager.uint32_type) ||
732 (target_type == TypeManager.uint64_type) ||
733 (target_type == TypeManager.int64_type) ||
734 (target_type == TypeManager.float_type) ||
735 (target_type == TypeManager.double_type) ||
736 (target_type == TypeManager.decimal_type))
739 } else if (expr_type == TypeManager.short_type){
741 // From short to int, long, double, float, decimal
743 if ((target_type == TypeManager.int32_type) ||
744 (target_type == TypeManager.int64_type) ||
745 (target_type == TypeManager.double_type) ||
746 (target_type == TypeManager.float_type) ||
747 (target_type == TypeManager.decimal_type))
750 } else if (expr_type == TypeManager.ushort_type){
752 // From ushort to int, uint, long, ulong, double, float, decimal
754 if ((target_type == TypeManager.uint32_type) ||
755 (target_type == TypeManager.uint64_type) ||
756 (target_type == TypeManager.int32_type) ||
757 (target_type == TypeManager.int64_type) ||
758 (target_type == TypeManager.double_type) ||
759 (target_type == TypeManager.float_type) ||
760 (target_type == TypeManager.decimal_type))
763 } else if (expr_type == TypeManager.int32_type){
765 // From int to long, double, float, decimal
767 if ((target_type == TypeManager.int64_type) ||
768 (target_type == TypeManager.double_type) ||
769 (target_type == TypeManager.float_type) ||
770 (target_type == TypeManager.decimal_type))
773 } else if (expr_type == TypeManager.uint32_type){
775 // From uint to long, ulong, double, float, decimal
777 if ((target_type == TypeManager.int64_type) ||
778 (target_type == TypeManager.uint64_type) ||
779 (target_type == TypeManager.double_type) ||
780 (target_type == TypeManager.float_type) ||
781 (target_type == TypeManager.decimal_type))
784 } else if ((expr_type == TypeManager.uint64_type) ||
785 (expr_type == TypeManager.int64_type)) {
787 // From long/ulong to double, float, decimal
789 if ((target_type == TypeManager.double_type) ||
790 (target_type == TypeManager.float_type) ||
791 (target_type == TypeManager.decimal_type))
794 } else if (expr_type == TypeManager.char_type){
796 // From char to ushort, int, uint, ulong, long, float, double, decimal
798 if ((target_type == TypeManager.ushort_type) ||
799 (target_type == TypeManager.int32_type) ||
800 (target_type == TypeManager.uint32_type) ||
801 (target_type == TypeManager.uint64_type) ||
802 (target_type == TypeManager.int64_type) ||
803 (target_type == TypeManager.float_type) ||
804 (target_type == TypeManager.double_type) ||
805 (target_type == TypeManager.decimal_type))
808 } else if (expr_type == TypeManager.float_type){
812 if (target_type == TypeManager.double_type)
816 if (expr.eclass == ExprClass.MethodGroup){
817 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
818 MethodGroupExpr mg = expr as MethodGroupExpr;
820 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
827 if (ImplicitReferenceConversionExists (expr, target_type))
831 // Implicit Constant Expression Conversions
833 if (expr is IntConstant){
834 int value = ((IntConstant) expr).Value;
836 if (target_type == TypeManager.sbyte_type){
837 if (value >= SByte.MinValue && value <= SByte.MaxValue)
839 } else if (target_type == TypeManager.byte_type){
840 if (value >= 0 && value <= Byte.MaxValue)
842 } else if (target_type == TypeManager.short_type){
843 if (value >= Int16.MinValue && value <= Int16.MaxValue)
845 } else if (target_type == TypeManager.ushort_type){
846 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
848 } else if (target_type == TypeManager.uint32_type){
851 } else if (target_type == TypeManager.uint64_type){
853 // we can optimize this case: a positive int32
854 // always fits on a uint64. But we need an opcode
861 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
865 if (expr is LongConstant && target_type == TypeManager.uint64_type){
867 // Try the implicit constant expression conversion
868 // from long to ulong, instead of a nice routine,
871 long v = ((LongConstant) expr).Value;
877 // If `expr_type' implements `target_type' (which is an iface)
878 // see TryImplicitIntConversion
880 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
883 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
890 /// Finds "most encompassed type" according to the spec (13.4.2)
891 /// amongst the methods in the MethodGroupExpr
893 static Type FindMostEncompassedType (ArrayList types)
897 if (types.Count == 0)
900 if (types.Count == 1)
901 return (Type) types [0];
903 EmptyExpression expr = EmptyExpression.Grab ();
905 foreach (Type t in types) {
912 if (ImplicitStandardConversionExists (expr, best))
917 foreach (Type t in types) {
920 if (!ImplicitStandardConversionExists (expr, t)) {
926 EmptyExpression.Release (expr);
932 /// Finds "most encompassing type" according to the spec (13.4.2)
933 /// amongst the types in the given set
935 static Type FindMostEncompassingType (ArrayList types)
939 if (types.Count == 0)
942 if (types.Count == 1)
943 return (Type) types [0];
945 EmptyExpression expr = EmptyExpression.Grab ();
947 foreach (Type t in types) {
954 if (ImplicitStandardConversionExists (expr, t))
958 foreach (Type t in types) {
962 if (!ImplicitStandardConversionExists (expr, best)) {
968 EmptyExpression.Release (expr);
974 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
975 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
976 /// for explicit and implicit conversion operators.
978 static public Type FindMostSpecificSource (IList list,
979 Expression source, bool apply_explicit_conv_rules)
981 ArrayList src_types_set = new ArrayList ();
984 // If any operator converts from S then Sx = S
986 Type source_type = source.Type;
987 foreach (MethodBase mb in list){
988 ParameterData pd = TypeManager.GetParameterData (mb);
989 Type param_type = pd.ParameterType (0);
991 if (param_type == source_type)
994 src_types_set.Add (param_type);
998 // Explicit Conv rules
1000 if (apply_explicit_conv_rules) {
1001 ArrayList candidate_set = new ArrayList ();
1003 foreach (Type param_type in src_types_set){
1004 if (ImplicitStandardConversionExists (source, param_type))
1005 candidate_set.Add (param_type);
1008 if (candidate_set.Count != 0)
1009 return FindMostEncompassedType (candidate_set);
1015 if (apply_explicit_conv_rules)
1016 return FindMostEncompassingType (src_types_set);
1018 return FindMostEncompassedType (src_types_set);
1022 /// Finds the most specific target Tx according to section 13.4.4
1024 static public Type FindMostSpecificTarget (IList list,
1025 Type target, bool apply_explicit_conv_rules)
1027 ArrayList tgt_types_set = new ArrayList ();
1030 // If any operator converts to T then Tx = T
1032 foreach (MethodInfo mi in list){
1033 Type ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
1034 if (ret_type == target)
1037 tgt_types_set.Add (ret_type);
1041 // Explicit conv rules
1043 if (apply_explicit_conv_rules) {
1044 ArrayList candidate_set = new ArrayList ();
1046 EmptyExpression expr = EmptyExpression.Grab ();
1048 foreach (Type ret_type in tgt_types_set){
1049 expr.SetType (ret_type);
1051 if (ImplicitStandardConversionExists (expr, target))
1052 candidate_set.Add (ret_type);
1055 EmptyExpression.Release (expr);
1057 if (candidate_set.Count != 0)
1058 return FindMostEncompassingType (candidate_set);
1062 // Okay, final case !
1064 if (apply_explicit_conv_rules)
1065 return FindMostEncompassedType (tgt_types_set);
1067 return FindMostEncompassingType (tgt_types_set);
1071 /// User-defined Implicit conversions
1073 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1074 Type target, Location loc)
1076 Expression expr = UserDefinedConversion (ec, source, target, loc, false);
1077 if (expr != null && !TypeManager.IsEqual (expr.Type, target))
1078 expr = ImplicitConversionStandard (ec, expr, target, loc);
1084 /// User-defined Explicit conversions
1086 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1087 Type target, Location loc)
1089 Expression expr = UserDefinedConversion (ec, source, target, loc, true);
1090 if (expr != null && !TypeManager.IsEqual (expr.Type, target))
1091 expr = ExplicitConversionStandard (ec, expr, target, loc);
1096 static void AddConversionOperators (ArrayList list,
1097 Expression source, Type target_type,
1098 bool look_for_explicit,
1104 Type source_type = source.Type;
1105 EmptyExpression expr = EmptyExpression.Grab ();
1108 // LAMESPEC: Undocumented IntPtr/UIntPtr conversions
1109 // IntPtr -> uint uses int
1110 // UIntPtr -> long uses ulong
1112 if (source_type == TypeManager.intptr_type) {
1113 if (target_type == TypeManager.uint32_type)
1114 target_type = TypeManager.int32_type;
1115 } else if (source_type == TypeManager.uintptr_type) {
1116 if (target_type == TypeManager.int64_type)
1117 target_type = TypeManager.uint64_type;
1120 foreach (MethodInfo m in mg.Methods) {
1121 ParameterData pd = TypeManager.GetParameterData (m);
1122 Type return_type = TypeManager.TypeToCoreType (m.ReturnType);
1123 Type arg_type = pd.ParameterType (0);
1125 if (source_type != arg_type) {
1126 if (!ImplicitStandardConversionExists (source, arg_type)) {
1127 if (!look_for_explicit)
1129 expr.SetType (arg_type);
1130 if (!ImplicitStandardConversionExists (expr, source_type))
1135 if (target_type != return_type) {
1136 expr.SetType (return_type);
1137 if (!ImplicitStandardConversionExists (expr, target_type)) {
1138 if (!look_for_explicit)
1140 expr.SetType (target_type);
1141 if (!ImplicitStandardConversionExists (expr, return_type))
1146 // See LAMESPEC: Exclude IntPtr -> int conversion
1147 if (source_type == TypeManager.uintptr_type && return_type == TypeManager.uint32_type)
1153 EmptyExpression.Release (expr);
1157 /// Compute the user-defined conversion operator from source_type to target_type.
1158 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1160 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1162 ArrayList ops = new ArrayList (4);
1164 Type source_type = source.Type;
1166 if (source_type != TypeManager.decimal_type) {
1167 AddConversionOperators (ops, source, target_type, look_for_explicit,
1168 Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1169 if (look_for_explicit) {
1170 AddConversionOperators (ops, source, target_type, look_for_explicit,
1171 Expression.MethodLookup (
1172 container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1176 if (target_type != TypeManager.decimal_type) {
1177 AddConversionOperators (ops, source, target_type, look_for_explicit,
1178 Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1179 if (look_for_explicit) {
1180 AddConversionOperators (ops, source, target_type, look_for_explicit,
1181 Expression.MethodLookup (
1182 container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1189 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1190 if (most_specific_source == null)
1193 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1194 if (most_specific_target == null)
1197 MethodInfo method = null;
1199 foreach (MethodInfo m in ops) {
1200 if (TypeManager.TypeToCoreType (m.ReturnType) != most_specific_target)
1202 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1204 // Ambiguous: more than one conversion operator satisfies the signature.
1213 static DoubleHash explicit_conv = new DoubleHash (100);
1214 static DoubleHash implicit_conv = new DoubleHash (100);
1217 /// User-defined conversions
1219 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1220 Type target, Location loc,
1221 bool look_for_explicit)
1223 Type source_type = source.Type;
1224 MethodInfo method = null;
1228 if (look_for_explicit) {
1229 hash = explicit_conv;
1231 // Implicit user operators cannot convert to interfaces
1232 if (target.IsInterface)
1235 hash = implicit_conv;
1238 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1239 method = (MethodInfo) o;
1241 method = GetConversionOperator (null, source, target, look_for_explicit);
1242 if (!(source is Constant))
1243 hash.Insert (source_type, target, method);
1249 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1252 // This will do the conversion to the best match that we
1253 // found. Now we need to perform an implict standard conversion
1254 // if the best match was not the type that we were requested
1257 if (look_for_explicit)
1258 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1260 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1265 return new UserCast (method, source, loc);
1269 /// Converts implicitly the resolved expression `expr' into the
1270 /// `target_type'. It returns a new expression that can be used
1271 /// in a context that expects a `target_type'.
1273 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1274 Type target_type, Location loc)
1278 if (target_type == null)
1279 throw new Exception ("Target type is null");
1281 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1285 e = ImplicitUserConversion (ec, expr, target_type, loc);
1294 /// Attempts to apply the `Standard Implicit
1295 /// Conversion' rules to the expression `expr' into
1296 /// the `target_type'. It returns a new expression
1297 /// that can be used in a context that expects a
1300 /// This is different from `ImplicitConversion' in that the
1301 /// user defined implicit conversions are excluded.
1303 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1304 Type target_type, Location loc)
1306 return ImplicitConversionStandard (ec, expr, target_type, loc, false);
1309 static Expression ImplicitConversionStandard (EmitContext ec, Expression expr, Type target_type, Location loc, bool explicit_cast)
1311 Type expr_type = expr.Type;
1315 if (TypeManager.IsNullableType (target_type)) {
1317 // From null to any nullable type
1319 if (expr_type == TypeManager.null_type)
1320 return Nullable.LiftedNull.Create (target_type, loc);
1322 Type target = TypeManager.GetTypeArguments (target_type) [0];
1324 if (TypeManager.IsNullableType (expr_type)) {
1325 Type etype = TypeManager.GetTypeArguments (expr_type) [0];
1326 if (TypeManager.IsEqual (etype, target))
1329 return new Nullable.LiftedConversion (
1330 expr, target_type, false, false, loc).Resolve (ec);
1332 e = ImplicitConversion (ec, expr, target, loc);
1334 return Nullable.Wrap.Create (e, target_type);
1338 if (expr.eclass == ExprClass.MethodGroup){
1339 if (!TypeManager.IsDelegateType (target_type)){
1344 // Only allow anonymous method conversions on post ISO_1
1346 if (RootContext.Version != LanguageVersion.ISO_1){
1347 MethodGroupExpr mg = expr as MethodGroupExpr;
1349 return ImplicitDelegateCreation.Create (
1350 ec, mg, target_type, loc);
1354 if (expr_type.Equals (target_type)) {
1355 if (expr_type != TypeManager.null_type && expr_type != TypeManager.anonymous_method_type)
1361 // Attempt to do the implicit constant expression conversions
1363 Constant c = expr as Constant;
1366 // If `target_type' is an interface and the type of `ic' implements the interface
1367 // e.g. target_type is IComparable, IConvertible, IFormattable
1369 if (c.Type == TypeManager.int32_type && target_type.IsInterface && target_type.IsAssignableFrom (c.Type))
1370 return new BoxedCast (c, target_type);
1373 c = c.ConvertImplicitly (target_type);
1375 Console.WriteLine ("Conversion error happened in line {0}", loc);
1382 e = ImplicitNumericConversion (expr, target_type);
1386 e = ImplicitReferenceConversion (expr, target_type, explicit_cast);
1390 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1391 IntLiteral i = (IntLiteral) expr;
1394 return new EnumConstant ((Constant) expr, target_type);
1398 if (expr_type.IsPointer){
1399 if (target_type == TypeManager.void_ptr_type)
1400 return EmptyCast.Create (expr, target_type);
1403 // yep, comparing pointer types cant be done with
1404 // t1 == t2, we have to compare their element types.
1406 if (target_type.IsPointer){
1407 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1414 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1415 return EmptyCast.Create (NullPointer.Null, target_type);
1418 if (expr_type == TypeManager.anonymous_method_type){
1419 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1420 Expression am = ame.Compatible (ec, target_type);
1422 return am.DoResolve (ec);
1429 /// Attempts to implicitly convert `source' into `target_type', using
1430 /// ImplicitConversion. If there is no implicit conversion, then
1431 /// an error is signaled
1433 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1434 Type target_type, Location loc)
1436 Expression e = ImplicitConversion (ec, source, target_type, loc);
1440 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1445 /// Performs the explicit numeric conversions
1447 /// There are a few conversions that are not part of the C# standard,
1448 /// they were interim hacks in the C# compiler that were supposed to
1449 /// become explicit operators in the UIntPtr class and IntPtr class,
1450 /// but for historical reasons it did not happen, so the C# compiler
1451 /// ended up with these special hacks.
1453 /// See bug 59800 for details.
1455 /// The conversion are:
1465 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1467 Type expr_type = expr.Type;
1468 Type real_target_type = target_type;
1470 if (expr_type == TypeManager.sbyte_type){
1472 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1474 if (real_target_type == TypeManager.byte_type)
1475 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1476 if (real_target_type == TypeManager.ushort_type)
1477 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1478 if (real_target_type == TypeManager.uint32_type)
1479 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1480 if (real_target_type == TypeManager.uint64_type)
1481 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1482 if (real_target_type == TypeManager.char_type)
1483 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1485 // One of the built-in conversions that belonged in the class library
1486 if (real_target_type == TypeManager.uintptr_type){
1487 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1489 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1491 } else if (expr_type == TypeManager.byte_type){
1493 // From byte to sbyte and char
1495 if (real_target_type == TypeManager.sbyte_type)
1496 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1497 if (real_target_type == TypeManager.char_type)
1498 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1499 } else if (expr_type == TypeManager.short_type){
1501 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1503 if (real_target_type == TypeManager.sbyte_type)
1504 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1505 if (real_target_type == TypeManager.byte_type)
1506 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1507 if (real_target_type == TypeManager.ushort_type)
1508 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1509 if (real_target_type == TypeManager.uint32_type)
1510 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1511 if (real_target_type == TypeManager.uint64_type)
1512 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1513 if (real_target_type == TypeManager.char_type)
1514 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1516 // One of the built-in conversions that belonged in the class library
1517 if (real_target_type == TypeManager.uintptr_type){
1518 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1520 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1522 } else if (expr_type == TypeManager.ushort_type){
1524 // From ushort to sbyte, byte, short, char
1526 if (real_target_type == TypeManager.sbyte_type)
1527 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1528 if (real_target_type == TypeManager.byte_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1530 if (real_target_type == TypeManager.short_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1532 if (real_target_type == TypeManager.char_type)
1533 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1534 } else if (expr_type == TypeManager.int32_type){
1536 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1538 if (real_target_type == TypeManager.sbyte_type)
1539 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1540 if (real_target_type == TypeManager.byte_type)
1541 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1542 if (real_target_type == TypeManager.short_type)
1543 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1544 if (real_target_type == TypeManager.ushort_type)
1545 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1546 if (real_target_type == TypeManager.uint32_type)
1547 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1548 if (real_target_type == TypeManager.uint64_type)
1549 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1550 if (real_target_type == TypeManager.char_type)
1551 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1553 // One of the built-in conversions that belonged in the class library
1554 if (real_target_type == TypeManager.uintptr_type){
1555 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1557 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1559 } else if (expr_type == TypeManager.uint32_type){
1561 // From uint to sbyte, byte, short, ushort, int, char
1563 if (real_target_type == TypeManager.sbyte_type)
1564 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1565 if (real_target_type == TypeManager.byte_type)
1566 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1567 if (real_target_type == TypeManager.short_type)
1568 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1569 if (real_target_type == TypeManager.ushort_type)
1570 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1571 if (real_target_type == TypeManager.int32_type)
1572 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1573 if (real_target_type == TypeManager.char_type)
1574 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1575 } else if (expr_type == TypeManager.int64_type){
1577 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1579 if (real_target_type == TypeManager.sbyte_type)
1580 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1581 if (real_target_type == TypeManager.byte_type)
1582 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1583 if (real_target_type == TypeManager.short_type)
1584 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1585 if (real_target_type == TypeManager.ushort_type)
1586 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1587 if (real_target_type == TypeManager.int32_type)
1588 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1589 if (real_target_type == TypeManager.uint32_type)
1590 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1591 if (real_target_type == TypeManager.uint64_type)
1592 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1593 if (real_target_type == TypeManager.char_type)
1594 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1595 } else if (expr_type == TypeManager.uint64_type){
1597 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1599 if (real_target_type == TypeManager.sbyte_type)
1600 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1601 if (real_target_type == TypeManager.byte_type)
1602 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1603 if (real_target_type == TypeManager.short_type)
1604 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1605 if (real_target_type == TypeManager.ushort_type)
1606 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1607 if (real_target_type == TypeManager.int32_type)
1608 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1609 if (real_target_type == TypeManager.uint32_type)
1610 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1611 if (real_target_type == TypeManager.int64_type)
1612 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1613 if (real_target_type == TypeManager.char_type)
1614 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1616 // One of the built-in conversions that belonged in the class library
1617 if (real_target_type == TypeManager.intptr_type){
1618 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1619 TypeManager.intptr_type, true);
1621 } else if (expr_type == TypeManager.char_type){
1623 // From char to sbyte, byte, short
1625 if (real_target_type == TypeManager.sbyte_type)
1626 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1627 if (real_target_type == TypeManager.byte_type)
1628 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1629 if (real_target_type == TypeManager.short_type)
1630 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1631 } else if (expr_type == TypeManager.float_type){
1633 // From float to sbyte, byte, short,
1634 // ushort, int, uint, long, ulong, char
1637 if (real_target_type == TypeManager.sbyte_type)
1638 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1639 if (real_target_type == TypeManager.byte_type)
1640 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1641 if (real_target_type == TypeManager.short_type)
1642 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1643 if (real_target_type == TypeManager.ushort_type)
1644 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1645 if (real_target_type == TypeManager.int32_type)
1646 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1647 if (real_target_type == TypeManager.uint32_type)
1648 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1649 if (real_target_type == TypeManager.int64_type)
1650 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1651 if (real_target_type == TypeManager.uint64_type)
1652 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1653 if (real_target_type == TypeManager.char_type)
1654 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1655 if (real_target_type == TypeManager.decimal_type)
1656 return new CastToDecimal (expr, true);
1657 } else if (expr_type == TypeManager.double_type){
1659 // From double to sbyte, byte, short,
1660 // ushort, int, uint, long, ulong,
1661 // char, float or decimal
1663 if (real_target_type == TypeManager.sbyte_type)
1664 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1665 if (real_target_type == TypeManager.byte_type)
1666 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1667 if (real_target_type == TypeManager.short_type)
1668 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1669 if (real_target_type == TypeManager.ushort_type)
1670 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1671 if (real_target_type == TypeManager.int32_type)
1672 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1673 if (real_target_type == TypeManager.uint32_type)
1674 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1675 if (real_target_type == TypeManager.int64_type)
1676 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1677 if (real_target_type == TypeManager.uint64_type)
1678 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1679 if (real_target_type == TypeManager.char_type)
1680 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1681 if (real_target_type == TypeManager.float_type)
1682 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1683 if (real_target_type == TypeManager.decimal_type)
1684 return new CastToDecimal (expr, true);
1685 } else if (expr_type == TypeManager.uintptr_type){
1687 // Various built-in conversions that belonged in the class library
1689 // from uintptr to sbyte, short, int32
1691 if (real_target_type == TypeManager.sbyte_type){
1692 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1693 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1695 if (real_target_type == TypeManager.short_type){
1696 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1697 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1699 if (real_target_type == TypeManager.int32_type){
1700 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1701 TypeManager.int32_type);
1703 } else if (expr_type == TypeManager.intptr_type){
1704 if (real_target_type == TypeManager.uint64_type){
1705 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1706 TypeManager.uint64_type);
1708 } else if (expr_type == TypeManager.decimal_type) {
1709 return new CastFromDecimal (expr, target_type).Resolve ();
1715 /// Returns whether an explicit reference conversion can be performed
1716 /// from source_type to target_type
1718 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1720 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1721 bool target_is_value_type = target_type.IsValueType;
1723 if (source_type == target_type)
1727 // From generic parameter to any type
1729 if (TypeManager.IsGenericParameter (source_type))
1730 return ExplicitTypeParameterConversionExists (source_type, target_type);
1733 // From object to a generic parameter
1735 if (source_type == TypeManager.object_type && target_is_type_param)
1739 // From object to any reference type
1741 if (source_type == TypeManager.object_type && !target_is_value_type)
1745 // From any class S to any class-type T, provided S is a base class of T
1747 if (TypeManager.IsSubclassOf (target_type, source_type))
1751 // From any interface type S to any interface T provided S is not derived from T
1753 if (source_type.IsInterface && target_type.IsInterface){
1754 if (!TypeManager.IsSubclassOf (target_type, source_type))
1759 // From any class type S to any interface T, provided S is not sealed
1760 // and provided S does not implement T.
1762 if (target_type.IsInterface && !source_type.IsSealed &&
1763 !TypeManager.ImplementsInterface (source_type, target_type))
1767 // From any interface-type S to to any class type T, provided T is not
1768 // sealed, or provided T implements S.
1770 if (source_type.IsInterface &&
1771 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1775 if (source_type.IsInterface && IList_To_Array (source_type, target_type))
1778 // From an array type S with an element type Se to an array type T with an
1779 // element type Te provided all the following are true:
1780 // * S and T differe only in element type, in other words, S and T
1781 // have the same number of dimensions.
1782 // * Both Se and Te are reference types
1783 // * An explicit referenc conversions exist from Se to Te
1785 if (source_type.IsArray && target_type.IsArray) {
1786 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1788 Type source_element_type = TypeManager.GetElementType (source_type);
1789 Type target_element_type = TypeManager.GetElementType (target_type);
1791 if (TypeManager.IsGenericParameter (source_element_type) ||
1792 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1793 if (ExplicitReferenceConversionExists (source_element_type,
1794 target_element_type))
1800 // From System.Array to any array-type
1801 if (source_type == TypeManager.array_type &&
1802 target_type.IsArray){
1807 // From System delegate to any delegate-type
1809 if (source_type == TypeManager.delegate_type &&
1810 TypeManager.IsDelegateType (target_type))
1817 /// Implements Explicit Reference conversions
1819 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1821 Type source_type = source.Type;
1822 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1823 bool target_is_value_type = target_type.IsValueType;
1826 // From object to a generic parameter
1828 if (source_type == TypeManager.object_type && target_is_type_param)
1829 return new UnboxCast (source, target_type);
1832 // Explicit type parameter conversion.
1835 if (TypeManager.IsGenericParameter (source_type))
1836 return ExplicitTypeParameterConversion (source, target_type);
1839 // From object to any reference type
1841 if (source_type == TypeManager.object_type && !target_is_value_type)
1842 return new ClassCast (source, target_type);
1845 // Unboxing conversion.
1847 if (((source_type == TypeManager.enum_type &&
1848 !(source is TypeCast)) ||
1849 source_type == TypeManager.value_type) && target_is_value_type)
1850 return new UnboxCast (source, target_type);
1853 // From any class S to any class-type T, provided S is a base class of T
1855 if (TypeManager.IsSubclassOf (target_type, source_type))
1856 return new ClassCast (source, target_type);
1859 // From any interface type S to any interface T provided S is not derived from T
1861 if (source_type.IsInterface && target_type.IsInterface){
1862 if (TypeManager.ImplementsInterface (source_type, target_type))
1865 return new ClassCast (source, target_type);
1869 // From any class type S to any interface T, provides S is not sealed
1870 // and provided S does not implement T.
1872 if (target_type.IsInterface &&
1873 (!source_type.IsSealed || source_type.IsArray) && // SRE: IsSealed does not work with aggregates
1874 !TypeManager.ImplementsInterface (source_type, target_type)) {
1875 return new ClassCast (source, target_type);
1879 // From any interface-type S to to any class type T, provided T is not
1880 // sealed, or provided T implements S.
1882 if (source_type.IsInterface) {
1883 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1884 if (target_type.IsClass)
1885 return new ClassCast (source, target_type);
1887 return new UnboxCast (source, target_type);
1891 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1892 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1894 if (IList_To_Array (source_type, target_type))
1895 return new ClassCast (source, target_type);
1900 // From an array type S with an element type Se to an array type T with an
1901 // element type Te provided all the following are true:
1902 // * S and T differe only in element type, in other words, S and T
1903 // have the same number of dimensions.
1904 // * Both Se and Te are reference types
1905 // * An explicit referenc conversions exist from Se to Te
1907 if (source_type.IsArray && target_type.IsArray) {
1908 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1910 Type source_element_type = TypeManager.GetElementType (source_type);
1911 Type target_element_type = TypeManager.GetElementType (target_type);
1913 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1914 if (ExplicitReferenceConversionExists (source_element_type,
1915 target_element_type))
1916 return new ClassCast (source, target_type);
1921 // From System.Array to any array-type
1922 if (source_type == TypeManager.array_type &&
1923 target_type.IsArray) {
1924 return new ClassCast (source, target_type);
1928 // From System delegate to any delegate-type
1930 if (source_type == TypeManager.delegate_type &&
1931 TypeManager.IsDelegateType (target_type))
1932 return new ClassCast (source, target_type);
1938 /// Performs an explicit conversion of the expression `expr' whose
1939 /// type is expr.Type to `target_type'.
1941 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1942 Type target_type, Location loc)
1944 Type expr_type = expr.Type;
1946 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1947 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc, true);
1952 // Unboxing conversions; only object types can be convertible to enum
1954 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1955 return new UnboxCast (expr, target_type);
1957 if (TypeManager.IsEnumType (expr_type)) {
1958 Expression underlying = EmptyCast.Create (expr, TypeManager.GetEnumUnderlyingType (expr_type));
1959 expr = ExplicitConversionCore (ec, underlying, target_type, loc);
1963 return ExplicitUserConversion (ec, underlying, target_type, loc);
1966 if (TypeManager.IsEnumType (target_type)){
1967 if (expr_type == TypeManager.enum_type)
1968 return new UnboxCast (expr, target_type);
1970 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.GetEnumUnderlyingType (target_type), loc);
1972 return EmptyCast.Create (ce, target_type);
1975 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1977 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1978 ne = ExplicitUserConversion (ec, expr, TypeManager.GetEnumUnderlyingType (target_type), loc);
1980 return ExplicitConversionCore (ec, ne, target_type, loc);
1986 ne = ExplicitNumericConversion (expr, target_type);
1991 // Skip the ExplicitReferenceConversion because we can not convert
1992 // from Null to a ValueType, and ExplicitReference wont check against
1993 // null literal explicitly
1995 if (expr_type != TypeManager.null_type){
1996 ne = ExplicitReferenceConversion (expr, target_type);
2002 ne = ExplicitUnsafe (expr, target_type);
2010 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
2012 Type expr_type = expr.Type;
2014 if (target_type.IsPointer){
2015 if (expr_type.IsPointer)
2016 return EmptyCast.Create (expr, target_type);
2018 if (expr_type == TypeManager.sbyte_type ||
2019 expr_type == TypeManager.short_type ||
2020 expr_type == TypeManager.int32_type)
2021 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
2023 if (expr_type == TypeManager.ushort_type ||
2024 expr_type == TypeManager.uint32_type ||
2025 expr_type == TypeManager.uint64_type || expr_type == TypeManager.int64_type ||
2026 expr_type == TypeManager.byte_type)
2027 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2030 if (expr_type.IsPointer){
2031 if (target_type == TypeManager.sbyte_type)
2032 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2033 else if (target_type == TypeManager.byte_type)
2034 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2035 else if (target_type == TypeManager.short_type)
2036 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2037 else if (target_type == TypeManager.ushort_type)
2038 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2039 else if (target_type == TypeManager.int32_type)
2040 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2041 else if (target_type == TypeManager.uint32_type)
2042 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2043 else if (target_type == TypeManager.uint64_type || target_type == TypeManager.int64_type)
2044 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2050 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2052 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2053 Type target_type, Location l)
2055 int errors = Report.Errors;
2056 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2057 if (Report.Errors > errors)
2063 ne = ExplicitNumericConversion (expr, target_type);
2067 ne = ExplicitReferenceConversion (expr, target_type);
2071 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2072 return EmptyCast.Create (expr, target_type);
2074 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
2079 /// Performs an explicit conversion of the expression `expr' whose
2080 /// type is expr.Type to `target_type'.
2082 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2083 Type target_type, Location loc)
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);
2094 } else if (expr_type == TypeManager.object_type) {
2095 return new UnboxCast (expr, target_type);
2097 Type target = TypeManager.GetTypeArguments (target_type) [0];
2099 e = ExplicitConversionCore (ec, expr, target, loc);
2101 return Nullable.Wrap.Create (e, target_type);
2103 } else if (TypeManager.IsNullableType (expr_type)) {
2104 e = Nullable.Unwrap.Create (expr, ec);
2106 bool use_class_cast;
2107 if (ImplicitBoxingConversionExists (e, target_type, out use_class_cast))
2108 return new BoxedCast (expr, target_type);
2110 e = ExplicitConversion (ec, e, target_type, loc);
2112 e = EmptyCast.Create (e, target_type);
2116 e = ExplicitConversionCore (ec, expr, target_type, loc);
2120 e = ExplicitUserConversion (ec, expr, target_type, loc);
2124 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);