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.
13 namespace Mono
.CSharp
{
15 using System
.Collections
;
16 using System
.Diagnostics
;
17 using System
.Reflection
;
18 using System
.Reflection
.Emit
;
21 // A container class for all the conversion operations
23 static class Convert
{
25 static EmptyExpression MyEmptyExpr
;
26 static DoubleHash explicit_conv
;
27 static DoubleHash implicit_conv
;
34 public static void Reset ()
37 explicit_conv
= new DoubleHash (100);
38 implicit_conv
= new DoubleHash (100);
41 static Type
TypeParam_EffectiveBaseType (GenericConstraints gc
)
43 ArrayList list
= new ArrayList ();
44 list
.Add (gc
.EffectiveBaseClass
);
45 foreach (Type t
in gc
.InterfaceConstraints
) {
46 if (!TypeManager
.IsGenericParameter (t
))
49 GenericConstraints new_gc
= TypeManager
.GetTypeParameterConstraints (t
);
51 list
.Add (TypeParam_EffectiveBaseType (new_gc
));
53 return FindMostEncompassedType (list
);
57 // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
58 // interfaces of this interface, provided there is an implicit reference conversion
61 static bool Array_To_IList (Type array
, Type list
, bool isExplicit
)
63 if ((array
.GetArrayRank () != 1) || !TypeManager
.IsGenericType (list
))
66 Type gt
= TypeManager
.DropGenericTypeArguments (list
);
67 if ((gt
!= TypeManager
.generic_ilist_type
) &&
68 (gt
!= TypeManager
.generic_icollection_type
) &&
69 (gt
!= TypeManager
.generic_ienumerable_type
))
72 Type element_type
= TypeManager
.GetElementType (array
);
73 Type arg_type
= TypeManager
.GetTypeArguments (list
) [0];
75 if (element_type
== arg_type
)
79 return ExplicitReferenceConversionExists (element_type
, arg_type
);
81 Type t
= TypeManager
.GetElementType (array
);
82 if (MyEmptyExpr
== null)
83 MyEmptyExpr
= new EmptyExpression (t
);
85 MyEmptyExpr
.SetType (t
);
87 return ImplicitReferenceConversionExists (MyEmptyExpr
, arg_type
);
90 static bool IList_To_Array(Type list
, Type array
)
92 if (!TypeManager
.IsGenericType (list
) || !array
.IsArray
|| array
.GetArrayRank() != 1)
95 Type gt
= TypeManager
.DropGenericTypeArguments (list
);
96 if (gt
!= TypeManager
.generic_ilist_type
&&
97 gt
!= TypeManager
.generic_icollection_type
&&
98 gt
!= TypeManager
.generic_ienumerable_type
)
101 Type arg_type
= TypeManager
.GetTypeArguments(list
)[0];
102 Type element_type
= TypeManager
.GetElementType(array
);
104 if (element_type
== arg_type
)
107 if (MyEmptyExpr
== null)
108 MyEmptyExpr
= new EmptyExpression(element_type
);
110 MyEmptyExpr
.SetType(element_type
);
112 return ImplicitReferenceConversionExists(MyEmptyExpr
, arg_type
) || ExplicitReferenceConversionExists(element_type
, arg_type
);
115 static Expression
ImplicitTypeParameterConversion (Expression expr
,
118 Type expr_type
= expr
.Type
;
120 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (expr_type
);
123 if (target_type
== TypeManager
.object_type
)
124 return new BoxedCast (expr
, target_type
);
129 // We're converting from a type parameter which is known to be a reference type.
130 Type base_type
= TypeParam_EffectiveBaseType (gc
);
132 if (TypeManager
.IsSubclassOf (base_type
, target_type
))
133 return new ClassCast (expr
, target_type
);
135 if (target_type
.IsInterface
) {
136 if (TypeManager
.ImplementsInterface (base_type
, target_type
))
137 return new ClassCast (expr
, target_type
);
139 foreach (Type t
in gc
.InterfaceConstraints
) {
140 if (TypeManager
.IsSubclassOf (t
, target_type
))
141 return new ClassCast (expr
, target_type
);
142 if (TypeManager
.ImplementsInterface (t
, target_type
))
143 return new ClassCast (expr
, target_type
);
147 foreach (Type t
in gc
.InterfaceConstraints
) {
148 if (!TypeManager
.IsGenericParameter (t
))
150 if (TypeManager
.IsSubclassOf (t
, target_type
))
151 return new ClassCast (expr
, target_type
);
152 if (TypeManager
.ImplementsInterface (t
, target_type
))
153 return new ClassCast (expr
, target_type
);
159 static bool ImplicitTypeParameterBoxingConversion (Type expr_type
, Type target_type
,
160 out bool use_class_cast
)
162 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (expr_type
);
165 use_class_cast
= false;
166 return target_type
== TypeManager
.object_type
;
169 use_class_cast
= true;
171 if (!gc
.HasReferenceTypeConstraint
)
174 // We're converting from a type parameter which is known to be a reference type.
175 Type base_type
= TypeParam_EffectiveBaseType (gc
);
177 if (TypeManager
.IsSubclassOf (base_type
, target_type
))
180 if (target_type
.IsInterface
) {
181 if (TypeManager
.ImplementsInterface (base_type
, target_type
))
184 foreach (Type t
in gc
.InterfaceConstraints
) {
185 if (TypeManager
.IsSubclassOf (t
, target_type
))
187 if (TypeManager
.ImplementsInterface (t
, target_type
))
192 foreach (Type t
in gc
.InterfaceConstraints
) {
193 if (!TypeManager
.IsGenericParameter (t
))
195 if (TypeManager
.IsSubclassOf (t
, target_type
))
197 if (TypeManager
.ImplementsInterface (t
, target_type
))
201 use_class_cast
= false;
205 static Expression
ExplicitTypeParameterConversion (Expression source
, Type source_type
, Type target_type
)
207 if (TypeManager
.IsGenericParameter (target_type
)) {
208 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (target_type
);
212 foreach (Type iface
in gc
.InterfaceConstraints
) {
213 if (!TypeManager
.IsGenericParameter (iface
))
216 if (TypeManager
.IsSubclassOf (source_type
, iface
))
217 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
, true);
221 if (target_type
.IsInterface
)
222 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
, true);
227 static Expression
ImplicitReferenceConversion (Expression expr
, Type target_type
, bool explicit_cast
)
229 Type expr_type
= expr
.Type
;
231 if (expr_type
== null && expr
.eclass
== ExprClass
.MethodGroup
){
232 // if we are a method group, emit a warning
237 if (expr_type
== TypeManager
.void_type
)
240 if (TypeManager
.IsGenericParameter (expr_type
))
241 return ImplicitTypeParameterConversion (expr
, target_type
);
244 // from the null type to any reference-type.
246 NullLiteral nl
= expr
as NullLiteral
;
248 return nl
.ConvertImplicitly(target_type
);
251 if (ImplicitReferenceConversionExists (expr
, target_type
)) {
253 // Avoid wrapping implicitly convertible reference type
258 return EmptyCast
.Create (expr
, target_type
);
262 if (ImplicitBoxingConversionExists (expr
, target_type
, out use_class_cast
)) {
264 return new ClassCast (expr
, target_type
);
266 return new BoxedCast (expr
, target_type
);
273 // 6.1.6 Implicit reference conversions
275 public static bool ImplicitReferenceConversionExists (Expression expr
, Type target_type
)
277 if (TypeManager
.IsStruct (target_type
))
280 Type expr_type
= expr
.Type
;
282 // from the null type to any reference-type.
283 if (expr_type
== TypeManager
.null_type
)
284 return target_type
!= TypeManager
.anonymous_method_type
;
286 if (TypeManager
.IsGenericParameter (expr_type
))
287 return ImplicitTypeParameterConversion (expr
, target_type
) != null;
290 // notice that it is possible to write "ValueType v = 1", the ValueType here
291 // is an abstract class, and not really a value type, so we apply the same rules.
293 if (target_type
== TypeManager
.object_type
) {
295 // A pointer type cannot be converted to object
297 if (expr_type
.IsPointer
)
300 if (TypeManager
.IsValueType (expr_type
))
302 if (expr_type
.IsClass
|| expr_type
.IsInterface
|| expr_type
== TypeManager
.enum_type
){
303 return expr_type
!= TypeManager
.anonymous_method_type
;
307 } else if (target_type
== TypeManager
.value_type
) {
308 return expr_type
== TypeManager
.enum_type
;
309 } else if (TypeManager
.IsSubclassOf (expr_type
, target_type
)) {
311 // Special case: enumeration to System.Enum.
312 // System.Enum is not a value type, it is a class, so we need
313 // a boxing conversion
315 if (target_type
== TypeManager
.enum_type
|| TypeManager
.IsGenericParameter (expr_type
))
321 // This code is kind of mirrored inside ImplicitStandardConversionExists
322 // with the small distinction that we only probe there
324 // Always ensure that the code here and there is in sync
326 // from any class-type S to any interface-type T.
327 if (target_type
.IsInterface
) {
328 if (TypeManager
.ImplementsInterface (expr_type
, target_type
)){
329 return !TypeManager
.IsGenericParameter (expr_type
) &&
330 !TypeManager
.IsValueType (expr_type
);
334 if (expr_type
.IsArray
) {
335 // from an array-type S to an array-type of type T
336 if (target_type
.IsArray
&& expr_type
.GetArrayRank () == target_type
.GetArrayRank ()) {
339 // Both SE and TE are reference-types
341 Type expr_element_type
= TypeManager
.GetElementType (expr_type
);
342 if (!TypeManager
.IsReferenceType (expr_element_type
))
345 Type target_element_type
= TypeManager
.GetElementType (target_type
);
346 if (!TypeManager
.IsReferenceType (target_element_type
))
349 if (MyEmptyExpr
== null)
350 MyEmptyExpr
= new EmptyExpression (expr_element_type
);
352 MyEmptyExpr
.SetType (expr_element_type
);
354 return ImplicitStandardConversionExists (MyEmptyExpr
, target_element_type
);
357 // from an array-type to System.Array
358 if (target_type
== TypeManager
.array_type
)
361 // from an array-type of type T to IList<T>
362 if (Array_To_IList (expr_type
, target_type
, false))
368 // from any interface type S to interface-type T.
369 if (expr_type
.IsInterface
&& target_type
.IsInterface
) {
370 return TypeManager
.ImplementsInterface (expr_type
, target_type
);
373 // from any delegate type to System.Delegate
374 if (target_type
== TypeManager
.delegate_type
&&
375 (expr_type
== TypeManager
.delegate_type
|| TypeManager
.IsDelegateType (expr_type
)))
378 if (TypeManager
.IsEqual (expr_type
, target_type
))
381 if (TypeManager
.IsVariantOf (expr_type
, target_type
))
387 public static bool ImplicitBoxingConversionExists (Expression expr
, Type target_type
,
388 out bool use_class_cast
)
390 Type expr_type
= expr
.Type
;
391 use_class_cast
= false;
394 // From any value-type to the type object.
396 if (target_type
== TypeManager
.object_type
) {
398 // A pointer type cannot be converted to object
400 if (expr_type
.IsPointer
)
403 return TypeManager
.IsValueType (expr_type
);
407 // From any value-type to the type System.ValueType.
409 if (target_type
== TypeManager
.value_type
)
410 return TypeManager
.IsValueType (expr_type
);
412 if (target_type
== TypeManager
.enum_type
) {
414 // From any enum-type to the type System.Enum.
416 if (TypeManager
.IsEnumType (expr_type
))
419 // From any nullable-type with an underlying enum-type to the type System.Enum
421 if (TypeManager
.IsNullableType (expr_type
))
422 return TypeManager
.IsEnumType (TypeManager
.GetTypeArguments (expr_type
) [0]);
425 if (TypeManager
.IsSubclassOf (expr_type
, target_type
)) {
427 // Don't box same type arguments
429 if (TypeManager
.IsGenericParameter (expr_type
) && expr_type
!= target_type
)
435 // This code is kind of mirrored inside ImplicitStandardConversionExists
436 // with the small distinction that we only probe there
438 // Always ensure that the code here and there is in sync
440 // from any class-type S to any interface-type T.
441 if (target_type
.IsInterface
) {
442 if (TypeManager
.ImplementsInterface (expr_type
, target_type
))
443 return TypeManager
.IsGenericParameter (expr_type
) ||
444 TypeManager
.IsValueType (expr_type
);
447 if (TypeManager
.IsGenericParameter (expr_type
))
448 return ImplicitTypeParameterBoxingConversion (
449 expr_type
, target_type
, out use_class_cast
);
454 static Expression
ImplicitNulableConversion (EmitContext ec
, Expression expr
, Type target_type
)
456 Type expr_type
= expr
.Type
;
459 // From null to any nullable type
461 if (expr_type
== TypeManager
.null_type
)
462 return ec
== null ? EmptyExpression
.Null
: Nullable
.LiftedNull
.Create (target_type
, expr
.Location
);
464 Type target
= TypeManager
.GetTypeArguments (target_type
)[0];
468 if (TypeManager
.IsNullableType (expr_type
)) {
469 Type etype
= TypeManager
.GetTypeArguments (expr_type
)[0];
472 return ImplicitConversionExists (ec
, new EmptyExpression (etype
), target
) ? EmptyExpression
.Null
: null;
474 Expression unwrap
= Nullable
.Unwrap
.Create (expr
);
475 e
= ImplicitConversion (ec
, unwrap
, target
, expr
.Location
);
479 return new Nullable
.Lifted (e
, unwrap
, target_type
).Resolve (ec
);
484 return ImplicitConversionExists (ec
, expr
, target
) ? EmptyExpression
.Null
: null;
486 e
= ImplicitConversion (ec
, expr
, target
, expr
.Location
);
488 return Nullable
.Wrap
.Create (e
, target_type
);
494 /// Implicit Numeric Conversions.
496 /// expr is the expression to convert, returns a new expression of type
497 /// target_type or null if an implicit conversion is not possible.
499 public static Expression
ImplicitNumericConversion (Expression expr
, Type target_type
)
501 return ImplicitNumericConversion (expr
, expr
.Type
, target_type
);
504 static Expression
ImplicitNumericConversion (Expression expr
, Type expr_type
, Type target_type
)
506 if (expr_type
== TypeManager
.sbyte_type
){
508 // From sbyte to short, int, long, float, double, decimal
510 if (target_type
== TypeManager
.int32_type
)
511 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I4
);
512 if (target_type
== TypeManager
.int64_type
)
513 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
514 if (target_type
== TypeManager
.double_type
)
515 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
516 if (target_type
== TypeManager
.float_type
)
517 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
518 if (target_type
== TypeManager
.short_type
)
519 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I2
);
520 if (target_type
== TypeManager
.decimal_type
)
521 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
522 } else if (expr_type
== TypeManager
.byte_type
){
524 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
526 if (target_type
== TypeManager
.int32_type
|| target_type
== TypeManager
.uint32_type
||
527 target_type
== TypeManager
.short_type
|| target_type
== TypeManager
.ushort_type
)
528 return expr
== null ? EmptyExpression
.Null
: EmptyCast
.Create (expr
, target_type
);
530 if (target_type
== TypeManager
.uint64_type
)
531 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
532 if (target_type
== TypeManager
.int64_type
)
533 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
534 if (target_type
== TypeManager
.float_type
)
535 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
536 if (target_type
== TypeManager
.double_type
)
537 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
538 if (target_type
== TypeManager
.decimal_type
)
539 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
541 } else if (expr_type
== TypeManager
.short_type
){
543 // From short to int, long, float, double, decimal
545 if (target_type
== TypeManager
.int32_type
)
546 return expr
== null ? EmptyExpression
.Null
: EmptyCast
.Create (expr
, target_type
);
547 if (target_type
== TypeManager
.int64_type
)
548 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
549 if (target_type
== TypeManager
.double_type
)
550 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
551 if (target_type
== TypeManager
.float_type
)
552 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
553 if (target_type
== TypeManager
.decimal_type
)
554 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
556 } else if (expr_type
== TypeManager
.ushort_type
){
558 // From ushort to int, uint, long, ulong, float, double, decimal
560 if (target_type
== TypeManager
.int32_type
|| target_type
== TypeManager
.uint32_type
)
561 return expr
== null ? EmptyExpression
.Null
: EmptyCast
.Create (expr
, target_type
);
563 if (target_type
== TypeManager
.uint64_type
)
564 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
565 if (target_type
== TypeManager
.int64_type
)
566 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
567 if (target_type
== TypeManager
.double_type
)
568 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
569 if (target_type
== TypeManager
.float_type
)
570 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
571 if (target_type
== TypeManager
.decimal_type
)
572 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
573 } else if (expr_type
== TypeManager
.int32_type
){
575 // From int to long, float, double, decimal
577 if (target_type
== TypeManager
.int64_type
)
578 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
579 if (target_type
== TypeManager
.double_type
)
580 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
581 if (target_type
== TypeManager
.float_type
)
582 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
583 if (target_type
== TypeManager
.decimal_type
)
584 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
585 } else if (expr_type
== TypeManager
.uint32_type
){
587 // From uint to long, ulong, float, double, decimal
589 if (target_type
== TypeManager
.int64_type
)
590 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
591 if (target_type
== TypeManager
.uint64_type
)
592 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
593 if (target_type
== TypeManager
.double_type
)
594 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
), target_type
, OpCodes
.Conv_R8
);
595 if (target_type
== TypeManager
.float_type
)
596 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
), 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
.int64_type
){
601 // From long/ulong to float, double
603 if (target_type
== TypeManager
.double_type
)
604 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
605 if (target_type
== TypeManager
.float_type
)
606 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
607 if (target_type
== TypeManager
.decimal_type
)
608 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
609 } else if (expr_type
== TypeManager
.uint64_type
){
611 // From ulong to float, double
613 if (target_type
== TypeManager
.double_type
)
614 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
), target_type
, OpCodes
.Conv_R8
);
615 if (target_type
== TypeManager
.float_type
)
616 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
), target_type
, OpCodes
.Conv_R4
);
617 if (target_type
== TypeManager
.decimal_type
)
618 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
619 } else if (expr_type
== TypeManager
.char_type
){
621 // From char to ushort, int, uint, long, ulong, float, double, decimal
623 if ((target_type
== TypeManager
.ushort_type
) ||
624 (target_type
== TypeManager
.int32_type
) ||
625 (target_type
== TypeManager
.uint32_type
))
626 return expr
== null ? EmptyExpression
.Null
: EmptyCast
.Create (expr
, target_type
);
627 if (target_type
== TypeManager
.uint64_type
)
628 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
629 if (target_type
== TypeManager
.int64_type
)
630 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
631 if (target_type
== TypeManager
.float_type
)
632 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
633 if (target_type
== TypeManager
.double_type
)
634 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
635 if (target_type
== TypeManager
.decimal_type
)
636 return expr
== null ? EmptyExpression
.Null
: new CastToDecimal (expr
);
637 } else if (expr_type
== TypeManager
.float_type
){
641 if (target_type
== TypeManager
.double_type
)
642 return expr
== null ? EmptyExpression
.Null
: new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
649 /// Same as ImplicitStandardConversionExists except that it also looks at
650 /// implicit user defined conversions - needed for overload resolution
652 public static bool ImplicitConversionExists (EmitContext ec
, Expression expr
, Type target_type
)
654 if (ImplicitStandardConversionExists (expr
, target_type
))
657 if (expr
.Type
== TypeManager
.anonymous_method_type
) {
658 if (!TypeManager
.IsDelegateType (target_type
) &&
659 TypeManager
.DropGenericTypeArguments (target_type
) != TypeManager
.expression_type
)
662 AnonymousMethodExpression ame
= (AnonymousMethodExpression
) expr
;
663 return ame
.ImplicitStandardConversionExists (ec
, target_type
);
666 return ImplicitUserConversion (ec
, expr
, target_type
, Location
.Null
) != null;
669 public static bool ImplicitUserConversionExists (EmitContext ec
, Type source
, Type target
)
671 return ImplicitUserConversion (ec
, new EmptyExpression (source
), target
, Location
.Null
) != null;
675 /// Determines if a standard implicit conversion exists from
676 /// expr_type to target_type
678 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
680 public static bool ImplicitStandardConversionExists (Expression expr
, Type target_type
)
682 Type expr_type
= expr
.Type
;
684 if (expr_type
== TypeManager
.null_type
) {
685 NullLiteral nl
= expr
as NullLiteral
;
687 return nl
.ConvertImplicitly (target_type
) != null;
690 if (expr_type
== TypeManager
.void_type
)
693 if (expr
.eclass
== ExprClass
.MethodGroup
) {
694 if (TypeManager
.IsDelegateType (target_type
) && RootContext
.Version
!= LanguageVersion
.ISO_1
) {
695 MethodGroupExpr mg
= expr
as MethodGroupExpr
;
697 return DelegateCreation
.ImplicitStandardConversionExists (mg
, target_type
) != null;
704 //Console.WriteLine ("Expr is {0}", expr);
705 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
706 if (TypeManager
.IsEqual (expr_type
, target_type
))
709 if (TypeManager
.IsNullableType (target_type
))
710 return ImplicitNulableConversion (null, expr
, target_type
) != null;
712 // First numeric conversions
713 if (ImplicitNumericConversion (null, expr_type
, target_type
) != null)
716 if (ImplicitReferenceConversionExists (expr
, target_type
))
720 if (ImplicitBoxingConversionExists (expr
, target_type
, out use_class_cast
))
724 // Implicit Constant Expression Conversions
726 if (expr
is IntConstant
){
727 int value = ((IntConstant
) expr
).Value
;
729 if (target_type
== TypeManager
.sbyte_type
){
730 if (value >= SByte
.MinValue
&& value <= SByte
.MaxValue
)
732 } else if (target_type
== TypeManager
.byte_type
){
733 if (value >= 0 && value <= Byte
.MaxValue
)
735 } else if (target_type
== TypeManager
.short_type
){
736 if (value >= Int16
.MinValue
&& value <= Int16
.MaxValue
)
738 } else if (target_type
== TypeManager
.ushort_type
){
739 if (value >= UInt16
.MinValue
&& value <= UInt16
.MaxValue
)
741 } else if (target_type
== TypeManager
.uint32_type
){
744 } else if (target_type
== TypeManager
.uint64_type
){
746 // we can optimize this case: a positive int32
747 // always fits on a uint64. But we need an opcode
754 if (value == 0 && expr
is IntLiteral
&& TypeManager
.IsEnumType (target_type
))
758 if (expr
is LongConstant
&& target_type
== TypeManager
.uint64_type
){
760 // Try the implicit constant expression conversion
761 // from long to ulong, instead of a nice routine,
764 long v
= ((LongConstant
) expr
).Value
;
770 // If `expr_type' implements `target_type' (which is an iface)
771 // see TryImplicitIntConversion
773 if (target_type
.IsInterface
&& TypeManager
.ImplementsInterface (expr_type
, target_type
))
776 if (target_type
== TypeManager
.void_ptr_type
&& expr_type
.IsPointer
)
783 /// Finds "most encompassed type" according to the spec (13.4.2)
784 /// amongst the methods in the MethodGroupExpr
786 static Type
FindMostEncompassedType (ArrayList types
)
790 if (types
.Count
== 0)
793 if (types
.Count
== 1)
794 return (Type
) types
[0];
796 EmptyExpression expr
= EmptyExpression
.Grab ();
798 foreach (Type t
in types
) {
805 if (ImplicitStandardConversionExists (expr
, best
))
810 foreach (Type t
in types
) {
813 if (!ImplicitStandardConversionExists (expr
, t
)) {
819 EmptyExpression
.Release (expr
);
825 /// Finds "most encompassing type" according to the spec (13.4.2)
826 /// amongst the types in the given set
828 static Type
FindMostEncompassingType (ArrayList types
)
832 if (types
.Count
== 0)
835 if (types
.Count
== 1)
836 return (Type
) types
[0];
838 EmptyExpression expr
= EmptyExpression
.Grab ();
840 foreach (Type t
in types
) {
847 if (ImplicitStandardConversionExists (expr
, t
))
851 foreach (Type t
in types
) {
855 if (!ImplicitStandardConversionExists (expr
, best
)) {
861 EmptyExpression
.Release (expr
);
867 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
868 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
869 /// for explicit and implicit conversion operators.
871 static public Type
FindMostSpecificSource (IList list
,
872 Expression source
, bool apply_explicit_conv_rules
)
874 ArrayList src_types_set
= new ArrayList ();
877 // If any operator converts from S then Sx = S
879 Type source_type
= source
.Type
;
880 foreach (MethodBase mb
in list
){
881 AParametersCollection pd
= TypeManager
.GetParameterData (mb
);
882 Type param_type
= pd
.Types
[0];
884 if (param_type
== source_type
)
887 src_types_set
.Add (param_type
);
891 // Explicit Conv rules
893 if (apply_explicit_conv_rules
) {
894 ArrayList candidate_set
= new ArrayList ();
896 foreach (Type param_type
in src_types_set
){
897 if (ImplicitStandardConversionExists (source
, param_type
))
898 candidate_set
.Add (param_type
);
901 if (candidate_set
.Count
!= 0)
902 return FindMostEncompassedType (candidate_set
);
908 if (apply_explicit_conv_rules
)
909 return FindMostEncompassingType (src_types_set
);
911 return FindMostEncompassedType (src_types_set
);
915 /// Finds the most specific target Tx according to section 13.4.4
917 static public Type
FindMostSpecificTarget (IList list
,
918 Type target
, bool apply_explicit_conv_rules
)
920 ArrayList tgt_types_set
= new ArrayList ();
923 // If any operator converts to T then Tx = T
925 foreach (MethodInfo mi
in list
){
926 Type ret_type
= TypeManager
.TypeToCoreType (mi
.ReturnType
);
927 if (ret_type
== target
)
930 tgt_types_set
.Add (ret_type
);
934 // Explicit conv rules
936 if (apply_explicit_conv_rules
) {
937 ArrayList candidate_set
= new ArrayList ();
939 EmptyExpression expr
= EmptyExpression
.Grab ();
941 foreach (Type ret_type
in tgt_types_set
){
942 expr
.SetType (ret_type
);
944 if (ImplicitStandardConversionExists (expr
, target
))
945 candidate_set
.Add (ret_type
);
948 EmptyExpression
.Release (expr
);
950 if (candidate_set
.Count
!= 0)
951 return FindMostEncompassingType (candidate_set
);
955 // Okay, final case !
957 if (apply_explicit_conv_rules
)
958 return FindMostEncompassedType (tgt_types_set
);
960 return FindMostEncompassingType (tgt_types_set
);
964 /// User-defined Implicit conversions
966 static public Expression
ImplicitUserConversion (EmitContext ec
, Expression source
,
967 Type target
, Location loc
)
969 Expression expr
= UserDefinedConversion (ec
, source
, target
, loc
, false);
970 if (expr
!= null && !TypeManager
.IsEqual (expr
.Type
, target
))
971 expr
= ImplicitConversionStandard (ec
, expr
, target
, loc
);
977 /// User-defined Explicit conversions
979 static public Expression
ExplicitUserConversion (EmitContext ec
, Expression source
,
980 Type target
, Location loc
)
982 Expression expr
= UserDefinedConversion (ec
, source
, target
, loc
, true);
983 if (expr
!= null && !TypeManager
.IsEqual (expr
.Type
, target
))
984 expr
= ExplicitConversionStandard (ec
, expr
, target
, loc
);
989 static void AddConversionOperators (ArrayList list
,
990 Expression source
, Type target_type
,
991 bool look_for_explicit
,
997 Type source_type
= source
.Type
;
998 EmptyExpression expr
= EmptyExpression
.Grab ();
1001 // LAMESPEC: Undocumented IntPtr/UIntPtr conversions
1002 // IntPtr -> uint uses int
1003 // UIntPtr -> long uses ulong
1005 if (source_type
== TypeManager
.intptr_type
) {
1006 if (target_type
== TypeManager
.uint32_type
)
1007 target_type
= TypeManager
.int32_type
;
1008 } else if (source_type
== TypeManager
.uintptr_type
) {
1009 if (target_type
== TypeManager
.int64_type
)
1010 target_type
= TypeManager
.uint64_type
;
1013 foreach (MethodInfo m
in mg
.Methods
) {
1014 AParametersCollection pd
= TypeManager
.GetParameterData (m
);
1015 Type return_type
= TypeManager
.TypeToCoreType (m
.ReturnType
);
1016 Type arg_type
= pd
.Types
[0];
1018 if (source_type
!= arg_type
) {
1019 if (!ImplicitStandardConversionExists (source
, arg_type
)) {
1020 if (!look_for_explicit
)
1022 expr
.SetType (arg_type
);
1023 if (!ImplicitStandardConversionExists (expr
, source_type
))
1028 if (target_type
!= return_type
) {
1029 expr
.SetType (return_type
);
1030 if (!ImplicitStandardConversionExists (expr
, target_type
)) {
1031 if (!look_for_explicit
)
1033 expr
.SetType (target_type
);
1034 if (!ImplicitStandardConversionExists (expr
, return_type
))
1039 // See LAMESPEC: Exclude IntPtr -> int conversion
1040 if (source_type
== TypeManager
.uintptr_type
&& return_type
== TypeManager
.uint32_type
)
1046 EmptyExpression
.Release (expr
);
1050 /// Compute the user-defined conversion operator from source_type to target_type.
1051 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1053 static MethodInfo
GetConversionOperator (Type container_type
, Expression source
, Type target_type
, bool look_for_explicit
)
1055 ArrayList ops
= new ArrayList (4);
1057 Type source_type
= source
.Type
;
1059 if (source_type
!= TypeManager
.decimal_type
) {
1060 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1061 Expression
.MethodLookup (container_type
, source_type
, "op_Implicit", Location
.Null
) as MethodGroupExpr
);
1062 if (look_for_explicit
) {
1063 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1064 Expression
.MethodLookup (
1065 container_type
, source_type
, "op_Explicit", Location
.Null
) as MethodGroupExpr
);
1069 if (target_type
!= TypeManager
.decimal_type
) {
1070 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1071 Expression
.MethodLookup (container_type
, target_type
, "op_Implicit", Location
.Null
) as MethodGroupExpr
);
1072 if (look_for_explicit
) {
1073 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1074 Expression
.MethodLookup (
1075 container_type
, target_type
, "op_Explicit", Location
.Null
) as MethodGroupExpr
);
1082 Type most_specific_source
= FindMostSpecificSource (ops
, source
, look_for_explicit
);
1083 if (most_specific_source
== null)
1086 Type most_specific_target
= FindMostSpecificTarget (ops
, target_type
, look_for_explicit
);
1087 if (most_specific_target
== null)
1090 MethodInfo method
= null;
1092 foreach (MethodInfo m
in ops
) {
1093 if (TypeManager
.TypeToCoreType (m
.ReturnType
) != most_specific_target
)
1095 if (TypeManager
.GetParameterData (m
).Types
[0] != most_specific_source
)
1097 // Ambiguous: more than one conversion operator satisfies the signature.
1107 /// User-defined conversions
1109 static public Expression
UserDefinedConversion (EmitContext ec
, Expression source
,
1110 Type target
, Location loc
,
1111 bool look_for_explicit
)
1113 Type source_type
= source
.Type
;
1114 MethodInfo method
= null;
1118 if (look_for_explicit
) {
1119 hash
= explicit_conv
;
1121 // Implicit user operators cannot convert to interfaces
1122 if (target
.IsInterface
)
1125 hash
= implicit_conv
;
1128 if (!(source
is Constant
) && hash
.Lookup (source_type
, target
, out o
)) {
1129 method
= (MethodInfo
) o
;
1131 method
= GetConversionOperator (null, source
, target
, look_for_explicit
);
1132 if (!(source
is Constant
))
1133 hash
.Insert (source_type
, target
, method
);
1139 Type most_specific_source
= TypeManager
.GetParameterData (method
).Types
[0];
1142 // This will do the conversion to the best match that we
1143 // found. Now we need to perform an implict standard conversion
1144 // if the best match was not the type that we were requested
1147 if (look_for_explicit
)
1148 source
= ExplicitConversionStandard (ec
, source
, most_specific_source
, loc
);
1150 source
= ImplicitConversionStandard (ec
, source
, most_specific_source
, loc
);
1155 return new UserCast (method
, source
, loc
).DoResolve (ec
);
1159 /// Converts implicitly the resolved expression `expr' into the
1160 /// `target_type'. It returns a new expression that can be used
1161 /// in a context that expects a `target_type'.
1163 static public Expression
ImplicitConversion (EmitContext ec
, Expression expr
,
1164 Type target_type
, Location loc
)
1168 if (target_type
== null)
1169 throw new Exception ("Target type is null");
1171 e
= ImplicitConversionStandard (ec
, expr
, target_type
, loc
);
1175 e
= ImplicitUserConversion (ec
, expr
, target_type
, loc
);
1184 /// Attempts to apply the `Standard Implicit
1185 /// Conversion' rules to the expression `expr' into
1186 /// the `target_type'. It returns a new expression
1187 /// that can be used in a context that expects a
1190 /// This is different from `ImplicitConversion' in that the
1191 /// user defined implicit conversions are excluded.
1193 static public Expression
ImplicitConversionStandard (EmitContext ec
, Expression expr
,
1194 Type target_type
, Location loc
)
1196 return ImplicitConversionStandard (ec
, expr
, target_type
, loc
, false);
1199 static Expression
ImplicitConversionStandard (EmitContext ec
, Expression expr
, Type target_type
, Location loc
, bool explicit_cast
)
1201 if (expr
.eclass
== ExprClass
.MethodGroup
){
1202 if (!TypeManager
.IsDelegateType (target_type
)){
1207 // Only allow anonymous method conversions on post ISO_1
1209 if (RootContext
.Version
!= LanguageVersion
.ISO_1
){
1210 MethodGroupExpr mg
= expr
as MethodGroupExpr
;
1212 return ImplicitDelegateCreation
.Create (
1213 ec
, mg
, target_type
, loc
);
1217 Type expr_type
= expr
.Type
;
1220 if (expr_type
.Equals (target_type
)) {
1221 if (expr_type
!= TypeManager
.null_type
&& expr_type
!= TypeManager
.anonymous_method_type
)
1226 if (TypeManager
.IsVariantOf (expr_type
, target_type
)) {
1230 if (TypeManager
.IsNullableType (target_type
))
1231 return ImplicitNulableConversion (ec
, expr
, target_type
);
1234 // Attempt to do the implicit constant expression conversions
1236 Constant c
= expr
as Constant
;
1239 c
= c
.ConvertImplicitly (target_type
);
1241 Console
.WriteLine ("Conversion error happened in line {0}", loc
);
1248 e
= ImplicitNumericConversion (expr
, expr_type
, target_type
);
1252 e
= ImplicitReferenceConversion (expr
, target_type
, explicit_cast
);
1256 if (expr
is IntConstant
&& TypeManager
.IsEnumType (target_type
)){
1257 Constant i
= (Constant
) expr
;
1259 // LAMESPEC: Conversion from any 0 constant is allowed
1261 // An implicit enumeration conversion permits the decimal-integer-literal 0
1262 // to be converted to any enum-type and to any nullable-type whose underlying
1263 // type is an enum-type
1265 if (i
.IsDefaultValue
)
1266 return new EnumConstant (i
, target_type
);
1270 if (expr_type
.IsPointer
){
1271 if (target_type
== TypeManager
.void_ptr_type
)
1272 return EmptyCast
.Create (expr
, target_type
);
1275 // yep, comparing pointer types cant be done with
1276 // t1 == t2, we have to compare their element types.
1278 if (target_type
.IsPointer
){
1279 if (TypeManager
.GetElementType(target_type
) == TypeManager
.GetElementType(expr_type
))
1286 if (expr_type
== TypeManager
.null_type
&& target_type
.IsPointer
)
1287 return EmptyCast
.Create (new NullPointer (loc
), target_type
);
1290 if (expr_type
== TypeManager
.anonymous_method_type
){
1291 AnonymousMethodExpression ame
= (AnonymousMethodExpression
) expr
;
1292 Expression am
= ame
.Compatible (ec
, target_type
);
1294 return am
.DoResolve (ec
);
1301 /// Attempts to implicitly convert `source' into `target_type', using
1302 /// ImplicitConversion. If there is no implicit conversion, then
1303 /// an error is signaled
1305 static public Expression
ImplicitConversionRequired (EmitContext ec
, Expression source
,
1306 Type target_type
, Location loc
)
1308 Expression e
= ImplicitConversion (ec
, source
, target_type
, loc
);
1312 source
.Error_ValueCannotBeConverted (ec
, loc
, target_type
, false);
1317 /// Performs the explicit numeric conversions
1319 /// There are a few conversions that are not part of the C# standard,
1320 /// they were interim hacks in the C# compiler that were supposed to
1321 /// become explicit operators in the UIntPtr class and IntPtr class,
1322 /// but for historical reasons it did not happen, so the C# compiler
1323 /// ended up with these special hacks.
1325 /// See bug 59800 for details.
1327 /// The conversion are:
1337 public static Expression
ExplicitNumericConversion (Expression expr
, Type target_type
)
1339 Type expr_type
= expr
.Type
;
1340 Type real_target_type
= target_type
;
1342 if (expr_type
== TypeManager
.sbyte_type
){
1344 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1346 if (real_target_type
== TypeManager
.byte_type
)
1347 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U1
);
1348 if (real_target_type
== TypeManager
.ushort_type
)
1349 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U2
);
1350 if (real_target_type
== TypeManager
.uint32_type
)
1351 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U4
);
1352 if (real_target_type
== TypeManager
.uint64_type
)
1353 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U8
);
1354 if (real_target_type
== TypeManager
.char_type
)
1355 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_CH
);
1357 // One of the built-in conversions that belonged in the class library
1358 if (real_target_type
== TypeManager
.uintptr_type
){
1359 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I1_U8
);
1361 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1363 } else if (expr_type
== TypeManager
.byte_type
){
1365 // From byte to sbyte and char
1367 if (real_target_type
== TypeManager
.sbyte_type
)
1368 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U1_I1
);
1369 if (real_target_type
== TypeManager
.char_type
)
1370 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U1_CH
);
1371 } else if (expr_type
== TypeManager
.short_type
){
1373 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1375 if (real_target_type
== TypeManager
.sbyte_type
)
1376 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_I1
);
1377 if (real_target_type
== TypeManager
.byte_type
)
1378 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U1
);
1379 if (real_target_type
== TypeManager
.ushort_type
)
1380 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U2
);
1381 if (real_target_type
== TypeManager
.uint32_type
)
1382 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U4
);
1383 if (real_target_type
== TypeManager
.uint64_type
)
1384 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U8
);
1385 if (real_target_type
== TypeManager
.char_type
)
1386 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_CH
);
1388 // One of the built-in conversions that belonged in the class library
1389 if (real_target_type
== TypeManager
.uintptr_type
){
1390 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I2_U8
);
1392 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1394 } else if (expr_type
== TypeManager
.ushort_type
){
1396 // From ushort to sbyte, byte, short, char
1398 if (real_target_type
== TypeManager
.sbyte_type
)
1399 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_I1
);
1400 if (real_target_type
== TypeManager
.byte_type
)
1401 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_U1
);
1402 if (real_target_type
== TypeManager
.short_type
)
1403 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_I2
);
1404 if (real_target_type
== TypeManager
.char_type
)
1405 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_CH
);
1406 } else if (expr_type
== TypeManager
.int32_type
){
1408 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1410 if (real_target_type
== TypeManager
.sbyte_type
)
1411 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_I1
);
1412 if (real_target_type
== TypeManager
.byte_type
)
1413 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U1
);
1414 if (real_target_type
== TypeManager
.short_type
)
1415 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_I2
);
1416 if (real_target_type
== TypeManager
.ushort_type
)
1417 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U2
);
1418 if (real_target_type
== TypeManager
.uint32_type
)
1419 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U4
);
1420 if (real_target_type
== TypeManager
.uint64_type
)
1421 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U8
);
1422 if (real_target_type
== TypeManager
.char_type
)
1423 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_CH
);
1425 // One of the built-in conversions that belonged in the class library
1426 if (real_target_type
== TypeManager
.uintptr_type
){
1427 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I2_U8
);
1429 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1431 } else if (expr_type
== TypeManager
.uint32_type
){
1433 // From uint to sbyte, byte, short, ushort, int, char
1435 if (real_target_type
== TypeManager
.sbyte_type
)
1436 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I1
);
1437 if (real_target_type
== TypeManager
.byte_type
)
1438 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_U1
);
1439 if (real_target_type
== TypeManager
.short_type
)
1440 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I2
);
1441 if (real_target_type
== TypeManager
.ushort_type
)
1442 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_U2
);
1443 if (real_target_type
== TypeManager
.int32_type
)
1444 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I4
);
1445 if (real_target_type
== TypeManager
.char_type
)
1446 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_CH
);
1447 } else if (expr_type
== TypeManager
.int64_type
){
1449 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1451 if (real_target_type
== TypeManager
.sbyte_type
)
1452 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I1
);
1453 if (real_target_type
== TypeManager
.byte_type
)
1454 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U1
);
1455 if (real_target_type
== TypeManager
.short_type
)
1456 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I2
);
1457 if (real_target_type
== TypeManager
.ushort_type
)
1458 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U2
);
1459 if (real_target_type
== TypeManager
.int32_type
)
1460 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I4
);
1461 if (real_target_type
== TypeManager
.uint32_type
)
1462 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U4
);
1463 if (real_target_type
== TypeManager
.uint64_type
)
1464 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U8
);
1465 if (real_target_type
== TypeManager
.char_type
)
1466 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_CH
);
1467 } else if (expr_type
== TypeManager
.uint64_type
){
1469 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1471 if (real_target_type
== TypeManager
.sbyte_type
)
1472 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I1
);
1473 if (real_target_type
== TypeManager
.byte_type
)
1474 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U1
);
1475 if (real_target_type
== TypeManager
.short_type
)
1476 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I2
);
1477 if (real_target_type
== TypeManager
.ushort_type
)
1478 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U2
);
1479 if (real_target_type
== TypeManager
.int32_type
)
1480 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I4
);
1481 if (real_target_type
== TypeManager
.uint32_type
)
1482 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U4
);
1483 if (real_target_type
== TypeManager
.int64_type
)
1484 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I8
);
1485 if (real_target_type
== TypeManager
.char_type
)
1486 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_CH
);
1488 // One of the built-in conversions that belonged in the class library
1489 if (real_target_type
== TypeManager
.intptr_type
){
1490 return new OperatorCast (EmptyCast
.Create (expr
, TypeManager
.int64_type
),
1491 TypeManager
.intptr_type
, true);
1493 } else if (expr_type
== TypeManager
.char_type
){
1495 // From char to sbyte, byte, short
1497 if (real_target_type
== TypeManager
.sbyte_type
)
1498 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_I1
);
1499 if (real_target_type
== TypeManager
.byte_type
)
1500 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_U1
);
1501 if (real_target_type
== TypeManager
.short_type
)
1502 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_I2
);
1503 } else if (expr_type
== TypeManager
.float_type
){
1505 // From float to sbyte, byte, short,
1506 // ushort, int, uint, long, ulong, char
1509 if (real_target_type
== TypeManager
.sbyte_type
)
1510 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I1
);
1511 if (real_target_type
== TypeManager
.byte_type
)
1512 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U1
);
1513 if (real_target_type
== TypeManager
.short_type
)
1514 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I2
);
1515 if (real_target_type
== TypeManager
.ushort_type
)
1516 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U2
);
1517 if (real_target_type
== TypeManager
.int32_type
)
1518 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I4
);
1519 if (real_target_type
== TypeManager
.uint32_type
)
1520 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U4
);
1521 if (real_target_type
== TypeManager
.int64_type
)
1522 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I8
);
1523 if (real_target_type
== TypeManager
.uint64_type
)
1524 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U8
);
1525 if (real_target_type
== TypeManager
.char_type
)
1526 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_CH
);
1527 if (real_target_type
== TypeManager
.decimal_type
)
1528 return new CastToDecimal (expr
, true);
1529 } else if (expr_type
== TypeManager
.double_type
){
1531 // From double to sbyte, byte, short,
1532 // ushort, int, uint, long, ulong,
1533 // char, float or decimal
1535 if (real_target_type
== TypeManager
.sbyte_type
)
1536 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I1
);
1537 if (real_target_type
== TypeManager
.byte_type
)
1538 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U1
);
1539 if (real_target_type
== TypeManager
.short_type
)
1540 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I2
);
1541 if (real_target_type
== TypeManager
.ushort_type
)
1542 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U2
);
1543 if (real_target_type
== TypeManager
.int32_type
)
1544 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I4
);
1545 if (real_target_type
== TypeManager
.uint32_type
)
1546 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U4
);
1547 if (real_target_type
== TypeManager
.int64_type
)
1548 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I8
);
1549 if (real_target_type
== TypeManager
.uint64_type
)
1550 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U8
);
1551 if (real_target_type
== TypeManager
.char_type
)
1552 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_CH
);
1553 if (real_target_type
== TypeManager
.float_type
)
1554 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_R4
);
1555 if (real_target_type
== TypeManager
.decimal_type
)
1556 return new CastToDecimal (expr
, true);
1557 } else if (expr_type
== TypeManager
.uintptr_type
){
1559 // Various built-in conversions that belonged in the class library
1561 // from uintptr to sbyte, short, int32
1563 if (real_target_type
== TypeManager
.sbyte_type
){
1564 Expression uint32e
= new OperatorCast (expr
, TypeManager
.uint32_type
, true);
1565 return new ConvCast (uint32e
, TypeManager
.sbyte_type
, ConvCast
.Mode
.U4_I1
);
1567 if (real_target_type
== TypeManager
.short_type
){
1568 Expression uint32e
= new OperatorCast (expr
, TypeManager
.uint32_type
, true);
1569 return new ConvCast (uint32e
, TypeManager
.sbyte_type
, ConvCast
.Mode
.U4_I2
);
1571 if (real_target_type
== TypeManager
.int32_type
){
1572 return EmptyCast
.Create (new OperatorCast (expr
, TypeManager
.uint32_type
, true),
1573 TypeManager
.int32_type
);
1575 } else if (expr_type
== TypeManager
.intptr_type
){
1576 if (real_target_type
== TypeManager
.uint64_type
){
1577 return EmptyCast
.Create (new OperatorCast (expr
, TypeManager
.int64_type
, true),
1578 TypeManager
.uint64_type
);
1580 } else if (expr_type
== TypeManager
.decimal_type
) {
1581 return new CastFromDecimal (expr
, target_type
).Resolve ();
1587 /// Returns whether an explicit reference conversion can be performed
1588 /// from source_type to target_type
1590 public static bool ExplicitReferenceConversionExists (Type source_type
, Type target_type
)
1592 Expression e
= ExplicitReferenceConversion (null, source_type
, target_type
);
1596 if (e
== EmptyExpression
.Null
)
1599 throw new InternalErrorException ("Invalid probing conversion result");
1603 /// Implements Explicit Reference conversions
1605 static Expression
ExplicitReferenceConversion (Expression source
, Type source_type
, Type target_type
)
1607 bool target_is_value_type
= TypeManager
.IsStruct (target_type
);
1610 // From object to a generic parameter
1612 if (source_type
== TypeManager
.object_type
&& TypeManager
.IsGenericParameter (target_type
))
1613 return source
== null ? EmptyExpression
.Null
: new UnboxCast (source
, target_type
);
1616 // Explicit type parameter conversion.
1618 if (TypeManager
.IsGenericParameter (source_type
))
1619 return ExplicitTypeParameterConversion (source
, source_type
, target_type
);
1622 // From object to any reference type or value type (unboxing)
1624 if (source_type
== TypeManager
.object_type
)
1625 return source
== null ? EmptyExpression
.Null
:
1626 target_is_value_type
? (Expression
) new UnboxCast (source
, target_type
) : new ClassCast (source
, target_type
);
1629 // Unboxing conversion from the types object and System.ValueType to any non-nullable-value-type
1631 if (source_type
== TypeManager
.value_type
&& target_is_value_type
)
1632 return source
== null ? EmptyExpression
.Null
: new UnboxCast (source
, target_type
);
1635 // From any class S to any class-type T, provided S is a base class of T
1637 if (TypeManager
.IsSubclassOf (target_type
, source_type
))
1638 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1641 // From any class type S to any interface T, provides S is not sealed
1642 // and provided S does not implement T.
1644 if (target_type
.IsInterface
&& !source_type
.IsSealed
&&
1645 !TypeManager
.ImplementsInterface (source_type
, target_type
)) {
1646 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1650 // From any interface-type S to to any class type T, provided T is not
1651 // sealed, or provided T implements S.
1653 if (source_type
.IsInterface
) {
1654 if (!target_type
.IsSealed
|| TypeManager
.ImplementsInterface (target_type
, source_type
)) {
1655 if (target_type
.IsClass
)
1656 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1659 // Unboxing conversion from any interface-type to any non-nullable-value-type that
1660 // implements the interface-type
1662 return source
== null ? EmptyExpression
.Null
: new UnboxCast (source
, target_type
);
1666 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1667 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1669 if (IList_To_Array (source_type
, target_type
))
1670 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1675 if (source_type
.IsArray
) {
1676 if (target_type
.IsArray
) {
1678 // From System.Array to any array-type
1680 if (source_type
== TypeManager
.array_type
)
1681 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1684 // From an array type S with an element type Se to an array type T with an
1685 // element type Te provided all the following are true:
1686 // * S and T differe only in element type, in other words, S and T
1687 // have the same number of dimensions.
1688 // * Both Se and Te are reference types
1689 // * An explicit reference conversions exist from Se to Te
1691 if (source_type
.GetArrayRank () == target_type
.GetArrayRank ()) {
1693 source_type
= TypeManager
.GetElementType (source_type
);
1694 if (!TypeManager
.IsReferenceType (source_type
))
1697 Type target_type_element
= TypeManager
.GetElementType (target_type
);
1698 if (!TypeManager
.IsReferenceType (target_type_element
))
1701 if (ExplicitReferenceConversionExists (source_type
, target_type_element
))
1702 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1709 // From a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces,
1710 // provided that there is an explicit reference conversion from S to T
1712 if (Array_To_IList (source_type
, target_type
, true))
1713 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1719 // From System delegate to any delegate-type
1721 if (source_type
== TypeManager
.delegate_type
&& TypeManager
.IsDelegateType (target_type
))
1722 return source
== null ? EmptyExpression
.Null
: new ClassCast (source
, target_type
);
1728 /// Performs an explicit conversion of the expression `expr' whose
1729 /// type is expr.Type to `target_type'.
1731 static public Expression
ExplicitConversionCore (EmitContext ec
, Expression expr
,
1732 Type target_type
, Location loc
)
1734 Type expr_type
= expr
.Type
;
1736 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1737 Expression ne
= ImplicitConversionStandard (ec
, expr
, target_type
, loc
, true);
1741 if (TypeManager
.IsEnumType (expr_type
)) {
1742 Expression underlying
= EmptyCast
.Create (expr
, TypeManager
.GetEnumUnderlyingType (expr_type
));
1743 expr
= ExplicitConversionCore (ec
, underlying
, target_type
, loc
);
1747 return ExplicitUserConversion (ec
, underlying
, target_type
, loc
);
1750 if (TypeManager
.IsEnumType (target_type
)){
1752 // Type System.Enum can be unboxed to any enum-type
1754 if (expr_type
== TypeManager
.enum_type
)
1755 return new UnboxCast (expr
, target_type
);
1757 Expression ce
= ExplicitConversionCore (ec
, expr
, TypeManager
.GetEnumUnderlyingType (target_type
), loc
);
1759 return EmptyCast
.Create (ce
, target_type
);
1762 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1764 if (expr_type
== TypeManager
.intptr_type
|| expr_type
== TypeManager
.uintptr_type
) {
1765 ne
= ExplicitUserConversion (ec
, expr
, TypeManager
.GetEnumUnderlyingType (target_type
), loc
);
1767 return ExplicitConversionCore (ec
, ne
, target_type
, loc
);
1773 ne
= ExplicitNumericConversion (expr
, target_type
);
1778 // Skip the ExplicitReferenceConversion because we can not convert
1779 // from Null to a ValueType, and ExplicitReference wont check against
1780 // null literal explicitly
1782 if (expr_type
!= TypeManager
.null_type
){
1783 ne
= ExplicitReferenceConversion (expr
, expr_type
, target_type
);
1789 ne
= ExplicitUnsafe (expr
, target_type
);
1797 public static Expression
ExplicitUnsafe (Expression expr
, Type target_type
)
1799 Type expr_type
= expr
.Type
;
1801 if (target_type
.IsPointer
){
1802 if (expr_type
.IsPointer
)
1803 return EmptyCast
.Create (expr
, target_type
);
1805 if (expr_type
== TypeManager
.sbyte_type
||
1806 expr_type
== TypeManager
.short_type
||
1807 expr_type
== TypeManager
.int32_type
)
1808 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I
);
1810 if (expr_type
== TypeManager
.ushort_type
||
1811 expr_type
== TypeManager
.uint32_type
||
1812 expr_type
== TypeManager
.uint64_type
|| expr_type
== TypeManager
.int64_type
||
1813 expr_type
== TypeManager
.byte_type
)
1814 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U
);
1817 if (expr_type
.IsPointer
){
1818 if (target_type
== TypeManager
.sbyte_type
)
1819 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I1
);
1820 else if (target_type
== TypeManager
.byte_type
)
1821 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U1
);
1822 else if (target_type
== TypeManager
.short_type
)
1823 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I2
);
1824 else if (target_type
== TypeManager
.ushort_type
)
1825 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U2
);
1826 else if (target_type
== TypeManager
.int32_type
)
1827 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I4
);
1828 else if (target_type
== TypeManager
.uint32_type
)
1829 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U4
);
1830 else if (target_type
== TypeManager
.uint64_type
|| target_type
== TypeManager
.int64_type
)
1831 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
1837 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1839 static public Expression
ExplicitConversionStandard (EmitContext ec
, Expression expr
,
1840 Type target_type
, Location l
)
1842 int errors
= Report
.Errors
;
1843 Expression ne
= ImplicitConversionStandard (ec
, expr
, target_type
, l
);
1844 if (Report
.Errors
> errors
)
1850 ne
= ExplicitNumericConversion (expr
, target_type
);
1854 ne
= ExplicitReferenceConversion (expr
, expr
.Type
, target_type
);
1858 if (ec
.InUnsafe
&& expr
.Type
== TypeManager
.void_ptr_type
&& target_type
.IsPointer
)
1859 return EmptyCast
.Create (expr
, target_type
);
1861 expr
.Error_ValueCannotBeConverted (ec
, l
, target_type
, true);
1866 /// Performs an explicit conversion of the expression `expr' whose
1867 /// type is expr.Type to `target_type'.
1869 static public Expression
ExplicitConversion (EmitContext ec
, Expression expr
,
1870 Type target_type
, Location loc
)
1872 Expression e
= ExplicitConversionCore (ec
, expr
, target_type
, loc
);
1876 Type expr_type
= expr
.Type
;
1877 if (TypeManager
.IsNullableType (target_type
)) {
1878 if (TypeManager
.IsNullableType (expr_type
)) {
1879 Type target
= TypeManager
.GetTypeArguments (target_type
)[0];
1880 Expression unwrap
= Nullable
.Unwrap
.Create (expr
);
1881 e
= ExplicitConversion (ec
, unwrap
, target
, expr
.Location
);
1885 return new Nullable
.Lifted (e
, unwrap
, target_type
).Resolve (ec
);
1886 } else if (expr_type
== TypeManager
.object_type
) {
1887 return new UnboxCast (expr
, target_type
);
1889 Type target
= TypeManager
.GetTypeArguments (target_type
) [0];
1891 e
= ExplicitConversionCore (ec
, expr
, target
, loc
);
1893 return Nullable
.Wrap
.Create (e
, target_type
);
1895 } else if (TypeManager
.IsNullableType (expr_type
)) {
1896 e
= Nullable
.Unwrap
.Create (expr
, false);
1898 bool use_class_cast
;
1899 if (ImplicitBoxingConversionExists (e
, target_type
, out use_class_cast
))
1900 return new BoxedCast (expr
, target_type
);
1902 e
= ExplicitConversion (ec
, e
, target_type
, loc
);
1904 e
= EmptyCast
.Create (e
, target_type
);
1908 e
= ExplicitUserConversion (ec
, expr
, target_type
, loc
);
1912 expr
.Error_ValueCannotBeConverted (ec
, loc
, target_type
, true);