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 // (C) 2001, 2002, 2003 Ximian, Inc.
12 namespace Mono.CSharp {
14 using System.Collections;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Reflection.Emit;
20 // A container class for all the conversion operations
22 public class Convert {
24 static bool TypeParameter_to_Null (Type target_type)
26 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
30 if (gc.HasReferenceTypeConstraint)
32 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
38 static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
40 ArrayList list = new ArrayList ();
41 list.Add (gc.EffectiveBaseClass);
42 foreach (Type t in gc.InterfaceConstraints) {
43 if (!TypeManager.IsGenericParameter (t))
46 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
48 list.Add (TypeParam_EffectiveBaseType (new_gc));
50 return FindMostEncompassedType (list);
55 // From a one-dimensional array-type S[] to System.Collections.IList<S> and base
56 // interfaces of this interface.
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
62 static bool Array_To_IList (Type array, Type list)
65 if (!array.IsArray || (array.GetArrayRank () != 1) || !list.IsGenericType)
68 Type gt = list.GetGenericTypeDefinition ();
69 if ((gt != TypeManager.generic_ilist_type) &&
70 (gt != TypeManager.generic_icollection_type) &&
71 (gt != TypeManager.generic_ienumerable_type))
74 Type element_type = TypeManager.GetElementType (array);
75 Type arg_type = TypeManager.GetTypeArguments (list) [0];
77 if (element_type == arg_type)
80 if (MyEmptyExpr == null)
81 MyEmptyExpr = new EmptyExpression ();
82 MyEmptyExpr.SetType (TypeManager.GetElementType (array));
84 return ImplicitReferenceConversionCore (MyEmptyExpr, arg_type);
90 static bool IList_To_Array(Type list, Type array)
93 if (!list.IsGenericType || !array.IsArray || array.GetArrayRank() != 1)
96 Type gt = list.GetGenericTypeDefinition();
97 if (gt != TypeManager.generic_ilist_type &&
98 gt != TypeManager.generic_icollection_type &&
99 gt != TypeManager.generic_ienumerable_type)
102 Type arg_type = TypeManager.GetTypeArguments(list)[0];
103 Type element_type = TypeManager.GetElementType(array);
105 if (element_type == arg_type)
108 if (MyEmptyExpr == null)
109 MyEmptyExpr = new EmptyExpression();
110 MyEmptyExpr.SetType(element_type);
111 return ImplicitReferenceConversionExists(MyEmptyExpr, arg_type) || ExplicitReferenceConversionExists(element_type, arg_type);
117 static Expression ImplicitTypeParameterConversion (Expression expr,
121 Type expr_type = expr.Type;
123 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
126 if (target_type == TypeManager.object_type)
127 return new BoxedCast (expr, target_type);
132 // We're converting from a type parameter which is known to be a reference type.
133 Type base_type = TypeParam_EffectiveBaseType (gc);
135 if (TypeManager.IsSubclassOf (base_type, target_type))
136 return new ClassCast (expr, target_type);
138 if (target_type.IsInterface) {
139 if (TypeManager.ImplementsInterface (base_type, target_type))
140 return new ClassCast (expr, target_type);
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);
150 foreach (Type t in gc.InterfaceConstraints) {
151 if (!TypeManager.IsGenericParameter (t))
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);
162 static bool ImplicitTypeParameterBoxingConversion (Type expr_type, Type target_type,
163 out bool use_class_cast)
166 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
169 use_class_cast = false;
170 return target_type == TypeManager.object_type;
173 use_class_cast = true;
175 if (!gc.HasReferenceTypeConstraint)
178 // We're converting from a type parameter which is known to be a reference type.
179 Type base_type = TypeParam_EffectiveBaseType (gc);
181 if (TypeManager.IsSubclassOf (base_type, target_type))
184 if (target_type.IsInterface) {
185 if (TypeManager.ImplementsInterface (base_type, target_type))
188 foreach (Type t in gc.InterfaceConstraints) {
189 if (TypeManager.IsSubclassOf (t, target_type))
191 if (TypeManager.ImplementsInterface (t, target_type))
196 foreach (Type t in gc.InterfaceConstraints) {
197 if (!TypeManager.IsGenericParameter (t))
199 if (TypeManager.IsSubclassOf (t, target_type))
201 if (TypeManager.ImplementsInterface (t, target_type))
206 use_class_cast = false;
210 static bool ExplicitTypeParameterConversionExists (Type source_type, Type target_type)
213 if (target_type.IsInterface)
216 if (TypeManager.IsGenericParameter (target_type)) {
217 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
221 foreach (Type iface in gc.InterfaceConstraints) {
222 if (!TypeManager.IsGenericParameter (iface))
225 if (TypeManager.IsSubclassOf (source_type, iface))
233 static Expression ExplicitTypeParameterConversion (Expression source, Type target_type)
236 Type source_type = source.Type;
238 if (target_type.IsInterface)
239 return new ClassCast (source, target_type);
241 if (TypeManager.IsGenericParameter (target_type)) {
242 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
246 foreach (Type iface in gc.InterfaceConstraints) {
247 if (!TypeManager.IsGenericParameter (iface))
250 if (TypeManager.IsSubclassOf (source_type, iface))
251 return new ClassCast (source, target_type);
258 static EmptyExpression MyEmptyExpr;
259 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
261 Type expr_type = expr.Type;
263 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
264 // if we are a method group, emit a warning
269 if (expr_type == TypeManager.void_type)
272 if (TypeManager.IsGenericParameter (expr_type))
273 return ImplicitTypeParameterConversion (expr, target_type);
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);
281 if (ImplicitReferenceConversionCore (expr, target_type))
282 return EmptyCast.Create (expr, target_type);
285 if (ImplicitBoxingConversionExists (expr, target_type, out use_class_cast)) {
287 return new ClassCast (expr, target_type);
289 return new BoxedCast (expr, target_type);
295 static public bool ImplicitReferenceConversionCore (Expression expr, Type target_type)
297 Type expr_type = expr.Type;
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.
303 if (target_type == TypeManager.object_type) {
305 // A pointer type cannot be converted to object
307 if (expr_type.IsPointer)
310 if (TypeManager.IsValueType (expr_type))
312 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
313 return expr_type != TypeManager.anonymous_method_type;
317 } else if (target_type == TypeManager.value_type) {
318 return expr_type == TypeManager.enum_type;
319 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
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
325 if (expr_type.IsEnum || TypeManager.IsGenericParameter (expr_type))
331 // This code is kind of mirrored inside ImplicitStandardConversionExists
332 // with the small distinction that we only probe there
334 // Always ensure that the code here and there is in sync
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)) {
348 if (TypeManager.ImplementsInterface (expr_type, target_type)){
349 return !TypeManager.IsGenericParameter (expr_type) &&
350 !TypeManager.IsValueType (expr_type);
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);
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 ()) {
363 Type expr_element_type = TypeManager.GetElementType (expr_type);
365 if (MyEmptyExpr == null)
366 MyEmptyExpr = new EmptyExpression ();
368 MyEmptyExpr.SetType (expr_element_type);
369 Type target_element_type = TypeManager.GetElementType (target_type);
371 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
372 return ImplicitStandardConversionExists (
373 MyEmptyExpr, target_element_type);
377 // from an array-type to System.Array
378 if (expr_type.IsArray && target_type == TypeManager.array_type)
381 // from an array-type of type T to IList<T>
382 if (expr_type.IsArray && Array_To_IList (expr_type, target_type))
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)
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)
396 // from a generic type definition to a generic instance.
397 if (TypeManager.IsEqual (expr_type, target_type))
403 static public bool ImplicitBoxingConversionExists (Expression expr, Type target_type,
404 out bool use_class_cast)
406 Type expr_type = expr.Type;
407 use_class_cast = false;
410 // From any value-type to the type object.
412 if (target_type == TypeManager.object_type) {
414 // A pointer type cannot be converted to object
416 if (expr_type.IsPointer)
419 return TypeManager.IsValueType (expr_type);
423 // From any value-type to the type System.ValueType.
425 if (target_type == TypeManager.value_type)
426 return TypeManager.IsValueType (expr_type);
428 if (target_type == TypeManager.enum_type) {
430 // From any enum-type to the type System.Enum.
432 if (expr_type.IsEnum)
435 // From any nullable-type with an underlying enum-type to the type System.Enum
437 if (TypeManager.IsNullableType (expr_type))
438 return TypeManager.GetTypeArguments (expr_type) [0].IsEnum;
441 if (TypeManager.IsSubclassOf (expr_type, target_type)) {
442 if (TypeManager.IsGenericParameter (expr_type))
448 // This code is kind of mirrored inside ImplicitStandardConversionExists
449 // with the small distinction that we only probe there
451 // Always ensure that the code here and there is in sync
453 // from any class-type S to any interface-type T.
454 if (target_type.IsInterface) {
455 if (target_type != TypeManager.iconvertible_type &&
456 expr_type.IsValueType && (expr is Constant) &&
457 !(expr is IntLiteral || expr is BoolLiteral ||
458 expr is FloatLiteral || expr is DoubleLiteral ||
459 expr is LongLiteral || expr is CharLiteral ||
460 expr is StringLiteral || expr is DecimalLiteral ||
461 expr is UIntLiteral || expr is ULongLiteral)) {
465 if (TypeManager.ImplementsInterface (expr_type, target_type))
466 return TypeManager.IsGenericParameter (expr_type) ||
467 TypeManager.IsValueType (expr_type);
470 if (TypeManager.IsGenericParameter (expr_type))
471 return ImplicitTypeParameterBoxingConversion (
472 expr_type, target_type, out use_class_cast);
478 // Tests whether an implicit reference conversion exists between expr_type
481 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
483 if (target_type.IsValueType)
486 Type expr_type = expr.Type;
488 // from the null type to any reference-type.
489 if (expr_type == TypeManager.null_type){
490 if (target_type.IsPointer)
493 if (!target_type.IsValueType)
497 if (TypeManager.IsGenericParameter (expr_type))
498 return ImplicitTypeParameterConversion (expr, target_type) != null;
501 if (ImplicitReferenceConversionCore (expr, target_type) ||
502 ImplicitBoxingConversionExists (expr, target_type, out use_class_cast))
509 /// Implicit Numeric Conversions.
511 /// expr is the expression to convert, returns a new expression of type
512 /// target_type or null if an implicit conversion is not possible.
514 static public Expression ImplicitNumericConversion (Expression expr,
517 Type expr_type = expr.Type;
518 Type real_target_type = target_type;
520 if (expr_type == TypeManager.sbyte_type){
522 // From sbyte to short, int, long, float, double, decimal
524 if (real_target_type == TypeManager.int32_type)
525 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
526 if (real_target_type == TypeManager.int64_type)
527 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
528 if (real_target_type == TypeManager.double_type)
529 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
530 if (real_target_type == TypeManager.float_type)
531 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
532 if (real_target_type == TypeManager.short_type)
533 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
534 if (real_target_type == TypeManager.decimal_type)
535 return new CastToDecimal (expr);
536 } else if (expr_type == TypeManager.byte_type){
538 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
540 if ((real_target_type == TypeManager.short_type) ||
541 (real_target_type == TypeManager.ushort_type) ||
542 (real_target_type == TypeManager.int32_type) ||
543 (real_target_type == TypeManager.uint32_type))
544 return EmptyCast.Create (expr, target_type);
546 if (real_target_type == TypeManager.uint64_type)
547 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
548 if (real_target_type == TypeManager.int64_type)
549 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
550 if (real_target_type == TypeManager.float_type)
551 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
552 if (real_target_type == TypeManager.double_type)
553 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
554 if (real_target_type == TypeManager.decimal_type)
555 return new CastToDecimal (expr);
557 } else if (expr_type == TypeManager.short_type){
559 // From short to int, long, float, double, decimal
561 if (real_target_type == TypeManager.int32_type)
562 return EmptyCast.Create (expr, target_type);
563 if (real_target_type == TypeManager.int64_type)
564 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
565 if (real_target_type == TypeManager.double_type)
566 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
567 if (real_target_type == TypeManager.float_type)
568 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
569 if (real_target_type == TypeManager.decimal_type)
570 return new CastToDecimal (expr);
572 } else if (expr_type == TypeManager.ushort_type){
574 // From ushort to int, uint, long, ulong, float, double, decimal
576 if (real_target_type == TypeManager.uint32_type)
577 return EmptyCast.Create (expr, target_type);
579 if (real_target_type == TypeManager.uint64_type)
580 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
581 if (real_target_type == TypeManager.int32_type)
582 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
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.int32_type){
593 // From int to long, float, double, decimal
595 if (real_target_type == TypeManager.int64_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
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.uint32_type){
605 // From uint to long, ulong, float, double, decimal
607 if (real_target_type == TypeManager.int64_type)
608 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
609 if (real_target_type == TypeManager.uint64_type)
610 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
611 if (real_target_type == TypeManager.double_type)
612 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
614 if (real_target_type == TypeManager.float_type)
615 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
617 if (real_target_type == TypeManager.decimal_type)
618 return new CastToDecimal (expr);
619 } else if (expr_type == TypeManager.int64_type){
621 // From long/ulong to float, double
623 if (real_target_type == TypeManager.double_type)
624 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
625 if (real_target_type == TypeManager.float_type)
626 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
627 if (real_target_type == TypeManager.decimal_type)
628 return new CastToDecimal (expr);
629 } else if (expr_type == TypeManager.uint64_type){
631 // From ulong to float, double
633 if (real_target_type == TypeManager.double_type)
634 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
636 if (real_target_type == TypeManager.float_type)
637 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
639 if (real_target_type == TypeManager.decimal_type)
640 return new CastToDecimal (expr);
641 } else if (expr_type == TypeManager.char_type){
643 // From char to ushort, int, uint, long, ulong, float, double, decimal
645 if ((real_target_type == TypeManager.ushort_type) ||
646 (real_target_type == TypeManager.int32_type) ||
647 (real_target_type == TypeManager.uint32_type))
648 return EmptyCast.Create (expr, target_type);
649 if (real_target_type == TypeManager.uint64_type)
650 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
651 if (real_target_type == TypeManager.int64_type)
652 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
653 if (real_target_type == TypeManager.float_type)
654 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
655 if (real_target_type == TypeManager.double_type)
656 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
657 if (real_target_type == TypeManager.decimal_type)
658 return new CastToDecimal (expr);
659 } else if (expr_type == TypeManager.float_type){
663 if (real_target_type == TypeManager.double_type)
664 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
671 /// Same as ImplicitStandardConversionExists except that it also looks at
672 /// implicit user defined conversions - needed for overload resolution
674 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
677 if (expr is NullLiteral) {
678 if (TypeManager.IsGenericParameter (target_type))
679 return TypeParameter_to_Null (target_type);
681 if (TypeManager.IsNullableType (target_type))
685 if (ImplicitStandardConversionExists (expr, target_type))
688 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
691 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
693 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
697 /// Determines if a standard implicit conversion exists from
698 /// expr_type to target_type
700 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
702 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
704 Type expr_type = expr.Type;
706 if (TypeManager.IsNullableType (target_type)) {
707 // if implicit standard conversion S -> T exists, S -> T? and S? -> T? also exists
708 target_type = TypeManager.GetTypeArguments (target_type) [0];
711 if (TypeManager.IsNullableType (expr_type)) {
712 EmptyExpression new_expr = EmptyExpression.Grab ();
713 new_expr.SetType (TypeManager.GetTypeArguments (expr_type) [0]);
714 bool retval = ImplicitStandardConversionExists (new_expr, target_type);
715 EmptyExpression.Release (new_expr);
720 if (expr_type == TypeManager.void_type)
723 //Console.WriteLine ("Expr is {0}", expr);
724 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
725 if (TypeManager.IsEqual (expr_type, target_type))
729 // First numeric conversions
731 if (expr_type == TypeManager.sbyte_type){
733 // From sbyte to short, int, long, float, double, decimal
735 if ((target_type == TypeManager.int32_type) ||
736 (target_type == TypeManager.int64_type) ||
737 (target_type == TypeManager.double_type) ||
738 (target_type == TypeManager.float_type) ||
739 (target_type == TypeManager.short_type) ||
740 (target_type == TypeManager.decimal_type))
743 } else if (expr_type == TypeManager.byte_type){
745 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
747 if ((target_type == TypeManager.short_type) ||
748 (target_type == TypeManager.ushort_type) ||
749 (target_type == TypeManager.int32_type) ||
750 (target_type == TypeManager.uint32_type) ||
751 (target_type == TypeManager.uint64_type) ||
752 (target_type == TypeManager.int64_type) ||
753 (target_type == TypeManager.float_type) ||
754 (target_type == TypeManager.double_type) ||
755 (target_type == TypeManager.decimal_type))
758 } else if (expr_type == TypeManager.short_type){
760 // From short to int, long, double, float, decimal
762 if ((target_type == TypeManager.int32_type) ||
763 (target_type == TypeManager.int64_type) ||
764 (target_type == TypeManager.double_type) ||
765 (target_type == TypeManager.float_type) ||
766 (target_type == TypeManager.decimal_type))
769 } else if (expr_type == TypeManager.ushort_type){
771 // From ushort to int, uint, long, ulong, double, float, decimal
773 if ((target_type == TypeManager.uint32_type) ||
774 (target_type == TypeManager.uint64_type) ||
775 (target_type == TypeManager.int32_type) ||
776 (target_type == TypeManager.int64_type) ||
777 (target_type == TypeManager.double_type) ||
778 (target_type == TypeManager.float_type) ||
779 (target_type == TypeManager.decimal_type))
782 } else if (expr_type == TypeManager.int32_type){
784 // From int to long, double, float, decimal
786 if ((target_type == TypeManager.int64_type) ||
787 (target_type == TypeManager.double_type) ||
788 (target_type == TypeManager.float_type) ||
789 (target_type == TypeManager.decimal_type))
792 } else if (expr_type == TypeManager.uint32_type){
794 // From uint to long, ulong, double, float, decimal
796 if ((target_type == TypeManager.int64_type) ||
797 (target_type == TypeManager.uint64_type) ||
798 (target_type == TypeManager.double_type) ||
799 (target_type == TypeManager.float_type) ||
800 (target_type == TypeManager.decimal_type))
803 } else if ((expr_type == TypeManager.uint64_type) ||
804 (expr_type == TypeManager.int64_type)) {
806 // From long/ulong to double, float, decimal
808 if ((target_type == TypeManager.double_type) ||
809 (target_type == TypeManager.float_type) ||
810 (target_type == TypeManager.decimal_type))
813 } else if (expr_type == TypeManager.char_type){
815 // From char to ushort, int, uint, ulong, long, float, double, decimal
817 if ((target_type == TypeManager.ushort_type) ||
818 (target_type == TypeManager.int32_type) ||
819 (target_type == TypeManager.uint32_type) ||
820 (target_type == TypeManager.uint64_type) ||
821 (target_type == TypeManager.int64_type) ||
822 (target_type == TypeManager.float_type) ||
823 (target_type == TypeManager.double_type) ||
824 (target_type == TypeManager.decimal_type))
827 } else if (expr_type == TypeManager.float_type){
831 if (target_type == TypeManager.double_type)
835 if (expr.eclass == ExprClass.MethodGroup){
836 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
837 MethodGroupExpr mg = expr as MethodGroupExpr;
839 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
844 if (ImplicitReferenceConversionExists (expr, target_type))
848 // Implicit Constant Expression Conversions
850 if (expr is IntConstant){
851 int value = ((IntConstant) expr).Value;
853 if (target_type == TypeManager.sbyte_type){
854 if (value >= SByte.MinValue && value <= SByte.MaxValue)
856 } else if (target_type == TypeManager.byte_type){
857 if (value >= 0 && value <= Byte.MaxValue)
859 } else if (target_type == TypeManager.short_type){
860 if (value >= Int16.MinValue && value <= Int16.MaxValue)
862 } else if (target_type == TypeManager.ushort_type){
863 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
865 } else if (target_type == TypeManager.uint32_type){
868 } else if (target_type == TypeManager.uint64_type){
870 // we can optimize this case: a positive int32
871 // always fits on a uint64. But we need an opcode
878 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
882 if (expr is LongConstant && target_type == TypeManager.uint64_type){
884 // Try the implicit constant expression conversion
885 // from long to ulong, instead of a nice routine,
888 long v = ((LongConstant) expr).Value;
894 // If `expr_type' implements `target_type' (which is an iface)
895 // see TryImplicitIntConversion
897 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
900 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
903 if (expr_type == TypeManager.anonymous_method_type){
904 if (!TypeManager.IsDelegateType (target_type) &&
905 TypeManager.DropGenericTypeArguments (target_type) != TypeManager.expression_type)
908 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
909 return ame.ImplicitStandardConversionExists (target_type);
916 /// Finds "most encompassed type" according to the spec (13.4.2)
917 /// amongst the methods in the MethodGroupExpr
919 static Type FindMostEncompassedType (ArrayList types)
923 if (types.Count == 0)
926 if (types.Count == 1)
927 return (Type) types [0];
929 EmptyExpression expr = EmptyExpression.Grab ();
931 foreach (Type t in types) {
938 if (ImplicitStandardConversionExists (expr, best))
943 foreach (Type t in types) {
946 if (!ImplicitStandardConversionExists (expr, t)) {
952 EmptyExpression.Release (expr);
958 /// Finds "most encompassing type" according to the spec (13.4.2)
959 /// amongst the types in the given set
961 static Type FindMostEncompassingType (ArrayList types)
965 if (types.Count == 0)
968 if (types.Count == 1)
969 return (Type) types [0];
971 EmptyExpression expr = EmptyExpression.Grab ();
973 foreach (Type t in types) {
980 if (ImplicitStandardConversionExists (expr, t))
984 foreach (Type t in types) {
988 if (!ImplicitStandardConversionExists (expr, best)) {
994 EmptyExpression.Release (expr);
1000 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1001 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1002 /// for explicit and implicit conversion operators.
1004 static public Type FindMostSpecificSource (IList list,
1005 Expression source, bool apply_explicit_conv_rules)
1007 ArrayList src_types_set = new ArrayList ();
1010 // If any operator converts from S then Sx = S
1012 Type source_type = source.Type;
1013 foreach (MethodBase mb in list){
1014 ParameterData pd = TypeManager.GetParameterData (mb);
1015 Type param_type = pd.ParameterType (0);
1017 if (param_type == source_type)
1020 src_types_set.Add (param_type);
1024 // Explicit Conv rules
1026 if (apply_explicit_conv_rules) {
1027 ArrayList candidate_set = new ArrayList ();
1029 foreach (Type param_type in src_types_set){
1030 if (ImplicitStandardConversionExists (source, param_type))
1031 candidate_set.Add (param_type);
1034 if (candidate_set.Count != 0)
1035 return FindMostEncompassedType (candidate_set);
1041 if (apply_explicit_conv_rules)
1042 return FindMostEncompassingType (src_types_set);
1044 return FindMostEncompassedType (src_types_set);
1048 /// Finds the most specific target Tx according to section 13.4.4
1050 static public Type FindMostSpecificTarget (IList list,
1051 Type target, bool apply_explicit_conv_rules)
1053 ArrayList tgt_types_set = new ArrayList ();
1056 // If any operator converts to T then Tx = T
1058 foreach (MethodInfo mi in list){
1059 Type ret_type = mi.ReturnType;
1060 if (ret_type == target)
1063 tgt_types_set.Add (ret_type);
1067 // Explicit conv rules
1069 if (apply_explicit_conv_rules) {
1070 ArrayList candidate_set = new ArrayList ();
1072 EmptyExpression expr = EmptyExpression.Grab ();
1074 foreach (Type ret_type in tgt_types_set){
1075 expr.SetType (ret_type);
1077 if (ImplicitStandardConversionExists (expr, target))
1078 candidate_set.Add (ret_type);
1081 EmptyExpression.Release (expr);
1083 if (candidate_set.Count != 0)
1084 return FindMostEncompassingType (candidate_set);
1088 // Okay, final case !
1090 if (apply_explicit_conv_rules)
1091 return FindMostEncompassedType (tgt_types_set);
1093 return FindMostEncompassingType (tgt_types_set);
1097 /// User-defined Implicit conversions
1099 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1100 Type target, Location loc)
1102 return UserDefinedConversion (ec, source, target, loc, false);
1106 /// User-defined Explicit conversions
1108 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1109 Type target, Location loc)
1111 return UserDefinedConversion (ec, source, target, loc, true);
1114 static void AddConversionOperators (ArrayList list,
1115 Expression source, Type target_type,
1116 bool look_for_explicit,
1122 Type source_type = source.Type;
1123 EmptyExpression expr = EmptyExpression.Grab ();
1124 foreach (MethodInfo m in mg.Methods) {
1125 ParameterData pd = TypeManager.GetParameterData (m);
1126 Type return_type = m.ReturnType;
1127 Type arg_type = pd.ParameterType (0);
1129 if (source_type != arg_type) {
1130 if (!ImplicitStandardConversionExists (source, arg_type)) {
1131 if (!look_for_explicit)
1133 expr.SetType (arg_type);
1134 if (!ImplicitStandardConversionExists (expr, source_type))
1139 if (target_type != return_type) {
1140 expr.SetType (return_type);
1141 if (!ImplicitStandardConversionExists (expr, target_type)) {
1142 if (!look_for_explicit)
1144 expr.SetType (target_type);
1145 if (!ImplicitStandardConversionExists (expr, return_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 (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;
1227 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1229 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1230 method = (MethodInfo) o;
1232 method = GetConversionOperator (null, source, target, look_for_explicit);
1233 if (!(source is Constant))
1234 hash.Insert (source_type, target, method);
1240 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1243 // This will do the conversion to the best match that we
1244 // found. Now we need to perform an implict standard conversion
1245 // if the best match was not the type that we were requested
1248 if (look_for_explicit)
1249 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1251 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1257 e = new UserCast (method, source, loc);
1258 if (e.Type != target){
1259 if (!look_for_explicit)
1260 e = ImplicitConversionStandard (ec, e, target, loc);
1262 e = ExplicitConversionStandard (ec, e, target, 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 Type expr_type = expr.Type;
1310 if (TypeManager.IsNullableType (target_type)) {
1312 // From null to any nullable type
1314 if (expr_type == TypeManager.null_type)
1315 return new Nullable.Null (target_type, loc);
1317 Type target = TypeManager.GetTypeArguments (target_type) [0];
1319 if (TypeManager.IsNullableType (expr.Type)) {
1320 e = new Nullable.LiftedConversion (
1321 expr, target_type, false, false, loc).Resolve (ec);
1325 e = ImplicitConversion (ec, expr, target, loc);
1327 return Nullable.Wrap.Create (e, ec);
1331 if (expr.eclass == ExprClass.MethodGroup){
1332 if (!TypeManager.IsDelegateType (target_type)){
1337 // Only allow anonymous method conversions on post ISO_1
1339 if (RootContext.Version != LanguageVersion.ISO_1){
1340 MethodGroupExpr mg = expr as MethodGroupExpr;
1342 return ImplicitDelegateCreation.Create (
1343 ec, mg, target_type, loc);
1347 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1351 // Attempt to do the implicit constant expression conversions
1353 Constant c = expr as Constant;
1356 // If `target_type' is an interface and the type of `ic' implements the interface
1357 // e.g. target_type is IComparable, IConvertible, IFormattable
1359 if (c.Type == TypeManager.int32_type && target_type.IsInterface && target_type.IsAssignableFrom (c.Type))
1360 return new BoxedCast (c, target_type);
1363 c = c.ConvertImplicitly (target_type);
1365 Console.WriteLine ("Conversion error happened in line {0}", loc);
1372 e = ImplicitNumericConversion (expr, target_type);
1376 e = ImplicitReferenceConversion (expr, target_type);
1380 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1381 IntLiteral i = (IntLiteral) expr;
1384 return new EnumConstant ((Constant) expr, target_type);
1388 if (expr_type.IsPointer){
1389 if (target_type == TypeManager.void_ptr_type)
1390 return EmptyCast.Create (expr, target_type);
1393 // yep, comparing pointer types cant be done with
1394 // t1 == t2, we have to compare their element types.
1396 if (target_type.IsPointer){
1397 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1404 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1405 return EmptyCast.Create (NullPointer.Null, target_type);
1408 if (expr_type == TypeManager.anonymous_method_type){
1409 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1410 Expression am = ame.Compatible (ec, target_type);
1412 return am.DoResolve (ec);
1419 /// Attempts to implicitly convert `source' into `target_type', using
1420 /// ImplicitConversion. If there is no implicit conversion, then
1421 /// an error is signaled
1423 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1424 Type target_type, Location loc)
1426 Expression e = ImplicitConversion (ec, source, target_type, loc);
1430 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1435 /// Performs the explicit numeric conversions
1437 /// There are a few conversions that are not part of the C# standard,
1438 /// they were interim hacks in the C# compiler that were supposed to
1439 /// become explicit operators in the UIntPtr class and IntPtr class,
1440 /// but for historical reasons it did not happen, so the C# compiler
1441 /// ended up with these special hacks.
1443 /// See bug 59800 for details.
1445 /// The conversion are:
1455 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1457 Type expr_type = expr.Type;
1458 Type real_target_type = target_type;
1460 if (expr_type == TypeManager.sbyte_type){
1462 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1464 if (real_target_type == TypeManager.byte_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1466 if (real_target_type == TypeManager.ushort_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1468 if (real_target_type == TypeManager.uint32_type)
1469 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1470 if (real_target_type == TypeManager.uint64_type)
1471 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1472 if (real_target_type == TypeManager.char_type)
1473 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1475 // One of the built-in conversions that belonged in the class library
1476 if (real_target_type == TypeManager.uintptr_type){
1477 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1479 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1481 } else if (expr_type == TypeManager.byte_type){
1483 // From byte to sbyte and char
1485 if (real_target_type == TypeManager.sbyte_type)
1486 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1487 if (real_target_type == TypeManager.char_type)
1488 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1489 } else if (expr_type == TypeManager.short_type){
1491 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1493 if (real_target_type == TypeManager.sbyte_type)
1494 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1495 if (real_target_type == TypeManager.byte_type)
1496 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1497 if (real_target_type == TypeManager.ushort_type)
1498 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1499 if (real_target_type == TypeManager.uint32_type)
1500 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1501 if (real_target_type == TypeManager.uint64_type)
1502 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1503 if (real_target_type == TypeManager.char_type)
1504 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1506 // One of the built-in conversions that belonged in the class library
1507 if (real_target_type == TypeManager.uintptr_type){
1508 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1510 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1512 } else if (expr_type == TypeManager.ushort_type){
1514 // From ushort to sbyte, byte, short, char
1516 if (real_target_type == TypeManager.sbyte_type)
1517 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1518 if (real_target_type == TypeManager.byte_type)
1519 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1520 if (real_target_type == TypeManager.short_type)
1521 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1522 if (real_target_type == TypeManager.char_type)
1523 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1524 } else if (expr_type == TypeManager.int32_type){
1526 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1528 if (real_target_type == TypeManager.sbyte_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1530 if (real_target_type == TypeManager.byte_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1532 if (real_target_type == TypeManager.short_type)
1533 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1534 if (real_target_type == TypeManager.ushort_type)
1535 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1536 if (real_target_type == TypeManager.uint32_type)
1537 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1538 if (real_target_type == TypeManager.uint64_type)
1539 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1540 if (real_target_type == TypeManager.char_type)
1541 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1543 // One of the built-in conversions that belonged in the class library
1544 if (real_target_type == TypeManager.uintptr_type){
1545 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1547 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1549 } else if (expr_type == TypeManager.uint32_type){
1551 // From uint to sbyte, byte, short, ushort, int, char
1553 if (real_target_type == TypeManager.sbyte_type)
1554 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1555 if (real_target_type == TypeManager.byte_type)
1556 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1557 if (real_target_type == TypeManager.short_type)
1558 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1559 if (real_target_type == TypeManager.ushort_type)
1560 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1561 if (real_target_type == TypeManager.int32_type)
1562 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1563 if (real_target_type == TypeManager.char_type)
1564 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1565 } else if (expr_type == TypeManager.int64_type){
1567 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1569 if (real_target_type == TypeManager.sbyte_type)
1570 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1571 if (real_target_type == TypeManager.byte_type)
1572 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1573 if (real_target_type == TypeManager.short_type)
1574 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1575 if (real_target_type == TypeManager.ushort_type)
1576 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1577 if (real_target_type == TypeManager.int32_type)
1578 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1579 if (real_target_type == TypeManager.uint32_type)
1580 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1581 if (real_target_type == TypeManager.uint64_type)
1582 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1583 if (real_target_type == TypeManager.char_type)
1584 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1585 } else if (expr_type == TypeManager.uint64_type){
1587 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1589 if (real_target_type == TypeManager.sbyte_type)
1590 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1591 if (real_target_type == TypeManager.byte_type)
1592 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1593 if (real_target_type == TypeManager.short_type)
1594 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1595 if (real_target_type == TypeManager.ushort_type)
1596 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1597 if (real_target_type == TypeManager.int32_type)
1598 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1599 if (real_target_type == TypeManager.uint32_type)
1600 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1601 if (real_target_type == TypeManager.int64_type)
1602 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1603 if (real_target_type == TypeManager.char_type)
1604 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1606 // One of the built-in conversions that belonged in the class library
1607 if (real_target_type == TypeManager.intptr_type){
1608 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1609 TypeManager.intptr_type, true);
1611 } else if (expr_type == TypeManager.char_type){
1613 // From char to sbyte, byte, short
1615 if (real_target_type == TypeManager.sbyte_type)
1616 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1617 if (real_target_type == TypeManager.byte_type)
1618 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1619 if (real_target_type == TypeManager.short_type)
1620 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1621 } else if (expr_type == TypeManager.float_type){
1623 // From float to sbyte, byte, short,
1624 // ushort, int, uint, long, ulong, char
1627 if (real_target_type == TypeManager.sbyte_type)
1628 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1629 if (real_target_type == TypeManager.byte_type)
1630 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1631 if (real_target_type == TypeManager.short_type)
1632 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1633 if (real_target_type == TypeManager.ushort_type)
1634 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1635 if (real_target_type == TypeManager.int32_type)
1636 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1637 if (real_target_type == TypeManager.uint32_type)
1638 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1639 if (real_target_type == TypeManager.int64_type)
1640 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1641 if (real_target_type == TypeManager.uint64_type)
1642 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1643 if (real_target_type == TypeManager.char_type)
1644 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1645 if (real_target_type == TypeManager.decimal_type)
1646 return new CastToDecimal (expr, true);
1647 } else if (expr_type == TypeManager.double_type){
1649 // From double to sbyte, byte, short,
1650 // ushort, int, uint, long, ulong,
1651 // char, float or decimal
1653 if (real_target_type == TypeManager.sbyte_type)
1654 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1655 if (real_target_type == TypeManager.byte_type)
1656 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1657 if (real_target_type == TypeManager.short_type)
1658 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1659 if (real_target_type == TypeManager.ushort_type)
1660 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1661 if (real_target_type == TypeManager.int32_type)
1662 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1663 if (real_target_type == TypeManager.uint32_type)
1664 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1665 if (real_target_type == TypeManager.int64_type)
1666 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1667 if (real_target_type == TypeManager.uint64_type)
1668 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1669 if (real_target_type == TypeManager.char_type)
1670 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1671 if (real_target_type == TypeManager.float_type)
1672 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1673 if (real_target_type == TypeManager.decimal_type)
1674 return new CastToDecimal (expr, true);
1675 } else if (expr_type == TypeManager.uintptr_type){
1677 // Various built-in conversions that belonged in the class library
1679 // from uintptr to sbyte, short, int32
1681 if (real_target_type == TypeManager.sbyte_type){
1682 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1683 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1685 if (real_target_type == TypeManager.short_type){
1686 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1687 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1689 if (real_target_type == TypeManager.int32_type){
1690 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1691 TypeManager.int32_type);
1693 } else if (expr_type == TypeManager.intptr_type){
1694 if (real_target_type == TypeManager.uint64_type){
1695 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1696 TypeManager.uint64_type);
1698 } else if (expr_type == TypeManager.decimal_type) {
1699 return new CastFromDecimal (expr, target_type).Resolve ();
1705 /// Returns whether an explicit reference conversion can be performed
1706 /// from source_type to target_type
1708 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1710 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1711 bool target_is_value_type = target_type.IsValueType;
1713 if (source_type == target_type)
1717 // From generic parameter to any type
1719 if (TypeManager.IsGenericParameter (source_type))
1720 return ExplicitTypeParameterConversionExists (source_type, target_type);
1723 // From object to a generic parameter
1725 if (source_type == TypeManager.object_type && target_is_type_param)
1729 // From object to any reference type
1731 if (source_type == TypeManager.object_type && !target_is_value_type)
1735 // From any class S to any class-type T, provided S is a base class of T
1737 if (TypeManager.IsSubclassOf (target_type, source_type))
1741 // From any interface type S to any interface T provided S is not derived from T
1743 if (source_type.IsInterface && target_type.IsInterface){
1744 if (!TypeManager.IsSubclassOf (target_type, source_type))
1749 // From any class type S to any interface T, provided S is not sealed
1750 // and provided S does not implement T.
1752 if (target_type.IsInterface && !source_type.IsSealed &&
1753 !TypeManager.ImplementsInterface (source_type, target_type))
1757 // From any interface-type S to to any class type T, provided T is not
1758 // sealed, or provided T implements S.
1760 if (source_type.IsInterface &&
1761 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1765 if (source_type.IsInterface && IList_To_Array (source_type, target_type))
1768 // From an array type S with an element type Se to an array type T with an
1769 // element type Te provided all the following are true:
1770 // * S and T differe only in element type, in other words, S and T
1771 // have the same number of dimensions.
1772 // * Both Se and Te are reference types
1773 // * An explicit referenc conversions exist from Se to Te
1775 if (source_type.IsArray && target_type.IsArray) {
1776 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1778 Type source_element_type = TypeManager.GetElementType (source_type);
1779 Type target_element_type = TypeManager.GetElementType (target_type);
1781 if (TypeManager.IsGenericParameter (source_element_type) ||
1782 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1783 if (ExplicitReferenceConversionExists (source_element_type,
1784 target_element_type))
1790 // From System.Array to any array-type
1791 if (source_type == TypeManager.array_type &&
1792 target_type.IsArray){
1797 // From System delegate to any delegate-type
1799 if (source_type == TypeManager.delegate_type &&
1800 TypeManager.IsDelegateType (target_type))
1804 // From ICloneable to Array or Delegate types
1806 if (source_type == TypeManager.icloneable_type &&
1807 (target_type == TypeManager.array_type ||
1808 target_type == TypeManager.delegate_type))
1815 /// Implements Explicit Reference conversions
1817 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1819 Type source_type = source.Type;
1820 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1821 bool target_is_value_type = target_type.IsValueType;
1824 // From object to a generic parameter
1826 if (source_type == TypeManager.object_type && target_is_type_param)
1827 return new UnboxCast (source, target_type);
1830 // Explicit type parameter conversion.
1833 if (TypeManager.IsGenericParameter (source_type))
1834 return ExplicitTypeParameterConversion (source, target_type);
1837 // From object to any reference type
1839 if (source_type == TypeManager.object_type && !target_is_value_type)
1840 return new ClassCast (source, target_type);
1843 // Unboxing conversion.
1845 if (((source_type == TypeManager.enum_type &&
1846 !(source is EmptyCast)) ||
1847 source_type == TypeManager.value_type) && target_is_value_type)
1848 return new UnboxCast (source, target_type);
1851 // From any class S to any class-type T, provided S is a base class of T
1853 if (TypeManager.IsSubclassOf (target_type, source_type))
1854 return new ClassCast (source, target_type);
1857 // From any interface type S to any interface T provided S is not derived from T
1859 if (source_type.IsInterface && target_type.IsInterface){
1860 if (TypeManager.ImplementsInterface (source_type, target_type))
1863 return new ClassCast (source, target_type);
1867 // From any class type S to any interface T, provides S is not sealed
1868 // and provided S does not implement T.
1870 if (target_type.IsInterface && !source_type.IsSealed) {
1871 if (TypeManager.ImplementsInterface (source_type, target_type))
1874 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);
1935 // From ICloneable to Array or Delegate types
1937 if (source_type == TypeManager.icloneable_type &&
1938 (target_type == TypeManager.array_type ||
1939 target_type == TypeManager.delegate_type))
1940 return new ClassCast (source, target_type);
1946 /// Performs an explicit conversion of the expression `expr' whose
1947 /// type is expr.Type to `target_type'.
1949 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1950 Type target_type, Location loc)
1952 Type expr_type = expr.Type;
1954 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1955 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1960 // Unboxing conversions; only object types can be convertible to enum
1962 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1963 return new UnboxCast (expr, target_type);
1965 if (TypeManager.IsEnumType (expr_type)) {
1966 Expression underlying = EmptyCast.Create (expr, TypeManager.EnumToUnderlying (expr_type));
1967 expr = ExplicitConversionCore (ec, underlying, target_type, loc);
1971 return ExplicitUserConversion (ec, underlying, target_type, loc);
1974 if (TypeManager.IsEnumType (target_type)){
1975 if (expr_type == TypeManager.enum_type)
1976 return new UnboxCast (expr, target_type);
1978 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1980 return EmptyCast.Create (ce, target_type);
1983 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1985 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1986 ne = ExplicitUserConversion (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1988 return ExplicitConversionCore (ec, ne, target_type, loc);
1994 ne = ExplicitNumericConversion (expr, target_type);
1999 // Skip the ExplicitReferenceConversion because we can not convert
2000 // from Null to a ValueType, and ExplicitReference wont check against
2001 // null literal explicitly
2003 if (expr_type != TypeManager.null_type){
2004 ne = ExplicitReferenceConversion (expr, target_type);
2010 ne = ExplicitUnsafe (expr, target_type);
2018 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
2020 Type expr_type = expr.Type;
2022 if (target_type.IsPointer){
2023 if (expr_type.IsPointer)
2024 return EmptyCast.Create (expr, target_type);
2026 if (expr_type == TypeManager.sbyte_type ||
2027 expr_type == TypeManager.short_type ||
2028 expr_type == TypeManager.int32_type ||
2029 expr_type == TypeManager.int64_type)
2030 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
2032 if (expr_type == TypeManager.ushort_type ||
2033 expr_type == TypeManager.uint32_type ||
2034 expr_type == TypeManager.uint64_type ||
2035 expr_type == TypeManager.byte_type)
2036 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2039 if (expr_type.IsPointer){
2040 if (target_type == TypeManager.sbyte_type)
2041 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2042 else if (target_type == TypeManager.byte_type)
2043 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2044 else if (target_type == TypeManager.short_type)
2045 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2046 else if (target_type == TypeManager.ushort_type)
2047 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2048 else if (target_type == TypeManager.int32_type)
2049 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2050 else if (target_type == TypeManager.uint32_type)
2051 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2052 else if (target_type == TypeManager.uint64_type)
2053 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2054 else if (target_type == TypeManager.int64_type){
2055 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2062 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2064 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2065 Type target_type, Location l)
2067 int errors = Report.Errors;
2068 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2069 if (Report.Errors > errors)
2075 ne = ExplicitNumericConversion (expr, target_type);
2079 ne = ExplicitReferenceConversion (expr, target_type);
2083 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2084 return EmptyCast.Create (expr, target_type);
2086 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
2091 /// Performs an explicit conversion of the expression `expr' whose
2092 /// type is expr.Type to `target_type'.
2094 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2095 Type target_type, Location loc)
2099 Type expr_type = expr.Type;
2100 if (TypeManager.IsNullableType (target_type)) {
2101 if (TypeManager.IsNullableType (expr_type)) {
2102 e = new Nullable.LiftedConversion (
2103 expr, target_type, false, true, loc).Resolve (ec);
2106 } else if (expr_type == TypeManager.object_type) {
2107 return new UnboxCast (expr, target_type);
2109 Type target = TypeManager.GetTypeArguments (target_type) [0];
2111 e = ExplicitConversionCore (ec, expr, target, loc);
2113 return Nullable.Wrap.Create (e, ec);
2115 } else if (TypeManager.IsNullableType (expr_type)) {
2116 e = ExplicitConversion (ec, Nullable.Unwrap.Create (expr, ec), target_type, loc);
2118 e = EmptyCast.Create (e, target_type);
2122 e = ExplicitConversionCore (ec, expr, target_type, loc);
2126 e = ExplicitUserConversion (ec, expr, target_type, loc);
2130 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);