2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
19 // A container class for all the conversion operations
21 public class Convert {
23 static bool TypeParameter_to_Null (Type target_type)
25 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
29 if (gc.HasReferenceTypeConstraint)
31 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
37 static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
39 ArrayList list = new ArrayList ();
40 list.Add (gc.EffectiveBaseClass);
41 foreach (Type t in gc.InterfaceConstraints) {
42 if (!TypeManager.IsGenericParameter (t))
45 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
47 list.Add (TypeParam_EffectiveBaseType (new_gc));
49 return FindMostEncompassedType (list);
54 // From a one-dimensional array-type S[] to System.Collections.IList<S> and base
55 // interfaces of this interface.
57 // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
58 // interfaces of this interface, provided there is an implicit reference conversion
61 static bool Array_To_IList (Type array, Type list)
64 if (!array.IsArray || (array.GetArrayRank () != 1) || !list.IsGenericType)
67 Type gt = list.GetGenericTypeDefinition ();
68 if ((gt != TypeManager.generic_ilist_type) &&
69 (gt != TypeManager.generic_icollection_type) &&
70 (gt != TypeManager.generic_ienumerable_type))
73 Type element_type = TypeManager.GetElementType (array);
74 Type arg_type = TypeManager.GetTypeArguments (list) [0];
76 if (element_type == arg_type)
79 if (MyEmptyExpr == null)
80 MyEmptyExpr = new EmptyExpression ();
81 MyEmptyExpr.SetType (TypeManager.GetElementType (array));
83 return ImplicitReferenceConversionExists (MyEmptyExpr, arg_type);
89 static Expression ImplicitTypeParameterConversion (Expression expr,
93 Type expr_type = expr.Type;
95 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
98 if (target_type == TypeManager.object_type)
99 return new BoxedCast (expr, target_type);
104 // We're converting from a type parameter which is known to be a reference type.
105 Type base_type = TypeParam_EffectiveBaseType (gc);
107 if (TypeManager.IsSubclassOf (base_type, target_type))
108 return new ClassCast (expr, target_type);
110 if (target_type.IsInterface) {
111 if (TypeManager.ImplementsInterface (base_type, target_type))
112 return new ClassCast (expr, target_type);
114 foreach (Type t in gc.InterfaceConstraints) {
115 if (TypeManager.IsSubclassOf (t, target_type))
116 return new ClassCast (expr, target_type);
117 if (TypeManager.ImplementsInterface (t, target_type))
118 return new ClassCast (expr, target_type);
122 foreach (Type t in gc.InterfaceConstraints) {
123 if (!TypeManager.IsGenericParameter (t))
125 if (TypeManager.IsSubclassOf (t, target_type))
126 return new ClassCast (expr, target_type);
127 if (TypeManager.ImplementsInterface (t, target_type))
128 return new ClassCast (expr, target_type);
134 static bool ExplicitTypeParameterConversionExists (Type source_type, Type target_type)
137 if (target_type.IsInterface)
140 if (TypeManager.IsGenericParameter (target_type)) {
141 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
145 foreach (Type iface in gc.InterfaceConstraints) {
146 if (!TypeManager.IsGenericParameter (iface))
149 if (TypeManager.IsSubclassOf (source_type, iface))
157 static Expression ExplicitTypeParameterConversion (Expression source, Type target_type)
160 Type source_type = source.Type;
162 if (target_type.IsInterface)
163 return new ClassCast (source, target_type);
165 if (TypeManager.IsGenericParameter (target_type)) {
166 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
170 foreach (Type iface in gc.InterfaceConstraints) {
171 if (!TypeManager.IsGenericParameter (iface))
174 if (TypeManager.IsSubclassOf (source_type, iface))
175 return new ClassCast (source, target_type);
182 static EmptyExpression MyEmptyExpr;
183 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
185 Type expr_type = expr.Type;
187 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
188 // if we are a method group, emit a warning
193 if (expr_type == TypeManager.void_type)
196 if (TypeManager.IsGenericParameter (expr_type))
197 return ImplicitTypeParameterConversion (expr, target_type);
200 // notice that it is possible to write "ValueType v = 1", the ValueType here
201 // is an abstract class, and not really a value type, so we apply the same rules.
203 if (target_type == TypeManager.object_type) {
205 // A pointer type cannot be converted to object
207 if (expr_type.IsPointer)
210 if (TypeManager.IsValueType (expr_type))
211 return new BoxedCast (expr, target_type);
212 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
213 if (expr_type == TypeManager.anonymous_method_type)
215 return new EmptyCast (expr, target_type);
219 } else if (target_type == TypeManager.value_type) {
220 if (TypeManager.IsValueType (expr_type))
221 return new BoxedCast (expr, target_type);
222 if (expr_type == TypeManager.null_type)
223 return new EmptyConstantCast ((Constant)expr, target_type);
226 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
228 // Special case: enumeration to System.Enum.
229 // System.Enum is not a value type, it is a class, so we need
230 // a boxing conversion
232 if (expr_type.IsEnum || TypeManager.IsGenericParameter (expr_type))
233 return new BoxedCast (expr, target_type);
235 return new EmptyCast (expr, target_type);
238 // This code is kind of mirrored inside ImplicitStandardConversionExists
239 // with the small distinction that we only probe there
241 // Always ensure that the code here and there is in sync
243 // from the null type to any reference-type.
244 if (expr_type == TypeManager.null_type) {
245 NullConstant nc = (NullConstant)expr;
246 return nc.ToType(target_type);
249 // from any class-type S to any interface-type T.
250 if (target_type.IsInterface) {
251 if (target_type != TypeManager.iconvertible_type &&
252 expr_type.IsValueType && (expr is Constant) &&
253 !(expr is IntLiteral || expr is BoolLiteral ||
254 expr is FloatLiteral || expr is DoubleLiteral ||
255 expr is LongLiteral || expr is CharLiteral ||
256 expr is StringLiteral || expr is DecimalLiteral ||
257 expr is UIntLiteral || expr is ULongLiteral)) {
261 if (TypeManager.ImplementsInterface (expr_type, target_type)){
262 if (TypeManager.IsGenericParameter (expr_type) || TypeManager.IsValueType (expr_type))
263 return new BoxedCast (expr, target_type);
265 return new EmptyCast (expr, target_type);
269 // from any interface type S to interface-type T.
270 if (expr_type.IsInterface && target_type.IsInterface) {
271 if (TypeManager.ImplementsInterface (expr_type, target_type))
272 return new EmptyCast (expr, target_type);
277 // from an array-type S to an array-type of type T
278 if (expr_type.IsArray && target_type.IsArray) {
279 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
281 Type expr_element_type = TypeManager.GetElementType (expr_type);
283 if (MyEmptyExpr == null)
284 MyEmptyExpr = new EmptyExpression ();
286 MyEmptyExpr.SetType (expr_element_type);
287 Type target_element_type = TypeManager.GetElementType (target_type);
289 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
290 if (ImplicitStandardConversionExists (MyEmptyExpr,
291 target_element_type))
292 return new EmptyCast (expr, target_type);
296 // from an array-type to System.Array
297 if (expr_type.IsArray && target_type == TypeManager.array_type)
298 return new EmptyCast (expr, target_type);
300 // from an array-type of type T to IList<T>
301 if (expr_type.IsArray && Array_To_IList (expr_type, target_type))
302 return new EmptyCast (expr, target_type);
304 // from any delegate type to System.Delegate
305 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
306 target_type == TypeManager.delegate_type)
307 return new EmptyCast (expr, target_type);
309 // from any array-type or delegate type into System.ICloneable.
310 if (expr_type.IsArray ||
311 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
312 if (target_type == TypeManager.icloneable_type)
313 return new EmptyCast (expr, target_type);
315 // from a generic type definition to a generic instance.
316 if (TypeManager.IsEqual (expr_type, target_type))
317 return new EmptyCast (expr, target_type);
323 // Tests whether an implicit reference conversion exists between expr_type
326 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
328 if (target_type.IsValueType)
331 Type expr_type = expr.Type;
333 if (TypeManager.IsGenericParameter (expr_type))
334 return ImplicitTypeParameterConversion (expr, target_type) != null;
337 // This is the boxed case.
339 if (target_type == TypeManager.object_type) {
340 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
341 expr_type.IsInterface || expr_type == TypeManager.enum_type)
342 if (target_type != TypeManager.anonymous_method_type)
346 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
349 // Please remember that all code below actually comes
350 // from ImplicitReferenceConversion so make sure code remains in sync
352 // from any class-type S to any interface-type T.
353 if (target_type.IsInterface) {
354 if (target_type != TypeManager.iconvertible_type &&
355 expr_type.IsValueType && (expr is Constant) &&
356 !(expr is IntLiteral || expr is BoolLiteral ||
357 expr is FloatLiteral || expr is DoubleLiteral ||
358 expr is LongLiteral || expr is CharLiteral ||
359 expr is StringLiteral || expr is DecimalLiteral ||
360 expr is UIntLiteral || expr is ULongLiteral)) {
364 if (TypeManager.ImplementsInterface (expr_type, target_type))
368 // from any interface type S to interface-type T.
369 if (expr_type.IsInterface && target_type.IsInterface)
370 if (TypeManager.ImplementsInterface (expr_type, target_type))
373 // from an array-type S to an array-type of type T
374 if (expr_type.IsArray && target_type.IsArray) {
375 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
377 Type expr_element_type = expr_type.GetElementType ();
379 if (MyEmptyExpr == null)
380 MyEmptyExpr = new EmptyExpression ();
382 MyEmptyExpr.SetType (expr_element_type);
383 Type target_element_type = TypeManager.GetElementType (target_type);
385 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
386 if (ImplicitStandardConversionExists (MyEmptyExpr,
387 target_element_type))
392 // from an array-type to System.Array
393 if (expr_type.IsArray && (target_type == TypeManager.array_type))
396 // from an array-type of type T to IList<T>
397 if (expr_type.IsArray && Array_To_IList (expr_type, target_type))
400 // from any delegate type to System.Delegate
401 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
402 target_type == TypeManager.delegate_type)
403 if (target_type.IsAssignableFrom (expr_type))
406 // from any array-type or delegate type into System.ICloneable.
407 if (expr_type.IsArray ||
408 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
409 if (target_type == TypeManager.icloneable_type)
412 // from the null type to any reference-type.
413 if (expr_type == TypeManager.null_type){
414 if (target_type.IsPointer)
417 if (!target_type.IsValueType)
421 // from a generic type definition to a generic instance.
422 if (TypeManager.IsEqual (expr_type, target_type))
429 /// Implicit Numeric Conversions.
431 /// expr is the expression to convert, returns a new expression of type
432 /// target_type or null if an implicit conversion is not possible.
434 static public Expression ImplicitNumericConversion (Expression expr,
437 Type expr_type = expr.Type;
440 // Attempt to do the implicit constant expression conversions
442 if (expr is Constant){
443 if (expr is IntConstant){
446 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
450 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
452 // Try the implicit constant expression conversion
453 // from long to ulong, instead of a nice routine,
456 long v = ((LongConstant) expr).Value;
458 return new ULongConstant ((ulong) v, expr.Location);
462 Type real_target_type = target_type;
464 if (expr_type == TypeManager.sbyte_type){
466 // From sbyte to short, int, long, float, double, decimal
468 if (real_target_type == TypeManager.int32_type)
469 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
470 if (real_target_type == TypeManager.int64_type)
471 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
472 if (real_target_type == TypeManager.double_type)
473 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
474 if (real_target_type == TypeManager.float_type)
475 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
476 if (real_target_type == TypeManager.short_type)
477 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
478 if (real_target_type == TypeManager.decimal_type)
479 return new CastToDecimal (expr);
480 } else if (expr_type == TypeManager.byte_type){
482 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
484 if ((real_target_type == TypeManager.short_type) ||
485 (real_target_type == TypeManager.ushort_type) ||
486 (real_target_type == TypeManager.int32_type) ||
487 (real_target_type == TypeManager.uint32_type))
488 return new EmptyCast (expr, target_type);
490 if (real_target_type == TypeManager.uint64_type)
491 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
492 if (real_target_type == TypeManager.int64_type)
493 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
494 if (real_target_type == TypeManager.float_type)
495 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
496 if (real_target_type == TypeManager.double_type)
497 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
498 if (real_target_type == TypeManager.decimal_type)
499 return new CastToDecimal (expr);
501 } else if (expr_type == TypeManager.short_type){
503 // From short to int, long, float, double, decimal
505 if (real_target_type == TypeManager.int32_type)
506 return new EmptyCast (expr, target_type);
507 if (real_target_type == TypeManager.int64_type)
508 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
509 if (real_target_type == TypeManager.double_type)
510 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
511 if (real_target_type == TypeManager.float_type)
512 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
513 if (real_target_type == TypeManager.decimal_type)
514 return new CastToDecimal (expr);
516 } else if (expr_type == TypeManager.ushort_type){
518 // From ushort to int, uint, long, ulong, float, double, decimal
520 if (real_target_type == TypeManager.uint32_type)
521 return new EmptyCast (expr, target_type);
523 if (real_target_type == TypeManager.uint64_type)
524 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
525 if (real_target_type == TypeManager.int32_type)
526 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
527 if (real_target_type == TypeManager.int64_type)
528 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
529 if (real_target_type == TypeManager.double_type)
530 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
531 if (real_target_type == TypeManager.float_type)
532 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
533 if (real_target_type == TypeManager.decimal_type)
534 return new CastToDecimal (expr);
535 } else if (expr_type == TypeManager.int32_type){
537 // From int to long, float, double, decimal
539 if (real_target_type == TypeManager.int64_type)
540 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
541 if (real_target_type == TypeManager.double_type)
542 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
543 if (real_target_type == TypeManager.float_type)
544 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
545 if (real_target_type == TypeManager.decimal_type)
546 return new CastToDecimal (expr);
547 } else if (expr_type == TypeManager.uint32_type){
549 // From uint to long, ulong, float, double, decimal
551 if (real_target_type == TypeManager.int64_type)
552 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
553 if (real_target_type == TypeManager.uint64_type)
554 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
555 if (real_target_type == TypeManager.double_type)
556 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
558 if (real_target_type == TypeManager.float_type)
559 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
561 if (real_target_type == TypeManager.decimal_type)
562 return new CastToDecimal (expr);
563 } else if (expr_type == TypeManager.int64_type){
565 // From long/ulong to float, double
567 if (real_target_type == TypeManager.double_type)
568 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
569 if (real_target_type == TypeManager.float_type)
570 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
571 if (real_target_type == TypeManager.decimal_type)
572 return new CastToDecimal (expr);
573 } else if (expr_type == TypeManager.uint64_type){
575 // From ulong to float, double
577 if (real_target_type == TypeManager.double_type)
578 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
580 if (real_target_type == TypeManager.float_type)
581 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
583 if (real_target_type == TypeManager.decimal_type)
584 return new CastToDecimal (expr);
585 } else if (expr_type == TypeManager.char_type){
587 // From char to ushort, int, uint, long, ulong, float, double, decimal
589 if ((real_target_type == TypeManager.ushort_type) ||
590 (real_target_type == TypeManager.int32_type) ||
591 (real_target_type == TypeManager.uint32_type))
592 return new EmptyCast (expr, target_type);
593 if (real_target_type == TypeManager.uint64_type)
594 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
595 if (real_target_type == TypeManager.int64_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
597 if (real_target_type == TypeManager.float_type)
598 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
599 if (real_target_type == TypeManager.double_type)
600 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
601 if (real_target_type == TypeManager.decimal_type)
602 return new CastToDecimal (expr);
603 } else if (expr_type == TypeManager.float_type){
607 if (real_target_type == TypeManager.double_type)
608 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
615 /// Same as ImplicitStandardConversionExists except that it also looks at
616 /// implicit user defined conversions - needed for overload resolution
618 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
621 if (expr is NullLiteral) {
622 if (TypeManager.IsGenericParameter (target_type))
623 return TypeParameter_to_Null (target_type);
625 if (TypeManager.IsNullableType (target_type))
629 if (ImplicitStandardConversionExists (expr, target_type))
632 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
635 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
637 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
641 /// Determines if a standard implicit conversion exists from
642 /// expr_type to target_type
644 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
646 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
648 Type expr_type = expr.Type;
650 if (TypeManager.IsNullableType (target_type)) {
651 // if implicit standard conversion S -> T exists, S -> T? and S? -> T? also exists
652 target_type = TypeManager.GetTypeArguments (target_type) [0];
655 if (TypeManager.IsNullableType (expr_type)) {
656 EmptyExpression new_expr = EmptyExpression.Grab ();
657 new_expr.SetType (TypeManager.GetTypeArguments (expr_type) [0]);
658 bool retval = ImplicitStandardConversionExists (new_expr, target_type);
659 EmptyExpression.Release (new_expr);
664 if (expr_type == TypeManager.void_type)
667 //Console.WriteLine ("Expr is {0}", expr);
668 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
669 if (expr_type.Equals (target_type))
673 // First numeric conversions
675 if (expr_type == TypeManager.sbyte_type){
677 // From sbyte to short, int, long, float, double, decimal
679 if ((target_type == TypeManager.int32_type) ||
680 (target_type == TypeManager.int64_type) ||
681 (target_type == TypeManager.double_type) ||
682 (target_type == TypeManager.float_type) ||
683 (target_type == TypeManager.short_type) ||
684 (target_type == TypeManager.decimal_type))
687 } else if (expr_type == TypeManager.byte_type){
689 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
691 if ((target_type == TypeManager.short_type) ||
692 (target_type == TypeManager.ushort_type) ||
693 (target_type == TypeManager.int32_type) ||
694 (target_type == TypeManager.uint32_type) ||
695 (target_type == TypeManager.uint64_type) ||
696 (target_type == TypeManager.int64_type) ||
697 (target_type == TypeManager.float_type) ||
698 (target_type == TypeManager.double_type) ||
699 (target_type == TypeManager.decimal_type))
702 } else if (expr_type == TypeManager.short_type){
704 // From short to int, long, double, float, decimal
706 if ((target_type == TypeManager.int32_type) ||
707 (target_type == TypeManager.int64_type) ||
708 (target_type == TypeManager.double_type) ||
709 (target_type == TypeManager.float_type) ||
710 (target_type == TypeManager.decimal_type))
713 } else if (expr_type == TypeManager.ushort_type){
715 // From ushort to int, uint, long, ulong, double, float, decimal
717 if ((target_type == TypeManager.uint32_type) ||
718 (target_type == TypeManager.uint64_type) ||
719 (target_type == TypeManager.int32_type) ||
720 (target_type == TypeManager.int64_type) ||
721 (target_type == TypeManager.double_type) ||
722 (target_type == TypeManager.float_type) ||
723 (target_type == TypeManager.decimal_type))
726 } else if (expr_type == TypeManager.int32_type){
728 // From int to long, double, float, decimal
730 if ((target_type == TypeManager.int64_type) ||
731 (target_type == TypeManager.double_type) ||
732 (target_type == TypeManager.float_type) ||
733 (target_type == TypeManager.decimal_type))
736 } else if (expr_type == TypeManager.uint32_type){
738 // From uint to long, ulong, double, float, decimal
740 if ((target_type == TypeManager.int64_type) ||
741 (target_type == TypeManager.uint64_type) ||
742 (target_type == TypeManager.double_type) ||
743 (target_type == TypeManager.float_type) ||
744 (target_type == TypeManager.decimal_type))
747 } else if ((expr_type == TypeManager.uint64_type) ||
748 (expr_type == TypeManager.int64_type)) {
750 // From long/ulong to double, float, decimal
752 if ((target_type == TypeManager.double_type) ||
753 (target_type == TypeManager.float_type) ||
754 (target_type == TypeManager.decimal_type))
757 } else if (expr_type == TypeManager.char_type){
759 // From char to ushort, int, uint, ulong, long, float, double, decimal
761 if ((target_type == TypeManager.ushort_type) ||
762 (target_type == TypeManager.int32_type) ||
763 (target_type == TypeManager.uint32_type) ||
764 (target_type == TypeManager.uint64_type) ||
765 (target_type == TypeManager.int64_type) ||
766 (target_type == TypeManager.float_type) ||
767 (target_type == TypeManager.double_type) ||
768 (target_type == TypeManager.decimal_type))
771 } else if (expr_type == TypeManager.float_type){
775 if (target_type == TypeManager.double_type)
779 if (expr.eclass == ExprClass.MethodGroup){
780 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
781 MethodGroupExpr mg = expr as MethodGroupExpr;
783 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
788 if (ImplicitReferenceConversionExists (expr, target_type))
792 // Implicit Constant Expression Conversions
794 if (expr is IntConstant){
795 int value = ((IntConstant) expr).Value;
797 if (target_type == TypeManager.sbyte_type){
798 if (value >= SByte.MinValue && value <= SByte.MaxValue)
800 } else if (target_type == TypeManager.byte_type){
801 if (value >= 0 && value <= Byte.MaxValue)
803 } else if (target_type == TypeManager.short_type){
804 if (value >= Int16.MinValue && value <= Int16.MaxValue)
806 } else if (target_type == TypeManager.ushort_type){
807 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
809 } else if (target_type == TypeManager.uint32_type){
812 } else if (target_type == TypeManager.uint64_type){
814 // we can optimize this case: a positive int32
815 // always fits on a uint64. But we need an opcode
822 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
826 if (expr is LongConstant && target_type == TypeManager.uint64_type){
828 // Try the implicit constant expression conversion
829 // from long to ulong, instead of a nice routine,
832 long v = ((LongConstant) expr).Value;
837 if ((target_type == TypeManager.enum_type ||
838 target_type.IsSubclassOf (TypeManager.enum_type)) &&
840 IntLiteral i = (IntLiteral) expr;
847 // If `expr_type' implements `target_type' (which is an iface)
848 // see TryImplicitIntConversion
850 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
853 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
856 if (expr_type == TypeManager.anonymous_method_type){
857 if (!TypeManager.IsDelegateType (target_type))
860 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
861 return ame.ImplicitStandardConversionExists (target_type);
868 /// Finds "most encompassed type" according to the spec (13.4.2)
869 /// amongst the methods in the MethodGroupExpr
871 static Type FindMostEncompassedType (ArrayList types)
875 if (types.Count == 0)
878 if (types.Count == 1)
879 return (Type) types [0];
881 EmptyExpression expr = EmptyExpression.Grab ();
883 foreach (Type t in types) {
890 if (ImplicitStandardConversionExists (expr, best))
895 foreach (Type t in types) {
898 if (!ImplicitStandardConversionExists (expr, t)) {
904 EmptyExpression.Release (expr);
910 /// Finds "most encompassing type" according to the spec (13.4.2)
911 /// amongst the types in the given set
913 static Type FindMostEncompassingType (ArrayList types)
917 if (types.Count == 0)
920 if (types.Count == 1)
921 return (Type) types [0];
923 EmptyExpression expr = EmptyExpression.Grab ();
925 foreach (Type t in types) {
932 if (ImplicitStandardConversionExists (expr, t))
936 foreach (Type t in types) {
940 if (!ImplicitStandardConversionExists (expr, best)) {
946 EmptyExpression.Release (expr);
952 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
953 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
954 /// for explicit and implicit conversion operators.
956 static public Type FindMostSpecificSource (IList list,
957 Expression source, bool apply_explicit_conv_rules)
959 ArrayList src_types_set = new ArrayList ();
962 // If any operator converts from S then Sx = S
964 Type source_type = source.Type;
965 foreach (MethodBase mb in list){
966 ParameterData pd = TypeManager.GetParameterData (mb);
967 Type param_type = pd.ParameterType (0);
969 if (param_type == source_type)
972 src_types_set.Add (param_type);
976 // Explicit Conv rules
978 if (apply_explicit_conv_rules) {
979 ArrayList candidate_set = new ArrayList ();
981 foreach (Type param_type in src_types_set){
982 if (ImplicitStandardConversionExists (source, param_type))
983 candidate_set.Add (param_type);
986 if (candidate_set.Count != 0)
987 return FindMostEncompassedType (candidate_set);
993 if (apply_explicit_conv_rules)
994 return FindMostEncompassingType (src_types_set);
996 return FindMostEncompassedType (src_types_set);
1000 /// Finds the most specific target Tx according to section 13.4.4
1002 static public Type FindMostSpecificTarget (IList list,
1003 Type target, bool apply_explicit_conv_rules)
1005 ArrayList tgt_types_set = new ArrayList ();
1008 // If any operator converts to T then Tx = T
1010 foreach (MethodInfo mi in list){
1011 Type ret_type = mi.ReturnType;
1012 if (ret_type == target)
1015 tgt_types_set.Add (ret_type);
1019 // Explicit conv rules
1021 if (apply_explicit_conv_rules) {
1022 ArrayList candidate_set = new ArrayList ();
1024 EmptyExpression expr = EmptyExpression.Grab ();
1026 foreach (Type ret_type in tgt_types_set){
1027 expr.SetType (ret_type);
1029 if (ImplicitStandardConversionExists (expr, target))
1030 candidate_set.Add (ret_type);
1033 EmptyExpression.Release (expr);
1035 if (candidate_set.Count != 0)
1036 return FindMostEncompassingType (candidate_set);
1040 // Okay, final case !
1042 if (apply_explicit_conv_rules)
1043 return FindMostEncompassedType (tgt_types_set);
1045 return FindMostEncompassingType (tgt_types_set);
1049 /// User-defined Implicit conversions
1051 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1052 Type target, Location loc)
1054 return UserDefinedConversion (ec, source, target, loc, false);
1058 /// User-defined Explicit conversions
1060 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1061 Type target, Location loc)
1063 return UserDefinedConversion (ec, source, target, loc, true);
1066 static void AddConversionOperators (ArrayList list,
1067 Expression source, Type target_type,
1068 bool look_for_explicit,
1074 Type source_type = source.Type;
1075 EmptyExpression expr = EmptyExpression.Grab ();
1076 foreach (MethodInfo m in mg.Methods) {
1077 ParameterData pd = TypeManager.GetParameterData (m);
1078 Type return_type = m.ReturnType;
1079 Type arg_type = pd.ParameterType (0);
1081 if (source_type != arg_type) {
1082 if (!ImplicitStandardConversionExists (source, arg_type)) {
1083 if (!look_for_explicit)
1085 expr.SetType (arg_type);
1086 if (!ImplicitStandardConversionExists (expr, source_type))
1091 if (target_type != return_type) {
1092 expr.SetType (return_type);
1093 if (!ImplicitStandardConversionExists (expr, target_type)) {
1094 if (!look_for_explicit)
1096 expr.SetType (target_type);
1097 if (!ImplicitStandardConversionExists (expr, return_type))
1105 EmptyExpression.Release (expr);
1109 /// Compute the user-defined conversion operator from source_type to target_type.
1110 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1112 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1114 ArrayList ops = new ArrayList (4);
1116 Type source_type = source.Type;
1118 if (source_type != TypeManager.decimal_type) {
1119 AddConversionOperators (ops, source, target_type, look_for_explicit,
1120 Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1121 if (look_for_explicit) {
1122 AddConversionOperators (ops, source, target_type, look_for_explicit,
1123 Expression.MethodLookup (
1124 container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1128 if (target_type != TypeManager.decimal_type) {
1129 AddConversionOperators (ops, source, target_type, look_for_explicit,
1130 Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1131 if (look_for_explicit) {
1132 AddConversionOperators (ops, source, target_type, look_for_explicit,
1133 Expression.MethodLookup (
1134 container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1141 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1142 if (most_specific_source == null)
1145 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1146 if (most_specific_target == null)
1149 MethodInfo method = null;
1151 foreach (MethodInfo m in ops) {
1152 if (m.ReturnType != most_specific_target)
1154 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1156 // Ambiguous: more than one conversion operator satisfies the signature.
1165 static DoubleHash explicit_conv = new DoubleHash (100);
1166 static DoubleHash implicit_conv = new DoubleHash (100);
1169 /// User-defined conversions
1171 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1172 Type target, Location loc,
1173 bool look_for_explicit)
1175 Type source_type = source.Type;
1176 MethodInfo method = null;
1179 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1181 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1182 method = (MethodInfo) o;
1184 method = GetConversionOperator (null, source, target, look_for_explicit);
1185 if (!(source is Constant))
1186 hash.Insert (source_type, target, method);
1192 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1195 // This will do the conversion to the best match that we
1196 // found. Now we need to perform an implict standard conversion
1197 // if the best match was not the type that we were requested
1200 if (look_for_explicit)
1201 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1203 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1209 e = new UserCast (method, source, loc);
1210 if (e.Type != target){
1211 if (!look_for_explicit)
1212 e = ImplicitConversionStandard (ec, e, target, loc);
1214 e = ExplicitConversionStandard (ec, e, target, loc);
1221 /// Converts implicitly the resolved expression `expr' into the
1222 /// `target_type'. It returns a new expression that can be used
1223 /// in a context that expects a `target_type'.
1225 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1226 Type target_type, Location loc)
1230 if (target_type == null)
1231 throw new Exception ("Target type is null");
1233 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1237 e = ImplicitUserConversion (ec, expr, target_type, loc);
1246 /// Attempts to apply the `Standard Implicit
1247 /// Conversion' rules to the expression `expr' into
1248 /// the `target_type'. It returns a new expression
1249 /// that can be used in a context that expects a
1252 /// This is different from `ImplicitConversion' in that the
1253 /// user defined implicit conversions are excluded.
1255 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1256 Type target_type, Location loc)
1258 Type expr_type = expr.Type;
1261 // TODO: refactor to be a part of constant infrastructure
1262 if (expr is NullLiteral) {
1263 if (TypeManager.IsNullableType (target_type))
1264 return new Nullable.NullableLiteral (target_type, loc);
1267 if (TypeManager.IsNullableType (target_type)) {
1268 Type target = TypeManager.GetTypeArguments (target_type) [0];
1270 if (TypeManager.IsNullableType (expr.Type)) {
1271 e = new Nullable.LiftedConversion (
1272 expr, target_type, false, false, loc).Resolve (ec);
1276 e = ImplicitConversion (ec, expr, target, loc);
1278 return Nullable.Wrap.Create (e, ec);
1282 if (expr.eclass == ExprClass.MethodGroup){
1283 if (!TypeManager.IsDelegateType (target_type)){
1288 // Only allow anonymous method conversions on post ISO_1
1290 if (RootContext.Version != LanguageVersion.ISO_1){
1291 MethodGroupExpr mg = expr as MethodGroupExpr;
1293 return ImplicitDelegateCreation.Create (
1294 ec, mg, target_type, loc);
1298 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1301 e = ImplicitNumericConversion (expr, target_type);
1305 e = ImplicitReferenceConversion (expr, target_type);
1309 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1310 IntLiteral i = (IntLiteral) expr;
1313 return new EnumConstant ((Constant) expr, target_type);
1317 if (expr_type.IsPointer){
1318 if (target_type == TypeManager.void_ptr_type)
1319 return new EmptyCast (expr, target_type);
1322 // yep, comparing pointer types cant be done with
1323 // t1 == t2, we have to compare their element types.
1325 if (target_type.IsPointer){
1326 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1333 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1334 return new EmptyCast (NullPointer.Null, target_type);
1337 if (expr_type == TypeManager.anonymous_method_type){
1338 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1340 int errors = Report.Errors;
1342 Expression conv = ame.Compatible (ec, target_type);
1347 // We return something instead of null, to avoid
1348 // the duplicate error, since am.Compatible would have
1349 // reported that already
1351 if (errors != Report.Errors)
1352 return new EmptyCast (expr, target_type);
1359 /// Attempts to perform an implicit constant conversion of the IntConstant
1360 /// into a different data type using casts (See Implicit Constant
1361 /// Expression Conversions)
1363 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1365 int value = ic.Value;
1367 if (target_type == TypeManager.sbyte_type){
1368 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1369 return new SByteConstant ((sbyte) value, ic.Location);
1370 } else if (target_type == TypeManager.byte_type){
1371 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1372 return new ByteConstant ((byte) value, ic.Location);
1373 } else if (target_type == TypeManager.short_type){
1374 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1375 return new ShortConstant ((short) value, ic.Location);
1376 } else if (target_type == TypeManager.ushort_type){
1377 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1378 return new UShortConstant ((ushort) value, ic.Location);
1379 } else if (target_type == TypeManager.uint32_type){
1381 return new UIntConstant ((uint) value, ic.Location);
1382 } else if (target_type == TypeManager.uint64_type){
1384 // we can optimize this case: a positive int32
1385 // always fits on a uint64. But we need an opcode
1389 return new ULongConstant ((ulong) value, ic.Location);
1390 } else if (target_type == TypeManager.double_type)
1391 return new DoubleConstant ((double) value, ic.Location);
1392 else if (target_type == TypeManager.float_type)
1393 return new FloatConstant ((float) value, ic.Location);
1395 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1396 Type underlying = TypeManager.EnumToUnderlying (target_type);
1397 Constant e = (Constant) ic;
1400 // Possibly, we need to create a different 0 literal before passing
1403 if (underlying == TypeManager.int64_type)
1404 e = new LongLiteral (0, ic.Location);
1405 else if (underlying == TypeManager.uint64_type)
1406 e = new ULongLiteral (0, ic.Location);
1408 return new EnumConstant (e, target_type);
1412 // If `target_type' is an interface and the type of `ic' implements the interface
1413 // e.g. target_type is IComparable, IConvertible, IFormattable
1415 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1416 return new BoxedCast (ic, target_type);
1422 /// Attempts to implicitly convert `source' into `target_type', using
1423 /// ImplicitConversion. If there is no implicit conversion, then
1424 /// an error is signaled
1426 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1427 Type target_type, Location loc)
1429 Expression e = ImplicitConversion (ec, source, target_type, loc);
1433 source.Error_ValueCannotBeConverted (loc, target_type, false);
1438 /// Performs the explicit numeric conversions
1440 /// There are a few conversions that are not part of the C# standard,
1441 /// they were interim hacks in the C# compiler that were supposed to
1442 /// become explicit operators in the UIntPtr class and IntPtr class,
1443 /// but for historical reasons it did not happen, so the C# compiler
1444 /// ended up with these special hacks.
1446 /// See bug 59800 for details.
1448 /// The conversion are:
1458 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1460 Type expr_type = expr.Type;
1461 Type real_target_type = target_type;
1463 if (expr_type == TypeManager.sbyte_type){
1465 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1467 if (real_target_type == TypeManager.byte_type)
1468 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1469 if (real_target_type == TypeManager.ushort_type)
1470 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1471 if (real_target_type == TypeManager.uint32_type)
1472 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1473 if (real_target_type == TypeManager.uint64_type)
1474 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1475 if (real_target_type == TypeManager.char_type)
1476 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1478 // One of the built-in conversions that belonged in the class library
1479 if (real_target_type == TypeManager.uintptr_type){
1480 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1482 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1484 } else if (expr_type == TypeManager.byte_type){
1486 // From byte to sbyte and char
1488 if (real_target_type == TypeManager.sbyte_type)
1489 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1490 if (real_target_type == TypeManager.char_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1492 } else if (expr_type == TypeManager.short_type){
1494 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1496 if (real_target_type == TypeManager.sbyte_type)
1497 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1498 if (real_target_type == TypeManager.byte_type)
1499 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1500 if (real_target_type == TypeManager.ushort_type)
1501 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1502 if (real_target_type == TypeManager.uint32_type)
1503 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1504 if (real_target_type == TypeManager.uint64_type)
1505 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1506 if (real_target_type == TypeManager.char_type)
1507 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1509 // One of the built-in conversions that belonged in the class library
1510 if (real_target_type == TypeManager.uintptr_type){
1511 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1513 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1515 } else if (expr_type == TypeManager.ushort_type){
1517 // From ushort to sbyte, byte, short, char
1519 if (real_target_type == TypeManager.sbyte_type)
1520 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1521 if (real_target_type == TypeManager.byte_type)
1522 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1523 if (real_target_type == TypeManager.short_type)
1524 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1525 if (real_target_type == TypeManager.char_type)
1526 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1527 } else if (expr_type == TypeManager.int32_type){
1529 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1531 if (real_target_type == TypeManager.sbyte_type)
1532 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1533 if (real_target_type == TypeManager.byte_type)
1534 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1535 if (real_target_type == TypeManager.short_type)
1536 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1537 if (real_target_type == TypeManager.ushort_type)
1538 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1539 if (real_target_type == TypeManager.uint32_type)
1540 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1541 if (real_target_type == TypeManager.uint64_type)
1542 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1543 if (real_target_type == TypeManager.char_type)
1544 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1546 // One of the built-in conversions that belonged in the class library
1547 if (real_target_type == TypeManager.uintptr_type){
1548 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1550 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1552 } else if (expr_type == TypeManager.uint32_type){
1554 // From uint to sbyte, byte, short, ushort, int, char
1556 if (real_target_type == TypeManager.sbyte_type)
1557 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1558 if (real_target_type == TypeManager.byte_type)
1559 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1560 if (real_target_type == TypeManager.short_type)
1561 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1562 if (real_target_type == TypeManager.ushort_type)
1563 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1564 if (real_target_type == TypeManager.int32_type)
1565 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1566 if (real_target_type == TypeManager.char_type)
1567 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1568 } else if (expr_type == TypeManager.int64_type){
1570 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1572 if (real_target_type == TypeManager.sbyte_type)
1573 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1574 if (real_target_type == TypeManager.byte_type)
1575 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1576 if (real_target_type == TypeManager.short_type)
1577 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1578 if (real_target_type == TypeManager.ushort_type)
1579 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1580 if (real_target_type == TypeManager.int32_type)
1581 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1582 if (real_target_type == TypeManager.uint32_type)
1583 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1584 if (real_target_type == TypeManager.uint64_type)
1585 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1586 if (real_target_type == TypeManager.char_type)
1587 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1588 } else if (expr_type == TypeManager.uint64_type){
1590 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1592 if (real_target_type == TypeManager.sbyte_type)
1593 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1594 if (real_target_type == TypeManager.byte_type)
1595 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1596 if (real_target_type == TypeManager.short_type)
1597 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1598 if (real_target_type == TypeManager.ushort_type)
1599 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1600 if (real_target_type == TypeManager.int32_type)
1601 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1602 if (real_target_type == TypeManager.uint32_type)
1603 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1604 if (real_target_type == TypeManager.int64_type)
1605 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1606 if (real_target_type == TypeManager.char_type)
1607 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1609 // One of the built-in conversions that belonged in the class library
1610 if (real_target_type == TypeManager.intptr_type){
1611 return new OperatorCast (new EmptyCast (expr, TypeManager.int64_type),
1612 TypeManager.intptr_type, true);
1614 } else if (expr_type == TypeManager.char_type){
1616 // From char to sbyte, byte, short
1618 if (real_target_type == TypeManager.sbyte_type)
1619 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1620 if (real_target_type == TypeManager.byte_type)
1621 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1622 if (real_target_type == TypeManager.short_type)
1623 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1624 } else if (expr_type == TypeManager.float_type){
1626 // From float to sbyte, byte, short,
1627 // ushort, int, uint, long, ulong, char
1630 if (real_target_type == TypeManager.sbyte_type)
1631 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1632 if (real_target_type == TypeManager.byte_type)
1633 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1634 if (real_target_type == TypeManager.short_type)
1635 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1636 if (real_target_type == TypeManager.ushort_type)
1637 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1638 if (real_target_type == TypeManager.int32_type)
1639 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1640 if (real_target_type == TypeManager.uint32_type)
1641 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1642 if (real_target_type == TypeManager.int64_type)
1643 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1644 if (real_target_type == TypeManager.uint64_type)
1645 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1646 if (real_target_type == TypeManager.char_type)
1647 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1648 if (real_target_type == TypeManager.decimal_type)
1649 return new CastToDecimal (expr, true);
1650 } else if (expr_type == TypeManager.double_type){
1652 // From double to sbyte, byte, short,
1653 // ushort, int, uint, long, ulong,
1654 // char, float or decimal
1656 if (real_target_type == TypeManager.sbyte_type)
1657 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1658 if (real_target_type == TypeManager.byte_type)
1659 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1660 if (real_target_type == TypeManager.short_type)
1661 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1662 if (real_target_type == TypeManager.ushort_type)
1663 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1664 if (real_target_type == TypeManager.int32_type)
1665 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1666 if (real_target_type == TypeManager.uint32_type)
1667 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1668 if (real_target_type == TypeManager.int64_type)
1669 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1670 if (real_target_type == TypeManager.uint64_type)
1671 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1672 if (real_target_type == TypeManager.char_type)
1673 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1674 if (real_target_type == TypeManager.float_type)
1675 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1676 if (real_target_type == TypeManager.decimal_type)
1677 return new CastToDecimal (expr, true);
1678 } else if (expr_type == TypeManager.uintptr_type){
1680 // Various built-in conversions that belonged in the class library
1682 // from uintptr to sbyte, short, int32
1684 if (real_target_type == TypeManager.sbyte_type){
1685 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1686 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1688 if (real_target_type == TypeManager.short_type){
1689 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1690 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1692 if (real_target_type == TypeManager.int32_type){
1693 return new EmptyCast (new OperatorCast (expr, TypeManager.uint32_type, true),
1694 TypeManager.int32_type);
1696 } else if (expr_type == TypeManager.intptr_type){
1697 if (real_target_type == TypeManager.uint64_type){
1698 return new EmptyCast (new OperatorCast (expr, TypeManager.int64_type, true),
1699 TypeManager.uint64_type);
1701 } else if (expr_type == TypeManager.decimal_type) {
1702 return new CastFromDecimal (expr, target_type).Resolve ();
1708 /// Returns whether an explicit reference conversion can be performed
1709 /// from source_type to target_type
1711 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1713 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1714 bool target_is_value_type = target_type.IsValueType;
1716 if (source_type == target_type)
1720 // From generic parameter to any type
1722 if (TypeManager.IsGenericParameter (source_type))
1723 return ExplicitTypeParameterConversionExists (source_type, target_type);
1726 // From object to a generic parameter
1728 if (source_type == TypeManager.object_type && target_is_type_param)
1732 // From object to any reference type
1734 if (source_type == TypeManager.object_type && !target_is_value_type)
1738 // From any class S to any class-type T, provided S is a base class of T
1740 if (TypeManager.IsSubclassOf (target_type, source_type))
1744 // From any interface type S to any interface T provided S is not derived from T
1746 if (source_type.IsInterface && target_type.IsInterface){
1747 if (!TypeManager.IsSubclassOf (target_type, source_type))
1752 // From any class type S to any interface T, provided S is not sealed
1753 // and provided S does not implement T.
1755 if (target_type.IsInterface && !source_type.IsSealed &&
1756 !TypeManager.ImplementsInterface (source_type, target_type))
1760 // From any interface-type S to to any class type T, provided T is not
1761 // sealed, or provided T implements S.
1763 if (source_type.IsInterface &&
1764 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1768 // From an array type S with an element type Se to an array type T with an
1769 // element type Te provided all the following are true:
1770 // * S and T differe only in element type, in other words, S and T
1771 // have the same number of dimensions.
1772 // * Both Se and Te are reference types
1773 // * An explicit referenc conversions exist from Se to Te
1775 if (source_type.IsArray && target_type.IsArray) {
1776 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1778 Type source_element_type = TypeManager.GetElementType (source_type);
1779 Type target_element_type = TypeManager.GetElementType (target_type);
1781 if (TypeManager.IsGenericParameter (source_element_type) ||
1782 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1783 if (ExplicitReferenceConversionExists (source_element_type,
1784 target_element_type))
1790 // From System.Array to any array-type
1791 if (source_type == TypeManager.array_type &&
1792 target_type.IsArray){
1797 // From System delegate to any delegate-type
1799 if (source_type == TypeManager.delegate_type &&
1800 TypeManager.IsDelegateType (target_type))
1804 // From ICloneable to Array or Delegate types
1806 if (source_type == TypeManager.icloneable_type &&
1807 (target_type == TypeManager.array_type ||
1808 target_type == TypeManager.delegate_type))
1815 /// Implements Explicit Reference conversions
1817 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1819 Type source_type = source.Type;
1820 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1821 bool target_is_value_type = target_type.IsValueType;
1824 // From object to a generic parameter
1826 if (source_type == TypeManager.object_type && target_is_type_param)
1827 return new UnboxCast (source, target_type);
1830 // Explicit type parameter conversion.
1833 if (TypeManager.IsGenericParameter (source_type))
1834 return ExplicitTypeParameterConversion (source, target_type);
1837 // From object to any reference type
1839 if (source_type == TypeManager.object_type && !target_is_value_type)
1840 return new ClassCast (source, target_type);
1843 // Unboxing conversion.
1845 if (((source_type == TypeManager.enum_type &&
1846 !(source is EmptyCast)) ||
1847 source_type == TypeManager.value_type) && target_is_value_type)
1848 return new UnboxCast (source, target_type);
1851 // From any class S to any class-type T, provided S is a base class of T
1853 if (TypeManager.IsSubclassOf (target_type, source_type))
1854 return new ClassCast (source, target_type);
1857 // From any interface type S to any interface T provided S is not derived from T
1859 if (source_type.IsInterface && target_type.IsInterface){
1860 if (TypeManager.ImplementsInterface (source_type, target_type))
1863 return new ClassCast (source, target_type);
1867 // From any class type S to any interface T, provides S is not sealed
1868 // and provided S does not implement T.
1870 if (target_type.IsInterface && !source_type.IsSealed) {
1871 if (TypeManager.ImplementsInterface (source_type, target_type))
1874 return new ClassCast (source, target_type);
1879 // From any interface-type S to to any class type T, provided T is not
1880 // sealed, or provided T implements S.
1882 if (source_type.IsInterface) {
1883 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1884 if (target_type.IsClass)
1885 return new ClassCast (source, target_type);
1887 return new UnboxCast (source, target_type);
1893 // From an array type S with an element type Se to an array type T with an
1894 // element type Te provided all the following are true:
1895 // * S and T differe only in element type, in other words, S and T
1896 // have the same number of dimensions.
1897 // * Both Se and Te are reference types
1898 // * An explicit referenc conversions exist from Se to Te
1900 if (source_type.IsArray && target_type.IsArray) {
1901 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1903 Type source_element_type = TypeManager.GetElementType (source_type);
1904 Type target_element_type = TypeManager.GetElementType (target_type);
1906 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1907 if (ExplicitReferenceConversionExists (source_element_type,
1908 target_element_type))
1909 return new ClassCast (source, target_type);
1914 // From System.Array to any array-type
1915 if (source_type == TypeManager.array_type &&
1916 target_type.IsArray) {
1917 return new ClassCast (source, target_type);
1921 // From System delegate to any delegate-type
1923 if (source_type == TypeManager.delegate_type &&
1924 TypeManager.IsDelegateType (target_type))
1925 return new ClassCast (source, target_type);
1928 // From ICloneable to Array or Delegate types
1930 if (source_type == TypeManager.icloneable_type &&
1931 (target_type == TypeManager.array_type ||
1932 target_type == TypeManager.delegate_type))
1933 return new ClassCast (source, target_type);
1939 /// Performs an explicit conversion of the expression `expr' whose
1940 /// type is expr.Type to `target_type'.
1942 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1943 Type target_type, Location loc)
1945 Type expr_type = expr.Type;
1947 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1948 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1953 // Unboxing conversions; only object types can be convertible to enum
1955 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1956 return new UnboxCast (expr, target_type);
1958 if (TypeManager.IsEnumType (expr_type)) {
1959 if (expr is EnumConstant)
1960 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1962 return ExplicitConversionCore (ec, new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1965 if (TypeManager.IsEnumType (target_type)){
1966 if (expr_type == TypeManager.enum_type)
1967 return new UnboxCast (expr, target_type);
1969 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1971 return new EmptyCast (ce, target_type);
1974 ne = ExplicitNumericConversion (expr, target_type);
1979 // Skip the ExplicitReferenceConversion because we can not convert
1980 // from Null to a ValueType, and ExplicitReference wont check against
1981 // null literal explicitly
1983 if (expr_type != TypeManager.null_type){
1984 ne = ExplicitReferenceConversion (expr, target_type);
1990 ne = ExplicitUnsafe (expr, target_type);
1995 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2002 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
2004 Type expr_type = expr.Type;
2006 if (target_type.IsPointer){
2007 if (expr_type.IsPointer)
2008 return new EmptyCast (expr, target_type);
2010 if (expr_type == TypeManager.sbyte_type ||
2011 expr_type == TypeManager.short_type ||
2012 expr_type == TypeManager.int32_type ||
2013 expr_type == TypeManager.int64_type)
2014 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
2016 if (expr_type == TypeManager.ushort_type ||
2017 expr_type == TypeManager.uint32_type ||
2018 expr_type == TypeManager.uint64_type ||
2019 expr_type == TypeManager.byte_type)
2020 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2023 if (expr_type.IsPointer){
2024 if (target_type == TypeManager.sbyte_type)
2025 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2026 else if (target_type == TypeManager.byte_type)
2027 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2028 else if (target_type == TypeManager.short_type)
2029 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2030 else if (target_type == TypeManager.ushort_type)
2031 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2032 else if (target_type == TypeManager.int32_type)
2033 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2034 else if (target_type == TypeManager.uint32_type)
2035 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2036 else if (target_type == TypeManager.uint64_type)
2037 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2038 else if (target_type == TypeManager.int64_type){
2039 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2046 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2048 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2049 Type target_type, Location l)
2051 int errors = Report.Errors;
2052 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2053 if (Report.Errors > errors)
2059 ne = ExplicitNumericConversion (expr, target_type);
2063 ne = ExplicitReferenceConversion (expr, target_type);
2067 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2068 return new EmptyCast (expr, target_type);
2070 expr.Error_ValueCannotBeConverted (l, target_type, true);
2075 /// Performs an explicit conversion of the expression `expr' whose
2076 /// type is expr.Type to `target_type'.
2078 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2079 Type target_type, Location loc)
2083 Type expr_type = expr.Type;
2084 if (TypeManager.IsNullableType (target_type)) {
2085 if (TypeManager.IsNullableType (expr_type)) {
2086 e = new Nullable.LiftedConversion (
2087 expr, target_type, false, true, loc).Resolve (ec);
2090 } else if (expr_type == TypeManager.object_type) {
2091 return new UnboxCast (expr, target_type);
2093 Type target = TypeManager.GetTypeArguments (target_type) [0];
2095 e = ExplicitConversionCore (ec, expr, target, loc);
2097 return Nullable.Wrap.Create (e, ec);
2099 } else if (TypeManager.IsNullableType (expr_type)) {
2100 Expression source = Nullable.Unwrap.Create (expr, ec);
2101 if (source != null) {
2102 e = ExplicitConversionCore (ec, source, target_type, loc);
2108 e = ExplicitConversionCore (ec, expr, target_type, loc);
2112 expr.Error_ValueCannotBeConverted (loc, target_type, true);