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 // Copyright 2001, 2002, 2003 Ximian, Inc.
10 // Copyright 2003-2008 Novell, Inc.
14 using System
.Collections
.Generic
;
15 using System
.Diagnostics
;
16 using System
.Reflection
;
17 using System
.Reflection
.Emit
;
19 namespace Mono
.CSharp
{
22 // A container class for all the conversion operations
24 static class Convert
{
26 static EmptyExpression MyEmptyExpr
;
27 static DoubleHash explicit_conv
;
28 static DoubleHash implicit_conv
;
35 public static void Reset ()
38 explicit_conv
= new DoubleHash (100);
39 implicit_conv
= new DoubleHash (100);
42 static Type
TypeParam_EffectiveBaseType (GenericConstraints gc
)
44 var list
= new List
<Type
> ();
45 list
.Add (gc
.EffectiveBaseClass
);
46 foreach (Type t
in gc
.InterfaceConstraints
) {
47 if (!TypeManager
.IsGenericParameter (t
))
50 GenericConstraints new_gc
= TypeManager
.GetTypeParameterConstraints (t
);
52 list
.Add (TypeParam_EffectiveBaseType (new_gc
));
54 return FindMostEncompassedType (list
);
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
, bool isExplicit
)
64 if ((array
.GetArrayRank () != 1) || !TypeManager
.IsGenericType (list
))
67 Type gt
= TypeManager
.DropGenericTypeArguments (list
);
68 if ((gt
!= TypeManager
.generic_ilist_type
) &&
69 (gt
!= TypeManager
.generic_icollection_type
) &&
70 (gt
!= TypeManager
.generic_ienumerable_type
))
73 Type element_type
= TypeManager
.GetElementType (array
);
74 Type arg_type
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (list
) [0]);
76 if (element_type
== arg_type
)
80 return ExplicitReferenceConversionExists (element_type
, arg_type
);
82 Type t
= TypeManager
.GetElementType (array
);
83 if (MyEmptyExpr
== null)
84 MyEmptyExpr
= new EmptyExpression (t
);
86 MyEmptyExpr
.SetType (t
);
88 return ImplicitReferenceConversionExists (MyEmptyExpr
, arg_type
);
91 static bool IList_To_Array(Type list
, Type array
)
93 if (!TypeManager
.IsGenericType (list
) || !array
.IsArray
|| array
.GetArrayRank() != 1)
96 Type gt
= TypeManager
.DropGenericTypeArguments (list
);
97 if (gt
!= TypeManager
.generic_ilist_type
&&
98 gt
!= TypeManager
.generic_icollection_type
&&
99 gt
!= TypeManager
.generic_ienumerable_type
)
102 Type arg_type
= TypeManager
.TypeToCoreType (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(element_type
);
111 MyEmptyExpr
.SetType(element_type
);
113 return ImplicitReferenceConversionExists(MyEmptyExpr
, arg_type
) || ExplicitReferenceConversionExists(element_type
, arg_type
);
116 static Expression
ImplicitTypeParameterConversion (Expression expr
,
119 Type expr_type
= expr
.Type
;
121 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (expr_type
);
124 if (target_type
== TypeManager
.object_type
)
125 return new BoxedCast (expr
, target_type
);
130 // We're converting from a type parameter which is known to be a reference type.
131 Type base_type
= TypeParam_EffectiveBaseType (gc
);
133 if (TypeManager
.IsSubclassOf (base_type
, target_type
))
134 return new ClassCast (expr
, target_type
);
136 if (target_type
.IsInterface
) {
137 if (TypeManager
.ImplementsInterface (base_type
, target_type
))
138 return new ClassCast (expr
, target_type
);
140 foreach (Type t
in gc
.InterfaceConstraints
) {
141 if (TypeManager
.IsSubclassOf (t
, target_type
))
142 return new ClassCast (expr
, target_type
);
143 if (TypeManager
.ImplementsInterface (t
, target_type
))
144 return new ClassCast (expr
, target_type
);
148 foreach (Type t
in gc
.InterfaceConstraints
) {
149 if (!TypeManager
.IsGenericParameter (t
))
151 if (TypeManager
.IsSubclassOf (t
, target_type
))
152 return new ClassCast (expr
, target_type
);
153 if (TypeManager
.ImplementsInterface (t
, target_type
))
154 return new ClassCast (expr
, target_type
);
160 static bool ImplicitTypeParameterBoxingConversion (Type expr_type
, Type target_type
,
161 out bool use_class_cast
)
163 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (expr_type
);
166 use_class_cast
= false;
167 return target_type
== TypeManager
.object_type
;
170 use_class_cast
= true;
172 if (!gc
.HasReferenceTypeConstraint
)
175 // We're converting from a type parameter which is known to be a reference type.
176 Type base_type
= TypeParam_EffectiveBaseType (gc
);
178 if (TypeManager
.IsSubclassOf (base_type
, target_type
))
181 if (target_type
.IsInterface
) {
182 if (TypeManager
.ImplementsInterface (base_type
, target_type
))
185 foreach (Type t
in gc
.InterfaceConstraints
) {
186 if (TypeManager
.IsSubclassOf (t
, target_type
))
188 if (TypeManager
.ImplementsInterface (t
, target_type
))
193 foreach (Type t
in gc
.InterfaceConstraints
) {
194 if (!TypeManager
.IsGenericParameter (t
))
196 if (TypeManager
.IsSubclassOf (t
, target_type
))
198 if (TypeManager
.ImplementsInterface (t
, target_type
))
202 use_class_cast
= false;
206 static Expression
ExplicitTypeParameterConversion (Expression source
, Type source_type
, Type target_type
)
208 if (TypeManager
.IsGenericParameter (target_type
)) {
209 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (target_type
);
213 foreach (Type iface
in gc
.InterfaceConstraints
) {
214 if (!TypeManager
.IsGenericParameter (iface
))
217 if (TypeManager
.IsSubclassOf (source_type
, iface
))
218 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
, true);
222 if (target_type
.IsInterface
)
223 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
, true);
228 static Expression
ImplicitReferenceConversion (Expression expr
, Type target_type
, bool explicit_cast
)
230 Type expr_type
= expr
.Type
;
232 if (expr_type
== null && expr
.eclass
== ExprClass
.MethodGroup
){
233 // if we are a method group, emit a warning
238 if (expr_type
== TypeManager
.void_type
)
241 if (TypeManager
.IsGenericParameter (expr_type
))
242 return ImplicitTypeParameterConversion (expr
, target_type
);
245 // from the null type to any reference-type.
247 NullLiteral nl
= expr
as NullLiteral
;
249 return nl
.ConvertImplicitly (null, target_type
);
252 if (ImplicitReferenceConversionExists (expr
, target_type
)) {
254 // Avoid wrapping implicitly convertible reference type
259 return EmptyCast
.Create (expr
, target_type
);
263 if (ImplicitBoxingConversionExists (expr
, target_type
, out use_class_cast
)) {
265 return new ClassCast (expr
, target_type
);
267 return new BoxedCast (expr
, target_type
);
274 // 6.1.6 Implicit reference conversions
276 public static bool ImplicitReferenceConversionExists (Expression expr
, Type target_type
)
278 if (TypeManager
.IsStruct (target_type
))
281 Type expr_type
= expr
.Type
;
283 // from the null type to any reference-type.
284 if (expr_type
== TypeManager
.null_type
)
285 return target_type
!= InternalType
.AnonymousMethod
;
287 if (TypeManager
.IsGenericParameter (expr_type
))
288 return ImplicitTypeParameterConversion (expr
, target_type
) != null;
291 // notice that it is possible to write "ValueType v = 1", the ValueType here
292 // is an abstract class, and not really a value type, so we apply the same rules.
294 if (target_type
== TypeManager
.object_type
|| TypeManager
.IsDynamicType (target_type
)) {
296 // A pointer type cannot be converted to object
298 if (expr_type
.IsPointer
)
301 if (TypeManager
.IsValueType (expr_type
))
304 if (expr_type
.IsClass
|| expr_type
.IsInterface
|| expr_type
== TypeManager
.enum_type
){
305 // No mcs internal types are convertible
306 return expr_type
.Module
!= typeof (Convert
).Module
;
310 } else if (target_type
== TypeManager
.value_type
) {
311 return expr_type
== TypeManager
.enum_type
;
312 } else if (TypeManager
.IsSubclassOf (expr_type
, target_type
)) {
314 // Special case: enumeration to System.Enum.
315 // System.Enum is not a value type, it is a class, so we need
316 // a boxing conversion
318 if (target_type
== TypeManager
.enum_type
|| TypeManager
.IsGenericParameter (expr_type
))
324 // This code is kind of mirrored inside ImplicitStandardConversionExists
325 // with the small distinction that we only probe there
327 // Always ensure that the code here and there is in sync
329 // from any class-type S to any interface-type T.
330 if (target_type
.IsInterface
) {
331 if (TypeManager
.ImplementsInterface (expr_type
, target_type
)){
332 return !TypeManager
.IsGenericParameter (expr_type
) &&
333 !TypeManager
.IsValueType (expr_type
);
337 if (expr_type
.IsArray
) {
338 // from an array-type S to an array-type of type T
339 if (target_type
.IsArray
&& expr_type
.GetArrayRank () == target_type
.GetArrayRank ()) {
342 // Both SE and TE are reference-types
344 Type expr_element_type
= TypeManager
.GetElementType (expr_type
);
345 if (!TypeManager
.IsReferenceType (expr_element_type
))
348 Type target_element_type
= TypeManager
.GetElementType (target_type
);
349 if (!TypeManager
.IsReferenceType (target_element_type
))
352 if (MyEmptyExpr
== null)
353 MyEmptyExpr
= new EmptyExpression (expr_element_type
);
355 MyEmptyExpr
.SetType (expr_element_type
);
357 return ImplicitStandardConversionExists (MyEmptyExpr
, target_element_type
);
360 // from an array-type to System.Array
361 if (target_type
== TypeManager
.array_type
)
364 // from an array-type of type T to IList<T>
365 if (Array_To_IList (expr_type
, target_type
, false))
371 if (TypeManager
.IsVariantOf (expr_type
, target_type
))
374 // from any interface type S to interface-type T.
375 if (expr_type
.IsInterface
&& target_type
.IsInterface
) {
376 return TypeManager
.ImplementsInterface (expr_type
, target_type
);
379 // from any delegate type to System.Delegate
380 if (target_type
== TypeManager
.delegate_type
&&
381 (expr_type
== TypeManager
.delegate_type
|| TypeManager
.IsDelegateType (expr_type
)))
384 if (TypeManager
.IsEqual (expr_type
, target_type
))
390 public static bool ImplicitBoxingConversionExists (Expression expr
, Type target_type
,
391 out bool use_class_cast
)
393 Type expr_type
= expr
.Type
;
394 use_class_cast
= false;
397 // From any value-type to the type object.
399 if (target_type
== TypeManager
.object_type
|| TypeManager
.IsDynamicType (target_type
)) {
401 // A pointer type cannot be converted to object
403 if (expr_type
.IsPointer
)
406 return TypeManager
.IsValueType (expr_type
);
410 // From any value-type to the type System.ValueType.
412 if (target_type
== TypeManager
.value_type
)
413 return TypeManager
.IsValueType (expr_type
);
415 if (target_type
== TypeManager
.enum_type
) {
417 // From any enum-type to the type System.Enum.
419 if (TypeManager
.IsEnumType (expr_type
))
422 // From any nullable-type with an underlying enum-type to the type System.Enum
424 if (TypeManager
.IsNullableType (expr_type
))
425 return TypeManager
.IsEnumType (TypeManager
.GetTypeArguments (expr_type
) [0]);
428 if (TypeManager
.IsSubclassOf (expr_type
, target_type
)) {
430 // Don't box same type arguments
432 if (TypeManager
.IsGenericParameter (expr_type
) && expr_type
!= target_type
)
438 // This code is kind of mirrored inside ImplicitStandardConversionExists
439 // with the small distinction that we only probe there
441 // Always ensure that the code here and there is in sync
443 // from any class-type S to any interface-type T.
444 if (target_type
.IsInterface
) {
445 if (TypeManager
.ImplementsInterface (expr_type
, target_type
))
446 return TypeManager
.IsGenericParameter (expr_type
) ||
447 TypeManager
.IsValueType (expr_type
);
450 if (TypeManager
.IsGenericParameter (expr_type
))
451 return ImplicitTypeParameterBoxingConversion (
452 expr_type
, target_type
, out use_class_cast
);
457 public static Expression
ImplicitNulableConversion (ResolveContext ec
, Expression expr
, Type target_type
)
459 Type expr_type
= expr
.Type
;
462 // From null to any nullable type
464 if (expr_type
== TypeManager
.null_type
)
465 return ec
== null ? EmptyExpression
.Null
: Nullable
.LiftedNull
.Create (target_type
, expr
.Location
);
468 Type t_el
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (target_type
)[0]);
471 if (TypeManager
.IsNullableType (expr_type
))
472 expr_type
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (expr_type
)[0]);
475 // Predefined implicit identity or implicit numeric conversion
476 // has to exist between underlying type S and underlying type T
481 if (expr_type
== t_el
)
482 return EmptyExpression
.Null
;
484 return ImplicitNumericConversion (null, expr_type
, t_el
);
488 if (expr_type
!= expr
.Type
)
489 unwrap
= Nullable
.Unwrap
.Create (expr
);
493 Expression conv
= expr_type
== t_el
? unwrap
: ImplicitNumericConversion (unwrap
, expr_type
, t_el
);
497 if (expr_type
!= expr
.Type
)
498 return new Nullable
.Lifted (conv
, unwrap
, target_type
).Resolve (ec
);
500 // Do constant optimization for S -> T?
501 if (unwrap
is Constant
)
502 conv
= ((Constant
) unwrap
).ConvertImplicitly (ec
, t_el
);
504 return Nullable
.Wrap
.Create (conv
, target_type
);
508 /// Implicit Numeric Conversions.
510 /// expr is the expression to convert, returns a new expression of type
511 /// target_type or null if an implicit conversion is not possible.
513 public static Expression
ImplicitNumericConversion (Expression expr
, Type target_type
)
515 return ImplicitNumericConversion (expr
, expr
.Type
, target_type
);
518 static Expression
ImplicitNumericConversion (Expression expr
, Type expr_type
, Type target_type
)
520 if (expr_type
== TypeManager
.sbyte_type
){
522 // From sbyte to short, int, long, float, double, decimal
524 if (target_type
== TypeManager
.int32_type
)
525 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I4
);
526 if (target_type
== TypeManager
.int64_type
)
527 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
528 if (target_type
== TypeManager
.double_type
)
529 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
530 if (target_type
== TypeManager
.float_type
)
531 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
532 if (target_type
== TypeManager
.short_type
)
533 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I2
);
534 if (target_type
== TypeManager
.decimal_type
)
535 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
536 } else if (expr_type
== TypeManager
.byte_type
){
538 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
540 if (target_type
== TypeManager
.int32_type
|| target_type
== TypeManager
.uint32_type
||
541 target_type
== TypeManager
.short_type
|| target_type
== TypeManager
.ushort_type
)
542 return expr
== null ? EmptyExpression
.Null
: EmptyCast
.Create (expr
, target_type
);
544 if (target_type
== TypeManager
.uint64_type
)
545 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
546 if (target_type
== TypeManager
.int64_type
)
547 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
548 if (target_type
== TypeManager
.float_type
)
549 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
550 if (target_type
== TypeManager
.double_type
)
551 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
552 if (target_type
== TypeManager
.decimal_type
)
553 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
555 } else if (expr_type
== TypeManager
.short_type
){
557 // From short to int, long, float, double, decimal
559 if (target_type
== TypeManager
.int32_type
)
560 return expr
== null ? EmptyExpression
.Null
: EmptyCast
.Create (expr
, target_type
);
561 if (target_type
== TypeManager
.int64_type
)
562 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
563 if (target_type
== TypeManager
.double_type
)
564 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
565 if (target_type
== TypeManager
.float_type
)
566 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
567 if (target_type
== TypeManager
.decimal_type
)
568 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
570 } else if (expr_type
== TypeManager
.ushort_type
){
572 // From ushort to int, uint, long, ulong, float, double, decimal
574 if (target_type
== TypeManager
.int32_type
|| target_type
== TypeManager
.uint32_type
)
575 return expr
== null ? EmptyExpression
.Null
: EmptyCast
.Create (expr
, target_type
);
577 if (target_type
== TypeManager
.uint64_type
)
578 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
579 if (target_type
== TypeManager
.int64_type
)
580 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
581 if (target_type
== TypeManager
.double_type
)
582 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
583 if (target_type
== TypeManager
.float_type
)
584 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
585 if (target_type
== TypeManager
.decimal_type
)
586 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
587 } else if (expr_type
== TypeManager
.int32_type
){
589 // From int to long, float, double, decimal
591 if (target_type
== TypeManager
.int64_type
)
592 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
593 if (target_type
== TypeManager
.double_type
)
594 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
595 if (target_type
== TypeManager
.float_type
)
596 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
597 if (target_type
== TypeManager
.decimal_type
)
598 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
599 } else if (expr_type
== TypeManager
.uint32_type
){
601 // From uint to long, ulong, float, double, decimal
603 if (target_type
== TypeManager
.int64_type
)
604 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
605 if (target_type
== TypeManager
.uint64_type
)
606 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
607 if (target_type
== TypeManager
.double_type
)
608 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
), target_type
, OpCodes
.Conv_R8
);
609 if (target_type
== TypeManager
.float_type
)
610 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
), target_type
, OpCodes
.Conv_R4
);
611 if (target_type
== TypeManager
.decimal_type
)
612 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
613 } else if (expr_type
== TypeManager
.int64_type
){
615 // From long/ulong to float, double
617 if (target_type
== TypeManager
.double_type
)
618 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
619 if (target_type
== TypeManager
.float_type
)
620 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
621 if (target_type
== TypeManager
.decimal_type
)
622 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
623 } else if (expr_type
== TypeManager
.uint64_type
){
625 // From ulong to float, double
627 if (target_type
== TypeManager
.double_type
)
628 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
), target_type
, OpCodes
.Conv_R8
);
629 if (target_type
== TypeManager
.float_type
)
630 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
), target_type
, OpCodes
.Conv_R4
);
631 if (target_type
== TypeManager
.decimal_type
)
632 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
633 } else if (expr_type
== TypeManager
.char_type
){
635 // From char to ushort, int, uint, long, ulong, float, double, decimal
637 if ((target_type
== TypeManager
.ushort_type
) ||
638 (target_type
== TypeManager
.int32_type
) ||
639 (target_type
== TypeManager
.uint32_type
))
640 return expr
== null ? EmptyExpression
.Null
: EmptyCast
.Create (expr
, target_type
);
641 if (target_type
== TypeManager
.uint64_type
)
642 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
643 if (target_type
== TypeManager
.int64_type
)
644 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
645 if (target_type
== TypeManager
.float_type
)
646 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
647 if (target_type
== TypeManager
.double_type
)
648 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
649 if (target_type
== TypeManager
.decimal_type
)
650 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
651 } else if (expr_type
== TypeManager
.float_type
){
655 if (target_type
== TypeManager
.double_type
)
656 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
663 /// Same as ImplicitStandardConversionExists except that it also looks at
664 /// implicit user defined conversions - needed for overload resolution
666 public static bool ImplicitConversionExists (ResolveContext ec
, Expression expr
, Type target_type
)
668 if (ImplicitStandardConversionExists (expr
, target_type
))
671 if (expr
.Type
== InternalType
.AnonymousMethod
) {
672 if (!TypeManager
.IsDelegateType (target_type
) &&
673 TypeManager
.DropGenericTypeArguments (target_type
) != TypeManager
.expression_type
)
676 AnonymousMethodExpression ame
= (AnonymousMethodExpression
) expr
;
677 return ame
.ImplicitStandardConversionExists (ec
, target_type
);
680 if (expr
.eclass
== ExprClass
.MethodGroup
) {
681 if (TypeManager
.IsDelegateType (target_type
) && RootContext
.Version
!= LanguageVersion
.ISO_1
) {
682 MethodGroupExpr mg
= expr
as MethodGroupExpr
;
684 return DelegateCreation
.ImplicitStandardConversionExists (ec
, mg
, target_type
);
690 return ImplicitUserConversion (ec
, expr
, target_type
, Location
.Null
) != null;
694 /// Determines if a standard implicit conversion exists from
695 /// expr_type to target_type
697 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
699 public static bool ImplicitStandardConversionExists (Expression expr
, Type target_type
)
701 Type expr_type
= expr
.Type
;
703 if (expr_type
== TypeManager
.null_type
) {
704 NullLiteral nl
= expr
as NullLiteral
;
706 return nl
.ConvertImplicitly (null, target_type
) != null;
709 if (expr_type
== TypeManager
.void_type
)
712 if (TypeManager
.IsEqual (expr_type
, target_type
))
715 if (TypeManager
.IsNullableType (target_type
)) {
716 return ImplicitNulableConversion (null, expr
, target_type
) != null;
719 // First numeric conversions
720 if (ImplicitNumericConversion (null, expr_type
, target_type
) != null)
723 if (ImplicitReferenceConversionExists (expr
, target_type
))
727 if (ImplicitBoxingConversionExists (expr
, target_type
, out use_class_cast
))
731 // Implicit Constant Expression Conversions
733 if (expr
is IntConstant
){
734 int value = ((IntConstant
) expr
).Value
;
736 if (target_type
== TypeManager
.sbyte_type
){
737 if (value >= SByte
.MinValue
&& value <= SByte
.MaxValue
)
739 } else if (target_type
== TypeManager
.byte_type
){
740 if (value >= 0 && value <= Byte
.MaxValue
)
742 } else if (target_type
== TypeManager
.short_type
){
743 if (value >= Int16
.MinValue
&& value <= Int16
.MaxValue
)
745 } else if (target_type
== TypeManager
.ushort_type
){
746 if (value >= UInt16
.MinValue
&& value <= UInt16
.MaxValue
)
748 } else if (target_type
== TypeManager
.uint32_type
){
751 } else if (target_type
== TypeManager
.uint64_type
){
753 // we can optimize this case: a positive int32
754 // always fits on a uint64. But we need an opcode
761 if (value == 0 && TypeManager
.IsEnumType (target_type
))
765 if (expr
is LongConstant
&& target_type
== TypeManager
.uint64_type
){
767 // Try the implicit constant expression conversion
768 // from long to ulong, instead of a nice routine,
771 long v
= ((LongConstant
) expr
).Value
;
777 // If `expr_type' implements `target_type' (which is an iface)
778 // see TryImplicitIntConversion
780 if (target_type
.IsInterface
&& TypeManager
.ImplementsInterface (expr_type
, target_type
))
783 if (target_type
== TypeManager
.void_ptr_type
&& expr_type
.IsPointer
)
786 // Conversion from __arglist to System.ArgIterator
787 if (expr_type
== InternalType
.Arglist
)
788 return target_type
== TypeManager
.arg_iterator_type
;
794 /// Finds "most encompassed type" according to the spec (13.4.2)
795 /// amongst the methods in the MethodGroupExpr
797 static Type
FindMostEncompassedType (IList
<Type
> types
)
801 if (types
.Count
== 0)
804 if (types
.Count
== 1)
805 return (Type
) types
[0];
807 EmptyExpression expr
= EmptyExpression
.Grab ();
809 foreach (Type t
in types
) {
816 if (ImplicitStandardConversionExists (expr
, best
))
821 foreach (Type t
in types
) {
824 if (!ImplicitStandardConversionExists (expr
, t
)) {
830 EmptyExpression
.Release (expr
);
836 /// Finds "most encompassing type" according to the spec (13.4.2)
837 /// amongst the types in the given set
839 static Type
FindMostEncompassingType (IList
<Type
> types
)
843 if (types
.Count
== 0)
846 if (types
.Count
== 1)
849 EmptyExpression expr
= EmptyExpression
.Grab ();
851 foreach (Type t
in types
) {
858 if (ImplicitStandardConversionExists (expr
, t
))
862 foreach (Type t
in types
) {
866 if (!ImplicitStandardConversionExists (expr
, best
)) {
872 EmptyExpression
.Release (expr
);
878 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
879 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
880 /// for explicit and implicit conversion operators.
882 static public Type
FindMostSpecificSource (IList
<MethodSpec
> list
,
883 Expression source
, bool apply_explicit_conv_rules
)
885 var src_types_set
= new List
<Type
> ();
888 // If any operator converts from S then Sx = S
890 Type source_type
= source
.Type
;
891 foreach (var mb
in list
){
892 Type param_type
= mb
.Parameters
.Types
[0];
894 if (param_type
== source_type
)
897 src_types_set
.Add (param_type
);
901 // Explicit Conv rules
903 if (apply_explicit_conv_rules
) {
904 var candidate_set
= new List
<Type
> ();
906 foreach (Type param_type
in src_types_set
){
907 if (ImplicitStandardConversionExists (source
, param_type
))
908 candidate_set
.Add (param_type
);
911 if (candidate_set
.Count
!= 0)
912 return FindMostEncompassedType (candidate_set
);
918 if (apply_explicit_conv_rules
)
919 return FindMostEncompassingType (src_types_set
);
921 return FindMostEncompassedType (src_types_set
);
925 /// Finds the most specific target Tx according to section 13.4.4
927 static public Type
FindMostSpecificTarget (IList
<MethodSpec
> list
,
928 Type target
, bool apply_explicit_conv_rules
)
930 var tgt_types_set
= new List
<Type
> ();
933 // If any operator converts to T then Tx = T
935 foreach (var mi
in list
){
936 Type ret_type
= TypeManager
.TypeToCoreType (mi
.ReturnType
);
937 if (ret_type
== target
)
940 tgt_types_set
.Add (ret_type
);
944 // Explicit conv rules
946 if (apply_explicit_conv_rules
) {
947 var candidate_set
= new List
<Type
> ();
949 EmptyExpression expr
= EmptyExpression
.Grab ();
951 foreach (Type ret_type
in tgt_types_set
){
952 expr
.SetType (ret_type
);
954 if (ImplicitStandardConversionExists (expr
, target
))
955 candidate_set
.Add (ret_type
);
958 EmptyExpression
.Release (expr
);
960 if (candidate_set
.Count
!= 0)
961 return FindMostEncompassingType (candidate_set
);
965 // Okay, final case !
967 if (apply_explicit_conv_rules
)
968 return FindMostEncompassedType (tgt_types_set
);
970 return FindMostEncompassingType (tgt_types_set
);
974 /// User-defined Implicit conversions
976 static public Expression
ImplicitUserConversion (ResolveContext ec
, Expression source
,
977 Type target
, Location loc
)
979 return UserDefinedConversion (ec
, source
, target
, loc
, false, true);
983 /// User-defined Explicit conversions
985 static Expression
ExplicitUserConversion (ResolveContext ec
, Expression source
,
986 Type target
, Location loc
)
988 return UserDefinedConversion (ec
, source
, target
, loc
, true, true);
991 static void AddConversionOperators (List
<MethodSpec
> list
,
992 Expression source
, Type target_type
,
993 bool look_for_explicit
,
999 Type source_type
= source
.Type
;
1000 EmptyExpression expr
= EmptyExpression
.Grab ();
1003 // LAMESPEC: Undocumented IntPtr/UIntPtr conversions
1004 // IntPtr -> uint uses int
1005 // UIntPtr -> long uses ulong
1007 if (source_type
== TypeManager
.intptr_type
) {
1008 if (target_type
== TypeManager
.uint32_type
)
1009 target_type
= TypeManager
.int32_type
;
1010 } else if (source_type
== TypeManager
.uintptr_type
) {
1011 if (target_type
== TypeManager
.int64_type
)
1012 target_type
= TypeManager
.uint64_type
;
1015 foreach (var m
in mg
.Methods
) {
1016 AParametersCollection pd
= m
.Parameters
;
1017 Type return_type
= TypeManager
.TypeToCoreType (m
.ReturnType
);
1018 Type arg_type
= pd
.Types
[0];
1020 if (source_type
!= arg_type
) {
1021 if (!ImplicitStandardConversionExists (source
, arg_type
)) {
1022 if (!look_for_explicit
)
1024 expr
.SetType (arg_type
);
1025 if (!ImplicitStandardConversionExists (expr
, source_type
))
1030 if (target_type
!= return_type
) {
1031 expr
.SetType (return_type
);
1032 if (!ImplicitStandardConversionExists (expr
, target_type
)) {
1033 if (!look_for_explicit
)
1035 expr
.SetType (target_type
);
1036 if (!ImplicitStandardConversionExists (expr
, return_type
))
1041 // See LAMESPEC: Exclude IntPtr -> int conversion
1042 if (source_type
== TypeManager
.uintptr_type
&& return_type
== TypeManager
.uint32_type
)
1048 EmptyExpression
.Release (expr
);
1052 /// Compute the user-defined conversion operator from source_type to target_type.
1053 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1055 static MethodSpec
GetConversionOperator (CompilerContext ctx
, Type container_type
, Expression source
, Type target_type
, bool look_for_explicit
)
1057 var ops
= new List
<MethodSpec
> (4);
1059 Type source_type
= source
.Type
;
1061 if (source_type
!= TypeManager
.decimal_type
) {
1062 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1063 Expression
.MethodLookup (ctx
, container_type
, source_type
, "op_Implicit", Location
.Null
) as MethodGroupExpr
);
1064 if (look_for_explicit
) {
1065 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1066 Expression
.MethodLookup (ctx
,
1067 container_type
, source_type
, "op_Explicit", Location
.Null
) as MethodGroupExpr
);
1071 if (target_type
!= TypeManager
.decimal_type
) {
1072 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1073 Expression
.MethodLookup (ctx
, container_type
, target_type
, "op_Implicit", Location
.Null
) as MethodGroupExpr
);
1074 if (look_for_explicit
) {
1075 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1076 Expression
.MethodLookup (ctx
,
1077 container_type
, target_type
, "op_Explicit", Location
.Null
) as MethodGroupExpr
);
1084 Type most_specific_source
= FindMostSpecificSource (ops
, source
, look_for_explicit
);
1085 if (most_specific_source
== null)
1088 Type most_specific_target
= FindMostSpecificTarget (ops
, target_type
, look_for_explicit
);
1089 if (most_specific_target
== null)
1092 MethodSpec method
= null;
1094 foreach (var m
in ops
) {
1095 if (TypeManager
.TypeToCoreType (m
.ReturnType
) != most_specific_target
)
1097 if (m
.Parameters
.Types
[0] != most_specific_source
)
1099 // Ambiguous: more than one conversion operator satisfies the signature.
1109 /// User-defined conversions
1111 public static Expression
UserDefinedConversion (ResolveContext ec
, Expression source
,
1112 Type target
, Location loc
,
1113 bool look_for_explicit
, bool return_convert
)
1115 Type source_type
= source
.Type
;
1116 MethodSpec method
= null;
1117 Expression expr
= null;
1121 if (look_for_explicit
) {
1122 hash
= explicit_conv
;
1124 // Implicit user operators cannot convert to interfaces
1125 if (target
.IsInterface
)
1128 hash
= implicit_conv
;
1131 if (!(source
is Constant
) && hash
.Lookup (source_type
, target
, out o
)) {
1132 method
= (MethodSpec
) o
;
1134 if (TypeManager
.IsDynamicType (source_type
))
1137 method
= GetConversionOperator (ec
.Compiler
, null, source
, target
, look_for_explicit
);
1140 if (method
!= null) {
1141 Type most_specific_source
= method
.Parameters
.Types
[0];
1144 // This will do the conversion to the best match that we
1145 // found. Now we need to perform an implict standard conversion
1146 // if the best match was not the type that we were requested
1149 if (look_for_explicit
) {
1150 ReportPrinter temp
= new SessionReportPrinter ();
1151 ReportPrinter prev
= ec
.Report
.SetPrinter (temp
);
1153 expr
= ExplicitConversionStandard (ec
, source
, most_specific_source
, loc
);
1155 ec
.Report
.SetPrinter (prev
);
1156 if (temp
.ErrorsCount
!= 0)
1159 if (ImplicitStandardConversionExists (source
, most_specific_source
))
1160 expr
= ImplicitConversionStandard (ec
, source
, most_specific_source
, loc
);
1167 bool nullable
= false;
1169 if (TypeManager
.IsNullableType (source_type
)) {
1170 source
= Nullable
.Unwrap
.Create (source
);
1174 Type target_underlying
;
1175 if (TypeManager
.IsNullableType (target
)) {
1176 target_underlying
= TypeManager
.GetTypeArguments (target
)[0];
1179 // No implicit conversion S? -> T for non-reference type T
1180 if (!look_for_explicit
&& !TypeManager
.IsReferenceType (target
))
1183 target_underlying
= target
;
1187 expr
= UserDefinedConversion (ec
, source
, target_underlying
, loc
, look_for_explicit
, return_convert
);
1189 // Do result expression lifting only when it's needed
1190 if (expr
!= null && (!look_for_explicit
|| TypeManager
.IsReferenceType (target
)))
1191 expr
= new Nullable
.Lifted (expr
, source
, target
).Resolve (ec
);
1196 expr
= new UserCast (method
, expr
, loc
).Resolve (ec
);
1198 if (return_convert
&& !TypeManager
.IsEqual (expr
.Type
, target
)) {
1199 if (look_for_explicit
) {
1200 expr
= ExplicitConversionStandard (ec
, expr
, target
, loc
);
1202 expr
= ImplicitConversionStandard (ec
, expr
, target
, loc
);
1207 if (!(source
is Constant
))
1208 hash
.Insert (source_type
, target
, method
);
1214 /// Converts implicitly the resolved expression `expr' into the
1215 /// `target_type'. It returns a new expression that can be used
1216 /// in a context that expects a `target_type'.
1218 static public Expression
ImplicitConversion (ResolveContext ec
, Expression expr
,
1219 Type target_type
, Location loc
)
1223 if (target_type
== null)
1224 throw new Exception ("Target type is null");
1226 e
= ImplicitConversionStandard (ec
, expr
, target_type
, loc
);
1230 e
= ImplicitUserConversion (ec
, expr
, target_type
, loc
);
1239 /// Attempts to apply the `Standard Implicit
1240 /// Conversion' rules to the expression `expr' into
1241 /// the `target_type'. It returns a new expression
1242 /// that can be used in a context that expects a
1245 /// This is different from `ImplicitConversion' in that the
1246 /// user defined implicit conversions are excluded.
1248 static public Expression
ImplicitConversionStandard (ResolveContext ec
, Expression expr
,
1249 Type target_type
, Location loc
)
1251 return ImplicitConversionStandard (ec
, expr
, target_type
, loc
, false);
1254 static Expression
ImplicitConversionStandard (ResolveContext ec
, Expression expr
, Type target_type
, Location loc
, bool explicit_cast
)
1256 if (expr
.eclass
== ExprClass
.MethodGroup
){
1257 if (!TypeManager
.IsDelegateType (target_type
)){
1262 // Only allow anonymous method conversions on post ISO_1
1264 if (RootContext
.Version
!= LanguageVersion
.ISO_1
){
1265 MethodGroupExpr mg
= expr
as MethodGroupExpr
;
1267 return ImplicitDelegateCreation
.Create (
1268 ec
, mg
, target_type
, loc
);
1272 Type expr_type
= expr
.Type
;
1275 if (expr_type
.Equals (target_type
)) {
1276 if (expr_type
!= TypeManager
.null_type
&& expr_type
!= InternalType
.AnonymousMethod
)
1281 if (TypeManager
.IsVariantOf (expr_type
, target_type
)) {
1285 if (TypeManager
.IsNullableType (target_type
))
1286 return ImplicitNulableConversion (ec
, expr
, target_type
);
1289 // Attempt to do the implicit constant expression conversions
1291 Constant c
= expr
as Constant
;
1294 c
= c
.ConvertImplicitly (ec
, target_type
);
1296 Console
.WriteLine ("Conversion error happened in line {0}", loc
);
1303 e
= ImplicitNumericConversion (expr
, expr_type
, target_type
);
1307 e
= ImplicitReferenceConversion (expr
, target_type
, explicit_cast
);
1311 if (expr
is IntConstant
&& TypeManager
.IsEnumType (target_type
)){
1312 Constant i
= (Constant
) expr
;
1314 // LAMESPEC: Conversion from any 0 constant is allowed
1316 // An implicit enumeration conversion permits the decimal-integer-literal 0
1317 // to be converted to any enum-type and to any nullable-type whose underlying
1318 // type is an enum-type
1320 if (i
.IsDefaultValue
)
1321 return new EnumConstant (i
, target_type
).Resolve (ec
);
1325 if (expr_type
.IsPointer
){
1326 if (target_type
== TypeManager
.void_ptr_type
)
1327 return EmptyCast
.Create (expr
, target_type
);
1330 // yep, comparing pointer types cant be done with
1331 // t1 == t2, we have to compare their element types.
1333 if (target_type
.IsPointer
){
1334 if (TypeManager
.GetElementType(target_type
) == TypeManager
.GetElementType(expr_type
))
1341 if (expr_type
== TypeManager
.null_type
&& target_type
.IsPointer
)
1342 return EmptyCast
.Create (new NullPointer (loc
), target_type
);
1345 if (expr_type
== InternalType
.AnonymousMethod
){
1346 AnonymousMethodExpression ame
= (AnonymousMethodExpression
) expr
;
1347 Expression am
= ame
.Compatible (ec
, target_type
);
1349 return am
.Resolve (ec
);
1352 if (expr_type
== InternalType
.Arglist
&& target_type
== TypeManager
.arg_iterator_type
)
1359 /// Attempts to implicitly convert `source' into `target_type', using
1360 /// ImplicitConversion. If there is no implicit conversion, then
1361 /// an error is signaled
1363 static public Expression
ImplicitConversionRequired (ResolveContext ec
, Expression source
,
1364 Type target_type
, Location loc
)
1366 Expression e
= ImplicitConversion (ec
, source
, target_type
, loc
);
1370 if (TypeManager
.IsDynamicType (source
.Type
)) {
1371 Arguments args
= new Arguments (1);
1372 args
.Add (new Argument (source
));
1373 return new DynamicConversion (target_type
, 0, args
, loc
).Resolve (ec
);
1376 source
.Error_ValueCannotBeConverted (ec
, loc
, target_type
, false);
1381 /// Performs the explicit numeric conversions
1383 /// There are a few conversions that are not part of the C# standard,
1384 /// they were interim hacks in the C# compiler that were supposed to
1385 /// become explicit operators in the UIntPtr class and IntPtr class,
1386 /// but for historical reasons it did not happen, so the C# compiler
1387 /// ended up with these special hacks.
1389 /// See bug 59800 for details.
1391 /// The conversion are:
1401 public static Expression
ExplicitNumericConversion (Expression expr
, Type target_type
)
1403 Type expr_type
= expr
.Type
;
1404 Type real_target_type
= target_type
;
1406 if (expr_type
== TypeManager
.sbyte_type
){
1408 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1410 if (real_target_type
== TypeManager
.byte_type
)
1411 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U1
);
1412 if (real_target_type
== TypeManager
.ushort_type
)
1413 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U2
);
1414 if (real_target_type
== TypeManager
.uint32_type
)
1415 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U4
);
1416 if (real_target_type
== TypeManager
.uint64_type
)
1417 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U8
);
1418 if (real_target_type
== TypeManager
.char_type
)
1419 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_CH
);
1421 // One of the built-in conversions that belonged in the class library
1422 if (real_target_type
== TypeManager
.uintptr_type
){
1423 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I1_U8
);
1425 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1427 } else if (expr_type
== TypeManager
.byte_type
){
1429 // From byte to sbyte and char
1431 if (real_target_type
== TypeManager
.sbyte_type
)
1432 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U1_I1
);
1433 if (real_target_type
== TypeManager
.char_type
)
1434 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U1_CH
);
1435 } else if (expr_type
== TypeManager
.short_type
){
1437 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1439 if (real_target_type
== TypeManager
.sbyte_type
)
1440 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_I1
);
1441 if (real_target_type
== TypeManager
.byte_type
)
1442 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U1
);
1443 if (real_target_type
== TypeManager
.ushort_type
)
1444 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U2
);
1445 if (real_target_type
== TypeManager
.uint32_type
)
1446 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U4
);
1447 if (real_target_type
== TypeManager
.uint64_type
)
1448 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U8
);
1449 if (real_target_type
== TypeManager
.char_type
)
1450 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_CH
);
1452 // One of the built-in conversions that belonged in the class library
1453 if (real_target_type
== TypeManager
.uintptr_type
){
1454 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I2_U8
);
1456 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1458 } else if (expr_type
== TypeManager
.ushort_type
){
1460 // From ushort to sbyte, byte, short, char
1462 if (real_target_type
== TypeManager
.sbyte_type
)
1463 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_I1
);
1464 if (real_target_type
== TypeManager
.byte_type
)
1465 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_U1
);
1466 if (real_target_type
== TypeManager
.short_type
)
1467 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_I2
);
1468 if (real_target_type
== TypeManager
.char_type
)
1469 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_CH
);
1470 } else if (expr_type
== TypeManager
.int32_type
){
1472 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1474 if (real_target_type
== TypeManager
.sbyte_type
)
1475 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_I1
);
1476 if (real_target_type
== TypeManager
.byte_type
)
1477 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U1
);
1478 if (real_target_type
== TypeManager
.short_type
)
1479 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_I2
);
1480 if (real_target_type
== TypeManager
.ushort_type
)
1481 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U2
);
1482 if (real_target_type
== TypeManager
.uint32_type
)
1483 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U4
);
1484 if (real_target_type
== TypeManager
.uint64_type
)
1485 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U8
);
1486 if (real_target_type
== TypeManager
.char_type
)
1487 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_CH
);
1489 // One of the built-in conversions that belonged in the class library
1490 if (real_target_type
== TypeManager
.uintptr_type
){
1491 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I2_U8
);
1493 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1495 } else if (expr_type
== TypeManager
.uint32_type
){
1497 // From uint to sbyte, byte, short, ushort, int, char
1499 if (real_target_type
== TypeManager
.sbyte_type
)
1500 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I1
);
1501 if (real_target_type
== TypeManager
.byte_type
)
1502 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_U1
);
1503 if (real_target_type
== TypeManager
.short_type
)
1504 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I2
);
1505 if (real_target_type
== TypeManager
.ushort_type
)
1506 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_U2
);
1507 if (real_target_type
== TypeManager
.int32_type
)
1508 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I4
);
1509 if (real_target_type
== TypeManager
.char_type
)
1510 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_CH
);
1511 } else if (expr_type
== TypeManager
.int64_type
){
1513 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1515 if (real_target_type
== TypeManager
.sbyte_type
)
1516 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I1
);
1517 if (real_target_type
== TypeManager
.byte_type
)
1518 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U1
);
1519 if (real_target_type
== TypeManager
.short_type
)
1520 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I2
);
1521 if (real_target_type
== TypeManager
.ushort_type
)
1522 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U2
);
1523 if (real_target_type
== TypeManager
.int32_type
)
1524 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I4
);
1525 if (real_target_type
== TypeManager
.uint32_type
)
1526 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U4
);
1527 if (real_target_type
== TypeManager
.uint64_type
)
1528 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U8
);
1529 if (real_target_type
== TypeManager
.char_type
)
1530 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_CH
);
1531 } else if (expr_type
== TypeManager
.uint64_type
){
1533 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1535 if (real_target_type
== TypeManager
.sbyte_type
)
1536 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I1
);
1537 if (real_target_type
== TypeManager
.byte_type
)
1538 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U1
);
1539 if (real_target_type
== TypeManager
.short_type
)
1540 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I2
);
1541 if (real_target_type
== TypeManager
.ushort_type
)
1542 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U2
);
1543 if (real_target_type
== TypeManager
.int32_type
)
1544 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I4
);
1545 if (real_target_type
== TypeManager
.uint32_type
)
1546 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U4
);
1547 if (real_target_type
== TypeManager
.int64_type
)
1548 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I8
);
1549 if (real_target_type
== TypeManager
.char_type
)
1550 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_CH
);
1552 // One of the built-in conversions that belonged in the class library
1553 if (real_target_type
== TypeManager
.intptr_type
){
1554 return new OperatorCast (EmptyCast
.Create (expr
, TypeManager
.int64_type
),
1555 TypeManager
.intptr_type
, true);
1557 } else if (expr_type
== TypeManager
.char_type
){
1559 // From char to sbyte, byte, short
1561 if (real_target_type
== TypeManager
.sbyte_type
)
1562 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_I1
);
1563 if (real_target_type
== TypeManager
.byte_type
)
1564 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_U1
);
1565 if (real_target_type
== TypeManager
.short_type
)
1566 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_I2
);
1567 } else if (expr_type
== TypeManager
.float_type
){
1569 // From float to sbyte, byte, short,
1570 // ushort, int, uint, long, ulong, char
1573 if (real_target_type
== TypeManager
.sbyte_type
)
1574 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I1
);
1575 if (real_target_type
== TypeManager
.byte_type
)
1576 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U1
);
1577 if (real_target_type
== TypeManager
.short_type
)
1578 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I2
);
1579 if (real_target_type
== TypeManager
.ushort_type
)
1580 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U2
);
1581 if (real_target_type
== TypeManager
.int32_type
)
1582 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I4
);
1583 if (real_target_type
== TypeManager
.uint32_type
)
1584 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U4
);
1585 if (real_target_type
== TypeManager
.int64_type
)
1586 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I8
);
1587 if (real_target_type
== TypeManager
.uint64_type
)
1588 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U8
);
1589 if (real_target_type
== TypeManager
.char_type
)
1590 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_CH
);
1591 if (real_target_type
== TypeManager
.decimal_type
)
1592 return new CastToDecimal (expr
, true);
1593 } else if (expr_type
== TypeManager
.double_type
){
1595 // From double to sbyte, byte, short,
1596 // ushort, int, uint, long, ulong,
1597 // char, float or decimal
1599 if (real_target_type
== TypeManager
.sbyte_type
)
1600 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I1
);
1601 if (real_target_type
== TypeManager
.byte_type
)
1602 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U1
);
1603 if (real_target_type
== TypeManager
.short_type
)
1604 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I2
);
1605 if (real_target_type
== TypeManager
.ushort_type
)
1606 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U2
);
1607 if (real_target_type
== TypeManager
.int32_type
)
1608 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I4
);
1609 if (real_target_type
== TypeManager
.uint32_type
)
1610 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U4
);
1611 if (real_target_type
== TypeManager
.int64_type
)
1612 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I8
);
1613 if (real_target_type
== TypeManager
.uint64_type
)
1614 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U8
);
1615 if (real_target_type
== TypeManager
.char_type
)
1616 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_CH
);
1617 if (real_target_type
== TypeManager
.float_type
)
1618 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_R4
);
1619 if (real_target_type
== TypeManager
.decimal_type
)
1620 return new CastToDecimal (expr
, true);
1621 } else if (expr_type
== TypeManager
.uintptr_type
){
1623 // Various built-in conversions that belonged in the class library
1625 // from uintptr to sbyte, short, int32
1627 if (real_target_type
== TypeManager
.sbyte_type
){
1628 Expression uint32e
= new OperatorCast (expr
, TypeManager
.uint32_type
, true);
1629 return new ConvCast (uint32e
, TypeManager
.sbyte_type
, ConvCast
.Mode
.U4_I1
);
1631 if (real_target_type
== TypeManager
.short_type
){
1632 Expression uint32e
= new OperatorCast (expr
, TypeManager
.uint32_type
, true);
1633 return new ConvCast (uint32e
, TypeManager
.sbyte_type
, ConvCast
.Mode
.U4_I2
);
1635 if (real_target_type
== TypeManager
.int32_type
){
1636 return EmptyCast
.Create (new OperatorCast (expr
, TypeManager
.uint32_type
, true),
1637 TypeManager
.int32_type
);
1639 } else if (expr_type
== TypeManager
.intptr_type
){
1640 if (real_target_type
== TypeManager
.uint64_type
){
1641 return EmptyCast
.Create (new OperatorCast (expr
, TypeManager
.int64_type
, true),
1642 TypeManager
.uint64_type
);
1644 } else if (expr_type
== TypeManager
.decimal_type
) {
1645 return new CastFromDecimal (expr
, target_type
).Resolve ();
1651 /// Returns whether an explicit reference conversion can be performed
1652 /// from source_type to target_type
1654 public static bool ExplicitReferenceConversionExists (Type source_type
, Type target_type
)
1656 Expression e
= ExplicitReferenceConversion (null, source_type
, target_type
);
1660 if (e
== EmptyExpression
.Null
)
1663 throw new InternalErrorException ("Invalid probing conversion result");
1667 /// Implements Explicit Reference conversions
1669 static Expression
ExplicitReferenceConversion (Expression source
, Type source_type
, Type target_type
)
1671 bool target_is_value_type
= TypeManager
.IsStruct (target_type
);
1674 // From object to a generic parameter
1676 if (source_type
== TypeManager
.object_type
&& TypeManager
.IsGenericParameter (target_type
))
1677 return source
== null ? EmptyExpression
.Null
: new UnboxCast (source
, target_type
);
1680 // Explicit type parameter conversion.
1682 if (TypeManager
.IsGenericParameter (source_type
))
1683 return ExplicitTypeParameterConversion (source
, source_type
, target_type
);
1686 // From object to any reference type or value type (unboxing)
1688 if (source_type
== TypeManager
.object_type
)
1689 return source
== null ? EmptyExpression
.Null
:
1690 target_is_value_type
? (Expression
) new UnboxCast (source
, target_type
) : new ClassCast (source
, target_type
);
1693 // Unboxing conversion from the types object and System.ValueType to any non-nullable-value-type
1695 if (source_type
== TypeManager
.value_type
&& target_is_value_type
)
1696 return source
== null ? EmptyExpression
.Null
: new UnboxCast (source
, target_type
);
1699 // From any class S to any class-type T, provided S is a base class of T
1701 if (TypeManager
.IsSubclassOf (target_type
, source_type
))
1702 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1705 // From any class type S to any interface T, provides S is not sealed
1706 // and provided S does not implement T.
1708 if (target_type
.IsInterface
&& !source_type
.IsSealed
&&
1709 !TypeManager
.ImplementsInterface (source_type
, target_type
)) {
1710 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1714 // From any interface-type S to to any class type T, provided T is not
1715 // sealed, or provided T implements S.
1717 if (source_type
.IsInterface
) {
1718 if (!target_type
.IsSealed
|| TypeManager
.ImplementsInterface (target_type
, source_type
)) {
1719 if (target_type
.IsClass
)
1720 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1723 // Unboxing conversion from any interface-type to any non-nullable-value-type that
1724 // implements the interface-type
1726 return source
== null ? EmptyExpression
.Null
: new UnboxCast (source
, target_type
);
1730 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1731 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1733 if (IList_To_Array (source_type
, target_type
))
1734 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1739 if (source_type
.IsArray
) {
1740 if (target_type
.IsArray
) {
1742 // From System.Array to any array-type
1744 if (source_type
== TypeManager
.array_type
)
1745 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1748 // From an array type S with an element type Se to an array type T with an
1749 // element type Te provided all the following are true:
1750 // * S and T differe only in element type, in other words, S and T
1751 // have the same number of dimensions.
1752 // * Both Se and Te are reference types
1753 // * An explicit reference conversions exist from Se to Te
1755 if (source_type
.GetArrayRank () == target_type
.GetArrayRank ()) {
1757 source_type
= TypeManager
.GetElementType (source_type
);
1758 if (!TypeManager
.IsReferenceType (source_type
))
1761 Type target_type_element
= TypeManager
.GetElementType (target_type
);
1762 if (!TypeManager
.IsReferenceType (target_type_element
))
1765 if (ExplicitReferenceConversionExists (source_type
, target_type_element
))
1766 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1773 // From a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces,
1774 // provided that there is an explicit reference conversion from S to T
1776 if (Array_To_IList (source_type
, target_type
, true))
1777 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1783 // From System delegate to any delegate-type
1785 if (source_type
== TypeManager
.delegate_type
&& TypeManager
.IsDelegateType (target_type
))
1786 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1792 /// Performs an explicit conversion of the expression `expr' whose
1793 /// type is expr.Type to `target_type'.
1795 static public Expression
ExplicitConversionCore (ResolveContext ec
, Expression expr
,
1796 Type target_type
, Location loc
)
1798 Type expr_type
= expr
.Type
;
1800 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1801 Expression ne
= ImplicitConversionStandard (ec
, expr
, target_type
, loc
, true);
1805 if (TypeManager
.IsEnumType (expr_type
)) {
1806 Expression underlying
= EmptyCast
.Create (expr
, TypeManager
.GetEnumUnderlyingType (expr_type
));
1807 expr
= ExplicitConversionCore (ec
, underlying
, target_type
, loc
);
1811 return ExplicitUserConversion (ec
, underlying
, target_type
, loc
);
1814 if (TypeManager
.IsEnumType (target_type
)){
1816 // Type System.Enum can be unboxed to any enum-type
1818 if (expr_type
== TypeManager
.enum_type
)
1819 return new UnboxCast (expr
, target_type
);
1821 Expression ce
= ExplicitConversionCore (ec
, expr
, TypeManager
.GetEnumUnderlyingType (target_type
), loc
);
1823 return EmptyCast
.Create (ce
, target_type
);
1826 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1828 if (expr_type
== TypeManager
.intptr_type
|| expr_type
== TypeManager
.uintptr_type
) {
1829 ne
= ExplicitUserConversion (ec
, expr
, TypeManager
.GetEnumUnderlyingType (target_type
), loc
);
1831 return ExplicitConversionCore (ec
, ne
, target_type
, loc
);
1837 ne
= ExplicitNumericConversion (expr
, target_type
);
1842 // Skip the ExplicitReferenceConversion because we can not convert
1843 // from Null to a ValueType, and ExplicitReference wont check against
1844 // null literal explicitly
1846 if (expr_type
!= TypeManager
.null_type
){
1847 ne
= ExplicitReferenceConversion (expr
, expr_type
, target_type
);
1853 ne
= ExplicitUnsafe (expr
, target_type
);
1861 public static Expression
ExplicitUnsafe (Expression expr
, Type target_type
)
1863 Type expr_type
= expr
.Type
;
1865 if (target_type
.IsPointer
){
1866 if (expr_type
.IsPointer
)
1867 return EmptyCast
.Create (expr
, target_type
);
1869 if (expr_type
== TypeManager
.sbyte_type
||
1870 expr_type
== TypeManager
.short_type
||
1871 expr_type
== TypeManager
.int32_type
)
1872 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I
);
1874 if (expr_type
== TypeManager
.ushort_type
||
1875 expr_type
== TypeManager
.uint32_type
||
1876 expr_type
== TypeManager
.byte_type
)
1877 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U
);
1879 if (expr_type
== TypeManager
.int64_type
)
1880 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I
);
1882 if (expr_type
== TypeManager
.uint64_type
)
1883 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I
);
1886 if (expr_type
.IsPointer
){
1887 if (target_type
== TypeManager
.sbyte_type
)
1888 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I1
);
1889 if (target_type
== TypeManager
.byte_type
)
1890 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U1
);
1891 if (target_type
== TypeManager
.short_type
)
1892 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I2
);
1893 if (target_type
== TypeManager
.ushort_type
)
1894 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U2
);
1895 if (target_type
== TypeManager
.int32_type
)
1896 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I4
);
1897 if (target_type
== TypeManager
.uint32_type
)
1898 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U4
);
1899 if (target_type
== TypeManager
.int64_type
)
1900 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I_I8
);
1901 if (target_type
== TypeManager
.uint64_type
)
1902 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
1908 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1910 static public Expression
ExplicitConversionStandard (ResolveContext ec
, Expression expr
,
1911 Type target_type
, Location l
)
1913 int errors
= ec
.Report
.Errors
;
1914 Expression ne
= ImplicitConversionStandard (ec
, expr
, target_type
, l
);
1915 if (ec
.Report
.Errors
> errors
)
1921 ne
= ExplicitNumericConversion (expr
, target_type
);
1925 ne
= ExplicitReferenceConversion (expr
, expr
.Type
, target_type
);
1929 if (ec
.IsUnsafe
&& expr
.Type
== TypeManager
.void_ptr_type
&& target_type
.IsPointer
)
1930 return EmptyCast
.Create (expr
, target_type
);
1932 expr
.Error_ValueCannotBeConverted (ec
, l
, target_type
, true);
1937 /// Performs an explicit conversion of the expression `expr' whose
1938 /// type is expr.Type to `target_type'.
1940 static public Expression
ExplicitConversion (ResolveContext ec
, Expression expr
,
1941 Type target_type
, Location loc
)
1943 Expression e
= ExplicitConversionCore (ec
, expr
, target_type
, loc
);
1946 // Don't eliminate explicit precission casts
1949 if (target_type
== TypeManager
.float_type
)
1950 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
1952 if (target_type
== TypeManager
.double_type
)
1953 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
1959 Type expr_type
= expr
.Type
;
1960 if (TypeManager
.IsNullableType (target_type
)) {
1961 if (TypeManager
.IsNullableType (expr_type
)) {
1962 Type target
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (target_type
)[0]);
1963 Expression unwrap
= Nullable
.Unwrap
.Create (expr
);
1964 e
= ExplicitConversion (ec
, unwrap
, target
, expr
.Location
);
1968 return new Nullable
.Lifted (e
, unwrap
, target_type
).Resolve (ec
);
1969 } else if (expr_type
== TypeManager
.object_type
) {
1970 return new UnboxCast (expr
, target_type
);
1972 Type target
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (target_type
) [0]);
1974 e
= ExplicitConversionCore (ec
, expr
, target
, loc
);
1976 return Nullable
.Wrap
.Create (e
, target_type
);
1978 } else if (TypeManager
.IsNullableType (expr_type
)) {
1979 e
= Nullable
.Unwrap
.Create (expr
, false);
1981 bool use_class_cast
;
1982 if (ImplicitBoxingConversionExists (e
, target_type
, out use_class_cast
))
1983 return new BoxedCast (expr
, target_type
);
1985 e
= ExplicitConversionCore (ec
, e
, target_type
, loc
);
1987 return EmptyCast
.Create (e
, target_type
);
1990 e
= ExplicitUserConversion (ec
, expr
, target_type
, loc
);
1994 expr
.Error_ValueCannotBeConverted (ec
, loc
, target_type
, true);