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;
96 // From T to a type parameter U
98 var ttype = target_type as TypeParameterSpec;
100 if (expr_type.IsReferenceType && !ttype.IsReferenceType)
101 return new BoxedCast (expr, target_type);
103 return new ClassCast (expr, target_type);
107 // From T to its effective base class C
108 // From T to any base class of C
109 // From T to any interface implemented by C
111 var base_type = expr_type.GetEffectiveBase ();
112 if (base_type == target_type || TypeManager.IsSubclassOf (base_type, target_type) || base_type.ImplementsInterface (target_type)) {
113 if (expr_type.IsReferenceType)
114 return new ClassCast (expr, target_type);
116 return new BoxedCast (expr, target_type);
119 if (target_type.IsInterface && expr_type.IsConvertibleToInterface (target_type)) {
120 if (expr_type.IsReferenceType)
121 return new ClassCast (expr, target_type);
123 return new BoxedCast (expr, target_type);
129 static Expression ExplicitTypeParameterConversion (Expression source, TypeSpec source_type, TypeSpec target_type)
131 var target_tp = target_type as TypeParameterSpec;
132 if (target_tp != null) {
133 if (target_tp.Interfaces != null) {
134 foreach (TypeSpec iface in target_tp.Interfaces) {
135 if (!TypeManager.IsGenericParameter (iface))
138 if (TypeManager.IsSubclassOf (source_type, iface))
139 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
146 if (target_type.IsInterface)
147 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
152 static Expression ImplicitReferenceConversion (Expression expr, TypeSpec target_type, bool explicit_cast)
154 TypeSpec expr_type = expr.Type;
156 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
157 // if we are a method group, emit a warning
162 if (expr_type == TypeManager.void_type)
165 if (expr_type.Kind == MemberKind.TypeParameter)
166 return ImplicitTypeParameterConversion (expr, target_type);
169 // from the null type to any reference-type.
171 NullLiteral nl = expr as NullLiteral;
173 return nl.ConvertImplicitly (null, target_type);
176 if (ImplicitReferenceConversionExists (expr, target_type)) {
178 // Avoid wrapping implicitly convertible reference type
183 return EmptyCast.Create (expr, target_type);
186 return ImplicitBoxingConversion (expr, expr_type, target_type);
190 // 6.1.6 Implicit reference conversions
192 public static bool ImplicitReferenceConversionExists (Expression expr, TypeSpec target_type)
194 if (TypeManager.IsStruct (target_type))
197 TypeSpec expr_type = expr.Type;
199 // from the null type to any reference-type.
200 if (expr_type == TypeManager.null_type)
201 return target_type != InternalType.AnonymousMethod;
203 if (TypeManager.IsGenericParameter (expr_type))
204 return ImplicitTypeParameterConversion (expr, target_type) != null;
206 // This code is kind of mirrored inside ImplicitStandardConversionExists
207 // with the small distinction that we only probe there
209 // Always ensure that the code here and there is in sync
211 // from any class-type S to any interface-type T.
212 if (target_type.IsInterface) {
213 if (expr_type.ImplementsInterface (target_type)){
214 return !TypeManager.IsValueType (expr_type);
219 // notice that it is possible to write "ValueType v = 1", the ValueType here
220 // is an abstract class, and not really a value type, so we apply the same rules.
222 if (target_type == TypeManager.object_type || target_type == InternalType.Dynamic) {
224 // A pointer type cannot be converted to object
226 if (expr_type.IsPointer)
229 if (TypeManager.IsValueType (expr_type))
232 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type || expr_type.IsDelegate) {
233 // No mcs internal types are convertible
234 return true; // expr_type.MetaInfo.Module != typeof (Convert).Module;
237 // From anything to dynamic
238 if (target_type == InternalType.Dynamic)
241 // From dynamic to object
242 if (expr_type == InternalType.Dynamic)
246 } else if (target_type == TypeManager.value_type) {
247 return expr_type == TypeManager.enum_type;
248 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
250 // Special case: enumeration to System.Enum.
251 // System.Enum is not a value type, it is a class, so we need
252 // a boxing conversion
254 if (target_type == TypeManager.enum_type || TypeManager.IsGenericParameter (expr_type))
257 if (TypeManager.IsValueType (expr_type))
260 // Array type variance conversion
261 //if (target_type.IsArray != expr_type.IsArray)
267 var expr_type_array = expr_type as ArrayContainer;
268 if (expr_type_array != null) {
269 var target_type_array = target_type as ArrayContainer;
270 // from an array-type S to an array-type of type T
271 if (target_type_array != null && expr_type_array.Rank == target_type_array.Rank) {
274 // Both SE and TE are reference-types
276 TypeSpec expr_element_type = expr_type_array.Element;
277 if (!TypeManager.IsReferenceType (expr_element_type))
280 TypeSpec target_element_type = target_type_array.Element;
281 if (!TypeManager.IsReferenceType (target_element_type))
284 if (MyEmptyExpr == null)
285 MyEmptyExpr = new EmptyExpression (expr_element_type);
287 MyEmptyExpr.SetType (expr_element_type);
289 return ImplicitStandardConversionExists (MyEmptyExpr, target_element_type);
292 // from an array-type to System.Array
293 if (target_type == TypeManager.array_type)
296 // from an array-type of type T to IList<T>
297 if (ArrayToIList (expr_type_array, target_type, false))
303 if (TypeSpecComparer.Variant.IsEqual (expr_type, target_type))
306 // from any interface type S to interface-type T.
307 if (expr_type.IsInterface && target_type.IsInterface) {
308 return expr_type.ImplementsInterface (target_type);
311 // from any delegate type to System.Delegate
312 if (target_type == TypeManager.delegate_type &&
313 (expr_type == TypeManager.delegate_type || expr_type.IsDelegate))
316 if (TypeManager.IsEqual (expr_type, target_type))
322 public static Expression ImplicitBoxingConversion (Expression expr, TypeSpec expr_type, TypeSpec target_type)
325 // From any value-type to the type object.
327 if (target_type == TypeManager.object_type || target_type == InternalType.Dynamic) {
329 // A pointer type cannot be converted to object
331 if (expr_type.IsPointer)
334 if (!TypeManager.IsValueType (expr_type))
337 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
341 // From any value-type to the type System.ValueType.
343 if (target_type == TypeManager.value_type) {
344 if (!TypeManager.IsValueType (expr_type))
347 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
350 if (target_type == TypeManager.enum_type) {
352 // From any enum-type to the type System.Enum.
354 if (TypeManager.IsEnumType (expr_type))
355 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
359 // From a nullable-type to a reference type, if a boxing conversion exists from
360 // the underlying type to the reference type
362 if (TypeManager.IsNullableType (expr_type)) {
363 if (!TypeManager.IsReferenceType (target_type))
366 var res = ImplicitBoxingConversion (expr, Nullable.NullableInfo.GetUnderlyingType (expr_type), target_type);
368 // "cast" underlying type to target type to emit correct InvalidCastException when
369 // underlying hierarchy changes without recompilation
370 if (res != null && expr != null)
371 res = new UnboxCast (res, target_type);
376 if (TypeManager.IsSubclassOf (expr_type, target_type)) {
378 // Don't box same type arguments
380 if (TypeManager.IsGenericParameter (expr_type) && expr_type != target_type)
381 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
386 // This code is kind of mirrored inside ImplicitStandardConversionExists
387 // with the small distinction that we only probe there
389 // Always ensure that the code here and there is in sync
391 // from any class-type S to any interface-type T.
392 if (target_type.IsInterface) {
393 if (expr_type.ImplementsInterface (target_type) &&
394 (TypeManager.IsGenericParameter (expr_type) || TypeManager.IsValueType (expr_type))) {
395 return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
402 public static Expression ImplicitNulableConversion (ResolveContext ec, Expression expr, TypeSpec target_type)
404 TypeSpec expr_type = expr.Type;
407 // From null to any nullable type
409 if (expr_type == TypeManager.null_type)
410 return ec == null ? EmptyExpression.Null : Nullable.LiftedNull.Create (target_type, expr.Location);
413 TypeSpec t_el = Nullable.NullableInfo.GetUnderlyingType (target_type);
416 if (TypeManager.IsNullableType (expr_type))
417 expr_type = Nullable.NullableInfo.GetUnderlyingType (expr_type);
420 // Predefined implicit identity or implicit numeric conversion
421 // has to exist between underlying type S and underlying type T
424 // conversion exists only mode
426 if (expr_type == t_el)
427 return EmptyExpression.Null;
429 if (expr is Constant)
430 return ((Constant) expr).ConvertImplicitly (ec, t_el);
432 return ImplicitNumericConversion (null, expr_type, t_el);
436 if (expr_type != expr.Type)
437 unwrap = Nullable.Unwrap.Create (expr);
441 Expression conv = unwrap;
442 if (expr_type != t_el) {
443 if (conv is Constant)
444 conv = ((Constant)conv).ConvertImplicitly (ec, t_el);
446 conv = ImplicitNumericConversion (conv, expr_type, t_el);
452 if (expr_type != expr.Type)
453 return new Nullable.Lifted (conv, unwrap, target_type).Resolve (ec);
455 return Nullable.Wrap.Create (conv, target_type);
459 /// Implicit Numeric Conversions.
461 /// expr is the expression to convert, returns a new expression of type
462 /// target_type or null if an implicit conversion is not possible.
464 public static Expression ImplicitNumericConversion (Expression expr, TypeSpec target_type)
466 return ImplicitNumericConversion (expr, expr.Type, target_type);
469 static Expression ImplicitNumericConversion (Expression expr, TypeSpec expr_type, TypeSpec target_type)
471 if (expr_type == TypeManager.sbyte_type){
473 // From sbyte to short, int, long, float, double, decimal
475 if (target_type == TypeManager.int32_type)
476 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
477 if (target_type == TypeManager.int64_type)
478 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
479 if (target_type == TypeManager.double_type)
480 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
481 if (target_type == TypeManager.float_type)
482 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
483 if (target_type == TypeManager.short_type)
484 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
485 if (target_type == TypeManager.decimal_type)
486 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
487 } else if (expr_type == TypeManager.byte_type){
489 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
491 if (target_type == TypeManager.int32_type || target_type == TypeManager.uint32_type ||
492 target_type == TypeManager.short_type || target_type == TypeManager.ushort_type)
493 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
495 if (target_type == TypeManager.uint64_type)
496 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
497 if (target_type == TypeManager.int64_type)
498 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
499 if (target_type == TypeManager.float_type)
500 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
501 if (target_type == TypeManager.double_type)
502 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
503 if (target_type == TypeManager.decimal_type)
504 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
506 } else if (expr_type == TypeManager.short_type){
508 // From short to int, long, float, double, decimal
510 if (target_type == TypeManager.int32_type)
511 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
512 if (target_type == TypeManager.int64_type)
513 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
514 if (target_type == TypeManager.double_type)
515 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
516 if (target_type == TypeManager.float_type)
517 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
518 if (target_type == TypeManager.decimal_type)
519 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
521 } else if (expr_type == TypeManager.ushort_type){
523 // From ushort to int, uint, long, ulong, float, double, decimal
525 if (target_type == TypeManager.int32_type || target_type == TypeManager.uint32_type)
526 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
528 if (target_type == TypeManager.uint64_type)
529 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
530 if (target_type == TypeManager.int64_type)
531 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
532 if (target_type == TypeManager.double_type)
533 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
534 if (target_type == TypeManager.float_type)
535 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
536 if (target_type == TypeManager.decimal_type)
537 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
538 } else if (expr_type == TypeManager.int32_type){
540 // From int to long, float, double, decimal
542 if (target_type == TypeManager.int64_type)
543 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
544 if (target_type == TypeManager.double_type)
545 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
546 if (target_type == TypeManager.float_type)
547 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
548 if (target_type == TypeManager.decimal_type)
549 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
550 } else if (expr_type == TypeManager.uint32_type){
552 // From uint to long, ulong, float, double, decimal
554 if (target_type == TypeManager.int64_type)
555 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
556 if (target_type == TypeManager.uint64_type)
557 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
558 if (target_type == TypeManager.double_type)
559 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
560 if (target_type == TypeManager.float_type)
561 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), 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.int64_type){
566 // From long/ulong to float, double
568 if (target_type == TypeManager.double_type)
569 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
570 if (target_type == TypeManager.float_type)
571 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
572 if (target_type == TypeManager.decimal_type)
573 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
574 } else if (expr_type == TypeManager.uint64_type){
576 // From ulong to float, double
578 if (target_type == TypeManager.double_type)
579 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
580 if (target_type == TypeManager.float_type)
581 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
582 if (target_type == TypeManager.decimal_type)
583 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
584 } else if (expr_type == TypeManager.char_type){
586 // From char to ushort, int, uint, long, ulong, float, double, decimal
588 if ((target_type == TypeManager.ushort_type) ||
589 (target_type == TypeManager.int32_type) ||
590 (target_type == TypeManager.uint32_type))
591 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
592 if (target_type == TypeManager.uint64_type)
593 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
594 if (target_type == TypeManager.int64_type)
595 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
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.double_type)
599 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
600 if (target_type == TypeManager.decimal_type)
601 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
602 } else if (expr_type == TypeManager.float_type){
606 if (target_type == TypeManager.double_type)
607 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
614 /// Same as ImplicitStandardConversionExists except that it also looks at
615 /// implicit user defined conversions - needed for overload resolution
617 public static bool ImplicitConversionExists (ResolveContext ec, Expression expr, TypeSpec target_type)
619 if (ImplicitStandardConversionExists (expr, target_type))
622 if (expr.Type == InternalType.AnonymousMethod) {
623 if (!TypeManager.IsDelegateType (target_type) && target_type.GetDefinition () != TypeManager.expression_type)
626 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
627 return ame.ImplicitStandardConversionExists (ec, target_type);
630 if (expr.eclass == ExprClass.MethodGroup) {
631 if (target_type.IsDelegate && RootContext.Version != LanguageVersion.ISO_1) {
632 MethodGroupExpr mg = expr as MethodGroupExpr;
634 return DelegateCreation.ImplicitStandardConversionExists (ec, mg, target_type);
640 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
644 /// Determines if a standard implicit conversion exists from
645 /// expr_type to target_type
647 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
649 public static bool ImplicitStandardConversionExists (Expression expr, TypeSpec target_type)
651 TypeSpec expr_type = expr.Type;
653 if (expr_type == TypeManager.null_type) {
654 NullLiteral nl = expr as NullLiteral;
656 return nl.ConvertImplicitly (null, target_type) != null;
659 if (expr_type == TypeManager.void_type)
662 if (TypeManager.IsEqual (expr_type, target_type))
665 if (TypeManager.IsNullableType (target_type)) {
666 return ImplicitNulableConversion (null, expr, target_type) != null;
669 // First numeric conversions
670 if (ImplicitNumericConversion (null, expr_type, target_type) != null)
673 if (ImplicitReferenceConversionExists (expr, target_type))
676 if (ImplicitBoxingConversion (null, expr_type, target_type) != null)
680 // Implicit Constant Expression Conversions
682 if (expr is IntConstant){
683 int value = ((IntConstant) expr).Value;
685 if (target_type == TypeManager.sbyte_type){
686 if (value >= SByte.MinValue && value <= SByte.MaxValue)
688 } else if (target_type == TypeManager.byte_type){
689 if (value >= 0 && value <= Byte.MaxValue)
691 } else if (target_type == TypeManager.short_type){
692 if (value >= Int16.MinValue && value <= Int16.MaxValue)
694 } else if (target_type == TypeManager.ushort_type){
695 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
697 } else if (target_type == TypeManager.uint32_type){
700 } else if (target_type == TypeManager.uint64_type){
702 // we can optimize this case: a positive int32
703 // always fits on a uint64. But we need an opcode
710 if (value == 0 && target_type.IsEnum)
714 if (expr is LongConstant && target_type == TypeManager.uint64_type){
716 // Try the implicit constant expression conversion
717 // from long to ulong, instead of a nice routine,
720 long v = ((LongConstant) expr).Value;
726 // If `expr_type' implements `target_type' (which is an iface)
727 // see TryImplicitIntConversion
729 if (target_type.IsInterface && expr_type.ImplementsInterface (target_type))
732 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
735 // Conversion from __arglist to System.ArgIterator
736 if (expr_type == InternalType.Arglist)
737 return target_type == TypeManager.arg_iterator_type;
743 /// Finds "most encompassed type" according to the spec (13.4.2)
744 /// amongst the methods in the MethodGroupExpr
746 public static TypeSpec FindMostEncompassedType (IEnumerable<TypeSpec> types)
748 TypeSpec best = null;
749 EmptyExpression expr = EmptyExpression.Grab ();
751 foreach (TypeSpec t in types) {
758 if (ImplicitStandardConversionExists (expr, best))
763 foreach (TypeSpec t in types) {
766 if (!ImplicitStandardConversionExists (expr, t)) {
772 EmptyExpression.Release (expr);
778 /// Finds "most encompassing type" according to the spec (13.4.2)
779 /// amongst the types in the given set
781 static TypeSpec FindMostEncompassingType (IList<TypeSpec> types)
783 TypeSpec best = null;
785 if (types.Count == 0)
788 if (types.Count == 1)
791 EmptyExpression expr = EmptyExpression.Grab ();
793 foreach (TypeSpec t in types) {
800 if (ImplicitStandardConversionExists (expr, t))
804 foreach (TypeSpec t in types) {
808 if (!ImplicitStandardConversionExists (expr, best)) {
814 EmptyExpression.Release (expr);
820 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
821 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
822 /// for explicit and implicit conversion operators.
824 static TypeSpec FindMostSpecificSource (IList<MethodSpec> list,
825 Expression source, bool apply_explicit_conv_rules)
827 var src_types_set = new List<TypeSpec> ();
830 // If any operator converts from S then Sx = S
832 TypeSpec source_type = source.Type;
833 foreach (var mb in list){
834 TypeSpec param_type = mb.Parameters.Types [0];
836 if (param_type == source_type)
839 src_types_set.Add (param_type);
843 // Explicit Conv rules
845 if (apply_explicit_conv_rules) {
846 var candidate_set = new List<TypeSpec> ();
848 foreach (TypeSpec param_type in src_types_set){
849 if (ImplicitStandardConversionExists (source, param_type))
850 candidate_set.Add (param_type);
853 if (candidate_set.Count != 0)
854 return FindMostEncompassedType (candidate_set);
860 if (apply_explicit_conv_rules)
861 return FindMostEncompassingType (src_types_set);
863 return FindMostEncompassedType (src_types_set);
867 /// Finds the most specific target Tx according to section 13.4.4
869 static public TypeSpec FindMostSpecificTarget (IList<MethodSpec> list,
870 TypeSpec target, bool apply_explicit_conv_rules)
872 var tgt_types_set = new List<TypeSpec> ();
875 // If any operator converts to T then Tx = T
877 foreach (var mi in list){
878 TypeSpec ret_type = mi.ReturnType;
879 if (ret_type == target)
882 tgt_types_set.Add (ret_type);
886 // Explicit conv rules
888 if (apply_explicit_conv_rules) {
889 var candidate_set = new List<TypeSpec> ();
891 EmptyExpression expr = EmptyExpression.Grab ();
893 foreach (TypeSpec ret_type in tgt_types_set){
894 expr.SetType (ret_type);
896 if (ImplicitStandardConversionExists (expr, target))
897 candidate_set.Add (ret_type);
900 EmptyExpression.Release (expr);
902 if (candidate_set.Count != 0)
903 return FindMostEncompassingType (candidate_set);
907 // Okay, final case !
909 if (apply_explicit_conv_rules)
910 return FindMostEncompassedType (tgt_types_set);
912 return FindMostEncompassingType (tgt_types_set);
916 /// User-defined Implicit conversions
918 static public Expression ImplicitUserConversion (ResolveContext ec, Expression source, TypeSpec target, Location loc)
920 return UserDefinedConversion (ec, source, target, true, loc);
924 /// User-defined Explicit conversions
926 static Expression ExplicitUserConversion (ResolveContext ec, Expression source, TypeSpec target, Location loc)
928 return UserDefinedConversion (ec, source, target, false, loc);
931 static void FindApplicableUserDefinedConversionOperators (MethodSpec[] operators, Expression source, TypeSpec target, bool implicitOnly, ref List<MethodSpec> candidates)
934 // LAMESPEC: Undocumented IntPtr/UIntPtr conversions
935 // IntPtr -> uint uses int
936 // UIntPtr -> long uses ulong
938 if (source.Type == TypeManager.intptr_type) {
939 if (target == TypeManager.uint32_type)
940 target = TypeManager.int32_type;
941 } else if (source.Type == TypeManager.uintptr_type) {
942 if (target == TypeManager.int64_type)
943 target = TypeManager.uint64_type;
946 // For a conversion operator to be applicable, it must be possible
947 // to perform a standard conversion from the source type to
948 // the operand type of the operator, and it must be possible
949 // to perform a standard conversion from the result type of
950 // the operator to the target type.
952 Expression texpr = null;
954 foreach (var op in operators) {
955 var t = op.Parameters.Types[0];
956 if (source.Type != t && !ImplicitStandardConversionExists (source, t)) {
960 if (!ImplicitStandardConversionExists (new EmptyExpression (t), source.Type))
966 // LAMESPEC: Exclude UIntPtr -> int conversion
967 if (t == TypeManager.uint32_type && source.Type == TypeManager.uintptr_type)
970 if (target != t && !ImplicitStandardConversionExists (new EmptyExpression (t), target)) {
975 texpr = new EmptyExpression (target);
977 if (!ImplicitStandardConversionExists (texpr, t))
981 if (candidates == null)
982 candidates = new List<MethodSpec> ();
989 // User-defined conversions
991 static Expression UserDefinedConversion (ResolveContext ec, Expression source, TypeSpec target, bool implicitOnly, Location loc)
993 List<MethodSpec> candidates = null;
996 // If S or T are nullable types, S0 and T0 are their underlying types
997 // otherwise S0 and T0 are equal to S and T respectively.
999 TypeSpec source_type = source.Type;
1000 TypeSpec target_type = target;
1003 if (TypeManager.IsNullableType (source_type)) {
1004 // No implicit conversion S? -> T for non-reference types
1005 if (implicitOnly && !TypeManager.IsReferenceType (target_type) && !TypeManager.IsNullableType (target_type))
1008 unwrap = source = Nullable.Unwrap.Create (source);
1009 source_type = source.Type;
1014 if (TypeManager.IsNullableType (target_type))
1015 target_type = Nullable.NullableInfo.GetUnderlyingType (target_type);
1017 // Only these containers can contain a user defined implicit or explicit operators
1018 const MemberKind user_conversion_kinds = MemberKind.Class | MemberKind.Struct | MemberKind.TypeParameter;
1020 if ((source_type.Kind & user_conversion_kinds) != 0 && source_type != TypeManager.decimal_type) {
1021 bool declared_only = source_type.IsStruct;
1023 var operators = MemberCache.GetUserOperator (source_type, Operator.OpType.Implicit, declared_only);
1024 if (operators != null) {
1025 FindApplicableUserDefinedConversionOperators (operators, source, target_type, implicitOnly, ref candidates);
1028 if (!implicitOnly) {
1029 operators = MemberCache.GetUserOperator (source_type, Operator.OpType.Explicit, declared_only);
1030 if (operators != null) {
1031 FindApplicableUserDefinedConversionOperators (operators, source, target_type, false, ref candidates);
1036 if ((target.Kind & user_conversion_kinds) != 0 && target != TypeManager.decimal_type) {
1037 bool declared_only = target.IsStruct || implicitOnly;
1039 var operators = MemberCache.GetUserOperator (target_type, Operator.OpType.Implicit, declared_only);
1040 if (operators != null) {
1041 FindApplicableUserDefinedConversionOperators (operators, source, target_type, implicitOnly, ref candidates);
1044 if (!implicitOnly) {
1045 operators = MemberCache.GetUserOperator (target_type, Operator.OpType.Explicit, declared_only);
1046 if (operators != null) {
1047 FindApplicableUserDefinedConversionOperators (operators, source, target_type, false, ref candidates);
1052 if (candidates == null)
1056 // Find the most specific conversion operator
1058 MethodSpec most_specific_operator;
1060 if (candidates.Count == 1) {
1061 most_specific_operator = candidates[0];
1062 s_x = most_specific_operator.Parameters.Types[0];
1063 t_x = most_specific_operator.ReturnType;
1065 s_x = FindMostSpecificSource (candidates, source, !implicitOnly);
1069 t_x = FindMostSpecificTarget (candidates, target, !implicitOnly);
1073 most_specific_operator = candidates[0];
1075 for (int i = 1; i < candidates.Count; ++i) {
1076 if (candidates[i].ReturnType == t_x && candidates[i].Parameters.Types[0] == s_x) {
1077 most_specific_operator = candidates[i];
1084 // Convert input type when it's different to best operator argument
1086 if (s_x != source.Type)
1087 source = implicitOnly ?
1088 ImplicitConversionStandard (ec, source, s_x, loc) :
1089 ExplicitConversionStandard (ec, source, s_x, loc);
1091 source = new UserCast (most_specific_operator, source, loc).Resolve (ec);
1094 // Convert result type when it's different to best operator return type
1096 if (t_x != target_type) {
1098 // User operator is of T?, no need to lift it
1100 if (TypeManager.IsNullableType (t_x) && t_x == target)
1103 source = implicitOnly ?
1104 ImplicitConversionStandard (ec, source, target_type, loc) :
1105 ExplicitConversionStandard (ec, source, target_type, loc);
1109 // Source expression is of nullable type, lift the result in case of it's null
1111 if (unwrap != null && (TypeManager.IsReferenceType (target) || target_type != target))
1112 source = new Nullable.Lifted (source, unwrap, target).Resolve (ec);
1113 else if (target_type != target)
1114 source = Nullable.Wrap.Create (source, target);
1120 /// Converts implicitly the resolved expression `expr' into the
1121 /// `target_type'. It returns a new expression that can be used
1122 /// in a context that expects a `target_type'.
1124 static public Expression ImplicitConversion (ResolveContext ec, Expression expr,
1125 TypeSpec target_type, Location loc)
1129 if (target_type == null)
1130 throw new Exception ("Target type is null");
1132 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1136 e = ImplicitUserConversion (ec, expr, target_type, loc);
1145 /// Attempts to apply the `Standard Implicit
1146 /// Conversion' rules to the expression `expr' into
1147 /// the `target_type'. It returns a new expression
1148 /// that can be used in a context that expects a
1151 /// This is different from `ImplicitConversion' in that the
1152 /// user defined implicit conversions are excluded.
1154 static public Expression ImplicitConversionStandard (ResolveContext ec, Expression expr,
1155 TypeSpec target_type, Location loc)
1157 return ImplicitConversionStandard (ec, expr, target_type, loc, false);
1160 static Expression ImplicitConversionStandard (ResolveContext ec, Expression expr, TypeSpec target_type, Location loc, bool explicit_cast)
1162 if (expr.eclass == ExprClass.MethodGroup){
1163 if (!TypeManager.IsDelegateType (target_type)){
1168 // Only allow anonymous method conversions on post ISO_1
1170 if (RootContext.Version != LanguageVersion.ISO_1){
1171 MethodGroupExpr mg = expr as MethodGroupExpr;
1173 return ImplicitDelegateCreation.Create (
1174 ec, mg, target_type, loc);
1178 TypeSpec expr_type = expr.Type;
1181 if (expr_type.Equals (target_type)) {
1182 if (expr_type != TypeManager.null_type && expr_type != InternalType.AnonymousMethod)
1187 if (TypeSpecComparer.Variant.IsEqual (expr_type, target_type)) {
1191 if (TypeManager.IsNullableType (target_type))
1192 return ImplicitNulableConversion (ec, expr, target_type);
1195 // Attempt to do the implicit constant expression conversions
1197 Constant c = expr as Constant;
1200 c = c.ConvertImplicitly (ec, target_type);
1202 Console.WriteLine ("Conversion error happened in line {0}", loc);
1209 e = ImplicitNumericConversion (expr, expr_type, target_type);
1213 e = ImplicitReferenceConversion (expr, target_type, explicit_cast);
1217 if (expr is IntConstant && TypeManager.IsEnumType (target_type)){
1218 Constant i = (Constant) expr;
1220 // LAMESPEC: Conversion from any 0 constant is allowed
1222 // An implicit enumeration conversion permits the decimal-integer-literal 0
1223 // to be converted to any enum-type and to any nullable-type whose underlying
1224 // type is an enum-type
1226 if (i.IsDefaultValue)
1227 return new EnumConstant (i, target_type).Resolve (ec);
1231 if (expr_type.IsPointer){
1232 if (target_type == TypeManager.void_ptr_type)
1233 return EmptyCast.Create (expr, target_type);
1236 // yep, comparing pointer types cant be done with
1237 // t1 == t2, we have to compare their element types.
1239 if (target_type.IsPointer){
1240 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1247 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1248 return EmptyCast.Create (new NullPointer (loc), target_type);
1251 if (expr_type == InternalType.AnonymousMethod){
1252 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1253 Expression am = ame.Compatible (ec, target_type);
1255 return am.Resolve (ec);
1258 if (expr_type == InternalType.Arglist && target_type == TypeManager.arg_iterator_type)
1265 /// Attempts to implicitly convert `source' into `target_type', using
1266 /// ImplicitConversion. If there is no implicit conversion, then
1267 /// an error is signaled
1269 static public Expression ImplicitConversionRequired (ResolveContext ec, Expression source,
1270 TypeSpec target_type, Location loc)
1272 Expression e = ImplicitConversion (ec, source, target_type, loc);
1276 if (source.Type == InternalType.Dynamic) {
1277 Arguments args = new Arguments (1);
1278 args.Add (new Argument (source));
1279 return new DynamicConversion (target_type, 0, args, loc).Resolve (ec);
1282 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1287 /// Performs the explicit numeric conversions
1289 /// There are a few conversions that are not part of the C# standard,
1290 /// they were interim hacks in the C# compiler that were supposed to
1291 /// become explicit operators in the UIntPtr class and IntPtr class,
1292 /// but for historical reasons it did not happen, so the C# compiler
1293 /// ended up with these special hacks.
1295 /// See bug 59800 for details.
1297 /// The conversion are:
1307 public static Expression ExplicitNumericConversion (Expression expr, TypeSpec target_type)
1309 TypeSpec expr_type = expr.Type;
1310 TypeSpec real_target_type = target_type;
1312 if (expr_type == TypeManager.sbyte_type){
1314 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1316 if (real_target_type == TypeManager.byte_type)
1317 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1318 if (real_target_type == TypeManager.ushort_type)
1319 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1320 if (real_target_type == TypeManager.uint32_type)
1321 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1322 if (real_target_type == TypeManager.uint64_type)
1323 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1324 if (real_target_type == TypeManager.char_type)
1325 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1327 // One of the built-in conversions that belonged in the class library
1328 if (real_target_type == TypeManager.uintptr_type){
1329 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1331 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1333 } else if (expr_type == TypeManager.byte_type){
1335 // From byte to sbyte and char
1337 if (real_target_type == TypeManager.sbyte_type)
1338 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1339 if (real_target_type == TypeManager.char_type)
1340 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1341 } else if (expr_type == TypeManager.short_type){
1343 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1345 if (real_target_type == TypeManager.sbyte_type)
1346 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1347 if (real_target_type == TypeManager.byte_type)
1348 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1349 if (real_target_type == TypeManager.ushort_type)
1350 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1351 if (real_target_type == TypeManager.uint32_type)
1352 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1353 if (real_target_type == TypeManager.uint64_type)
1354 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1355 if (real_target_type == TypeManager.char_type)
1356 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1358 // One of the built-in conversions that belonged in the class library
1359 if (real_target_type == TypeManager.uintptr_type){
1360 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1362 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1364 } else if (expr_type == TypeManager.ushort_type){
1366 // From ushort to sbyte, byte, short, char
1368 if (real_target_type == TypeManager.sbyte_type)
1369 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1370 if (real_target_type == TypeManager.byte_type)
1371 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1372 if (real_target_type == TypeManager.short_type)
1373 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1374 if (real_target_type == TypeManager.char_type)
1375 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1376 } else if (expr_type == TypeManager.int32_type){
1378 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1380 if (real_target_type == TypeManager.sbyte_type)
1381 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1382 if (real_target_type == TypeManager.byte_type)
1383 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1384 if (real_target_type == TypeManager.short_type)
1385 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1386 if (real_target_type == TypeManager.ushort_type)
1387 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1388 if (real_target_type == TypeManager.uint32_type)
1389 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1390 if (real_target_type == TypeManager.uint64_type)
1391 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1392 if (real_target_type == TypeManager.char_type)
1393 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1395 // One of the built-in conversions that belonged in the class library
1396 if (real_target_type == TypeManager.uintptr_type){
1397 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1399 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1401 } else if (expr_type == TypeManager.uint32_type){
1403 // From uint to sbyte, byte, short, ushort, int, char
1405 if (real_target_type == TypeManager.sbyte_type)
1406 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1407 if (real_target_type == TypeManager.byte_type)
1408 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1409 if (real_target_type == TypeManager.short_type)
1410 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1411 if (real_target_type == TypeManager.ushort_type)
1412 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1413 if (real_target_type == TypeManager.int32_type)
1414 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1415 if (real_target_type == TypeManager.char_type)
1416 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1417 } else if (expr_type == TypeManager.int64_type){
1419 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1421 if (real_target_type == TypeManager.sbyte_type)
1422 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1423 if (real_target_type == TypeManager.byte_type)
1424 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1425 if (real_target_type == TypeManager.short_type)
1426 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1427 if (real_target_type == TypeManager.ushort_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1429 if (real_target_type == TypeManager.int32_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1431 if (real_target_type == TypeManager.uint32_type)
1432 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1433 if (real_target_type == TypeManager.uint64_type)
1434 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1435 if (real_target_type == TypeManager.char_type)
1436 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1437 } else if (expr_type == TypeManager.uint64_type){
1439 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1441 if (real_target_type == TypeManager.sbyte_type)
1442 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1443 if (real_target_type == TypeManager.byte_type)
1444 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1445 if (real_target_type == TypeManager.short_type)
1446 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1447 if (real_target_type == TypeManager.ushort_type)
1448 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1449 if (real_target_type == TypeManager.int32_type)
1450 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1451 if (real_target_type == TypeManager.uint32_type)
1452 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1453 if (real_target_type == TypeManager.int64_type)
1454 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1455 if (real_target_type == TypeManager.char_type)
1456 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1458 // One of the built-in conversions that belonged in the class library
1459 if (real_target_type == TypeManager.intptr_type){
1460 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1461 TypeManager.intptr_type, true);
1463 } else if (expr_type == TypeManager.char_type){
1465 // From char to sbyte, byte, short
1467 if (real_target_type == TypeManager.sbyte_type)
1468 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1469 if (real_target_type == TypeManager.byte_type)
1470 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1471 if (real_target_type == TypeManager.short_type)
1472 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1473 } else if (expr_type == TypeManager.float_type){
1475 // From float to sbyte, byte, short,
1476 // ushort, int, uint, long, ulong, char
1479 if (real_target_type == TypeManager.sbyte_type)
1480 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1481 if (real_target_type == TypeManager.byte_type)
1482 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1483 if (real_target_type == TypeManager.short_type)
1484 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1485 if (real_target_type == TypeManager.ushort_type)
1486 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1487 if (real_target_type == TypeManager.int32_type)
1488 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1489 if (real_target_type == TypeManager.uint32_type)
1490 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1491 if (real_target_type == TypeManager.int64_type)
1492 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1493 if (real_target_type == TypeManager.uint64_type)
1494 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1495 if (real_target_type == TypeManager.char_type)
1496 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1497 if (real_target_type == TypeManager.decimal_type)
1498 return new CastToDecimal (expr, true);
1499 } else if (expr_type == TypeManager.double_type){
1501 // From double to sbyte, byte, short,
1502 // ushort, int, uint, long, ulong,
1503 // char, float or decimal
1505 if (real_target_type == TypeManager.sbyte_type)
1506 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1507 if (real_target_type == TypeManager.byte_type)
1508 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1509 if (real_target_type == TypeManager.short_type)
1510 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1511 if (real_target_type == TypeManager.ushort_type)
1512 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1513 if (real_target_type == TypeManager.int32_type)
1514 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1515 if (real_target_type == TypeManager.uint32_type)
1516 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1517 if (real_target_type == TypeManager.int64_type)
1518 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1519 if (real_target_type == TypeManager.uint64_type)
1520 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1521 if (real_target_type == TypeManager.char_type)
1522 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1523 if (real_target_type == TypeManager.float_type)
1524 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1525 if (real_target_type == TypeManager.decimal_type)
1526 return new CastToDecimal (expr, true);
1527 } else if (expr_type == TypeManager.uintptr_type){
1529 // Various built-in conversions that belonged in the class library
1531 // from uintptr to sbyte, short, int32
1533 if (real_target_type == TypeManager.sbyte_type){
1534 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1535 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1537 if (real_target_type == TypeManager.short_type){
1538 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1539 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1541 if (real_target_type == TypeManager.int32_type){
1542 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1543 TypeManager.int32_type);
1545 } else if (expr_type == TypeManager.intptr_type){
1546 if (real_target_type == TypeManager.uint64_type){
1547 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1548 TypeManager.uint64_type);
1550 } else if (expr_type == TypeManager.decimal_type) {
1551 return new CastFromDecimal (expr, target_type).Resolve ();
1557 /// Returns whether an explicit reference conversion can be performed
1558 /// from source_type to target_type
1560 public static bool ExplicitReferenceConversionExists (TypeSpec source_type, TypeSpec target_type)
1562 Expression e = ExplicitReferenceConversion (null, source_type, target_type);
1566 if (e == EmptyExpression.Null)
1569 throw new InternalErrorException ("Invalid probing conversion result");
1573 /// Implements Explicit Reference conversions
1575 static Expression ExplicitReferenceConversion (Expression source, TypeSpec source_type, TypeSpec target_type)
1578 // From object to a generic parameter
1580 if (source_type == TypeManager.object_type && TypeManager.IsGenericParameter (target_type))
1581 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1584 // Explicit type parameter conversion.
1586 if (TypeManager.IsGenericParameter (source_type))
1587 return ExplicitTypeParameterConversion (source, source_type, target_type);
1589 bool target_is_value_type = TypeManager.IsStruct (target_type) || TypeManager.IsEnumType (target_type);
1592 // Unboxing conversion from System.ValueType to any non-nullable-value-type
1594 if (source_type == TypeManager.value_type && target_is_value_type)
1595 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1598 // From object to any reference type or value type (unboxing)
1600 if (source_type == TypeManager.object_type)
1602 source == null ? EmptyExpression.Null :
1603 target_is_value_type ? new UnboxCast (source, target_type) :
1604 source is Constant ? (Expression) new EmptyConstantCast ((Constant) source, target_type) :
1605 new ClassCast (source, target_type);
1608 // From any class S to any class-type T, provided S is a base class of T
1610 if (TypeManager.IsSubclassOf (target_type, source_type))
1611 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1614 // From any interface-type S to to any class type T, provided T is not
1615 // sealed, or provided T implements S.
1617 if (source_type.IsInterface) {
1618 if (!target_type.IsSealed || target_type.ImplementsInterface (source_type)) {
1619 if (target_type.IsClass)
1620 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1623 // Unboxing conversion from any interface-type to any non-nullable-value-type that
1624 // implements the interface-type
1626 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1630 // From System.Collections.Generic.IList<T> and its base interfaces to a one-dimensional
1631 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1633 var target_array = target_type as ArrayContainer;
1634 if (target_array != null && IList_To_Array (source_type, target_array))
1635 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1640 var source_array = source_type as ArrayContainer;
1641 if (source_array != null) {
1642 var target_array = target_type as ArrayContainer;
1643 if (target_array != null) {
1645 // From System.Array to any array-type
1647 if (source_type == TypeManager.array_type)
1648 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1651 // From an array type S with an element type Se to an array type T with an
1652 // element type Te provided all the following are true:
1653 // * S and T differe only in element type, in other words, S and T
1654 // have the same number of dimensions.
1655 // * Both Se and Te are reference types
1656 // * An explicit reference conversions exist from Se to Te
1658 if (source_array.Rank == target_array.Rank) {
1660 source_type = source_array.Element;
1661 if (!TypeManager.IsReferenceType (source_type))
1664 var target_element = target_array.Element;
1665 if (!TypeManager.IsReferenceType (target_element))
1668 if (ExplicitReferenceConversionExists (source_type, target_element))
1669 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1676 // From a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces,
1677 // provided that there is an explicit reference conversion from S to T
1679 if (ArrayToIList (source_array, target_type, true))
1680 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1686 // From any class type S to any interface T, provides S is not sealed
1687 // and provided S does not implement T.
1689 if (target_type.IsInterface && !source_type.IsSealed && !source_type.ImplementsInterface (target_type)) {
1690 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1694 // From System delegate to any delegate-type
1696 if (source_type == TypeManager.delegate_type && TypeManager.IsDelegateType (target_type))
1697 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1703 /// Performs an explicit conversion of the expression `expr' whose
1704 /// type is expr.Type to `target_type'.
1706 static public Expression ExplicitConversionCore (ResolveContext ec, Expression expr,
1707 TypeSpec target_type, Location loc)
1709 TypeSpec expr_type = expr.Type;
1711 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1712 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc, true);
1716 if (TypeManager.IsEnumType (expr_type)) {
1717 TypeSpec real_target = TypeManager.IsEnumType (target_type) ? EnumSpec.GetUnderlyingType (target_type) : target_type;
1718 Expression underlying = EmptyCast.Create (expr, EnumSpec.GetUnderlyingType (expr_type));
1719 if (underlying.Type == real_target)
1723 ne = ImplicitNumericConversion (underlying, real_target);
1726 ne = ExplicitNumericConversion (underlying, real_target);
1729 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1731 if (ne == null && (real_target == TypeManager.intptr_type || real_target == TypeManager.uintptr_type))
1732 ne = ExplicitUserConversion (ec, underlying, real_target, loc);
1734 return ne != null ? EmptyCast.Create (ne, target_type) : null;
1737 if (TypeManager.IsEnumType (target_type)) {
1739 // System.Enum can be unboxed to any enum-type
1741 if (expr_type == TypeManager.enum_type)
1742 return new UnboxCast (expr, target_type);
1744 TypeSpec real_target = TypeManager.IsEnumType (target_type) ? EnumSpec.GetUnderlyingType (target_type) : target_type;
1746 if (expr_type == real_target)
1747 return EmptyCast.Create (expr, target_type);
1749 ne = ImplicitNumericConversion (expr, real_target);
1751 return EmptyCast.Create (ne, target_type);
1753 ne = ExplicitNumericConversion (expr, real_target);
1755 return EmptyCast.Create (ne, target_type);
1758 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1760 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1761 ne = ExplicitUserConversion (ec, expr, real_target, loc);
1763 return ExplicitConversionCore (ec, ne, target_type, loc);
1766 ne = ExplicitNumericConversion (expr, target_type);
1772 // Skip the ExplicitReferenceConversion because we can not convert
1773 // from Null to a ValueType, and ExplicitReference wont check against
1774 // null literal explicitly
1776 if (expr_type != TypeManager.null_type){
1777 ne = ExplicitReferenceConversion (expr, expr_type, target_type);
1783 ne = ExplicitUnsafe (expr, target_type);
1791 public static Expression ExplicitUnsafe (Expression expr, TypeSpec target_type)
1793 TypeSpec expr_type = expr.Type;
1795 if (target_type.IsPointer){
1796 if (expr_type.IsPointer)
1797 return EmptyCast.Create (expr, target_type);
1799 if (expr_type == TypeManager.sbyte_type ||
1800 expr_type == TypeManager.short_type ||
1801 expr_type == TypeManager.int32_type)
1802 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1804 if (expr_type == TypeManager.ushort_type ||
1805 expr_type == TypeManager.uint32_type ||
1806 expr_type == TypeManager.byte_type)
1807 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1809 if (expr_type == TypeManager.int64_type)
1810 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I);
1812 if (expr_type == TypeManager.uint64_type)
1813 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I);
1816 if (expr_type.IsPointer){
1817 if (target_type == TypeManager.sbyte_type)
1818 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1819 if (target_type == TypeManager.byte_type)
1820 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1821 if (target_type == TypeManager.short_type)
1822 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1823 if (target_type == TypeManager.ushort_type)
1824 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1825 if (target_type == TypeManager.int32_type)
1826 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1827 if (target_type == TypeManager.uint32_type)
1828 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1829 if (target_type == TypeManager.int64_type)
1830 return new ConvCast (expr, target_type, ConvCast.Mode.I_I8);
1831 if (target_type == TypeManager.uint64_type)
1832 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1838 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1840 static public Expression ExplicitConversionStandard (ResolveContext ec, Expression expr,
1841 TypeSpec target_type, Location l)
1843 int errors = ec.Report.Errors;
1844 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1845 if (ec.Report.Errors > errors)
1851 ne = ExplicitNumericConversion (expr, target_type);
1855 ne = ExplicitReferenceConversion (expr, expr.Type, target_type);
1859 if (ec.IsUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1860 return EmptyCast.Create (expr, target_type);
1862 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
1867 /// Performs an explicit conversion of the expression `expr' whose
1868 /// type is expr.Type to `target_type'.
1870 static public Expression ExplicitConversion (ResolveContext ec, Expression expr,
1871 TypeSpec target_type, Location loc)
1873 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1876 // Don't eliminate explicit precission casts
1879 if (target_type == TypeManager.float_type)
1880 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1882 if (target_type == TypeManager.double_type)
1883 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
1889 TypeSpec expr_type = expr.Type;
1890 if (TypeManager.IsNullableType (target_type)) {
1891 if (TypeManager.IsNullableType (expr_type)) {
1892 TypeSpec target = Nullable.NullableInfo.GetUnderlyingType (target_type);
1893 Expression unwrap = Nullable.Unwrap.Create (expr);
1894 e = ExplicitConversion (ec, unwrap, target, expr.Location);
1898 return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
1899 } else if (expr_type == TypeManager.object_type) {
1900 return new UnboxCast (expr, target_type);
1902 TypeSpec target = TypeManager.GetTypeArguments (target_type) [0];
1904 e = ExplicitConversionCore (ec, expr, target, loc);
1906 return Nullable.Wrap.Create (e, target_type);
1908 } else if (TypeManager.IsNullableType (expr_type)) {
1909 e = ImplicitBoxingConversion (expr, Nullable.NullableInfo.GetUnderlyingType (expr_type), target_type);
1913 e = Nullable.Unwrap.Create (expr, false);
1914 e = ExplicitConversionCore (ec, e, target_type, loc);
1916 return EmptyCast.Create (e, target_type);
1919 e = ExplicitUserConversion (ec, expr, target_type, loc);
1923 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);