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;
408 use_class_cast = false;
411 // notice that it is possible to write "ValueType v = 1", the ValueType here
412 // is an abstract class, and not really a value type, so we apply the same rules.
414 if (target_type == TypeManager.object_type) {
416 // A pointer type cannot be converted to object
418 if (expr_type.IsPointer)
421 return TypeManager.IsValueType (expr_type);
422 } else if (target_type == TypeManager.value_type) {
423 return TypeManager.IsValueType (expr_type);
424 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
426 // Special case: enumeration to System.Enum.
427 // System.Enum is not a value type, it is a class, so we need
428 // a boxing conversion
430 if (expr_type.IsEnum || TypeManager.IsGenericParameter (expr_type))
436 // This code is kind of mirrored inside ImplicitStandardConversionExists
437 // with the small distinction that we only probe there
439 // Always ensure that the code here and there is in sync
441 // from any class-type S to any interface-type T.
442 if (target_type.IsInterface) {
443 if (target_type != TypeManager.iconvertible_type &&
444 expr_type.IsValueType && (expr is Constant) &&
445 !(expr is IntLiteral || expr is BoolLiteral ||
446 expr is FloatLiteral || expr is DoubleLiteral ||
447 expr is LongLiteral || expr is CharLiteral ||
448 expr is StringLiteral || expr is DecimalLiteral ||
449 expr is UIntLiteral || expr is ULongLiteral)) {
453 if (TypeManager.ImplementsInterface (expr_type, target_type))
454 return TypeManager.IsGenericParameter (expr_type) ||
455 TypeManager.IsValueType (expr_type);
458 if (TypeManager.IsGenericParameter (expr_type))
459 return ImplicitTypeParameterBoxingConversion (
460 expr_type, target_type, out use_class_cast);
466 // Tests whether an implicit reference conversion exists between expr_type
469 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
471 if (target_type.IsValueType)
474 Type expr_type = expr.Type;
476 // from the null type to any reference-type.
477 if (expr_type == TypeManager.null_type){
478 if (target_type.IsPointer)
481 if (!target_type.IsValueType)
485 if (TypeManager.IsGenericParameter (expr_type))
486 return ImplicitTypeParameterConversion (expr, target_type) != null;
489 if (ImplicitReferenceConversionCore (expr, target_type) ||
490 ImplicitBoxingConversionExists (expr, target_type, out use_class_cast))
497 /// Implicit Numeric Conversions.
499 /// expr is the expression to convert, returns a new expression of type
500 /// target_type or null if an implicit conversion is not possible.
502 static public Expression ImplicitNumericConversion (Expression expr,
505 Type expr_type = expr.Type;
506 Type real_target_type = target_type;
508 if (expr_type == TypeManager.sbyte_type){
510 // From sbyte to short, int, long, float, double, decimal
512 if (real_target_type == TypeManager.int32_type)
513 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
514 if (real_target_type == TypeManager.int64_type)
515 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
516 if (real_target_type == TypeManager.double_type)
517 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
518 if (real_target_type == TypeManager.float_type)
519 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
520 if (real_target_type == TypeManager.short_type)
521 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
522 if (real_target_type == TypeManager.decimal_type)
523 return new CastToDecimal (expr);
524 } else if (expr_type == TypeManager.byte_type){
526 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
528 if ((real_target_type == TypeManager.short_type) ||
529 (real_target_type == TypeManager.ushort_type) ||
530 (real_target_type == TypeManager.int32_type) ||
531 (real_target_type == TypeManager.uint32_type))
532 return EmptyCast.Create (expr, target_type);
534 if (real_target_type == TypeManager.uint64_type)
535 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
536 if (real_target_type == TypeManager.int64_type)
537 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
538 if (real_target_type == TypeManager.float_type)
539 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
540 if (real_target_type == TypeManager.double_type)
541 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
542 if (real_target_type == TypeManager.decimal_type)
543 return new CastToDecimal (expr);
545 } else if (expr_type == TypeManager.short_type){
547 // From short to int, long, float, double, decimal
549 if (real_target_type == TypeManager.int32_type)
550 return EmptyCast.Create (expr, target_type);
551 if (real_target_type == TypeManager.int64_type)
552 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
553 if (real_target_type == TypeManager.double_type)
554 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
555 if (real_target_type == TypeManager.float_type)
556 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
557 if (real_target_type == TypeManager.decimal_type)
558 return new CastToDecimal (expr);
560 } else if (expr_type == TypeManager.ushort_type){
562 // From ushort to int, uint, long, ulong, float, double, decimal
564 if (real_target_type == TypeManager.uint32_type)
565 return EmptyCast.Create (expr, target_type);
567 if (real_target_type == TypeManager.uint64_type)
568 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
569 if (real_target_type == TypeManager.int32_type)
570 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
571 if (real_target_type == TypeManager.int64_type)
572 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
573 if (real_target_type == TypeManager.double_type)
574 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
575 if (real_target_type == TypeManager.float_type)
576 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
577 if (real_target_type == TypeManager.decimal_type)
578 return new CastToDecimal (expr);
579 } else if (expr_type == TypeManager.int32_type){
581 // From int to long, float, double, decimal
583 if (real_target_type == TypeManager.int64_type)
584 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
585 if (real_target_type == TypeManager.double_type)
586 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
587 if (real_target_type == TypeManager.float_type)
588 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
589 if (real_target_type == TypeManager.decimal_type)
590 return new CastToDecimal (expr);
591 } else if (expr_type == TypeManager.uint32_type){
593 // From uint to long, ulong, float, double, decimal
595 if (real_target_type == TypeManager.int64_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
597 if (real_target_type == TypeManager.uint64_type)
598 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
599 if (real_target_type == TypeManager.double_type)
600 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
602 if (real_target_type == TypeManager.float_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
605 if (real_target_type == TypeManager.decimal_type)
606 return new CastToDecimal (expr);
607 } else if (expr_type == TypeManager.int64_type){
609 // From long/ulong to float, double
611 if (real_target_type == TypeManager.double_type)
612 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
613 if (real_target_type == TypeManager.float_type)
614 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
615 if (real_target_type == TypeManager.decimal_type)
616 return new CastToDecimal (expr);
617 } else if (expr_type == TypeManager.uint64_type){
619 // From ulong to float, double
621 if (real_target_type == TypeManager.double_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
624 if (real_target_type == TypeManager.float_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
627 if (real_target_type == TypeManager.decimal_type)
628 return new CastToDecimal (expr);
629 } else if (expr_type == TypeManager.char_type){
631 // From char to ushort, int, uint, long, ulong, float, double, decimal
633 if ((real_target_type == TypeManager.ushort_type) ||
634 (real_target_type == TypeManager.int32_type) ||
635 (real_target_type == TypeManager.uint32_type))
636 return EmptyCast.Create (expr, target_type);
637 if (real_target_type == TypeManager.uint64_type)
638 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
639 if (real_target_type == TypeManager.int64_type)
640 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
641 if (real_target_type == TypeManager.float_type)
642 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
643 if (real_target_type == TypeManager.double_type)
644 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
645 if (real_target_type == TypeManager.decimal_type)
646 return new CastToDecimal (expr);
647 } else if (expr_type == TypeManager.float_type){
651 if (real_target_type == TypeManager.double_type)
652 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
659 /// Same as ImplicitStandardConversionExists except that it also looks at
660 /// implicit user defined conversions - needed for overload resolution
662 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
665 if (expr is NullLiteral) {
666 if (TypeManager.IsGenericParameter (target_type))
667 return TypeParameter_to_Null (target_type);
669 if (TypeManager.IsNullableType (target_type))
673 if (ImplicitStandardConversionExists (expr, target_type))
676 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
679 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
681 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
685 /// Determines if a standard implicit conversion exists from
686 /// expr_type to target_type
688 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
690 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
692 Type expr_type = expr.Type;
694 if (TypeManager.IsNullableType (target_type)) {
695 // if implicit standard conversion S -> T exists, S -> T? and S? -> T? also exists
696 target_type = TypeManager.GetTypeArguments (target_type) [0];
699 if (TypeManager.IsNullableType (expr_type)) {
700 EmptyExpression new_expr = EmptyExpression.Grab ();
701 new_expr.SetType (TypeManager.GetTypeArguments (expr_type) [0]);
702 bool retval = ImplicitStandardConversionExists (new_expr, target_type);
703 EmptyExpression.Release (new_expr);
708 if (expr_type == TypeManager.void_type)
711 //Console.WriteLine ("Expr is {0}", expr);
712 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
713 if (TypeManager.IsEqual (expr_type, target_type))
717 // First numeric conversions
719 if (expr_type == TypeManager.sbyte_type){
721 // From sbyte to short, int, long, float, double, decimal
723 if ((target_type == TypeManager.int32_type) ||
724 (target_type == TypeManager.int64_type) ||
725 (target_type == TypeManager.double_type) ||
726 (target_type == TypeManager.float_type) ||
727 (target_type == TypeManager.short_type) ||
728 (target_type == TypeManager.decimal_type))
731 } else if (expr_type == TypeManager.byte_type){
733 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
735 if ((target_type == TypeManager.short_type) ||
736 (target_type == TypeManager.ushort_type) ||
737 (target_type == TypeManager.int32_type) ||
738 (target_type == TypeManager.uint32_type) ||
739 (target_type == TypeManager.uint64_type) ||
740 (target_type == TypeManager.int64_type) ||
741 (target_type == TypeManager.float_type) ||
742 (target_type == TypeManager.double_type) ||
743 (target_type == TypeManager.decimal_type))
746 } else if (expr_type == TypeManager.short_type){
748 // From short to int, long, double, float, decimal
750 if ((target_type == TypeManager.int32_type) ||
751 (target_type == TypeManager.int64_type) ||
752 (target_type == TypeManager.double_type) ||
753 (target_type == TypeManager.float_type) ||
754 (target_type == TypeManager.decimal_type))
757 } else if (expr_type == TypeManager.ushort_type){
759 // From ushort to int, uint, long, ulong, double, float, decimal
761 if ((target_type == TypeManager.uint32_type) ||
762 (target_type == TypeManager.uint64_type) ||
763 (target_type == TypeManager.int32_type) ||
764 (target_type == TypeManager.int64_type) ||
765 (target_type == TypeManager.double_type) ||
766 (target_type == TypeManager.float_type) ||
767 (target_type == TypeManager.decimal_type))
770 } else if (expr_type == TypeManager.int32_type){
772 // From int to long, double, float, decimal
774 if ((target_type == TypeManager.int64_type) ||
775 (target_type == TypeManager.double_type) ||
776 (target_type == TypeManager.float_type) ||
777 (target_type == TypeManager.decimal_type))
780 } else if (expr_type == TypeManager.uint32_type){
782 // From uint to long, ulong, double, float, decimal
784 if ((target_type == TypeManager.int64_type) ||
785 (target_type == TypeManager.uint64_type) ||
786 (target_type == TypeManager.double_type) ||
787 (target_type == TypeManager.float_type) ||
788 (target_type == TypeManager.decimal_type))
791 } else if ((expr_type == TypeManager.uint64_type) ||
792 (expr_type == TypeManager.int64_type)) {
794 // From long/ulong to double, float, decimal
796 if ((target_type == TypeManager.double_type) ||
797 (target_type == TypeManager.float_type) ||
798 (target_type == TypeManager.decimal_type))
801 } else if (expr_type == TypeManager.char_type){
803 // From char to ushort, int, uint, ulong, long, float, double, decimal
805 if ((target_type == TypeManager.ushort_type) ||
806 (target_type == TypeManager.int32_type) ||
807 (target_type == TypeManager.uint32_type) ||
808 (target_type == TypeManager.uint64_type) ||
809 (target_type == TypeManager.int64_type) ||
810 (target_type == TypeManager.float_type) ||
811 (target_type == TypeManager.double_type) ||
812 (target_type == TypeManager.decimal_type))
815 } else if (expr_type == TypeManager.float_type){
819 if (target_type == TypeManager.double_type)
823 if (expr.eclass == ExprClass.MethodGroup){
824 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
825 MethodGroupExpr mg = expr as MethodGroupExpr;
827 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
832 if (ImplicitReferenceConversionExists (expr, target_type))
836 // Implicit Constant Expression Conversions
838 if (expr is IntConstant){
839 int value = ((IntConstant) expr).Value;
841 if (target_type == TypeManager.sbyte_type){
842 if (value >= SByte.MinValue && value <= SByte.MaxValue)
844 } else if (target_type == TypeManager.byte_type){
845 if (value >= 0 && value <= Byte.MaxValue)
847 } else if (target_type == TypeManager.short_type){
848 if (value >= Int16.MinValue && value <= Int16.MaxValue)
850 } else if (target_type == TypeManager.ushort_type){
851 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
853 } else if (target_type == TypeManager.uint32_type){
856 } else if (target_type == TypeManager.uint64_type){
858 // we can optimize this case: a positive int32
859 // always fits on a uint64. But we need an opcode
866 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
870 if (expr is LongConstant && target_type == TypeManager.uint64_type){
872 // Try the implicit constant expression conversion
873 // from long to ulong, instead of a nice routine,
876 long v = ((LongConstant) expr).Value;
882 // If `expr_type' implements `target_type' (which is an iface)
883 // see TryImplicitIntConversion
885 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
888 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
891 if (expr_type == TypeManager.anonymous_method_type){
892 if (!TypeManager.IsDelegateType (target_type))
895 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
896 return ame.ImplicitStandardConversionExists (target_type);
903 /// Finds "most encompassed type" according to the spec (13.4.2)
904 /// amongst the methods in the MethodGroupExpr
906 static Type FindMostEncompassedType (ArrayList types)
910 if (types.Count == 0)
913 if (types.Count == 1)
914 return (Type) types [0];
916 EmptyExpression expr = EmptyExpression.Grab ();
918 foreach (Type t in types) {
925 if (ImplicitStandardConversionExists (expr, best))
930 foreach (Type t in types) {
933 if (!ImplicitStandardConversionExists (expr, t)) {
939 EmptyExpression.Release (expr);
945 /// Finds "most encompassing type" according to the spec (13.4.2)
946 /// amongst the types in the given set
948 static Type FindMostEncompassingType (ArrayList types)
952 if (types.Count == 0)
955 if (types.Count == 1)
956 return (Type) types [0];
958 EmptyExpression expr = EmptyExpression.Grab ();
960 foreach (Type t in types) {
967 if (ImplicitStandardConversionExists (expr, t))
971 foreach (Type t in types) {
975 if (!ImplicitStandardConversionExists (expr, best)) {
981 EmptyExpression.Release (expr);
987 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
988 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
989 /// for explicit and implicit conversion operators.
991 static public Type FindMostSpecificSource (IList list,
992 Expression source, bool apply_explicit_conv_rules)
994 ArrayList src_types_set = new ArrayList ();
997 // If any operator converts from S then Sx = S
999 Type source_type = source.Type;
1000 foreach (MethodBase mb in list){
1001 ParameterData pd = TypeManager.GetParameterData (mb);
1002 Type param_type = pd.ParameterType (0);
1004 if (param_type == source_type)
1007 src_types_set.Add (param_type);
1011 // Explicit Conv rules
1013 if (apply_explicit_conv_rules) {
1014 ArrayList candidate_set = new ArrayList ();
1016 foreach (Type param_type in src_types_set){
1017 if (ImplicitStandardConversionExists (source, param_type))
1018 candidate_set.Add (param_type);
1021 if (candidate_set.Count != 0)
1022 return FindMostEncompassedType (candidate_set);
1028 if (apply_explicit_conv_rules)
1029 return FindMostEncompassingType (src_types_set);
1031 return FindMostEncompassedType (src_types_set);
1035 /// Finds the most specific target Tx according to section 13.4.4
1037 static public Type FindMostSpecificTarget (IList list,
1038 Type target, bool apply_explicit_conv_rules)
1040 ArrayList tgt_types_set = new ArrayList ();
1043 // If any operator converts to T then Tx = T
1045 foreach (MethodInfo mi in list){
1046 Type ret_type = mi.ReturnType;
1047 if (ret_type == target)
1050 tgt_types_set.Add (ret_type);
1054 // Explicit conv rules
1056 if (apply_explicit_conv_rules) {
1057 ArrayList candidate_set = new ArrayList ();
1059 EmptyExpression expr = EmptyExpression.Grab ();
1061 foreach (Type ret_type in tgt_types_set){
1062 expr.SetType (ret_type);
1064 if (ImplicitStandardConversionExists (expr, target))
1065 candidate_set.Add (ret_type);
1068 EmptyExpression.Release (expr);
1070 if (candidate_set.Count != 0)
1071 return FindMostEncompassingType (candidate_set);
1075 // Okay, final case !
1077 if (apply_explicit_conv_rules)
1078 return FindMostEncompassedType (tgt_types_set);
1080 return FindMostEncompassingType (tgt_types_set);
1084 /// User-defined Implicit conversions
1086 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1087 Type target, Location loc)
1089 return UserDefinedConversion (ec, source, target, loc, false);
1093 /// User-defined Explicit conversions
1095 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1096 Type target, Location loc)
1098 return UserDefinedConversion (ec, source, target, loc, true);
1101 static void AddConversionOperators (ArrayList list,
1102 Expression source, Type target_type,
1103 bool look_for_explicit,
1109 Type source_type = source.Type;
1110 EmptyExpression expr = EmptyExpression.Grab ();
1111 foreach (MethodInfo m in mg.Methods) {
1112 ParameterData pd = TypeManager.GetParameterData (m);
1113 Type return_type = m.ReturnType;
1114 Type arg_type = pd.ParameterType (0);
1116 if (source_type != arg_type) {
1117 if (!ImplicitStandardConversionExists (source, arg_type)) {
1118 if (!look_for_explicit)
1120 expr.SetType (arg_type);
1121 if (!ImplicitStandardConversionExists (expr, source_type))
1126 if (target_type != return_type) {
1127 expr.SetType (return_type);
1128 if (!ImplicitStandardConversionExists (expr, target_type)) {
1129 if (!look_for_explicit)
1131 expr.SetType (target_type);
1132 if (!ImplicitStandardConversionExists (expr, return_type))
1140 EmptyExpression.Release (expr);
1144 /// Compute the user-defined conversion operator from source_type to target_type.
1145 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1147 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1149 ArrayList ops = new ArrayList (4);
1151 Type source_type = source.Type;
1153 if (source_type != TypeManager.decimal_type) {
1154 AddConversionOperators (ops, source, target_type, look_for_explicit,
1155 Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1156 if (look_for_explicit) {
1157 AddConversionOperators (ops, source, target_type, look_for_explicit,
1158 Expression.MethodLookup (
1159 container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1163 if (target_type != TypeManager.decimal_type) {
1164 AddConversionOperators (ops, source, target_type, look_for_explicit,
1165 Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1166 if (look_for_explicit) {
1167 AddConversionOperators (ops, source, target_type, look_for_explicit,
1168 Expression.MethodLookup (
1169 container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1176 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1177 if (most_specific_source == null)
1180 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1181 if (most_specific_target == null)
1184 MethodInfo method = null;
1186 foreach (MethodInfo m in ops) {
1187 if (m.ReturnType != most_specific_target)
1189 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1191 // Ambiguous: more than one conversion operator satisfies the signature.
1200 static DoubleHash explicit_conv = new DoubleHash (100);
1201 static DoubleHash implicit_conv = new DoubleHash (100);
1204 /// User-defined conversions
1206 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1207 Type target, Location loc,
1208 bool look_for_explicit)
1210 Type source_type = source.Type;
1211 MethodInfo method = null;
1214 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1216 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1217 method = (MethodInfo) o;
1219 method = GetConversionOperator (null, source, target, look_for_explicit);
1220 if (!(source is Constant))
1221 hash.Insert (source_type, target, method);
1227 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1230 // This will do the conversion to the best match that we
1231 // found. Now we need to perform an implict standard conversion
1232 // if the best match was not the type that we were requested
1235 if (look_for_explicit)
1236 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1238 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1244 e = new UserCast (method, source, loc);
1245 if (e.Type != target){
1246 if (!look_for_explicit)
1247 e = ImplicitConversionStandard (ec, e, target, loc);
1249 e = ExplicitConversionStandard (ec, e, target, loc);
1256 /// Converts implicitly the resolved expression `expr' into the
1257 /// `target_type'. It returns a new expression that can be used
1258 /// in a context that expects a `target_type'.
1260 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1261 Type target_type, Location loc)
1265 if (target_type == null)
1266 throw new Exception ("Target type is null");
1268 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1272 e = ImplicitUserConversion (ec, expr, target_type, loc);
1281 /// Attempts to apply the `Standard Implicit
1282 /// Conversion' rules to the expression `expr' into
1283 /// the `target_type'. It returns a new expression
1284 /// that can be used in a context that expects a
1287 /// This is different from `ImplicitConversion' in that the
1288 /// user defined implicit conversions are excluded.
1290 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1291 Type target_type, Location loc)
1293 Type expr_type = expr.Type;
1296 // TODO: refactor to be a part of constant infrastructure
1297 if (expr is NullLiteral) {
1298 if (TypeManager.IsNullableType (target_type))
1299 return new Nullable.NullableLiteral (target_type, loc);
1302 if (TypeManager.IsNullableType (target_type)) {
1303 Type target = TypeManager.GetTypeArguments (target_type) [0];
1305 if (TypeManager.IsNullableType (expr.Type)) {
1306 e = new Nullable.LiftedConversion (
1307 expr, target_type, false, false, loc).Resolve (ec);
1311 e = ImplicitConversion (ec, expr, target, loc);
1313 return Nullable.Wrap.Create (e, ec);
1317 if (expr.eclass == ExprClass.MethodGroup){
1318 if (!TypeManager.IsDelegateType (target_type)){
1323 // Only allow anonymous method conversions on post ISO_1
1325 if (RootContext.Version != LanguageVersion.ISO_1){
1326 MethodGroupExpr mg = expr as MethodGroupExpr;
1328 return ImplicitDelegateCreation.Create (
1329 ec, mg, target_type, loc);
1333 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1337 // Attempt to do the implicit constant expression conversions
1339 Constant c = expr as Constant;
1342 // If `target_type' is an interface and the type of `ic' implements the interface
1343 // e.g. target_type is IComparable, IConvertible, IFormattable
1345 if (c.Type == TypeManager.int32_type && target_type.IsInterface && target_type.IsAssignableFrom (c.Type))
1346 return new BoxedCast (c, target_type);
1349 c = c.ConvertImplicitly (target_type);
1351 Console.WriteLine ("Conversion error happened in line {0}", loc);
1358 e = ImplicitNumericConversion (expr, target_type);
1362 e = ImplicitReferenceConversion (expr, target_type);
1366 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1367 IntLiteral i = (IntLiteral) expr;
1370 return new EnumConstant ((Constant) expr, target_type);
1374 if (expr_type.IsPointer){
1375 if (target_type == TypeManager.void_ptr_type)
1376 return EmptyCast.Create (expr, target_type);
1379 // yep, comparing pointer types cant be done with
1380 // t1 == t2, we have to compare their element types.
1382 if (target_type.IsPointer){
1383 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1390 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1391 return EmptyCast.Create (NullPointer.Null, target_type);
1394 if (expr_type == TypeManager.anonymous_method_type){
1395 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1397 AnonymousMethod am = ame.Compatible (ec, target_type);
1399 return am.Resolve (ec);
1406 /// Attempts to implicitly convert `source' into `target_type', using
1407 /// ImplicitConversion. If there is no implicit conversion, then
1408 /// an error is signaled
1410 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1411 Type target_type, Location loc)
1413 Expression e = ImplicitConversion (ec, source, target_type, loc);
1417 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1422 /// Performs the explicit numeric conversions
1424 /// There are a few conversions that are not part of the C# standard,
1425 /// they were interim hacks in the C# compiler that were supposed to
1426 /// become explicit operators in the UIntPtr class and IntPtr class,
1427 /// but for historical reasons it did not happen, so the C# compiler
1428 /// ended up with these special hacks.
1430 /// See bug 59800 for details.
1432 /// The conversion are:
1442 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1444 Type expr_type = expr.Type;
1445 Type real_target_type = target_type;
1447 if (expr_type == TypeManager.sbyte_type){
1449 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1451 if (real_target_type == TypeManager.byte_type)
1452 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1453 if (real_target_type == TypeManager.ushort_type)
1454 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1455 if (real_target_type == TypeManager.uint32_type)
1456 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1457 if (real_target_type == TypeManager.uint64_type)
1458 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1459 if (real_target_type == TypeManager.char_type)
1460 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1462 // One of the built-in conversions that belonged in the class library
1463 if (real_target_type == TypeManager.uintptr_type){
1464 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1466 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1468 } else if (expr_type == TypeManager.byte_type){
1470 // From byte to sbyte and char
1472 if (real_target_type == TypeManager.sbyte_type)
1473 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1474 if (real_target_type == TypeManager.char_type)
1475 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1476 } else if (expr_type == TypeManager.short_type){
1478 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1480 if (real_target_type == TypeManager.sbyte_type)
1481 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1482 if (real_target_type == TypeManager.byte_type)
1483 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1484 if (real_target_type == TypeManager.ushort_type)
1485 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1486 if (real_target_type == TypeManager.uint32_type)
1487 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1488 if (real_target_type == TypeManager.uint64_type)
1489 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1490 if (real_target_type == TypeManager.char_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1493 // One of the built-in conversions that belonged in the class library
1494 if (real_target_type == TypeManager.uintptr_type){
1495 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1497 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1499 } else if (expr_type == TypeManager.ushort_type){
1501 // From ushort to sbyte, byte, short, char
1503 if (real_target_type == TypeManager.sbyte_type)
1504 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1505 if (real_target_type == TypeManager.byte_type)
1506 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1507 if (real_target_type == TypeManager.short_type)
1508 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1509 if (real_target_type == TypeManager.char_type)
1510 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1511 } else if (expr_type == TypeManager.int32_type){
1513 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1515 if (real_target_type == TypeManager.sbyte_type)
1516 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1517 if (real_target_type == TypeManager.byte_type)
1518 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1519 if (real_target_type == TypeManager.short_type)
1520 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1521 if (real_target_type == TypeManager.ushort_type)
1522 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1523 if (real_target_type == TypeManager.uint32_type)
1524 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1525 if (real_target_type == TypeManager.uint64_type)
1526 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1527 if (real_target_type == TypeManager.char_type)
1528 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1530 // One of the built-in conversions that belonged in the class library
1531 if (real_target_type == TypeManager.uintptr_type){
1532 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1534 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1536 } else if (expr_type == TypeManager.uint32_type){
1538 // From uint to sbyte, byte, short, ushort, int, char
1540 if (real_target_type == TypeManager.sbyte_type)
1541 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1542 if (real_target_type == TypeManager.byte_type)
1543 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1544 if (real_target_type == TypeManager.short_type)
1545 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1546 if (real_target_type == TypeManager.ushort_type)
1547 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1548 if (real_target_type == TypeManager.int32_type)
1549 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1550 if (real_target_type == TypeManager.char_type)
1551 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1552 } else if (expr_type == TypeManager.int64_type){
1554 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1556 if (real_target_type == TypeManager.sbyte_type)
1557 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1558 if (real_target_type == TypeManager.byte_type)
1559 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1560 if (real_target_type == TypeManager.short_type)
1561 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1562 if (real_target_type == TypeManager.ushort_type)
1563 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1564 if (real_target_type == TypeManager.int32_type)
1565 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1566 if (real_target_type == TypeManager.uint32_type)
1567 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1568 if (real_target_type == TypeManager.uint64_type)
1569 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1570 if (real_target_type == TypeManager.char_type)
1571 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1572 } else if (expr_type == TypeManager.uint64_type){
1574 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1576 if (real_target_type == TypeManager.sbyte_type)
1577 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1578 if (real_target_type == TypeManager.byte_type)
1579 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1580 if (real_target_type == TypeManager.short_type)
1581 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1582 if (real_target_type == TypeManager.ushort_type)
1583 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1584 if (real_target_type == TypeManager.int32_type)
1585 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1586 if (real_target_type == TypeManager.uint32_type)
1587 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1588 if (real_target_type == TypeManager.int64_type)
1589 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1590 if (real_target_type == TypeManager.char_type)
1591 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1593 // One of the built-in conversions that belonged in the class library
1594 if (real_target_type == TypeManager.intptr_type){
1595 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1596 TypeManager.intptr_type, true);
1598 } else if (expr_type == TypeManager.char_type){
1600 // From char to sbyte, byte, short
1602 if (real_target_type == TypeManager.sbyte_type)
1603 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1604 if (real_target_type == TypeManager.byte_type)
1605 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1606 if (real_target_type == TypeManager.short_type)
1607 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1608 } else if (expr_type == TypeManager.float_type){
1610 // From float to sbyte, byte, short,
1611 // ushort, int, uint, long, ulong, char
1614 if (real_target_type == TypeManager.sbyte_type)
1615 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1616 if (real_target_type == TypeManager.byte_type)
1617 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1618 if (real_target_type == TypeManager.short_type)
1619 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1620 if (real_target_type == TypeManager.ushort_type)
1621 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1622 if (real_target_type == TypeManager.int32_type)
1623 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1624 if (real_target_type == TypeManager.uint32_type)
1625 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1626 if (real_target_type == TypeManager.int64_type)
1627 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1628 if (real_target_type == TypeManager.uint64_type)
1629 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1630 if (real_target_type == TypeManager.char_type)
1631 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1632 if (real_target_type == TypeManager.decimal_type)
1633 return new CastToDecimal (expr, true);
1634 } else if (expr_type == TypeManager.double_type){
1636 // From double to sbyte, byte, short,
1637 // ushort, int, uint, long, ulong,
1638 // char, float or decimal
1640 if (real_target_type == TypeManager.sbyte_type)
1641 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1642 if (real_target_type == TypeManager.byte_type)
1643 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1644 if (real_target_type == TypeManager.short_type)
1645 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1646 if (real_target_type == TypeManager.ushort_type)
1647 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1648 if (real_target_type == TypeManager.int32_type)
1649 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1650 if (real_target_type == TypeManager.uint32_type)
1651 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1652 if (real_target_type == TypeManager.int64_type)
1653 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1654 if (real_target_type == TypeManager.uint64_type)
1655 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1656 if (real_target_type == TypeManager.char_type)
1657 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1658 if (real_target_type == TypeManager.float_type)
1659 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1660 if (real_target_type == TypeManager.decimal_type)
1661 return new CastToDecimal (expr, true);
1662 } else if (expr_type == TypeManager.uintptr_type){
1664 // Various built-in conversions that belonged in the class library
1666 // from uintptr to sbyte, short, int32
1668 if (real_target_type == TypeManager.sbyte_type){
1669 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1670 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1672 if (real_target_type == TypeManager.short_type){
1673 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1674 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1676 if (real_target_type == TypeManager.int32_type){
1677 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1678 TypeManager.int32_type);
1680 } else if (expr_type == TypeManager.intptr_type){
1681 if (real_target_type == TypeManager.uint64_type){
1682 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1683 TypeManager.uint64_type);
1685 } else if (expr_type == TypeManager.decimal_type) {
1686 return new CastFromDecimal (expr, target_type).Resolve ();
1692 /// Returns whether an explicit reference conversion can be performed
1693 /// from source_type to target_type
1695 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1697 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1698 bool target_is_value_type = target_type.IsValueType;
1700 if (source_type == target_type)
1704 // From generic parameter to any type
1706 if (TypeManager.IsGenericParameter (source_type))
1707 return ExplicitTypeParameterConversionExists (source_type, target_type);
1710 // From object to a generic parameter
1712 if (source_type == TypeManager.object_type && target_is_type_param)
1716 // From object to any reference type
1718 if (source_type == TypeManager.object_type && !target_is_value_type)
1722 // From any class S to any class-type T, provided S is a base class of T
1724 if (TypeManager.IsSubclassOf (target_type, source_type))
1728 // From any interface type S to any interface T provided S is not derived from T
1730 if (source_type.IsInterface && target_type.IsInterface){
1731 if (!TypeManager.IsSubclassOf (target_type, source_type))
1736 // From any class type S to any interface T, provided S is not sealed
1737 // and provided S does not implement T.
1739 if (target_type.IsInterface && !source_type.IsSealed &&
1740 !TypeManager.ImplementsInterface (source_type, target_type))
1744 // From any interface-type S to to any class type T, provided T is not
1745 // sealed, or provided T implements S.
1747 if (source_type.IsInterface &&
1748 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1752 if (source_type.IsInterface && IList_To_Array (source_type, target_type))
1755 // From an array type S with an element type Se to an array type T with an
1756 // element type Te provided all the following are true:
1757 // * S and T differe only in element type, in other words, S and T
1758 // have the same number of dimensions.
1759 // * Both Se and Te are reference types
1760 // * An explicit referenc conversions exist from Se to Te
1762 if (source_type.IsArray && target_type.IsArray) {
1763 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1765 Type source_element_type = TypeManager.GetElementType (source_type);
1766 Type target_element_type = TypeManager.GetElementType (target_type);
1768 if (TypeManager.IsGenericParameter (source_element_type) ||
1769 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1770 if (ExplicitReferenceConversionExists (source_element_type,
1771 target_element_type))
1777 // From System.Array to any array-type
1778 if (source_type == TypeManager.array_type &&
1779 target_type.IsArray){
1784 // From System delegate to any delegate-type
1786 if (source_type == TypeManager.delegate_type &&
1787 TypeManager.IsDelegateType (target_type))
1791 // From ICloneable to Array or Delegate types
1793 if (source_type == TypeManager.icloneable_type &&
1794 (target_type == TypeManager.array_type ||
1795 target_type == TypeManager.delegate_type))
1802 /// Implements Explicit Reference conversions
1804 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1806 Type source_type = source.Type;
1807 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1808 bool target_is_value_type = target_type.IsValueType;
1811 // From object to a generic parameter
1813 if (source_type == TypeManager.object_type && target_is_type_param)
1814 return new UnboxCast (source, target_type);
1817 // Explicit type parameter conversion.
1820 if (TypeManager.IsGenericParameter (source_type))
1821 return ExplicitTypeParameterConversion (source, target_type);
1824 // From object to any reference type
1826 if (source_type == TypeManager.object_type && !target_is_value_type)
1827 return new ClassCast (source, target_type);
1830 // Unboxing conversion.
1832 if (((source_type == TypeManager.enum_type &&
1833 !(source is EmptyCast)) ||
1834 source_type == TypeManager.value_type) && target_is_value_type)
1835 return new UnboxCast (source, target_type);
1838 // From any class S to any class-type T, provided S is a base class of T
1840 if (TypeManager.IsSubclassOf (target_type, source_type))
1841 return new ClassCast (source, target_type);
1844 // From any interface type S to any interface T provided S is not derived from T
1846 if (source_type.IsInterface && target_type.IsInterface){
1847 if (TypeManager.ImplementsInterface (source_type, target_type))
1850 return new ClassCast (source, target_type);
1854 // From any class type S to any interface T, provides S is not sealed
1855 // and provided S does not implement T.
1857 if (target_type.IsInterface && !source_type.IsSealed) {
1858 if (TypeManager.ImplementsInterface (source_type, target_type))
1861 return new ClassCast (source, target_type);
1866 // From any interface-type S to to any class type T, provided T is not
1867 // sealed, or provided T implements S.
1869 if (source_type.IsInterface) {
1870 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1871 if (target_type.IsClass)
1872 return new ClassCast (source, target_type);
1874 return new UnboxCast (source, target_type);
1878 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1879 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1881 if (IList_To_Array (source_type, target_type))
1882 return new ClassCast (source, target_type);
1887 // From an array type S with an element type Se to an array type T with an
1888 // element type Te provided all the following are true:
1889 // * S and T differe only in element type, in other words, S and T
1890 // have the same number of dimensions.
1891 // * Both Se and Te are reference types
1892 // * An explicit referenc conversions exist from Se to Te
1894 if (source_type.IsArray && target_type.IsArray) {
1895 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1897 Type source_element_type = TypeManager.GetElementType (source_type);
1898 Type target_element_type = TypeManager.GetElementType (target_type);
1900 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1901 if (ExplicitReferenceConversionExists (source_element_type,
1902 target_element_type))
1903 return new ClassCast (source, target_type);
1908 // From System.Array to any array-type
1909 if (source_type == TypeManager.array_type &&
1910 target_type.IsArray) {
1911 return new ClassCast (source, target_type);
1915 // From System delegate to any delegate-type
1917 if (source_type == TypeManager.delegate_type &&
1918 TypeManager.IsDelegateType (target_type))
1919 return new ClassCast (source, target_type);
1922 // From ICloneable to Array or Delegate types
1924 if (source_type == TypeManager.icloneable_type &&
1925 (target_type == TypeManager.array_type ||
1926 target_type == TypeManager.delegate_type))
1927 return new ClassCast (source, target_type);
1933 /// Performs an explicit conversion of the expression `expr' whose
1934 /// type is expr.Type to `target_type'.
1936 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1937 Type target_type, Location loc)
1939 Type expr_type = expr.Type;
1941 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1942 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1947 // Unboxing conversions; only object types can be convertible to enum
1949 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1950 return new UnboxCast (expr, target_type);
1952 if (TypeManager.IsEnumType (expr_type)) {
1953 if (expr is EnumConstant)
1954 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1956 return ExplicitConversionCore (ec, EmptyCast.Create (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1959 if (TypeManager.IsEnumType (target_type)){
1960 if (expr_type == TypeManager.enum_type)
1961 return new UnboxCast (expr, target_type);
1963 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1965 return EmptyCast.Create (ce, target_type);
1968 ne = ExplicitNumericConversion (expr, target_type);
1973 // Skip the ExplicitReferenceConversion because we can not convert
1974 // from Null to a ValueType, and ExplicitReference wont check against
1975 // null literal explicitly
1977 if (expr_type != TypeManager.null_type){
1978 ne = ExplicitReferenceConversion (expr, target_type);
1984 ne = ExplicitUnsafe (expr, target_type);
1989 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1996 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1998 Type expr_type = expr.Type;
2000 if (target_type.IsPointer){
2001 if (expr_type.IsPointer)
2002 return EmptyCast.Create (expr, target_type);
2004 if (expr_type == TypeManager.sbyte_type ||
2005 expr_type == TypeManager.short_type ||
2006 expr_type == TypeManager.int32_type ||
2007 expr_type == TypeManager.int64_type)
2008 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
2010 if (expr_type == TypeManager.ushort_type ||
2011 expr_type == TypeManager.uint32_type ||
2012 expr_type == TypeManager.uint64_type ||
2013 expr_type == TypeManager.byte_type)
2014 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2017 if (expr_type.IsPointer){
2018 if (target_type == TypeManager.sbyte_type)
2019 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2020 else if (target_type == TypeManager.byte_type)
2021 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2022 else if (target_type == TypeManager.short_type)
2023 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2024 else if (target_type == TypeManager.ushort_type)
2025 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2026 else if (target_type == TypeManager.int32_type)
2027 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2028 else if (target_type == TypeManager.uint32_type)
2029 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2030 else if (target_type == TypeManager.uint64_type)
2031 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2032 else if (target_type == TypeManager.int64_type){
2033 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2040 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2042 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2043 Type target_type, Location l)
2045 int errors = Report.Errors;
2046 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2047 if (Report.Errors > errors)
2053 ne = ExplicitNumericConversion (expr, target_type);
2057 ne = ExplicitReferenceConversion (expr, target_type);
2061 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2062 return EmptyCast.Create (expr, target_type);
2064 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
2069 /// Performs an explicit conversion of the expression `expr' whose
2070 /// type is expr.Type to `target_type'.
2072 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2073 Type target_type, Location loc)
2077 Type expr_type = expr.Type;
2078 if (TypeManager.IsNullableType (target_type)) {
2079 if (TypeManager.IsNullableType (expr_type)) {
2080 e = new Nullable.LiftedConversion (
2081 expr, target_type, false, true, loc).Resolve (ec);
2084 } else if (expr_type == TypeManager.object_type) {
2085 return new UnboxCast (expr, target_type);
2087 Type target = TypeManager.GetTypeArguments (target_type) [0];
2089 e = ExplicitConversionCore (ec, expr, target, loc);
2091 return Nullable.Wrap.Create (e, ec);
2093 } else if (TypeManager.IsNullableType (expr_type)) {
2094 Expression source = Nullable.Unwrap.Create (expr, ec);
2095 if (source != null) {
2096 e = ExplicitConversionCore (ec, source, target_type, loc);
2102 e = ExplicitConversionCore (ec, expr, target_type, loc);
2106 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);