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, GenericConstraints gc)
69 ArrayList list = new ArrayList ();
70 list.Add (gc.EffectiveBaseClass);
71 foreach (Type t in gc.InterfaceConstraints) {
72 if (!t.IsGenericParameter)
75 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
77 list.Add (TypeParam_EffectiveBaseType (ec, new_gc));
79 return FindMostEncompassedType (ec, list);
82 static Expression TypeParameterConversion (Expression expr, bool is_reference, Type target_type)
85 return new EmptyCast (expr, target_type);
87 return new BoxedCast (expr, target_type);
90 static Expression ImplicitTypeParameterConversion (EmitContext ec, Expression expr,
93 Type expr_type = expr.Type;
95 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
98 if (target_type == TypeManager.object_type)
99 return new BoxedCast (expr);
104 // We're converting from a type parameter which is known to be a reference type.
105 bool is_reference = gc.IsReferenceType;
106 Type base_type = TypeParam_EffectiveBaseType (ec, gc);
108 if (TypeManager.IsSubclassOf (base_type, target_type))
109 return TypeParameterConversion (expr, is_reference, target_type);
111 if (target_type.IsInterface) {
112 if (TypeManager.ImplementsInterface (base_type, target_type))
113 return TypeParameterConversion (expr, is_reference, target_type);
115 foreach (Type t in gc.InterfaceConstraints) {
116 if (TypeManager.IsSubclassOf (t, target_type))
117 return TypeParameterConversion (expr, is_reference, target_type);
121 foreach (Type t in gc.InterfaceConstraints) {
122 if (!t.IsGenericParameter)
124 if (TypeManager.IsSubclassOf (t, target_type))
125 return TypeParameterConversion (expr, is_reference, target_type);
131 static EmptyExpression MyEmptyExpr;
132 static public Expression ImplicitReferenceConversion (EmitContext ec, Expression expr, Type target_type)
134 Type expr_type = expr.Type;
136 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
137 // if we are a method group, emit a warning
142 if (expr_type == TypeManager.void_type)
145 if (expr_type.IsGenericParameter)
146 return ImplicitTypeParameterConversion (ec, expr, target_type);
149 // notice that it is possible to write "ValueType v = 1", the ValueType here
150 // is an abstract class, and not really a value type, so we apply the same rules.
152 if (target_type == TypeManager.object_type) {
154 // A pointer type cannot be converted to object
156 if (expr_type.IsPointer)
159 if (TypeManager.IsValueType (expr_type))
160 return new BoxedCast (expr);
161 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
162 if (expr_type == TypeManager.anonymous_method_type)
164 return new EmptyCast (expr, target_type);
168 } else if (target_type == TypeManager.value_type) {
169 if (TypeManager.IsValueType (expr_type))
170 return new BoxedCast (expr);
171 if (expr_type == TypeManager.null_type)
172 return new NullCast (expr, target_type);
175 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
177 // Special case: enumeration to System.Enum.
178 // System.Enum is not a value type, it is a class, so we need
179 // a boxing conversion
181 if (expr_type.IsEnum || expr_type.IsGenericParameter)
182 return new BoxedCast (expr);
184 return new EmptyCast (expr, target_type);
187 // This code is kind of mirrored inside ImplicitStandardConversionExists
188 // with the small distinction that we only probe there
190 // Always ensure that the code here and there is in sync
192 // from the null type to any reference-type.
193 if (expr_type == TypeManager.null_type){
194 if (target_type.IsPointer)
195 return new EmptyCast (expr, target_type);
197 if (!target_type.IsValueType)
198 return new NullCast (expr, target_type);
201 // from any class-type S to any interface-type T.
202 if (target_type.IsInterface) {
203 if (target_type != TypeManager.iconvertible_type &&
204 expr_type.IsValueType && (expr is Constant) &&
205 !(expr is IntLiteral || expr is BoolLiteral ||
206 expr is FloatLiteral || expr is DoubleLiteral ||
207 expr is LongLiteral || expr is CharLiteral ||
208 expr is StringLiteral || expr is DecimalLiteral ||
209 expr is UIntLiteral || expr is ULongLiteral)) {
213 if (TypeManager.ImplementsInterface (expr_type, target_type)){
214 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
215 return new BoxedCast (expr, target_type);
217 return new EmptyCast (expr, target_type);
221 // from any interface type S to interface-type T.
222 if (expr_type.IsInterface && target_type.IsInterface) {
223 if (TypeManager.ImplementsInterface (expr_type, target_type))
224 return new EmptyCast (expr, target_type);
229 // from an array-type S to an array-type of type T
230 if (expr_type.IsArray && target_type.IsArray) {
231 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
233 Type expr_element_type = TypeManager.GetElementType (expr_type);
235 if (MyEmptyExpr == null)
236 MyEmptyExpr = new EmptyExpression ();
238 MyEmptyExpr.SetType (expr_element_type);
239 Type target_element_type = TypeManager.GetElementType (target_type);
241 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
242 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
243 target_element_type))
244 return new EmptyCast (expr, target_type);
248 // from an array-type to System.Array
249 if (expr_type.IsArray && target_type == TypeManager.array_type)
250 return new EmptyCast (expr, target_type);
252 // from an array-type of type T to IEnumerable<T>
253 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
254 return new EmptyCast (expr, target_type);
256 // from any delegate type to System.Delegate
257 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
258 target_type == TypeManager.delegate_type)
259 return new EmptyCast (expr, target_type);
261 // from any array-type or delegate type into System.ICloneable.
262 if (expr_type.IsArray ||
263 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
264 if (target_type == TypeManager.icloneable_type)
265 return new EmptyCast (expr, target_type);
267 // from a generic type definition to a generic instance.
268 if (TypeManager.IsEqual (expr_type, target_type))
269 return new EmptyCast (expr, target_type);
275 // Tests whether an implicit reference conversion exists between expr_type
278 public static bool ImplicitReferenceConversionExists (EmitContext ec, Expression expr, Type target_type)
280 Type expr_type = expr.Type;
282 if (expr_type.IsGenericParameter)
283 return ImplicitTypeParameterConversion (ec, expr, target_type) != null;
286 // This is the boxed case.
288 if (target_type == TypeManager.object_type) {
289 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
290 expr_type.IsInterface || expr_type == TypeManager.enum_type)
291 if (target_type != TypeManager.anonymous_method_type)
295 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
298 // Please remember that all code below actually comes
299 // from ImplicitReferenceConversion so make sure code remains in sync
301 // from any class-type S to any interface-type T.
302 if (target_type.IsInterface) {
303 if (target_type != TypeManager.iconvertible_type &&
304 expr_type.IsValueType && (expr is Constant) &&
305 !(expr is IntLiteral || expr is BoolLiteral ||
306 expr is FloatLiteral || expr is DoubleLiteral ||
307 expr is LongLiteral || expr is CharLiteral ||
308 expr is StringLiteral || expr is DecimalLiteral ||
309 expr is UIntLiteral || expr is ULongLiteral)) {
313 if (TypeManager.ImplementsInterface (expr_type, target_type))
317 // from any interface type S to interface-type T.
318 if (expr_type.IsInterface && target_type.IsInterface)
319 if (TypeManager.ImplementsInterface (expr_type, target_type))
322 // from an array-type S to an array-type of type T
323 if (expr_type.IsArray && target_type.IsArray) {
324 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
326 Type expr_element_type = expr_type.GetElementType ();
328 if (MyEmptyExpr == null)
329 MyEmptyExpr = new EmptyExpression ();
331 MyEmptyExpr.SetType (expr_element_type);
332 Type target_element_type = TypeManager.GetElementType (target_type);
334 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
335 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
336 target_element_type))
341 // from an array-type to System.Array
342 if (expr_type.IsArray && (target_type == TypeManager.array_type))
345 // from an array-type of type T to IEnumerable<T>
346 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
349 // from any delegate type to System.Delegate
350 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
351 target_type == TypeManager.delegate_type)
352 if (target_type.IsAssignableFrom (expr_type))
355 // from any array-type or delegate type into System.ICloneable.
356 if (expr_type.IsArray ||
357 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
358 if (target_type == TypeManager.icloneable_type)
361 // from the null type to any reference-type.
362 if (expr_type == TypeManager.null_type){
363 if (target_type.IsPointer)
366 if (!target_type.IsValueType)
370 // from a generic type definition to a generic instance.
371 if (TypeManager.IsEqual (expr_type, target_type))
378 /// Implicit Numeric Conversions.
380 /// expr is the expression to convert, returns a new expression of type
381 /// target_type or null if an implicit conversion is not possible.
383 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
384 Type target_type, Location loc)
386 Type expr_type = expr.Type;
389 // Attempt to do the implicit constant expression conversions
391 if (expr is Constant){
392 if (expr is IntConstant){
395 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
399 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
401 // Try the implicit constant expression conversion
402 // from long to ulong, instead of a nice routine,
405 long v = ((LongConstant) expr).Value;
407 return new ULongConstant ((ulong) v);
411 Type real_target_type = target_type;
413 if (expr_type == TypeManager.sbyte_type){
415 // From sbyte to short, int, long, float, double.
417 if (real_target_type == TypeManager.int32_type)
418 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
419 if (real_target_type == TypeManager.int64_type)
420 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
421 if (real_target_type == TypeManager.double_type)
422 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
423 if (real_target_type == TypeManager.float_type)
424 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
425 if (real_target_type == TypeManager.short_type)
426 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
427 } else if (expr_type == TypeManager.byte_type){
429 // From byte to short, ushort, int, uint, long, ulong, float, double
431 if ((real_target_type == TypeManager.short_type) ||
432 (real_target_type == TypeManager.ushort_type) ||
433 (real_target_type == TypeManager.int32_type) ||
434 (real_target_type == TypeManager.uint32_type))
435 return new EmptyCast (expr, target_type);
437 if (real_target_type == TypeManager.uint64_type)
438 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
439 if (real_target_type == TypeManager.int64_type)
440 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
441 if (real_target_type == TypeManager.float_type)
442 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
443 if (real_target_type == TypeManager.double_type)
444 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
445 } else if (expr_type == TypeManager.short_type){
447 // From short to int, long, float, double
449 if (real_target_type == TypeManager.int32_type)
450 return new EmptyCast (expr, target_type);
451 if (real_target_type == TypeManager.int64_type)
452 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
453 if (real_target_type == TypeManager.double_type)
454 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
455 if (real_target_type == TypeManager.float_type)
456 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
457 } else if (expr_type == TypeManager.ushort_type){
459 // From ushort to int, uint, long, ulong, float, double
461 if (real_target_type == TypeManager.uint32_type)
462 return new EmptyCast (expr, target_type);
464 if (real_target_type == TypeManager.uint64_type)
465 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
466 if (real_target_type == TypeManager.int32_type)
467 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
468 if (real_target_type == TypeManager.int64_type)
469 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
470 if (real_target_type == TypeManager.double_type)
471 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
472 if (real_target_type == TypeManager.float_type)
473 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
474 } else if (expr_type == TypeManager.int32_type){
476 // From int to long, float, double
478 if (real_target_type == TypeManager.int64_type)
479 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
480 if (real_target_type == TypeManager.double_type)
481 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
482 if (real_target_type == TypeManager.float_type)
483 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
484 } else if (expr_type == TypeManager.uint32_type){
486 // From uint to long, ulong, float, double
488 if (real_target_type == TypeManager.int64_type)
489 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
490 if (real_target_type == TypeManager.uint64_type)
491 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
492 if (real_target_type == TypeManager.double_type)
493 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
495 if (real_target_type == TypeManager.float_type)
496 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
498 } else if (expr_type == TypeManager.int64_type){
500 // From long/ulong to float, double
502 if (real_target_type == TypeManager.double_type)
503 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
504 if (real_target_type == TypeManager.float_type)
505 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
506 } else if (expr_type == TypeManager.uint64_type){
508 // From ulong to float, double
510 if (real_target_type == TypeManager.double_type)
511 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
513 if (real_target_type == TypeManager.float_type)
514 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
516 } else if (expr_type == TypeManager.char_type){
518 // From char to ushort, int, uint, long, ulong, float, double
520 if ((real_target_type == TypeManager.ushort_type) ||
521 (real_target_type == TypeManager.int32_type) ||
522 (real_target_type == TypeManager.uint32_type))
523 return new EmptyCast (expr, target_type);
524 if (real_target_type == TypeManager.uint64_type)
525 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
526 if (real_target_type == TypeManager.int64_type)
527 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
528 if (real_target_type == TypeManager.float_type)
529 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
530 if (real_target_type == TypeManager.double_type)
531 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
532 } else if (expr_type == TypeManager.float_type){
536 if (real_target_type == TypeManager.double_type)
537 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
545 /// Same as ImplicitStandardConversionExists except that it also looks at
546 /// implicit user defined conversions - needed for overload resolution
548 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
550 if (expr is NullLiteral) {
551 if (target_type.IsGenericParameter)
552 return TypeParameter_to_Null (target_type);
554 if (TypeManager.IsNullableType (target_type))
558 if (ImplicitStandardConversionExists (ec, expr, target_type))
561 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
569 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
571 Expression dummy = ImplicitUserConversion (
572 ec, new EmptyExpression (source), target, Location.Null);
573 return dummy != null;
577 /// Determines if a standard implicit conversion exists from
578 /// expr_type to target_type
580 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
582 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
584 Type expr_type = expr.Type;
586 if (expr_type == TypeManager.void_type)
589 //Console.WriteLine ("Expr is {0}", expr);
590 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
591 if (expr_type.Equals (target_type))
595 // First numeric conversions
597 if (expr_type == TypeManager.sbyte_type){
599 // From sbyte to short, int, long, float, double.
601 if ((target_type == TypeManager.int32_type) ||
602 (target_type == TypeManager.int64_type) ||
603 (target_type == TypeManager.double_type) ||
604 (target_type == TypeManager.float_type) ||
605 (target_type == TypeManager.short_type) ||
606 (target_type == TypeManager.decimal_type))
609 } else if (expr_type == TypeManager.byte_type){
611 // From byte to short, ushort, int, uint, long, ulong, float, double
613 if ((target_type == TypeManager.short_type) ||
614 (target_type == TypeManager.ushort_type) ||
615 (target_type == TypeManager.int32_type) ||
616 (target_type == TypeManager.uint32_type) ||
617 (target_type == TypeManager.uint64_type) ||
618 (target_type == TypeManager.int64_type) ||
619 (target_type == TypeManager.float_type) ||
620 (target_type == TypeManager.double_type) ||
621 (target_type == TypeManager.decimal_type))
624 } else if (expr_type == TypeManager.short_type){
626 // From short to int, long, float, double
628 if ((target_type == TypeManager.int32_type) ||
629 (target_type == TypeManager.int64_type) ||
630 (target_type == TypeManager.double_type) ||
631 (target_type == TypeManager.float_type) ||
632 (target_type == TypeManager.decimal_type))
635 } else if (expr_type == TypeManager.ushort_type){
637 // From ushort to int, uint, long, ulong, float, double
639 if ((target_type == TypeManager.uint32_type) ||
640 (target_type == TypeManager.uint64_type) ||
641 (target_type == TypeManager.int32_type) ||
642 (target_type == TypeManager.int64_type) ||
643 (target_type == TypeManager.double_type) ||
644 (target_type == TypeManager.float_type) ||
645 (target_type == TypeManager.decimal_type))
648 } else if (expr_type == TypeManager.int32_type){
650 // From int to long, float, double
652 if ((target_type == TypeManager.int64_type) ||
653 (target_type == TypeManager.double_type) ||
654 (target_type == TypeManager.float_type) ||
655 (target_type == TypeManager.decimal_type))
658 } else if (expr_type == TypeManager.uint32_type){
660 // From uint to long, ulong, float, double
662 if ((target_type == TypeManager.int64_type) ||
663 (target_type == TypeManager.uint64_type) ||
664 (target_type == TypeManager.double_type) ||
665 (target_type == TypeManager.float_type) ||
666 (target_type == TypeManager.decimal_type))
669 } else if ((expr_type == TypeManager.uint64_type) ||
670 (expr_type == TypeManager.int64_type)) {
672 // From long/ulong to float, double
674 if ((target_type == TypeManager.double_type) ||
675 (target_type == TypeManager.float_type) ||
676 (target_type == TypeManager.decimal_type))
679 } else if (expr_type == TypeManager.char_type){
681 // From char to ushort, int, uint, long, ulong, float, double
683 if ((target_type == TypeManager.ushort_type) ||
684 (target_type == TypeManager.int32_type) ||
685 (target_type == TypeManager.uint32_type) ||
686 (target_type == TypeManager.uint64_type) ||
687 (target_type == TypeManager.int64_type) ||
688 (target_type == TypeManager.float_type) ||
689 (target_type == TypeManager.double_type) ||
690 (target_type == TypeManager.decimal_type))
693 } else if (expr_type == TypeManager.float_type){
697 if (target_type == TypeManager.double_type)
701 if (expr.eclass == ExprClass.MethodGroup){
702 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
703 MethodGroupExpr mg = expr as MethodGroupExpr;
706 // This should not happen frequently, so we can create an object
707 // to test compatibility
709 Expression c = ImplicitDelegateCreation.Create (ec, mg, target_type, Location.Null);
715 if (ImplicitReferenceConversionExists (ec, expr, target_type))
719 // Implicit Constant Expression Conversions
721 if (expr is IntConstant){
722 int value = ((IntConstant) expr).Value;
724 if (target_type == TypeManager.sbyte_type){
725 if (value >= SByte.MinValue && value <= SByte.MaxValue)
727 } else if (target_type == TypeManager.byte_type){
728 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
730 } else if (target_type == TypeManager.short_type){
731 if (value >= Int16.MinValue && value <= Int16.MaxValue)
733 } else if (target_type == TypeManager.ushort_type){
734 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
736 } else if (target_type == TypeManager.uint32_type){
739 } else if (target_type == TypeManager.uint64_type){
741 // we can optimize this case: a positive int32
742 // always fits on a uint64. But we need an opcode
749 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
753 if (expr is LongConstant && target_type == TypeManager.uint64_type){
755 // Try the implicit constant expression conversion
756 // from long to ulong, instead of a nice routine,
759 long v = ((LongConstant) expr).Value;
764 if ((target_type == TypeManager.enum_type ||
765 target_type.IsSubclassOf (TypeManager.enum_type)) &&
767 IntLiteral i = (IntLiteral) expr;
774 // If `expr_type' implements `target_type' (which is an iface)
775 // see TryImplicitIntConversion
777 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
780 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
783 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
786 if (expr_type == TypeManager.anonymous_method_type){
787 if (!TypeManager.IsDelegateType (target_type))
790 AnonymousMethod am = (AnonymousMethod) expr;
792 Expression conv = am.Compatible (ec, target_type, true);
801 // Used internally by FindMostEncompassedType, this is used
802 // to avoid creating lots of objects in the tight loop inside
803 // FindMostEncompassedType
805 static EmptyExpression priv_fmet_param;
808 /// Finds "most encompassed type" according to the spec (13.4.2)
809 /// amongst the methods in the MethodGroupExpr
811 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
815 if (priv_fmet_param == null)
816 priv_fmet_param = new EmptyExpression ();
818 foreach (Type t in types){
819 priv_fmet_param.SetType (t);
826 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
834 // Used internally by FindMostEncompassingType, this is used
835 // to avoid creating lots of objects in the tight loop inside
836 // FindMostEncompassingType
838 static EmptyExpression priv_fmee_ret;
841 /// Finds "most encompassing type" according to the spec (13.4.2)
842 /// amongst the types in the given set
844 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
848 if (priv_fmee_ret == null)
849 priv_fmee_ret = new EmptyExpression ();
851 foreach (Type t in types){
852 priv_fmee_ret.SetType (best);
859 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
867 // Used to avoid creating too many objects
869 static EmptyExpression priv_fms_expr;
872 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
873 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
874 /// for explicit and implicit conversion operators.
876 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
877 Expression source, bool apply_explicit_conv_rules,
880 ArrayList src_types_set = new ArrayList ();
882 if (priv_fms_expr == null)
883 priv_fms_expr = new EmptyExpression ();
886 // If any operator converts from S then Sx = S
888 Type source_type = source.Type;
889 foreach (MethodBase mb in me.Methods){
890 ParameterData pd = TypeManager.GetParameterData (mb);
891 Type param_type = pd.ParameterType (0);
893 if (param_type == source_type)
896 if (apply_explicit_conv_rules) {
899 // Find the set of applicable user-defined conversion operators, U. This set
901 // user-defined implicit or explicit conversion operators declared by
902 // the classes or structs in D that convert from a type encompassing
903 // or encompassed by S to a type encompassing or encompassed by T
905 priv_fms_expr.SetType (param_type);
906 if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
907 src_types_set.Add (param_type);
909 if (ImplicitStandardConversionExists (ec, source, param_type))
910 src_types_set.Add (param_type);
914 // Only if S is encompassed by param_type
916 if (ImplicitStandardConversionExists (ec, source, param_type))
917 src_types_set.Add (param_type);
922 // Explicit Conv rules
924 if (apply_explicit_conv_rules) {
925 ArrayList candidate_set = new ArrayList ();
927 foreach (Type param_type in src_types_set){
928 if (ImplicitStandardConversionExists (ec, source, param_type))
929 candidate_set.Add (param_type);
932 if (candidate_set.Count != 0)
933 return FindMostEncompassedType (ec, candidate_set);
939 if (apply_explicit_conv_rules)
940 return FindMostEncompassingType (ec, src_types_set);
942 return FindMostEncompassedType (ec, src_types_set);
946 // Useful in avoiding proliferation of objects
948 static EmptyExpression priv_fmt_expr;
951 /// Finds the most specific target Tx according to section 13.4.4
953 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
954 Type target, bool apply_explicit_conv_rules,
957 ArrayList tgt_types_set = new ArrayList ();
959 if (priv_fmt_expr == null)
960 priv_fmt_expr = new EmptyExpression ();
963 // If any operator converts to T then Tx = T
965 foreach (MethodInfo mi in me.Methods){
966 Type ret_type = mi.ReturnType;
968 if (ret_type == target)
971 if (apply_explicit_conv_rules) {
974 // Find the set of applicable user-defined conversion operators, U.
976 // This set consists of the
977 // user-defined implicit or explicit conversion operators declared by
978 // the classes or structs in D that convert from a type encompassing
979 // or encompassed by S to a type encompassing or encompassed by T
981 priv_fms_expr.SetType (ret_type);
982 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
983 tgt_types_set.Add (ret_type);
985 priv_fms_expr.SetType (target);
986 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
987 tgt_types_set.Add (ret_type);
991 // Only if T is encompassed by param_type
993 priv_fms_expr.SetType (ret_type);
994 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
995 tgt_types_set.Add (ret_type);
1000 // Explicit conv rules
1002 if (apply_explicit_conv_rules) {
1003 ArrayList candidate_set = new ArrayList ();
1005 foreach (Type ret_type in tgt_types_set){
1006 priv_fmt_expr.SetType (ret_type);
1008 if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
1009 candidate_set.Add (ret_type);
1012 if (candidate_set.Count != 0)
1013 return FindMostEncompassingType (ec, candidate_set);
1017 // Okay, final case !
1019 if (apply_explicit_conv_rules)
1020 return FindMostEncompassedType (ec, tgt_types_set);
1022 return FindMostEncompassingType (ec, tgt_types_set);
1026 /// User-defined Implicit conversions
1028 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1029 Type target, Location loc)
1031 return UserDefinedConversion (ec, source, target, loc, false);
1035 /// User-defined Explicit conversions
1037 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1038 Type target, Location loc)
1040 return UserDefinedConversion (ec, source, target, loc, true);
1043 static DoubleHash explicit_conv = new DoubleHash (100);
1044 static DoubleHash implicit_conv = new DoubleHash (100);
1046 /// Computes the MethodGroup for the user-defined conversion
1047 /// operators from source_type to target_type. `look_for_explicit'
1048 /// controls whether we should also include the list of explicit
1051 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1052 Type source_type, Type target_type,
1053 Location loc, bool look_for_explicit)
1055 Expression mg1 = null, mg2 = null;
1056 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1059 op_name = "op_Implicit";
1061 MethodGroupExpr union3;
1063 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1064 return (MethodGroupExpr) r;
1066 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1067 if (source_type.BaseType != null)
1068 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1071 union3 = (MethodGroupExpr) mg2;
1072 else if (mg2 == null)
1073 union3 = (MethodGroupExpr) mg1;
1075 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1077 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1080 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1082 union3 = (MethodGroupExpr) mg1;
1085 if (target_type.BaseType != null)
1086 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1090 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1092 union3 = (MethodGroupExpr) mg1;
1095 MethodGroupExpr union4 = null;
1097 if (look_for_explicit) {
1098 op_name = "op_Explicit";
1100 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1101 if (source_type.BaseType != null)
1102 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1104 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1105 if (target_type.BaseType != null)
1106 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1108 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1109 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1111 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1114 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1115 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1120 /// User-defined conversions
1122 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1123 Type target, Location loc,
1124 bool look_for_explicit)
1126 MethodGroupExpr union;
1127 Type source_type = source.Type;
1128 MethodBase method = null;
1130 if (TypeManager.IsNullableType (source_type) && TypeManager.IsNullableType (target))
1131 return new Nullable.LiftedConversion (
1132 source, target, true, look_for_explicit, loc).Resolve (ec);
1134 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1138 Type most_specific_source, most_specific_target;
1140 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1141 if (most_specific_source == null)
1144 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1145 if (most_specific_target == null)
1151 foreach (MethodBase mb in union.Methods){
1152 ParameterData pd = TypeManager.GetParameterData (mb);
1153 MethodInfo mi = (MethodInfo) mb;
1155 if (pd.ParameterType (0) == most_specific_source &&
1156 mi.ReturnType == most_specific_target) {
1162 if (method == null || count > 1)
1167 // This will do the conversion to the best match that we
1168 // found. Now we need to perform an implict standard conversion
1169 // if the best match was not the type that we were requested
1172 if (look_for_explicit)
1173 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1175 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1181 e = new UserCast ((MethodInfo) method, source, loc);
1182 if (e.Type != target){
1183 if (!look_for_explicit)
1184 e = ImplicitConversionStandard (ec, e, target, loc);
1186 e = ExplicitConversionStandard (ec, e, target, loc);
1193 /// Converts implicitly the resolved expression `expr' into the
1194 /// `target_type'. It returns a new expression that can be used
1195 /// in a context that expects a `target_type'.
1197 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1198 Type target_type, Location loc)
1202 if (target_type == null)
1203 throw new Exception ("Target type is null");
1205 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1209 e = ImplicitUserConversion (ec, expr, target_type, loc);
1218 /// Attempts to apply the `Standard Implicit
1219 /// Conversion' rules to the expression `expr' into
1220 /// the `target_type'. It returns a new expression
1221 /// that can be used in a context that expects a
1224 /// This is different from `ImplicitConversion' in that the
1225 /// user defined implicit conversions are excluded.
1227 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1228 Type target_type, Location loc)
1230 Type expr_type = expr.Type;
1233 if (expr is NullLiteral) {
1234 if (target_type.IsGenericParameter)
1235 return TypeParameter_to_Null (expr, target_type, loc);
1237 if (TypeManager.IsNullableType (target_type))
1238 return new Nullable.NullableLiteral (target_type, loc);
1241 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1242 return new Nullable.LiftedConversion (
1243 expr, target_type, false, false, loc).Resolve (ec);
1245 if (expr.eclass == ExprClass.MethodGroup){
1246 if (!TypeManager.IsDelegateType (target_type)){
1251 // Only allow anonymous method conversions on post ISO_1
1253 if (RootContext.Version != LanguageVersion.ISO_1){
1254 MethodGroupExpr mg = expr as MethodGroupExpr;
1256 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1260 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1263 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1267 e = ImplicitReferenceConversion (ec, expr, target_type);
1271 if ((target_type == TypeManager.enum_type ||
1272 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1273 expr is IntLiteral){
1274 IntLiteral i = (IntLiteral) expr;
1277 return new EnumConstant ((Constant) expr, target_type);
1281 if (expr_type.IsPointer){
1282 if (target_type == TypeManager.void_ptr_type)
1283 return new EmptyCast (expr, target_type);
1286 // yep, comparing pointer types cant be done with
1287 // t1 == t2, we have to compare their element types.
1289 if (target_type.IsPointer){
1290 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1295 if (target_type.IsPointer) {
1296 if (expr_type == TypeManager.null_type)
1297 return new EmptyCast (expr, target_type);
1299 if (expr_type == TypeManager.void_ptr_type)
1300 return new EmptyCast (expr, target_type);
1304 if (expr_type == TypeManager.anonymous_method_type){
1305 if (!TypeManager.IsDelegateType (target_type)){
1306 Report.Error (1660, loc,
1307 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1308 TypeManager.CSharpName (target_type));
1312 AnonymousMethod am = (AnonymousMethod) expr;
1313 int errors = Report.Errors;
1315 Expression conv = am.Compatible (ec, target_type, false);
1320 // We return something instead of null, to avoid
1321 // the duplicate error, since am.Compatible would have
1322 // reported that already
1324 if (errors != Report.Errors)
1325 return new EmptyCast (expr, target_type);
1332 /// Attempts to perform an implicit constant conversion of the IntConstant
1333 /// into a different data type using casts (See Implicit Constant
1334 /// Expression Conversions)
1336 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1338 int value = ic.Value;
1340 if (target_type == TypeManager.sbyte_type){
1341 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1342 return new SByteConstant ((sbyte) value);
1343 } else if (target_type == TypeManager.byte_type){
1344 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1345 return new ByteConstant ((byte) value);
1346 } else if (target_type == TypeManager.short_type){
1347 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1348 return new ShortConstant ((short) value);
1349 } else if (target_type == TypeManager.ushort_type){
1350 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1351 return new UShortConstant ((ushort) value);
1352 } else if (target_type == TypeManager.uint32_type){
1354 return new UIntConstant ((uint) value);
1355 } else if (target_type == TypeManager.uint64_type){
1357 // we can optimize this case: a positive int32
1358 // always fits on a uint64. But we need an opcode
1362 return new ULongConstant ((ulong) value);
1363 } else if (target_type == TypeManager.double_type)
1364 return new DoubleConstant ((double) value);
1365 else if (target_type == TypeManager.float_type)
1366 return new FloatConstant ((float) value);
1368 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1369 Type underlying = TypeManager.EnumToUnderlying (target_type);
1370 Constant e = (Constant) ic;
1373 // Possibly, we need to create a different 0 literal before passing
1376 if (underlying == TypeManager.int64_type)
1377 e = new LongLiteral (0);
1378 else if (underlying == TypeManager.uint64_type)
1379 e = new ULongLiteral (0);
1381 return new EnumConstant (e, target_type);
1385 // If `target_type' is an interface and the type of `ic' implements the interface
1386 // e.g. target_type is IComparable, IConvertible, IFormattable
1388 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1389 return new BoxedCast (ic);
1394 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1396 if (source.Name == target.Name){
1397 Report.ExtraInformation (loc,
1399 "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
1400 source.Name, source.Assembly.FullName, target.Assembly.FullName));
1403 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1404 source == TypeManager.anonymous_method_type ?
1405 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1406 TypeManager.CSharpName (target));
1410 /// Attempts to implicitly convert `source' into `target_type', using
1411 /// ImplicitConversion. If there is no implicit conversion, then
1412 /// an error is signaled
1414 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1415 Type target_type, Location loc)
1419 int errors = Report.Errors;
1420 e = ImplicitConversion (ec, source, target_type, loc);
1421 if (Report.Errors > errors)
1426 if (source is DoubleLiteral) {
1427 if (target_type == TypeManager.float_type) {
1428 Error_664 (loc, "float", "f");
1431 if (target_type == TypeManager.decimal_type) {
1432 Error_664 (loc, "decimal", "m");
1437 if (source is Constant){
1438 Constant c = (Constant) source;
1440 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1444 Error_CannotImplicitConversion (loc, source.Type, target_type);
1449 static void Error_664 (Location loc, string type, string suffix) {
1450 Report.Error (664, loc,
1451 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1456 /// Performs the explicit numeric conversions
1458 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1460 Type expr_type = expr.Type;
1463 // If we have an enumeration, extract the underlying type,
1464 // use this during the comparison, but wrap around the original
1467 Type real_target_type = target_type;
1469 if (TypeManager.IsEnumType (real_target_type))
1470 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1472 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1473 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1475 if (real_target_type != target_type)
1476 return new EmptyCast (ce, target_type);
1480 if (expr_type == TypeManager.sbyte_type){
1482 // From sbyte to byte, ushort, uint, ulong, char
1484 if (real_target_type == TypeManager.byte_type)
1485 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1486 if (real_target_type == TypeManager.ushort_type)
1487 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1488 if (real_target_type == TypeManager.uint32_type)
1489 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1490 if (real_target_type == TypeManager.uint64_type)
1491 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1492 if (real_target_type == TypeManager.char_type)
1493 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1494 } else if (expr_type == TypeManager.byte_type){
1496 // From byte to sbyte and char
1498 if (real_target_type == TypeManager.sbyte_type)
1499 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1500 if (real_target_type == TypeManager.char_type)
1501 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1502 } else if (expr_type == TypeManager.short_type){
1504 // From short to sbyte, byte, ushort, uint, ulong, char
1506 if (real_target_type == TypeManager.sbyte_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1508 if (real_target_type == TypeManager.byte_type)
1509 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1510 if (real_target_type == TypeManager.ushort_type)
1511 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1512 if (real_target_type == TypeManager.uint32_type)
1513 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1514 if (real_target_type == TypeManager.uint64_type)
1515 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1516 if (real_target_type == TypeManager.char_type)
1517 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1518 } else if (expr_type == TypeManager.ushort_type){
1520 // From ushort to sbyte, byte, short, char
1522 if (real_target_type == TypeManager.sbyte_type)
1523 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1524 if (real_target_type == TypeManager.byte_type)
1525 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1526 if (real_target_type == TypeManager.short_type)
1527 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1528 if (real_target_type == TypeManager.char_type)
1529 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1530 } else if (expr_type == TypeManager.int32_type){
1532 // From int to sbyte, byte, short, ushort, uint, ulong, char
1534 if (real_target_type == TypeManager.sbyte_type)
1535 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1536 if (real_target_type == TypeManager.byte_type)
1537 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1538 if (real_target_type == TypeManager.short_type)
1539 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1540 if (real_target_type == TypeManager.ushort_type)
1541 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1542 if (real_target_type == TypeManager.uint32_type)
1543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1544 if (real_target_type == TypeManager.uint64_type)
1545 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1546 if (real_target_type == TypeManager.char_type)
1547 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1548 } else if (expr_type == TypeManager.uint32_type){
1550 // From uint to sbyte, byte, short, ushort, int, char
1552 if (real_target_type == TypeManager.sbyte_type)
1553 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1554 if (real_target_type == TypeManager.byte_type)
1555 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1556 if (real_target_type == TypeManager.short_type)
1557 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1558 if (real_target_type == TypeManager.ushort_type)
1559 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1560 if (real_target_type == TypeManager.int32_type)
1561 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1562 if (real_target_type == TypeManager.char_type)
1563 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1564 } else if (expr_type == TypeManager.int64_type){
1566 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1568 if (real_target_type == TypeManager.sbyte_type)
1569 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1570 if (real_target_type == TypeManager.byte_type)
1571 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1572 if (real_target_type == TypeManager.short_type)
1573 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1574 if (real_target_type == TypeManager.ushort_type)
1575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1576 if (real_target_type == TypeManager.int32_type)
1577 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1578 if (real_target_type == TypeManager.uint32_type)
1579 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1580 if (real_target_type == TypeManager.uint64_type)
1581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1582 if (real_target_type == TypeManager.char_type)
1583 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1584 } else if (expr_type == TypeManager.uint64_type){
1586 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1588 if (real_target_type == TypeManager.sbyte_type)
1589 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1590 if (real_target_type == TypeManager.byte_type)
1591 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1592 if (real_target_type == TypeManager.short_type)
1593 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1594 if (real_target_type == TypeManager.ushort_type)
1595 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1596 if (real_target_type == TypeManager.int32_type)
1597 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1598 if (real_target_type == TypeManager.uint32_type)
1599 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1600 if (real_target_type == TypeManager.int64_type)
1601 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1602 if (real_target_type == TypeManager.char_type)
1603 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1604 } else if (expr_type == TypeManager.char_type){
1606 // From char to sbyte, byte, short
1608 if (real_target_type == TypeManager.sbyte_type)
1609 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1610 if (real_target_type == TypeManager.byte_type)
1611 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1612 if (real_target_type == TypeManager.short_type)
1613 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1614 } else if (expr_type == TypeManager.float_type){
1616 // From float to sbyte, byte, short,
1617 // ushort, int, uint, long, ulong, char
1620 if (real_target_type == TypeManager.sbyte_type)
1621 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1622 if (real_target_type == TypeManager.byte_type)
1623 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1624 if (real_target_type == TypeManager.short_type)
1625 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1626 if (real_target_type == TypeManager.ushort_type)
1627 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1628 if (real_target_type == TypeManager.int32_type)
1629 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1630 if (real_target_type == TypeManager.uint32_type)
1631 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1632 if (real_target_type == TypeManager.int64_type)
1633 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1634 if (real_target_type == TypeManager.uint64_type)
1635 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1636 if (real_target_type == TypeManager.char_type)
1637 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1638 } else if (expr_type == TypeManager.double_type){
1640 // From double to byte, byte, short,
1641 // ushort, int, uint, long, ulong,
1642 // char, float or decimal
1644 if (real_target_type == TypeManager.sbyte_type)
1645 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1646 if (real_target_type == TypeManager.byte_type)
1647 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1648 if (real_target_type == TypeManager.short_type)
1649 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1650 if (real_target_type == TypeManager.ushort_type)
1651 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1652 if (real_target_type == TypeManager.int32_type)
1653 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1654 if (real_target_type == TypeManager.uint32_type)
1655 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1656 if (real_target_type == TypeManager.int64_type)
1657 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1658 if (real_target_type == TypeManager.uint64_type)
1659 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1660 if (real_target_type == TypeManager.char_type)
1661 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1662 if (real_target_type == TypeManager.float_type)
1663 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1666 // decimal is taken care of by the op_Explicit methods.
1672 /// Returns whether an explicit reference conversion can be performed
1673 /// from source_type to target_type
1675 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1677 bool target_is_type_param = target_type.IsGenericParameter;
1678 bool target_is_value_type = target_type.IsValueType;
1680 if (source_type == target_type)
1684 // From generic parameter to any type
1686 if (source_type.IsGenericParameter)
1690 // From object to a generic parameter
1692 if (source_type == TypeManager.object_type && target_is_type_param)
1696 // From object to any reference type
1698 if (source_type == TypeManager.object_type && !target_is_value_type)
1702 // From any class S to any class-type T, provided S is a base class of T
1704 if (TypeManager.IsSubclassOf (target_type, source_type))
1708 // From any interface type S to any interface T provided S is not derived from T
1710 if (source_type.IsInterface && target_type.IsInterface){
1711 if (!TypeManager.IsSubclassOf (target_type, source_type))
1716 // From any class type S to any interface T, provided S is not sealed
1717 // and provided S does not implement T.
1719 if (target_type.IsInterface && !source_type.IsSealed &&
1720 !TypeManager.ImplementsInterface (source_type, target_type))
1724 // From any interface-type S to to any class type T, provided T is not
1725 // sealed, or provided T implements S.
1727 if (source_type.IsInterface &&
1728 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1732 // From an array type S with an element type Se to an array type T with an
1733 // element type Te provided all the following are true:
1734 // * S and T differe only in element type, in other words, S and T
1735 // have the same number of dimensions.
1736 // * Both Se and Te are reference types
1737 // * An explicit referenc conversions exist from Se to Te
1739 if (source_type.IsArray && target_type.IsArray) {
1740 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1742 Type source_element_type = TypeManager.GetElementType (source_type);
1743 Type target_element_type = TypeManager.GetElementType (target_type);
1745 if (source_element_type.IsGenericParameter ||
1746 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1747 if (ExplicitReferenceConversionExists (source_element_type,
1748 target_element_type))
1754 // From System.Array to any array-type
1755 if (source_type == TypeManager.array_type &&
1756 target_type.IsArray){
1761 // From System delegate to any delegate-type
1763 if (source_type == TypeManager.delegate_type &&
1764 TypeManager.IsDelegateType (target_type))
1768 // From ICloneable to Array or Delegate types
1770 if (source_type == TypeManager.icloneable_type &&
1771 (target_type == TypeManager.array_type ||
1772 target_type == TypeManager.delegate_type))
1779 /// Implements Explicit Reference conversions
1781 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1783 Type source_type = source.Type;
1784 bool target_is_type_param = target_type.IsGenericParameter;
1785 bool target_is_value_type = target_type.IsValueType;
1788 // From object to a generic parameter
1790 if (source_type == TypeManager.object_type && target_is_type_param)
1791 return new UnboxCast (source, target_type);
1794 // From object to any reference type
1796 if (source_type == TypeManager.object_type && !target_is_value_type)
1797 return new ClassCast (source, target_type);
1800 // Unboxing conversion.
1802 if (((source_type == TypeManager.enum_type &&
1803 !(source is EmptyCast)) ||
1804 source_type == TypeManager.value_type) && target_is_value_type)
1805 return new UnboxCast (source, target_type);
1808 // From any class S to any class-type T, provided S is a base class of T
1810 if (TypeManager.IsSubclassOf (target_type, source_type))
1811 return new ClassCast (source, target_type);
1814 // From any interface type S to any interface T provided S is not derived from T
1816 if (source_type.IsInterface && target_type.IsInterface){
1817 if (TypeManager.ImplementsInterface (source_type, target_type))
1820 return new ClassCast (source, target_type);
1824 // From any class type S to any interface T, provides S is not sealed
1825 // and provided S does not implement T.
1827 if (target_type.IsInterface && !source_type.IsSealed) {
1828 if (TypeManager.ImplementsInterface (source_type, target_type))
1831 return new ClassCast (source, target_type);
1836 // From any interface-type S to to any class type T, provided T is not
1837 // sealed, or provided T implements S.
1839 if (source_type.IsInterface) {
1840 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1841 if (target_type.IsClass)
1842 return new ClassCast (source, target_type);
1844 return new UnboxCast (source, target_type);
1850 // From an array type S with an element type Se to an array type T with an
1851 // element type Te provided all the following are true:
1852 // * S and T differe only in element type, in other words, S and T
1853 // have the same number of dimensions.
1854 // * Both Se and Te are reference types
1855 // * An explicit referenc conversions exist from Se to Te
1857 if (source_type.IsArray && target_type.IsArray) {
1858 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1860 Type source_element_type = TypeManager.GetElementType (source_type);
1861 Type target_element_type = TypeManager.GetElementType (target_type);
1863 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1864 if (ExplicitReferenceConversionExists (source_element_type,
1865 target_element_type))
1866 return new ClassCast (source, target_type);
1871 // From System.Array to any array-type
1872 if (source_type == TypeManager.array_type &&
1873 target_type.IsArray) {
1874 return new ClassCast (source, target_type);
1878 // From System delegate to any delegate-type
1880 if (source_type == TypeManager.delegate_type &&
1881 TypeManager.IsDelegateType (target_type))
1882 return new ClassCast (source, target_type);
1885 // From ICloneable to Array or Delegate types
1887 if (source_type == TypeManager.icloneable_type &&
1888 (target_type == TypeManager.array_type ||
1889 target_type == TypeManager.delegate_type))
1890 return new ClassCast (source, target_type);
1896 /// Performs an explicit conversion of the expression `expr' whose
1897 /// type is expr.Type to `target_type'.
1899 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1900 Type target_type, Location loc)
1902 Type expr_type = expr.Type;
1903 Type original_expr_type = expr_type;
1905 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1906 if (target_type == TypeManager.enum_type ||
1907 target_type == TypeManager.object_type) {
1908 if (expr is EnumConstant)
1909 expr = ((EnumConstant) expr).Child;
1910 // We really need all these casts here .... :-(
1911 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1912 return new EmptyCast (expr, target_type);
1913 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1914 target_type.IsSubclassOf (TypeManager.enum_type))
1915 return new UnboxCast (expr, target_type);
1918 // Notice that we have kept the expr_type unmodified, which is only
1920 if (expr is EnumConstant)
1921 expr = ((EnumConstant) expr).Child;
1923 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1924 expr_type = expr.Type;
1927 int errors = Report.Errors;
1928 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1929 if (Report.Errors > errors)
1935 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
1936 return new Nullable.LiftedConversion (
1937 expr, target_type, false, true, loc).Resolve (ec);
1939 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1944 // Unboxing conversion.
1946 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1947 return new UnboxCast (expr, target_type);
1950 // Skip the ExplicitReferenceConversion because we can not convert
1951 // from Null to a ValueType, and ExplicitReference wont check against
1952 // null literal explicitly
1954 if (expr_type != TypeManager.null_type){
1955 ne = ExplicitReferenceConversion (expr, target_type);
1961 if (target_type.IsPointer){
1962 if (expr_type.IsPointer)
1963 return new EmptyCast (expr, target_type);
1965 if (expr_type == TypeManager.sbyte_type ||
1966 expr_type == TypeManager.short_type ||
1967 expr_type == TypeManager.int32_type ||
1968 expr_type == TypeManager.int64_type)
1969 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1971 if (expr_type == TypeManager.ushort_type ||
1972 expr_type == TypeManager.uint32_type ||
1973 expr_type == TypeManager.uint64_type ||
1974 expr_type == TypeManager.byte_type)
1975 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1977 if (expr_type.IsPointer){
1978 Expression e = null;
1980 if (target_type == TypeManager.sbyte_type)
1981 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1982 else if (target_type == TypeManager.byte_type)
1983 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1984 else if (target_type == TypeManager.short_type)
1985 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1986 else if (target_type == TypeManager.ushort_type)
1987 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1988 else if (target_type == TypeManager.int32_type)
1989 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1990 else if (target_type == TypeManager.uint32_type)
1991 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1992 else if (target_type == TypeManager.uint64_type)
1993 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1994 else if (target_type == TypeManager.int64_type){
1995 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2001 ci = ImplicitConversionStandard (ec, e, target_type, loc);
2006 ce = ExplicitNumericConversion (ec, e, target_type, loc);
2010 // We should always be able to go from an uint32
2011 // implicitly or explicitly to the other integral
2014 throw new Exception ("Internal compiler error");
2019 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2023 if (expr is NullLiteral){
2024 Report.Error (37, loc, "Cannot convert null to value type `" +
2025 TypeManager.CSharpName (target_type) + "'");
2029 Error_CannotConvertType (loc, original_expr_type, target_type);
2034 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2036 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2037 Type target_type, Location l)
2039 int errors = Report.Errors;
2040 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2041 if (Report.Errors > errors)
2047 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
2048 return new Nullable.LiftedConversion (
2049 expr, target_type, false, true, l).Resolve (ec);
2051 ne = ExplicitNumericConversion (ec, expr, target_type, l);
2055 ne = ExplicitReferenceConversion (expr, target_type);
2059 Error_CannotConvertType (l, expr.Type, target_type);