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 // This is used to prettify the code: a null argument is allowed
24 // for ImplicitStandardConversion as long as it is known that
25 // no anonymous method will play a role.
27 // FIXME: renamed from `const' to `static' to allow bootstraping from older
28 // versions of the compiler that could not cope with this construct.
30 public static EmitContext ConstantEC = null;
32 static public void Error_CannotConvertType (Location loc, Type source, Type target)
34 Report.Error (30, loc, "Cannot convert type '" +
35 TypeManager.CSharpName (source) + "' to '" +
36 TypeManager.CSharpName (target) + "'");
39 static Expression TypeParameter_to_Null (Expression expr, Type target_type,
42 if (!TypeParameter_to_Null (target_type)) {
43 Report.Error (403, loc, "Cannot convert null to the type " +
44 "parameter `{0}' becaues it could be a value " +
45 "type. Consider using `default ({0})' instead.",
50 return new NullCast (expr, target_type);
53 static bool TypeParameter_to_Null (Type target_type)
55 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
59 if (gc.HasReferenceTypeConstraint)
61 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
67 static Type TypeParam_EffectiveBaseType (EmitContext ec, Type t)
69 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
71 return TypeManager.object_type;
73 return TypeParam_EffectiveBaseType (ec, gc);
76 static Type TypeParam_EffectiveBaseType (EmitContext ec, GenericConstraints gc)
78 ArrayList list = new ArrayList ();
79 list.Add (gc.EffectiveBaseClass);
80 foreach (Type t in gc.InterfaceConstraints) {
81 if (!t.IsGenericParameter)
84 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
86 list.Add (TypeParam_EffectiveBaseType (ec, new_gc));
88 return FindMostEncompassedType (ec, list);
91 static Expression TypeParameterConversion (Expression expr, bool is_reference, Type target_type)
94 return new EmptyCast (expr, target_type);
96 return new BoxedCast (expr, target_type);
99 static Expression ImplicitTypeParameterConversion (EmitContext ec, Expression expr,
102 Type expr_type = expr.Type;
104 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
107 if (target_type == TypeManager.object_type)
108 return new BoxedCast (expr);
113 // We're converting from a type parameter which is known to be a reference type.
114 bool is_reference = gc.IsReferenceType;
115 Type base_type = TypeParam_EffectiveBaseType (ec, gc);
117 if (TypeManager.IsSubclassOf (base_type, target_type))
118 return TypeParameterConversion (expr, is_reference, target_type);
120 if (target_type.IsInterface) {
121 if (TypeManager.ImplementsInterface (base_type, target_type))
122 return TypeParameterConversion (expr, is_reference, target_type);
124 foreach (Type t in gc.InterfaceConstraints) {
125 if (TypeManager.IsSubclassOf (t, target_type))
126 return TypeParameterConversion (expr, is_reference, target_type);
130 foreach (Type t in gc.InterfaceConstraints) {
131 if (!t.IsGenericParameter)
133 if (TypeManager.IsSubclassOf (t, target_type))
134 return TypeParameterConversion (expr, is_reference, target_type);
140 static EmptyExpression MyEmptyExpr;
141 static public Expression ImplicitReferenceConversion (EmitContext ec, Expression expr, Type target_type)
143 Type expr_type = expr.Type;
145 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
146 // if we are a method group, emit a warning
151 if (expr_type == TypeManager.void_type)
154 if (expr_type.IsGenericParameter)
155 return ImplicitTypeParameterConversion (ec, expr, target_type);
158 // notice that it is possible to write "ValueType v = 1", the ValueType here
159 // is an abstract class, and not really a value type, so we apply the same rules.
161 if (target_type == TypeManager.object_type) {
163 // A pointer type cannot be converted to object
165 if (expr_type.IsPointer)
168 if (TypeManager.IsValueType (expr_type))
169 return new BoxedCast (expr);
170 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
171 if (expr_type == TypeManager.anonymous_method_type)
173 return new EmptyCast (expr, target_type);
177 } else if (target_type == TypeManager.value_type) {
178 if (TypeManager.IsValueType (expr_type))
179 return new BoxedCast (expr);
180 if (expr_type == TypeManager.null_type)
181 return new NullCast (expr, target_type);
184 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
186 // Special case: enumeration to System.Enum.
187 // System.Enum is not a value type, it is a class, so we need
188 // a boxing conversion
190 if (expr_type.IsEnum || expr_type.IsGenericParameter)
191 return new BoxedCast (expr);
193 return new EmptyCast (expr, target_type);
196 // This code is kind of mirrored inside ImplicitStandardConversionExists
197 // with the small distinction that we only probe there
199 // Always ensure that the code here and there is in sync
201 // from the null type to any reference-type.
202 if (expr_type == TypeManager.null_type){
203 if (target_type.IsPointer)
204 return new EmptyCast (expr, target_type);
206 if (!target_type.IsValueType)
207 return new NullCast (expr, target_type);
210 // from any class-type S to any interface-type T.
211 if (target_type.IsInterface) {
212 if (target_type != TypeManager.iconvertible_type &&
213 expr_type.IsValueType && (expr is Constant) &&
214 !(expr is IntLiteral || expr is BoolLiteral ||
215 expr is FloatLiteral || expr is DoubleLiteral ||
216 expr is LongLiteral || expr is CharLiteral ||
217 expr is StringLiteral || expr is DecimalLiteral ||
218 expr is UIntLiteral || expr is ULongLiteral)) {
222 if (TypeManager.ImplementsInterface (expr_type, target_type)){
223 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
224 return new BoxedCast (expr, target_type);
226 return new EmptyCast (expr, target_type);
230 // from any interface type S to interface-type T.
231 if (expr_type.IsInterface && target_type.IsInterface) {
232 if (TypeManager.ImplementsInterface (expr_type, target_type))
233 return new EmptyCast (expr, target_type);
238 // from an array-type S to an array-type of type T
239 if (expr_type.IsArray && target_type.IsArray) {
240 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
242 Type expr_element_type = TypeManager.GetElementType (expr_type);
244 if (MyEmptyExpr == null)
245 MyEmptyExpr = new EmptyExpression ();
247 MyEmptyExpr.SetType (expr_element_type);
248 Type target_element_type = TypeManager.GetElementType (target_type);
250 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
251 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
252 target_element_type))
253 return new EmptyCast (expr, target_type);
257 // from an array-type to System.Array
258 if (expr_type.IsArray && target_type == TypeManager.array_type)
259 return new EmptyCast (expr, target_type);
261 // from an array-type of type T to IEnumerable<T>
262 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
263 return new EmptyCast (expr, target_type);
265 // from any delegate type to System.Delegate
266 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
267 target_type == TypeManager.delegate_type)
268 return new EmptyCast (expr, target_type);
270 // from any array-type or delegate type into System.ICloneable.
271 if (expr_type.IsArray ||
272 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
273 if (target_type == TypeManager.icloneable_type)
274 return new EmptyCast (expr, target_type);
276 // from a generic type definition to a generic instance.
277 if (TypeManager.IsEqual (expr_type, target_type))
278 return new EmptyCast (expr, target_type);
284 // Tests whether an implicit reference conversion exists between expr_type
287 public static bool ImplicitReferenceConversionExists (EmitContext ec, Expression expr, Type target_type)
289 Type expr_type = expr.Type;
291 if (expr_type.IsGenericParameter)
292 return ImplicitTypeParameterConversion (ec, expr, target_type) != null;
295 // This is the boxed case.
297 if (target_type == TypeManager.object_type) {
298 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
299 expr_type.IsInterface || expr_type == TypeManager.enum_type)
300 if (target_type != TypeManager.anonymous_method_type)
304 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
307 // Please remember that all code below actually comes
308 // from ImplicitReferenceConversion so make sure code remains in sync
310 // from any class-type S to any interface-type T.
311 if (target_type.IsInterface) {
312 if (target_type != TypeManager.iconvertible_type &&
313 expr_type.IsValueType && (expr is Constant) &&
314 !(expr is IntLiteral || expr is BoolLiteral ||
315 expr is FloatLiteral || expr is DoubleLiteral ||
316 expr is LongLiteral || expr is CharLiteral ||
317 expr is StringLiteral || expr is DecimalLiteral ||
318 expr is UIntLiteral || expr is ULongLiteral)) {
322 if (TypeManager.ImplementsInterface (expr_type, target_type))
326 // from any interface type S to interface-type T.
327 if (expr_type.IsInterface && target_type.IsInterface)
328 if (TypeManager.ImplementsInterface (expr_type, target_type))
331 // from an array-type S to an array-type of type T
332 if (expr_type.IsArray && target_type.IsArray) {
333 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
335 Type expr_element_type = expr_type.GetElementType ();
337 if (MyEmptyExpr == null)
338 MyEmptyExpr = new EmptyExpression ();
340 MyEmptyExpr.SetType (expr_element_type);
341 Type target_element_type = TypeManager.GetElementType (target_type);
343 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
344 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
345 target_element_type))
350 // from an array-type to System.Array
351 if (expr_type.IsArray && (target_type == TypeManager.array_type))
354 // from an array-type of type T to IEnumerable<T>
355 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
358 // from any delegate type to System.Delegate
359 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
360 target_type == TypeManager.delegate_type)
361 if (target_type.IsAssignableFrom (expr_type))
364 // from any array-type or delegate type into System.ICloneable.
365 if (expr_type.IsArray ||
366 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
367 if (target_type == TypeManager.icloneable_type)
370 // from the null type to any reference-type.
371 if (expr_type == TypeManager.null_type){
372 if (target_type.IsPointer)
375 if (!target_type.IsValueType)
379 // from a generic type definition to a generic instance.
380 if (TypeManager.IsEqual (expr_type, target_type))
387 /// Implicit Numeric Conversions.
389 /// expr is the expression to convert, returns a new expression of type
390 /// target_type or null if an implicit conversion is not possible.
392 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
393 Type target_type, Location loc)
395 Type expr_type = expr.Type;
398 // Attempt to do the implicit constant expression conversions
400 if (expr is Constant){
401 if (expr is IntConstant){
404 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
408 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
410 // Try the implicit constant expression conversion
411 // from long to ulong, instead of a nice routine,
414 long v = ((LongConstant) expr).Value;
416 return new ULongConstant ((ulong) v);
420 Type real_target_type = target_type;
422 if (expr_type == TypeManager.sbyte_type){
424 // From sbyte to short, int, long, float, double.
426 if (real_target_type == TypeManager.int32_type)
427 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
428 if (real_target_type == TypeManager.int64_type)
429 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
430 if (real_target_type == TypeManager.double_type)
431 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
432 if (real_target_type == TypeManager.float_type)
433 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
434 if (real_target_type == TypeManager.short_type)
435 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
436 } else if (expr_type == TypeManager.byte_type){
438 // From byte to short, ushort, int, uint, long, ulong, float, double
440 if ((real_target_type == TypeManager.short_type) ||
441 (real_target_type == TypeManager.ushort_type) ||
442 (real_target_type == TypeManager.int32_type) ||
443 (real_target_type == TypeManager.uint32_type))
444 return new EmptyCast (expr, target_type);
446 if (real_target_type == TypeManager.uint64_type)
447 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
448 if (real_target_type == TypeManager.int64_type)
449 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
450 if (real_target_type == TypeManager.float_type)
451 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
452 if (real_target_type == TypeManager.double_type)
453 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
454 } else if (expr_type == TypeManager.short_type){
456 // From short to int, long, float, double
458 if (real_target_type == TypeManager.int32_type)
459 return new EmptyCast (expr, target_type);
460 if (real_target_type == TypeManager.int64_type)
461 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
462 if (real_target_type == TypeManager.double_type)
463 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
464 if (real_target_type == TypeManager.float_type)
465 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
466 } else if (expr_type == TypeManager.ushort_type){
468 // From ushort to int, uint, long, ulong, float, double
470 if (real_target_type == TypeManager.uint32_type)
471 return new EmptyCast (expr, target_type);
473 if (real_target_type == TypeManager.uint64_type)
474 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
475 if (real_target_type == TypeManager.int32_type)
476 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
477 if (real_target_type == TypeManager.int64_type)
478 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
479 if (real_target_type == TypeManager.double_type)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
481 if (real_target_type == TypeManager.float_type)
482 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
483 } else if (expr_type == TypeManager.int32_type){
485 // From int to long, float, double
487 if (real_target_type == TypeManager.int64_type)
488 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
489 if (real_target_type == TypeManager.double_type)
490 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
491 if (real_target_type == TypeManager.float_type)
492 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
493 } else if (expr_type == TypeManager.uint32_type){
495 // From uint to long, ulong, float, double
497 if (real_target_type == TypeManager.int64_type)
498 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
499 if (real_target_type == TypeManager.uint64_type)
500 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
501 if (real_target_type == TypeManager.double_type)
502 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
504 if (real_target_type == TypeManager.float_type)
505 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
507 } else if (expr_type == TypeManager.int64_type){
509 // From long/ulong to float, double
511 if (real_target_type == TypeManager.double_type)
512 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
513 if (real_target_type == TypeManager.float_type)
514 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
515 } else if (expr_type == TypeManager.uint64_type){
517 // From ulong to float, double
519 if (real_target_type == TypeManager.double_type)
520 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
522 if (real_target_type == TypeManager.float_type)
523 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
525 } else if (expr_type == TypeManager.char_type){
527 // From char to ushort, int, uint, long, ulong, float, double
529 if ((real_target_type == TypeManager.ushort_type) ||
530 (real_target_type == TypeManager.int32_type) ||
531 (real_target_type == TypeManager.uint32_type))
532 return new EmptyCast (expr, target_type);
533 if (real_target_type == TypeManager.uint64_type)
534 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
535 if (real_target_type == TypeManager.int64_type)
536 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
537 if (real_target_type == TypeManager.float_type)
538 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
539 if (real_target_type == TypeManager.double_type)
540 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
541 } else if (expr_type == TypeManager.float_type){
545 if (real_target_type == TypeManager.double_type)
546 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
554 /// Same as ImplicitStandardConversionExists except that it also looks at
555 /// implicit user defined conversions - needed for overload resolution
557 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
559 if (expr is NullLiteral) {
560 if (target_type.IsGenericParameter)
561 return TypeParameter_to_Null (target_type);
563 if (TypeManager.IsNullableType (target_type))
567 if (ImplicitStandardConversionExists (ec, expr, target_type))
570 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
578 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
580 Expression dummy = ImplicitUserConversion (
581 ec, new EmptyExpression (source), target, Location.Null);
582 return dummy != null;
586 /// Determines if a standard implicit conversion exists from
587 /// expr_type to target_type
589 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
591 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
593 Type expr_type = expr.Type;
595 if (expr_type == TypeManager.void_type)
598 //Console.WriteLine ("Expr is {0}", expr);
599 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
600 if (expr_type.Equals (target_type))
604 // First numeric conversions
606 if (expr_type == TypeManager.sbyte_type){
608 // From sbyte to short, int, long, float, double.
610 if ((target_type == TypeManager.int32_type) ||
611 (target_type == TypeManager.int64_type) ||
612 (target_type == TypeManager.double_type) ||
613 (target_type == TypeManager.float_type) ||
614 (target_type == TypeManager.short_type) ||
615 (target_type == TypeManager.decimal_type))
618 } else if (expr_type == TypeManager.byte_type){
620 // From byte to short, ushort, int, uint, long, ulong, float, double
622 if ((target_type == TypeManager.short_type) ||
623 (target_type == TypeManager.ushort_type) ||
624 (target_type == TypeManager.int32_type) ||
625 (target_type == TypeManager.uint32_type) ||
626 (target_type == TypeManager.uint64_type) ||
627 (target_type == TypeManager.int64_type) ||
628 (target_type == TypeManager.float_type) ||
629 (target_type == TypeManager.double_type) ||
630 (target_type == TypeManager.decimal_type))
633 } else if (expr_type == TypeManager.short_type){
635 // From short to int, long, float, double
637 if ((target_type == TypeManager.int32_type) ||
638 (target_type == TypeManager.int64_type) ||
639 (target_type == TypeManager.double_type) ||
640 (target_type == TypeManager.float_type) ||
641 (target_type == TypeManager.decimal_type))
644 } else if (expr_type == TypeManager.ushort_type){
646 // From ushort to int, uint, long, ulong, float, double
648 if ((target_type == TypeManager.uint32_type) ||
649 (target_type == TypeManager.uint64_type) ||
650 (target_type == TypeManager.int32_type) ||
651 (target_type == TypeManager.int64_type) ||
652 (target_type == TypeManager.double_type) ||
653 (target_type == TypeManager.float_type) ||
654 (target_type == TypeManager.decimal_type))
657 } else if (expr_type == TypeManager.int32_type){
659 // From int to long, float, double
661 if ((target_type == TypeManager.int64_type) ||
662 (target_type == TypeManager.double_type) ||
663 (target_type == TypeManager.float_type) ||
664 (target_type == TypeManager.decimal_type))
667 } else if (expr_type == TypeManager.uint32_type){
669 // From uint to long, ulong, float, double
671 if ((target_type == TypeManager.int64_type) ||
672 (target_type == TypeManager.uint64_type) ||
673 (target_type == TypeManager.double_type) ||
674 (target_type == TypeManager.float_type) ||
675 (target_type == TypeManager.decimal_type))
678 } else if ((expr_type == TypeManager.uint64_type) ||
679 (expr_type == TypeManager.int64_type)) {
681 // From long/ulong to float, double
683 if ((target_type == TypeManager.double_type) ||
684 (target_type == TypeManager.float_type) ||
685 (target_type == TypeManager.decimal_type))
688 } else if (expr_type == TypeManager.char_type){
690 // From char to ushort, int, uint, long, ulong, float, double
692 if ((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.float_type){
706 if (target_type == TypeManager.double_type)
710 if (expr.eclass == ExprClass.MethodGroup){
711 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
712 MethodGroupExpr mg = expr as MethodGroupExpr;
715 // This should not happen frequently, so we can create an object
716 // to test compatibility
718 Expression c = ImplicitDelegateCreation.Create (ec, mg, target_type, Location.Null);
724 if (ImplicitReferenceConversionExists (ec, expr, target_type))
728 // Implicit Constant Expression Conversions
730 if (expr is IntConstant){
731 int value = ((IntConstant) expr).Value;
733 if (target_type == TypeManager.sbyte_type){
734 if (value >= SByte.MinValue && value <= SByte.MaxValue)
736 } else if (target_type == TypeManager.byte_type){
737 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
739 } else if (target_type == TypeManager.short_type){
740 if (value >= Int16.MinValue && value <= Int16.MaxValue)
742 } else if (target_type == TypeManager.ushort_type){
743 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
745 } else if (target_type == TypeManager.uint32_type){
748 } else if (target_type == TypeManager.uint64_type){
750 // we can optimize this case: a positive int32
751 // always fits on a uint64. But we need an opcode
758 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
762 if (expr is LongConstant && target_type == TypeManager.uint64_type){
764 // Try the implicit constant expression conversion
765 // from long to ulong, instead of a nice routine,
768 long v = ((LongConstant) expr).Value;
773 if ((target_type == TypeManager.enum_type ||
774 target_type.IsSubclassOf (TypeManager.enum_type)) &&
776 IntLiteral i = (IntLiteral) expr;
783 // If `expr_type' implements `target_type' (which is an iface)
784 // see TryImplicitIntConversion
786 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
789 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
792 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
795 if (expr_type == TypeManager.anonymous_method_type){
796 if (!TypeManager.IsDelegateType (target_type))
799 AnonymousMethod am = (AnonymousMethod) expr;
801 Expression conv = am.Compatible (ec, target_type, true);
810 // Used internally by FindMostEncompassedType, this is used
811 // to avoid creating lots of objects in the tight loop inside
812 // FindMostEncompassedType
814 static EmptyExpression priv_fmet_param;
817 /// Finds "most encompassed type" according to the spec (13.4.2)
818 /// amongst the methods in the MethodGroupExpr
820 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
824 if (priv_fmet_param == null)
825 priv_fmet_param = new EmptyExpression ();
827 foreach (Type t in types){
828 priv_fmet_param.SetType (t);
835 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
843 // Used internally by FindMostEncompassingType, this is used
844 // to avoid creating lots of objects in the tight loop inside
845 // FindMostEncompassingType
847 static EmptyExpression priv_fmee_ret;
850 /// Finds "most encompassing type" according to the spec (13.4.2)
851 /// amongst the types in the given set
853 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
857 if (priv_fmee_ret == null)
858 priv_fmee_ret = new EmptyExpression ();
860 foreach (Type t in types){
861 priv_fmee_ret.SetType (best);
868 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
876 // Used to avoid creating too many objects
878 static EmptyExpression priv_fms_expr;
881 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
882 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
883 /// for explicit and implicit conversion operators.
885 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
886 Expression source, bool apply_explicit_conv_rules,
889 ArrayList src_types_set = new ArrayList ();
891 if (priv_fms_expr == null)
892 priv_fms_expr = new EmptyExpression ();
895 // If any operator converts from S then Sx = S
897 Type source_type = source.Type;
898 foreach (MethodBase mb in me.Methods){
899 ParameterData pd = TypeManager.GetParameterData (mb);
900 Type param_type = pd.ParameterType (0);
902 if (param_type == source_type)
905 if (apply_explicit_conv_rules) {
908 // Find the set of applicable user-defined conversion operators, U. This set
910 // user-defined implicit or explicit conversion operators declared by
911 // the classes or structs in D that convert from a type encompassing
912 // or encompassed by S to a type encompassing or encompassed by T
914 priv_fms_expr.SetType (param_type);
915 if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
916 src_types_set.Add (param_type);
918 if (ImplicitStandardConversionExists (ec, source, param_type))
919 src_types_set.Add (param_type);
923 // Only if S is encompassed by param_type
925 if (ImplicitStandardConversionExists (ec, source, param_type))
926 src_types_set.Add (param_type);
931 // Explicit Conv rules
933 if (apply_explicit_conv_rules) {
934 ArrayList candidate_set = new ArrayList ();
936 foreach (Type param_type in src_types_set){
937 if (ImplicitStandardConversionExists (ec, source, param_type))
938 candidate_set.Add (param_type);
941 if (candidate_set.Count != 0)
942 return FindMostEncompassedType (ec, candidate_set);
948 if (apply_explicit_conv_rules)
949 return FindMostEncompassingType (ec, src_types_set);
951 return FindMostEncompassedType (ec, src_types_set);
955 // Useful in avoiding proliferation of objects
957 static EmptyExpression priv_fmt_expr;
960 /// Finds the most specific target Tx according to section 13.4.4
962 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
963 Type target, bool apply_explicit_conv_rules,
966 ArrayList tgt_types_set = new ArrayList ();
968 if (priv_fmt_expr == null)
969 priv_fmt_expr = new EmptyExpression ();
972 // If any operator converts to T then Tx = T
974 foreach (MethodInfo mi in me.Methods){
975 Type ret_type = mi.ReturnType;
977 if (ret_type == target)
980 if (apply_explicit_conv_rules) {
983 // Find the set of applicable user-defined conversion operators, U.
985 // This set consists of the
986 // user-defined implicit or explicit conversion operators declared by
987 // the classes or structs in D that convert from a type encompassing
988 // or encompassed by S to a type encompassing or encompassed by T
990 priv_fms_expr.SetType (ret_type);
991 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
992 tgt_types_set.Add (ret_type);
994 priv_fms_expr.SetType (target);
995 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
996 tgt_types_set.Add (ret_type);
1000 // Only if T is encompassed by param_type
1002 priv_fms_expr.SetType (ret_type);
1003 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
1004 tgt_types_set.Add (ret_type);
1009 // Explicit conv rules
1011 if (apply_explicit_conv_rules) {
1012 ArrayList candidate_set = new ArrayList ();
1014 foreach (Type ret_type in tgt_types_set){
1015 priv_fmt_expr.SetType (ret_type);
1017 if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
1018 candidate_set.Add (ret_type);
1021 if (candidate_set.Count != 0)
1022 return FindMostEncompassingType (ec, candidate_set);
1026 // Okay, final case !
1028 if (apply_explicit_conv_rules)
1029 return FindMostEncompassedType (ec, tgt_types_set);
1031 return FindMostEncompassingType (ec, tgt_types_set);
1035 /// User-defined Implicit conversions
1037 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1038 Type target, Location loc)
1040 return UserDefinedConversion (ec, source, target, loc, false);
1044 /// User-defined Explicit conversions
1046 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1047 Type target, Location loc)
1049 return UserDefinedConversion (ec, source, target, loc, true);
1052 static DoubleHash explicit_conv = new DoubleHash (100);
1053 static DoubleHash implicit_conv = new DoubleHash (100);
1055 /// Computes the MethodGroup for the user-defined conversion
1056 /// operators from source_type to target_type. `look_for_explicit'
1057 /// controls whether we should also include the list of explicit
1060 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1061 Type source_type, Type target_type,
1062 Location loc, bool look_for_explicit)
1064 Expression mg1 = null, mg2 = null;
1065 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1068 op_name = "op_Implicit";
1070 MethodGroupExpr union3;
1072 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1073 return (MethodGroupExpr) r;
1075 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1076 if (source_type.BaseType != null)
1077 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1080 union3 = (MethodGroupExpr) mg2;
1081 else if (mg2 == null)
1082 union3 = (MethodGroupExpr) mg1;
1084 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1086 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1089 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1091 union3 = (MethodGroupExpr) mg1;
1094 if (target_type.BaseType != null)
1095 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1099 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1101 union3 = (MethodGroupExpr) mg1;
1104 MethodGroupExpr union4 = null;
1106 if (look_for_explicit) {
1107 op_name = "op_Explicit";
1109 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1110 if (source_type.BaseType != null)
1111 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1113 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1114 if (target_type.BaseType != null)
1115 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1117 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1118 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1120 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1123 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1124 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1129 /// User-defined conversions
1131 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1132 Type target, Location loc,
1133 bool look_for_explicit)
1135 MethodGroupExpr union;
1136 Type source_type = source.Type;
1137 MethodBase method = null;
1139 if (TypeManager.IsNullableType (source_type) && TypeManager.IsNullableType (target))
1140 return new Nullable.LiftedConversion (
1141 source, target, true, look_for_explicit, loc).Resolve (ec);
1143 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1147 Type most_specific_source, most_specific_target;
1149 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1150 if (most_specific_source == null)
1153 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1154 if (most_specific_target == null)
1160 foreach (MethodBase mb in union.Methods){
1161 ParameterData pd = TypeManager.GetParameterData (mb);
1162 MethodInfo mi = (MethodInfo) mb;
1164 if (pd.ParameterType (0) == most_specific_source &&
1165 mi.ReturnType == most_specific_target) {
1171 if (method == null || count > 1)
1176 // This will do the conversion to the best match that we
1177 // found. Now we need to perform an implict standard conversion
1178 // if the best match was not the type that we were requested
1181 if (look_for_explicit)
1182 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1184 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1190 e = new UserCast ((MethodInfo) method, source, loc);
1191 if (e.Type != target){
1192 if (!look_for_explicit)
1193 e = ImplicitConversionStandard (ec, e, target, loc);
1195 e = ExplicitConversionStandard (ec, e, target, loc);
1202 /// Converts implicitly the resolved expression `expr' into the
1203 /// `target_type'. It returns a new expression that can be used
1204 /// in a context that expects a `target_type'.
1206 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1207 Type target_type, Location loc)
1211 if (target_type == null)
1212 throw new Exception ("Target type is null");
1214 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1218 e = ImplicitUserConversion (ec, expr, target_type, loc);
1227 /// Attempts to apply the `Standard Implicit
1228 /// Conversion' rules to the expression `expr' into
1229 /// the `target_type'. It returns a new expression
1230 /// that can be used in a context that expects a
1233 /// This is different from `ImplicitConversion' in that the
1234 /// user defined implicit conversions are excluded.
1236 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1237 Type target_type, Location loc)
1239 Type expr_type = expr.Type;
1242 if (expr is NullLiteral) {
1243 if (target_type.IsGenericParameter)
1244 return TypeParameter_to_Null (expr, target_type, loc);
1246 if (TypeManager.IsNullableType (target_type))
1247 return new Nullable.NullableLiteral (target_type, loc);
1250 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1251 return new Nullable.LiftedConversion (
1252 expr, target_type, false, false, loc).Resolve (ec);
1254 if (expr.eclass == ExprClass.MethodGroup){
1255 if (!TypeManager.IsDelegateType (target_type)){
1260 // Only allow anonymous method conversions on post ISO_1
1262 if (RootContext.Version != LanguageVersion.ISO_1){
1263 MethodGroupExpr mg = expr as MethodGroupExpr;
1265 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1269 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1272 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1276 e = ImplicitReferenceConversion (ec, expr, target_type);
1280 if ((target_type == TypeManager.enum_type ||
1281 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1282 expr is IntLiteral){
1283 IntLiteral i = (IntLiteral) expr;
1286 return new EnumConstant ((Constant) expr, target_type);
1290 if (expr_type.IsPointer){
1291 if (target_type == TypeManager.void_ptr_type)
1292 return new EmptyCast (expr, target_type);
1295 // yep, comparing pointer types cant be done with
1296 // t1 == t2, we have to compare their element types.
1298 if (target_type.IsPointer){
1299 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1304 if (target_type.IsPointer) {
1305 if (expr_type == TypeManager.null_type)
1306 return new EmptyCast (expr, target_type);
1308 if (expr_type == TypeManager.void_ptr_type)
1309 return new EmptyCast (expr, target_type);
1313 if (expr_type == TypeManager.anonymous_method_type){
1314 if (!TypeManager.IsDelegateType (target_type)){
1315 Report.Error (1660, loc,
1316 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1317 TypeManager.CSharpName (target_type));
1321 AnonymousMethod am = (AnonymousMethod) expr;
1322 int errors = Report.Errors;
1324 Expression conv = am.Compatible (ec, target_type, false);
1329 // We return something instead of null, to avoid
1330 // the duplicate error, since am.Compatible would have
1331 // reported that already
1333 if (errors != Report.Errors)
1334 return new EmptyCast (expr, target_type);
1341 /// Attempts to perform an implicit constant conversion of the IntConstant
1342 /// into a different data type using casts (See Implicit Constant
1343 /// Expression Conversions)
1345 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1347 int value = ic.Value;
1349 if (target_type == TypeManager.sbyte_type){
1350 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1351 return new SByteConstant ((sbyte) value);
1352 } else if (target_type == TypeManager.byte_type){
1353 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1354 return new ByteConstant ((byte) value);
1355 } else if (target_type == TypeManager.short_type){
1356 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1357 return new ShortConstant ((short) value);
1358 } else if (target_type == TypeManager.ushort_type){
1359 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1360 return new UShortConstant ((ushort) value);
1361 } else if (target_type == TypeManager.uint32_type){
1363 return new UIntConstant ((uint) value);
1364 } else if (target_type == TypeManager.uint64_type){
1366 // we can optimize this case: a positive int32
1367 // always fits on a uint64. But we need an opcode
1371 return new ULongConstant ((ulong) value);
1372 } else if (target_type == TypeManager.double_type)
1373 return new DoubleConstant ((double) value);
1374 else if (target_type == TypeManager.float_type)
1375 return new FloatConstant ((float) value);
1377 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1378 Type underlying = TypeManager.EnumToUnderlying (target_type);
1379 Constant e = (Constant) ic;
1382 // Possibly, we need to create a different 0 literal before passing
1385 if (underlying == TypeManager.int64_type)
1386 e = new LongLiteral (0);
1387 else if (underlying == TypeManager.uint64_type)
1388 e = new ULongLiteral (0);
1390 return new EnumConstant (e, target_type);
1394 // If `target_type' is an interface and the type of `ic' implements the interface
1395 // e.g. target_type is IComparable, IConvertible, IFormattable
1397 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1398 return new BoxedCast (ic);
1403 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1405 if (source.Name == target.Name){
1406 Report.ExtraInformation (loc,
1408 "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
1409 source.Name, source.Assembly.FullName, target.Assembly.FullName));
1412 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1413 source == TypeManager.anonymous_method_type ?
1414 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1415 TypeManager.CSharpName (target));
1419 /// Attempts to implicitly convert `source' into `target_type', using
1420 /// ImplicitConversion. If there is no implicit conversion, then
1421 /// an error is signaled
1423 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1424 Type target_type, Location loc)
1428 int errors = Report.Errors;
1429 e = ImplicitConversion (ec, source, target_type, loc);
1430 if (Report.Errors > errors)
1435 if (source is DoubleLiteral) {
1436 if (target_type == TypeManager.float_type) {
1437 Error_664 (loc, "float", "f");
1440 if (target_type == TypeManager.decimal_type) {
1441 Error_664 (loc, "decimal", "m");
1446 if (source is Constant){
1447 Constant c = (Constant) source;
1449 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1453 Error_CannotImplicitConversion (loc, source.Type, target_type);
1458 static void Error_664 (Location loc, string type, string suffix) {
1459 Report.Error (664, loc,
1460 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1465 /// Performs the explicit numeric conversions
1467 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1469 Type expr_type = expr.Type;
1472 // If we have an enumeration, extract the underlying type,
1473 // use this during the comparison, but wrap around the original
1476 Type real_target_type = target_type;
1478 if (TypeManager.IsEnumType (real_target_type))
1479 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1481 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1482 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1484 if (real_target_type != target_type)
1485 return new EmptyCast (ce, target_type);
1489 if (expr_type == TypeManager.sbyte_type){
1491 // From sbyte to byte, ushort, uint, ulong, char
1493 if (real_target_type == TypeManager.byte_type)
1494 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1495 if (real_target_type == TypeManager.ushort_type)
1496 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1497 if (real_target_type == TypeManager.uint32_type)
1498 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1499 if (real_target_type == TypeManager.uint64_type)
1500 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1501 if (real_target_type == TypeManager.char_type)
1502 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1503 } else if (expr_type == TypeManager.byte_type){
1505 // From byte to sbyte and char
1507 if (real_target_type == TypeManager.sbyte_type)
1508 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1509 if (real_target_type == TypeManager.char_type)
1510 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1511 } else if (expr_type == TypeManager.short_type){
1513 // From short to sbyte, byte, ushort, uint, ulong, char
1515 if (real_target_type == TypeManager.sbyte_type)
1516 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1517 if (real_target_type == TypeManager.byte_type)
1518 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1519 if (real_target_type == TypeManager.ushort_type)
1520 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1521 if (real_target_type == TypeManager.uint32_type)
1522 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1523 if (real_target_type == TypeManager.uint64_type)
1524 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1525 if (real_target_type == TypeManager.char_type)
1526 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1527 } else if (expr_type == TypeManager.ushort_type){
1529 // From ushort to sbyte, byte, short, char
1531 if (real_target_type == TypeManager.sbyte_type)
1532 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1533 if (real_target_type == TypeManager.byte_type)
1534 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1535 if (real_target_type == TypeManager.short_type)
1536 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1537 if (real_target_type == TypeManager.char_type)
1538 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1539 } else if (expr_type == TypeManager.int32_type){
1541 // From int to sbyte, byte, short, ushort, uint, ulong, char
1543 if (real_target_type == TypeManager.sbyte_type)
1544 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1545 if (real_target_type == TypeManager.byte_type)
1546 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1547 if (real_target_type == TypeManager.short_type)
1548 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1549 if (real_target_type == TypeManager.ushort_type)
1550 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1551 if (real_target_type == TypeManager.uint32_type)
1552 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1553 if (real_target_type == TypeManager.uint64_type)
1554 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1555 if (real_target_type == TypeManager.char_type)
1556 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1557 } else if (expr_type == TypeManager.uint32_type){
1559 // From uint to sbyte, byte, short, ushort, int, char
1561 if (real_target_type == TypeManager.sbyte_type)
1562 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1563 if (real_target_type == TypeManager.byte_type)
1564 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1565 if (real_target_type == TypeManager.short_type)
1566 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1567 if (real_target_type == TypeManager.ushort_type)
1568 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1569 if (real_target_type == TypeManager.int32_type)
1570 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1571 if (real_target_type == TypeManager.char_type)
1572 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1573 } else if (expr_type == TypeManager.int64_type){
1575 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1577 if (real_target_type == TypeManager.sbyte_type)
1578 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1579 if (real_target_type == TypeManager.byte_type)
1580 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1581 if (real_target_type == TypeManager.short_type)
1582 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1583 if (real_target_type == TypeManager.ushort_type)
1584 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1585 if (real_target_type == TypeManager.int32_type)
1586 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1587 if (real_target_type == TypeManager.uint32_type)
1588 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1589 if (real_target_type == TypeManager.uint64_type)
1590 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1591 if (real_target_type == TypeManager.char_type)
1592 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1593 } else if (expr_type == TypeManager.uint64_type){
1595 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1597 if (real_target_type == TypeManager.sbyte_type)
1598 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1599 if (real_target_type == TypeManager.byte_type)
1600 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1601 if (real_target_type == TypeManager.short_type)
1602 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1603 if (real_target_type == TypeManager.ushort_type)
1604 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1605 if (real_target_type == TypeManager.int32_type)
1606 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1607 if (real_target_type == TypeManager.uint32_type)
1608 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1609 if (real_target_type == TypeManager.int64_type)
1610 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1611 if (real_target_type == TypeManager.char_type)
1612 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1613 } else if (expr_type == TypeManager.char_type){
1615 // From char to sbyte, byte, short
1617 if (real_target_type == TypeManager.sbyte_type)
1618 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1619 if (real_target_type == TypeManager.byte_type)
1620 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1621 if (real_target_type == TypeManager.short_type)
1622 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1623 } else if (expr_type == TypeManager.float_type){
1625 // From float to sbyte, byte, short,
1626 // ushort, int, uint, long, ulong, char
1629 if (real_target_type == TypeManager.sbyte_type)
1630 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1631 if (real_target_type == TypeManager.byte_type)
1632 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1633 if (real_target_type == TypeManager.short_type)
1634 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1635 if (real_target_type == TypeManager.ushort_type)
1636 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1637 if (real_target_type == TypeManager.int32_type)
1638 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1639 if (real_target_type == TypeManager.uint32_type)
1640 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1641 if (real_target_type == TypeManager.int64_type)
1642 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1643 if (real_target_type == TypeManager.uint64_type)
1644 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1645 if (real_target_type == TypeManager.char_type)
1646 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1647 } else if (expr_type == TypeManager.double_type){
1649 // From double to byte, byte, short,
1650 // ushort, int, uint, long, ulong,
1651 // char, float or decimal
1653 if (real_target_type == TypeManager.sbyte_type)
1654 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1655 if (real_target_type == TypeManager.byte_type)
1656 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1657 if (real_target_type == TypeManager.short_type)
1658 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1659 if (real_target_type == TypeManager.ushort_type)
1660 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1661 if (real_target_type == TypeManager.int32_type)
1662 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1663 if (real_target_type == TypeManager.uint32_type)
1664 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1665 if (real_target_type == TypeManager.int64_type)
1666 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1667 if (real_target_type == TypeManager.uint64_type)
1668 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1669 if (real_target_type == TypeManager.char_type)
1670 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1671 if (real_target_type == TypeManager.float_type)
1672 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1675 // decimal is taken care of by the op_Explicit methods.
1681 /// Returns whether an explicit reference conversion can be performed
1682 /// from source_type to target_type
1684 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1686 bool target_is_type_param = target_type.IsGenericParameter;
1687 bool target_is_value_type = target_type.IsValueType;
1689 if (source_type == target_type)
1693 // From generic parameter to any type
1695 if (source_type.IsGenericParameter)
1699 // From object to a generic parameter
1701 if (source_type == TypeManager.object_type && target_is_type_param)
1705 // From object to any reference type
1707 if (source_type == TypeManager.object_type && !target_is_value_type)
1711 // From any class S to any class-type T, provided S is a base class of T
1713 if (TypeManager.IsSubclassOf (target_type, source_type))
1717 // From any interface type S to any interface T provided S is not derived from T
1719 if (source_type.IsInterface && target_type.IsInterface){
1720 if (!TypeManager.IsSubclassOf (target_type, source_type))
1725 // From any class type S to any interface T, provided S is not sealed
1726 // and provided S does not implement T.
1728 if (target_type.IsInterface && !source_type.IsSealed &&
1729 !TypeManager.ImplementsInterface (source_type, target_type))
1733 // From any interface-type S to to any class type T, provided T is not
1734 // sealed, or provided T implements S.
1736 if (source_type.IsInterface &&
1737 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1741 // From an array type S with an element type Se to an array type T with an
1742 // element type Te provided all the following are true:
1743 // * S and T differe only in element type, in other words, S and T
1744 // have the same number of dimensions.
1745 // * Both Se and Te are reference types
1746 // * An explicit referenc conversions exist from Se to Te
1748 if (source_type.IsArray && target_type.IsArray) {
1749 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1751 Type source_element_type = TypeManager.GetElementType (source_type);
1752 Type target_element_type = TypeManager.GetElementType (target_type);
1754 if (source_element_type.IsGenericParameter ||
1755 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1756 if (ExplicitReferenceConversionExists (source_element_type,
1757 target_element_type))
1763 // From System.Array to any array-type
1764 if (source_type == TypeManager.array_type &&
1765 target_type.IsArray){
1770 // From System delegate to any delegate-type
1772 if (source_type == TypeManager.delegate_type &&
1773 TypeManager.IsDelegateType (target_type))
1777 // From ICloneable to Array or Delegate types
1779 if (source_type == TypeManager.icloneable_type &&
1780 (target_type == TypeManager.array_type ||
1781 target_type == TypeManager.delegate_type))
1788 /// Implements Explicit Reference conversions
1790 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1792 Type source_type = source.Type;
1793 bool target_is_type_param = target_type.IsGenericParameter;
1794 bool target_is_value_type = target_type.IsValueType;
1797 // From object to a generic parameter
1799 if (source_type == TypeManager.object_type && target_is_type_param)
1800 return new UnboxCast (source, target_type);
1803 // From object to any reference type
1805 if (source_type == TypeManager.object_type && !target_is_value_type)
1806 return new ClassCast (source, target_type);
1809 // Unboxing conversion.
1811 if (((source_type == TypeManager.enum_type &&
1812 !(source is EmptyCast)) ||
1813 source_type == TypeManager.value_type) && target_is_value_type)
1814 return new UnboxCast (source, target_type);
1817 // From any class S to any class-type T, provided S is a base class of T
1819 if (TypeManager.IsSubclassOf (target_type, source_type))
1820 return new ClassCast (source, target_type);
1823 // From any interface type S to any interface T provided S is not derived from T
1825 if (source_type.IsInterface && target_type.IsInterface){
1826 if (TypeManager.ImplementsInterface (source_type, target_type))
1829 return new ClassCast (source, target_type);
1833 // From any class type S to any interface T, provides S is not sealed
1834 // and provided S does not implement T.
1836 if (target_type.IsInterface && !source_type.IsSealed) {
1837 if (TypeManager.ImplementsInterface (source_type, target_type))
1840 return new ClassCast (source, target_type);
1845 // From any interface-type S to to any class type T, provided T is not
1846 // sealed, or provided T implements S.
1848 if (source_type.IsInterface) {
1849 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1850 if (target_type.IsClass)
1851 return new ClassCast (source, target_type);
1853 return new UnboxCast (source, target_type);
1859 // From an array type S with an element type Se to an array type T with an
1860 // element type Te provided all the following are true:
1861 // * S and T differe only in element type, in other words, S and T
1862 // have the same number of dimensions.
1863 // * Both Se and Te are reference types
1864 // * An explicit referenc conversions exist from Se to Te
1866 if (source_type.IsArray && target_type.IsArray) {
1867 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1869 Type source_element_type = TypeManager.GetElementType (source_type);
1870 Type target_element_type = TypeManager.GetElementType (target_type);
1872 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1873 if (ExplicitReferenceConversionExists (source_element_type,
1874 target_element_type))
1875 return new ClassCast (source, target_type);
1880 // From System.Array to any array-type
1881 if (source_type == TypeManager.array_type &&
1882 target_type.IsArray) {
1883 return new ClassCast (source, target_type);
1887 // From System delegate to any delegate-type
1889 if (source_type == TypeManager.delegate_type &&
1890 TypeManager.IsDelegateType (target_type))
1891 return new ClassCast (source, target_type);
1894 // From ICloneable to Array or Delegate types
1896 if (source_type == TypeManager.icloneable_type &&
1897 (target_type == TypeManager.array_type ||
1898 target_type == TypeManager.delegate_type))
1899 return new ClassCast (source, target_type);
1905 /// Performs an explicit conversion of the expression `expr' whose
1906 /// type is expr.Type to `target_type'.
1908 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1909 Type target_type, Location loc)
1911 Type expr_type = expr.Type;
1912 Type original_expr_type = expr_type;
1914 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1915 if (target_type == TypeManager.enum_type ||
1916 target_type == TypeManager.object_type) {
1917 if (expr is EnumConstant)
1918 expr = ((EnumConstant) expr).Child;
1919 // We really need all these casts here .... :-(
1920 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1921 return new EmptyCast (expr, target_type);
1922 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1923 target_type.IsSubclassOf (TypeManager.enum_type))
1924 return new UnboxCast (expr, target_type);
1927 // Notice that we have kept the expr_type unmodified, which is only
1929 if (expr is EnumConstant)
1930 expr = ((EnumConstant) expr).Child;
1932 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1933 expr_type = expr.Type;
1936 int errors = Report.Errors;
1937 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1938 if (Report.Errors > errors)
1944 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
1945 return new Nullable.LiftedConversion (
1946 expr, target_type, false, true, loc).Resolve (ec);
1948 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1953 // Unboxing conversion.
1955 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1956 return new UnboxCast (expr, target_type);
1959 // Skip the ExplicitReferenceConversion because we can not convert
1960 // from Null to a ValueType, and ExplicitReference wont check against
1961 // null literal explicitly
1963 if (expr_type != TypeManager.null_type){
1964 ne = ExplicitReferenceConversion (expr, target_type);
1971 if (target_type.IsPointer){
1972 if (expr_type.IsPointer)
1973 return new EmptyCast (expr, target_type);
1975 if (expr_type == TypeManager.sbyte_type ||
1976 expr_type == TypeManager.byte_type ||
1977 expr_type == TypeManager.short_type ||
1978 expr_type == TypeManager.ushort_type ||
1979 expr_type == TypeManager.int32_type ||
1980 expr_type == TypeManager.uint32_type ||
1981 expr_type == TypeManager.uint64_type ||
1982 expr_type == TypeManager.int64_type)
1983 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1985 if (expr_type.IsPointer){
1986 Expression e = null;
1988 if (target_type == TypeManager.sbyte_type)
1989 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1990 else if (target_type == TypeManager.byte_type)
1991 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1992 else if (target_type == TypeManager.short_type)
1993 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1994 else if (target_type == TypeManager.ushort_type)
1995 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1996 else if (target_type == TypeManager.int32_type)
1997 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1998 else if (target_type == TypeManager.uint32_type)
1999 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2000 else if (target_type == TypeManager.uint64_type)
2001 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2002 else if (target_type == TypeManager.int64_type){
2003 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2009 ci = ImplicitConversionStandard (ec, e, target_type, loc);
2014 ce = ExplicitNumericConversion (ec, e, target_type, loc);
2018 // We should always be able to go from an uint32
2019 // implicitly or explicitly to the other integral
2022 throw new Exception ("Internal compiler error");
2027 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2031 if (expr is NullLiteral){
2032 Report.Error (37, loc, "Cannot convert null to value type `" +
2033 TypeManager.CSharpName (target_type) + "'");
2037 Error_CannotConvertType (loc, original_expr_type, target_type);
2042 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2044 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2045 Type target_type, Location l)
2047 int errors = Report.Errors;
2048 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2049 if (Report.Errors > errors)
2055 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
2056 return new Nullable.LiftedConversion (
2057 expr, target_type, false, true, l).Resolve (ec);
2059 ne = ExplicitNumericConversion (ec, expr, target_type, l);
2063 ne = ExplicitReferenceConversion (expr, target_type);
2067 Error_CannotConvertType (l, expr.Type, target_type);