2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright 2001, 2002, 2003 Ximian, Inc.
10 // Copyright 2003-2008 Novell, Inc.
14 using System.Collections.Generic;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Reflection.Emit;
19 namespace Mono.CSharp {
22 // A container class for all the conversion operations
24 static class Convert {
26 static EmptyExpression MyEmptyExpr;
33 public static void Reset ()
39 // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
40 // interfaces of this interface, provided there is an implicit reference conversion
43 static bool ArrayToIList (ArrayContainer array, TypeSpec list, bool isExplicit)
45 if (array.Rank != 1 || !list.IsGeneric)
48 var open_version = list.GetDefinition ();
49 if ((open_version != TypeManager.generic_ilist_type) &&
50 (open_version != TypeManager.generic_icollection_type) &&
51 (open_version != TypeManager.generic_ienumerable_type))
54 var arg_type = list.TypeArguments[0];
55 if (array.Element == arg_type)
59 return ExplicitReferenceConversionExists (array.Element, arg_type);
61 if (MyEmptyExpr == null)
62 MyEmptyExpr = new EmptyExpression (array.Element);
64 MyEmptyExpr.SetType (array.Element);
66 return ImplicitReferenceConversionExists (MyEmptyExpr, arg_type);
69 static bool IList_To_Array(TypeSpec list, ArrayContainer array)
71 if (array.Rank != 1 || !list.IsGeneric)
74 var open_version = list.GetDefinition ();
75 if ((open_version != TypeManager.generic_ilist_type) &&
76 (open_version != TypeManager.generic_icollection_type) &&
77 (open_version != TypeManager.generic_ienumerable_type))
80 var arg_type = list.TypeArguments[0];
81 if (array.Element == arg_type)
84 if (MyEmptyExpr == null)
85 MyEmptyExpr = new EmptyExpression (array.Element);
87 MyEmptyExpr.SetType (array.Element);
89 return ImplicitReferenceConversionExists (MyEmptyExpr, arg_type) || ExplicitReferenceConversionExists (array.Element, arg_type);
92 static Expression ImplicitTypeParameterConversion (Expression expr, TypeSpec target_type)
94 var expr_type = (TypeParameterSpec) expr.Type;
97 // From T to a type parameter U, provided T depends on U
99 var ttype = target_type as TypeParameterSpec;
101 if (expr_type.TypeArguments != null) {
102 foreach (var targ in expr_type.TypeArguments) {
103 if (!TypeSpecComparer.Override.IsEqual (ttype, targ))
106 if (expr_type.IsReferenceType && !ttype.IsReferenceType)
107 return new BoxedCast (expr, target_type);
109 return new ClassCast (expr, target_type);
117 // LAMESPEC: From T to dynamic type because it's like T to object
119 if (target_type == InternalType.Dynamic) {
120 if (expr_type.IsReferenceType)
121 return new ClassCast (expr, target_type);
123 return new BoxedCast (expr, target_type);
127 // From T to its effective base class C
128 // From T to any base class of C (it cannot contain dynamic of be of dynamic type)
129 // From T to any interface implemented by C
131 var base_type = expr_type.GetEffectiveBase ();
132 if (base_type == target_type || TypeSpec.IsBaseClass (base_type, target_type, false) || base_type.ImplementsInterface (target_type)) {
133 if (expr_type.IsReferenceType)
134 return new ClassCast (expr, target_type);
136 return new BoxedCast (expr, target_type);
139 if (target_type.IsInterface && expr_type.IsConvertibleToInterface (target_type)) {
140 if (expr_type.IsReferenceType)
141 return new ClassCast (expr, target_type);
143 return new BoxedCast (expr, target_type);
149 static Expression ExplicitTypeParameterConversion (Expression source, TypeSpec source_type, TypeSpec target_type)
151 var target_tp = target_type as TypeParameterSpec;
152 if (target_tp != null) {
153 if (target_tp.TypeArguments != null) {
154 foreach (var targ in target_tp.TypeArguments) {
155 if (!TypeSpecComparer.Override.IsEqual (source_type, targ))
158 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
162 if (target_tp.Interfaces != null) {
163 foreach (TypeSpec iface in target_tp.Interfaces) {
164 if (!TypeManager.IsGenericParameter (iface))
167 if (TypeManager.IsSubclassOf (source_type, iface))
168 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
175 if (target_type.IsInterface)
176 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
181 static Expression ImplicitReferenceConversion (Expression expr, TypeSpec target_type, bool explicit_cast)
183 TypeSpec expr_type = expr.Type;
185 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
186 // if we are a method group, emit a warning
191 if (expr_type == TypeManager.void_type)
194 if (expr_type.Kind == MemberKind.TypeParameter)
195 return ImplicitTypeParameterConversion (expr, target_type);
198 // from the null type to any reference-type.
200 NullLiteral nl = expr as NullLiteral;
202 return nl.ConvertImplicitly (null, target_type);
205 if (ImplicitReferenceConversionExists (expr, target_type)) {
207 // Avoid wrapping implicitly convertible reference type
212 return EmptyCast.Create (expr, target_type);
215 return ImplicitBoxingConversion (expr, expr_type, target_type);
219 // 6.1.6 Implicit reference conversions
221 public static bool ImplicitReferenceConversionExists (Expression expr, TypeSpec target_type)
223 if (TypeManager.IsStruct (target_type))
226 TypeSpec expr_type = expr.Type;
228 // from the null type to any reference-type.
229 if (expr_type == InternalType.Null)
230 return target_type != InternalType.AnonymousMethod;
232 if (TypeManager.IsGenericParameter (expr_type))
233 return ImplicitTypeParameterConversion (expr, target_type) != null;
235 // This code is kind of mirrored inside ImplicitStandardConversionExists
236 // with the small distinction that we only probe there
238 // Always ensure that the code here and there is in sync
240 // from any class-type S to any interface-type T.
241 if (target_type.IsInterface) {
242 if (expr_type.ImplementsInterface (target_type)){
243 return !TypeManager.IsValueType (expr_type);
248 // notice that it is possible to write "ValueType v = 1", the ValueType here
249 // is an abstract class, and not really a value type, so we apply the same rules.
251 if (target_type == TypeManager.object_type || target_type == InternalType.Dynamic) {
253 // A pointer type cannot be converted to object
255 if (expr_type.IsPointer)
258 if (TypeManager.IsValueType (expr_type))
261 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type || expr_type.IsDelegate || expr_type.Kind == MemberKind.ArrayType) {
262 // No mcs internal types are convertible
263 return true; // expr_type.MetaInfo.Module != typeof (Convert).Module;
266 // From anything to dynamic, including dynamic -> dynamic
267 if (target_type == InternalType.Dynamic)
270 // From dynamic to object
271 if (expr_type == InternalType.Dynamic)
275 } else if (target_type == TypeManager.value_type) {
276 return expr_type == TypeManager.enum_type;
277 } else if (expr_type == target_type || TypeSpec.IsBaseClass (expr_type, target_type, true)) {
279 // Special case: enumeration to System.Enum.
280 // System.Enum is not a value type, it is a class, so we need
281 // a boxing conversion
283 if (target_type == TypeManager.enum_type || TypeManager.IsGenericParameter (expr_type))
286 if (TypeManager.IsValueType (expr_type))
289 // Array type variance conversion
290 //if (target_type.IsArray != expr_type.IsArray)
296 var expr_type_array = expr_type as ArrayContainer;
297 if (expr_type_array != null) {
298 var target_type_array = target_type as ArrayContainer;
299 // from an array-type S to an array-type of type T
300 if (target_type_array != null && expr_type_array.Rank == target_type_array.Rank) {
303 // Both SE and TE are reference-types
305 TypeSpec expr_element_type = expr_type_array.Element;
306 if (!TypeManager.IsReferenceType (expr_element_type))
309 TypeSpec target_element_type = target_type_array.Element;
310 if (!TypeManager.IsReferenceType (target_element_type))
313 if (MyEmptyExpr == null)
314 MyEmptyExpr = new EmptyExpression (expr_element_type);
316 MyEmptyExpr.SetType (expr_element_type);
318 return ImplicitStandardConversionExists (MyEmptyExpr, target_element_type);
321 // from an array-type to System.Array
322 if (target_type == TypeManager.array_type)
325 // from an array-type of type T to IList<T>
326 if (ArrayToIList (expr_type_array, target_type, false))
332 if (TypeSpecComparer.Variant.IsEqual (expr_type, target_type))
335 // from any interface type S to interface-type T.
336 if (expr_type.IsInterface && target_type.IsInterface) {
337 return expr_type.ImplementsInterface (target_type);
340 // from any delegate type to System.Delegate
341 if (target_type == TypeManager.delegate_type &&
342 (expr_type == TypeManager.delegate_type || expr_type.IsDelegate))
348 public static Expression ImplicitBoxingConversion (Expression expr, TypeSpec expr_type, TypeSpec target_type)
351 // From any value-type to the type object.
353 if (target_type == TypeManager.object_type || target_type == InternalType.Dynamic) {
355 // A pointer type cannot be converted to object
357 if (expr_type.IsPointer)
360 if (!TypeManager.IsValueType (expr_type))
363 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
367 // From any value-type to the type System.ValueType.
369 if (target_type == TypeManager.value_type) {
370 if (!TypeManager.IsValueType (expr_type))
373 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
376 if (target_type == TypeManager.enum_type) {
378 // From any enum-type to the type System.Enum.
380 if (TypeManager.IsEnumType (expr_type))
381 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
385 // From a nullable-type to a reference type, if a boxing conversion exists from
386 // the underlying type to the reference type
388 if (TypeManager.IsNullableType (expr_type)) {
389 if (!TypeManager.IsReferenceType (target_type))
392 var res = ImplicitBoxingConversion (expr, Nullable.NullableInfo.GetUnderlyingType (expr_type), target_type);
394 // "cast" underlying type to target type to emit correct InvalidCastException when
395 // underlying hierarchy changes without recompilation
396 if (res != null && expr != null)
397 res = new UnboxCast (res, target_type);
402 if (TypeSpec.IsBaseClass (expr_type, target_type, false)) {
404 // Don't box same type arguments
406 if (TypeManager.IsGenericParameter (expr_type) && expr_type != target_type)
407 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
412 // This code is kind of mirrored inside ImplicitStandardConversionExists
413 // with the small distinction that we only probe there
415 // Always ensure that the code here and there is in sync
417 // from any class-type S to any interface-type T.
418 if (target_type.IsInterface) {
419 if (expr_type.ImplementsInterface (target_type) &&
420 (TypeManager.IsGenericParameter (expr_type) || TypeManager.IsValueType (expr_type))) {
421 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
428 public static Expression ImplicitNulableConversion (ResolveContext ec, Expression expr, TypeSpec target_type)
430 TypeSpec expr_type = expr.Type;
433 // From null to any nullable type
435 if (expr_type == InternalType.Null)
436 return ec == null ? EmptyExpression.Null : Nullable.LiftedNull.Create (target_type, expr.Location);
439 TypeSpec t_el = Nullable.NullableInfo.GetUnderlyingType (target_type);
442 if (TypeManager.IsNullableType (expr_type))
443 expr_type = Nullable.NullableInfo.GetUnderlyingType (expr_type);
446 // Predefined implicit identity or implicit numeric conversion
447 // has to exist between underlying type S and underlying type T
450 // conversion exists only mode
452 if (expr_type == t_el)
453 return EmptyExpression.Null;
455 if (expr is Constant)
456 return ((Constant) expr).ConvertImplicitly (ec, t_el);
458 return ImplicitNumericConversion (null, expr_type, t_el);
462 if (expr_type != expr.Type)
463 unwrap = Nullable.Unwrap.Create (expr);
467 Expression conv = unwrap;
468 if (expr_type != t_el) {
469 if (conv is Constant)
470 conv = ((Constant)conv).ConvertImplicitly (ec, t_el);
472 conv = ImplicitNumericConversion (conv, expr_type, t_el);
478 if (expr_type != expr.Type)
479 return new Nullable.Lifted (conv, unwrap, target_type).Resolve (ec);
481 return Nullable.Wrap.Create (conv, target_type);
485 /// Implicit Numeric Conversions.
487 /// expr is the expression to convert, returns a new expression of type
488 /// target_type or null if an implicit conversion is not possible.
490 public static Expression ImplicitNumericConversion (Expression expr, TypeSpec target_type)
492 return ImplicitNumericConversion (expr, expr.Type, target_type);
495 static Expression ImplicitNumericConversion (Expression expr, TypeSpec expr_type, TypeSpec target_type)
497 if (expr_type == TypeManager.sbyte_type){
499 // From sbyte to short, int, long, float, double, decimal
501 if (target_type == TypeManager.int32_type)
502 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
503 if (target_type == TypeManager.int64_type)
504 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
505 if (target_type == TypeManager.double_type)
506 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
507 if (target_type == TypeManager.float_type)
508 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
509 if (target_type == TypeManager.short_type)
510 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
511 if (target_type == TypeManager.decimal_type)
512 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
513 } else if (expr_type == TypeManager.byte_type){
515 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
517 if (target_type == TypeManager.int32_type || target_type == TypeManager.uint32_type ||
518 target_type == TypeManager.short_type || target_type == TypeManager.ushort_type)
519 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
521 if (target_type == TypeManager.uint64_type)
522 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
523 if (target_type == TypeManager.int64_type)
524 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
525 if (target_type == TypeManager.float_type)
526 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
527 if (target_type == TypeManager.double_type)
528 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
529 if (target_type == TypeManager.decimal_type)
530 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
532 } else if (expr_type == TypeManager.short_type){
534 // From short to int, long, float, double, decimal
536 if (target_type == TypeManager.int32_type)
537 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
538 if (target_type == TypeManager.int64_type)
539 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
540 if (target_type == TypeManager.double_type)
541 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
542 if (target_type == TypeManager.float_type)
543 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
544 if (target_type == TypeManager.decimal_type)
545 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
547 } else if (expr_type == TypeManager.ushort_type){
549 // From ushort to int, uint, long, ulong, float, double, decimal
551 if (target_type == TypeManager.int32_type || target_type == TypeManager.uint32_type)
552 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
554 if (target_type == TypeManager.uint64_type)
555 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
556 if (target_type == TypeManager.int64_type)
557 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
558 if (target_type == TypeManager.double_type)
559 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
560 if (target_type == TypeManager.float_type)
561 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
562 if (target_type == TypeManager.decimal_type)
563 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
564 } else if (expr_type == TypeManager.int32_type){
566 // From int to long, float, double, decimal
568 if (target_type == TypeManager.int64_type)
569 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
570 if (target_type == TypeManager.double_type)
571 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
572 if (target_type == TypeManager.float_type)
573 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
574 if (target_type == TypeManager.decimal_type)
575 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
576 } else if (expr_type == TypeManager.uint32_type){
578 // From uint to long, ulong, float, double, decimal
580 if (target_type == TypeManager.int64_type)
581 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
582 if (target_type == TypeManager.uint64_type)
583 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
584 if (target_type == TypeManager.double_type)
585 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
586 if (target_type == TypeManager.float_type)
587 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
588 if (target_type == TypeManager.decimal_type)
589 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
590 } else if (expr_type == TypeManager.int64_type){
592 // From long/ulong to float, double
594 if (target_type == TypeManager.double_type)
595 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
596 if (target_type == TypeManager.float_type)
597 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
598 if (target_type == TypeManager.decimal_type)
599 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
600 } else if (expr_type == TypeManager.uint64_type){
602 // From ulong to float, double
604 if (target_type == TypeManager.double_type)
605 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
606 if (target_type == TypeManager.float_type)
607 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
608 if (target_type == TypeManager.decimal_type)
609 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
610 } else if (expr_type == TypeManager.char_type){
612 // From char to ushort, int, uint, long, ulong, float, double, decimal
614 if ((target_type == TypeManager.ushort_type) ||
615 (target_type == TypeManager.int32_type) ||
616 (target_type == TypeManager.uint32_type))
617 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
618 if (target_type == TypeManager.uint64_type)
619 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
620 if (target_type == TypeManager.int64_type)
621 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
622 if (target_type == TypeManager.float_type)
623 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
624 if (target_type == TypeManager.double_type)
625 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
626 if (target_type == TypeManager.decimal_type)
627 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
628 } else if (expr_type == TypeManager.float_type){
632 if (target_type == TypeManager.double_type)
633 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
640 /// Same as ImplicitStandardConversionExists except that it also looks at
641 /// implicit user defined conversions - needed for overload resolution
643 public static bool ImplicitConversionExists (ResolveContext ec, Expression expr, TypeSpec target_type)
645 if (ImplicitStandardConversionExists (expr, target_type))
648 if (expr.Type == InternalType.AnonymousMethod) {
649 if (!TypeManager.IsDelegateType (target_type) && target_type.GetDefinition () != TypeManager.expression_type)
652 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
653 return ame.ImplicitStandardConversionExists (ec, target_type);
656 if (expr.eclass == ExprClass.MethodGroup) {
657 if (target_type.IsDelegate && RootContext.Version != LanguageVersion.ISO_1) {
658 MethodGroupExpr mg = expr as MethodGroupExpr;
660 return DelegateCreation.ImplicitStandardConversionExists (ec, mg, target_type);
666 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
670 /// Determines if a standard implicit conversion exists from
671 /// expr_type to target_type
674 public static bool ImplicitStandardConversionExists (Expression expr, TypeSpec target_type)
676 TypeSpec expr_type = expr.Type;
678 NullLiteral nl = expr as NullLiteral;
680 return nl.ConvertImplicitly (null, target_type) != null;
682 if (expr_type == TypeManager.void_type)
685 if (expr_type == target_type)
688 if (TypeManager.IsNullableType (target_type)) {
689 return ImplicitNulableConversion (null, expr, target_type) != null;
692 // First numeric conversions
693 if (ImplicitNumericConversion (null, expr_type, target_type) != null)
696 if (ImplicitReferenceConversionExists (expr, target_type))
699 if (ImplicitBoxingConversion (null, expr_type, target_type) != null)
703 // Implicit Constant Expression Conversions
705 if (expr is IntConstant){
706 int value = ((IntConstant) expr).Value;
708 if (target_type == TypeManager.sbyte_type){
709 if (value >= SByte.MinValue && value <= SByte.MaxValue)
711 } else if (target_type == TypeManager.byte_type){
712 if (value >= 0 && value <= Byte.MaxValue)
714 } else if (target_type == TypeManager.short_type){
715 if (value >= Int16.MinValue && value <= Int16.MaxValue)
717 } else if (target_type == TypeManager.ushort_type){
718 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
720 } else if (target_type == TypeManager.uint32_type){
723 } else if (target_type == TypeManager.uint64_type){
725 // we can optimize this case: a positive int32
726 // always fits on a uint64. But we need an opcode
734 if (expr is LongConstant && target_type == TypeManager.uint64_type){
736 // Try the implicit constant expression conversion
737 // from long to ulong, instead of a nice routine,
740 long v = ((LongConstant) expr).Value;
745 if (expr is IntegralConstant && TypeManager.IsEnumType (target_type)) {
746 var i = (IntegralConstant) expr;
748 // LAMESPEC: csc allows any constant like 0 values to be converted, including const float f = 0.0
750 // An implicit enumeration conversion permits the decimal-integer-literal 0
751 // to be converted to any enum-type and to any nullable-type whose underlying
752 // type is an enum-type
754 return i.IsZeroInteger;
758 // If `expr_type' implements `target_type' (which is an iface)
759 // see TryImplicitIntConversion
761 if (target_type.IsInterface && expr_type.ImplementsInterface (target_type))
764 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
767 // Conversion from __arglist to System.ArgIterator
768 if (expr_type == InternalType.Arglist)
769 return target_type == TypeManager.arg_iterator_type;
775 /// Finds "most encompassed type" according to the spec (13.4.2)
776 /// amongst the methods in the MethodGroupExpr
778 public static TypeSpec FindMostEncompassedType (IEnumerable<TypeSpec> types)
780 TypeSpec best = null;
781 EmptyExpression expr = EmptyExpression.Grab ();
783 foreach (TypeSpec t in types) {
790 if (ImplicitStandardConversionExists (expr, best))
795 foreach (TypeSpec t in types) {
798 if (!ImplicitStandardConversionExists (expr, t)) {
804 EmptyExpression.Release (expr);
810 /// Finds "most encompassing type" according to the spec (13.4.2)
811 /// amongst the types in the given set
813 static TypeSpec FindMostEncompassingType (IList<TypeSpec> types)
815 TypeSpec best = null;
817 if (types.Count == 0)
820 if (types.Count == 1)
823 EmptyExpression expr = EmptyExpression.Grab ();
825 foreach (TypeSpec t in types) {
832 if (ImplicitStandardConversionExists (expr, t))
836 foreach (TypeSpec t in types) {
840 if (!ImplicitStandardConversionExists (expr, best)) {
846 EmptyExpression.Release (expr);
852 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
853 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
854 /// for explicit and implicit conversion operators.
856 static TypeSpec FindMostSpecificSource (IList<MethodSpec> list,
857 Expression source, bool apply_explicit_conv_rules)
859 var src_types_set = new List<TypeSpec> ();
862 // If any operator converts from S then Sx = S
864 TypeSpec source_type = source.Type;
865 foreach (var mb in list){
866 TypeSpec param_type = mb.Parameters.Types [0];
868 if (param_type == source_type)
871 src_types_set.Add (param_type);
875 // Explicit Conv rules
877 if (apply_explicit_conv_rules) {
878 var candidate_set = new List<TypeSpec> ();
880 foreach (TypeSpec param_type in src_types_set){
881 if (ImplicitStandardConversionExists (source, param_type))
882 candidate_set.Add (param_type);
885 if (candidate_set.Count != 0)
886 return FindMostEncompassedType (candidate_set);
892 if (apply_explicit_conv_rules)
893 return FindMostEncompassingType (src_types_set);
895 return FindMostEncompassedType (src_types_set);
899 /// Finds the most specific target Tx according to section 13.4.4
901 static public TypeSpec FindMostSpecificTarget (IList<MethodSpec> list,
902 TypeSpec target, bool apply_explicit_conv_rules)
904 var tgt_types_set = new List<TypeSpec> ();
907 // If any operator converts to T then Tx = T
909 foreach (var mi in list){
910 TypeSpec ret_type = mi.ReturnType;
911 if (ret_type == target)
914 tgt_types_set.Add (ret_type);
918 // Explicit conv rules
920 if (apply_explicit_conv_rules) {
921 var candidate_set = new List<TypeSpec> ();
923 EmptyExpression expr = EmptyExpression.Grab ();
925 foreach (TypeSpec ret_type in tgt_types_set){
926 expr.SetType (ret_type);
928 if (ImplicitStandardConversionExists (expr, target))
929 candidate_set.Add (ret_type);
932 EmptyExpression.Release (expr);
934 if (candidate_set.Count != 0)
935 return FindMostEncompassingType (candidate_set);
939 // Okay, final case !
941 if (apply_explicit_conv_rules)
942 return FindMostEncompassedType (tgt_types_set);
944 return FindMostEncompassingType (tgt_types_set);
948 /// User-defined Implicit conversions
950 static public Expression ImplicitUserConversion (ResolveContext ec, Expression source, TypeSpec target, Location loc)
952 return UserDefinedConversion (ec, source, target, true, loc);
956 /// User-defined Explicit conversions
958 static Expression ExplicitUserConversion (ResolveContext ec, Expression source, TypeSpec target, Location loc)
960 return UserDefinedConversion (ec, source, target, false, loc);
963 static void FindApplicableUserDefinedConversionOperators (IList<MemberSpec> operators, Expression source, TypeSpec target, bool implicitOnly, ref List<MethodSpec> candidates)
966 // LAMESPEC: Undocumented IntPtr/UIntPtr conversions
967 // IntPtr -> uint uses int
968 // UIntPtr -> long uses ulong
970 if (source.Type == TypeManager.intptr_type) {
971 if (target == TypeManager.uint32_type)
972 target = TypeManager.int32_type;
973 } else if (source.Type == TypeManager.uintptr_type) {
974 if (target == TypeManager.int64_type)
975 target = TypeManager.uint64_type;
978 // For a conversion operator to be applicable, it must be possible
979 // to perform a standard conversion from the source type to
980 // the operand type of the operator, and it must be possible
981 // to perform a standard conversion from the result type of
982 // the operator to the target type.
984 Expression texpr = null;
986 foreach (MethodSpec op in operators) {
988 // Can be null because MemberCache.GetUserOperator does not resize the array
992 var t = op.Parameters.Types[0];
993 if (source.Type != t && !ImplicitStandardConversionExists (source, t)) {
997 if (!ImplicitStandardConversionExists (new EmptyExpression (t), source.Type))
1003 // LAMESPEC: Exclude UIntPtr -> int conversion
1004 if (t == TypeManager.uint32_type && source.Type == TypeManager.uintptr_type)
1007 if (target != t && !ImplicitStandardConversionExists (new EmptyExpression (t), target)) {
1012 texpr = new EmptyExpression (target);
1014 if (!ImplicitStandardConversionExists (texpr, t))
1018 if (candidates == null)
1019 candidates = new List<MethodSpec> ();
1021 candidates.Add (op);
1026 // User-defined conversions
1028 static Expression UserDefinedConversion (ResolveContext ec, Expression source, TypeSpec target, bool implicitOnly, Location loc)
1030 List<MethodSpec> candidates = null;
1033 // If S or T are nullable types, S0 and T0 are their underlying types
1034 // otherwise S0 and T0 are equal to S and T respectively.
1036 TypeSpec source_type = source.Type;
1037 TypeSpec target_type = target;
1040 if (TypeManager.IsNullableType (source_type)) {
1041 // No implicit conversion S? -> T for non-reference types
1042 if (implicitOnly && !TypeManager.IsReferenceType (target_type) && !TypeManager.IsNullableType (target_type))
1045 unwrap = source = Nullable.Unwrap.Create (source);
1046 source_type = source.Type;
1051 if (TypeManager.IsNullableType (target_type))
1052 target_type = Nullable.NullableInfo.GetUnderlyingType (target_type);
1054 // Only these containers can contain a user defined implicit or explicit operators
1055 const MemberKind user_conversion_kinds = MemberKind.Class | MemberKind.Struct | MemberKind.TypeParameter;
1057 if ((source_type.Kind & user_conversion_kinds) != 0 && source_type != TypeManager.decimal_type) {
1058 bool declared_only = source_type.IsStruct;
1060 var operators = MemberCache.GetUserOperator (source_type, Operator.OpType.Implicit, declared_only);
1061 if (operators != null) {
1062 FindApplicableUserDefinedConversionOperators (operators, source, target_type, implicitOnly, ref candidates);
1065 if (!implicitOnly) {
1066 operators = MemberCache.GetUserOperator (source_type, Operator.OpType.Explicit, declared_only);
1067 if (operators != null) {
1068 FindApplicableUserDefinedConversionOperators (operators, source, target_type, false, ref candidates);
1073 if ((target.Kind & user_conversion_kinds) != 0 && target != TypeManager.decimal_type) {
1074 bool declared_only = target.IsStruct || implicitOnly;
1076 var operators = MemberCache.GetUserOperator (target_type, Operator.OpType.Implicit, declared_only);
1077 if (operators != null) {
1078 FindApplicableUserDefinedConversionOperators (operators, source, target_type, implicitOnly, ref candidates);
1081 if (!implicitOnly) {
1082 operators = MemberCache.GetUserOperator (target_type, Operator.OpType.Explicit, declared_only);
1083 if (operators != null) {
1084 FindApplicableUserDefinedConversionOperators (operators, source, target_type, false, ref candidates);
1089 if (candidates == null)
1093 // Find the most specific conversion operator
1095 MethodSpec most_specific_operator;
1097 if (candidates.Count == 1) {
1098 most_specific_operator = candidates[0];
1099 s_x = most_specific_operator.Parameters.Types[0];
1100 t_x = most_specific_operator.ReturnType;
1102 s_x = FindMostSpecificSource (candidates, source, !implicitOnly);
1106 t_x = FindMostSpecificTarget (candidates, target, !implicitOnly);
1110 most_specific_operator = candidates[0];
1112 for (int i = 1; i < candidates.Count; ++i) {
1113 if (candidates[i].ReturnType == t_x && candidates[i].Parameters.Types[0] == s_x) {
1114 most_specific_operator = candidates[i];
1121 // Convert input type when it's different to best operator argument
1123 if (s_x != source.Type)
1124 source = implicitOnly ?
1125 ImplicitConversionStandard (ec, source, s_x, loc) :
1126 ExplicitConversionStandard (ec, source, s_x, loc);
1128 source = new UserCast (most_specific_operator, source, loc).Resolve (ec);
1131 // Convert result type when it's different to best operator return type
1133 if (t_x != target_type) {
1135 // User operator is of T?, no need to lift it
1137 if (TypeManager.IsNullableType (t_x) && t_x == target)
1140 source = implicitOnly ?
1141 ImplicitConversionStandard (ec, source, target_type, loc) :
1142 ExplicitConversionStandard (ec, source, target_type, loc);
1146 // Source expression is of nullable type, lift the result in case of it's null
1148 if (unwrap != null && (TypeManager.IsReferenceType (target) || target_type != target))
1149 source = new Nullable.Lifted (source, unwrap, target).Resolve (ec);
1150 else if (target_type != target)
1151 source = Nullable.Wrap.Create (source, target);
1157 /// Converts implicitly the resolved expression `expr' into the
1158 /// `target_type'. It returns a new expression that can be used
1159 /// in a context that expects a `target_type'.
1161 static public Expression ImplicitConversion (ResolveContext ec, Expression expr,
1162 TypeSpec target_type, Location loc)
1166 if (target_type == null)
1167 throw new Exception ("Target type is null");
1169 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1173 e = ImplicitUserConversion (ec, expr, target_type, loc);
1182 /// Attempts to apply the `Standard Implicit
1183 /// Conversion' rules to the expression `expr' into
1184 /// the `target_type'. It returns a new expression
1185 /// that can be used in a context that expects a
1188 /// This is different from `ImplicitConversion' in that the
1189 /// user defined implicit conversions are excluded.
1191 static public Expression ImplicitConversionStandard (ResolveContext ec, Expression expr,
1192 TypeSpec target_type, Location loc)
1194 return ImplicitConversionStandard (ec, expr, target_type, loc, false);
1197 static Expression ImplicitConversionStandard (ResolveContext ec, Expression expr, TypeSpec target_type, Location loc, bool explicit_cast)
1199 if (expr.eclass == ExprClass.MethodGroup){
1200 if (!TypeManager.IsDelegateType (target_type)){
1205 // Only allow anonymous method conversions on post ISO_1
1207 if (RootContext.Version != LanguageVersion.ISO_1){
1208 MethodGroupExpr mg = expr as MethodGroupExpr;
1210 return ImplicitDelegateCreation.Create (
1211 ec, mg, target_type, loc);
1215 TypeSpec expr_type = expr.Type;
1218 if (expr_type == target_type) {
1219 if (expr_type != InternalType.Null && expr_type != InternalType.AnonymousMethod)
1224 if (TypeManager.IsNullableType (target_type))
1225 return ImplicitNulableConversion (ec, expr, target_type);
1228 // Attempt to do the implicit constant expression conversions
1230 Constant c = expr as Constant;
1233 c = c.ConvertImplicitly (ec, target_type);
1235 Console.WriteLine ("Conversion error happened in line {0}", loc);
1242 e = ImplicitNumericConversion (expr, expr_type, target_type);
1246 e = ImplicitReferenceConversion (expr, target_type, explicit_cast);
1250 if (expr is IntegralConstant && TypeManager.IsEnumType (target_type)){
1251 var i = (IntegralConstant) expr;
1253 // LAMESPEC: csc allows any constant like 0 values to be converted, including const float f = 0.0
1255 // An implicit enumeration conversion permits the decimal-integer-literal 0
1256 // to be converted to any enum-type and to any nullable-type whose underlying
1257 // type is an enum-type
1259 if (i.IsZeroInteger) {
1260 // Recreate 0 literal to remove any collected conversions
1261 return new EnumConstant (new IntLiteral (0, i.Location), target_type).Resolve (ec);
1266 if (expr_type.IsPointer){
1267 if (target_type == TypeManager.void_ptr_type)
1268 return EmptyCast.Create (expr, target_type);
1271 // yep, comparing pointer types cant be done with
1272 // t1 == t2, we have to compare their element types.
1274 if (target_type.IsPointer){
1275 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1282 if (expr_type == InternalType.Null && target_type.IsPointer)
1283 return EmptyCast.Create (new NullPointer (loc), target_type);
1286 if (expr_type == InternalType.AnonymousMethod){
1287 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1288 Expression am = ame.Compatible (ec, target_type);
1290 return am.Resolve (ec);
1293 if (expr_type == InternalType.Arglist && target_type == TypeManager.arg_iterator_type)
1300 /// Attempts to implicitly convert `source' into `target_type', using
1301 /// ImplicitConversion. If there is no implicit conversion, then
1302 /// an error is signaled
1304 static public Expression ImplicitConversionRequired (ResolveContext ec, Expression source,
1305 TypeSpec target_type, Location loc)
1307 Expression e = ImplicitConversion (ec, source, target_type, loc);
1311 if (source.Type == InternalType.Dynamic) {
1312 Arguments args = new Arguments (1);
1313 args.Add (new Argument (source));
1314 return new DynamicConversion (target_type, 0, args, loc).Resolve (ec);
1317 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1322 /// Performs the explicit numeric conversions
1324 /// There are a few conversions that are not part of the C# standard,
1325 /// they were interim hacks in the C# compiler that were supposed to
1326 /// become explicit operators in the UIntPtr class and IntPtr class,
1327 /// but for historical reasons it did not happen, so the C# compiler
1328 /// ended up with these special hacks.
1330 /// See bug 59800 for details.
1332 /// The conversion are:
1342 public static Expression ExplicitNumericConversion (Expression expr, TypeSpec target_type)
1344 TypeSpec expr_type = expr.Type;
1345 TypeSpec real_target_type = target_type;
1347 if (expr_type == TypeManager.sbyte_type){
1349 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1351 if (real_target_type == TypeManager.byte_type)
1352 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1353 if (real_target_type == TypeManager.ushort_type)
1354 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1355 if (real_target_type == TypeManager.uint32_type)
1356 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1357 if (real_target_type == TypeManager.uint64_type)
1358 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1359 if (real_target_type == TypeManager.char_type)
1360 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1362 // One of the built-in conversions that belonged in the class library
1363 if (real_target_type == TypeManager.uintptr_type){
1364 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1366 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1368 } else if (expr_type == TypeManager.byte_type){
1370 // From byte to sbyte and char
1372 if (real_target_type == TypeManager.sbyte_type)
1373 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1374 if (real_target_type == TypeManager.char_type)
1375 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1376 } else if (expr_type == TypeManager.short_type){
1378 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1380 if (real_target_type == TypeManager.sbyte_type)
1381 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1382 if (real_target_type == TypeManager.byte_type)
1383 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1384 if (real_target_type == TypeManager.ushort_type)
1385 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1386 if (real_target_type == TypeManager.uint32_type)
1387 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1388 if (real_target_type == TypeManager.uint64_type)
1389 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1390 if (real_target_type == TypeManager.char_type)
1391 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1393 // One of the built-in conversions that belonged in the class library
1394 if (real_target_type == TypeManager.uintptr_type){
1395 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1397 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1399 } else if (expr_type == TypeManager.ushort_type){
1401 // From ushort to sbyte, byte, short, char
1403 if (real_target_type == TypeManager.sbyte_type)
1404 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1405 if (real_target_type == TypeManager.byte_type)
1406 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1407 if (real_target_type == TypeManager.short_type)
1408 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1409 if (real_target_type == TypeManager.char_type)
1410 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1411 } else if (expr_type == TypeManager.int32_type){
1413 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1415 if (real_target_type == TypeManager.sbyte_type)
1416 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1417 if (real_target_type == TypeManager.byte_type)
1418 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1419 if (real_target_type == TypeManager.short_type)
1420 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1421 if (real_target_type == TypeManager.ushort_type)
1422 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1423 if (real_target_type == TypeManager.uint32_type)
1424 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1425 if (real_target_type == TypeManager.uint64_type)
1426 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1427 if (real_target_type == TypeManager.char_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1430 // One of the built-in conversions that belonged in the class library
1431 if (real_target_type == TypeManager.uintptr_type){
1432 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1434 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1436 } else if (expr_type == TypeManager.uint32_type){
1438 // From uint to sbyte, byte, short, ushort, int, char
1440 if (real_target_type == TypeManager.sbyte_type)
1441 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1442 if (real_target_type == TypeManager.byte_type)
1443 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1444 if (real_target_type == TypeManager.short_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1446 if (real_target_type == TypeManager.ushort_type)
1447 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1448 if (real_target_type == TypeManager.int32_type)
1449 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1450 if (real_target_type == TypeManager.char_type)
1451 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1452 } else if (expr_type == TypeManager.int64_type){
1454 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1456 if (real_target_type == TypeManager.sbyte_type)
1457 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1458 if (real_target_type == TypeManager.byte_type)
1459 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1460 if (real_target_type == TypeManager.short_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1462 if (real_target_type == TypeManager.ushort_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1464 if (real_target_type == TypeManager.int32_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1466 if (real_target_type == TypeManager.uint32_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1468 if (real_target_type == TypeManager.uint64_type)
1469 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1470 if (real_target_type == TypeManager.char_type)
1471 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1472 } else if (expr_type == TypeManager.uint64_type){
1474 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1476 if (real_target_type == TypeManager.sbyte_type)
1477 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1478 if (real_target_type == TypeManager.byte_type)
1479 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1480 if (real_target_type == TypeManager.short_type)
1481 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1482 if (real_target_type == TypeManager.ushort_type)
1483 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1484 if (real_target_type == TypeManager.int32_type)
1485 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1486 if (real_target_type == TypeManager.uint32_type)
1487 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1488 if (real_target_type == TypeManager.int64_type)
1489 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1490 if (real_target_type == TypeManager.char_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1493 // One of the built-in conversions that belonged in the class library
1494 if (real_target_type == TypeManager.intptr_type){
1495 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1496 TypeManager.intptr_type, true);
1498 } else if (expr_type == TypeManager.char_type){
1500 // From char to sbyte, byte, short
1502 if (real_target_type == TypeManager.sbyte_type)
1503 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1504 if (real_target_type == TypeManager.byte_type)
1505 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1506 if (real_target_type == TypeManager.short_type)
1507 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1508 } else if (expr_type == TypeManager.float_type){
1510 // From float to sbyte, byte, short,
1511 // ushort, int, uint, long, ulong, char
1514 if (real_target_type == TypeManager.sbyte_type)
1515 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1516 if (real_target_type == TypeManager.byte_type)
1517 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1518 if (real_target_type == TypeManager.short_type)
1519 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1520 if (real_target_type == TypeManager.ushort_type)
1521 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1522 if (real_target_type == TypeManager.int32_type)
1523 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1524 if (real_target_type == TypeManager.uint32_type)
1525 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1526 if (real_target_type == TypeManager.int64_type)
1527 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1528 if (real_target_type == TypeManager.uint64_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1530 if (real_target_type == TypeManager.char_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1532 if (real_target_type == TypeManager.decimal_type)
1533 return new CastToDecimal (expr, true);
1534 } else if (expr_type == TypeManager.double_type){
1536 // From double to sbyte, byte, short,
1537 // ushort, int, uint, long, ulong,
1538 // char, float or decimal
1540 if (real_target_type == TypeManager.sbyte_type)
1541 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1542 if (real_target_type == TypeManager.byte_type)
1543 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1544 if (real_target_type == TypeManager.short_type)
1545 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1546 if (real_target_type == TypeManager.ushort_type)
1547 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1548 if (real_target_type == TypeManager.int32_type)
1549 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1550 if (real_target_type == TypeManager.uint32_type)
1551 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1552 if (real_target_type == TypeManager.int64_type)
1553 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1554 if (real_target_type == TypeManager.uint64_type)
1555 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1556 if (real_target_type == TypeManager.char_type)
1557 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1558 if (real_target_type == TypeManager.float_type)
1559 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1560 if (real_target_type == TypeManager.decimal_type)
1561 return new CastToDecimal (expr, true);
1562 } else if (expr_type == TypeManager.uintptr_type){
1564 // Various built-in conversions that belonged in the class library
1566 // from uintptr to sbyte, short, int32
1568 if (real_target_type == TypeManager.sbyte_type){
1569 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1570 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1572 if (real_target_type == TypeManager.short_type){
1573 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1574 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1576 if (real_target_type == TypeManager.int32_type){
1577 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1578 TypeManager.int32_type);
1580 } else if (expr_type == TypeManager.intptr_type){
1581 if (real_target_type == TypeManager.uint64_type){
1582 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1583 TypeManager.uint64_type);
1585 } else if (expr_type == TypeManager.decimal_type) {
1586 return new CastFromDecimal (expr, target_type).Resolve ();
1592 /// Returns whether an explicit reference conversion can be performed
1593 /// from source_type to target_type
1595 public static bool ExplicitReferenceConversionExists (TypeSpec source_type, TypeSpec target_type)
1597 Expression e = ExplicitReferenceConversion (null, source_type, target_type);
1601 if (e == EmptyExpression.Null)
1604 throw new InternalErrorException ("Invalid probing conversion result");
1608 /// Implements Explicit Reference conversions
1610 static Expression ExplicitReferenceConversion (Expression source, TypeSpec source_type, TypeSpec target_type)
1613 // From object to a generic parameter
1615 if (source_type == TypeManager.object_type && TypeManager.IsGenericParameter (target_type))
1616 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1619 // Explicit type parameter conversion.
1621 if (source_type.Kind == MemberKind.TypeParameter)
1622 return ExplicitTypeParameterConversion (source, source_type, target_type);
1624 bool target_is_value_type = TypeManager.IsStruct (target_type) || TypeManager.IsEnumType (target_type);
1627 // Unboxing conversion from System.ValueType to any non-nullable-value-type
1629 if (source_type == TypeManager.value_type && target_is_value_type)
1630 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1633 // From object to any reference type or value type (unboxing)
1635 if (source_type == TypeManager.object_type)
1637 source == null ? EmptyExpression.Null :
1638 target_is_value_type ? new UnboxCast (source, target_type) :
1639 source is Constant ? (Expression) new EmptyConstantCast ((Constant) source, target_type) :
1640 new ClassCast (source, target_type);
1643 // From any class S to any class-type T, provided S is a base class of T
1645 if (source_type.Kind == MemberKind.Class && TypeSpec.IsBaseClass (target_type, source_type, true))
1646 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1649 // From any interface-type S to to any class type T, provided T is not
1650 // sealed, or provided T implements S.
1652 if (source_type.IsInterface) {
1653 if (!target_type.IsSealed || target_type.ImplementsInterface (source_type)) {
1654 if (target_type.IsClass)
1655 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1658 // Unboxing conversion from any interface-type to any non-nullable-value-type that
1659 // implements the interface-type
1661 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1665 // From System.Collections.Generic.IList<T> and its base interfaces to a one-dimensional
1666 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1668 var target_array = target_type as ArrayContainer;
1669 if (target_array != null && IList_To_Array (source_type, target_array))
1670 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1675 var source_array = source_type as ArrayContainer;
1676 if (source_array != null) {
1677 var target_array = target_type as ArrayContainer;
1678 if (target_array != null) {
1680 // From System.Array to any array-type
1682 if (source_type == TypeManager.array_type)
1683 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1686 // From an array type S with an element type Se to an array type T with an
1687 // element type Te provided all the following are true:
1688 // * S and T differe only in element type, in other words, S and T
1689 // have the same number of dimensions.
1690 // * Both Se and Te are reference types
1691 // * An explicit reference conversions exist from Se to Te
1693 if (source_array.Rank == target_array.Rank) {
1695 source_type = source_array.Element;
1696 if (!TypeManager.IsReferenceType (source_type))
1699 var target_element = target_array.Element;
1700 if (!TypeManager.IsReferenceType (target_element))
1703 if (ExplicitReferenceConversionExists (source_type, target_element))
1704 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1711 // From a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces,
1712 // provided that there is an explicit reference conversion from S to T
1714 if (ArrayToIList (source_array, target_type, true))
1715 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1721 // From any class type S to any interface T, provides S is not sealed
1722 // and provided S does not implement T.
1724 if (target_type.IsInterface && !source_type.IsSealed && !source_type.ImplementsInterface (target_type)) {
1725 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1729 // From System delegate to any delegate-type
1731 if (source_type == TypeManager.delegate_type && TypeManager.IsDelegateType (target_type))
1732 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1738 /// Performs an explicit conversion of the expression `expr' whose
1739 /// type is expr.Type to `target_type'.
1741 static public Expression ExplicitConversionCore (ResolveContext ec, Expression expr,
1742 TypeSpec target_type, Location loc)
1744 TypeSpec expr_type = expr.Type;
1746 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1747 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc, true);
1751 if (TypeManager.IsEnumType (expr_type)) {
1752 TypeSpec real_target = TypeManager.IsEnumType (target_type) ? EnumSpec.GetUnderlyingType (target_type) : target_type;
1753 Expression underlying = EmptyCast.Create (expr, EnumSpec.GetUnderlyingType (expr_type));
1754 if (underlying.Type == real_target)
1758 ne = ImplicitNumericConversion (underlying, real_target);
1761 ne = ExplicitNumericConversion (underlying, real_target);
1764 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1766 if (ne == null && (real_target == TypeManager.intptr_type || real_target == TypeManager.uintptr_type))
1767 ne = ExplicitUserConversion (ec, underlying, real_target, loc);
1769 return ne != null ? EmptyCast.Create (ne, target_type) : null;
1772 if (TypeManager.IsEnumType (target_type)) {
1774 // System.Enum can be unboxed to any enum-type
1776 if (expr_type == TypeManager.enum_type)
1777 return new UnboxCast (expr, target_type);
1779 TypeSpec real_target = TypeManager.IsEnumType (target_type) ? EnumSpec.GetUnderlyingType (target_type) : target_type;
1781 if (expr_type == real_target)
1782 return EmptyCast.Create (expr, target_type);
1784 ne = ImplicitNumericConversion (expr, real_target);
1786 return EmptyCast.Create (ne, target_type);
1788 ne = ExplicitNumericConversion (expr, real_target);
1790 return EmptyCast.Create (ne, target_type);
1793 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1795 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1796 ne = ExplicitUserConversion (ec, expr, real_target, loc);
1798 return ExplicitConversionCore (ec, ne, target_type, loc);
1801 ne = ExplicitNumericConversion (expr, target_type);
1807 // Skip the ExplicitReferenceConversion because we can not convert
1808 // from Null to a ValueType, and ExplicitReference wont check against
1809 // null literal explicitly
1811 if (expr_type != InternalType.Null) {
1812 ne = ExplicitReferenceConversion (expr, expr_type, target_type);
1818 ne = ExplicitUnsafe (expr, target_type);
1826 public static Expression ExplicitUnsafe (Expression expr, TypeSpec target_type)
1828 TypeSpec expr_type = expr.Type;
1830 if (target_type.IsPointer){
1831 if (expr_type.IsPointer)
1832 return EmptyCast.Create (expr, target_type);
1834 if (expr_type == TypeManager.sbyte_type ||
1835 expr_type == TypeManager.short_type ||
1836 expr_type == TypeManager.int32_type)
1837 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1839 if (expr_type == TypeManager.ushort_type ||
1840 expr_type == TypeManager.uint32_type ||
1841 expr_type == TypeManager.byte_type)
1842 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1844 if (expr_type == TypeManager.int64_type)
1845 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I);
1847 if (expr_type == TypeManager.uint64_type)
1848 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I);
1851 if (expr_type.IsPointer){
1852 if (target_type == TypeManager.sbyte_type)
1853 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1854 if (target_type == TypeManager.byte_type)
1855 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1856 if (target_type == TypeManager.short_type)
1857 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1858 if (target_type == TypeManager.ushort_type)
1859 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1860 if (target_type == TypeManager.int32_type)
1861 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1862 if (target_type == TypeManager.uint32_type)
1863 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1864 if (target_type == TypeManager.int64_type)
1865 return new ConvCast (expr, target_type, ConvCast.Mode.I_I8);
1866 if (target_type == TypeManager.uint64_type)
1867 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1873 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1875 static public Expression ExplicitConversionStandard (ResolveContext ec, Expression expr,
1876 TypeSpec target_type, Location l)
1878 int errors = ec.Report.Errors;
1879 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1880 if (ec.Report.Errors > errors)
1886 ne = ExplicitNumericConversion (expr, target_type);
1890 ne = ExplicitReferenceConversion (expr, expr.Type, target_type);
1894 if (ec.IsUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1895 return EmptyCast.Create (expr, target_type);
1897 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
1902 /// Performs an explicit conversion of the expression `expr' whose
1903 /// type is expr.Type to `target_type'.
1905 static public Expression ExplicitConversion (ResolveContext ec, Expression expr,
1906 TypeSpec target_type, Location loc)
1908 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1911 // Don't eliminate explicit precission casts
1914 if (target_type == TypeManager.float_type)
1915 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1917 if (target_type == TypeManager.double_type)
1918 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
1924 TypeSpec expr_type = expr.Type;
1925 if (TypeManager.IsNullableType (target_type)) {
1926 if (TypeManager.IsNullableType (expr_type)) {
1927 TypeSpec target = Nullable.NullableInfo.GetUnderlyingType (target_type);
1928 Expression unwrap = Nullable.Unwrap.Create (expr);
1929 e = ExplicitConversion (ec, unwrap, target, expr.Location);
1933 return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
1934 } else if (expr_type == TypeManager.object_type) {
1935 return new UnboxCast (expr, target_type);
1937 TypeSpec target = TypeManager.GetTypeArguments (target_type) [0];
1939 e = ExplicitConversionCore (ec, expr, target, loc);
1941 return Nullable.Wrap.Create (e, target_type);
1943 } else if (TypeManager.IsNullableType (expr_type)) {
1944 e = ImplicitBoxingConversion (expr, Nullable.NullableInfo.GetUnderlyingType (expr_type), target_type);
1948 e = Nullable.Unwrap.Create (expr, false);
1949 e = ExplicitConversionCore (ec, e, target_type, loc);
1951 return EmptyCast.Create (e, target_type);
1954 e = ExplicitUserConversion (ec, expr, target_type, loc);
1958 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);