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 any delegate type to System.Delegate
262 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
263 target_type == TypeManager.delegate_type)
264 return new EmptyCast (expr, target_type);
266 // from any array-type or delegate type into System.ICloneable.
267 if (expr_type.IsArray ||
268 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
269 if (target_type == TypeManager.icloneable_type)
270 return new EmptyCast (expr, target_type);
272 // from a generic type definition to a generic instance.
273 if (TypeManager.IsEqual (expr_type, target_type))
274 return new EmptyCast (expr, target_type);
280 // Tests whether an implicit reference conversion exists between expr_type
283 public static bool ImplicitReferenceConversionExists (EmitContext ec, Expression expr, Type target_type)
285 Type expr_type = expr.Type;
287 if (expr_type.IsGenericParameter)
288 return ImplicitTypeParameterConversion (ec, expr, target_type) != null;
291 // This is the boxed case.
293 if (target_type == TypeManager.object_type) {
294 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
295 expr_type.IsInterface || expr_type == TypeManager.enum_type)
296 if (target_type != TypeManager.anonymous_method_type)
300 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
303 // Please remember that all code below actually comes
304 // from ImplicitReferenceConversion so make sure code remains in sync
306 // from any class-type S to any interface-type T.
307 if (target_type.IsInterface) {
308 if (target_type != TypeManager.iconvertible_type &&
309 expr_type.IsValueType && (expr is Constant) &&
310 !(expr is IntLiteral || expr is BoolLiteral ||
311 expr is FloatLiteral || expr is DoubleLiteral ||
312 expr is LongLiteral || expr is CharLiteral ||
313 expr is StringLiteral || expr is DecimalLiteral ||
314 expr is UIntLiteral || expr is ULongLiteral)) {
318 if (TypeManager.ImplementsInterface (expr_type, target_type))
322 // from any interface type S to interface-type T.
323 if (expr_type.IsInterface && target_type.IsInterface)
324 if (TypeManager.ImplementsInterface (expr_type, target_type))
327 // from an array-type S to an array-type of type T
328 if (expr_type.IsArray && target_type.IsArray) {
329 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
331 Type expr_element_type = expr_type.GetElementType ();
333 if (MyEmptyExpr == null)
334 MyEmptyExpr = new EmptyExpression ();
336 MyEmptyExpr.SetType (expr_element_type);
337 Type target_element_type = TypeManager.GetElementType (target_type);
339 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
340 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
341 target_element_type))
346 // from an array-type to System.Array
347 if (expr_type.IsArray && (target_type == TypeManager.array_type))
350 // from any delegate type to System.Delegate
351 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
352 target_type == TypeManager.delegate_type)
353 if (target_type.IsAssignableFrom (expr_type))
356 // from any array-type or delegate type into System.ICloneable.
357 if (expr_type.IsArray ||
358 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
359 if (target_type == TypeManager.icloneable_type)
362 // from the null type to any reference-type.
363 if (expr_type == TypeManager.null_type){
364 if (target_type.IsPointer)
367 if (!target_type.IsValueType)
371 // from a generic type definition to a generic instance.
372 if (TypeManager.IsEqual (expr_type, target_type))
379 /// Implicit Numeric Conversions.
381 /// expr is the expression to convert, returns a new expression of type
382 /// target_type or null if an implicit conversion is not possible.
384 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
385 Type target_type, Location loc)
387 Type expr_type = expr.Type;
390 // Attempt to do the implicit constant expression conversions
392 if (expr is Constant){
393 if (expr is IntConstant){
396 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
400 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
402 // Try the implicit constant expression conversion
403 // from long to ulong, instead of a nice routine,
406 long v = ((LongConstant) expr).Value;
408 return new ULongConstant ((ulong) v);
412 Type real_target_type = target_type;
414 if (expr_type == TypeManager.sbyte_type){
416 // From sbyte to short, int, long, float, double.
418 if (real_target_type == TypeManager.int32_type)
419 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
420 if (real_target_type == TypeManager.int64_type)
421 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
422 if (real_target_type == TypeManager.double_type)
423 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
424 if (real_target_type == TypeManager.float_type)
425 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
426 if (real_target_type == TypeManager.short_type)
427 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
428 } else if (expr_type == TypeManager.byte_type){
430 // From byte to short, ushort, int, uint, long, ulong, float, double
432 if ((real_target_type == TypeManager.short_type) ||
433 (real_target_type == TypeManager.ushort_type) ||
434 (real_target_type == TypeManager.int32_type) ||
435 (real_target_type == TypeManager.uint32_type))
436 return new EmptyCast (expr, target_type);
438 if (real_target_type == TypeManager.uint64_type)
439 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
440 if (real_target_type == TypeManager.int64_type)
441 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
442 if (real_target_type == TypeManager.float_type)
443 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
444 if (real_target_type == TypeManager.double_type)
445 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
446 } else if (expr_type == TypeManager.short_type){
448 // From short to int, long, float, double
450 if (real_target_type == TypeManager.int32_type)
451 return new EmptyCast (expr, target_type);
452 if (real_target_type == TypeManager.int64_type)
453 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
454 if (real_target_type == TypeManager.double_type)
455 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
456 if (real_target_type == TypeManager.float_type)
457 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
458 } else if (expr_type == TypeManager.ushort_type){
460 // From ushort to int, uint, long, ulong, float, double
462 if (real_target_type == TypeManager.uint32_type)
463 return new EmptyCast (expr, target_type);
465 if (real_target_type == TypeManager.uint64_type)
466 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
467 if (real_target_type == TypeManager.int32_type)
468 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
469 if (real_target_type == TypeManager.int64_type)
470 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
471 if (real_target_type == TypeManager.double_type)
472 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
473 if (real_target_type == TypeManager.float_type)
474 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
475 } else if (expr_type == TypeManager.int32_type){
477 // From int to long, float, double
479 if (real_target_type == TypeManager.int64_type)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
481 if (real_target_type == TypeManager.double_type)
482 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
483 if (real_target_type == TypeManager.float_type)
484 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
485 } else if (expr_type == TypeManager.uint32_type){
487 // From uint to long, ulong, float, double
489 if (real_target_type == TypeManager.int64_type)
490 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
491 if (real_target_type == TypeManager.uint64_type)
492 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
493 if (real_target_type == TypeManager.double_type)
494 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
496 if (real_target_type == TypeManager.float_type)
497 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
499 } else if (expr_type == TypeManager.int64_type){
501 // From long/ulong to float, double
503 if (real_target_type == TypeManager.double_type)
504 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
505 if (real_target_type == TypeManager.float_type)
506 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
507 } else if (expr_type == TypeManager.uint64_type){
509 // From ulong to float, double
511 if (real_target_type == TypeManager.double_type)
512 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
514 if (real_target_type == TypeManager.float_type)
515 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
517 } else if (expr_type == TypeManager.char_type){
519 // From char to ushort, int, uint, long, ulong, float, double
521 if ((real_target_type == TypeManager.ushort_type) ||
522 (real_target_type == TypeManager.int32_type) ||
523 (real_target_type == TypeManager.uint32_type))
524 return new EmptyCast (expr, target_type);
525 if (real_target_type == TypeManager.uint64_type)
526 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
527 if (real_target_type == TypeManager.int64_type)
528 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
529 if (real_target_type == TypeManager.float_type)
530 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
531 if (real_target_type == TypeManager.double_type)
532 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
533 } else if (expr_type == TypeManager.float_type){
537 if (real_target_type == TypeManager.double_type)
538 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
546 /// Same as ImplicitStandardConversionExists except that it also looks at
547 /// implicit user defined conversions - needed for overload resolution
549 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
551 if ((expr is NullLiteral) && target_type.IsGenericParameter)
552 return TypeParameter_to_Null (target_type);
554 if (ImplicitStandardConversionExists (ec, expr, target_type))
557 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
565 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
567 Expression dummy = ImplicitUserConversion (
568 ec, new EmptyExpression (source), target, Location.Null);
569 return dummy != null;
573 /// Determines if a standard implicit conversion exists from
574 /// expr_type to target_type
576 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
578 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
580 Type expr_type = expr.Type;
582 if (expr_type == TypeManager.void_type)
585 //Console.WriteLine ("Expr is {0}", expr);
586 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
587 if (expr_type.Equals (target_type))
591 // First numeric conversions
593 if (expr_type == TypeManager.sbyte_type){
595 // From sbyte to short, int, long, float, double.
597 if ((target_type == TypeManager.int32_type) ||
598 (target_type == TypeManager.int64_type) ||
599 (target_type == TypeManager.double_type) ||
600 (target_type == TypeManager.float_type) ||
601 (target_type == TypeManager.short_type) ||
602 (target_type == TypeManager.decimal_type))
605 } else if (expr_type == TypeManager.byte_type){
607 // From byte to short, ushort, int, uint, long, ulong, float, double
609 if ((target_type == TypeManager.short_type) ||
610 (target_type == TypeManager.ushort_type) ||
611 (target_type == TypeManager.int32_type) ||
612 (target_type == TypeManager.uint32_type) ||
613 (target_type == TypeManager.uint64_type) ||
614 (target_type == TypeManager.int64_type) ||
615 (target_type == TypeManager.float_type) ||
616 (target_type == TypeManager.double_type) ||
617 (target_type == TypeManager.decimal_type))
620 } else if (expr_type == TypeManager.short_type){
622 // From short to int, long, float, double
624 if ((target_type == TypeManager.int32_type) ||
625 (target_type == TypeManager.int64_type) ||
626 (target_type == TypeManager.double_type) ||
627 (target_type == TypeManager.float_type) ||
628 (target_type == TypeManager.decimal_type))
631 } else if (expr_type == TypeManager.ushort_type){
633 // From ushort to int, uint, long, ulong, float, double
635 if ((target_type == TypeManager.uint32_type) ||
636 (target_type == TypeManager.uint64_type) ||
637 (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.int32_type){
646 // From int to long, float, double
648 if ((target_type == TypeManager.int64_type) ||
649 (target_type == TypeManager.double_type) ||
650 (target_type == TypeManager.float_type) ||
651 (target_type == TypeManager.decimal_type))
654 } else if (expr_type == TypeManager.uint32_type){
656 // From uint to long, ulong, float, double
658 if ((target_type == TypeManager.int64_type) ||
659 (target_type == TypeManager.uint64_type) ||
660 (target_type == TypeManager.double_type) ||
661 (target_type == TypeManager.float_type) ||
662 (target_type == TypeManager.decimal_type))
665 } else if ((expr_type == TypeManager.uint64_type) ||
666 (expr_type == TypeManager.int64_type)) {
668 // From long/ulong to float, double
670 if ((target_type == TypeManager.double_type) ||
671 (target_type == TypeManager.float_type) ||
672 (target_type == TypeManager.decimal_type))
675 } else if (expr_type == TypeManager.char_type){
677 // From char to ushort, int, uint, long, ulong, float, double
679 if ((target_type == TypeManager.ushort_type) ||
680 (target_type == TypeManager.int32_type) ||
681 (target_type == TypeManager.uint32_type) ||
682 (target_type == TypeManager.uint64_type) ||
683 (target_type == TypeManager.int64_type) ||
684 (target_type == TypeManager.float_type) ||
685 (target_type == TypeManager.double_type) ||
686 (target_type == TypeManager.decimal_type))
689 } else if (expr_type == TypeManager.float_type){
693 if (target_type == TypeManager.double_type)
697 if (expr.eclass == ExprClass.MethodGroup){
698 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
699 MethodGroupExpr mg = expr as MethodGroupExpr;
702 // This should not happen frequently, so we can create an object
703 // to test compatibility
705 Expression c = ImplicitDelegateCreation.Create (ec, mg, target_type, Location.Null);
711 if (ImplicitReferenceConversionExists (ec, expr, target_type))
715 // Implicit Constant Expression Conversions
717 if (expr is IntConstant){
718 int value = ((IntConstant) expr).Value;
720 if (target_type == TypeManager.sbyte_type){
721 if (value >= SByte.MinValue && value <= SByte.MaxValue)
723 } else if (target_type == TypeManager.byte_type){
724 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
726 } else if (target_type == TypeManager.short_type){
727 if (value >= Int16.MinValue && value <= Int16.MaxValue)
729 } else if (target_type == TypeManager.ushort_type){
730 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
732 } else if (target_type == TypeManager.uint32_type){
735 } else if (target_type == TypeManager.uint64_type){
737 // we can optimize this case: a positive int32
738 // always fits on a uint64. But we need an opcode
745 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
749 if (expr is LongConstant && target_type == TypeManager.uint64_type){
751 // Try the implicit constant expression conversion
752 // from long to ulong, instead of a nice routine,
755 long v = ((LongConstant) expr).Value;
760 if ((target_type == TypeManager.enum_type ||
761 target_type.IsSubclassOf (TypeManager.enum_type)) &&
763 IntLiteral i = (IntLiteral) expr;
770 // If `expr_type' implements `target_type' (which is an iface)
771 // see TryImplicitIntConversion
773 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
776 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
779 if (expr_type == TypeManager.anonymous_method_type){
780 if (!TypeManager.IsDelegateType (target_type))
783 AnonymousMethod am = (AnonymousMethod) expr;
785 Expression conv = am.Compatible (ec, target_type, true);
794 // Used internally by FindMostEncompassedType, this is used
795 // to avoid creating lots of objects in the tight loop inside
796 // FindMostEncompassedType
798 static EmptyExpression priv_fmet_param;
801 /// Finds "most encompassed type" according to the spec (13.4.2)
802 /// amongst the methods in the MethodGroupExpr
804 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
808 if (priv_fmet_param == null)
809 priv_fmet_param = new EmptyExpression ();
811 foreach (Type t in types){
812 priv_fmet_param.SetType (t);
819 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
827 // Used internally by FindMostEncompassingType, this is used
828 // to avoid creating lots of objects in the tight loop inside
829 // FindMostEncompassingType
831 static EmptyExpression priv_fmee_ret;
834 /// Finds "most encompassing type" according to the spec (13.4.2)
835 /// amongst the types in the given set
837 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
841 if (priv_fmee_ret == null)
842 priv_fmee_ret = new EmptyExpression ();
844 foreach (Type t in types){
845 priv_fmee_ret.SetType (best);
852 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
860 // Used to avoid creating too many objects
862 static EmptyExpression priv_fms_expr;
865 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
866 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
867 /// for explicit and implicit conversion operators.
869 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
870 Expression source, bool apply_explicit_conv_rules,
873 ArrayList src_types_set = new ArrayList ();
875 if (priv_fms_expr == null)
876 priv_fms_expr = new EmptyExpression ();
879 // If any operator converts from S then Sx = S
881 Type source_type = source.Type;
882 foreach (MethodBase mb in me.Methods){
883 ParameterData pd = Invocation.GetParameterData (mb);
884 Type param_type = pd.ParameterType (0);
886 if (param_type == source_type)
889 if (apply_explicit_conv_rules) {
892 // Find the set of applicable user-defined conversion operators, U. This set
894 // user-defined implicit or explicit conversion operators declared by
895 // the classes or structs in D that convert from a type encompassing
896 // or encompassed by S to a type encompassing or encompassed by T
898 priv_fms_expr.SetType (param_type);
899 if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
900 src_types_set.Add (param_type);
902 if (ImplicitStandardConversionExists (ec, source, param_type))
903 src_types_set.Add (param_type);
907 // Only if S is encompassed by param_type
909 if (ImplicitStandardConversionExists (ec, source, param_type))
910 src_types_set.Add (param_type);
915 // Explicit Conv rules
917 if (apply_explicit_conv_rules) {
918 ArrayList candidate_set = new ArrayList ();
920 foreach (Type param_type in src_types_set){
921 if (ImplicitStandardConversionExists (ec, source, param_type))
922 candidate_set.Add (param_type);
925 if (candidate_set.Count != 0)
926 return FindMostEncompassedType (ec, candidate_set);
932 if (apply_explicit_conv_rules)
933 return FindMostEncompassingType (ec, src_types_set);
935 return FindMostEncompassedType (ec, src_types_set);
939 // Useful in avoiding proliferation of objects
941 static EmptyExpression priv_fmt_expr;
944 /// Finds the most specific target Tx according to section 13.4.4
946 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
947 Type target, bool apply_explicit_conv_rules,
950 ArrayList tgt_types_set = new ArrayList ();
952 if (priv_fmt_expr == null)
953 priv_fmt_expr = new EmptyExpression ();
956 // If any operator converts to T then Tx = T
958 foreach (MethodInfo mi in me.Methods){
959 Type ret_type = mi.ReturnType;
961 if (ret_type == target)
964 if (apply_explicit_conv_rules) {
967 // Find the set of applicable user-defined conversion operators, U.
969 // This set consists of the
970 // user-defined implicit or explicit conversion operators declared by
971 // the classes or structs in D that convert from a type encompassing
972 // or encompassed by S to a type encompassing or encompassed by T
974 priv_fms_expr.SetType (ret_type);
975 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
976 tgt_types_set.Add (ret_type);
978 priv_fms_expr.SetType (target);
979 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
980 tgt_types_set.Add (ret_type);
984 // Only if T is encompassed by param_type
986 priv_fms_expr.SetType (ret_type);
987 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
988 tgt_types_set.Add (ret_type);
993 // Explicit conv rules
995 if (apply_explicit_conv_rules) {
996 ArrayList candidate_set = new ArrayList ();
998 foreach (Type ret_type in tgt_types_set){
999 priv_fmt_expr.SetType (ret_type);
1001 if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
1002 candidate_set.Add (ret_type);
1005 if (candidate_set.Count != 0)
1006 return FindMostEncompassingType (ec, candidate_set);
1010 // Okay, final case !
1012 if (apply_explicit_conv_rules)
1013 return FindMostEncompassedType (ec, tgt_types_set);
1015 return FindMostEncompassingType (ec, tgt_types_set);
1019 /// User-defined Implicit conversions
1021 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1022 Type target, Location loc)
1024 return UserDefinedConversion (ec, source, target, loc, false);
1028 /// User-defined Explicit conversions
1030 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1031 Type target, Location loc)
1033 return UserDefinedConversion (ec, source, target, loc, true);
1036 static DoubleHash explicit_conv = new DoubleHash (100);
1037 static DoubleHash implicit_conv = new DoubleHash (100);
1039 /// Computes the MethodGroup for the user-defined conversion
1040 /// operators from source_type to target_type. `look_for_explicit'
1041 /// controls whether we should also include the list of explicit
1044 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1045 Type source_type, Type target_type,
1046 Location loc, bool look_for_explicit)
1048 Expression mg1 = null, mg2 = null;
1049 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1052 op_name = "op_Implicit";
1054 MethodGroupExpr union3;
1056 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1057 return (MethodGroupExpr) r;
1059 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1060 if (source_type.BaseType != null)
1061 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1064 union3 = (MethodGroupExpr) mg2;
1065 else if (mg2 == null)
1066 union3 = (MethodGroupExpr) mg1;
1068 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1070 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1073 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1075 union3 = (MethodGroupExpr) mg1;
1078 if (target_type.BaseType != null)
1079 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1083 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1085 union3 = (MethodGroupExpr) mg1;
1088 MethodGroupExpr union4 = null;
1090 if (look_for_explicit) {
1091 op_name = "op_Explicit";
1093 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1094 if (source_type.BaseType != null)
1095 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1097 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1098 if (target_type.BaseType != null)
1099 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1101 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1102 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1104 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1107 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1108 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1113 /// User-defined conversions
1115 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1116 Type target, Location loc,
1117 bool look_for_explicit)
1119 MethodGroupExpr union;
1120 Type source_type = source.Type;
1121 MethodBase method = null;
1123 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1127 Type most_specific_source, most_specific_target;
1129 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1130 if (most_specific_source == null)
1133 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1134 if (most_specific_target == null)
1140 foreach (MethodBase mb in union.Methods){
1141 ParameterData pd = Invocation.GetParameterData (mb);
1142 MethodInfo mi = (MethodInfo) mb;
1144 if (pd.ParameterType (0) == most_specific_source &&
1145 mi.ReturnType == most_specific_target) {
1151 if (method == null || count > 1)
1156 // This will do the conversion to the best match that we
1157 // found. Now we need to perform an implict standard conversion
1158 // if the best match was not the type that we were requested
1161 if (look_for_explicit)
1162 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1164 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1170 e = new UserCast ((MethodInfo) method, source, loc);
1171 if (e.Type != target){
1172 if (!look_for_explicit)
1173 e = ImplicitConversionStandard (ec, e, target, loc);
1175 e = ExplicitConversionStandard (ec, e, target, loc);
1182 /// Converts implicitly the resolved expression `expr' into the
1183 /// `target_type'. It returns a new expression that can be used
1184 /// in a context that expects a `target_type'.
1186 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1187 Type target_type, Location loc)
1191 if (target_type == null)
1192 throw new Exception ("Target type is null");
1194 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1198 e = ImplicitUserConversion (ec, expr, target_type, loc);
1207 /// Attempts to apply the `Standard Implicit
1208 /// Conversion' rules to the expression `expr' into
1209 /// the `target_type'. It returns a new expression
1210 /// that can be used in a context that expects a
1213 /// This is different from `ImplicitConversion' in that the
1214 /// user defined implicit conversions are excluded.
1216 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1217 Type target_type, Location loc)
1219 Type expr_type = expr.Type;
1222 if ((expr is NullLiteral) && target_type.IsGenericParameter)
1223 return TypeParameter_to_Null (expr, target_type, loc);
1225 if (expr.eclass == ExprClass.MethodGroup){
1226 if (!TypeManager.IsDelegateType (target_type)){
1231 // Only allow anonymous method conversions on post ISO_1
1233 if (RootContext.Version != LanguageVersion.ISO_1){
1234 MethodGroupExpr mg = expr as MethodGroupExpr;
1236 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1240 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1243 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1247 e = ImplicitReferenceConversion (ec, expr, target_type);
1251 if ((target_type == TypeManager.enum_type ||
1252 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1253 expr is IntLiteral){
1254 IntLiteral i = (IntLiteral) expr;
1257 return new EnumConstant ((Constant) expr, target_type);
1261 if (expr_type.IsPointer){
1262 if (target_type == TypeManager.void_ptr_type)
1263 return new EmptyCast (expr, target_type);
1266 // yep, comparing pointer types cant be done with
1267 // t1 == t2, we have to compare their element types.
1269 if (target_type.IsPointer){
1270 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1275 if (target_type.IsPointer) {
1276 if (expr_type == TypeManager.null_type)
1277 return new EmptyCast (expr, target_type);
1279 if (expr_type == TypeManager.void_ptr_type)
1280 return new EmptyCast (expr, target_type);
1284 if (expr_type == TypeManager.anonymous_method_type){
1285 if (!TypeManager.IsDelegateType (target_type)){
1286 Report.Error (1660, loc,
1287 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1288 TypeManager.CSharpName (target_type));
1292 AnonymousMethod am = (AnonymousMethod) expr;
1293 int errors = Report.Errors;
1295 Expression conv = am.Compatible (ec, target_type, false);
1300 // We return something instead of null, to avoid
1301 // the duplicate error, since am.Compatible would have
1302 // reported that already
1304 if (errors != Report.Errors)
1305 return new EmptyCast (expr, target_type);
1312 /// Attempts to perform an implicit constant conversion of the IntConstant
1313 /// into a different data type using casts (See Implicit Constant
1314 /// Expression Conversions)
1316 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1318 int value = ic.Value;
1320 if (target_type == TypeManager.sbyte_type){
1321 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1322 return new SByteConstant ((sbyte) value);
1323 } else if (target_type == TypeManager.byte_type){
1324 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1325 return new ByteConstant ((byte) value);
1326 } else if (target_type == TypeManager.short_type){
1327 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1328 return new ShortConstant ((short) value);
1329 } else if (target_type == TypeManager.ushort_type){
1330 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1331 return new UShortConstant ((ushort) value);
1332 } else if (target_type == TypeManager.uint32_type){
1334 return new UIntConstant ((uint) value);
1335 } else if (target_type == TypeManager.uint64_type){
1337 // we can optimize this case: a positive int32
1338 // always fits on a uint64. But we need an opcode
1342 return new ULongConstant ((ulong) value);
1343 } else if (target_type == TypeManager.double_type)
1344 return new DoubleConstant ((double) value);
1345 else if (target_type == TypeManager.float_type)
1346 return new FloatConstant ((float) value);
1348 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1349 Type underlying = TypeManager.EnumToUnderlying (target_type);
1350 Constant e = (Constant) ic;
1353 // Possibly, we need to create a different 0 literal before passing
1356 if (underlying == TypeManager.int64_type)
1357 e = new LongLiteral (0);
1358 else if (underlying == TypeManager.uint64_type)
1359 e = new ULongLiteral (0);
1361 return new EnumConstant (e, target_type);
1365 // If `target_type' is an interface and the type of `ic' implements the interface
1366 // e.g. target_type is IComparable, IConvertible, IFormattable
1368 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1369 return new BoxedCast (ic);
1374 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1376 if (source.Name == target.Name){
1377 Report.ExtraInformation (loc,
1379 "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
1380 source.Assembly.FullName, target.Assembly.FullName));
1383 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1384 source == TypeManager.anonymous_method_type ?
1385 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1386 TypeManager.CSharpName (target));
1390 /// Attempts to implicitly convert `source' into `target_type', using
1391 /// ImplicitConversion. If there is no implicit conversion, then
1392 /// an error is signaled
1394 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1395 Type target_type, Location loc)
1399 int errors = Report.Errors;
1400 e = ImplicitConversion (ec, source, target_type, loc);
1401 if (Report.Errors > errors)
1406 if (source is DoubleLiteral) {
1407 if (target_type == TypeManager.float_type) {
1408 Error_664 (loc, "float", "f");
1411 if (target_type == TypeManager.decimal_type) {
1412 Error_664 (loc, "decimal", "m");
1417 if (source is Constant){
1418 Constant c = (Constant) source;
1420 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1424 Error_CannotImplicitConversion (loc, source.Type, target_type);
1429 static void Error_664 (Location loc, string type, string suffix) {
1430 Report.Error (664, loc,
1431 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1436 /// Performs the explicit numeric conversions
1438 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1440 Type expr_type = expr.Type;
1443 // If we have an enumeration, extract the underlying type,
1444 // use this during the comparison, but wrap around the original
1447 Type real_target_type = target_type;
1449 if (TypeManager.IsEnumType (real_target_type))
1450 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1452 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1453 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1455 if (real_target_type != target_type)
1456 return new EmptyCast (ce, target_type);
1460 if (expr_type == TypeManager.sbyte_type){
1462 // From sbyte to byte, ushort, uint, ulong, char
1464 if (real_target_type == TypeManager.byte_type)
1465 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1466 if (real_target_type == TypeManager.ushort_type)
1467 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1468 if (real_target_type == TypeManager.uint32_type)
1469 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1470 if (real_target_type == TypeManager.uint64_type)
1471 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1472 if (real_target_type == TypeManager.char_type)
1473 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1474 } else if (expr_type == TypeManager.byte_type){
1476 // From byte to sbyte and char
1478 if (real_target_type == TypeManager.sbyte_type)
1479 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1480 if (real_target_type == TypeManager.char_type)
1481 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1482 } else if (expr_type == TypeManager.short_type){
1484 // From short to sbyte, byte, ushort, uint, ulong, char
1486 if (real_target_type == TypeManager.sbyte_type)
1487 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1488 if (real_target_type == TypeManager.byte_type)
1489 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1490 if (real_target_type == TypeManager.ushort_type)
1491 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1492 if (real_target_type == TypeManager.uint32_type)
1493 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1494 if (real_target_type == TypeManager.uint64_type)
1495 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1496 if (real_target_type == TypeManager.char_type)
1497 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1498 } else if (expr_type == TypeManager.ushort_type){
1500 // From ushort to sbyte, byte, short, char
1502 if (real_target_type == TypeManager.sbyte_type)
1503 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1504 if (real_target_type == TypeManager.byte_type)
1505 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1506 if (real_target_type == TypeManager.short_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1508 if (real_target_type == TypeManager.char_type)
1509 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1510 } else if (expr_type == TypeManager.int32_type){
1512 // From int to sbyte, byte, short, ushort, uint, ulong, char
1514 if (real_target_type == TypeManager.sbyte_type)
1515 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1516 if (real_target_type == TypeManager.byte_type)
1517 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1518 if (real_target_type == TypeManager.short_type)
1519 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1520 if (real_target_type == TypeManager.ushort_type)
1521 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1522 if (real_target_type == TypeManager.uint32_type)
1523 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1524 if (real_target_type == TypeManager.uint64_type)
1525 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1526 if (real_target_type == TypeManager.char_type)
1527 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1528 } else if (expr_type == TypeManager.uint32_type){
1530 // From uint to sbyte, byte, short, ushort, int, char
1532 if (real_target_type == TypeManager.sbyte_type)
1533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1534 if (real_target_type == TypeManager.byte_type)
1535 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1536 if (real_target_type == TypeManager.short_type)
1537 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1538 if (real_target_type == TypeManager.ushort_type)
1539 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1540 if (real_target_type == TypeManager.int32_type)
1541 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1542 if (real_target_type == TypeManager.char_type)
1543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1544 } else if (expr_type == TypeManager.int64_type){
1546 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1548 if (real_target_type == TypeManager.sbyte_type)
1549 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1550 if (real_target_type == TypeManager.byte_type)
1551 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1552 if (real_target_type == TypeManager.short_type)
1553 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1554 if (real_target_type == TypeManager.ushort_type)
1555 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1556 if (real_target_type == TypeManager.int32_type)
1557 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1558 if (real_target_type == TypeManager.uint32_type)
1559 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1560 if (real_target_type == TypeManager.uint64_type)
1561 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1562 if (real_target_type == TypeManager.char_type)
1563 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1564 } else if (expr_type == TypeManager.uint64_type){
1566 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1568 if (real_target_type == TypeManager.sbyte_type)
1569 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1570 if (real_target_type == TypeManager.byte_type)
1571 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1572 if (real_target_type == TypeManager.short_type)
1573 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1574 if (real_target_type == TypeManager.ushort_type)
1575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1576 if (real_target_type == TypeManager.int32_type)
1577 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1578 if (real_target_type == TypeManager.uint32_type)
1579 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1580 if (real_target_type == TypeManager.int64_type)
1581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1582 if (real_target_type == TypeManager.char_type)
1583 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1584 } else if (expr_type == TypeManager.char_type){
1586 // From char to sbyte, byte, short
1588 if (real_target_type == TypeManager.sbyte_type)
1589 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1590 if (real_target_type == TypeManager.byte_type)
1591 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1592 if (real_target_type == TypeManager.short_type)
1593 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1594 } else if (expr_type == TypeManager.float_type){
1596 // From float to sbyte, byte, short,
1597 // ushort, int, uint, long, ulong, char
1600 if (real_target_type == TypeManager.sbyte_type)
1601 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1602 if (real_target_type == TypeManager.byte_type)
1603 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1604 if (real_target_type == TypeManager.short_type)
1605 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1606 if (real_target_type == TypeManager.ushort_type)
1607 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1608 if (real_target_type == TypeManager.int32_type)
1609 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1610 if (real_target_type == TypeManager.uint32_type)
1611 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1612 if (real_target_type == TypeManager.int64_type)
1613 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1614 if (real_target_type == TypeManager.uint64_type)
1615 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1616 if (real_target_type == TypeManager.char_type)
1617 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1618 } else if (expr_type == TypeManager.double_type){
1620 // From double to byte, byte, short,
1621 // ushort, int, uint, long, ulong,
1622 // char, float or decimal
1624 if (real_target_type == TypeManager.sbyte_type)
1625 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1626 if (real_target_type == TypeManager.byte_type)
1627 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1628 if (real_target_type == TypeManager.short_type)
1629 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1630 if (real_target_type == TypeManager.ushort_type)
1631 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1632 if (real_target_type == TypeManager.int32_type)
1633 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1634 if (real_target_type == TypeManager.uint32_type)
1635 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1636 if (real_target_type == TypeManager.int64_type)
1637 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1638 if (real_target_type == TypeManager.uint64_type)
1639 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1640 if (real_target_type == TypeManager.char_type)
1641 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1642 if (real_target_type == TypeManager.float_type)
1643 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1646 // decimal is taken care of by the op_Explicit methods.
1652 /// Returns whether an explicit reference conversion can be performed
1653 /// from source_type to target_type
1655 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1657 bool target_is_type_param = target_type.IsGenericParameter;
1658 bool target_is_value_type = target_type.IsValueType;
1660 if (source_type == target_type)
1664 // From object to a generic parameter
1666 if (source_type == TypeManager.object_type && target_is_type_param)
1670 // From object to any reference type
1672 if (source_type == TypeManager.object_type && !target_is_value_type)
1676 // From any class S to any class-type T, provided S is a base class of T
1678 if (TypeManager.IsSubclassOf (target_type, source_type))
1682 // From any interface type S to any interface T provided S is not derived from T
1684 if (source_type.IsInterface && target_type.IsInterface){
1685 if (!TypeManager.IsSubclassOf (target_type, source_type))
1690 // From any class type S to any interface T, provided S is not sealed
1691 // and provided S does not implement T.
1693 if (target_type.IsInterface && !source_type.IsSealed &&
1694 !TypeManager.ImplementsInterface (source_type, target_type))
1698 // From any interface-type S to to any class type T, provided T is not
1699 // sealed, or provided T implements S.
1701 if (source_type.IsInterface &&
1702 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1706 // From an array type S with an element type Se to an array type T with an
1707 // element type Te provided all the following are true:
1708 // * S and T differe only in element type, in other words, S and T
1709 // have the same number of dimensions.
1710 // * Both Se and Te are reference types
1711 // * An explicit referenc conversions exist from Se to Te
1713 if (source_type.IsArray && target_type.IsArray) {
1714 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1716 Type source_element_type = TypeManager.GetElementType (source_type);
1717 Type target_element_type = TypeManager.GetElementType (target_type);
1719 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1720 if (ExplicitReferenceConversionExists (source_element_type,
1721 target_element_type))
1727 // From System.Array to any array-type
1728 if (source_type == TypeManager.array_type &&
1729 target_type.IsArray){
1734 // From System delegate to any delegate-type
1736 if (source_type == TypeManager.delegate_type &&
1737 TypeManager.IsDelegateType (target_type))
1741 // From ICloneable to Array or Delegate types
1743 if (source_type == TypeManager.icloneable_type &&
1744 (target_type == TypeManager.array_type ||
1745 target_type == TypeManager.delegate_type))
1752 /// Implements Explicit Reference conversions
1754 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1756 Type source_type = source.Type;
1757 bool target_is_type_param = target_type.IsGenericParameter;
1758 bool target_is_value_type = target_type.IsValueType;
1761 // From object to a generic parameter
1763 if (source_type == TypeManager.object_type && target_is_type_param)
1764 return new UnboxCast (source, target_type);
1767 // From object to any reference type
1769 if (source_type == TypeManager.object_type && !target_is_value_type)
1770 return new ClassCast (source, target_type);
1773 // Unboxing conversion.
1775 if (((source_type == TypeManager.enum_type &&
1776 !(source is EmptyCast)) ||
1777 source_type == TypeManager.value_type) && target_is_value_type)
1778 return new UnboxCast (source, target_type);
1781 // From any class S to any class-type T, provided S is a base class of T
1783 if (TypeManager.IsSubclassOf (target_type, source_type))
1784 return new ClassCast (source, target_type);
1787 // From any interface type S to any interface T provided S is not derived from T
1789 if (source_type.IsInterface && target_type.IsInterface){
1790 if (TypeManager.ImplementsInterface (source_type, target_type))
1793 return new ClassCast (source, target_type);
1797 // From any class type S to any interface T, provides S is not sealed
1798 // and provided S does not implement T.
1800 if (target_type.IsInterface && !source_type.IsSealed) {
1801 if (TypeManager.ImplementsInterface (source_type, target_type))
1804 return new ClassCast (source, target_type);
1809 // From any interface-type S to to any class type T, provided T is not
1810 // sealed, or provided T implements S.
1812 if (source_type.IsInterface) {
1813 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1814 if (target_type.IsClass)
1815 return new ClassCast (source, target_type);
1817 return new UnboxCast (source, target_type);
1823 // From an array type S with an element type Se to an array type T with an
1824 // element type Te provided all the following are true:
1825 // * S and T differe only in element type, in other words, S and T
1826 // have the same number of dimensions.
1827 // * Both Se and Te are reference types
1828 // * An explicit referenc conversions exist from Se to Te
1830 if (source_type.IsArray && target_type.IsArray) {
1831 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1833 Type source_element_type = TypeManager.GetElementType (source_type);
1834 Type target_element_type = TypeManager.GetElementType (target_type);
1836 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1837 if (ExplicitReferenceConversionExists (source_element_type,
1838 target_element_type))
1839 return new ClassCast (source, target_type);
1844 // From System.Array to any array-type
1845 if (source_type == TypeManager.array_type &&
1846 target_type.IsArray) {
1847 return new ClassCast (source, target_type);
1851 // From System delegate to any delegate-type
1853 if (source_type == TypeManager.delegate_type &&
1854 TypeManager.IsDelegateType (target_type))
1855 return new ClassCast (source, target_type);
1858 // From ICloneable to Array or Delegate types
1860 if (source_type == TypeManager.icloneable_type &&
1861 (target_type == TypeManager.array_type ||
1862 target_type == TypeManager.delegate_type))
1863 return new ClassCast (source, target_type);
1869 /// Performs an explicit conversion of the expression `expr' whose
1870 /// type is expr.Type to `target_type'.
1872 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1873 Type target_type, Location loc)
1875 Type expr_type = expr.Type;
1876 Type original_expr_type = expr_type;
1878 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1879 if (target_type == TypeManager.enum_type ||
1880 target_type == TypeManager.object_type) {
1881 if (expr is EnumConstant)
1882 expr = ((EnumConstant) expr).Child;
1883 // We really need all these casts here .... :-(
1884 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1885 return new EmptyCast (expr, target_type);
1886 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1887 target_type.IsSubclassOf (TypeManager.enum_type))
1888 return new UnboxCast (expr, target_type);
1891 // Notice that we have kept the expr_type unmodified, which is only
1893 if (expr is EnumConstant)
1894 expr = ((EnumConstant) expr).Child;
1896 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1897 expr_type = expr.Type;
1900 int errors = Report.Errors;
1901 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1902 if (Report.Errors > errors)
1908 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1913 // Unboxing conversion.
1915 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1916 return new UnboxCast (expr, target_type);
1919 // Skip the ExplicitReferenceConversion because we can not convert
1920 // from Null to a ValueType, and ExplicitReference wont check against
1921 // null literal explicitly
1923 if (expr_type != TypeManager.null_type){
1924 ne = ExplicitReferenceConversion (expr, target_type);
1931 if (target_type.IsPointer){
1932 if (expr_type.IsPointer)
1933 return new EmptyCast (expr, target_type);
1935 if (expr_type == TypeManager.sbyte_type ||
1936 expr_type == TypeManager.byte_type ||
1937 expr_type == TypeManager.short_type ||
1938 expr_type == TypeManager.ushort_type ||
1939 expr_type == TypeManager.int32_type ||
1940 expr_type == TypeManager.uint32_type ||
1941 expr_type == TypeManager.uint64_type ||
1942 expr_type == TypeManager.int64_type)
1943 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1945 if (expr_type.IsPointer){
1946 Expression e = null;
1948 if (target_type == TypeManager.sbyte_type)
1949 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1950 else if (target_type == TypeManager.byte_type)
1951 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1952 else if (target_type == TypeManager.short_type)
1953 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1954 else if (target_type == TypeManager.ushort_type)
1955 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1956 else if (target_type == TypeManager.int32_type)
1957 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1958 else if (target_type == TypeManager.uint32_type)
1959 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1960 else if (target_type == TypeManager.uint64_type)
1961 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1962 else if (target_type == TypeManager.int64_type){
1963 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1969 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1974 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1978 // We should always be able to go from an uint32
1979 // implicitly or explicitly to the other integral
1982 throw new Exception ("Internal compiler error");
1987 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1991 if (expr is NullLiteral){
1992 Report.Error (37, loc, "Cannot convert null to value type `" +
1993 TypeManager.CSharpName (target_type) + "'");
1997 Error_CannotConvertType (loc, original_expr_type, target_type);
2002 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2004 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2005 Type target_type, Location l)
2007 int errors = Report.Errors;
2008 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2009 if (Report.Errors > errors)
2015 ne = ExplicitNumericConversion (ec, expr, target_type, l);
2019 ne = ExplicitReferenceConversion (expr, target_type);
2023 Error_CannotConvertType (l, expr.Type, target_type);