2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright 2001, 2002, 2003 Ximian, Inc.
10 // Copyright 2003-2008 Novell, Inc.
14 using System.Collections.Generic;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Reflection.Emit;
19 namespace Mono.CSharp {
22 // A container class for all the conversion operations
24 static class Convert {
26 static EmptyExpression MyEmptyExpr;
27 static DoubleHash explicit_conv;
28 static DoubleHash implicit_conv;
35 public static void Reset ()
38 explicit_conv = new DoubleHash (100);
39 implicit_conv = new DoubleHash (100);
43 // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
44 // interfaces of this interface, provided there is an implicit reference conversion
47 static bool ArrayToIList (ArrayContainer array, TypeSpec list, bool isExplicit)
49 if (array.Rank != 1 || !list.IsGeneric)
52 var open_version = list.GetDefinition ();
53 if ((open_version != TypeManager.generic_ilist_type) &&
54 (open_version != TypeManager.generic_icollection_type) &&
55 (open_version != TypeManager.generic_ienumerable_type))
58 var arg_type = list.TypeArguments[0];
59 if (array.Element == arg_type)
63 return ExplicitReferenceConversionExists (array.Element, arg_type);
65 if (MyEmptyExpr == null)
66 MyEmptyExpr = new EmptyExpression (array.Element);
68 MyEmptyExpr.SetType (array.Element);
70 return ImplicitReferenceConversionExists (MyEmptyExpr, arg_type);
73 static bool IList_To_Array(TypeSpec list, ArrayContainer array)
75 if (array.Rank != 1 || !list.IsGeneric)
78 var open_version = list.GetDefinition ();
79 if ((open_version != TypeManager.generic_ilist_type) &&
80 (open_version != TypeManager.generic_icollection_type) &&
81 (open_version != TypeManager.generic_ienumerable_type))
84 var arg_type = list.TypeArguments[0];
85 if (array.Element == arg_type)
88 if (MyEmptyExpr == null)
89 MyEmptyExpr = new EmptyExpression (array.Element);
91 MyEmptyExpr.SetType (array.Element);
93 return ImplicitReferenceConversionExists (MyEmptyExpr, arg_type) || ExplicitReferenceConversionExists (array.Element, arg_type);
96 static Expression ImplicitTypeParameterConversion (Expression expr, TypeSpec target_type)
98 var expr_type = (TypeParameterSpec) expr.Type;
100 // From T to a type parameter U
102 var ttype = target_type as TypeParameterSpec;
104 if (expr_type.IsReferenceType && !ttype.IsReferenceType)
105 return new BoxedCast (expr, target_type);
107 return new ClassCast (expr, target_type);
111 // From T to its effective base class C
112 // From T to any base class of C
113 // From T to any interface implemented by C
115 var base_type = expr_type.BaseType;
116 if (base_type == target_type || TypeManager.IsSubclassOf (base_type, target_type) || base_type.ImplementsInterface (target_type)) {
117 if (expr_type.IsReferenceType)
118 return new ClassCast (expr, target_type);
120 return new BoxedCast (expr, target_type);
123 base_type = expr_type.GetEffectiveBase ();
125 var effective_ifaces = expr_type.Interfaces;
126 if (effective_ifaces != null) {
127 foreach (var t in effective_ifaces) {
128 if (t == target_type || t.ImplementsInterface (target_type)) {
129 if (expr_type.IsReferenceType)
130 return new ClassCast (expr, target_type);
132 return new BoxedCast (expr, target_type);
140 static Expression ExplicitTypeParameterConversion (Expression source, TypeSpec source_type, TypeSpec target_type)
142 var target_tp = target_type as TypeParameterSpec;
143 if (target_tp != null) {
144 if (target_tp.Interfaces != null) {
145 foreach (TypeSpec iface in target_tp.Interfaces) {
146 if (!TypeManager.IsGenericParameter (iface))
149 if (TypeManager.IsSubclassOf (source_type, iface))
150 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
157 if (target_type.IsInterface)
158 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
163 static Expression ImplicitReferenceConversion (Expression expr, TypeSpec target_type, bool explicit_cast)
165 TypeSpec expr_type = expr.Type;
167 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
168 // if we are a method group, emit a warning
173 if (expr_type == TypeManager.void_type)
176 if (expr_type.Kind == MemberKind.TypeParameter)
177 return ImplicitTypeParameterConversion (expr, target_type);
180 // from the null type to any reference-type.
182 NullLiteral nl = expr as NullLiteral;
184 return nl.ConvertImplicitly (null, target_type);
187 if (ImplicitReferenceConversionExists (expr, target_type)) {
189 // Avoid wrapping implicitly convertible reference type
194 return EmptyCast.Create (expr, target_type);
198 if (ImplicitBoxingConversionExists (expr, target_type, out use_class_cast)) {
200 return new ClassCast (expr, target_type);
202 return new BoxedCast (expr, target_type);
209 // 6.1.6 Implicit reference conversions
211 public static bool ImplicitReferenceConversionExists (Expression expr, TypeSpec target_type)
213 if (TypeManager.IsStruct (target_type))
216 TypeSpec expr_type = expr.Type;
218 // from the null type to any reference-type.
219 if (expr_type == TypeManager.null_type)
220 return target_type != InternalType.AnonymousMethod;
222 if (TypeManager.IsGenericParameter (expr_type))
223 return ImplicitTypeParameterConversion (expr, target_type) != null;
225 // This code is kind of mirrored inside ImplicitStandardConversionExists
226 // with the small distinction that we only probe there
228 // Always ensure that the code here and there is in sync
230 // from any class-type S to any interface-type T.
231 if (target_type.IsInterface) {
232 if (expr_type.ImplementsInterface (target_type)){
233 return !TypeManager.IsValueType (expr_type);
238 // notice that it is possible to write "ValueType v = 1", the ValueType here
239 // is an abstract class, and not really a value type, so we apply the same rules.
241 if (target_type == TypeManager.object_type || target_type == InternalType.Dynamic) {
243 // A pointer type cannot be converted to object
245 if (expr_type.IsPointer)
248 if (TypeManager.IsValueType (expr_type))
251 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type || expr_type.IsDelegate) {
252 // No mcs internal types are convertible
253 return true; // expr_type.MetaInfo.Module != typeof (Convert).Module;
256 // From anything to dynamic
257 if (target_type == InternalType.Dynamic)
260 // From dynamic to object
261 if (expr_type == InternalType.Dynamic)
265 } else if (target_type == TypeManager.value_type) {
266 return expr_type == TypeManager.enum_type;
267 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
269 // Special case: enumeration to System.Enum.
270 // System.Enum is not a value type, it is a class, so we need
271 // a boxing conversion
273 if (target_type == TypeManager.enum_type || TypeManager.IsGenericParameter (expr_type))
276 if (TypeManager.IsValueType (expr_type))
279 // Array type variance conversion
280 //if (target_type.IsArray != expr_type.IsArray)
286 var expr_type_array = expr_type as ArrayContainer;
287 if (expr_type_array != null) {
288 var target_type_array = target_type as ArrayContainer;
289 // from an array-type S to an array-type of type T
290 if (target_type_array != null && expr_type_array.Rank == target_type_array.Rank) {
293 // Both SE and TE are reference-types
295 TypeSpec expr_element_type = expr_type_array.Element;
296 if (!TypeManager.IsReferenceType (expr_element_type))
299 TypeSpec target_element_type = target_type_array.Element;
300 if (!TypeManager.IsReferenceType (target_element_type))
303 if (MyEmptyExpr == null)
304 MyEmptyExpr = new EmptyExpression (expr_element_type);
306 MyEmptyExpr.SetType (expr_element_type);
308 return ImplicitStandardConversionExists (MyEmptyExpr, target_element_type);
311 // from an array-type to System.Array
312 if (target_type == TypeManager.array_type)
315 // from an array-type of type T to IList<T>
316 if (ArrayToIList (expr_type_array, target_type, false))
322 if (TypeSpecComparer.Variant.IsEqual (expr_type, target_type))
325 // from any interface type S to interface-type T.
326 if (expr_type.IsInterface && target_type.IsInterface) {
327 return expr_type.ImplementsInterface (target_type);
330 // from any delegate type to System.Delegate
331 if (target_type == TypeManager.delegate_type &&
332 (expr_type == TypeManager.delegate_type || expr_type.IsDelegate))
335 if (TypeManager.IsEqual (expr_type, target_type))
341 public static bool ImplicitBoxingConversionExists (Expression expr, TypeSpec target_type,
342 out bool use_class_cast)
344 TypeSpec expr_type = expr.Type;
345 use_class_cast = false;
348 // From any value-type to the type object.
350 if (target_type == TypeManager.object_type || target_type == InternalType.Dynamic) {
352 // A pointer type cannot be converted to object
354 if (expr_type.IsPointer)
357 return TypeManager.IsValueType (expr_type);
361 // From any value-type to the type System.ValueType.
363 if (target_type == TypeManager.value_type)
364 return TypeManager.IsValueType (expr_type);
366 if (target_type == TypeManager.enum_type) {
368 // From any enum-type to the type System.Enum.
370 if (TypeManager.IsEnumType (expr_type))
373 // From any nullable-type with an underlying enum-type to the type System.Enum
375 if (TypeManager.IsNullableType (expr_type))
376 return TypeManager.IsEnumType (Nullable.NullableInfo.GetUnderlyingType (expr_type));
379 if (TypeManager.IsSubclassOf (expr_type, target_type)) {
381 // Don't box same type arguments
383 if (TypeManager.IsGenericParameter (expr_type) && expr_type != target_type)
389 // This code is kind of mirrored inside ImplicitStandardConversionExists
390 // with the small distinction that we only probe there
392 // Always ensure that the code here and there is in sync
394 // from any class-type S to any interface-type T.
395 if (target_type.IsInterface) {
396 if (expr_type.ImplementsInterface (target_type))
397 return TypeManager.IsGenericParameter (expr_type) ||
398 TypeManager.IsValueType (expr_type);
401 if (TypeManager.IsGenericParameter (expr_type)) {
402 return ImplicitTypeParameterConversion (expr, target_type) != null;
403 // return ImplicitTypeParameterBoxingConversion (
404 // expr_type, target_type, out use_class_cast);
410 public static Expression ImplicitNulableConversion (ResolveContext ec, Expression expr, TypeSpec target_type)
412 TypeSpec expr_type = expr.Type;
415 // From null to any nullable type
417 if (expr_type == TypeManager.null_type)
418 return ec == null ? EmptyExpression.Null : Nullable.LiftedNull.Create (target_type, expr.Location);
421 TypeSpec t_el = TypeManager.GetTypeArguments (target_type)[0];
424 if (TypeManager.IsNullableType (expr_type))
425 expr_type = TypeManager.GetTypeArguments (expr_type)[0];
428 // Predefined implicit identity or implicit numeric conversion
429 // has to exist between underlying type S and underlying type T
434 if (expr_type == t_el)
435 return EmptyExpression.Null;
437 return ImplicitNumericConversion (null, expr_type, t_el);
441 if (expr_type != expr.Type)
442 unwrap = Nullable.Unwrap.Create (expr);
446 Expression conv = expr_type == t_el ? unwrap : ImplicitNumericConversion (unwrap, expr_type, t_el);
450 if (expr_type != expr.Type)
451 return new Nullable.Lifted (conv, unwrap, target_type).Resolve (ec);
453 // Do constant optimization for S -> T?
454 if (unwrap is Constant)
455 conv = ((Constant) unwrap).ConvertImplicitly (ec, t_el);
457 return Nullable.Wrap.Create (conv, target_type);
461 /// Implicit Numeric Conversions.
463 /// expr is the expression to convert, returns a new expression of type
464 /// target_type or null if an implicit conversion is not possible.
466 public static Expression ImplicitNumericConversion (Expression expr, TypeSpec target_type)
468 return ImplicitNumericConversion (expr, expr.Type, target_type);
471 static Expression ImplicitNumericConversion (Expression expr, TypeSpec expr_type, TypeSpec target_type)
473 if (expr_type == TypeManager.sbyte_type){
475 // From sbyte to short, int, long, float, double, decimal
477 if (target_type == TypeManager.int32_type)
478 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
479 if (target_type == TypeManager.int64_type)
480 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
481 if (target_type == TypeManager.double_type)
482 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
483 if (target_type == TypeManager.float_type)
484 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
485 if (target_type == TypeManager.short_type)
486 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
487 if (target_type == TypeManager.decimal_type)
488 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
489 } else if (expr_type == TypeManager.byte_type){
491 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
493 if (target_type == TypeManager.int32_type || target_type == TypeManager.uint32_type ||
494 target_type == TypeManager.short_type || target_type == TypeManager.ushort_type)
495 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
497 if (target_type == TypeManager.uint64_type)
498 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
499 if (target_type == TypeManager.int64_type)
500 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
501 if (target_type == TypeManager.float_type)
502 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
503 if (target_type == TypeManager.double_type)
504 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
505 if (target_type == TypeManager.decimal_type)
506 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
508 } else if (expr_type == TypeManager.short_type){
510 // From short to int, long, float, double, decimal
512 if (target_type == TypeManager.int32_type)
513 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
514 if (target_type == TypeManager.int64_type)
515 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
516 if (target_type == TypeManager.double_type)
517 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
518 if (target_type == TypeManager.float_type)
519 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
520 if (target_type == TypeManager.decimal_type)
521 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
523 } else if (expr_type == TypeManager.ushort_type){
525 // From ushort to int, uint, long, ulong, float, double, decimal
527 if (target_type == TypeManager.int32_type || target_type == TypeManager.uint32_type)
528 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
530 if (target_type == TypeManager.uint64_type)
531 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
532 if (target_type == TypeManager.int64_type)
533 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
534 if (target_type == TypeManager.double_type)
535 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
536 if (target_type == TypeManager.float_type)
537 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
538 if (target_type == TypeManager.decimal_type)
539 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
540 } else if (expr_type == TypeManager.int32_type){
542 // From int to long, float, double, decimal
544 if (target_type == TypeManager.int64_type)
545 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
546 if (target_type == TypeManager.double_type)
547 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
548 if (target_type == TypeManager.float_type)
549 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
550 if (target_type == TypeManager.decimal_type)
551 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
552 } else if (expr_type == TypeManager.uint32_type){
554 // From uint to long, ulong, float, double, decimal
556 if (target_type == TypeManager.int64_type)
557 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
558 if (target_type == TypeManager.uint64_type)
559 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
560 if (target_type == TypeManager.double_type)
561 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
562 if (target_type == TypeManager.float_type)
563 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
564 if (target_type == TypeManager.decimal_type)
565 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
566 } else if (expr_type == TypeManager.int64_type){
568 // From long/ulong to float, double
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.uint64_type){
578 // From ulong to float, double
580 if (target_type == TypeManager.double_type)
581 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
582 if (target_type == TypeManager.float_type)
583 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
584 if (target_type == TypeManager.decimal_type)
585 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
586 } else if (expr_type == TypeManager.char_type){
588 // From char to ushort, int, uint, long, ulong, float, double, decimal
590 if ((target_type == TypeManager.ushort_type) ||
591 (target_type == TypeManager.int32_type) ||
592 (target_type == TypeManager.uint32_type))
593 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
594 if (target_type == TypeManager.uint64_type)
595 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
596 if (target_type == TypeManager.int64_type)
597 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
598 if (target_type == TypeManager.float_type)
599 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
600 if (target_type == TypeManager.double_type)
601 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
602 if (target_type == TypeManager.decimal_type)
603 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
604 } else if (expr_type == TypeManager.float_type){
608 if (target_type == TypeManager.double_type)
609 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
616 /// Same as ImplicitStandardConversionExists except that it also looks at
617 /// implicit user defined conversions - needed for overload resolution
619 public static bool ImplicitConversionExists (ResolveContext ec, Expression expr, TypeSpec target_type)
621 if (ImplicitStandardConversionExists (expr, target_type))
624 if (expr.Type == InternalType.AnonymousMethod) {
625 if (!TypeManager.IsDelegateType (target_type) && target_type.GetDefinition () != TypeManager.expression_type)
628 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
629 return ame.ImplicitStandardConversionExists (ec, target_type);
632 if (expr.eclass == ExprClass.MethodGroup) {
633 if (target_type.IsDelegate && RootContext.Version != LanguageVersion.ISO_1) {
634 MethodGroupExpr mg = expr as MethodGroupExpr;
636 return DelegateCreation.ImplicitStandardConversionExists (ec, mg, target_type);
642 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
646 /// Determines if a standard implicit conversion exists from
647 /// expr_type to target_type
649 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
651 public static bool ImplicitStandardConversionExists (Expression expr, TypeSpec target_type)
653 TypeSpec expr_type = expr.Type;
655 if (expr_type == TypeManager.null_type) {
656 NullLiteral nl = expr as NullLiteral;
658 return nl.ConvertImplicitly (null, target_type) != null;
661 if (expr_type == TypeManager.void_type)
664 if (TypeManager.IsEqual (expr_type, target_type))
667 if (TypeManager.IsNullableType (target_type)) {
668 return ImplicitNulableConversion (null, expr, target_type) != null;
671 // First numeric conversions
672 if (ImplicitNumericConversion (null, expr_type, target_type) != null)
675 if (ImplicitReferenceConversionExists (expr, target_type))
679 if (ImplicitBoxingConversionExists (expr, target_type, out use_class_cast))
683 // Implicit Constant Expression Conversions
685 if (expr is IntConstant){
686 int value = ((IntConstant) expr).Value;
688 if (target_type == TypeManager.sbyte_type){
689 if (value >= SByte.MinValue && value <= SByte.MaxValue)
691 } else if (target_type == TypeManager.byte_type){
692 if (value >= 0 && value <= Byte.MaxValue)
694 } else if (target_type == TypeManager.short_type){
695 if (value >= Int16.MinValue && value <= Int16.MaxValue)
697 } else if (target_type == TypeManager.ushort_type){
698 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
700 } else if (target_type == TypeManager.uint32_type){
703 } else if (target_type == TypeManager.uint64_type){
705 // we can optimize this case: a positive int32
706 // always fits on a uint64. But we need an opcode
713 if (value == 0 && target_type.IsEnum)
717 if (expr is LongConstant && target_type == TypeManager.uint64_type){
719 // Try the implicit constant expression conversion
720 // from long to ulong, instead of a nice routine,
723 long v = ((LongConstant) expr).Value;
729 // If `expr_type' implements `target_type' (which is an iface)
730 // see TryImplicitIntConversion
732 if (target_type.IsInterface && expr_type.ImplementsInterface (target_type))
735 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
738 // Conversion from __arglist to System.ArgIterator
739 if (expr_type == InternalType.Arglist)
740 return target_type == TypeManager.arg_iterator_type;
746 /// Finds "most encompassed type" according to the spec (13.4.2)
747 /// amongst the methods in the MethodGroupExpr
749 public static TypeSpec FindMostEncompassedType (IEnumerable<TypeSpec> types)
751 TypeSpec best = null;
752 EmptyExpression expr = EmptyExpression.Grab ();
754 foreach (TypeSpec t in types) {
761 if (ImplicitStandardConversionExists (expr, best))
766 foreach (TypeSpec t in types) {
769 if (!ImplicitStandardConversionExists (expr, t)) {
775 EmptyExpression.Release (expr);
781 /// Finds "most encompassing type" according to the spec (13.4.2)
782 /// amongst the types in the given set
784 static TypeSpec FindMostEncompassingType (IList<TypeSpec> types)
786 TypeSpec best = null;
788 if (types.Count == 0)
791 if (types.Count == 1)
794 EmptyExpression expr = EmptyExpression.Grab ();
796 foreach (TypeSpec t in types) {
803 if (ImplicitStandardConversionExists (expr, t))
807 foreach (TypeSpec t in types) {
811 if (!ImplicitStandardConversionExists (expr, best)) {
817 EmptyExpression.Release (expr);
823 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
824 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
825 /// for explicit and implicit conversion operators.
827 static public TypeSpec FindMostSpecificSource (IList<MethodSpec> list,
828 Expression source, bool apply_explicit_conv_rules)
830 var src_types_set = new List<TypeSpec> ();
833 // If any operator converts from S then Sx = S
835 TypeSpec source_type = source.Type;
836 foreach (var mb in list){
837 TypeSpec param_type = mb.Parameters.Types [0];
839 if (param_type == source_type)
842 src_types_set.Add (param_type);
846 // Explicit Conv rules
848 if (apply_explicit_conv_rules) {
849 var candidate_set = new List<TypeSpec> ();
851 foreach (TypeSpec param_type in src_types_set){
852 if (ImplicitStandardConversionExists (source, param_type))
853 candidate_set.Add (param_type);
856 if (candidate_set.Count != 0)
857 return FindMostEncompassedType (candidate_set);
863 if (apply_explicit_conv_rules)
864 return FindMostEncompassingType (src_types_set);
866 return FindMostEncompassedType (src_types_set);
870 /// Finds the most specific target Tx according to section 13.4.4
872 static public TypeSpec FindMostSpecificTarget (IList<MethodSpec> list,
873 TypeSpec target, bool apply_explicit_conv_rules)
875 var tgt_types_set = new List<TypeSpec> ();
878 // If any operator converts to T then Tx = T
880 foreach (var mi in list){
881 TypeSpec ret_type = mi.ReturnType;
882 if (ret_type == target)
885 tgt_types_set.Add (ret_type);
889 // Explicit conv rules
891 if (apply_explicit_conv_rules) {
892 var candidate_set = new List<TypeSpec> ();
894 EmptyExpression expr = EmptyExpression.Grab ();
896 foreach (TypeSpec ret_type in tgt_types_set){
897 expr.SetType (ret_type);
899 if (ImplicitStandardConversionExists (expr, target))
900 candidate_set.Add (ret_type);
903 EmptyExpression.Release (expr);
905 if (candidate_set.Count != 0)
906 return FindMostEncompassingType (candidate_set);
910 // Okay, final case !
912 if (apply_explicit_conv_rules)
913 return FindMostEncompassedType (tgt_types_set);
915 return FindMostEncompassingType (tgt_types_set);
919 /// User-defined Implicit conversions
921 static public Expression ImplicitUserConversion (ResolveContext ec, Expression source,
922 TypeSpec target, Location loc)
924 return UserDefinedConversion (ec, source, target, loc, false, true);
928 /// User-defined Explicit conversions
930 static Expression ExplicitUserConversion (ResolveContext ec, Expression source,
931 TypeSpec target, Location loc)
933 return UserDefinedConversion (ec, source, target, loc, true, true);
936 static void AddConversionOperators (List<MethodSpec> list,
937 Expression source, TypeSpec target_type,
938 bool look_for_explicit,
944 TypeSpec source_type = source.Type;
945 EmptyExpression expr = EmptyExpression.Grab ();
948 // LAMESPEC: Undocumented IntPtr/UIntPtr conversions
949 // IntPtr -> uint uses int
950 // UIntPtr -> long uses ulong
952 if (source_type == TypeManager.intptr_type) {
953 if (target_type == TypeManager.uint32_type)
954 target_type = TypeManager.int32_type;
955 } else if (source_type == TypeManager.uintptr_type) {
956 if (target_type == TypeManager.int64_type)
957 target_type = TypeManager.uint64_type;
960 foreach (MethodSpec m in mg.Methods) {
961 AParametersCollection pd = m.Parameters;
962 TypeSpec return_type = m.ReturnType;
963 TypeSpec arg_type = pd.Types [0];
965 if (source_type != arg_type) {
966 if (!ImplicitStandardConversionExists (source, arg_type)) {
967 if (!look_for_explicit)
969 expr.SetType (arg_type);
970 if (!ImplicitStandardConversionExists (expr, source_type))
975 if (target_type != return_type) {
976 expr.SetType (return_type);
977 if (!ImplicitStandardConversionExists (expr, target_type)) {
978 if (!look_for_explicit)
980 expr.SetType (target_type);
981 if (!ImplicitStandardConversionExists (expr, return_type))
986 // See LAMESPEC: Exclude IntPtr -> int conversion
987 if (source_type == TypeManager.uintptr_type && return_type == TypeManager.uint32_type)
993 EmptyExpression.Release (expr);
997 /// Compute the user-defined conversion operator from source_type to target_type.
998 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1000 static MethodSpec GetConversionOperator (CompilerContext ctx, TypeSpec container_type, Expression source, TypeSpec target_type, bool look_for_explicit)
1002 var ops = new List<MethodSpec> (4);
1004 TypeSpec source_type = source.Type;
1006 if (source_type != TypeManager.decimal_type) {
1007 AddConversionOperators (ops, source, target_type, look_for_explicit,
1008 Expression.MethodLookup (ctx, container_type, source_type, MemberKind.Operator, "op_Implicit", 0, Location.Null));
1009 if (look_for_explicit) {
1010 AddConversionOperators (ops, source, target_type, look_for_explicit,
1011 Expression.MethodLookup (ctx,
1012 container_type, source_type, MemberKind.Operator, "op_Explicit", 0, Location.Null));
1016 if (target_type != TypeManager.decimal_type) {
1017 AddConversionOperators (ops, source, target_type, look_for_explicit,
1018 Expression.MethodLookup (ctx, container_type, target_type, MemberKind.Operator, "op_Implicit", 0, Location.Null));
1019 if (look_for_explicit) {
1020 AddConversionOperators (ops, source, target_type, look_for_explicit,
1021 Expression.MethodLookup (ctx,
1022 container_type, target_type, MemberKind.Operator, "op_Explicit", 0, Location.Null));
1029 TypeSpec most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1030 if (most_specific_source == null)
1033 TypeSpec most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1034 if (most_specific_target == null)
1037 MethodSpec method = null;
1039 foreach (var m in ops) {
1040 if (m.ReturnType != most_specific_target)
1042 if (m.Parameters.Types [0] != most_specific_source)
1044 // Ambiguous: more than one conversion operator satisfies the signature.
1054 /// User-defined conversions
1056 public static Expression UserDefinedConversion (ResolveContext ec, Expression source,
1057 TypeSpec target, Location loc,
1058 bool look_for_explicit, bool return_convert)
1060 TypeSpec source_type = source.Type;
1061 MethodSpec method = null;
1062 Expression expr = null;
1066 if (look_for_explicit) {
1067 hash = explicit_conv;
1069 // Implicit user operators cannot convert to interfaces
1070 if (target.IsInterface)
1073 hash = implicit_conv;
1076 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1077 method = (MethodSpec) o;
1079 if (source_type == InternalType.Dynamic)
1082 method = GetConversionOperator (ec.Compiler, null, source, target, look_for_explicit);
1085 if (method != null) {
1086 TypeSpec most_specific_source = method.Parameters.Types[0];
1089 // This will do the conversion to the best match that we
1090 // found. Now we need to perform an implict standard conversion
1091 // if the best match was not the type that we were requested
1094 if (look_for_explicit) {
1095 ReportPrinter temp = new SessionReportPrinter ();
1096 ReportPrinter prev = ec.Report.SetPrinter (temp);
1098 expr = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1100 ec.Report.SetPrinter (prev);
1101 if (temp.ErrorsCount != 0)
1104 if (ImplicitStandardConversionExists (source, most_specific_source))
1105 expr = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1112 bool nullable = false;
1114 if (TypeManager.IsNullableType (source_type)) {
1115 source = Nullable.Unwrap.Create (source);
1119 TypeSpec target_underlying;
1120 if (TypeManager.IsNullableType (target)) {
1121 target_underlying = TypeManager.GetTypeArguments (target)[0];
1124 // No implicit conversion S? -> T for non-reference type T
1125 if (!look_for_explicit && !TypeManager.IsReferenceType (target))
1128 target_underlying = target;
1132 expr = UserDefinedConversion (ec, source, target_underlying, loc, look_for_explicit, return_convert);
1134 // Do result expression lifting only when it's needed
1135 if (expr != null && (!look_for_explicit || TypeManager.IsReferenceType (target)))
1136 expr = new Nullable.Lifted (expr, source, target).Resolve (ec);
1141 expr = new UserCast (method, expr, loc).Resolve (ec);
1143 if (return_convert && !TypeManager.IsEqual (expr.Type, target)) {
1144 if (look_for_explicit) {
1145 expr = ExplicitConversionStandard (ec, expr, target, loc);
1147 expr = ImplicitConversionStandard (ec, expr, target, loc);
1152 if (!(source is Constant))
1153 hash.Insert (source_type, target, method);
1159 /// Converts implicitly the resolved expression `expr' into the
1160 /// `target_type'. It returns a new expression that can be used
1161 /// in a context that expects a `target_type'.
1163 static public Expression ImplicitConversion (ResolveContext ec, Expression expr,
1164 TypeSpec target_type, Location loc)
1168 if (target_type == null)
1169 throw new Exception ("Target type is null");
1171 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1175 e = ImplicitUserConversion (ec, expr, target_type, loc);
1184 /// Attempts to apply the `Standard Implicit
1185 /// Conversion' rules to the expression `expr' into
1186 /// the `target_type'. It returns a new expression
1187 /// that can be used in a context that expects a
1190 /// This is different from `ImplicitConversion' in that the
1191 /// user defined implicit conversions are excluded.
1193 static public Expression ImplicitConversionStandard (ResolveContext ec, Expression expr,
1194 TypeSpec target_type, Location loc)
1196 return ImplicitConversionStandard (ec, expr, target_type, loc, false);
1199 static Expression ImplicitConversionStandard (ResolveContext ec, Expression expr, TypeSpec target_type, Location loc, bool explicit_cast)
1201 if (expr.eclass == ExprClass.MethodGroup){
1202 if (!TypeManager.IsDelegateType (target_type)){
1207 // Only allow anonymous method conversions on post ISO_1
1209 if (RootContext.Version != LanguageVersion.ISO_1){
1210 MethodGroupExpr mg = expr as MethodGroupExpr;
1212 return ImplicitDelegateCreation.Create (
1213 ec, mg, target_type, loc);
1217 TypeSpec expr_type = expr.Type;
1220 if (expr_type.Equals (target_type)) {
1221 if (expr_type != TypeManager.null_type && expr_type != InternalType.AnonymousMethod)
1226 if (TypeSpecComparer.Variant.IsEqual (expr_type, target_type)) {
1230 if (TypeManager.IsNullableType (target_type))
1231 return ImplicitNulableConversion (ec, expr, target_type);
1234 // Attempt to do the implicit constant expression conversions
1236 Constant c = expr as Constant;
1239 c = c.ConvertImplicitly (ec, target_type);
1241 Console.WriteLine ("Conversion error happened in line {0}", loc);
1248 e = ImplicitNumericConversion (expr, expr_type, target_type);
1252 e = ImplicitReferenceConversion (expr, target_type, explicit_cast);
1256 if (expr is IntConstant && TypeManager.IsEnumType (target_type)){
1257 Constant i = (Constant) expr;
1259 // LAMESPEC: Conversion from any 0 constant is allowed
1261 // An implicit enumeration conversion permits the decimal-integer-literal 0
1262 // to be converted to any enum-type and to any nullable-type whose underlying
1263 // type is an enum-type
1265 if (i.IsDefaultValue)
1266 return new EnumConstant (i, target_type).Resolve (ec);
1270 if (expr_type.IsPointer){
1271 if (target_type == TypeManager.void_ptr_type)
1272 return EmptyCast.Create (expr, target_type);
1275 // yep, comparing pointer types cant be done with
1276 // t1 == t2, we have to compare their element types.
1278 if (target_type.IsPointer){
1279 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1286 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1287 return EmptyCast.Create (new NullPointer (loc), target_type);
1290 if (expr_type == InternalType.AnonymousMethod){
1291 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1292 Expression am = ame.Compatible (ec, target_type);
1294 return am.Resolve (ec);
1297 if (expr_type == InternalType.Arglist && target_type == TypeManager.arg_iterator_type)
1304 /// Attempts to implicitly convert `source' into `target_type', using
1305 /// ImplicitConversion. If there is no implicit conversion, then
1306 /// an error is signaled
1308 static public Expression ImplicitConversionRequired (ResolveContext ec, Expression source,
1309 TypeSpec target_type, Location loc)
1311 Expression e = ImplicitConversion (ec, source, target_type, loc);
1315 if (source.Type == InternalType.Dynamic) {
1316 Arguments args = new Arguments (1);
1317 args.Add (new Argument (source));
1318 return new DynamicConversion (target_type, 0, args, loc).Resolve (ec);
1321 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1326 /// Performs the explicit numeric conversions
1328 /// There are a few conversions that are not part of the C# standard,
1329 /// they were interim hacks in the C# compiler that were supposed to
1330 /// become explicit operators in the UIntPtr class and IntPtr class,
1331 /// but for historical reasons it did not happen, so the C# compiler
1332 /// ended up with these special hacks.
1334 /// See bug 59800 for details.
1336 /// The conversion are:
1346 public static Expression ExplicitNumericConversion (Expression expr, TypeSpec target_type)
1348 TypeSpec expr_type = expr.Type;
1349 TypeSpec real_target_type = target_type;
1351 if (expr_type == TypeManager.sbyte_type){
1353 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1355 if (real_target_type == TypeManager.byte_type)
1356 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1357 if (real_target_type == TypeManager.ushort_type)
1358 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1359 if (real_target_type == TypeManager.uint32_type)
1360 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1361 if (real_target_type == TypeManager.uint64_type)
1362 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1363 if (real_target_type == TypeManager.char_type)
1364 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1366 // One of the built-in conversions that belonged in the class library
1367 if (real_target_type == TypeManager.uintptr_type){
1368 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1370 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1372 } else if (expr_type == TypeManager.byte_type){
1374 // From byte to sbyte and char
1376 if (real_target_type == TypeManager.sbyte_type)
1377 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1378 if (real_target_type == TypeManager.char_type)
1379 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1380 } else if (expr_type == TypeManager.short_type){
1382 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1384 if (real_target_type == TypeManager.sbyte_type)
1385 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1386 if (real_target_type == TypeManager.byte_type)
1387 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1388 if (real_target_type == TypeManager.ushort_type)
1389 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1390 if (real_target_type == TypeManager.uint32_type)
1391 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1392 if (real_target_type == TypeManager.uint64_type)
1393 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1394 if (real_target_type == TypeManager.char_type)
1395 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1397 // One of the built-in conversions that belonged in the class library
1398 if (real_target_type == TypeManager.uintptr_type){
1399 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1401 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1403 } else if (expr_type == TypeManager.ushort_type){
1405 // From ushort to sbyte, byte, short, char
1407 if (real_target_type == TypeManager.sbyte_type)
1408 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1409 if (real_target_type == TypeManager.byte_type)
1410 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1411 if (real_target_type == TypeManager.short_type)
1412 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1413 if (real_target_type == TypeManager.char_type)
1414 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1415 } else if (expr_type == TypeManager.int32_type){
1417 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1419 if (real_target_type == TypeManager.sbyte_type)
1420 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1421 if (real_target_type == TypeManager.byte_type)
1422 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1423 if (real_target_type == TypeManager.short_type)
1424 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1425 if (real_target_type == TypeManager.ushort_type)
1426 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1427 if (real_target_type == TypeManager.uint32_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1429 if (real_target_type == TypeManager.uint64_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1431 if (real_target_type == TypeManager.char_type)
1432 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1434 // One of the built-in conversions that belonged in the class library
1435 if (real_target_type == TypeManager.uintptr_type){
1436 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1438 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1440 } else if (expr_type == TypeManager.uint32_type){
1442 // From uint to sbyte, byte, short, ushort, int, char
1444 if (real_target_type == TypeManager.sbyte_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1446 if (real_target_type == TypeManager.byte_type)
1447 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1448 if (real_target_type == TypeManager.short_type)
1449 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1450 if (real_target_type == TypeManager.ushort_type)
1451 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1452 if (real_target_type == TypeManager.int32_type)
1453 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1454 if (real_target_type == TypeManager.char_type)
1455 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1456 } else if (expr_type == TypeManager.int64_type){
1458 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1460 if (real_target_type == TypeManager.sbyte_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1462 if (real_target_type == TypeManager.byte_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1464 if (real_target_type == TypeManager.short_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1466 if (real_target_type == TypeManager.ushort_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1468 if (real_target_type == TypeManager.int32_type)
1469 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1470 if (real_target_type == TypeManager.uint32_type)
1471 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1472 if (real_target_type == TypeManager.uint64_type)
1473 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1474 if (real_target_type == TypeManager.char_type)
1475 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1476 } else if (expr_type == TypeManager.uint64_type){
1478 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1480 if (real_target_type == TypeManager.sbyte_type)
1481 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1482 if (real_target_type == TypeManager.byte_type)
1483 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1484 if (real_target_type == TypeManager.short_type)
1485 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1486 if (real_target_type == TypeManager.ushort_type)
1487 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1488 if (real_target_type == TypeManager.int32_type)
1489 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1490 if (real_target_type == TypeManager.uint32_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1492 if (real_target_type == TypeManager.int64_type)
1493 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1494 if (real_target_type == TypeManager.char_type)
1495 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1497 // One of the built-in conversions that belonged in the class library
1498 if (real_target_type == TypeManager.intptr_type){
1499 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1500 TypeManager.intptr_type, true);
1502 } else if (expr_type == TypeManager.char_type){
1504 // From char to sbyte, byte, short
1506 if (real_target_type == TypeManager.sbyte_type)
1507 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1508 if (real_target_type == TypeManager.byte_type)
1509 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1510 if (real_target_type == TypeManager.short_type)
1511 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1512 } else if (expr_type == TypeManager.float_type){
1514 // From float to sbyte, byte, short,
1515 // ushort, int, uint, long, ulong, char
1518 if (real_target_type == TypeManager.sbyte_type)
1519 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1520 if (real_target_type == TypeManager.byte_type)
1521 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1522 if (real_target_type == TypeManager.short_type)
1523 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1524 if (real_target_type == TypeManager.ushort_type)
1525 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1526 if (real_target_type == TypeManager.int32_type)
1527 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1528 if (real_target_type == TypeManager.uint32_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1530 if (real_target_type == TypeManager.int64_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1532 if (real_target_type == TypeManager.uint64_type)
1533 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1534 if (real_target_type == TypeManager.char_type)
1535 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1536 if (real_target_type == TypeManager.decimal_type)
1537 return new CastToDecimal (expr, true);
1538 } else if (expr_type == TypeManager.double_type){
1540 // From double to sbyte, byte, short,
1541 // ushort, int, uint, long, ulong,
1542 // char, float or decimal
1544 if (real_target_type == TypeManager.sbyte_type)
1545 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1546 if (real_target_type == TypeManager.byte_type)
1547 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1548 if (real_target_type == TypeManager.short_type)
1549 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1550 if (real_target_type == TypeManager.ushort_type)
1551 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1552 if (real_target_type == TypeManager.int32_type)
1553 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1554 if (real_target_type == TypeManager.uint32_type)
1555 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1556 if (real_target_type == TypeManager.int64_type)
1557 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1558 if (real_target_type == TypeManager.uint64_type)
1559 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1560 if (real_target_type == TypeManager.char_type)
1561 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1562 if (real_target_type == TypeManager.float_type)
1563 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1564 if (real_target_type == TypeManager.decimal_type)
1565 return new CastToDecimal (expr, true);
1566 } else if (expr_type == TypeManager.uintptr_type){
1568 // Various built-in conversions that belonged in the class library
1570 // from uintptr to sbyte, short, int32
1572 if (real_target_type == TypeManager.sbyte_type){
1573 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1574 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1576 if (real_target_type == TypeManager.short_type){
1577 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1578 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1580 if (real_target_type == TypeManager.int32_type){
1581 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1582 TypeManager.int32_type);
1584 } else if (expr_type == TypeManager.intptr_type){
1585 if (real_target_type == TypeManager.uint64_type){
1586 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1587 TypeManager.uint64_type);
1589 } else if (expr_type == TypeManager.decimal_type) {
1590 return new CastFromDecimal (expr, target_type).Resolve ();
1596 /// Returns whether an explicit reference conversion can be performed
1597 /// from source_type to target_type
1599 public static bool ExplicitReferenceConversionExists (TypeSpec source_type, TypeSpec target_type)
1601 Expression e = ExplicitReferenceConversion (null, source_type, target_type);
1605 if (e == EmptyExpression.Null)
1608 throw new InternalErrorException ("Invalid probing conversion result");
1612 /// Implements Explicit Reference conversions
1614 static Expression ExplicitReferenceConversion (Expression source, TypeSpec source_type, TypeSpec target_type)
1616 bool target_is_value_type = TypeManager.IsStruct (target_type);
1619 // From object to a generic parameter
1621 if (source_type == TypeManager.object_type && TypeManager.IsGenericParameter (target_type))
1622 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1625 // Explicit type parameter conversion.
1627 if (TypeManager.IsGenericParameter (source_type))
1628 return ExplicitTypeParameterConversion (source, source_type, target_type);
1631 // From object to any reference type or value type (unboxing)
1633 if (source_type == TypeManager.object_type)
1634 return source == null ? EmptyExpression.Null :
1635 target_is_value_type ? (Expression) new UnboxCast (source, target_type) : new ClassCast (source, target_type);
1638 // Unboxing conversion from the types object and System.ValueType to any non-nullable-value-type
1640 if (source_type == TypeManager.value_type && target_is_value_type)
1641 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1644 // From any class S to any class-type T, provided S is a base class of T
1646 if (TypeManager.IsSubclassOf (target_type, source_type))
1647 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1650 // From any interface-type S to to any class type T, provided T is not
1651 // sealed, or provided T implements S.
1653 if (source_type.IsInterface) {
1654 if (!target_type.IsSealed || target_type.ImplementsInterface (source_type)) {
1655 if (target_type.IsClass)
1656 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1659 // Unboxing conversion from any interface-type to any non-nullable-value-type that
1660 // implements the interface-type
1662 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1666 // From System.Collections.Generic.IList<T> and its base interfaces to a one-dimensional
1667 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1669 var target_array = target_type as ArrayContainer;
1670 if (target_array != null && IList_To_Array (source_type, target_array))
1671 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1676 var source_array = source_type as ArrayContainer;
1677 if (source_array != null) {
1678 var target_array = target_type as ArrayContainer;
1679 if (target_array != null) {
1681 // From System.Array to any array-type
1683 if (source_type == TypeManager.array_type)
1684 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1687 // From an array type S with an element type Se to an array type T with an
1688 // element type Te provided all the following are true:
1689 // * S and T differe only in element type, in other words, S and T
1690 // have the same number of dimensions.
1691 // * Both Se and Te are reference types
1692 // * An explicit reference conversions exist from Se to Te
1694 if (source_array.Rank == target_array.Rank) {
1696 source_type = source_array.Element;
1697 if (!TypeManager.IsReferenceType (source_type))
1700 var target_element = target_array.Element;
1701 if (!TypeManager.IsReferenceType (target_element))
1704 if (ExplicitReferenceConversionExists (source_type, target_element))
1705 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1712 // From a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces,
1713 // provided that there is an explicit reference conversion from S to T
1715 if (ArrayToIList (source_array, target_type, true))
1716 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1722 // From any class type S to any interface T, provides S is not sealed
1723 // and provided S does not implement T.
1725 if (target_type.IsInterface && !source_type.IsSealed && !source_type.ImplementsInterface (target_type)) {
1726 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1730 // From System delegate to any delegate-type
1732 if (source_type == TypeManager.delegate_type && TypeManager.IsDelegateType (target_type))
1733 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1739 /// Performs an explicit conversion of the expression `expr' whose
1740 /// type is expr.Type to `target_type'.
1742 static public Expression ExplicitConversionCore (ResolveContext ec, Expression expr,
1743 TypeSpec target_type, Location loc)
1745 TypeSpec expr_type = expr.Type;
1747 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1748 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc, true);
1752 if (TypeManager.IsEnumType (expr_type)) {
1753 Expression underlying = EmptyCast.Create (expr, EnumSpec.GetUnderlyingType (expr_type));
1754 expr = ExplicitConversionCore (ec, underlying, target_type, loc);
1758 return ExplicitUserConversion (ec, underlying, target_type, loc);
1761 if (TypeManager.IsEnumType (target_type)){
1763 // Type System.Enum can be unboxed to any enum-type
1765 if (expr_type == TypeManager.enum_type)
1766 return new UnboxCast (expr, target_type);
1768 Expression ce = ExplicitConversionCore (ec, expr, EnumSpec.GetUnderlyingType (target_type), loc);
1770 return EmptyCast.Create (ce, target_type);
1773 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1775 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1776 ne = ExplicitUserConversion (ec, expr, EnumSpec.GetUnderlyingType (target_type), loc);
1778 return ExplicitConversionCore (ec, ne, target_type, loc);
1784 ne = ExplicitNumericConversion (expr, target_type);
1789 // Skip the ExplicitReferenceConversion because we can not convert
1790 // from Null to a ValueType, and ExplicitReference wont check against
1791 // null literal explicitly
1793 if (expr_type != TypeManager.null_type){
1794 ne = ExplicitReferenceConversion (expr, expr_type, target_type);
1800 ne = ExplicitUnsafe (expr, target_type);
1808 public static Expression ExplicitUnsafe (Expression expr, TypeSpec target_type)
1810 TypeSpec expr_type = expr.Type;
1812 if (target_type.IsPointer){
1813 if (expr_type.IsPointer)
1814 return EmptyCast.Create (expr, target_type);
1816 if (expr_type == TypeManager.sbyte_type ||
1817 expr_type == TypeManager.short_type ||
1818 expr_type == TypeManager.int32_type)
1819 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1821 if (expr_type == TypeManager.ushort_type ||
1822 expr_type == TypeManager.uint32_type ||
1823 expr_type == TypeManager.byte_type)
1824 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1826 if (expr_type == TypeManager.int64_type)
1827 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I);
1829 if (expr_type == TypeManager.uint64_type)
1830 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I);
1833 if (expr_type.IsPointer){
1834 if (target_type == TypeManager.sbyte_type)
1835 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1836 if (target_type == TypeManager.byte_type)
1837 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1838 if (target_type == TypeManager.short_type)
1839 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1840 if (target_type == TypeManager.ushort_type)
1841 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1842 if (target_type == TypeManager.int32_type)
1843 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1844 if (target_type == TypeManager.uint32_type)
1845 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1846 if (target_type == TypeManager.int64_type)
1847 return new ConvCast (expr, target_type, ConvCast.Mode.I_I8);
1848 if (target_type == TypeManager.uint64_type)
1849 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1855 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1857 static public Expression ExplicitConversionStandard (ResolveContext ec, Expression expr,
1858 TypeSpec target_type, Location l)
1860 int errors = ec.Report.Errors;
1861 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1862 if (ec.Report.Errors > errors)
1868 ne = ExplicitNumericConversion (expr, target_type);
1872 ne = ExplicitReferenceConversion (expr, expr.Type, target_type);
1876 if (ec.IsUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1877 return EmptyCast.Create (expr, target_type);
1879 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
1884 /// Performs an explicit conversion of the expression `expr' whose
1885 /// type is expr.Type to `target_type'.
1887 static public Expression ExplicitConversion (ResolveContext ec, Expression expr,
1888 TypeSpec target_type, Location loc)
1890 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1893 // Don't eliminate explicit precission casts
1896 if (target_type == TypeManager.float_type)
1897 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1899 if (target_type == TypeManager.double_type)
1900 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
1906 TypeSpec expr_type = expr.Type;
1907 if (TypeManager.IsNullableType (target_type)) {
1908 if (TypeManager.IsNullableType (expr_type)) {
1909 TypeSpec target = TypeManager.GetTypeArguments (target_type)[0];
1910 Expression unwrap = Nullable.Unwrap.Create (expr);
1911 e = ExplicitConversion (ec, unwrap, target, expr.Location);
1915 return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
1916 } else if (expr_type == TypeManager.object_type) {
1917 return new UnboxCast (expr, target_type);
1919 TypeSpec target = TypeManager.GetTypeArguments (target_type) [0];
1921 e = ExplicitConversionCore (ec, expr, target, loc);
1923 return Nullable.Wrap.Create (e, target_type);
1925 } else if (TypeManager.IsNullableType (expr_type)) {
1926 e = Nullable.Unwrap.Create (expr, false);
1928 bool use_class_cast;
1929 if (ImplicitBoxingConversionExists (e, target_type, out use_class_cast))
1930 return new BoxedCast (expr, target_type);
1932 e = ExplicitConversionCore (ec, e, target_type, loc);
1934 return EmptyCast.Create (e, target_type);
1937 e = ExplicitUserConversion (ec, expr, target_type, loc);
1941 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);