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);
353 if (!TypeManager.IsGenericType (target_type)) {
354 foreach (Type iface in TypeManager.GetInterfaces (target_type)) {
355 if (TypeManager.ImplementsInterface (expr_type, iface))
361 // from any interface type S to interface-type T.
362 if (expr_type.IsInterface && target_type.IsInterface) {
363 return TypeManager.ImplementsInterface (expr_type, target_type);
366 // from an array-type S to an array-type of type T
367 if (expr_type.IsArray && target_type.IsArray) {
368 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
370 Type expr_element_type = TypeManager.GetElementType (expr_type);
372 if (MyEmptyExpr == null)
373 MyEmptyExpr = new EmptyExpression ();
375 MyEmptyExpr.SetType (expr_element_type);
376 Type target_element_type = TypeManager.GetElementType (target_type);
378 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
379 return ImplicitStandardConversionExists (
380 MyEmptyExpr, target_element_type);
384 // from an array-type to System.Array
385 if (expr_type.IsArray && target_type == TypeManager.array_type)
388 // from an array-type of type T to IList<T>
389 if (expr_type.IsArray && Array_To_IList (expr_type, target_type))
392 // from any delegate type to System.Delegate
393 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
394 target_type == TypeManager.delegate_type)
397 // from any array-type or delegate type into System.ICloneable.
398 if (expr_type.IsArray ||
399 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
400 if (target_type == TypeManager.icloneable_type)
403 // from a generic type definition to a generic instance.
404 if (TypeManager.IsEqual (expr_type, target_type))
410 static public bool ImplicitBoxingConversionExists (Expression expr, Type target_type,
411 out bool use_class_cast)
413 Type expr_type = expr.Type;
415 use_class_cast = false;
418 // notice that it is possible to write "ValueType v = 1", the ValueType here
419 // is an abstract class, and not really a value type, so we apply the same rules.
421 if (target_type == TypeManager.object_type) {
423 // A pointer type cannot be converted to object
425 if (expr_type.IsPointer)
428 return TypeManager.IsValueType (expr_type);
429 } else if (target_type == TypeManager.value_type) {
430 return TypeManager.IsValueType (expr_type);
431 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
433 // Special case: enumeration to System.Enum.
434 // System.Enum is not a value type, it is a class, so we need
435 // a boxing conversion
437 if (expr_type.IsEnum || TypeManager.IsGenericParameter (expr_type))
443 // This code is kind of mirrored inside ImplicitStandardConversionExists
444 // with the small distinction that we only probe there
446 // Always ensure that the code here and there is in sync
448 // from any class-type S to any interface-type T.
449 if (target_type.IsInterface) {
450 if (target_type != TypeManager.iconvertible_type &&
451 expr_type.IsValueType && (expr is Constant) &&
452 !(expr is IntLiteral || expr is BoolLiteral ||
453 expr is FloatLiteral || expr is DoubleLiteral ||
454 expr is LongLiteral || expr is CharLiteral ||
455 expr is StringLiteral || expr is DecimalLiteral ||
456 expr is UIntLiteral || expr is ULongLiteral)) {
460 if (TypeManager.ImplementsInterface (expr_type, target_type))
461 return TypeManager.IsGenericParameter (expr_type) ||
462 TypeManager.IsValueType (expr_type);
465 if (TypeManager.IsGenericParameter (expr_type))
466 return ImplicitTypeParameterBoxingConversion (
467 expr_type, target_type, out use_class_cast);
473 // Tests whether an implicit reference conversion exists between expr_type
476 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
478 if (target_type.IsValueType)
481 Type expr_type = expr.Type;
483 // from the null type to any reference-type.
484 if (expr_type == TypeManager.null_type){
485 if (target_type.IsPointer)
488 if (!target_type.IsValueType)
492 if (TypeManager.IsGenericParameter (expr_type))
493 return ImplicitTypeParameterConversion (expr, target_type) != null;
496 if (ImplicitReferenceConversionCore (expr, target_type) ||
497 ImplicitBoxingConversionExists (expr, target_type, out use_class_cast))
504 /// Implicit Numeric Conversions.
506 /// expr is the expression to convert, returns a new expression of type
507 /// target_type or null if an implicit conversion is not possible.
509 static public Expression ImplicitNumericConversion (Expression expr,
512 Type expr_type = expr.Type;
513 Type real_target_type = target_type;
515 if (expr_type == TypeManager.sbyte_type){
517 // From sbyte to short, int, long, float, double, decimal
519 if (real_target_type == TypeManager.int32_type)
520 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
521 if (real_target_type == TypeManager.int64_type)
522 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
523 if (real_target_type == TypeManager.double_type)
524 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
525 if (real_target_type == TypeManager.float_type)
526 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
527 if (real_target_type == TypeManager.short_type)
528 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
529 if (real_target_type == TypeManager.decimal_type)
530 return new CastToDecimal (expr);
531 } else if (expr_type == TypeManager.byte_type){
533 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
535 if ((real_target_type == TypeManager.short_type) ||
536 (real_target_type == TypeManager.ushort_type) ||
537 (real_target_type == TypeManager.int32_type) ||
538 (real_target_type == TypeManager.uint32_type))
539 return EmptyCast.Create (expr, target_type);
541 if (real_target_type == TypeManager.uint64_type)
542 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
543 if (real_target_type == TypeManager.int64_type)
544 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
545 if (real_target_type == TypeManager.float_type)
546 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
547 if (real_target_type == TypeManager.double_type)
548 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
549 if (real_target_type == TypeManager.decimal_type)
550 return new CastToDecimal (expr);
552 } else if (expr_type == TypeManager.short_type){
554 // From short to int, long, float, double, decimal
556 if (real_target_type == TypeManager.int32_type)
557 return EmptyCast.Create (expr, target_type);
558 if (real_target_type == TypeManager.int64_type)
559 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
560 if (real_target_type == TypeManager.double_type)
561 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
562 if (real_target_type == TypeManager.float_type)
563 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
564 if (real_target_type == TypeManager.decimal_type)
565 return new CastToDecimal (expr);
567 } else if (expr_type == TypeManager.ushort_type){
569 // From ushort to int, uint, long, ulong, float, double, decimal
571 if (real_target_type == TypeManager.uint32_type)
572 return EmptyCast.Create (expr, target_type);
574 if (real_target_type == TypeManager.uint64_type)
575 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
576 if (real_target_type == TypeManager.int32_type)
577 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
578 if (real_target_type == TypeManager.int64_type)
579 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
580 if (real_target_type == TypeManager.double_type)
581 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
582 if (real_target_type == TypeManager.float_type)
583 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
584 if (real_target_type == TypeManager.decimal_type)
585 return new CastToDecimal (expr);
586 } else if (expr_type == TypeManager.int32_type){
588 // From int to long, float, double, decimal
590 if (real_target_type == TypeManager.int64_type)
591 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
592 if (real_target_type == TypeManager.double_type)
593 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
594 if (real_target_type == TypeManager.float_type)
595 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
596 if (real_target_type == TypeManager.decimal_type)
597 return new CastToDecimal (expr);
598 } else if (expr_type == TypeManager.uint32_type){
600 // From uint to long, ulong, float, double, decimal
602 if (real_target_type == TypeManager.int64_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
604 if (real_target_type == TypeManager.uint64_type)
605 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
606 if (real_target_type == TypeManager.double_type)
607 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
609 if (real_target_type == TypeManager.float_type)
610 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
612 if (real_target_type == TypeManager.decimal_type)
613 return new CastToDecimal (expr);
614 } else if (expr_type == TypeManager.int64_type){
616 // From long/ulong to float, double
618 if (real_target_type == TypeManager.double_type)
619 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
620 if (real_target_type == TypeManager.float_type)
621 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
622 if (real_target_type == TypeManager.decimal_type)
623 return new CastToDecimal (expr);
624 } else if (expr_type == TypeManager.uint64_type){
626 // From ulong to float, double
628 if (real_target_type == TypeManager.double_type)
629 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
631 if (real_target_type == TypeManager.float_type)
632 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
634 if (real_target_type == TypeManager.decimal_type)
635 return new CastToDecimal (expr);
636 } else if (expr_type == TypeManager.char_type){
638 // From char to ushort, int, uint, long, ulong, float, double, decimal
640 if ((real_target_type == TypeManager.ushort_type) ||
641 (real_target_type == TypeManager.int32_type) ||
642 (real_target_type == TypeManager.uint32_type))
643 return EmptyCast.Create (expr, target_type);
644 if (real_target_type == TypeManager.uint64_type)
645 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
646 if (real_target_type == TypeManager.int64_type)
647 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
648 if (real_target_type == TypeManager.float_type)
649 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
650 if (real_target_type == TypeManager.double_type)
651 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
652 if (real_target_type == TypeManager.decimal_type)
653 return new CastToDecimal (expr);
654 } else if (expr_type == TypeManager.float_type){
658 if (real_target_type == TypeManager.double_type)
659 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
666 /// Same as ImplicitStandardConversionExists except that it also looks at
667 /// implicit user defined conversions - needed for overload resolution
669 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
672 if (expr is NullLiteral) {
673 if (TypeManager.IsGenericParameter (target_type))
674 return TypeParameter_to_Null (target_type);
676 if (TypeManager.IsNullableType (target_type))
680 if (ImplicitStandardConversionExists (expr, target_type))
683 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
686 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
688 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
692 /// Determines if a standard implicit conversion exists from
693 /// expr_type to target_type
695 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
697 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
699 Type expr_type = expr.Type;
701 if (TypeManager.IsNullableType (target_type)) {
702 // if implicit standard conversion S -> T exists, S -> T? and S? -> T? also exists
703 target_type = TypeManager.GetTypeArguments (target_type) [0];
706 if (TypeManager.IsNullableType (expr_type)) {
707 EmptyExpression new_expr = EmptyExpression.Grab ();
708 new_expr.SetType (TypeManager.GetTypeArguments (expr_type) [0]);
709 bool retval = ImplicitStandardConversionExists (new_expr, target_type);
710 EmptyExpression.Release (new_expr);
715 if (expr_type == TypeManager.void_type)
718 //Console.WriteLine ("Expr is {0}", expr);
719 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
720 if (TypeManager.IsEqual (expr_type, target_type))
724 // First numeric conversions
726 if (expr_type == TypeManager.sbyte_type){
728 // From sbyte to short, int, long, float, double, decimal
730 if ((target_type == TypeManager.int32_type) ||
731 (target_type == TypeManager.int64_type) ||
732 (target_type == TypeManager.double_type) ||
733 (target_type == TypeManager.float_type) ||
734 (target_type == TypeManager.short_type) ||
735 (target_type == TypeManager.decimal_type))
738 } else if (expr_type == TypeManager.byte_type){
740 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
742 if ((target_type == TypeManager.short_type) ||
743 (target_type == TypeManager.ushort_type) ||
744 (target_type == TypeManager.int32_type) ||
745 (target_type == TypeManager.uint32_type) ||
746 (target_type == TypeManager.uint64_type) ||
747 (target_type == TypeManager.int64_type) ||
748 (target_type == TypeManager.float_type) ||
749 (target_type == TypeManager.double_type) ||
750 (target_type == TypeManager.decimal_type))
753 } else if (expr_type == TypeManager.short_type){
755 // From short to int, long, double, float, decimal
757 if ((target_type == TypeManager.int32_type) ||
758 (target_type == TypeManager.int64_type) ||
759 (target_type == TypeManager.double_type) ||
760 (target_type == TypeManager.float_type) ||
761 (target_type == TypeManager.decimal_type))
764 } else if (expr_type == TypeManager.ushort_type){
766 // From ushort to int, uint, long, ulong, double, float, decimal
768 if ((target_type == TypeManager.uint32_type) ||
769 (target_type == TypeManager.uint64_type) ||
770 (target_type == TypeManager.int32_type) ||
771 (target_type == TypeManager.int64_type) ||
772 (target_type == TypeManager.double_type) ||
773 (target_type == TypeManager.float_type) ||
774 (target_type == TypeManager.decimal_type))
777 } else if (expr_type == TypeManager.int32_type){
779 // From int to long, double, float, decimal
781 if ((target_type == TypeManager.int64_type) ||
782 (target_type == TypeManager.double_type) ||
783 (target_type == TypeManager.float_type) ||
784 (target_type == TypeManager.decimal_type))
787 } else if (expr_type == TypeManager.uint32_type){
789 // From uint to long, ulong, double, float, decimal
791 if ((target_type == TypeManager.int64_type) ||
792 (target_type == TypeManager.uint64_type) ||
793 (target_type == TypeManager.double_type) ||
794 (target_type == TypeManager.float_type) ||
795 (target_type == TypeManager.decimal_type))
798 } else if ((expr_type == TypeManager.uint64_type) ||
799 (expr_type == TypeManager.int64_type)) {
801 // From long/ulong to double, float, decimal
803 if ((target_type == TypeManager.double_type) ||
804 (target_type == TypeManager.float_type) ||
805 (target_type == TypeManager.decimal_type))
808 } else if (expr_type == TypeManager.char_type){
810 // From char to ushort, int, uint, ulong, long, float, double, decimal
812 if ((target_type == TypeManager.ushort_type) ||
813 (target_type == TypeManager.int32_type) ||
814 (target_type == TypeManager.uint32_type) ||
815 (target_type == TypeManager.uint64_type) ||
816 (target_type == TypeManager.int64_type) ||
817 (target_type == TypeManager.float_type) ||
818 (target_type == TypeManager.double_type) ||
819 (target_type == TypeManager.decimal_type))
822 } else if (expr_type == TypeManager.float_type){
826 if (target_type == TypeManager.double_type)
830 if (expr.eclass == ExprClass.MethodGroup){
831 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
832 MethodGroupExpr mg = expr as MethodGroupExpr;
834 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
839 if (ImplicitReferenceConversionExists (expr, target_type))
843 // Implicit Constant Expression Conversions
845 if (expr is IntConstant){
846 int value = ((IntConstant) expr).Value;
848 if (target_type == TypeManager.sbyte_type){
849 if (value >= SByte.MinValue && value <= SByte.MaxValue)
851 } else if (target_type == TypeManager.byte_type){
852 if (value >= 0 && value <= Byte.MaxValue)
854 } else if (target_type == TypeManager.short_type){
855 if (value >= Int16.MinValue && value <= Int16.MaxValue)
857 } else if (target_type == TypeManager.ushort_type){
858 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
860 } else if (target_type == TypeManager.uint32_type){
863 } else if (target_type == TypeManager.uint64_type){
865 // we can optimize this case: a positive int32
866 // always fits on a uint64. But we need an opcode
873 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
877 if (expr is LongConstant && target_type == TypeManager.uint64_type){
879 // Try the implicit constant expression conversion
880 // from long to ulong, instead of a nice routine,
883 long v = ((LongConstant) expr).Value;
889 // If `expr_type' implements `target_type' (which is an iface)
890 // see TryImplicitIntConversion
892 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
895 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
898 if (expr_type == TypeManager.anonymous_method_type){
899 if (!TypeManager.IsDelegateType (target_type))
902 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
903 return ame.ImplicitStandardConversionExists (target_type);
910 /// Finds "most encompassed type" according to the spec (13.4.2)
911 /// amongst the methods in the MethodGroupExpr
913 static Type FindMostEncompassedType (ArrayList types)
917 if (types.Count == 0)
920 if (types.Count == 1)
921 return (Type) types [0];
923 EmptyExpression expr = EmptyExpression.Grab ();
925 foreach (Type t in types) {
932 if (ImplicitStandardConversionExists (expr, best))
937 foreach (Type t in types) {
940 if (!ImplicitStandardConversionExists (expr, t)) {
946 EmptyExpression.Release (expr);
952 /// Finds "most encompassing type" according to the spec (13.4.2)
953 /// amongst the types in the given set
955 static Type FindMostEncompassingType (ArrayList types)
959 if (types.Count == 0)
962 if (types.Count == 1)
963 return (Type) types [0];
965 EmptyExpression expr = EmptyExpression.Grab ();
967 foreach (Type t in types) {
974 if (ImplicitStandardConversionExists (expr, t))
978 foreach (Type t in types) {
982 if (!ImplicitStandardConversionExists (expr, best)) {
988 EmptyExpression.Release (expr);
994 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
995 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
996 /// for explicit and implicit conversion operators.
998 static public Type FindMostSpecificSource (IList list,
999 Expression source, bool apply_explicit_conv_rules)
1001 ArrayList src_types_set = new ArrayList ();
1004 // If any operator converts from S then Sx = S
1006 Type source_type = source.Type;
1007 foreach (MethodBase mb in list){
1008 ParameterData pd = TypeManager.GetParameterData (mb);
1009 Type param_type = pd.ParameterType (0);
1011 if (param_type == source_type)
1014 src_types_set.Add (param_type);
1018 // Explicit Conv rules
1020 if (apply_explicit_conv_rules) {
1021 ArrayList candidate_set = new ArrayList ();
1023 foreach (Type param_type in src_types_set){
1024 if (ImplicitStandardConversionExists (source, param_type))
1025 candidate_set.Add (param_type);
1028 if (candidate_set.Count != 0)
1029 return FindMostEncompassedType (candidate_set);
1035 if (apply_explicit_conv_rules)
1036 return FindMostEncompassingType (src_types_set);
1038 return FindMostEncompassedType (src_types_set);
1042 /// Finds the most specific target Tx according to section 13.4.4
1044 static public Type FindMostSpecificTarget (IList list,
1045 Type target, bool apply_explicit_conv_rules)
1047 ArrayList tgt_types_set = new ArrayList ();
1050 // If any operator converts to T then Tx = T
1052 foreach (MethodInfo mi in list){
1053 Type ret_type = mi.ReturnType;
1054 if (ret_type == target)
1057 tgt_types_set.Add (ret_type);
1061 // Explicit conv rules
1063 if (apply_explicit_conv_rules) {
1064 ArrayList candidate_set = new ArrayList ();
1066 EmptyExpression expr = EmptyExpression.Grab ();
1068 foreach (Type ret_type in tgt_types_set){
1069 expr.SetType (ret_type);
1071 if (ImplicitStandardConversionExists (expr, target))
1072 candidate_set.Add (ret_type);
1075 EmptyExpression.Release (expr);
1077 if (candidate_set.Count != 0)
1078 return FindMostEncompassingType (candidate_set);
1082 // Okay, final case !
1084 if (apply_explicit_conv_rules)
1085 return FindMostEncompassedType (tgt_types_set);
1087 return FindMostEncompassingType (tgt_types_set);
1091 /// User-defined Implicit conversions
1093 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1094 Type target, Location loc)
1096 return UserDefinedConversion (ec, source, target, loc, false);
1100 /// User-defined Explicit conversions
1102 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1103 Type target, Location loc)
1105 return UserDefinedConversion (ec, source, target, loc, true);
1108 static void AddConversionOperators (ArrayList list,
1109 Expression source, Type target_type,
1110 bool look_for_explicit,
1116 Type source_type = source.Type;
1117 EmptyExpression expr = EmptyExpression.Grab ();
1118 foreach (MethodInfo m in mg.Methods) {
1119 ParameterData pd = TypeManager.GetParameterData (m);
1120 Type return_type = m.ReturnType;
1121 Type arg_type = pd.ParameterType (0);
1123 if (source_type != arg_type) {
1124 if (!ImplicitStandardConversionExists (source, arg_type)) {
1125 if (!look_for_explicit)
1127 expr.SetType (arg_type);
1128 if (!ImplicitStandardConversionExists (expr, source_type))
1133 if (target_type != return_type) {
1134 expr.SetType (return_type);
1135 if (!ImplicitStandardConversionExists (expr, target_type)) {
1136 if (!look_for_explicit)
1138 expr.SetType (target_type);
1139 if (!ImplicitStandardConversionExists (expr, return_type))
1147 EmptyExpression.Release (expr);
1151 /// Compute the user-defined conversion operator from source_type to target_type.
1152 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1154 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1156 ArrayList ops = new ArrayList (4);
1158 Type source_type = source.Type;
1160 if (source_type != TypeManager.decimal_type) {
1161 AddConversionOperators (ops, source, target_type, look_for_explicit,
1162 Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1163 if (look_for_explicit) {
1164 AddConversionOperators (ops, source, target_type, look_for_explicit,
1165 Expression.MethodLookup (
1166 container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1170 if (target_type != TypeManager.decimal_type) {
1171 AddConversionOperators (ops, source, target_type, look_for_explicit,
1172 Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1173 if (look_for_explicit) {
1174 AddConversionOperators (ops, source, target_type, look_for_explicit,
1175 Expression.MethodLookup (
1176 container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1183 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1184 if (most_specific_source == null)
1187 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1188 if (most_specific_target == null)
1191 MethodInfo method = null;
1193 foreach (MethodInfo m in ops) {
1194 if (m.ReturnType != most_specific_target)
1196 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1198 // Ambiguous: more than one conversion operator satisfies the signature.
1207 static DoubleHash explicit_conv = new DoubleHash (100);
1208 static DoubleHash implicit_conv = new DoubleHash (100);
1211 /// User-defined conversions
1213 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1214 Type target, Location loc,
1215 bool look_for_explicit)
1217 Type source_type = source.Type;
1218 MethodInfo method = null;
1221 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1223 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1224 method = (MethodInfo) o;
1226 method = GetConversionOperator (null, source, target, look_for_explicit);
1227 if (!(source is Constant))
1228 hash.Insert (source_type, target, method);
1234 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1237 // This will do the conversion to the best match that we
1238 // found. Now we need to perform an implict standard conversion
1239 // if the best match was not the type that we were requested
1242 if (look_for_explicit)
1243 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1245 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1251 e = new UserCast (method, source, loc);
1252 if (e.Type != target){
1253 if (!look_for_explicit)
1254 e = ImplicitConversionStandard (ec, e, target, loc);
1256 e = ExplicitConversionStandard (ec, e, target, loc);
1263 /// Converts implicitly the resolved expression `expr' into the
1264 /// `target_type'. It returns a new expression that can be used
1265 /// in a context that expects a `target_type'.
1267 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1268 Type target_type, Location loc)
1272 if (target_type == null)
1273 throw new Exception ("Target type is null");
1275 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1279 e = ImplicitUserConversion (ec, expr, target_type, loc);
1288 /// Attempts to apply the `Standard Implicit
1289 /// Conversion' rules to the expression `expr' into
1290 /// the `target_type'. It returns a new expression
1291 /// that can be used in a context that expects a
1294 /// This is different from `ImplicitConversion' in that the
1295 /// user defined implicit conversions are excluded.
1297 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1298 Type target_type, Location loc)
1300 Type expr_type = expr.Type;
1303 // TODO: refactor to be a part of constant infrastructure
1304 if (expr is NullLiteral) {
1305 if (TypeManager.IsNullableType (target_type))
1306 return new Nullable.NullableLiteral (target_type, loc);
1309 if (TypeManager.IsNullableType (target_type)) {
1310 Type target = TypeManager.GetTypeArguments (target_type) [0];
1312 if (TypeManager.IsNullableType (expr.Type)) {
1313 e = new Nullable.LiftedConversion (
1314 expr, target_type, false, false, loc).Resolve (ec);
1318 e = ImplicitConversion (ec, expr, target, loc);
1320 return Nullable.Wrap.Create (e, ec);
1324 if (expr.eclass == ExprClass.MethodGroup){
1325 if (!TypeManager.IsDelegateType (target_type)){
1330 // Only allow anonymous method conversions on post ISO_1
1332 if (RootContext.Version != LanguageVersion.ISO_1){
1333 MethodGroupExpr mg = expr as MethodGroupExpr;
1335 return ImplicitDelegateCreation.Create (
1336 ec, mg, target_type, loc);
1340 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1344 // Attempt to do the implicit constant expression conversions
1346 Constant c = expr as Constant;
1349 // If `target_type' is an interface and the type of `ic' implements the interface
1350 // e.g. target_type is IComparable, IConvertible, IFormattable
1352 if (c.Type == TypeManager.int32_type && target_type.IsInterface && target_type.IsAssignableFrom (c.Type))
1353 return new BoxedCast (c, target_type);
1356 c = c.ConvertImplicitly (target_type);
1358 Console.WriteLine ("Conversion error happened in line {0}", loc);
1365 e = ImplicitNumericConversion (expr, target_type);
1369 e = ImplicitReferenceConversion (expr, target_type);
1373 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1374 IntLiteral i = (IntLiteral) expr;
1377 return new EnumConstant ((Constant) expr, target_type);
1381 if (expr_type.IsPointer){
1382 if (target_type == TypeManager.void_ptr_type)
1383 return EmptyCast.Create (expr, target_type);
1386 // yep, comparing pointer types cant be done with
1387 // t1 == t2, we have to compare their element types.
1389 if (target_type.IsPointer){
1390 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1397 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1398 return EmptyCast.Create (NullPointer.Null, target_type);
1401 if (expr_type == TypeManager.anonymous_method_type){
1402 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1404 AnonymousMethod am = ame.Compatible (ec, target_type);
1406 return am.Resolve (ec);
1413 /// Attempts to implicitly convert `source' into `target_type', using
1414 /// ImplicitConversion. If there is no implicit conversion, then
1415 /// an error is signaled
1417 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1418 Type target_type, Location loc)
1420 Expression e = ImplicitConversion (ec, source, target_type, loc);
1424 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1429 /// Performs the explicit numeric conversions
1431 /// There are a few conversions that are not part of the C# standard,
1432 /// they were interim hacks in the C# compiler that were supposed to
1433 /// become explicit operators in the UIntPtr class and IntPtr class,
1434 /// but for historical reasons it did not happen, so the C# compiler
1435 /// ended up with these special hacks.
1437 /// See bug 59800 for details.
1439 /// The conversion are:
1449 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1451 Type expr_type = expr.Type;
1452 Type real_target_type = target_type;
1454 if (expr_type == TypeManager.sbyte_type){
1456 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1458 if (real_target_type == TypeManager.byte_type)
1459 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1460 if (real_target_type == TypeManager.ushort_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1462 if (real_target_type == TypeManager.uint32_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1464 if (real_target_type == TypeManager.uint64_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1466 if (real_target_type == TypeManager.char_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1469 // One of the built-in conversions that belonged in the class library
1470 if (real_target_type == TypeManager.uintptr_type){
1471 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1473 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1475 } else if (expr_type == TypeManager.byte_type){
1477 // From byte to sbyte and char
1479 if (real_target_type == TypeManager.sbyte_type)
1480 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1481 if (real_target_type == TypeManager.char_type)
1482 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1483 } else if (expr_type == TypeManager.short_type){
1485 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1487 if (real_target_type == TypeManager.sbyte_type)
1488 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1489 if (real_target_type == TypeManager.byte_type)
1490 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1491 if (real_target_type == TypeManager.ushort_type)
1492 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1493 if (real_target_type == TypeManager.uint32_type)
1494 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1495 if (real_target_type == TypeManager.uint64_type)
1496 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1497 if (real_target_type == TypeManager.char_type)
1498 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1500 // One of the built-in conversions that belonged in the class library
1501 if (real_target_type == TypeManager.uintptr_type){
1502 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1504 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1506 } else if (expr_type == TypeManager.ushort_type){
1508 // From ushort to sbyte, byte, short, char
1510 if (real_target_type == TypeManager.sbyte_type)
1511 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1512 if (real_target_type == TypeManager.byte_type)
1513 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1514 if (real_target_type == TypeManager.short_type)
1515 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1516 if (real_target_type == TypeManager.char_type)
1517 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1518 } else if (expr_type == TypeManager.int32_type){
1520 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1522 if (real_target_type == TypeManager.sbyte_type)
1523 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1524 if (real_target_type == TypeManager.byte_type)
1525 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1526 if (real_target_type == TypeManager.short_type)
1527 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1528 if (real_target_type == TypeManager.ushort_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1530 if (real_target_type == TypeManager.uint32_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1532 if (real_target_type == TypeManager.uint64_type)
1533 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1534 if (real_target_type == TypeManager.char_type)
1535 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1537 // One of the built-in conversions that belonged in the class library
1538 if (real_target_type == TypeManager.uintptr_type){
1539 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1541 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1543 } else if (expr_type == TypeManager.uint32_type){
1545 // From uint to sbyte, byte, short, ushort, int, char
1547 if (real_target_type == TypeManager.sbyte_type)
1548 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1549 if (real_target_type == TypeManager.byte_type)
1550 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1551 if (real_target_type == TypeManager.short_type)
1552 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1553 if (real_target_type == TypeManager.ushort_type)
1554 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1555 if (real_target_type == TypeManager.int32_type)
1556 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1557 if (real_target_type == TypeManager.char_type)
1558 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1559 } else if (expr_type == TypeManager.int64_type){
1561 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1563 if (real_target_type == TypeManager.sbyte_type)
1564 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1565 if (real_target_type == TypeManager.byte_type)
1566 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1567 if (real_target_type == TypeManager.short_type)
1568 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1569 if (real_target_type == TypeManager.ushort_type)
1570 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1571 if (real_target_type == TypeManager.int32_type)
1572 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1573 if (real_target_type == TypeManager.uint32_type)
1574 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1575 if (real_target_type == TypeManager.uint64_type)
1576 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1577 if (real_target_type == TypeManager.char_type)
1578 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1579 } else if (expr_type == TypeManager.uint64_type){
1581 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1583 if (real_target_type == TypeManager.sbyte_type)
1584 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1585 if (real_target_type == TypeManager.byte_type)
1586 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1587 if (real_target_type == TypeManager.short_type)
1588 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1589 if (real_target_type == TypeManager.ushort_type)
1590 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1591 if (real_target_type == TypeManager.int32_type)
1592 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1593 if (real_target_type == TypeManager.uint32_type)
1594 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1595 if (real_target_type == TypeManager.int64_type)
1596 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1597 if (real_target_type == TypeManager.char_type)
1598 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1600 // One of the built-in conversions that belonged in the class library
1601 if (real_target_type == TypeManager.intptr_type){
1602 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1603 TypeManager.intptr_type, true);
1605 } else if (expr_type == TypeManager.char_type){
1607 // From char to sbyte, byte, short
1609 if (real_target_type == TypeManager.sbyte_type)
1610 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1611 if (real_target_type == TypeManager.byte_type)
1612 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1613 if (real_target_type == TypeManager.short_type)
1614 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1615 } else if (expr_type == TypeManager.float_type){
1617 // From float to sbyte, byte, short,
1618 // ushort, int, uint, long, ulong, char
1621 if (real_target_type == TypeManager.sbyte_type)
1622 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1623 if (real_target_type == TypeManager.byte_type)
1624 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1625 if (real_target_type == TypeManager.short_type)
1626 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1627 if (real_target_type == TypeManager.ushort_type)
1628 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1629 if (real_target_type == TypeManager.int32_type)
1630 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1631 if (real_target_type == TypeManager.uint32_type)
1632 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1633 if (real_target_type == TypeManager.int64_type)
1634 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1635 if (real_target_type == TypeManager.uint64_type)
1636 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1637 if (real_target_type == TypeManager.char_type)
1638 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1639 if (real_target_type == TypeManager.decimal_type)
1640 return new CastToDecimal (expr, true);
1641 } else if (expr_type == TypeManager.double_type){
1643 // From double to sbyte, byte, short,
1644 // ushort, int, uint, long, ulong,
1645 // char, float or decimal
1647 if (real_target_type == TypeManager.sbyte_type)
1648 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1649 if (real_target_type == TypeManager.byte_type)
1650 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1651 if (real_target_type == TypeManager.short_type)
1652 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1653 if (real_target_type == TypeManager.ushort_type)
1654 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1655 if (real_target_type == TypeManager.int32_type)
1656 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1657 if (real_target_type == TypeManager.uint32_type)
1658 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1659 if (real_target_type == TypeManager.int64_type)
1660 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1661 if (real_target_type == TypeManager.uint64_type)
1662 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1663 if (real_target_type == TypeManager.char_type)
1664 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1665 if (real_target_type == TypeManager.float_type)
1666 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1667 if (real_target_type == TypeManager.decimal_type)
1668 return new CastToDecimal (expr, true);
1669 } else if (expr_type == TypeManager.uintptr_type){
1671 // Various built-in conversions that belonged in the class library
1673 // from uintptr to sbyte, short, int32
1675 if (real_target_type == TypeManager.sbyte_type){
1676 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1677 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1679 if (real_target_type == TypeManager.short_type){
1680 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1681 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1683 if (real_target_type == TypeManager.int32_type){
1684 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1685 TypeManager.int32_type);
1687 } else if (expr_type == TypeManager.intptr_type){
1688 if (real_target_type == TypeManager.uint64_type){
1689 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1690 TypeManager.uint64_type);
1692 } else if (expr_type == TypeManager.decimal_type) {
1693 return new CastFromDecimal (expr, target_type).Resolve ();
1699 /// Returns whether an explicit reference conversion can be performed
1700 /// from source_type to target_type
1702 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1704 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1705 bool target_is_value_type = target_type.IsValueType;
1707 if (source_type == target_type)
1711 // From generic parameter to any type
1713 if (TypeManager.IsGenericParameter (source_type))
1714 return ExplicitTypeParameterConversionExists (source_type, target_type);
1717 // From object to a generic parameter
1719 if (source_type == TypeManager.object_type && target_is_type_param)
1723 // From object to any reference type
1725 if (source_type == TypeManager.object_type && !target_is_value_type)
1729 // From any class S to any class-type T, provided S is a base class of T
1731 if (TypeManager.IsSubclassOf (target_type, source_type))
1735 // From any interface type S to any interface T provided S is not derived from T
1737 if (source_type.IsInterface && target_type.IsInterface){
1738 if (!TypeManager.IsSubclassOf (target_type, source_type))
1743 // From any class type S to any interface T, provided S is not sealed
1744 // and provided S does not implement T.
1746 if (target_type.IsInterface && !source_type.IsSealed &&
1747 !TypeManager.ImplementsInterface (source_type, target_type))
1751 // From any interface-type S to to any class type T, provided T is not
1752 // sealed, or provided T implements S.
1754 if (source_type.IsInterface &&
1755 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1759 if (source_type.IsInterface && IList_To_Array (source_type, target_type))
1762 // From an array type S with an element type Se to an array type T with an
1763 // element type Te provided all the following are true:
1764 // * S and T differe only in element type, in other words, S and T
1765 // have the same number of dimensions.
1766 // * Both Se and Te are reference types
1767 // * An explicit referenc conversions exist from Se to Te
1769 if (source_type.IsArray && target_type.IsArray) {
1770 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1772 Type source_element_type = TypeManager.GetElementType (source_type);
1773 Type target_element_type = TypeManager.GetElementType (target_type);
1775 if (TypeManager.IsGenericParameter (source_element_type) ||
1776 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1777 if (ExplicitReferenceConversionExists (source_element_type,
1778 target_element_type))
1784 // From System.Array to any array-type
1785 if (source_type == TypeManager.array_type &&
1786 target_type.IsArray){
1791 // From System delegate to any delegate-type
1793 if (source_type == TypeManager.delegate_type &&
1794 TypeManager.IsDelegateType (target_type))
1798 // From ICloneable to Array or Delegate types
1800 if (source_type == TypeManager.icloneable_type &&
1801 (target_type == TypeManager.array_type ||
1802 target_type == TypeManager.delegate_type))
1809 /// Implements Explicit Reference conversions
1811 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1813 Type source_type = source.Type;
1814 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1815 bool target_is_value_type = target_type.IsValueType;
1818 // From object to a generic parameter
1820 if (source_type == TypeManager.object_type && target_is_type_param)
1821 return new UnboxCast (source, target_type);
1824 // Explicit type parameter conversion.
1827 if (TypeManager.IsGenericParameter (source_type))
1828 return ExplicitTypeParameterConversion (source, target_type);
1831 // From object to any reference type
1833 if (source_type == TypeManager.object_type && !target_is_value_type)
1834 return new ClassCast (source, target_type);
1837 // Unboxing conversion.
1839 if (((source_type == TypeManager.enum_type &&
1840 !(source is EmptyCast)) ||
1841 source_type == TypeManager.value_type) && target_is_value_type)
1842 return new UnboxCast (source, target_type);
1845 // From any class S to any class-type T, provided S is a base class of T
1847 if (TypeManager.IsSubclassOf (target_type, source_type))
1848 return new ClassCast (source, target_type);
1851 // From any interface type S to any interface T provided S is not derived from T
1853 if (source_type.IsInterface && target_type.IsInterface){
1854 if (TypeManager.ImplementsInterface (source_type, target_type))
1857 return new ClassCast (source, target_type);
1861 // From any class type S to any interface T, provides S is not sealed
1862 // and provided S does not implement T.
1864 if (target_type.IsInterface && !source_type.IsSealed) {
1865 if (TypeManager.ImplementsInterface (source_type, target_type))
1868 return new ClassCast (source, target_type);
1873 // From any interface-type S to to any class type T, provided T is not
1874 // sealed, or provided T implements S.
1876 if (source_type.IsInterface) {
1877 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1878 if (target_type.IsClass)
1879 return new ClassCast (source, target_type);
1881 return new UnboxCast (source, target_type);
1885 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1886 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1888 if (IList_To_Array (source_type, target_type))
1889 return new ClassCast (source, target_type);
1894 // From an array type S with an element type Se to an array type T with an
1895 // element type Te provided all the following are true:
1896 // * S and T differe only in element type, in other words, S and T
1897 // have the same number of dimensions.
1898 // * Both Se and Te are reference types
1899 // * An explicit referenc conversions exist from Se to Te
1901 if (source_type.IsArray && target_type.IsArray) {
1902 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1904 Type source_element_type = TypeManager.GetElementType (source_type);
1905 Type target_element_type = TypeManager.GetElementType (target_type);
1907 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1908 if (ExplicitReferenceConversionExists (source_element_type,
1909 target_element_type))
1910 return new ClassCast (source, target_type);
1915 // From System.Array to any array-type
1916 if (source_type == TypeManager.array_type &&
1917 target_type.IsArray) {
1918 return new ClassCast (source, target_type);
1922 // From System delegate to any delegate-type
1924 if (source_type == TypeManager.delegate_type &&
1925 TypeManager.IsDelegateType (target_type))
1926 return new ClassCast (source, target_type);
1929 // From ICloneable to Array or Delegate types
1931 if (source_type == TypeManager.icloneable_type &&
1932 (target_type == TypeManager.array_type ||
1933 target_type == TypeManager.delegate_type))
1934 return new ClassCast (source, target_type);
1940 /// Performs an explicit conversion of the expression `expr' whose
1941 /// type is expr.Type to `target_type'.
1943 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1944 Type target_type, Location loc)
1946 Type expr_type = expr.Type;
1948 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1949 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1954 // Unboxing conversions; only object types can be convertible to enum
1956 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1957 return new UnboxCast (expr, target_type);
1959 if (TypeManager.IsEnumType (expr_type)) {
1960 if (expr is EnumConstant)
1961 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1963 return ExplicitConversionCore (ec, EmptyCast.Create (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1966 if (TypeManager.IsEnumType (target_type)){
1967 if (expr_type == TypeManager.enum_type)
1968 return new UnboxCast (expr, target_type);
1970 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1972 return EmptyCast.Create (ce, target_type);
1975 ne = ExplicitNumericConversion (expr, target_type);
1980 // Skip the ExplicitReferenceConversion because we can not convert
1981 // from Null to a ValueType, and ExplicitReference wont check against
1982 // null literal explicitly
1984 if (expr_type != TypeManager.null_type){
1985 ne = ExplicitReferenceConversion (expr, target_type);
1991 ne = ExplicitUnsafe (expr, target_type);
1996 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2003 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
2005 Type expr_type = expr.Type;
2007 if (target_type.IsPointer){
2008 if (expr_type.IsPointer)
2009 return EmptyCast.Create (expr, target_type);
2011 if (expr_type == TypeManager.sbyte_type ||
2012 expr_type == TypeManager.short_type ||
2013 expr_type == TypeManager.int32_type ||
2014 expr_type == TypeManager.int64_type)
2015 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
2017 if (expr_type == TypeManager.ushort_type ||
2018 expr_type == TypeManager.uint32_type ||
2019 expr_type == TypeManager.uint64_type ||
2020 expr_type == TypeManager.byte_type)
2021 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2024 if (expr_type.IsPointer){
2025 if (target_type == TypeManager.sbyte_type)
2026 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2027 else if (target_type == TypeManager.byte_type)
2028 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2029 else if (target_type == TypeManager.short_type)
2030 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2031 else if (target_type == TypeManager.ushort_type)
2032 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2033 else if (target_type == TypeManager.int32_type)
2034 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2035 else if (target_type == TypeManager.uint32_type)
2036 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2037 else if (target_type == TypeManager.uint64_type)
2038 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2039 else if (target_type == TypeManager.int64_type){
2040 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2047 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2049 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2050 Type target_type, Location l)
2052 int errors = Report.Errors;
2053 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2054 if (Report.Errors > errors)
2060 ne = ExplicitNumericConversion (expr, target_type);
2064 ne = ExplicitReferenceConversion (expr, target_type);
2068 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2069 return EmptyCast.Create (expr, target_type);
2071 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
2076 /// Performs an explicit conversion of the expression `expr' whose
2077 /// type is expr.Type to `target_type'.
2079 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2080 Type target_type, Location loc)
2084 Type expr_type = expr.Type;
2085 if (TypeManager.IsNullableType (target_type)) {
2086 if (TypeManager.IsNullableType (expr_type)) {
2087 e = new Nullable.LiftedConversion (
2088 expr, target_type, false, true, loc).Resolve (ec);
2091 } else if (expr_type == TypeManager.object_type) {
2092 return new UnboxCast (expr, target_type);
2094 Type target = TypeManager.GetTypeArguments (target_type) [0];
2096 e = ExplicitConversionCore (ec, expr, target, loc);
2098 return Nullable.Wrap.Create (e, ec);
2100 } else if (TypeManager.IsNullableType (expr_type)) {
2101 Expression source = Nullable.Unwrap.Create (expr, ec);
2102 if (source != null) {
2103 e = ExplicitConversionCore (ec, source, target_type, loc);
2109 e = ExplicitConversionCore (ec, expr, target_type, loc);
2113 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);