2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // (C) 2001, 2002, 2003 Ximian, Inc.
12 namespace Mono
.CSharp
{
14 using System
.Collections
;
15 using System
.Diagnostics
;
16 using System
.Reflection
;
17 using System
.Reflection
.Emit
;
20 // A container class for all the conversion operations
22 public class Convert
{
24 static bool TypeParameter_to_Null (Type target_type
)
26 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (target_type
);
30 if (gc
.HasReferenceTypeConstraint
)
32 if (gc
.HasClassConstraint
&& !TypeManager
.IsValueType (gc
.ClassConstraint
))
38 static Type
TypeParam_EffectiveBaseType (GenericConstraints gc
)
40 ArrayList list
= new ArrayList ();
41 list
.Add (gc
.EffectiveBaseClass
);
42 foreach (Type t
in gc
.InterfaceConstraints
) {
43 if (!TypeManager
.IsGenericParameter (t
))
46 GenericConstraints new_gc
= TypeManager
.GetTypeParameterConstraints (t
);
48 list
.Add (TypeParam_EffectiveBaseType (new_gc
));
50 return FindMostEncompassedType (list
);
55 // From a one-dimensional array-type S[] to System.Collections.IList<S> and base
56 // interfaces of this interface.
58 // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
59 // interfaces of this interface, provided there is an implicit reference conversion
62 static bool Array_To_IList (Type array
, Type list
)
65 if (!array
.IsArray
|| (array
.GetArrayRank () != 1) || !list
.IsGenericType
)
68 Type gt
= list
.GetGenericTypeDefinition ();
69 if ((gt
!= TypeManager
.generic_ilist_type
) &&
70 (gt
!= TypeManager
.generic_icollection_type
) &&
71 (gt
!= TypeManager
.generic_ienumerable_type
))
74 Type element_type
= TypeManager
.GetElementType (array
);
75 Type arg_type
= TypeManager
.GetTypeArguments (list
) [0];
77 if (element_type
== arg_type
)
80 if (MyEmptyExpr
== null)
81 MyEmptyExpr
= new EmptyExpression ();
82 MyEmptyExpr
.SetType (TypeManager
.GetElementType (array
));
84 return ImplicitReferenceConversionCore (MyEmptyExpr
, arg_type
);
90 static bool IList_To_Array(Type list
, Type array
)
93 if (!list
.IsGenericType
|| !array
.IsArray
|| array
.GetArrayRank() != 1)
96 Type gt
= list
.GetGenericTypeDefinition();
97 if (gt
!= TypeManager
.generic_ilist_type
&&
98 gt
!= TypeManager
.generic_icollection_type
&&
99 gt
!= TypeManager
.generic_ienumerable_type
)
102 Type arg_type
= TypeManager
.GetTypeArguments(list
)[0];
103 Type element_type
= TypeManager
.GetElementType(array
);
105 if (element_type
== arg_type
)
108 if (MyEmptyExpr
== null)
109 MyEmptyExpr
= new EmptyExpression();
110 MyEmptyExpr
.SetType(element_type
);
111 return ImplicitReferenceConversionExists(MyEmptyExpr
, arg_type
) || ExplicitReferenceConversionExists(element_type
, arg_type
);
117 static Expression
ImplicitTypeParameterConversion (Expression expr
,
121 Type expr_type
= expr
.Type
;
123 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (expr_type
);
126 if (target_type
== TypeManager
.object_type
)
127 return new BoxedCast (expr
, target_type
);
132 // We're converting from a type parameter which is known to be a reference type.
133 Type base_type
= TypeParam_EffectiveBaseType (gc
);
135 if (TypeManager
.IsSubclassOf (base_type
, target_type
))
136 return new ClassCast (expr
, target_type
);
138 if (target_type
.IsInterface
) {
139 if (TypeManager
.ImplementsInterface (base_type
, target_type
))
140 return new ClassCast (expr
, target_type
);
142 foreach (Type t
in gc
.InterfaceConstraints
) {
143 if (TypeManager
.IsSubclassOf (t
, target_type
))
144 return new ClassCast (expr
, target_type
);
145 if (TypeManager
.ImplementsInterface (t
, target_type
))
146 return new ClassCast (expr
, target_type
);
150 foreach (Type t
in gc
.InterfaceConstraints
) {
151 if (!TypeManager
.IsGenericParameter (t
))
153 if (TypeManager
.IsSubclassOf (t
, target_type
))
154 return new ClassCast (expr
, target_type
);
155 if (TypeManager
.ImplementsInterface (t
, target_type
))
156 return new ClassCast (expr
, target_type
);
162 static bool ImplicitTypeParameterBoxingConversion (Type expr_type
, Type target_type
,
163 out bool use_class_cast
)
166 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (expr_type
);
169 use_class_cast
= false;
170 return target_type
== TypeManager
.object_type
;
173 use_class_cast
= true;
175 if (!gc
.HasReferenceTypeConstraint
)
178 // We're converting from a type parameter which is known to be a reference type.
179 Type base_type
= TypeParam_EffectiveBaseType (gc
);
181 if (TypeManager
.IsSubclassOf (base_type
, target_type
))
184 if (target_type
.IsInterface
) {
185 if (TypeManager
.ImplementsInterface (base_type
, target_type
))
188 foreach (Type t
in gc
.InterfaceConstraints
) {
189 if (TypeManager
.IsSubclassOf (t
, target_type
))
191 if (TypeManager
.ImplementsInterface (t
, target_type
))
196 foreach (Type t
in gc
.InterfaceConstraints
) {
197 if (!TypeManager
.IsGenericParameter (t
))
199 if (TypeManager
.IsSubclassOf (t
, target_type
))
201 if (TypeManager
.ImplementsInterface (t
, target_type
))
206 use_class_cast
= false;
210 static bool ExplicitTypeParameterConversionExists (Type source_type
, Type target_type
)
213 if (target_type
.IsInterface
)
216 if (TypeManager
.IsGenericParameter (target_type
)) {
217 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (target_type
);
221 foreach (Type iface
in gc
.InterfaceConstraints
) {
222 if (!TypeManager
.IsGenericParameter (iface
))
225 if (TypeManager
.IsSubclassOf (source_type
, iface
))
233 static Expression
ExplicitTypeParameterConversion (Expression source
, Type target_type
)
236 Type source_type
= source
.Type
;
238 if (target_type
.IsInterface
)
239 return new ClassCast (source
, target_type
);
241 if (TypeManager
.IsGenericParameter (target_type
)) {
242 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (target_type
);
246 foreach (Type iface
in gc
.InterfaceConstraints
) {
247 if (!TypeManager
.IsGenericParameter (iface
))
250 if (TypeManager
.IsSubclassOf (source_type
, iface
))
251 return new ClassCast (source
, target_type
);
258 static EmptyExpression MyEmptyExpr
;
259 static public Expression
ImplicitReferenceConversion (Expression expr
, Type target_type
)
261 Type expr_type
= expr
.Type
;
263 if (expr_type
== null && expr
.eclass
== ExprClass
.MethodGroup
){
264 // if we are a method group, emit a warning
269 if (expr_type
== TypeManager
.void_type
)
272 if (TypeManager
.IsGenericParameter (expr_type
))
273 return ImplicitTypeParameterConversion (expr
, target_type
);
275 // from the null type to any reference-type.
276 if (expr_type
== TypeManager
.null_type
) {
277 NullConstant nc
= (NullConstant
)expr
;
278 return nc
.ConvertImplicitly(target_type
);
281 if (ImplicitReferenceConversionCore (expr
, target_type
))
282 return new EmptyCast (expr
, target_type
);
285 if (ImplicitBoxingConversionExists (expr
, target_type
, out use_class_cast
)) {
287 return new ClassCast (expr
, target_type
);
289 return new BoxedCast (expr
, target_type
);
295 static public bool ImplicitReferenceConversionCore (Expression expr
, Type target_type
)
297 Type expr_type
= expr
.Type
;
300 // notice that it is possible to write "ValueType v = 1", the ValueType here
301 // is an abstract class, and not really a value type, so we apply the same rules.
303 if (target_type
== TypeManager
.object_type
) {
305 // A pointer type cannot be converted to object
307 if (expr_type
.IsPointer
)
310 if (TypeManager
.IsValueType (expr_type
))
312 if (expr_type
.IsClass
|| expr_type
.IsInterface
|| expr_type
== TypeManager
.enum_type
){
313 return expr_type
!= TypeManager
.anonymous_method_type
;
317 } else if (target_type
== TypeManager
.value_type
) {
318 return expr_type
== TypeManager
.enum_type
;
319 } else if (TypeManager
.IsSubclassOf (expr_type
, target_type
)) {
321 // Special case: enumeration to System.Enum.
322 // System.Enum is not a value type, it is a class, so we need
323 // a boxing conversion
325 if (expr_type
.IsEnum
|| TypeManager
.IsGenericParameter (expr_type
))
331 // This code is kind of mirrored inside ImplicitStandardConversionExists
332 // with the small distinction that we only probe there
334 // Always ensure that the code here and there is in sync
336 // from any class-type S to any interface-type T.
337 if (target_type
.IsInterface
) {
338 if (target_type
!= TypeManager
.iconvertible_type
&&
339 expr_type
.IsValueType
&& (expr
is Constant
) &&
340 !(expr
is IntLiteral
|| expr
is BoolLiteral
||
341 expr
is FloatLiteral
|| expr
is DoubleLiteral
||
342 expr
is LongLiteral
|| expr
is CharLiteral
||
343 expr
is StringLiteral
|| expr
is DecimalLiteral
||
344 expr
is UIntLiteral
|| expr
is ULongLiteral
)) {
348 if (TypeManager
.ImplementsInterface (expr_type
, target_type
)){
349 return !TypeManager
.IsGenericParameter (expr_type
) &&
350 !TypeManager
.IsValueType (expr_type
);
354 // from any interface type S to interface-type T.
355 if (expr_type
.IsInterface
&& target_type
.IsInterface
) {
356 return TypeManager
.ImplementsInterface (expr_type
, target_type
);
359 // from an array-type S to an array-type of type T
360 if (expr_type
.IsArray
&& target_type
.IsArray
) {
361 if (expr_type
.GetArrayRank () == target_type
.GetArrayRank ()) {
363 Type expr_element_type
= TypeManager
.GetElementType (expr_type
);
365 if (MyEmptyExpr
== null)
366 MyEmptyExpr
= new EmptyExpression ();
368 MyEmptyExpr
.SetType (expr_element_type
);
369 Type target_element_type
= TypeManager
.GetElementType (target_type
);
371 if (!expr_element_type
.IsValueType
&& !target_element_type
.IsValueType
)
372 return ImplicitStandardConversionExists (
373 MyEmptyExpr
, target_element_type
);
377 // from an array-type to System.Array
378 if (expr_type
.IsArray
&& target_type
== TypeManager
.array_type
)
381 // from an array-type of type T to IList<T>
382 if (expr_type
.IsArray
&& Array_To_IList (expr_type
, target_type
))
385 // from any delegate type to System.Delegate
386 if ((expr_type
== TypeManager
.delegate_type
|| TypeManager
.IsDelegateType (expr_type
)) &&
387 target_type
== TypeManager
.delegate_type
)
390 // from any array-type or delegate type into System.ICloneable.
391 if (expr_type
.IsArray
||
392 expr_type
== TypeManager
.delegate_type
|| TypeManager
.IsDelegateType (expr_type
))
393 if (target_type
== TypeManager
.icloneable_type
)
396 // from a generic type definition to a generic instance.
397 if (TypeManager
.IsEqual (expr_type
, target_type
))
403 static public bool ImplicitBoxingConversionExists (Expression expr
, Type target_type
,
404 out bool use_class_cast
)
406 Type expr_type
= expr
.Type
;
408 use_class_cast
= false;
411 // notice that it is possible to write "ValueType v = 1", the ValueType here
412 // is an abstract class, and not really a value type, so we apply the same rules.
414 if (target_type
== TypeManager
.object_type
) {
416 // A pointer type cannot be converted to object
418 if (expr_type
.IsPointer
)
421 return TypeManager
.IsValueType (expr_type
);
422 } else if (target_type
== TypeManager
.value_type
) {
423 return TypeManager
.IsValueType (expr_type
);
424 } else if (TypeManager
.IsSubclassOf (expr_type
, target_type
)) {
426 // Special case: enumeration to System.Enum.
427 // System.Enum is not a value type, it is a class, so we need
428 // a boxing conversion
430 if (expr_type
.IsEnum
|| TypeManager
.IsGenericParameter (expr_type
))
436 // This code is kind of mirrored inside ImplicitStandardConversionExists
437 // with the small distinction that we only probe there
439 // Always ensure that the code here and there is in sync
441 // from any class-type S to any interface-type T.
442 if (target_type
.IsInterface
) {
443 if (target_type
!= TypeManager
.iconvertible_type
&&
444 expr_type
.IsValueType
&& (expr
is Constant
) &&
445 !(expr
is IntLiteral
|| expr
is BoolLiteral
||
446 expr
is FloatLiteral
|| expr
is DoubleLiteral
||
447 expr
is LongLiteral
|| expr
is CharLiteral
||
448 expr
is StringLiteral
|| expr
is DecimalLiteral
||
449 expr
is UIntLiteral
|| expr
is ULongLiteral
)) {
453 if (TypeManager
.ImplementsInterface (expr_type
, target_type
))
454 return TypeManager
.IsGenericParameter (expr_type
) ||
455 TypeManager
.IsValueType (expr_type
);
458 if (TypeManager
.IsGenericParameter (expr_type
))
459 return ImplicitTypeParameterBoxingConversion (
460 expr_type
, target_type
, out use_class_cast
);
466 // Tests whether an implicit reference conversion exists between expr_type
469 public static bool ImplicitReferenceConversionExists (Expression expr
, Type target_type
)
471 if (target_type
.IsValueType
)
474 Type expr_type
= expr
.Type
;
476 // from the null type to any reference-type.
477 if (expr_type
== TypeManager
.null_type
){
478 if (target_type
.IsPointer
)
481 if (!target_type
.IsValueType
)
485 if (TypeManager
.IsGenericParameter (expr_type
))
486 return ImplicitTypeParameterConversion (expr
, target_type
) != null;
489 if (ImplicitReferenceConversionCore (expr
, target_type
) ||
490 ImplicitBoxingConversionExists (expr
, target_type
, out use_class_cast
))
497 /// Implicit Numeric Conversions.
499 /// expr is the expression to convert, returns a new expression of type
500 /// target_type or null if an implicit conversion is not possible.
502 static public Expression
ImplicitNumericConversion (Expression expr
,
505 Type expr_type
= expr
.Type
;
506 Type real_target_type
= target_type
;
508 if (expr_type
== TypeManager
.sbyte_type
){
510 // From sbyte to short, int, long, float, double, decimal
512 if (real_target_type
== TypeManager
.int32_type
)
513 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I4
);
514 if (real_target_type
== TypeManager
.int64_type
)
515 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
516 if (real_target_type
== TypeManager
.double_type
)
517 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
518 if (real_target_type
== TypeManager
.float_type
)
519 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
520 if (real_target_type
== TypeManager
.short_type
)
521 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I2
);
522 if (real_target_type
== TypeManager
.decimal_type
)
523 return new CastToDecimal (expr
);
524 } else if (expr_type
== TypeManager
.byte_type
){
526 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
528 if ((real_target_type
== TypeManager
.short_type
) ||
529 (real_target_type
== TypeManager
.ushort_type
) ||
530 (real_target_type
== TypeManager
.int32_type
) ||
531 (real_target_type
== TypeManager
.uint32_type
))
532 return new EmptyCast (expr
, target_type
);
534 if (real_target_type
== TypeManager
.uint64_type
)
535 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
536 if (real_target_type
== TypeManager
.int64_type
)
537 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
538 if (real_target_type
== TypeManager
.float_type
)
539 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
540 if (real_target_type
== TypeManager
.double_type
)
541 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
542 if (real_target_type
== TypeManager
.decimal_type
)
543 return new CastToDecimal (expr
);
545 } else if (expr_type
== TypeManager
.short_type
){
547 // From short to int, long, float, double, decimal
549 if (real_target_type
== TypeManager
.int32_type
)
550 return new EmptyCast (expr
, target_type
);
551 if (real_target_type
== TypeManager
.int64_type
)
552 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
553 if (real_target_type
== TypeManager
.double_type
)
554 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
555 if (real_target_type
== TypeManager
.float_type
)
556 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
557 if (real_target_type
== TypeManager
.decimal_type
)
558 return new CastToDecimal (expr
);
560 } else if (expr_type
== TypeManager
.ushort_type
){
562 // From ushort to int, uint, long, ulong, float, double, decimal
564 if (real_target_type
== TypeManager
.uint32_type
)
565 return new EmptyCast (expr
, target_type
);
567 if (real_target_type
== TypeManager
.uint64_type
)
568 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
569 if (real_target_type
== TypeManager
.int32_type
)
570 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I4
);
571 if (real_target_type
== TypeManager
.int64_type
)
572 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
573 if (real_target_type
== TypeManager
.double_type
)
574 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
575 if (real_target_type
== TypeManager
.float_type
)
576 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
577 if (real_target_type
== TypeManager
.decimal_type
)
578 return new CastToDecimal (expr
);
579 } else if (expr_type
== TypeManager
.int32_type
){
581 // From int to long, float, double, decimal
583 if (real_target_type
== TypeManager
.int64_type
)
584 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
585 if (real_target_type
== TypeManager
.double_type
)
586 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
587 if (real_target_type
== TypeManager
.float_type
)
588 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
589 if (real_target_type
== TypeManager
.decimal_type
)
590 return new CastToDecimal (expr
);
591 } else if (expr_type
== TypeManager
.uint32_type
){
593 // From uint to long, ulong, float, double, decimal
595 if (real_target_type
== TypeManager
.int64_type
)
596 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
597 if (real_target_type
== TypeManager
.uint64_type
)
598 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
599 if (real_target_type
== TypeManager
.double_type
)
600 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
,
602 if (real_target_type
== TypeManager
.float_type
)
603 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
,
605 if (real_target_type
== TypeManager
.decimal_type
)
606 return new CastToDecimal (expr
);
607 } else if (expr_type
== TypeManager
.int64_type
){
609 // From long/ulong to float, double
611 if (real_target_type
== TypeManager
.double_type
)
612 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
613 if (real_target_type
== TypeManager
.float_type
)
614 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
615 if (real_target_type
== TypeManager
.decimal_type
)
616 return new CastToDecimal (expr
);
617 } else if (expr_type
== TypeManager
.uint64_type
){
619 // From ulong to float, double
621 if (real_target_type
== TypeManager
.double_type
)
622 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
,
624 if (real_target_type
== TypeManager
.float_type
)
625 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R_Un
,
627 if (real_target_type
== TypeManager
.decimal_type
)
628 return new CastToDecimal (expr
);
629 } else if (expr_type
== TypeManager
.char_type
){
631 // From char to ushort, int, uint, long, ulong, float, double, decimal
633 if ((real_target_type
== TypeManager
.ushort_type
) ||
634 (real_target_type
== TypeManager
.int32_type
) ||
635 (real_target_type
== TypeManager
.uint32_type
))
636 return new EmptyCast (expr
, target_type
);
637 if (real_target_type
== TypeManager
.uint64_type
)
638 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
639 if (real_target_type
== TypeManager
.int64_type
)
640 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
641 if (real_target_type
== TypeManager
.float_type
)
642 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R4
);
643 if (real_target_type
== TypeManager
.double_type
)
644 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
645 if (real_target_type
== TypeManager
.decimal_type
)
646 return new CastToDecimal (expr
);
647 } else if (expr_type
== TypeManager
.float_type
){
651 if (real_target_type
== TypeManager
.double_type
)
652 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_R8
);
659 /// Same as ImplicitStandardConversionExists except that it also looks at
660 /// implicit user defined conversions - needed for overload resolution
662 public static bool ImplicitConversionExists (EmitContext ec
, Expression expr
, Type target_type
)
665 if (expr
is NullLiteral
) {
666 if (TypeManager
.IsGenericParameter (target_type
))
667 return TypeParameter_to_Null (target_type
);
669 if (TypeManager
.IsNullableType (target_type
))
673 if (ImplicitStandardConversionExists (expr
, target_type
))
676 return ImplicitUserConversion (ec
, expr
, target_type
, Location
.Null
) != null;
679 public static bool ImplicitUserConversionExists (EmitContext ec
, Type source
, Type target
)
681 return ImplicitUserConversion (ec
, new EmptyExpression (source
), target
, Location
.Null
) != null;
685 /// Determines if a standard implicit conversion exists from
686 /// expr_type to target_type
688 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
690 public static bool ImplicitStandardConversionExists (Expression expr
, Type target_type
)
692 Type expr_type
= expr
.Type
;
694 if (TypeManager
.IsNullableType (target_type
)) {
695 // if implicit standard conversion S -> T exists, S -> T? and S? -> T? also exists
696 target_type
= TypeManager
.GetTypeArguments (target_type
) [0];
699 if (TypeManager
.IsNullableType (expr_type
)) {
700 EmptyExpression new_expr
= EmptyExpression
.Grab ();
701 new_expr
.SetType (TypeManager
.GetTypeArguments (expr_type
) [0]);
702 bool retval
= ImplicitStandardConversionExists (new_expr
, target_type
);
703 EmptyExpression
.Release (new_expr
);
708 if (expr_type
== TypeManager
.void_type
)
711 //Console.WriteLine ("Expr is {0}", expr);
712 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
713 if (TypeManager
.IsEqual (expr_type
, target_type
))
717 // First numeric conversions
719 if (expr_type
== TypeManager
.sbyte_type
){
721 // From sbyte to short, int, long, float, double, decimal
723 if ((target_type
== TypeManager
.int32_type
) ||
724 (target_type
== TypeManager
.int64_type
) ||
725 (target_type
== TypeManager
.double_type
) ||
726 (target_type
== TypeManager
.float_type
) ||
727 (target_type
== TypeManager
.short_type
) ||
728 (target_type
== TypeManager
.decimal_type
))
731 } else if (expr_type
== TypeManager
.byte_type
){
733 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
735 if ((target_type
== TypeManager
.short_type
) ||
736 (target_type
== TypeManager
.ushort_type
) ||
737 (target_type
== TypeManager
.int32_type
) ||
738 (target_type
== TypeManager
.uint32_type
) ||
739 (target_type
== TypeManager
.uint64_type
) ||
740 (target_type
== TypeManager
.int64_type
) ||
741 (target_type
== TypeManager
.float_type
) ||
742 (target_type
== TypeManager
.double_type
) ||
743 (target_type
== TypeManager
.decimal_type
))
746 } else if (expr_type
== TypeManager
.short_type
){
748 // From short to int, long, double, float, decimal
750 if ((target_type
== TypeManager
.int32_type
) ||
751 (target_type
== TypeManager
.int64_type
) ||
752 (target_type
== TypeManager
.double_type
) ||
753 (target_type
== TypeManager
.float_type
) ||
754 (target_type
== TypeManager
.decimal_type
))
757 } else if (expr_type
== TypeManager
.ushort_type
){
759 // From ushort to int, uint, long, ulong, double, float, decimal
761 if ((target_type
== TypeManager
.uint32_type
) ||
762 (target_type
== TypeManager
.uint64_type
) ||
763 (target_type
== TypeManager
.int32_type
) ||
764 (target_type
== TypeManager
.int64_type
) ||
765 (target_type
== TypeManager
.double_type
) ||
766 (target_type
== TypeManager
.float_type
) ||
767 (target_type
== TypeManager
.decimal_type
))
770 } else if (expr_type
== TypeManager
.int32_type
){
772 // From int to long, double, float, decimal
774 if ((target_type
== TypeManager
.int64_type
) ||
775 (target_type
== TypeManager
.double_type
) ||
776 (target_type
== TypeManager
.float_type
) ||
777 (target_type
== TypeManager
.decimal_type
))
780 } else if (expr_type
== TypeManager
.uint32_type
){
782 // From uint to long, ulong, double, float, decimal
784 if ((target_type
== TypeManager
.int64_type
) ||
785 (target_type
== TypeManager
.uint64_type
) ||
786 (target_type
== TypeManager
.double_type
) ||
787 (target_type
== TypeManager
.float_type
) ||
788 (target_type
== TypeManager
.decimal_type
))
791 } else if ((expr_type
== TypeManager
.uint64_type
) ||
792 (expr_type
== TypeManager
.int64_type
)) {
794 // From long/ulong to double, float, decimal
796 if ((target_type
== TypeManager
.double_type
) ||
797 (target_type
== TypeManager
.float_type
) ||
798 (target_type
== TypeManager
.decimal_type
))
801 } else if (expr_type
== TypeManager
.char_type
){
803 // From char to ushort, int, uint, ulong, long, float, double, decimal
805 if ((target_type
== TypeManager
.ushort_type
) ||
806 (target_type
== TypeManager
.int32_type
) ||
807 (target_type
== TypeManager
.uint32_type
) ||
808 (target_type
== TypeManager
.uint64_type
) ||
809 (target_type
== TypeManager
.int64_type
) ||
810 (target_type
== TypeManager
.float_type
) ||
811 (target_type
== TypeManager
.double_type
) ||
812 (target_type
== TypeManager
.decimal_type
))
815 } else if (expr_type
== TypeManager
.float_type
){
819 if (target_type
== TypeManager
.double_type
)
823 if (expr
.eclass
== ExprClass
.MethodGroup
){
824 if (TypeManager
.IsDelegateType (target_type
) && RootContext
.Version
!= LanguageVersion
.ISO_1
){
825 MethodGroupExpr mg
= expr
as MethodGroupExpr
;
827 return DelegateCreation
.ImplicitStandardConversionExists (mg
, target_type
) != null;
832 if (ImplicitReferenceConversionExists (expr
, target_type
))
836 // Implicit Constant Expression Conversions
838 if (expr
is IntConstant
){
839 int value = ((IntConstant
) expr
).Value
;
841 if (target_type
== TypeManager
.sbyte_type
){
842 if (value >= SByte
.MinValue
&& value <= SByte
.MaxValue
)
844 } else if (target_type
== TypeManager
.byte_type
){
845 if (value >= 0 && value <= Byte
.MaxValue
)
847 } else if (target_type
== TypeManager
.short_type
){
848 if (value >= Int16
.MinValue
&& value <= Int16
.MaxValue
)
850 } else if (target_type
== TypeManager
.ushort_type
){
851 if (value >= UInt16
.MinValue
&& value <= UInt16
.MaxValue
)
853 } else if (target_type
== TypeManager
.uint32_type
){
856 } else if (target_type
== TypeManager
.uint64_type
){
858 // we can optimize this case: a positive int32
859 // always fits on a uint64. But we need an opcode
866 if (value == 0 && expr
is IntLiteral
&& TypeManager
.IsEnumType (target_type
))
870 if (expr
is LongConstant
&& target_type
== TypeManager
.uint64_type
){
872 // Try the implicit constant expression conversion
873 // from long to ulong, instead of a nice routine,
876 long v
= ((LongConstant
) expr
).Value
;
882 // If `expr_type' implements `target_type' (which is an iface)
883 // see TryImplicitIntConversion
885 if (target_type
.IsInterface
&& target_type
.IsAssignableFrom (expr_type
))
888 if (target_type
== TypeManager
.void_ptr_type
&& expr_type
.IsPointer
)
891 if (expr_type
== TypeManager
.anonymous_method_type
){
892 if (!TypeManager
.IsDelegateType (target_type
))
895 AnonymousMethodExpression ame
= (AnonymousMethodExpression
) expr
;
896 return ame
.ImplicitStandardConversionExists (target_type
);
903 /// Finds "most encompassed type" according to the spec (13.4.2)
904 /// amongst the methods in the MethodGroupExpr
906 static Type
FindMostEncompassedType (ArrayList types
)
910 if (types
.Count
== 0)
913 if (types
.Count
== 1)
914 return (Type
) types
[0];
916 EmptyExpression expr
= EmptyExpression
.Grab ();
918 foreach (Type t
in types
) {
925 if (ImplicitStandardConversionExists (expr
, best
))
930 foreach (Type t
in types
) {
933 if (!ImplicitStandardConversionExists (expr
, t
)) {
939 EmptyExpression
.Release (expr
);
945 /// Finds "most encompassing type" according to the spec (13.4.2)
946 /// amongst the types in the given set
948 static Type
FindMostEncompassingType (ArrayList types
)
952 if (types
.Count
== 0)
955 if (types
.Count
== 1)
956 return (Type
) types
[0];
958 EmptyExpression expr
= EmptyExpression
.Grab ();
960 foreach (Type t
in types
) {
967 if (ImplicitStandardConversionExists (expr
, t
))
971 foreach (Type t
in types
) {
975 if (!ImplicitStandardConversionExists (expr
, best
)) {
981 EmptyExpression
.Release (expr
);
987 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
988 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
989 /// for explicit and implicit conversion operators.
991 static public Type
FindMostSpecificSource (IList list
,
992 Expression source
, bool apply_explicit_conv_rules
)
994 ArrayList src_types_set
= new ArrayList ();
997 // If any operator converts from S then Sx = S
999 Type source_type
= source
.Type
;
1000 foreach (MethodBase mb
in list
){
1001 ParameterData pd
= TypeManager
.GetParameterData (mb
);
1002 Type param_type
= pd
.ParameterType (0);
1004 if (param_type
== source_type
)
1007 src_types_set
.Add (param_type
);
1011 // Explicit Conv rules
1013 if (apply_explicit_conv_rules
) {
1014 ArrayList candidate_set
= new ArrayList ();
1016 foreach (Type param_type
in src_types_set
){
1017 if (ImplicitStandardConversionExists (source
, param_type
))
1018 candidate_set
.Add (param_type
);
1021 if (candidate_set
.Count
!= 0)
1022 return FindMostEncompassedType (candidate_set
);
1028 if (apply_explicit_conv_rules
)
1029 return FindMostEncompassingType (src_types_set
);
1031 return FindMostEncompassedType (src_types_set
);
1035 /// Finds the most specific target Tx according to section 13.4.4
1037 static public Type
FindMostSpecificTarget (IList list
,
1038 Type target
, bool apply_explicit_conv_rules
)
1040 ArrayList tgt_types_set
= new ArrayList ();
1043 // If any operator converts to T then Tx = T
1045 foreach (MethodInfo mi
in list
){
1046 Type ret_type
= mi
.ReturnType
;
1047 if (ret_type
== target
)
1050 tgt_types_set
.Add (ret_type
);
1054 // Explicit conv rules
1056 if (apply_explicit_conv_rules
) {
1057 ArrayList candidate_set
= new ArrayList ();
1059 EmptyExpression expr
= EmptyExpression
.Grab ();
1061 foreach (Type ret_type
in tgt_types_set
){
1062 expr
.SetType (ret_type
);
1064 if (ImplicitStandardConversionExists (expr
, target
))
1065 candidate_set
.Add (ret_type
);
1068 EmptyExpression
.Release (expr
);
1070 if (candidate_set
.Count
!= 0)
1071 return FindMostEncompassingType (candidate_set
);
1075 // Okay, final case !
1077 if (apply_explicit_conv_rules
)
1078 return FindMostEncompassedType (tgt_types_set
);
1080 return FindMostEncompassingType (tgt_types_set
);
1084 /// User-defined Implicit conversions
1086 static public Expression
ImplicitUserConversion (EmitContext ec
, Expression source
,
1087 Type target
, Location loc
)
1089 return UserDefinedConversion (ec
, source
, target
, loc
, false);
1093 /// User-defined Explicit conversions
1095 static public Expression
ExplicitUserConversion (EmitContext ec
, Expression source
,
1096 Type target
, Location loc
)
1098 return UserDefinedConversion (ec
, source
, target
, loc
, true);
1101 static void AddConversionOperators (ArrayList list
,
1102 Expression source
, Type target_type
,
1103 bool look_for_explicit
,
1109 Type source_type
= source
.Type
;
1110 EmptyExpression expr
= EmptyExpression
.Grab ();
1111 foreach (MethodInfo m
in mg
.Methods
) {
1112 ParameterData pd
= TypeManager
.GetParameterData (m
);
1113 Type return_type
= m
.ReturnType
;
1114 Type arg_type
= pd
.ParameterType (0);
1116 if (source_type
!= arg_type
) {
1117 if (!ImplicitStandardConversionExists (source
, arg_type
)) {
1118 if (!look_for_explicit
)
1120 expr
.SetType (arg_type
);
1121 if (!ImplicitStandardConversionExists (expr
, source_type
))
1126 if (target_type
!= return_type
) {
1127 expr
.SetType (return_type
);
1128 if (!ImplicitStandardConversionExists (expr
, target_type
)) {
1129 if (!look_for_explicit
)
1131 expr
.SetType (target_type
);
1132 if (!ImplicitStandardConversionExists (expr
, return_type
))
1140 EmptyExpression
.Release (expr
);
1144 /// Compute the user-defined conversion operator from source_type to target_type.
1145 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1147 static MethodInfo
GetConversionOperator (Type container_type
, Expression source
, Type target_type
, bool look_for_explicit
)
1149 ArrayList ops
= new ArrayList (4);
1151 Type source_type
= source
.Type
;
1153 if (source_type
!= TypeManager
.decimal_type
) {
1154 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1155 Expression
.MethodLookup (container_type
, source_type
, "op_Implicit", Location
.Null
) as MethodGroupExpr
);
1156 if (look_for_explicit
) {
1157 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1158 Expression
.MethodLookup (
1159 container_type
, source_type
, "op_Explicit", Location
.Null
) as MethodGroupExpr
);
1163 if (target_type
!= TypeManager
.decimal_type
) {
1164 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1165 Expression
.MethodLookup (container_type
, target_type
, "op_Implicit", Location
.Null
) as MethodGroupExpr
);
1166 if (look_for_explicit
) {
1167 AddConversionOperators (ops
, source
, target_type
, look_for_explicit
,
1168 Expression
.MethodLookup (
1169 container_type
, target_type
, "op_Explicit", Location
.Null
) as MethodGroupExpr
);
1176 Type most_specific_source
= FindMostSpecificSource (ops
, source
, look_for_explicit
);
1177 if (most_specific_source
== null)
1180 Type most_specific_target
= FindMostSpecificTarget (ops
, target_type
, look_for_explicit
);
1181 if (most_specific_target
== null)
1184 MethodInfo method
= null;
1186 foreach (MethodInfo m
in ops
) {
1187 if (m
.ReturnType
!= most_specific_target
)
1189 if (TypeManager
.GetParameterData (m
).ParameterType (0) != most_specific_source
)
1191 // Ambiguous: more than one conversion operator satisfies the signature.
1200 static DoubleHash explicit_conv
= new DoubleHash (100);
1201 static DoubleHash implicit_conv
= new DoubleHash (100);
1204 /// User-defined conversions
1206 static public Expression
UserDefinedConversion (EmitContext ec
, Expression source
,
1207 Type target
, Location loc
,
1208 bool look_for_explicit
)
1210 Type source_type
= source
.Type
;
1211 MethodInfo method
= null;
1214 DoubleHash hash
= look_for_explicit
? explicit_conv
: implicit_conv
;
1216 if (!(source
is Constant
) && hash
.Lookup (source_type
, target
, out o
)) {
1217 method
= (MethodInfo
) o
;
1219 method
= GetConversionOperator (null, source
, target
, look_for_explicit
);
1220 if (!(source
is Constant
))
1221 hash
.Insert (source_type
, target
, method
);
1227 Type most_specific_source
= TypeManager
.GetParameterData (method
).ParameterType (0);
1230 // This will do the conversion to the best match that we
1231 // found. Now we need to perform an implict standard conversion
1232 // if the best match was not the type that we were requested
1235 if (look_for_explicit
)
1236 source
= ExplicitConversionStandard (ec
, source
, most_specific_source
, loc
);
1238 source
= ImplicitConversionStandard (ec
, source
, most_specific_source
, loc
);
1244 e
= new UserCast (method
, source
, loc
);
1245 if (e
.Type
!= target
){
1246 if (!look_for_explicit
)
1247 e
= ImplicitConversionStandard (ec
, e
, target
, loc
);
1249 e
= ExplicitConversionStandard (ec
, e
, target
, loc
);
1256 /// Converts implicitly the resolved expression `expr' into the
1257 /// `target_type'. It returns a new expression that can be used
1258 /// in a context that expects a `target_type'.
1260 static public Expression
ImplicitConversion (EmitContext ec
, Expression expr
,
1261 Type target_type
, Location loc
)
1265 if (target_type
== null)
1266 throw new Exception ("Target type is null");
1268 e
= ImplicitConversionStandard (ec
, expr
, target_type
, loc
);
1272 e
= ImplicitUserConversion (ec
, expr
, target_type
, loc
);
1281 /// Attempts to apply the `Standard Implicit
1282 /// Conversion' rules to the expression `expr' into
1283 /// the `target_type'. It returns a new expression
1284 /// that can be used in a context that expects a
1287 /// This is different from `ImplicitConversion' in that the
1288 /// user defined implicit conversions are excluded.
1290 static public Expression
ImplicitConversionStandard (EmitContext ec
, Expression expr
,
1291 Type target_type
, Location loc
)
1293 Type expr_type
= expr
.Type
;
1296 // TODO: refactor to be a part of constant infrastructure
1297 if (expr
is NullLiteral
) {
1298 if (TypeManager
.IsNullableType (target_type
))
1299 return new Nullable
.NullableLiteral (target_type
, loc
);
1302 if (TypeManager
.IsNullableType (target_type
)) {
1303 Type target
= TypeManager
.GetTypeArguments (target_type
) [0];
1305 if (TypeManager
.IsNullableType (expr
.Type
)) {
1306 e
= new Nullable
.LiftedConversion (
1307 expr
, target_type
, false, false, loc
).Resolve (ec
);
1311 e
= ImplicitConversion (ec
, expr
, target
, loc
);
1313 return Nullable
.Wrap
.Create (e
, ec
);
1317 if (expr
.eclass
== ExprClass
.MethodGroup
){
1318 if (!TypeManager
.IsDelegateType (target_type
)){
1323 // Only allow anonymous method conversions on post ISO_1
1325 if (RootContext
.Version
!= LanguageVersion
.ISO_1
){
1326 MethodGroupExpr mg
= expr
as MethodGroupExpr
;
1328 return ImplicitDelegateCreation
.Create (
1329 ec
, mg
, target_type
, loc
);
1333 if (expr_type
.Equals (target_type
) && !TypeManager
.IsNullType (expr_type
))
1337 // Attempt to do the implicit constant expression conversions
1339 Constant c
= expr
as Constant
;
1342 // If `target_type' is an interface and the type of `ic' implements the interface
1343 // e.g. target_type is IComparable, IConvertible, IFormattable
1345 if (c
.Type
== TypeManager
.int32_type
&& target_type
.IsInterface
&& target_type
.IsAssignableFrom (c
.Type
))
1346 return new BoxedCast (c
, target_type
);
1349 c
= c
.ConvertImplicitly (target_type
);
1351 Console
.WriteLine ("Conversion error happened in line {0}", loc
);
1358 e
= ImplicitNumericConversion (expr
, target_type
);
1362 e
= ImplicitReferenceConversion (expr
, target_type
);
1366 if (TypeManager
.IsEnumType (target_type
) && expr
is IntLiteral
){
1367 IntLiteral i
= (IntLiteral
) expr
;
1370 return new EnumConstant ((Constant
) expr
, target_type
);
1374 if (expr_type
.IsPointer
){
1375 if (target_type
== TypeManager
.void_ptr_type
)
1376 return new EmptyCast (expr
, target_type
);
1379 // yep, comparing pointer types cant be done with
1380 // t1 == t2, we have to compare their element types.
1382 if (target_type
.IsPointer
){
1383 if (TypeManager
.GetElementType(target_type
) == TypeManager
.GetElementType(expr_type
))
1390 if (expr_type
== TypeManager
.null_type
&& target_type
.IsPointer
)
1391 return new EmptyCast (NullPointer
.Null
, target_type
);
1394 if (expr_type
== TypeManager
.anonymous_method_type
){
1395 AnonymousMethodExpression ame
= (AnonymousMethodExpression
) expr
;
1397 int errors
= Report
.Errors
;
1399 Expression conv
= ame
.Compatible (ec
, target_type
);
1404 // We return something instead of null, to avoid
1405 // the duplicate error, since am.Compatible would have
1406 // reported that already
1408 if (errors
!= Report
.Errors
)
1409 return new EmptyCast (expr
, target_type
);
1416 /// Attempts to implicitly convert `source' into `target_type', using
1417 /// ImplicitConversion. If there is no implicit conversion, then
1418 /// an error is signaled
1420 static public Expression
ImplicitConversionRequired (EmitContext ec
, Expression source
,
1421 Type target_type
, Location loc
)
1423 Expression e
= ImplicitConversion (ec
, source
, target_type
, loc
);
1427 source
.Error_ValueCannotBeConverted (ec
, loc
, target_type
, false);
1432 /// Performs the explicit numeric conversions
1434 /// There are a few conversions that are not part of the C# standard,
1435 /// they were interim hacks in the C# compiler that were supposed to
1436 /// become explicit operators in the UIntPtr class and IntPtr class,
1437 /// but for historical reasons it did not happen, so the C# compiler
1438 /// ended up with these special hacks.
1440 /// See bug 59800 for details.
1442 /// The conversion are:
1452 public static Expression
ExplicitNumericConversion (Expression expr
, Type target_type
)
1454 Type expr_type
= expr
.Type
;
1455 Type real_target_type
= target_type
;
1457 if (expr_type
== TypeManager
.sbyte_type
){
1459 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1461 if (real_target_type
== TypeManager
.byte_type
)
1462 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U1
);
1463 if (real_target_type
== TypeManager
.ushort_type
)
1464 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U2
);
1465 if (real_target_type
== TypeManager
.uint32_type
)
1466 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U4
);
1467 if (real_target_type
== TypeManager
.uint64_type
)
1468 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_U8
);
1469 if (real_target_type
== TypeManager
.char_type
)
1470 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I1_CH
);
1472 // One of the built-in conversions that belonged in the class library
1473 if (real_target_type
== TypeManager
.uintptr_type
){
1474 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I1_U8
);
1476 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1478 } else if (expr_type
== TypeManager
.byte_type
){
1480 // From byte to sbyte and char
1482 if (real_target_type
== TypeManager
.sbyte_type
)
1483 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U1_I1
);
1484 if (real_target_type
== TypeManager
.char_type
)
1485 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U1_CH
);
1486 } else if (expr_type
== TypeManager
.short_type
){
1488 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1490 if (real_target_type
== TypeManager
.sbyte_type
)
1491 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_I1
);
1492 if (real_target_type
== TypeManager
.byte_type
)
1493 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U1
);
1494 if (real_target_type
== TypeManager
.ushort_type
)
1495 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U2
);
1496 if (real_target_type
== TypeManager
.uint32_type
)
1497 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U4
);
1498 if (real_target_type
== TypeManager
.uint64_type
)
1499 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_U8
);
1500 if (real_target_type
== TypeManager
.char_type
)
1501 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I2_CH
);
1503 // One of the built-in conversions that belonged in the class library
1504 if (real_target_type
== TypeManager
.uintptr_type
){
1505 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I2_U8
);
1507 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1509 } else if (expr_type
== TypeManager
.ushort_type
){
1511 // From ushort to sbyte, byte, short, char
1513 if (real_target_type
== TypeManager
.sbyte_type
)
1514 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_I1
);
1515 if (real_target_type
== TypeManager
.byte_type
)
1516 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_U1
);
1517 if (real_target_type
== TypeManager
.short_type
)
1518 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_I2
);
1519 if (real_target_type
== TypeManager
.char_type
)
1520 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U2_CH
);
1521 } else if (expr_type
== TypeManager
.int32_type
){
1523 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1525 if (real_target_type
== TypeManager
.sbyte_type
)
1526 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_I1
);
1527 if (real_target_type
== TypeManager
.byte_type
)
1528 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U1
);
1529 if (real_target_type
== TypeManager
.short_type
)
1530 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_I2
);
1531 if (real_target_type
== TypeManager
.ushort_type
)
1532 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U2
);
1533 if (real_target_type
== TypeManager
.uint32_type
)
1534 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U4
);
1535 if (real_target_type
== TypeManager
.uint64_type
)
1536 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_U8
);
1537 if (real_target_type
== TypeManager
.char_type
)
1538 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I4_CH
);
1540 // One of the built-in conversions that belonged in the class library
1541 if (real_target_type
== TypeManager
.uintptr_type
){
1542 Expression u8e
= new ConvCast (expr
, TypeManager
.uint64_type
, ConvCast
.Mode
.I2_U8
);
1544 return new OperatorCast (u8e
, TypeManager
.uintptr_type
, true);
1546 } else if (expr_type
== TypeManager
.uint32_type
){
1548 // From uint to sbyte, byte, short, ushort, int, char
1550 if (real_target_type
== TypeManager
.sbyte_type
)
1551 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I1
);
1552 if (real_target_type
== TypeManager
.byte_type
)
1553 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_U1
);
1554 if (real_target_type
== TypeManager
.short_type
)
1555 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I2
);
1556 if (real_target_type
== TypeManager
.ushort_type
)
1557 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_U2
);
1558 if (real_target_type
== TypeManager
.int32_type
)
1559 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_I4
);
1560 if (real_target_type
== TypeManager
.char_type
)
1561 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U4_CH
);
1562 } else if (expr_type
== TypeManager
.int64_type
){
1564 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1566 if (real_target_type
== TypeManager
.sbyte_type
)
1567 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I1
);
1568 if (real_target_type
== TypeManager
.byte_type
)
1569 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U1
);
1570 if (real_target_type
== TypeManager
.short_type
)
1571 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I2
);
1572 if (real_target_type
== TypeManager
.ushort_type
)
1573 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U2
);
1574 if (real_target_type
== TypeManager
.int32_type
)
1575 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_I4
);
1576 if (real_target_type
== TypeManager
.uint32_type
)
1577 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U4
);
1578 if (real_target_type
== TypeManager
.uint64_type
)
1579 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_U8
);
1580 if (real_target_type
== TypeManager
.char_type
)
1581 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.I8_CH
);
1582 } else if (expr_type
== TypeManager
.uint64_type
){
1584 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1586 if (real_target_type
== TypeManager
.sbyte_type
)
1587 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I1
);
1588 if (real_target_type
== TypeManager
.byte_type
)
1589 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U1
);
1590 if (real_target_type
== TypeManager
.short_type
)
1591 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I2
);
1592 if (real_target_type
== TypeManager
.ushort_type
)
1593 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U2
);
1594 if (real_target_type
== TypeManager
.int32_type
)
1595 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I4
);
1596 if (real_target_type
== TypeManager
.uint32_type
)
1597 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_U4
);
1598 if (real_target_type
== TypeManager
.int64_type
)
1599 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_I8
);
1600 if (real_target_type
== TypeManager
.char_type
)
1601 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.U8_CH
);
1603 // One of the built-in conversions that belonged in the class library
1604 if (real_target_type
== TypeManager
.intptr_type
){
1605 return new OperatorCast (new EmptyCast (expr
, TypeManager
.int64_type
),
1606 TypeManager
.intptr_type
, true);
1608 } else if (expr_type
== TypeManager
.char_type
){
1610 // From char to sbyte, byte, short
1612 if (real_target_type
== TypeManager
.sbyte_type
)
1613 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_I1
);
1614 if (real_target_type
== TypeManager
.byte_type
)
1615 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_U1
);
1616 if (real_target_type
== TypeManager
.short_type
)
1617 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.CH_I2
);
1618 } else if (expr_type
== TypeManager
.float_type
){
1620 // From float to sbyte, byte, short,
1621 // ushort, int, uint, long, ulong, char
1624 if (real_target_type
== TypeManager
.sbyte_type
)
1625 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I1
);
1626 if (real_target_type
== TypeManager
.byte_type
)
1627 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U1
);
1628 if (real_target_type
== TypeManager
.short_type
)
1629 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I2
);
1630 if (real_target_type
== TypeManager
.ushort_type
)
1631 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U2
);
1632 if (real_target_type
== TypeManager
.int32_type
)
1633 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I4
);
1634 if (real_target_type
== TypeManager
.uint32_type
)
1635 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U4
);
1636 if (real_target_type
== TypeManager
.int64_type
)
1637 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_I8
);
1638 if (real_target_type
== TypeManager
.uint64_type
)
1639 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_U8
);
1640 if (real_target_type
== TypeManager
.char_type
)
1641 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R4_CH
);
1642 if (real_target_type
== TypeManager
.decimal_type
)
1643 return new CastToDecimal (expr
, true);
1644 } else if (expr_type
== TypeManager
.double_type
){
1646 // From double to sbyte, byte, short,
1647 // ushort, int, uint, long, ulong,
1648 // char, float or decimal
1650 if (real_target_type
== TypeManager
.sbyte_type
)
1651 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I1
);
1652 if (real_target_type
== TypeManager
.byte_type
)
1653 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U1
);
1654 if (real_target_type
== TypeManager
.short_type
)
1655 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I2
);
1656 if (real_target_type
== TypeManager
.ushort_type
)
1657 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U2
);
1658 if (real_target_type
== TypeManager
.int32_type
)
1659 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I4
);
1660 if (real_target_type
== TypeManager
.uint32_type
)
1661 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U4
);
1662 if (real_target_type
== TypeManager
.int64_type
)
1663 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_I8
);
1664 if (real_target_type
== TypeManager
.uint64_type
)
1665 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_U8
);
1666 if (real_target_type
== TypeManager
.char_type
)
1667 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_CH
);
1668 if (real_target_type
== TypeManager
.float_type
)
1669 return new ConvCast (expr
, target_type
, ConvCast
.Mode
.R8_R4
);
1670 if (real_target_type
== TypeManager
.decimal_type
)
1671 return new CastToDecimal (expr
, true);
1672 } else if (expr_type
== TypeManager
.uintptr_type
){
1674 // Various built-in conversions that belonged in the class library
1676 // from uintptr to sbyte, short, int32
1678 if (real_target_type
== TypeManager
.sbyte_type
){
1679 Expression uint32e
= new OperatorCast (expr
, TypeManager
.uint32_type
, true);
1680 return new ConvCast (uint32e
, TypeManager
.sbyte_type
, ConvCast
.Mode
.U4_I1
);
1682 if (real_target_type
== TypeManager
.short_type
){
1683 Expression uint32e
= new OperatorCast (expr
, TypeManager
.uint32_type
, true);
1684 return new ConvCast (uint32e
, TypeManager
.sbyte_type
, ConvCast
.Mode
.U4_I2
);
1686 if (real_target_type
== TypeManager
.int32_type
){
1687 return new EmptyCast (new OperatorCast (expr
, TypeManager
.uint32_type
, true),
1688 TypeManager
.int32_type
);
1690 } else if (expr_type
== TypeManager
.intptr_type
){
1691 if (real_target_type
== TypeManager
.uint64_type
){
1692 return new EmptyCast (new OperatorCast (expr
, TypeManager
.int64_type
, true),
1693 TypeManager
.uint64_type
);
1695 } else if (expr_type
== TypeManager
.decimal_type
) {
1696 return new CastFromDecimal (expr
, target_type
).Resolve ();
1702 /// Returns whether an explicit reference conversion can be performed
1703 /// from source_type to target_type
1705 public static bool ExplicitReferenceConversionExists (Type source_type
, Type target_type
)
1707 bool target_is_type_param
= TypeManager
.IsGenericParameter (target_type
);
1708 bool target_is_value_type
= target_type
.IsValueType
;
1710 if (source_type
== target_type
)
1714 // From generic parameter to any type
1716 if (TypeManager
.IsGenericParameter (source_type
))
1717 return ExplicitTypeParameterConversionExists (source_type
, target_type
);
1720 // From object to a generic parameter
1722 if (source_type
== TypeManager
.object_type
&& target_is_type_param
)
1726 // From object to any reference type
1728 if (source_type
== TypeManager
.object_type
&& !target_is_value_type
)
1732 // From any class S to any class-type T, provided S is a base class of T
1734 if (TypeManager
.IsSubclassOf (target_type
, source_type
))
1738 // From any interface type S to any interface T provided S is not derived from T
1740 if (source_type
.IsInterface
&& target_type
.IsInterface
){
1741 if (!TypeManager
.IsSubclassOf (target_type
, source_type
))
1746 // From any class type S to any interface T, provided S is not sealed
1747 // and provided S does not implement T.
1749 if (target_type
.IsInterface
&& !source_type
.IsSealed
&&
1750 !TypeManager
.ImplementsInterface (source_type
, target_type
))
1754 // From any interface-type S to to any class type T, provided T is not
1755 // sealed, or provided T implements S.
1757 if (source_type
.IsInterface
&&
1758 (!target_type
.IsSealed
|| TypeManager
.ImplementsInterface (target_type
, source_type
)))
1762 if (source_type
.IsInterface
&& IList_To_Array (source_type
, target_type
))
1765 // From an array type S with an element type Se to an array type T with an
1766 // element type Te provided all the following are true:
1767 // * S and T differe only in element type, in other words, S and T
1768 // have the same number of dimensions.
1769 // * Both Se and Te are reference types
1770 // * An explicit referenc conversions exist from Se to Te
1772 if (source_type
.IsArray
&& target_type
.IsArray
) {
1773 if (source_type
.GetArrayRank () == target_type
.GetArrayRank ()) {
1775 Type source_element_type
= TypeManager
.GetElementType (source_type
);
1776 Type target_element_type
= TypeManager
.GetElementType (target_type
);
1778 if (TypeManager
.IsGenericParameter (source_element_type
) ||
1779 (!source_element_type
.IsValueType
&& !target_element_type
.IsValueType
))
1780 if (ExplicitReferenceConversionExists (source_element_type
,
1781 target_element_type
))
1787 // From System.Array to any array-type
1788 if (source_type
== TypeManager
.array_type
&&
1789 target_type
.IsArray
){
1794 // From System delegate to any delegate-type
1796 if (source_type
== TypeManager
.delegate_type
&&
1797 TypeManager
.IsDelegateType (target_type
))
1801 // From ICloneable to Array or Delegate types
1803 if (source_type
== TypeManager
.icloneable_type
&&
1804 (target_type
== TypeManager
.array_type
||
1805 target_type
== TypeManager
.delegate_type
))
1812 /// Implements Explicit Reference conversions
1814 static Expression
ExplicitReferenceConversion (Expression source
, Type target_type
)
1816 Type source_type
= source
.Type
;
1817 bool target_is_type_param
= TypeManager
.IsGenericParameter (target_type
);
1818 bool target_is_value_type
= target_type
.IsValueType
;
1821 // From object to a generic parameter
1823 if (source_type
== TypeManager
.object_type
&& target_is_type_param
)
1824 return new UnboxCast (source
, target_type
);
1827 // Explicit type parameter conversion.
1830 if (TypeManager
.IsGenericParameter (source_type
))
1831 return ExplicitTypeParameterConversion (source
, target_type
);
1834 // From object to any reference type
1836 if (source_type
== TypeManager
.object_type
&& !target_is_value_type
)
1837 return new ClassCast (source
, target_type
);
1840 // Unboxing conversion.
1842 if (((source_type
== TypeManager
.enum_type
&&
1843 !(source
is EmptyCast
)) ||
1844 source_type
== TypeManager
.value_type
) && target_is_value_type
)
1845 return new UnboxCast (source
, target_type
);
1848 // From any class S to any class-type T, provided S is a base class of T
1850 if (TypeManager
.IsSubclassOf (target_type
, source_type
))
1851 return new ClassCast (source
, target_type
);
1854 // From any interface type S to any interface T provided S is not derived from T
1856 if (source_type
.IsInterface
&& target_type
.IsInterface
){
1857 if (TypeManager
.ImplementsInterface (source_type
, target_type
))
1860 return new ClassCast (source
, target_type
);
1864 // From any class type S to any interface T, provides S is not sealed
1865 // and provided S does not implement T.
1867 if (target_type
.IsInterface
&& !source_type
.IsSealed
) {
1868 if (TypeManager
.ImplementsInterface (source_type
, target_type
))
1871 return new ClassCast (source
, target_type
);
1876 // From any interface-type S to to any class type T, provided T is not
1877 // sealed, or provided T implements S.
1879 if (source_type
.IsInterface
) {
1880 if (!target_type
.IsSealed
|| TypeManager
.ImplementsInterface (target_type
, source_type
)) {
1881 if (target_type
.IsClass
)
1882 return new ClassCast (source
, target_type
);
1884 return new UnboxCast (source
, target_type
);
1888 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1889 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1891 if (IList_To_Array (source_type
, target_type
))
1892 return new ClassCast (source
, target_type
);
1897 // From an array type S with an element type Se to an array type T with an
1898 // element type Te provided all the following are true:
1899 // * S and T differe only in element type, in other words, S and T
1900 // have the same number of dimensions.
1901 // * Both Se and Te are reference types
1902 // * An explicit referenc conversions exist from Se to Te
1904 if (source_type
.IsArray
&& target_type
.IsArray
) {
1905 if (source_type
.GetArrayRank () == target_type
.GetArrayRank ()) {
1907 Type source_element_type
= TypeManager
.GetElementType (source_type
);
1908 Type target_element_type
= TypeManager
.GetElementType (target_type
);
1910 if (!source_element_type
.IsValueType
&& !target_element_type
.IsValueType
)
1911 if (ExplicitReferenceConversionExists (source_element_type
,
1912 target_element_type
))
1913 return new ClassCast (source
, target_type
);
1918 // From System.Array to any array-type
1919 if (source_type
== TypeManager
.array_type
&&
1920 target_type
.IsArray
) {
1921 return new ClassCast (source
, target_type
);
1925 // From System delegate to any delegate-type
1927 if (source_type
== TypeManager
.delegate_type
&&
1928 TypeManager
.IsDelegateType (target_type
))
1929 return new ClassCast (source
, target_type
);
1932 // From ICloneable to Array or Delegate types
1934 if (source_type
== TypeManager
.icloneable_type
&&
1935 (target_type
== TypeManager
.array_type
||
1936 target_type
== TypeManager
.delegate_type
))
1937 return new ClassCast (source
, target_type
);
1943 /// Performs an explicit conversion of the expression `expr' whose
1944 /// type is expr.Type to `target_type'.
1946 static public Expression
ExplicitConversionCore (EmitContext ec
, Expression expr
,
1947 Type target_type
, Location loc
)
1949 Type expr_type
= expr
.Type
;
1951 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1952 Expression ne
= ImplicitConversionStandard (ec
, expr
, target_type
, loc
);
1957 // Unboxing conversions; only object types can be convertible to enum
1959 if (expr_type
== TypeManager
.object_type
&& target_type
.IsValueType
)
1960 return new UnboxCast (expr
, target_type
);
1962 if (TypeManager
.IsEnumType (expr_type
)) {
1963 if (expr
is EnumConstant
)
1964 return ExplicitConversionCore (ec
, ((EnumConstant
) expr
).Child
, target_type
, loc
);
1966 return ExplicitConversionCore (ec
, new EmptyCast (expr
, TypeManager
.EnumToUnderlying (expr_type
)), target_type
, loc
);
1969 if (TypeManager
.IsEnumType (target_type
)){
1970 if (expr_type
== TypeManager
.enum_type
)
1971 return new UnboxCast (expr
, target_type
);
1973 Expression ce
= ExplicitConversionCore (ec
, expr
, TypeManager
.EnumToUnderlying (target_type
), loc
);
1975 return new EmptyCast (ce
, target_type
);
1978 ne
= ExplicitNumericConversion (expr
, target_type
);
1983 // Skip the ExplicitReferenceConversion because we can not convert
1984 // from Null to a ValueType, and ExplicitReference wont check against
1985 // null literal explicitly
1987 if (expr_type
!= TypeManager
.null_type
){
1988 ne
= ExplicitReferenceConversion (expr
, target_type
);
1994 ne
= ExplicitUnsafe (expr
, target_type
);
1999 ne
= ExplicitUserConversion (ec
, expr
, target_type
, loc
);
2006 public static Expression
ExplicitUnsafe (Expression expr
, Type target_type
)
2008 Type expr_type
= expr
.Type
;
2010 if (target_type
.IsPointer
){
2011 if (expr_type
.IsPointer
)
2012 return new EmptyCast (expr
, target_type
);
2014 if (expr_type
== TypeManager
.sbyte_type
||
2015 expr_type
== TypeManager
.short_type
||
2016 expr_type
== TypeManager
.int32_type
||
2017 expr_type
== TypeManager
.int64_type
)
2018 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I
);
2020 if (expr_type
== TypeManager
.ushort_type
||
2021 expr_type
== TypeManager
.uint32_type
||
2022 expr_type
== TypeManager
.uint64_type
||
2023 expr_type
== TypeManager
.byte_type
)
2024 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U
);
2027 if (expr_type
.IsPointer
){
2028 if (target_type
== TypeManager
.sbyte_type
)
2029 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I1
);
2030 else if (target_type
== TypeManager
.byte_type
)
2031 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U1
);
2032 else if (target_type
== TypeManager
.short_type
)
2033 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I2
);
2034 else if (target_type
== TypeManager
.ushort_type
)
2035 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U2
);
2036 else if (target_type
== TypeManager
.int32_type
)
2037 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I4
);
2038 else if (target_type
== TypeManager
.uint32_type
)
2039 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U4
);
2040 else if (target_type
== TypeManager
.uint64_type
)
2041 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_U8
);
2042 else if (target_type
== TypeManager
.int64_type
){
2043 return new OpcodeCast (expr
, target_type
, OpCodes
.Conv_I8
);
2050 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2052 static public Expression
ExplicitConversionStandard (EmitContext ec
, Expression expr
,
2053 Type target_type
, Location l
)
2055 int errors
= Report
.Errors
;
2056 Expression ne
= ImplicitConversionStandard (ec
, expr
, target_type
, l
);
2057 if (Report
.Errors
> errors
)
2063 ne
= ExplicitNumericConversion (expr
, target_type
);
2067 ne
= ExplicitReferenceConversion (expr
, target_type
);
2071 if (ec
.InUnsafe
&& expr
.Type
== TypeManager
.void_ptr_type
&& target_type
.IsPointer
)
2072 return new EmptyCast (expr
, target_type
);
2074 expr
.Error_ValueCannotBeConverted (ec
, l
, target_type
, true);
2079 /// Performs an explicit conversion of the expression `expr' whose
2080 /// type is expr.Type to `target_type'.
2082 static public Expression
ExplicitConversion (EmitContext ec
, Expression expr
,
2083 Type target_type
, Location loc
)
2087 Type expr_type
= expr
.Type
;
2088 if (TypeManager
.IsNullableType (target_type
)) {
2089 if (TypeManager
.IsNullableType (expr_type
)) {
2090 e
= new Nullable
.LiftedConversion (
2091 expr
, target_type
, false, true, loc
).Resolve (ec
);
2094 } else if (expr_type
== TypeManager
.object_type
) {
2095 return new UnboxCast (expr
, target_type
);
2097 Type target
= TypeManager
.GetTypeArguments (target_type
) [0];
2099 e
= ExplicitConversionCore (ec
, expr
, target
, loc
);
2101 return Nullable
.Wrap
.Create (e
, ec
);
2103 } else if (TypeManager
.IsNullableType (expr_type
)) {
2104 Expression source
= Nullable
.Unwrap
.Create (expr
, ec
);
2105 if (source
!= null) {
2106 e
= ExplicitConversionCore (ec
, source
, target_type
, loc
);
2112 e
= ExplicitConversionCore (ec
, expr
, target_type
, loc
);
2116 expr
.Error_ValueCannotBeConverted (ec
, loc
, target_type
, true);