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))
907 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
908 return ame.ImplicitStandardConversionExists (target_type);
915 /// Finds "most encompassed type" according to the spec (13.4.2)
916 /// amongst the methods in the MethodGroupExpr
918 static Type FindMostEncompassedType (ArrayList types)
922 if (types.Count == 0)
925 if (types.Count == 1)
926 return (Type) types [0];
928 EmptyExpression expr = EmptyExpression.Grab ();
930 foreach (Type t in types) {
937 if (ImplicitStandardConversionExists (expr, best))
942 foreach (Type t in types) {
945 if (!ImplicitStandardConversionExists (expr, t)) {
951 EmptyExpression.Release (expr);
957 /// Finds "most encompassing type" according to the spec (13.4.2)
958 /// amongst the types in the given set
960 static Type FindMostEncompassingType (ArrayList types)
964 if (types.Count == 0)
967 if (types.Count == 1)
968 return (Type) types [0];
970 EmptyExpression expr = EmptyExpression.Grab ();
972 foreach (Type t in types) {
979 if (ImplicitStandardConversionExists (expr, t))
983 foreach (Type t in types) {
987 if (!ImplicitStandardConversionExists (expr, best)) {
993 EmptyExpression.Release (expr);
999 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1000 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1001 /// for explicit and implicit conversion operators.
1003 static public Type FindMostSpecificSource (IList list,
1004 Expression source, bool apply_explicit_conv_rules)
1006 ArrayList src_types_set = new ArrayList ();
1009 // If any operator converts from S then Sx = S
1011 Type source_type = source.Type;
1012 foreach (MethodBase mb in list){
1013 ParameterData pd = TypeManager.GetParameterData (mb);
1014 Type param_type = pd.ParameterType (0);
1016 if (param_type == source_type)
1019 src_types_set.Add (param_type);
1023 // Explicit Conv rules
1025 if (apply_explicit_conv_rules) {
1026 ArrayList candidate_set = new ArrayList ();
1028 foreach (Type param_type in src_types_set){
1029 if (ImplicitStandardConversionExists (source, param_type))
1030 candidate_set.Add (param_type);
1033 if (candidate_set.Count != 0)
1034 return FindMostEncompassedType (candidate_set);
1040 if (apply_explicit_conv_rules)
1041 return FindMostEncompassingType (src_types_set);
1043 return FindMostEncompassedType (src_types_set);
1047 /// Finds the most specific target Tx according to section 13.4.4
1049 static public Type FindMostSpecificTarget (IList list,
1050 Type target, bool apply_explicit_conv_rules)
1052 ArrayList tgt_types_set = new ArrayList ();
1055 // If any operator converts to T then Tx = T
1057 foreach (MethodInfo mi in list){
1058 Type ret_type = mi.ReturnType;
1059 if (ret_type == target)
1062 tgt_types_set.Add (ret_type);
1066 // Explicit conv rules
1068 if (apply_explicit_conv_rules) {
1069 ArrayList candidate_set = new ArrayList ();
1071 EmptyExpression expr = EmptyExpression.Grab ();
1073 foreach (Type ret_type in tgt_types_set){
1074 expr.SetType (ret_type);
1076 if (ImplicitStandardConversionExists (expr, target))
1077 candidate_set.Add (ret_type);
1080 EmptyExpression.Release (expr);
1082 if (candidate_set.Count != 0)
1083 return FindMostEncompassingType (candidate_set);
1087 // Okay, final case !
1089 if (apply_explicit_conv_rules)
1090 return FindMostEncompassedType (tgt_types_set);
1092 return FindMostEncompassingType (tgt_types_set);
1096 /// User-defined Implicit conversions
1098 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1099 Type target, Location loc)
1101 return UserDefinedConversion (ec, source, target, loc, false);
1105 /// User-defined Explicit conversions
1107 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1108 Type target, Location loc)
1110 return UserDefinedConversion (ec, source, target, loc, true);
1113 static void AddConversionOperators (ArrayList list,
1114 Expression source, Type target_type,
1115 bool look_for_explicit,
1121 Type source_type = source.Type;
1122 EmptyExpression expr = EmptyExpression.Grab ();
1123 foreach (MethodInfo m in mg.Methods) {
1124 ParameterData pd = TypeManager.GetParameterData (m);
1125 Type return_type = m.ReturnType;
1126 Type arg_type = pd.ParameterType (0);
1128 if (source_type != arg_type) {
1129 if (!ImplicitStandardConversionExists (source, arg_type)) {
1130 if (!look_for_explicit)
1132 expr.SetType (arg_type);
1133 if (!ImplicitStandardConversionExists (expr, source_type))
1138 if (target_type != return_type) {
1139 expr.SetType (return_type);
1140 if (!ImplicitStandardConversionExists (expr, target_type)) {
1141 if (!look_for_explicit)
1143 expr.SetType (target_type);
1144 if (!ImplicitStandardConversionExists (expr, return_type))
1152 EmptyExpression.Release (expr);
1156 /// Compute the user-defined conversion operator from source_type to target_type.
1157 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1159 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1161 ArrayList ops = new ArrayList (4);
1163 Type source_type = source.Type;
1165 if (source_type != TypeManager.decimal_type) {
1166 AddConversionOperators (ops, source, target_type, look_for_explicit,
1167 Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1168 if (look_for_explicit) {
1169 AddConversionOperators (ops, source, target_type, look_for_explicit,
1170 Expression.MethodLookup (
1171 container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1175 if (target_type != TypeManager.decimal_type) {
1176 AddConversionOperators (ops, source, target_type, look_for_explicit,
1177 Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1178 if (look_for_explicit) {
1179 AddConversionOperators (ops, source, target_type, look_for_explicit,
1180 Expression.MethodLookup (
1181 container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1188 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1189 if (most_specific_source == null)
1192 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1193 if (most_specific_target == null)
1196 MethodInfo method = null;
1198 foreach (MethodInfo m in ops) {
1199 if (m.ReturnType != most_specific_target)
1201 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1203 // Ambiguous: more than one conversion operator satisfies the signature.
1212 static DoubleHash explicit_conv = new DoubleHash (100);
1213 static DoubleHash implicit_conv = new DoubleHash (100);
1216 /// User-defined conversions
1218 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1219 Type target, Location loc,
1220 bool look_for_explicit)
1222 Type source_type = source.Type;
1223 MethodInfo method = null;
1226 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1228 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1229 method = (MethodInfo) o;
1231 method = GetConversionOperator (null, source, target, look_for_explicit);
1232 if (!(source is Constant))
1233 hash.Insert (source_type, target, method);
1239 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1242 // This will do the conversion to the best match that we
1243 // found. Now we need to perform an implict standard conversion
1244 // if the best match was not the type that we were requested
1247 if (look_for_explicit)
1248 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1250 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1256 e = new UserCast (method, source, loc);
1257 if (e.Type != target){
1258 if (!look_for_explicit)
1259 e = ImplicitConversionStandard (ec, e, target, loc);
1261 e = ExplicitConversionStandard (ec, e, target, loc);
1268 /// Converts implicitly the resolved expression `expr' into the
1269 /// `target_type'. It returns a new expression that can be used
1270 /// in a context that expects a `target_type'.
1272 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1273 Type target_type, Location loc)
1277 if (target_type == null)
1278 throw new Exception ("Target type is null");
1280 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1284 e = ImplicitUserConversion (ec, expr, target_type, loc);
1293 /// Attempts to apply the `Standard Implicit
1294 /// Conversion' rules to the expression `expr' into
1295 /// the `target_type'. It returns a new expression
1296 /// that can be used in a context that expects a
1299 /// This is different from `ImplicitConversion' in that the
1300 /// user defined implicit conversions are excluded.
1302 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1303 Type target_type, Location loc)
1305 Type expr_type = expr.Type;
1308 // TODO: refactor to be a part of constant infrastructure
1309 if (expr is NullLiteral) {
1310 if (TypeManager.IsNullableType (target_type))
1311 return new Nullable.NullableLiteral (target_type, loc);
1314 if (TypeManager.IsNullableType (target_type)) {
1315 Type target = TypeManager.GetTypeArguments (target_type) [0];
1317 if (TypeManager.IsNullableType (expr.Type)) {
1318 e = new Nullable.LiftedConversion (
1319 expr, target_type, false, false, loc).Resolve (ec);
1323 e = ImplicitConversion (ec, expr, target, loc);
1325 return Nullable.Wrap.Create (e, ec);
1329 if (expr.eclass == ExprClass.MethodGroup){
1330 if (!TypeManager.IsDelegateType (target_type)){
1335 // Only allow anonymous method conversions on post ISO_1
1337 if (RootContext.Version != LanguageVersion.ISO_1){
1338 MethodGroupExpr mg = expr as MethodGroupExpr;
1340 return ImplicitDelegateCreation.Create (
1341 ec, mg, target_type, loc);
1345 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1349 // Attempt to do the implicit constant expression conversions
1351 Constant c = expr as Constant;
1354 // If `target_type' is an interface and the type of `ic' implements the interface
1355 // e.g. target_type is IComparable, IConvertible, IFormattable
1357 if (c.Type == TypeManager.int32_type && target_type.IsInterface && target_type.IsAssignableFrom (c.Type))
1358 return new BoxedCast (c, target_type);
1361 c = c.ConvertImplicitly (target_type);
1363 Console.WriteLine ("Conversion error happened in line {0}", loc);
1370 e = ImplicitNumericConversion (expr, target_type);
1374 e = ImplicitReferenceConversion (expr, target_type);
1378 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1379 IntLiteral i = (IntLiteral) expr;
1382 return new EnumConstant ((Constant) expr, target_type);
1386 if (expr_type.IsPointer){
1387 if (target_type == TypeManager.void_ptr_type)
1388 return EmptyCast.Create (expr, target_type);
1391 // yep, comparing pointer types cant be done with
1392 // t1 == t2, we have to compare their element types.
1394 if (target_type.IsPointer){
1395 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1402 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1403 return EmptyCast.Create (NullPointer.Null, target_type);
1406 if (expr_type == TypeManager.anonymous_method_type){
1407 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1408 Expression am = ame.Compatible (ec, target_type);
1410 return am.DoResolve (ec);
1417 /// Attempts to implicitly convert `source' into `target_type', using
1418 /// ImplicitConversion. If there is no implicit conversion, then
1419 /// an error is signaled
1421 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1422 Type target_type, Location loc)
1424 Expression e = ImplicitConversion (ec, source, target_type, loc);
1428 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1433 /// Performs the explicit numeric conversions
1435 /// There are a few conversions that are not part of the C# standard,
1436 /// they were interim hacks in the C# compiler that were supposed to
1437 /// become explicit operators in the UIntPtr class and IntPtr class,
1438 /// but for historical reasons it did not happen, so the C# compiler
1439 /// ended up with these special hacks.
1441 /// See bug 59800 for details.
1443 /// The conversion are:
1453 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1455 Type expr_type = expr.Type;
1456 Type real_target_type = target_type;
1458 if (expr_type == TypeManager.sbyte_type){
1460 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1462 if (real_target_type == TypeManager.byte_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1464 if (real_target_type == TypeManager.ushort_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1466 if (real_target_type == TypeManager.uint32_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1468 if (real_target_type == TypeManager.uint64_type)
1469 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1470 if (real_target_type == TypeManager.char_type)
1471 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1473 // One of the built-in conversions that belonged in the class library
1474 if (real_target_type == TypeManager.uintptr_type){
1475 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1477 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1479 } else if (expr_type == TypeManager.byte_type){
1481 // From byte to sbyte and char
1483 if (real_target_type == TypeManager.sbyte_type)
1484 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1485 if (real_target_type == TypeManager.char_type)
1486 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1487 } else if (expr_type == TypeManager.short_type){
1489 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1491 if (real_target_type == TypeManager.sbyte_type)
1492 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1493 if (real_target_type == TypeManager.byte_type)
1494 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1495 if (real_target_type == TypeManager.ushort_type)
1496 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1497 if (real_target_type == TypeManager.uint32_type)
1498 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1499 if (real_target_type == TypeManager.uint64_type)
1500 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1501 if (real_target_type == TypeManager.char_type)
1502 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1504 // One of the built-in conversions that belonged in the class library
1505 if (real_target_type == TypeManager.uintptr_type){
1506 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1508 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1510 } else if (expr_type == TypeManager.ushort_type){
1512 // From ushort to sbyte, byte, short, char
1514 if (real_target_type == TypeManager.sbyte_type)
1515 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1516 if (real_target_type == TypeManager.byte_type)
1517 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1518 if (real_target_type == TypeManager.short_type)
1519 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1520 if (real_target_type == TypeManager.char_type)
1521 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1522 } else if (expr_type == TypeManager.int32_type){
1524 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1526 if (real_target_type == TypeManager.sbyte_type)
1527 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1528 if (real_target_type == TypeManager.byte_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1530 if (real_target_type == TypeManager.short_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1532 if (real_target_type == TypeManager.ushort_type)
1533 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1534 if (real_target_type == TypeManager.uint32_type)
1535 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1536 if (real_target_type == TypeManager.uint64_type)
1537 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1538 if (real_target_type == TypeManager.char_type)
1539 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1541 // One of the built-in conversions that belonged in the class library
1542 if (real_target_type == TypeManager.uintptr_type){
1543 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1545 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1547 } else if (expr_type == TypeManager.uint32_type){
1549 // From uint to sbyte, byte, short, ushort, int, char
1551 if (real_target_type == TypeManager.sbyte_type)
1552 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1553 if (real_target_type == TypeManager.byte_type)
1554 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1555 if (real_target_type == TypeManager.short_type)
1556 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1557 if (real_target_type == TypeManager.ushort_type)
1558 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1559 if (real_target_type == TypeManager.int32_type)
1560 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1561 if (real_target_type == TypeManager.char_type)
1562 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1563 } else if (expr_type == TypeManager.int64_type){
1565 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1567 if (real_target_type == TypeManager.sbyte_type)
1568 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1569 if (real_target_type == TypeManager.byte_type)
1570 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1571 if (real_target_type == TypeManager.short_type)
1572 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1573 if (real_target_type == TypeManager.ushort_type)
1574 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1575 if (real_target_type == TypeManager.int32_type)
1576 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1577 if (real_target_type == TypeManager.uint32_type)
1578 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1579 if (real_target_type == TypeManager.uint64_type)
1580 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1581 if (real_target_type == TypeManager.char_type)
1582 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1583 } else if (expr_type == TypeManager.uint64_type){
1585 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1587 if (real_target_type == TypeManager.sbyte_type)
1588 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1589 if (real_target_type == TypeManager.byte_type)
1590 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1591 if (real_target_type == TypeManager.short_type)
1592 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1593 if (real_target_type == TypeManager.ushort_type)
1594 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1595 if (real_target_type == TypeManager.int32_type)
1596 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1597 if (real_target_type == TypeManager.uint32_type)
1598 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1599 if (real_target_type == TypeManager.int64_type)
1600 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1601 if (real_target_type == TypeManager.char_type)
1602 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1604 // One of the built-in conversions that belonged in the class library
1605 if (real_target_type == TypeManager.intptr_type){
1606 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1607 TypeManager.intptr_type, true);
1609 } else if (expr_type == TypeManager.char_type){
1611 // From char to sbyte, byte, short
1613 if (real_target_type == TypeManager.sbyte_type)
1614 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1615 if (real_target_type == TypeManager.byte_type)
1616 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1617 if (real_target_type == TypeManager.short_type)
1618 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1619 } else if (expr_type == TypeManager.float_type){
1621 // From float to sbyte, byte, short,
1622 // ushort, int, uint, long, ulong, char
1625 if (real_target_type == TypeManager.sbyte_type)
1626 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1627 if (real_target_type == TypeManager.byte_type)
1628 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1629 if (real_target_type == TypeManager.short_type)
1630 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1631 if (real_target_type == TypeManager.ushort_type)
1632 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1633 if (real_target_type == TypeManager.int32_type)
1634 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1635 if (real_target_type == TypeManager.uint32_type)
1636 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1637 if (real_target_type == TypeManager.int64_type)
1638 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1639 if (real_target_type == TypeManager.uint64_type)
1640 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1641 if (real_target_type == TypeManager.char_type)
1642 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1643 if (real_target_type == TypeManager.decimal_type)
1644 return new CastToDecimal (expr, true);
1645 } else if (expr_type == TypeManager.double_type){
1647 // From double to sbyte, byte, short,
1648 // ushort, int, uint, long, ulong,
1649 // char, float or decimal
1651 if (real_target_type == TypeManager.sbyte_type)
1652 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1653 if (real_target_type == TypeManager.byte_type)
1654 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1655 if (real_target_type == TypeManager.short_type)
1656 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1657 if (real_target_type == TypeManager.ushort_type)
1658 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1659 if (real_target_type == TypeManager.int32_type)
1660 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1661 if (real_target_type == TypeManager.uint32_type)
1662 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1663 if (real_target_type == TypeManager.int64_type)
1664 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1665 if (real_target_type == TypeManager.uint64_type)
1666 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1667 if (real_target_type == TypeManager.char_type)
1668 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1669 if (real_target_type == TypeManager.float_type)
1670 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1671 if (real_target_type == TypeManager.decimal_type)
1672 return new CastToDecimal (expr, true);
1673 } else if (expr_type == TypeManager.uintptr_type){
1675 // Various built-in conversions that belonged in the class library
1677 // from uintptr to sbyte, short, int32
1679 if (real_target_type == TypeManager.sbyte_type){
1680 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1681 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1683 if (real_target_type == TypeManager.short_type){
1684 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1685 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1687 if (real_target_type == TypeManager.int32_type){
1688 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1689 TypeManager.int32_type);
1691 } else if (expr_type == TypeManager.intptr_type){
1692 if (real_target_type == TypeManager.uint64_type){
1693 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1694 TypeManager.uint64_type);
1696 } else if (expr_type == TypeManager.decimal_type) {
1697 return new CastFromDecimal (expr, target_type).Resolve ();
1703 /// Returns whether an explicit reference conversion can be performed
1704 /// from source_type to target_type
1706 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1708 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1709 bool target_is_value_type = target_type.IsValueType;
1711 if (source_type == target_type)
1715 // From generic parameter to any type
1717 if (TypeManager.IsGenericParameter (source_type))
1718 return ExplicitTypeParameterConversionExists (source_type, target_type);
1721 // From object to a generic parameter
1723 if (source_type == TypeManager.object_type && target_is_type_param)
1727 // From object to any reference type
1729 if (source_type == TypeManager.object_type && !target_is_value_type)
1733 // From any class S to any class-type T, provided S is a base class of T
1735 if (TypeManager.IsSubclassOf (target_type, source_type))
1739 // From any interface type S to any interface T provided S is not derived from T
1741 if (source_type.IsInterface && target_type.IsInterface){
1742 if (!TypeManager.IsSubclassOf (target_type, source_type))
1747 // From any class type S to any interface T, provided S is not sealed
1748 // and provided S does not implement T.
1750 if (target_type.IsInterface && !source_type.IsSealed &&
1751 !TypeManager.ImplementsInterface (source_type, target_type))
1755 // From any interface-type S to to any class type T, provided T is not
1756 // sealed, or provided T implements S.
1758 if (source_type.IsInterface &&
1759 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1763 if (source_type.IsInterface && IList_To_Array (source_type, target_type))
1766 // From an array type S with an element type Se to an array type T with an
1767 // element type Te provided all the following are true:
1768 // * S and T differe only in element type, in other words, S and T
1769 // have the same number of dimensions.
1770 // * Both Se and Te are reference types
1771 // * An explicit referenc conversions exist from Se to Te
1773 if (source_type.IsArray && target_type.IsArray) {
1774 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1776 Type source_element_type = TypeManager.GetElementType (source_type);
1777 Type target_element_type = TypeManager.GetElementType (target_type);
1779 if (TypeManager.IsGenericParameter (source_element_type) ||
1780 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1781 if (ExplicitReferenceConversionExists (source_element_type,
1782 target_element_type))
1788 // From System.Array to any array-type
1789 if (source_type == TypeManager.array_type &&
1790 target_type.IsArray){
1795 // From System delegate to any delegate-type
1797 if (source_type == TypeManager.delegate_type &&
1798 TypeManager.IsDelegateType (target_type))
1802 // From ICloneable to Array or Delegate types
1804 if (source_type == TypeManager.icloneable_type &&
1805 (target_type == TypeManager.array_type ||
1806 target_type == TypeManager.delegate_type))
1813 /// Implements Explicit Reference conversions
1815 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1817 Type source_type = source.Type;
1818 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1819 bool target_is_value_type = target_type.IsValueType;
1822 // From object to a generic parameter
1824 if (source_type == TypeManager.object_type && target_is_type_param)
1825 return new UnboxCast (source, target_type);
1828 // Explicit type parameter conversion.
1831 if (TypeManager.IsGenericParameter (source_type))
1832 return ExplicitTypeParameterConversion (source, target_type);
1835 // From object to any reference type
1837 if (source_type == TypeManager.object_type && !target_is_value_type)
1838 return new ClassCast (source, target_type);
1841 // Unboxing conversion.
1843 if (((source_type == TypeManager.enum_type &&
1844 !(source is EmptyCast)) ||
1845 source_type == TypeManager.value_type) && target_is_value_type)
1846 return new UnboxCast (source, target_type);
1849 // From any class S to any class-type T, provided S is a base class of T
1851 if (TypeManager.IsSubclassOf (target_type, source_type))
1852 return new ClassCast (source, target_type);
1855 // From any interface type S to any interface T provided S is not derived from T
1857 if (source_type.IsInterface && target_type.IsInterface){
1858 if (TypeManager.ImplementsInterface (source_type, target_type))
1861 return new ClassCast (source, target_type);
1865 // From any class type S to any interface T, provides S is not sealed
1866 // and provided S does not implement T.
1868 if (target_type.IsInterface && !source_type.IsSealed) {
1869 if (TypeManager.ImplementsInterface (source_type, target_type))
1872 return new ClassCast (source, target_type);
1877 // From any interface-type S to to any class type T, provided T is not
1878 // sealed, or provided T implements S.
1880 if (source_type.IsInterface) {
1881 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1882 if (target_type.IsClass)
1883 return new ClassCast (source, target_type);
1885 return new UnboxCast (source, target_type);
1889 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1890 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1892 if (IList_To_Array (source_type, target_type))
1893 return new ClassCast (source, target_type);
1898 // From an array type S with an element type Se to an array type T with an
1899 // element type Te provided all the following are true:
1900 // * S and T differe only in element type, in other words, S and T
1901 // have the same number of dimensions.
1902 // * Both Se and Te are reference types
1903 // * An explicit referenc conversions exist from Se to Te
1905 if (source_type.IsArray && target_type.IsArray) {
1906 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1908 Type source_element_type = TypeManager.GetElementType (source_type);
1909 Type target_element_type = TypeManager.GetElementType (target_type);
1911 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1912 if (ExplicitReferenceConversionExists (source_element_type,
1913 target_element_type))
1914 return new ClassCast (source, target_type);
1919 // From System.Array to any array-type
1920 if (source_type == TypeManager.array_type &&
1921 target_type.IsArray) {
1922 return new ClassCast (source, target_type);
1926 // From System delegate to any delegate-type
1928 if (source_type == TypeManager.delegate_type &&
1929 TypeManager.IsDelegateType (target_type))
1930 return new ClassCast (source, target_type);
1933 // From ICloneable to Array or Delegate types
1935 if (source_type == TypeManager.icloneable_type &&
1936 (target_type == TypeManager.array_type ||
1937 target_type == TypeManager.delegate_type))
1938 return new ClassCast (source, target_type);
1944 /// Performs an explicit conversion of the expression `expr' whose
1945 /// type is expr.Type to `target_type'.
1947 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1948 Type target_type, Location loc)
1950 Type expr_type = expr.Type;
1952 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1953 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1958 // Unboxing conversions; only object types can be convertible to enum
1960 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1961 return new UnboxCast (expr, target_type);
1963 if (TypeManager.IsEnumType (expr_type)) {
1964 Expression underlying = EmptyCast.Create (expr, TypeManager.EnumToUnderlying (expr_type));
1965 expr = ExplicitConversionCore (ec, underlying, target_type, loc);
1969 return ExplicitUserConversion (ec, underlying, target_type, loc);
1972 if (TypeManager.IsEnumType (target_type)){
1973 if (expr_type == TypeManager.enum_type)
1974 return new UnboxCast (expr, target_type);
1976 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1978 return EmptyCast.Create (ce, target_type);
1981 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1983 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1984 ne = ExplicitUserConversion (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1986 return ExplicitConversionCore (ec, ne, target_type, loc);
1992 ne = ExplicitNumericConversion (expr, target_type);
1997 // Skip the ExplicitReferenceConversion because we can not convert
1998 // from Null to a ValueType, and ExplicitReference wont check against
1999 // null literal explicitly
2001 if (expr_type != TypeManager.null_type){
2002 ne = ExplicitReferenceConversion (expr, target_type);
2008 ne = ExplicitUnsafe (expr, target_type);
2016 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
2018 Type expr_type = expr.Type;
2020 if (target_type.IsPointer){
2021 if (expr_type.IsPointer)
2022 return EmptyCast.Create (expr, target_type);
2024 if (expr_type == TypeManager.sbyte_type ||
2025 expr_type == TypeManager.short_type ||
2026 expr_type == TypeManager.int32_type ||
2027 expr_type == TypeManager.int64_type)
2028 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
2030 if (expr_type == TypeManager.ushort_type ||
2031 expr_type == TypeManager.uint32_type ||
2032 expr_type == TypeManager.uint64_type ||
2033 expr_type == TypeManager.byte_type)
2034 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2037 if (expr_type.IsPointer){
2038 if (target_type == TypeManager.sbyte_type)
2039 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2040 else if (target_type == TypeManager.byte_type)
2041 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2042 else if (target_type == TypeManager.short_type)
2043 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2044 else if (target_type == TypeManager.ushort_type)
2045 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2046 else if (target_type == TypeManager.int32_type)
2047 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2048 else if (target_type == TypeManager.uint32_type)
2049 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2050 else if (target_type == TypeManager.uint64_type)
2051 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2052 else if (target_type == TypeManager.int64_type){
2053 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2060 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2062 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2063 Type target_type, Location l)
2065 int errors = Report.Errors;
2066 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2067 if (Report.Errors > errors)
2073 ne = ExplicitNumericConversion (expr, target_type);
2077 ne = ExplicitReferenceConversion (expr, target_type);
2081 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2082 return EmptyCast.Create (expr, target_type);
2084 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
2089 /// Performs an explicit conversion of the expression `expr' whose
2090 /// type is expr.Type to `target_type'.
2092 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2093 Type target_type, Location loc)
2097 Type expr_type = expr.Type;
2098 if (TypeManager.IsNullableType (target_type)) {
2099 if (TypeManager.IsNullableType (expr_type)) {
2100 e = new Nullable.LiftedConversion (
2101 expr, target_type, false, true, loc).Resolve (ec);
2104 } else if (expr_type == TypeManager.object_type) {
2105 return new UnboxCast (expr, target_type);
2107 Type target = TypeManager.GetTypeArguments (target_type) [0];
2109 e = ExplicitConversionCore (ec, expr, target, loc);
2111 return Nullable.Wrap.Create (e, ec);
2113 } else if (TypeManager.IsNullableType (expr_type)) {
2114 Expression source = Nullable.Unwrap.Create (expr, ec);
2115 if (source != null) {
2116 return ExplicitConversion (ec, source, target_type, loc);
2120 e = ExplicitConversionCore (ec, expr, target_type, loc);
2124 e = ExplicitUserConversion (ec, expr, target_type, loc);
2128 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);