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);
276 // from the null type to any reference-type.
278 NullLiteral nl = expr as NullLiteral;
280 return nl.ConvertImplicitly(target_type);
283 if (ImplicitReferenceConversionCore (expr, target_type))
284 return EmptyCast.Create (expr, target_type);
287 if (ImplicitBoxingConversionExists (expr, target_type, out use_class_cast)) {
289 return new ClassCast (expr, target_type);
291 return new BoxedCast (expr, target_type);
297 static public bool ImplicitReferenceConversionCore (Expression expr, Type target_type)
299 Type expr_type = expr.Type;
302 // notice that it is possible to write "ValueType v = 1", the ValueType here
303 // is an abstract class, and not really a value type, so we apply the same rules.
305 if (target_type == TypeManager.object_type) {
307 // A pointer type cannot be converted to object
309 if (expr_type.IsPointer)
312 if (TypeManager.IsValueType (expr_type))
314 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
315 return expr_type != TypeManager.anonymous_method_type;
319 } else if (target_type == TypeManager.value_type) {
320 return expr_type == TypeManager.enum_type;
321 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
323 // Special case: enumeration to System.Enum.
324 // System.Enum is not a value type, it is a class, so we need
325 // a boxing conversion
327 if (target_type == TypeManager.enum_type || TypeManager.IsGenericParameter (expr_type))
333 // This code is kind of mirrored inside ImplicitStandardConversionExists
334 // with the small distinction that we only probe there
336 // Always ensure that the code here and there is in sync
338 // from any class-type S to any interface-type T.
339 if (target_type.IsInterface) {
340 if (TypeManager.ImplementsInterface (expr_type, target_type)){
341 return !TypeManager.IsGenericParameter (expr_type) &&
342 !TypeManager.IsValueType (expr_type);
346 // from any interface type S to interface-type T.
347 if (expr_type.IsInterface && target_type.IsInterface) {
348 return TypeManager.ImplementsInterface (expr_type, target_type);
351 // from an array-type S to an array-type of type T
352 if (expr_type.IsArray && target_type.IsArray) {
353 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
355 Type expr_element_type = TypeManager.GetElementType (expr_type);
357 if (MyEmptyExpr == null)
358 MyEmptyExpr = new EmptyExpression ();
360 MyEmptyExpr.SetType (expr_element_type);
361 Type target_element_type = TypeManager.GetElementType (target_type);
363 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
364 return ImplicitStandardConversionExists (
365 MyEmptyExpr, target_element_type);
369 // from an array-type to System.Array
370 if (expr_type.IsArray && target_type == TypeManager.array_type)
373 // from an array-type of type T to IList<T>
374 if (expr_type.IsArray && Array_To_IList (expr_type, target_type))
377 // from any delegate type to System.Delegate
378 if (target_type == TypeManager.delegate_type &&
379 (expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)))
382 // from a generic type definition to a generic instance.
383 if (TypeManager.IsEqual (expr_type, target_type))
389 static public bool ImplicitBoxingConversionExists (Expression expr, Type target_type,
390 out bool use_class_cast)
392 Type expr_type = expr.Type;
393 use_class_cast = false;
396 // From any value-type to the type object.
398 if (target_type == TypeManager.object_type) {
400 // A pointer type cannot be converted to object
402 if (expr_type.IsPointer)
405 return TypeManager.IsValueType (expr_type);
409 // From any value-type to the type System.ValueType.
411 if (target_type == TypeManager.value_type)
412 return TypeManager.IsValueType (expr_type);
414 if (target_type == TypeManager.enum_type) {
416 // From any enum-type to the type System.Enum.
418 if (expr_type.IsEnum)
421 // From any nullable-type with an underlying enum-type to the type System.Enum
423 if (TypeManager.IsNullableType (expr_type))
424 return TypeManager.GetTypeArguments (expr_type) [0].IsEnum;
427 if (TypeManager.IsSubclassOf (expr_type, target_type)) {
428 if (TypeManager.IsGenericParameter (expr_type))
434 // This code is kind of mirrored inside ImplicitStandardConversionExists
435 // with the small distinction that we only probe there
437 // Always ensure that the code here and there is in sync
439 // from any class-type S to any interface-type T.
440 if (target_type.IsInterface) {
441 if (TypeManager.ImplementsInterface (expr_type, target_type))
442 return TypeManager.IsGenericParameter (expr_type) ||
443 TypeManager.IsValueType (expr_type);
446 if (TypeManager.IsGenericParameter (expr_type))
447 return ImplicitTypeParameterBoxingConversion (
448 expr_type, target_type, out use_class_cast);
454 // Tests whether an implicit reference conversion exists between expr_type
457 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
459 if (target_type.IsValueType)
462 Type expr_type = expr.Type;
464 // from the null type to any reference-type.
465 if (expr_type == TypeManager.null_type){
469 if (TypeManager.IsGenericParameter (expr_type))
470 return ImplicitTypeParameterConversion (expr, target_type) != null;
473 if (ImplicitReferenceConversionCore (expr, target_type) ||
474 ImplicitBoxingConversionExists (expr, target_type, out use_class_cast))
481 /// Implicit Numeric Conversions.
483 /// expr is the expression to convert, returns a new expression of type
484 /// target_type or null if an implicit conversion is not possible.
486 static public Expression ImplicitNumericConversion (Expression expr,
489 Type expr_type = expr.Type;
490 Type real_target_type = target_type;
492 if (expr_type == TypeManager.sbyte_type){
494 // From sbyte to short, int, long, float, double, decimal
496 if (real_target_type == TypeManager.int32_type)
497 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
498 if (real_target_type == TypeManager.int64_type)
499 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
500 if (real_target_type == TypeManager.double_type)
501 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
502 if (real_target_type == TypeManager.float_type)
503 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
504 if (real_target_type == TypeManager.short_type)
505 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
506 if (real_target_type == TypeManager.decimal_type)
507 return new CastToDecimal (expr);
508 } else if (expr_type == TypeManager.byte_type){
510 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
512 if ((real_target_type == TypeManager.short_type) ||
513 (real_target_type == TypeManager.ushort_type) ||
514 (real_target_type == TypeManager.int32_type) ||
515 (real_target_type == TypeManager.uint32_type))
516 return EmptyCast.Create (expr, target_type);
518 if (real_target_type == TypeManager.uint64_type)
519 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
520 if (real_target_type == TypeManager.int64_type)
521 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
522 if (real_target_type == TypeManager.float_type)
523 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
524 if (real_target_type == TypeManager.double_type)
525 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
526 if (real_target_type == TypeManager.decimal_type)
527 return new CastToDecimal (expr);
529 } else if (expr_type == TypeManager.short_type){
531 // From short to int, long, float, double, decimal
533 if (real_target_type == TypeManager.int32_type)
534 return EmptyCast.Create (expr, target_type);
535 if (real_target_type == TypeManager.int64_type)
536 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
537 if (real_target_type == TypeManager.double_type)
538 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
539 if (real_target_type == TypeManager.float_type)
540 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
541 if (real_target_type == TypeManager.decimal_type)
542 return new CastToDecimal (expr);
544 } else if (expr_type == TypeManager.ushort_type){
546 // From ushort to int, uint, long, ulong, float, double, decimal
548 if (real_target_type == TypeManager.uint32_type)
549 return EmptyCast.Create (expr, target_type);
551 if (real_target_type == TypeManager.uint64_type)
552 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
553 if (real_target_type == TypeManager.int32_type)
554 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
555 if (real_target_type == TypeManager.int64_type)
556 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
557 if (real_target_type == TypeManager.double_type)
558 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
559 if (real_target_type == TypeManager.float_type)
560 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
561 if (real_target_type == TypeManager.decimal_type)
562 return new CastToDecimal (expr);
563 } else if (expr_type == TypeManager.int32_type){
565 // From int to long, float, double, decimal
567 if (real_target_type == TypeManager.int64_type)
568 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
569 if (real_target_type == TypeManager.double_type)
570 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
571 if (real_target_type == TypeManager.float_type)
572 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
573 if (real_target_type == TypeManager.decimal_type)
574 return new CastToDecimal (expr);
575 } else if (expr_type == TypeManager.uint32_type){
577 // From uint to long, ulong, float, double, decimal
579 if (real_target_type == TypeManager.int64_type)
580 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
581 if (real_target_type == TypeManager.uint64_type)
582 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
583 if (real_target_type == TypeManager.double_type)
584 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
586 if (real_target_type == TypeManager.float_type)
587 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
589 if (real_target_type == TypeManager.decimal_type)
590 return new CastToDecimal (expr);
591 } else if (expr_type == TypeManager.int64_type){
593 // From long/ulong to float, double
595 if (real_target_type == TypeManager.double_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
597 if (real_target_type == TypeManager.float_type)
598 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
599 if (real_target_type == TypeManager.decimal_type)
600 return new CastToDecimal (expr);
601 } else if (expr_type == TypeManager.uint64_type){
603 // From ulong to float, double
605 if (real_target_type == TypeManager.double_type)
606 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
608 if (real_target_type == TypeManager.float_type)
609 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
611 if (real_target_type == TypeManager.decimal_type)
612 return new CastToDecimal (expr);
613 } else if (expr_type == TypeManager.char_type){
615 // From char to ushort, int, uint, long, ulong, float, double, decimal
617 if ((real_target_type == TypeManager.ushort_type) ||
618 (real_target_type == TypeManager.int32_type) ||
619 (real_target_type == TypeManager.uint32_type))
620 return EmptyCast.Create (expr, target_type);
621 if (real_target_type == TypeManager.uint64_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
623 if (real_target_type == TypeManager.int64_type)
624 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
625 if (real_target_type == TypeManager.float_type)
626 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
627 if (real_target_type == TypeManager.double_type)
628 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
629 if (real_target_type == TypeManager.decimal_type)
630 return new CastToDecimal (expr);
631 } else if (expr_type == TypeManager.float_type){
635 if (real_target_type == TypeManager.double_type)
636 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
643 /// Same as ImplicitStandardConversionExists except that it also looks at
644 /// implicit user defined conversions - needed for overload resolution
646 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
649 if (expr is NullLiteral) {
650 if (TypeManager.IsGenericParameter (target_type))
651 return TypeParameter_to_Null (target_type);
653 if (TypeManager.IsNullableType (target_type))
657 if (ImplicitStandardConversionExists (expr, target_type))
660 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
663 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
665 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
669 /// Determines if a standard implicit conversion exists from
670 /// expr_type to target_type
672 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
674 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
676 Type expr_type = expr.Type;
678 if (TypeManager.IsNullableType (target_type)) {
679 // if implicit standard conversion S -> T exists, S -> T? and S? -> T? also exists
680 target_type = TypeManager.GetTypeArguments (target_type) [0];
683 if (TypeManager.IsNullableType (expr_type)) {
684 EmptyExpression new_expr = EmptyExpression.Grab ();
685 new_expr.SetType (TypeManager.GetTypeArguments (expr_type) [0]);
686 bool retval = ImplicitStandardConversionExists (new_expr, target_type);
687 EmptyExpression.Release (new_expr);
692 if (expr_type == TypeManager.void_type)
695 //Console.WriteLine ("Expr is {0}", expr);
696 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
697 if (TypeManager.IsEqual (expr_type, target_type))
701 // First numeric conversions
703 if (expr_type == TypeManager.sbyte_type){
705 // From sbyte to short, int, long, float, double, decimal
707 if ((target_type == TypeManager.int32_type) ||
708 (target_type == TypeManager.int64_type) ||
709 (target_type == TypeManager.double_type) ||
710 (target_type == TypeManager.float_type) ||
711 (target_type == TypeManager.short_type) ||
712 (target_type == TypeManager.decimal_type))
715 } else if (expr_type == TypeManager.byte_type){
717 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
719 if ((target_type == TypeManager.short_type) ||
720 (target_type == TypeManager.ushort_type) ||
721 (target_type == TypeManager.int32_type) ||
722 (target_type == TypeManager.uint32_type) ||
723 (target_type == TypeManager.uint64_type) ||
724 (target_type == TypeManager.int64_type) ||
725 (target_type == TypeManager.float_type) ||
726 (target_type == TypeManager.double_type) ||
727 (target_type == TypeManager.decimal_type))
730 } else if (expr_type == TypeManager.short_type){
732 // From short to int, long, double, float, decimal
734 if ((target_type == TypeManager.int32_type) ||
735 (target_type == TypeManager.int64_type) ||
736 (target_type == TypeManager.double_type) ||
737 (target_type == TypeManager.float_type) ||
738 (target_type == TypeManager.decimal_type))
741 } else if (expr_type == TypeManager.ushort_type){
743 // From ushort to int, uint, long, ulong, double, float, decimal
745 if ((target_type == TypeManager.uint32_type) ||
746 (target_type == TypeManager.uint64_type) ||
747 (target_type == TypeManager.int32_type) ||
748 (target_type == TypeManager.int64_type) ||
749 (target_type == TypeManager.double_type) ||
750 (target_type == TypeManager.float_type) ||
751 (target_type == TypeManager.decimal_type))
754 } else if (expr_type == TypeManager.int32_type){
756 // From int to long, double, float, decimal
758 if ((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.uint32_type){
766 // From uint to long, ulong, double, float, decimal
768 if ((target_type == TypeManager.int64_type) ||
769 (target_type == TypeManager.uint64_type) ||
770 (target_type == TypeManager.double_type) ||
771 (target_type == TypeManager.float_type) ||
772 (target_type == TypeManager.decimal_type))
775 } else if ((expr_type == TypeManager.uint64_type) ||
776 (expr_type == TypeManager.int64_type)) {
778 // From long/ulong to double, float, decimal
780 if ((target_type == TypeManager.double_type) ||
781 (target_type == TypeManager.float_type) ||
782 (target_type == TypeManager.decimal_type))
785 } else if (expr_type == TypeManager.char_type){
787 // From char to ushort, int, uint, ulong, long, float, double, decimal
789 if ((target_type == TypeManager.ushort_type) ||
790 (target_type == TypeManager.int32_type) ||
791 (target_type == TypeManager.uint32_type) ||
792 (target_type == TypeManager.uint64_type) ||
793 (target_type == TypeManager.int64_type) ||
794 (target_type == TypeManager.float_type) ||
795 (target_type == TypeManager.double_type) ||
796 (target_type == TypeManager.decimal_type))
799 } else if (expr_type == TypeManager.float_type){
803 if (target_type == TypeManager.double_type)
807 if (expr.eclass == ExprClass.MethodGroup){
808 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
809 MethodGroupExpr mg = expr as MethodGroupExpr;
811 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
816 if (ImplicitReferenceConversionExists (expr, target_type))
820 // Implicit Constant Expression Conversions
822 if (expr is IntConstant){
823 int value = ((IntConstant) expr).Value;
825 if (target_type == TypeManager.sbyte_type){
826 if (value >= SByte.MinValue && value <= SByte.MaxValue)
828 } else if (target_type == TypeManager.byte_type){
829 if (value >= 0 && value <= Byte.MaxValue)
831 } else if (target_type == TypeManager.short_type){
832 if (value >= Int16.MinValue && value <= Int16.MaxValue)
834 } else if (target_type == TypeManager.ushort_type){
835 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
837 } else if (target_type == TypeManager.uint32_type){
840 } else if (target_type == TypeManager.uint64_type){
842 // we can optimize this case: a positive int32
843 // always fits on a uint64. But we need an opcode
850 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
854 if (expr is LongConstant && target_type == TypeManager.uint64_type){
856 // Try the implicit constant expression conversion
857 // from long to ulong, instead of a nice routine,
860 long v = ((LongConstant) expr).Value;
866 // If `expr_type' implements `target_type' (which is an iface)
867 // see TryImplicitIntConversion
869 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
872 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
875 if (expr_type == TypeManager.anonymous_method_type){
876 if (!TypeManager.IsDelegateType (target_type) &&
877 TypeManager.DropGenericTypeArguments (target_type) != TypeManager.expression_type)
880 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
881 return ame.ImplicitStandardConversionExists (target_type);
888 /// Finds "most encompassed type" according to the spec (13.4.2)
889 /// amongst the methods in the MethodGroupExpr
891 static Type FindMostEncompassedType (ArrayList types)
895 if (types.Count == 0)
898 if (types.Count == 1)
899 return (Type) types [0];
901 EmptyExpression expr = EmptyExpression.Grab ();
903 foreach (Type t in types) {
910 if (ImplicitStandardConversionExists (expr, best))
915 foreach (Type t in types) {
918 if (!ImplicitStandardConversionExists (expr, t)) {
924 EmptyExpression.Release (expr);
930 /// Finds "most encompassing type" according to the spec (13.4.2)
931 /// amongst the types in the given set
933 static Type FindMostEncompassingType (ArrayList types)
937 if (types.Count == 0)
940 if (types.Count == 1)
941 return (Type) types [0];
943 EmptyExpression expr = EmptyExpression.Grab ();
945 foreach (Type t in types) {
952 if (ImplicitStandardConversionExists (expr, t))
956 foreach (Type t in types) {
960 if (!ImplicitStandardConversionExists (expr, best)) {
966 EmptyExpression.Release (expr);
972 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
973 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
974 /// for explicit and implicit conversion operators.
976 static public Type FindMostSpecificSource (IList list,
977 Expression source, bool apply_explicit_conv_rules)
979 ArrayList src_types_set = new ArrayList ();
982 // If any operator converts from S then Sx = S
984 Type source_type = source.Type;
985 foreach (MethodBase mb in list){
986 ParameterData pd = TypeManager.GetParameterData (mb);
987 Type param_type = pd.ParameterType (0);
989 if (param_type == source_type)
992 src_types_set.Add (param_type);
996 // Explicit Conv rules
998 if (apply_explicit_conv_rules) {
999 ArrayList candidate_set = new ArrayList ();
1001 foreach (Type param_type in src_types_set){
1002 if (ImplicitStandardConversionExists (source, param_type))
1003 candidate_set.Add (param_type);
1006 if (candidate_set.Count != 0)
1007 return FindMostEncompassedType (candidate_set);
1013 if (apply_explicit_conv_rules)
1014 return FindMostEncompassingType (src_types_set);
1016 return FindMostEncompassedType (src_types_set);
1020 /// Finds the most specific target Tx according to section 13.4.4
1022 static public Type FindMostSpecificTarget (IList list,
1023 Type target, bool apply_explicit_conv_rules)
1025 ArrayList tgt_types_set = new ArrayList ();
1028 // If any operator converts to T then Tx = T
1030 foreach (MethodInfo mi in list){
1031 Type ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
1032 if (ret_type == target)
1035 tgt_types_set.Add (ret_type);
1039 // Explicit conv rules
1041 if (apply_explicit_conv_rules) {
1042 ArrayList candidate_set = new ArrayList ();
1044 EmptyExpression expr = EmptyExpression.Grab ();
1046 foreach (Type ret_type in tgt_types_set){
1047 expr.SetType (ret_type);
1049 if (ImplicitStandardConversionExists (expr, target))
1050 candidate_set.Add (ret_type);
1053 EmptyExpression.Release (expr);
1055 if (candidate_set.Count != 0)
1056 return FindMostEncompassingType (candidate_set);
1060 // Okay, final case !
1062 if (apply_explicit_conv_rules)
1063 return FindMostEncompassedType (tgt_types_set);
1065 return FindMostEncompassingType (tgt_types_set);
1069 /// User-defined Implicit conversions
1071 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1072 Type target, Location loc)
1074 return UserDefinedConversion (ec, source, target, loc, false);
1078 /// User-defined Explicit conversions
1080 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1081 Type target, Location loc)
1083 return UserDefinedConversion (ec, source, target, loc, true);
1086 static void AddConversionOperators (ArrayList list,
1087 Expression source, Type target_type,
1088 bool look_for_explicit,
1094 Type source_type = source.Type;
1095 EmptyExpression expr = EmptyExpression.Grab ();
1096 foreach (MethodInfo m in mg.Methods) {
1097 ParameterData pd = TypeManager.GetParameterData (m);
1098 Type return_type = TypeManager.TypeToCoreType (m.ReturnType);
1099 Type arg_type = pd.ParameterType (0);
1101 if (source_type != arg_type) {
1102 if (!ImplicitStandardConversionExists (source, arg_type)) {
1103 if (!look_for_explicit)
1105 expr.SetType (arg_type);
1106 if (!ImplicitStandardConversionExists (expr, source_type))
1111 if (target_type != return_type) {
1112 expr.SetType (return_type);
1113 if (!ImplicitStandardConversionExists (expr, target_type)) {
1114 if (!look_for_explicit)
1116 expr.SetType (target_type);
1117 if (!ImplicitStandardConversionExists (expr, return_type))
1125 EmptyExpression.Release (expr);
1129 /// Compute the user-defined conversion operator from source_type to target_type.
1130 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1132 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1134 ArrayList ops = new ArrayList (4);
1136 Type source_type = source.Type;
1138 if (source_type != TypeManager.decimal_type) {
1139 AddConversionOperators (ops, source, target_type, look_for_explicit,
1140 Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1141 if (look_for_explicit) {
1142 AddConversionOperators (ops, source, target_type, look_for_explicit,
1143 Expression.MethodLookup (
1144 container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1148 if (target_type != TypeManager.decimal_type) {
1149 AddConversionOperators (ops, source, target_type, look_for_explicit,
1150 Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1151 if (look_for_explicit) {
1152 AddConversionOperators (ops, source, target_type, look_for_explicit,
1153 Expression.MethodLookup (
1154 container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1161 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1162 if (most_specific_source == null)
1165 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1166 if (most_specific_target == null)
1169 MethodInfo method = null;
1171 foreach (MethodInfo m in ops) {
1172 if (TypeManager.TypeToCoreType (m.ReturnType) != most_specific_target)
1174 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1176 // Ambiguous: more than one conversion operator satisfies the signature.
1185 static DoubleHash explicit_conv = new DoubleHash (100);
1186 static DoubleHash implicit_conv = new DoubleHash (100);
1189 /// User-defined conversions
1191 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1192 Type target, Location loc,
1193 bool look_for_explicit)
1195 Type source_type = source.Type;
1196 MethodInfo method = null;
1199 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1201 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1202 method = (MethodInfo) o;
1204 method = GetConversionOperator (null, source, target, look_for_explicit);
1205 if (!(source is Constant))
1206 hash.Insert (source_type, target, method);
1212 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1215 // This will do the conversion to the best match that we
1216 // found. Now we need to perform an implict standard conversion
1217 // if the best match was not the type that we were requested
1220 if (look_for_explicit)
1221 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1223 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1229 e = new UserCast (method, source, loc);
1230 if (e.Type != target){
1231 if (!look_for_explicit)
1232 e = ImplicitConversionStandard (ec, e, target, loc);
1234 e = ExplicitConversionStandard (ec, e, target, loc);
1241 /// Converts implicitly the resolved expression `expr' into the
1242 /// `target_type'. It returns a new expression that can be used
1243 /// in a context that expects a `target_type'.
1245 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1246 Type target_type, Location loc)
1250 if (target_type == null)
1251 throw new Exception ("Target type is null");
1253 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1257 e = ImplicitUserConversion (ec, expr, target_type, loc);
1266 /// Attempts to apply the `Standard Implicit
1267 /// Conversion' rules to the expression `expr' into
1268 /// the `target_type'. It returns a new expression
1269 /// that can be used in a context that expects a
1272 /// This is different from `ImplicitConversion' in that the
1273 /// user defined implicit conversions are excluded.
1275 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1276 Type target_type, Location loc)
1278 Type expr_type = expr.Type;
1282 if (TypeManager.IsNullableType (target_type)) {
1284 // From null to any nullable type
1286 if (expr_type == TypeManager.null_type)
1287 return new Nullable.Null (target_type, loc);
1289 Type target = TypeManager.GetTypeArguments (target_type) [0];
1291 if (TypeManager.IsNullableType (expr.Type)) {
1292 e = new Nullable.LiftedConversion (
1293 expr, target_type, false, false, loc).Resolve (ec);
1297 e = ImplicitConversion (ec, expr, target, loc);
1299 return Nullable.Wrap.Create (e, target_type);
1303 if (expr.eclass == ExprClass.MethodGroup){
1304 if (!TypeManager.IsDelegateType (target_type)){
1309 // Only allow anonymous method conversions on post ISO_1
1311 if (RootContext.Version != LanguageVersion.ISO_1){
1312 MethodGroupExpr mg = expr as MethodGroupExpr;
1314 return ImplicitDelegateCreation.Create (
1315 ec, mg, target_type, loc);
1319 if (expr_type.Equals (target_type) && expr_type != TypeManager.null_type)
1323 // Attempt to do the implicit constant expression conversions
1325 Constant c = expr as Constant;
1328 // If `target_type' is an interface and the type of `ic' implements the interface
1329 // e.g. target_type is IComparable, IConvertible, IFormattable
1331 if (c.Type == TypeManager.int32_type && target_type.IsInterface && target_type.IsAssignableFrom (c.Type))
1332 return new BoxedCast (c, target_type);
1335 c = c.ConvertImplicitly (target_type);
1337 Console.WriteLine ("Conversion error happened in line {0}", loc);
1344 e = ImplicitNumericConversion (expr, target_type);
1348 e = ImplicitReferenceConversion (expr, target_type);
1352 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1353 IntLiteral i = (IntLiteral) expr;
1356 return new EnumConstant ((Constant) expr, target_type);
1360 if (expr_type.IsPointer){
1361 if (target_type == TypeManager.void_ptr_type)
1362 return EmptyCast.Create (expr, target_type);
1365 // yep, comparing pointer types cant be done with
1366 // t1 == t2, we have to compare their element types.
1368 if (target_type.IsPointer){
1369 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1376 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1377 return EmptyCast.Create (NullPointer.Null, target_type);
1380 if (expr_type == TypeManager.anonymous_method_type){
1381 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1382 Expression am = ame.Compatible (ec, target_type);
1384 return am.DoResolve (ec);
1391 /// Attempts to implicitly convert `source' into `target_type', using
1392 /// ImplicitConversion. If there is no implicit conversion, then
1393 /// an error is signaled
1395 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1396 Type target_type, Location loc)
1398 Expression e = ImplicitConversion (ec, source, target_type, loc);
1402 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1407 /// Performs the explicit numeric conversions
1409 /// There are a few conversions that are not part of the C# standard,
1410 /// they were interim hacks in the C# compiler that were supposed to
1411 /// become explicit operators in the UIntPtr class and IntPtr class,
1412 /// but for historical reasons it did not happen, so the C# compiler
1413 /// ended up with these special hacks.
1415 /// See bug 59800 for details.
1417 /// The conversion are:
1427 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1429 Type expr_type = expr.Type;
1430 Type real_target_type = target_type;
1432 if (expr_type == TypeManager.sbyte_type){
1434 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1436 if (real_target_type == TypeManager.byte_type)
1437 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1438 if (real_target_type == TypeManager.ushort_type)
1439 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1440 if (real_target_type == TypeManager.uint32_type)
1441 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1442 if (real_target_type == TypeManager.uint64_type)
1443 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1444 if (real_target_type == TypeManager.char_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1447 // One of the built-in conversions that belonged in the class library
1448 if (real_target_type == TypeManager.uintptr_type){
1449 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1451 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1453 } else if (expr_type == TypeManager.byte_type){
1455 // From byte to sbyte and char
1457 if (real_target_type == TypeManager.sbyte_type)
1458 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1459 if (real_target_type == TypeManager.char_type)
1460 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1461 } else if (expr_type == TypeManager.short_type){
1463 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1465 if (real_target_type == TypeManager.sbyte_type)
1466 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1467 if (real_target_type == TypeManager.byte_type)
1468 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1469 if (real_target_type == TypeManager.ushort_type)
1470 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1471 if (real_target_type == TypeManager.uint32_type)
1472 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1473 if (real_target_type == TypeManager.uint64_type)
1474 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1475 if (real_target_type == TypeManager.char_type)
1476 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1478 // One of the built-in conversions that belonged in the class library
1479 if (real_target_type == TypeManager.uintptr_type){
1480 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1482 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1484 } else if (expr_type == TypeManager.ushort_type){
1486 // From ushort to sbyte, byte, short, char
1488 if (real_target_type == TypeManager.sbyte_type)
1489 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1490 if (real_target_type == TypeManager.byte_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1492 if (real_target_type == TypeManager.short_type)
1493 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1494 if (real_target_type == TypeManager.char_type)
1495 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1496 } else if (expr_type == TypeManager.int32_type){
1498 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1500 if (real_target_type == TypeManager.sbyte_type)
1501 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1502 if (real_target_type == TypeManager.byte_type)
1503 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1504 if (real_target_type == TypeManager.short_type)
1505 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1506 if (real_target_type == TypeManager.ushort_type)
1507 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1508 if (real_target_type == TypeManager.uint32_type)
1509 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1510 if (real_target_type == TypeManager.uint64_type)
1511 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1512 if (real_target_type == TypeManager.char_type)
1513 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1515 // One of the built-in conversions that belonged in the class library
1516 if (real_target_type == TypeManager.uintptr_type){
1517 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1519 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1521 } else if (expr_type == TypeManager.uint32_type){
1523 // From uint to sbyte, byte, short, ushort, int, char
1525 if (real_target_type == TypeManager.sbyte_type)
1526 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1527 if (real_target_type == TypeManager.byte_type)
1528 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1529 if (real_target_type == TypeManager.short_type)
1530 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1531 if (real_target_type == TypeManager.ushort_type)
1532 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1533 if (real_target_type == TypeManager.int32_type)
1534 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1535 if (real_target_type == TypeManager.char_type)
1536 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1537 } else if (expr_type == TypeManager.int64_type){
1539 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1541 if (real_target_type == TypeManager.sbyte_type)
1542 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1543 if (real_target_type == TypeManager.byte_type)
1544 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1545 if (real_target_type == TypeManager.short_type)
1546 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1547 if (real_target_type == TypeManager.ushort_type)
1548 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1549 if (real_target_type == TypeManager.int32_type)
1550 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1551 if (real_target_type == TypeManager.uint32_type)
1552 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1553 if (real_target_type == TypeManager.uint64_type)
1554 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1555 if (real_target_type == TypeManager.char_type)
1556 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1557 } else if (expr_type == TypeManager.uint64_type){
1559 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1561 if (real_target_type == TypeManager.sbyte_type)
1562 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1563 if (real_target_type == TypeManager.byte_type)
1564 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1565 if (real_target_type == TypeManager.short_type)
1566 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1567 if (real_target_type == TypeManager.ushort_type)
1568 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1569 if (real_target_type == TypeManager.int32_type)
1570 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1571 if (real_target_type == TypeManager.uint32_type)
1572 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1573 if (real_target_type == TypeManager.int64_type)
1574 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1575 if (real_target_type == TypeManager.char_type)
1576 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1578 // One of the built-in conversions that belonged in the class library
1579 if (real_target_type == TypeManager.intptr_type){
1580 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1581 TypeManager.intptr_type, true);
1583 } else if (expr_type == TypeManager.char_type){
1585 // From char to sbyte, byte, short
1587 if (real_target_type == TypeManager.sbyte_type)
1588 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1589 if (real_target_type == TypeManager.byte_type)
1590 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1591 if (real_target_type == TypeManager.short_type)
1592 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1593 } else if (expr_type == TypeManager.float_type){
1595 // From float to sbyte, byte, short,
1596 // ushort, int, uint, long, ulong, char
1599 if (real_target_type == TypeManager.sbyte_type)
1600 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1601 if (real_target_type == TypeManager.byte_type)
1602 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1603 if (real_target_type == TypeManager.short_type)
1604 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1605 if (real_target_type == TypeManager.ushort_type)
1606 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1607 if (real_target_type == TypeManager.int32_type)
1608 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1609 if (real_target_type == TypeManager.uint32_type)
1610 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1611 if (real_target_type == TypeManager.int64_type)
1612 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1613 if (real_target_type == TypeManager.uint64_type)
1614 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1615 if (real_target_type == TypeManager.char_type)
1616 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1617 if (real_target_type == TypeManager.decimal_type)
1618 return new CastToDecimal (expr, true);
1619 } else if (expr_type == TypeManager.double_type){
1621 // From double to sbyte, byte, short,
1622 // ushort, int, uint, long, ulong,
1623 // char, float or decimal
1625 if (real_target_type == TypeManager.sbyte_type)
1626 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1627 if (real_target_type == TypeManager.byte_type)
1628 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1629 if (real_target_type == TypeManager.short_type)
1630 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1631 if (real_target_type == TypeManager.ushort_type)
1632 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1633 if (real_target_type == TypeManager.int32_type)
1634 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1635 if (real_target_type == TypeManager.uint32_type)
1636 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1637 if (real_target_type == TypeManager.int64_type)
1638 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1639 if (real_target_type == TypeManager.uint64_type)
1640 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1641 if (real_target_type == TypeManager.char_type)
1642 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1643 if (real_target_type == TypeManager.float_type)
1644 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1645 if (real_target_type == TypeManager.decimal_type)
1646 return new CastToDecimal (expr, true);
1647 } else if (expr_type == TypeManager.uintptr_type){
1649 // Various built-in conversions that belonged in the class library
1651 // from uintptr to sbyte, short, int32
1653 if (real_target_type == TypeManager.sbyte_type){
1654 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1655 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1657 if (real_target_type == TypeManager.short_type){
1658 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1659 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1661 if (real_target_type == TypeManager.int32_type){
1662 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1663 TypeManager.int32_type);
1665 } else if (expr_type == TypeManager.intptr_type){
1666 if (real_target_type == TypeManager.uint64_type){
1667 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1668 TypeManager.uint64_type);
1670 } else if (expr_type == TypeManager.decimal_type) {
1671 return new CastFromDecimal (expr, target_type).Resolve ();
1677 /// Returns whether an explicit reference conversion can be performed
1678 /// from source_type to target_type
1680 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1682 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1683 bool target_is_value_type = target_type.IsValueType;
1685 if (source_type == target_type)
1689 // From generic parameter to any type
1691 if (TypeManager.IsGenericParameter (source_type))
1692 return ExplicitTypeParameterConversionExists (source_type, target_type);
1695 // From object to a generic parameter
1697 if (source_type == TypeManager.object_type && target_is_type_param)
1701 // From object to any reference type
1703 if (source_type == TypeManager.object_type && !target_is_value_type)
1707 // From any class S to any class-type T, provided S is a base class of T
1709 if (TypeManager.IsSubclassOf (target_type, source_type))
1713 // From any interface type S to any interface T provided S is not derived from T
1715 if (source_type.IsInterface && target_type.IsInterface){
1716 if (!TypeManager.IsSubclassOf (target_type, source_type))
1721 // From any class type S to any interface T, provided S is not sealed
1722 // and provided S does not implement T.
1724 if (target_type.IsInterface && !source_type.IsSealed &&
1725 !TypeManager.ImplementsInterface (source_type, target_type))
1729 // From any interface-type S to to any class type T, provided T is not
1730 // sealed, or provided T implements S.
1732 if (source_type.IsInterface &&
1733 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1737 if (source_type.IsInterface && IList_To_Array (source_type, target_type))
1740 // From an array type S with an element type Se to an array type T with an
1741 // element type Te provided all the following are true:
1742 // * S and T differe only in element type, in other words, S and T
1743 // have the same number of dimensions.
1744 // * Both Se and Te are reference types
1745 // * An explicit referenc conversions exist from Se to Te
1747 if (source_type.IsArray && target_type.IsArray) {
1748 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1750 Type source_element_type = TypeManager.GetElementType (source_type);
1751 Type target_element_type = TypeManager.GetElementType (target_type);
1753 if (TypeManager.IsGenericParameter (source_element_type) ||
1754 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1755 if (ExplicitReferenceConversionExists (source_element_type,
1756 target_element_type))
1762 // From System.Array to any array-type
1763 if (source_type == TypeManager.array_type &&
1764 target_type.IsArray){
1769 // From System delegate to any delegate-type
1771 if (source_type == TypeManager.delegate_type &&
1772 TypeManager.IsDelegateType (target_type))
1779 /// Implements Explicit Reference conversions
1781 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1783 Type source_type = source.Type;
1784 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1785 bool target_is_value_type = target_type.IsValueType;
1788 // From object to a generic parameter
1790 if (source_type == TypeManager.object_type && target_is_type_param)
1791 return new UnboxCast (source, target_type);
1794 // Explicit type parameter conversion.
1797 if (TypeManager.IsGenericParameter (source_type))
1798 return ExplicitTypeParameterConversion (source, target_type);
1801 // From object to any reference type
1803 if (source_type == TypeManager.object_type && !target_is_value_type)
1804 return new ClassCast (source, target_type);
1807 // Unboxing conversion.
1809 if (((source_type == TypeManager.enum_type &&
1810 !(source is EmptyCast)) ||
1811 source_type == TypeManager.value_type) && target_is_value_type)
1812 return new UnboxCast (source, target_type);
1815 // From any class S to any class-type T, provided S is a base class of T
1817 if (TypeManager.IsSubclassOf (target_type, source_type))
1818 return new ClassCast (source, target_type);
1821 // From any interface type S to any interface T provided S is not derived from T
1823 if (source_type.IsInterface && target_type.IsInterface){
1824 if (TypeManager.ImplementsInterface (source_type, target_type))
1827 return new ClassCast (source, target_type);
1831 // From any class type S to any interface T, provides S is not sealed
1832 // and provided S does not implement T.
1834 if (target_type.IsInterface && !source_type.IsSealed) {
1835 if (TypeManager.ImplementsInterface (source_type, target_type))
1838 return new ClassCast (source, target_type);
1843 // From any interface-type S to to any class type T, provided T is not
1844 // sealed, or provided T implements S.
1846 if (source_type.IsInterface) {
1847 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1848 if (target_type.IsClass)
1849 return new ClassCast (source, target_type);
1851 return new UnboxCast (source, target_type);
1855 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1856 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1858 if (IList_To_Array (source_type, target_type))
1859 return new ClassCast (source, target_type);
1864 // From an array type S with an element type Se to an array type T with an
1865 // element type Te provided all the following are true:
1866 // * S and T differe only in element type, in other words, S and T
1867 // have the same number of dimensions.
1868 // * Both Se and Te are reference types
1869 // * An explicit referenc conversions exist from Se to Te
1871 if (source_type.IsArray && target_type.IsArray) {
1872 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1874 Type source_element_type = TypeManager.GetElementType (source_type);
1875 Type target_element_type = TypeManager.GetElementType (target_type);
1877 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1878 if (ExplicitReferenceConversionExists (source_element_type,
1879 target_element_type))
1880 return new ClassCast (source, target_type);
1885 // From System.Array to any array-type
1886 if (source_type == TypeManager.array_type &&
1887 target_type.IsArray) {
1888 return new ClassCast (source, target_type);
1892 // From System delegate to any delegate-type
1894 if (source_type == TypeManager.delegate_type &&
1895 TypeManager.IsDelegateType (target_type))
1896 return new ClassCast (source, target_type);
1902 /// Performs an explicit conversion of the expression `expr' whose
1903 /// type is expr.Type to `target_type'.
1905 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1906 Type target_type, Location loc)
1908 Type expr_type = expr.Type;
1910 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1911 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1916 // Unboxing conversions; only object types can be convertible to enum
1918 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1919 return new UnboxCast (expr, target_type);
1921 if (TypeManager.IsEnumType (expr_type)) {
1922 Expression underlying = EmptyCast.Create (expr, TypeManager.GetEnumUnderlyingType (expr_type));
1923 expr = ExplicitConversionCore (ec, underlying, target_type, loc);
1927 return ExplicitUserConversion (ec, underlying, target_type, loc);
1930 if (TypeManager.IsEnumType (target_type)){
1931 if (expr_type == TypeManager.enum_type)
1932 return new UnboxCast (expr, target_type);
1934 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.GetEnumUnderlyingType (target_type), loc);
1936 return EmptyCast.Create (ce, target_type);
1939 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1941 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1942 ne = ExplicitUserConversion (ec, expr, TypeManager.GetEnumUnderlyingType (target_type), loc);
1944 return ExplicitConversionCore (ec, ne, target_type, loc);
1950 ne = ExplicitNumericConversion (expr, target_type);
1955 // Skip the ExplicitReferenceConversion because we can not convert
1956 // from Null to a ValueType, and ExplicitReference wont check against
1957 // null literal explicitly
1959 if (expr_type != TypeManager.null_type){
1960 ne = ExplicitReferenceConversion (expr, target_type);
1966 ne = ExplicitUnsafe (expr, target_type);
1974 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1976 Type expr_type = expr.Type;
1978 if (target_type.IsPointer){
1979 if (expr_type.IsPointer)
1980 return EmptyCast.Create (expr, target_type);
1982 if (expr_type == TypeManager.sbyte_type ||
1983 expr_type == TypeManager.short_type ||
1984 expr_type == TypeManager.int32_type ||
1985 expr_type == TypeManager.int64_type)
1986 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1988 if (expr_type == TypeManager.ushort_type ||
1989 expr_type == TypeManager.uint32_type ||
1990 expr_type == TypeManager.uint64_type ||
1991 expr_type == TypeManager.byte_type)
1992 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1995 if (expr_type.IsPointer){
1996 if (target_type == TypeManager.sbyte_type)
1997 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1998 else if (target_type == TypeManager.byte_type)
1999 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2000 else if (target_type == TypeManager.short_type)
2001 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2002 else if (target_type == TypeManager.ushort_type)
2003 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2004 else if (target_type == TypeManager.int32_type)
2005 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2006 else if (target_type == TypeManager.uint32_type)
2007 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2008 else if (target_type == TypeManager.uint64_type)
2009 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2010 else if (target_type == TypeManager.int64_type){
2011 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2018 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2020 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2021 Type target_type, Location l)
2023 int errors = Report.Errors;
2024 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2025 if (Report.Errors > errors)
2031 ne = ExplicitNumericConversion (expr, target_type);
2035 ne = ExplicitReferenceConversion (expr, target_type);
2039 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2040 return EmptyCast.Create (expr, target_type);
2042 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
2047 /// Performs an explicit conversion of the expression `expr' whose
2048 /// type is expr.Type to `target_type'.
2050 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2051 Type target_type, Location loc)
2055 Type expr_type = expr.Type;
2056 if (TypeManager.IsNullableType (target_type)) {
2057 if (TypeManager.IsNullableType (expr_type)) {
2058 e = new Nullable.LiftedConversion (
2059 expr, target_type, false, true, loc).Resolve (ec);
2062 } else if (expr_type == TypeManager.object_type) {
2063 return new UnboxCast (expr, target_type);
2065 Type target = TypeManager.GetTypeArguments (target_type) [0];
2067 e = ExplicitConversionCore (ec, expr, target, loc);
2069 return Nullable.Wrap.Create (e, target_type);
2071 } else if (TypeManager.IsNullableType (expr_type)) {
2072 e = ExplicitConversion (ec, Nullable.Unwrap.Create (expr, ec), target_type, loc);
2074 e = EmptyCast.Create (e, target_type);
2078 e = ExplicitConversionCore (ec, expr, target_type, loc);
2082 e = ExplicitUserConversion (ec, expr, target_type, loc);
2086 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);