2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
19 // A container class for all the conversion operations
21 public class Convert {
23 static Expression TypeParameter_to_Null (Type target_type, Location loc)
25 if (!TypeParameter_to_Null (target_type)) {
26 Report.Error (403, loc, "Cannot convert null to the type " +
27 "parameter `{0}' becaues it could be a value " +
28 "type. Consider using `default ({0})' instead.",
29 target_type.ToString ());
33 Constant expr = new Nullable.NullableLiteral (target_type, loc);
34 return new NullCast (expr, target_type);
37 static bool TypeParameter_to_Null (Type target_type)
39 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
43 if (gc.HasReferenceTypeConstraint)
45 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
51 static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
53 ArrayList list = new ArrayList ();
54 list.Add (gc.EffectiveBaseClass);
55 foreach (Type t in gc.InterfaceConstraints) {
56 if (!t.IsGenericParameter)
59 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
61 list.Add (TypeParam_EffectiveBaseType (new_gc));
63 return FindMostEncompassedType (list);
66 static Expression ImplicitTypeParameterConversion (Expression expr,
69 Type expr_type = expr.Type;
71 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
74 if (target_type == TypeManager.object_type)
75 return new BoxedCast (expr, target_type);
80 // We're converting from a type parameter which is known to be a reference type.
81 Type base_type = TypeParam_EffectiveBaseType (gc);
83 if (TypeManager.IsSubclassOf (base_type, target_type))
84 return new ClassCast (expr, target_type);
86 if (target_type.IsInterface) {
87 if (TypeManager.ImplementsInterface (base_type, target_type))
88 return new ClassCast (expr, target_type);
90 foreach (Type t in gc.InterfaceConstraints) {
91 if (TypeManager.IsSubclassOf (t, target_type))
92 return new ClassCast (expr, target_type);
93 if (TypeManager.ImplementsInterface (t, target_type))
94 return new ClassCast (expr, target_type);
98 foreach (Type t in gc.InterfaceConstraints) {
99 if (!t.IsGenericParameter)
101 if (TypeManager.IsSubclassOf (t, target_type))
102 return new ClassCast (expr, target_type);
103 if (TypeManager.ImplementsInterface (t, target_type))
104 return new ClassCast (expr, target_type);
110 static bool ExplicitTypeParameterConversionExists (Type source_type, Type target_type)
112 if (target_type.IsInterface)
115 if (target_type.IsGenericParameter) {
116 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
120 foreach (Type iface in gc.InterfaceConstraints) {
121 if (!iface.IsGenericParameter)
124 if (TypeManager.IsSubclassOf (source_type, iface))
132 static Expression ExplicitTypeParameterConversion (Expression source, Type target_type)
134 Type source_type = source.Type;
136 if (target_type.IsInterface)
137 return new ClassCast (source, target_type);
139 if (target_type.IsGenericParameter) {
140 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
144 foreach (Type iface in gc.InterfaceConstraints) {
145 if (!iface.IsGenericParameter)
148 if (TypeManager.IsSubclassOf (source_type, iface))
149 return new ClassCast (source, target_type);
156 static EmptyExpression MyEmptyExpr;
157 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
159 Type expr_type = expr.Type;
161 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
162 // if we are a method group, emit a warning
167 if (expr_type == TypeManager.void_type)
170 if (expr_type.IsGenericParameter)
171 return ImplicitTypeParameterConversion (expr, target_type);
174 // notice that it is possible to write "ValueType v = 1", the ValueType here
175 // is an abstract class, and not really a value type, so we apply the same rules.
177 if (target_type == TypeManager.object_type) {
179 // A pointer type cannot be converted to object
181 if (expr_type.IsPointer)
184 if (TypeManager.IsValueType (expr_type))
185 return new BoxedCast (expr, target_type);
186 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
187 if (expr_type == TypeManager.anonymous_method_type)
189 return new EmptyCast (expr, target_type);
193 } else if (target_type == TypeManager.value_type) {
194 if (TypeManager.IsValueType (expr_type))
195 return new BoxedCast (expr, target_type);
196 if (expr_type == TypeManager.null_type)
197 return new NullCast ((Constant)expr, target_type);
200 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
202 // Special case: enumeration to System.Enum.
203 // System.Enum is not a value type, it is a class, so we need
204 // a boxing conversion
206 if (expr_type.IsEnum || expr_type.IsGenericParameter)
207 return new BoxedCast (expr, target_type);
209 return new EmptyCast (expr, target_type);
212 // This code is kind of mirrored inside ImplicitStandardConversionExists
213 // with the small distinction that we only probe there
215 // Always ensure that the code here and there is in sync
217 // from the null type to any reference-type.
218 if (expr_type == TypeManager.null_type){
219 if (target_type.IsPointer)
220 return new EmptyCast (NullPointer.Null, target_type);
222 if (!target_type.IsValueType) {
223 if (expr is Constant)
224 return new NullCast ((Constant)expr, target_type);
226 // I found only one case when it happens -- Foo () ? null : null;
227 Report.Warning (-100, 1, expr.Location, "The result of the expression is always `null'");
228 return new NullCast (new NullLiteral (expr.Location), target_type);
232 // from any class-type S to any interface-type T.
233 if (target_type.IsInterface) {
234 if (target_type != TypeManager.iconvertible_type &&
235 expr_type.IsValueType && (expr is Constant) &&
236 !(expr is IntLiteral || expr is BoolLiteral ||
237 expr is FloatLiteral || expr is DoubleLiteral ||
238 expr is LongLiteral || expr is CharLiteral ||
239 expr is StringLiteral || expr is DecimalLiteral ||
240 expr is UIntLiteral || expr is ULongLiteral)) {
244 if (TypeManager.ImplementsInterface (expr_type, target_type)){
245 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
246 return new BoxedCast (expr, target_type);
248 return new EmptyCast (expr, target_type);
252 // from any interface type S to interface-type T.
253 if (expr_type.IsInterface && target_type.IsInterface) {
254 if (TypeManager.ImplementsInterface (expr_type, target_type))
255 return new EmptyCast (expr, target_type);
260 // from an array-type S to an array-type of type T
261 if (expr_type.IsArray && target_type.IsArray) {
262 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
264 Type expr_element_type = TypeManager.GetElementType (expr_type);
266 if (MyEmptyExpr == null)
267 MyEmptyExpr = new EmptyExpression ();
269 MyEmptyExpr.SetType (expr_element_type);
270 Type target_element_type = TypeManager.GetElementType (target_type);
272 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
273 if (ImplicitStandardConversionExists (MyEmptyExpr,
274 target_element_type))
275 return new EmptyCast (expr, target_type);
279 // from an array-type to System.Array
280 if (expr_type.IsArray && target_type == TypeManager.array_type)
281 return new EmptyCast (expr, target_type);
283 // from an array-type of type T to IEnumerable<T>
284 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
285 return new EmptyCast (expr, target_type);
287 // from any delegate type to System.Delegate
288 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
289 target_type == TypeManager.delegate_type)
290 return new EmptyCast (expr, target_type);
292 // from any array-type or delegate type into System.ICloneable.
293 if (expr_type.IsArray ||
294 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
295 if (target_type == TypeManager.icloneable_type)
296 return new EmptyCast (expr, target_type);
298 // from a generic type definition to a generic instance.
299 if (TypeManager.IsEqual (expr_type, target_type))
300 return new EmptyCast (expr, target_type);
306 // Tests whether an implicit reference conversion exists between expr_type
309 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
311 if (target_type.IsValueType)
314 Type expr_type = expr.Type;
316 if (expr_type.IsGenericParameter)
317 return ImplicitTypeParameterConversion (expr, target_type) != null;
320 // This is the boxed case.
322 if (target_type == TypeManager.object_type) {
323 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
324 expr_type.IsInterface || expr_type == TypeManager.enum_type)
325 if (target_type != TypeManager.anonymous_method_type)
329 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
332 // Please remember that all code below actually comes
333 // from ImplicitReferenceConversion so make sure code remains in sync
335 // from any class-type S to any interface-type T.
336 if (target_type.IsInterface) {
337 if (target_type != TypeManager.iconvertible_type &&
338 expr_type.IsValueType && (expr is Constant) &&
339 !(expr is IntLiteral || expr is BoolLiteral ||
340 expr is FloatLiteral || expr is DoubleLiteral ||
341 expr is LongLiteral || expr is CharLiteral ||
342 expr is StringLiteral || expr is DecimalLiteral ||
343 expr is UIntLiteral || expr is ULongLiteral)) {
347 if (TypeManager.ImplementsInterface (expr_type, target_type))
351 // from any interface type S to interface-type T.
352 if (expr_type.IsInterface && target_type.IsInterface)
353 if (TypeManager.ImplementsInterface (expr_type, target_type))
356 // from an array-type S to an array-type of type T
357 if (expr_type.IsArray && target_type.IsArray) {
358 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
360 Type expr_element_type = expr_type.GetElementType ();
362 if (MyEmptyExpr == null)
363 MyEmptyExpr = new EmptyExpression ();
365 MyEmptyExpr.SetType (expr_element_type);
366 Type target_element_type = TypeManager.GetElementType (target_type);
368 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
369 if (ImplicitStandardConversionExists (MyEmptyExpr,
370 target_element_type))
375 // from an array-type to System.Array
376 if (expr_type.IsArray && (target_type == TypeManager.array_type))
379 // from an array-type of type T to IEnumerable<T>
380 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
383 // from any delegate type to System.Delegate
384 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
385 target_type == TypeManager.delegate_type)
386 if (target_type.IsAssignableFrom (expr_type))
389 // from any array-type or delegate type into System.ICloneable.
390 if (expr_type.IsArray ||
391 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
392 if (target_type == TypeManager.icloneable_type)
395 // from the null type to any reference-type.
396 if (expr_type == TypeManager.null_type){
397 if (target_type.IsPointer)
400 if (!target_type.IsValueType)
404 // from a generic type definition to a generic instance.
405 if (TypeManager.IsEqual (expr_type, target_type))
412 /// Implicit Numeric Conversions.
414 /// expr is the expression to convert, returns a new expression of type
415 /// target_type or null if an implicit conversion is not possible.
417 static public Expression ImplicitNumericConversion (Expression expr,
420 Type expr_type = expr.Type;
423 // Attempt to do the implicit constant expression conversions
425 if (expr is Constant){
426 if (expr is IntConstant){
429 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
433 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
435 // Try the implicit constant expression conversion
436 // from long to ulong, instead of a nice routine,
439 long v = ((LongConstant) expr).Value;
441 return new ULongConstant ((ulong) v, expr.Location);
445 Type real_target_type = target_type;
447 if (expr_type == TypeManager.sbyte_type){
449 // From sbyte to short, int, long, float, double, decimal
451 if (real_target_type == TypeManager.int32_type)
452 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
453 if (real_target_type == TypeManager.int64_type)
454 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
455 if (real_target_type == TypeManager.double_type)
456 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
457 if (real_target_type == TypeManager.float_type)
458 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
459 if (real_target_type == TypeManager.short_type)
460 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
461 if (real_target_type == TypeManager.decimal_type)
462 return new CastToDecimal (expr);
463 } else if (expr_type == TypeManager.byte_type){
465 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
467 if ((real_target_type == TypeManager.short_type) ||
468 (real_target_type == TypeManager.ushort_type) ||
469 (real_target_type == TypeManager.int32_type) ||
470 (real_target_type == TypeManager.uint32_type))
471 return new EmptyCast (expr, target_type);
473 if (real_target_type == TypeManager.uint64_type)
474 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
475 if (real_target_type == TypeManager.int64_type)
476 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
477 if (real_target_type == TypeManager.float_type)
478 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
479 if (real_target_type == TypeManager.double_type)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
481 if (real_target_type == TypeManager.decimal_type)
482 return new CastToDecimal (expr);
484 } else if (expr_type == TypeManager.short_type){
486 // From short to int, long, float, double, decimal
488 if (real_target_type == TypeManager.int32_type)
489 return new EmptyCast (expr, target_type);
490 if (real_target_type == TypeManager.int64_type)
491 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
492 if (real_target_type == TypeManager.double_type)
493 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
494 if (real_target_type == TypeManager.float_type)
495 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
496 if (real_target_type == TypeManager.decimal_type)
497 return new CastToDecimal (expr);
499 } else if (expr_type == TypeManager.ushort_type){
501 // From ushort to int, uint, long, ulong, float, double, decimal
503 if (real_target_type == TypeManager.uint32_type)
504 return new EmptyCast (expr, target_type);
506 if (real_target_type == TypeManager.uint64_type)
507 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
508 if (real_target_type == TypeManager.int32_type)
509 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
510 if (real_target_type == TypeManager.int64_type)
511 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
512 if (real_target_type == TypeManager.double_type)
513 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
514 if (real_target_type == TypeManager.float_type)
515 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
516 if (real_target_type == TypeManager.decimal_type)
517 return new CastToDecimal (expr);
518 } else if (expr_type == TypeManager.int32_type){
520 // From int to long, float, double, decimal
522 if (real_target_type == TypeManager.int64_type)
523 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
524 if (real_target_type == TypeManager.double_type)
525 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
526 if (real_target_type == TypeManager.float_type)
527 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
528 if (real_target_type == TypeManager.decimal_type)
529 return new CastToDecimal (expr);
530 } else if (expr_type == TypeManager.uint32_type){
532 // From uint to long, ulong, float, double, decimal
534 if (real_target_type == TypeManager.int64_type)
535 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
536 if (real_target_type == TypeManager.uint64_type)
537 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
538 if (real_target_type == TypeManager.double_type)
539 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
541 if (real_target_type == TypeManager.float_type)
542 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
544 if (real_target_type == TypeManager.decimal_type)
545 return new CastToDecimal (expr);
546 } else if (expr_type == TypeManager.int64_type){
548 // From long/ulong to float, double
550 if (real_target_type == TypeManager.double_type)
551 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
552 if (real_target_type == TypeManager.float_type)
553 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
554 if (real_target_type == TypeManager.decimal_type)
555 return new CastToDecimal (expr);
556 } else if (expr_type == TypeManager.uint64_type){
558 // From ulong to float, double
560 if (real_target_type == TypeManager.double_type)
561 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
563 if (real_target_type == TypeManager.float_type)
564 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
566 if (real_target_type == TypeManager.decimal_type)
567 return new CastToDecimal (expr);
568 } else if (expr_type == TypeManager.char_type){
570 // From char to ushort, int, uint, long, ulong, float, double, decimal
572 if ((real_target_type == TypeManager.ushort_type) ||
573 (real_target_type == TypeManager.int32_type) ||
574 (real_target_type == TypeManager.uint32_type))
575 return new EmptyCast (expr, target_type);
576 if (real_target_type == TypeManager.uint64_type)
577 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
578 if (real_target_type == TypeManager.int64_type)
579 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
580 if (real_target_type == TypeManager.float_type)
581 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
582 if (real_target_type == TypeManager.double_type)
583 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
584 if (real_target_type == TypeManager.decimal_type)
585 return new CastToDecimal (expr);
586 } else if (expr_type == TypeManager.float_type){
590 if (real_target_type == TypeManager.double_type)
591 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
598 /// Same as ImplicitStandardConversionExists except that it also looks at
599 /// implicit user defined conversions - needed for overload resolution
601 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
603 if (expr is NullLiteral) {
604 if (target_type.IsGenericParameter)
605 return TypeParameter_to_Null (target_type);
607 if (TypeManager.IsNullableType (target_type))
611 if (ImplicitStandardConversionExists (expr, target_type))
614 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
617 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
619 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
623 /// Determines if a standard implicit conversion exists from
624 /// expr_type to target_type
626 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
628 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
630 Type expr_type = expr.Type;
632 if (expr_type == TypeManager.void_type)
635 //Console.WriteLine ("Expr is {0}", expr);
636 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
637 if (expr_type.Equals (target_type))
641 // First numeric conversions
643 if (expr_type == TypeManager.sbyte_type){
645 // From sbyte to short, int, long, float, double, decimal
647 if ((target_type == TypeManager.int32_type) ||
648 (target_type == TypeManager.int64_type) ||
649 (target_type == TypeManager.double_type) ||
650 (target_type == TypeManager.float_type) ||
651 (target_type == TypeManager.short_type) ||
652 (target_type == TypeManager.decimal_type))
655 } else if (expr_type == TypeManager.byte_type){
657 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
659 if ((target_type == TypeManager.short_type) ||
660 (target_type == TypeManager.ushort_type) ||
661 (target_type == TypeManager.int32_type) ||
662 (target_type == TypeManager.uint32_type) ||
663 (target_type == TypeManager.uint64_type) ||
664 (target_type == TypeManager.int64_type) ||
665 (target_type == TypeManager.float_type) ||
666 (target_type == TypeManager.double_type) ||
667 (target_type == TypeManager.decimal_type))
670 } else if (expr_type == TypeManager.short_type){
672 // From short to int, long, double, float, decimal
674 if ((target_type == TypeManager.int32_type) ||
675 (target_type == TypeManager.int64_type) ||
676 (target_type == TypeManager.double_type) ||
677 (target_type == TypeManager.float_type) ||
678 (target_type == TypeManager.decimal_type))
681 } else if (expr_type == TypeManager.ushort_type){
683 // From ushort to int, uint, long, ulong, double, float, decimal
685 if ((target_type == TypeManager.uint32_type) ||
686 (target_type == TypeManager.uint64_type) ||
687 (target_type == TypeManager.int32_type) ||
688 (target_type == TypeManager.int64_type) ||
689 (target_type == TypeManager.double_type) ||
690 (target_type == TypeManager.float_type) ||
691 (target_type == TypeManager.decimal_type))
694 } else if (expr_type == TypeManager.int32_type){
696 // From int to long, double, float, decimal
698 if ((target_type == TypeManager.int64_type) ||
699 (target_type == TypeManager.double_type) ||
700 (target_type == TypeManager.float_type) ||
701 (target_type == TypeManager.decimal_type))
704 } else if (expr_type == TypeManager.uint32_type){
706 // From uint to long, ulong, double, float, decimal
708 if ((target_type == TypeManager.int64_type) ||
709 (target_type == TypeManager.uint64_type) ||
710 (target_type == TypeManager.double_type) ||
711 (target_type == TypeManager.float_type) ||
712 (target_type == TypeManager.decimal_type))
715 } else if ((expr_type == TypeManager.uint64_type) ||
716 (expr_type == TypeManager.int64_type)) {
718 // From long/ulong to double, float, decimal
720 if ((target_type == TypeManager.double_type) ||
721 (target_type == TypeManager.float_type) ||
722 (target_type == TypeManager.decimal_type))
725 } else if (expr_type == TypeManager.char_type){
727 // From char to ushort, int, uint, ulong, long, float, double, decimal
729 if ((target_type == TypeManager.ushort_type) ||
730 (target_type == TypeManager.int32_type) ||
731 (target_type == TypeManager.uint32_type) ||
732 (target_type == TypeManager.uint64_type) ||
733 (target_type == TypeManager.int64_type) ||
734 (target_type == TypeManager.float_type) ||
735 (target_type == TypeManager.double_type) ||
736 (target_type == TypeManager.decimal_type))
739 } else if (expr_type == TypeManager.float_type){
743 if (target_type == TypeManager.double_type)
747 if (expr.eclass == ExprClass.MethodGroup){
748 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
749 MethodGroupExpr mg = expr as MethodGroupExpr;
751 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
756 if (ImplicitReferenceConversionExists (expr, target_type))
760 // Implicit Constant Expression Conversions
762 if (expr is IntConstant){
763 int value = ((IntConstant) expr).Value;
765 if (target_type == TypeManager.sbyte_type){
766 if (value >= SByte.MinValue && value <= SByte.MaxValue)
768 } else if (target_type == TypeManager.byte_type){
769 if (value >= 0 && value <= Byte.MaxValue)
771 } else if (target_type == TypeManager.short_type){
772 if (value >= Int16.MinValue && value <= Int16.MaxValue)
774 } else if (target_type == TypeManager.ushort_type){
775 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
777 } else if (target_type == TypeManager.uint32_type){
780 } else if (target_type == TypeManager.uint64_type){
782 // we can optimize this case: a positive int32
783 // always fits on a uint64. But we need an opcode
790 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
794 if (expr is LongConstant && target_type == TypeManager.uint64_type){
796 // Try the implicit constant expression conversion
797 // from long to ulong, instead of a nice routine,
800 long v = ((LongConstant) expr).Value;
805 if ((target_type == TypeManager.enum_type ||
806 target_type.IsSubclassOf (TypeManager.enum_type)) &&
808 IntLiteral i = (IntLiteral) expr;
815 // If `expr_type' implements `target_type' (which is an iface)
816 // see TryImplicitIntConversion
818 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
821 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
824 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
827 if (TypeManager.IsNullableTypeOf (target_type, expr_type))
830 if (expr_type == TypeManager.anonymous_method_type){
831 if (!TypeManager.IsDelegateType (target_type))
834 AnonymousMethod am = (AnonymousMethod) expr;
835 return am.ImplicitStandardConversionExists (target_type);
842 /// Finds "most encompassed type" according to the spec (13.4.2)
843 /// amongst the methods in the MethodGroupExpr
845 static Type FindMostEncompassedType (ArrayList types)
849 if (types.Count == 0)
852 if (types.Count == 1)
853 return (Type) types [0];
855 EmptyExpression expr = EmptyExpression.Grab ();
857 foreach (Type t in types) {
864 if (ImplicitStandardConversionExists (expr, best))
869 foreach (Type t in types) {
872 if (!ImplicitStandardConversionExists (expr, t)) {
878 EmptyExpression.Release (expr);
884 /// Finds "most encompassing type" according to the spec (13.4.2)
885 /// amongst the types in the given set
887 static Type FindMostEncompassingType (ArrayList types)
891 if (types.Count == 0)
894 if (types.Count == 1)
895 return (Type) types [0];
897 EmptyExpression expr = EmptyExpression.Grab ();
899 foreach (Type t in types) {
906 if (ImplicitStandardConversionExists (expr, t))
910 foreach (Type t in types) {
914 if (!ImplicitStandardConversionExists (expr, best)) {
920 EmptyExpression.Release (expr);
926 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
927 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
928 /// for explicit and implicit conversion operators.
930 static public Type FindMostSpecificSource (IList list,
931 Expression source, bool apply_explicit_conv_rules)
933 ArrayList src_types_set = new ArrayList ();
936 // If any operator converts from S then Sx = S
938 Type source_type = source.Type;
939 foreach (MethodBase mb in list){
940 ParameterData pd = TypeManager.GetParameterData (mb);
941 Type param_type = pd.ParameterType (0);
943 if (param_type == source_type)
946 src_types_set.Add (param_type);
950 // Explicit Conv rules
952 if (apply_explicit_conv_rules) {
953 ArrayList candidate_set = new ArrayList ();
955 foreach (Type param_type in src_types_set){
956 if (ImplicitStandardConversionExists (source, param_type))
957 candidate_set.Add (param_type);
960 if (candidate_set.Count != 0)
961 return FindMostEncompassedType (candidate_set);
967 if (apply_explicit_conv_rules)
968 return FindMostEncompassingType (src_types_set);
970 return FindMostEncompassedType (src_types_set);
974 /// Finds the most specific target Tx according to section 13.4.4
976 static public Type FindMostSpecificTarget (IList list,
977 Type target, bool apply_explicit_conv_rules)
979 ArrayList tgt_types_set = new ArrayList ();
982 // If any operator converts to T then Tx = T
984 foreach (MethodInfo mi in list){
985 Type ret_type = mi.ReturnType;
986 if (ret_type == target)
989 tgt_types_set.Add (ret_type);
993 // Explicit conv rules
995 if (apply_explicit_conv_rules) {
996 ArrayList candidate_set = new ArrayList ();
998 EmptyExpression expr = EmptyExpression.Grab ();
1000 foreach (Type ret_type in tgt_types_set){
1001 expr.SetType (ret_type);
1003 if (ImplicitStandardConversionExists (expr, target))
1004 candidate_set.Add (ret_type);
1007 EmptyExpression.Release (expr);
1009 if (candidate_set.Count != 0)
1010 return FindMostEncompassingType (candidate_set);
1014 // Okay, final case !
1016 if (apply_explicit_conv_rules)
1017 return FindMostEncompassedType (tgt_types_set);
1019 return FindMostEncompassingType (tgt_types_set);
1023 /// User-defined Implicit conversions
1025 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1026 Type target, Location loc)
1028 return UserDefinedConversion (ec, source, target, loc, false);
1032 /// User-defined Explicit conversions
1034 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1035 Type target, Location loc)
1037 return UserDefinedConversion (ec, source, target, loc, true);
1040 static void AddConversionOperators (ArrayList list,
1041 Expression source, Type target_type,
1042 bool look_for_explicit,
1048 Type source_type = source.Type;
1049 EmptyExpression expr = EmptyExpression.Grab ();
1050 foreach (MethodInfo m in mg.Methods) {
1051 ParameterData pd = TypeManager.GetParameterData (m);
1052 Type return_type = m.ReturnType;
1053 Type arg_type = pd.ParameterType (0);
1055 if (source_type != arg_type) {
1056 if (!ImplicitStandardConversionExists (source, arg_type)) {
1057 if (!look_for_explicit)
1059 expr.SetType (arg_type);
1060 if (!ImplicitStandardConversionExists (expr, source_type))
1065 if (target_type != return_type) {
1066 expr.SetType (return_type);
1067 if (!ImplicitStandardConversionExists (expr, target_type)) {
1068 if (!look_for_explicit)
1070 expr.SetType (target_type);
1071 if (!ImplicitStandardConversionExists (expr, return_type))
1079 EmptyExpression.Release (expr);
1083 /// Compute the user-defined conversion operator from source_type to target_type.
1084 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1086 static MethodInfo GetConversionOperator (EmitContext ec, Expression source, Type target_type, bool look_for_explicit)
1088 ArrayList ops = new ArrayList (4);
1090 Type source_type = source.Type;
1092 if (source_type != TypeManager.decimal_type) {
1093 AddConversionOperators (ops, source, target_type, look_for_explicit,
1094 Expression.MethodLookup (ec, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1095 if (look_for_explicit) {
1096 AddConversionOperators (ops, source, target_type, look_for_explicit,
1097 Expression.MethodLookup (
1098 ec, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1102 if (target_type != TypeManager.decimal_type) {
1103 AddConversionOperators (ops, source, target_type, look_for_explicit,
1104 Expression.MethodLookup (ec, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1105 if (look_for_explicit) {
1106 AddConversionOperators (ops, source, target_type, look_for_explicit,
1107 Expression.MethodLookup (
1108 ec, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1115 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1116 if (most_specific_source == null)
1119 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1120 if (most_specific_target == null)
1123 MethodInfo method = null;
1125 foreach (MethodInfo m in ops) {
1126 if (m.ReturnType != most_specific_target)
1128 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1130 // Ambiguous: more than one conversion operator satisfies the signature.
1139 static DoubleHash explicit_conv = new DoubleHash (100);
1140 static DoubleHash implicit_conv = new DoubleHash (100);
1143 /// User-defined conversions
1145 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1146 Type target, Location loc,
1147 bool look_for_explicit)
1149 Type source_type = source.Type;
1150 MethodInfo method = null;
1152 if (TypeManager.IsNullableType (source_type) && TypeManager.IsNullableType (target))
1153 return new Nullable.LiftedConversion (
1154 source, target, true, look_for_explicit, loc).Resolve (ec);
1157 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1159 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1160 method = (MethodInfo) o;
1162 method = GetConversionOperator (ec, source, target, look_for_explicit);
1163 if (!(source is Constant))
1164 hash.Insert (source_type, target, method);
1170 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1173 // This will do the conversion to the best match that we
1174 // found. Now we need to perform an implict standard conversion
1175 // if the best match was not the type that we were requested
1178 if (look_for_explicit)
1179 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1181 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1187 e = new UserCast (method, source, loc);
1188 if (e.Type != target){
1189 if (!look_for_explicit)
1190 e = ImplicitConversionStandard (ec, e, target, loc);
1192 e = ExplicitConversionStandard (ec, e, target, loc);
1199 /// Converts implicitly the resolved expression `expr' into the
1200 /// `target_type'. It returns a new expression that can be used
1201 /// in a context that expects a `target_type'.
1203 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1204 Type target_type, Location loc)
1208 if (target_type == null)
1209 throw new Exception ("Target type is null");
1211 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1215 e = ImplicitUserConversion (ec, expr, target_type, loc);
1224 /// Attempts to apply the `Standard Implicit
1225 /// Conversion' rules to the expression `expr' into
1226 /// the `target_type'. It returns a new expression
1227 /// that can be used in a context that expects a
1230 /// This is different from `ImplicitConversion' in that the
1231 /// user defined implicit conversions are excluded.
1233 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1234 Type target_type, Location loc)
1236 Type expr_type = expr.Type;
1239 if (expr is NullLiteral) {
1240 if (target_type.IsGenericParameter)
1241 return TypeParameter_to_Null (target_type, loc);
1243 if (TypeManager.IsNullableType (target_type))
1244 return new Nullable.NullableLiteral (target_type, loc);
1247 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1248 return new Nullable.LiftedConversion (
1249 expr, target_type, false, false, loc).Resolve (ec);
1251 if (expr.eclass == ExprClass.MethodGroup){
1252 if (!TypeManager.IsDelegateType (target_type)){
1257 // Only allow anonymous method conversions on post ISO_1
1259 if (RootContext.Version != LanguageVersion.ISO_1){
1260 MethodGroupExpr mg = expr as MethodGroupExpr;
1262 return ImplicitDelegateCreation.Create (
1263 ec, mg, target_type, loc);
1267 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1270 e = ImplicitNumericConversion (expr, target_type);
1274 e = ImplicitReferenceConversion (expr, target_type);
1278 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1279 IntLiteral i = (IntLiteral) expr;
1282 return new EnumConstant ((Constant) expr, target_type);
1286 if (expr_type.IsPointer){
1287 if (target_type == TypeManager.void_ptr_type)
1288 return new EmptyCast (expr, target_type);
1291 // yep, comparing pointer types cant be done with
1292 // t1 == t2, we have to compare their element types.
1294 if (target_type.IsPointer){
1295 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1302 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1303 return new EmptyCast (NullPointer.Null, target_type);
1306 if (TypeManager.IsNullableTypeOf (target_type, expr_type))
1307 return new Nullable.Wrap (expr, loc).Resolve (ec);
1309 if (expr_type == TypeManager.anonymous_method_type){
1310 if (!TypeManager.IsDelegateType (target_type)){
1311 Report.Error (1660, loc,
1312 "Cannot convert anonymous method block to type `{0}' because it is not a delegate type",
1313 TypeManager.CSharpName (target_type));
1317 AnonymousMethod am = (AnonymousMethod) expr;
1318 int errors = Report.Errors;
1320 Expression conv = am.Compatible (ec, target_type);
1325 // We return something instead of null, to avoid
1326 // the duplicate error, since am.Compatible would have
1327 // reported that already
1329 if (errors != Report.Errors)
1330 return new EmptyCast (expr, target_type);
1337 /// Attempts to perform an implicit constant conversion of the IntConstant
1338 /// into a different data type using casts (See Implicit Constant
1339 /// Expression Conversions)
1341 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1343 int value = ic.Value;
1345 if (target_type == TypeManager.sbyte_type){
1346 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1347 return new SByteConstant ((sbyte) value, ic.Location);
1348 } else if (target_type == TypeManager.byte_type){
1349 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1350 return new ByteConstant ((byte) value, ic.Location);
1351 } else if (target_type == TypeManager.short_type){
1352 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1353 return new ShortConstant ((short) value, ic.Location);
1354 } else if (target_type == TypeManager.ushort_type){
1355 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1356 return new UShortConstant ((ushort) value, ic.Location);
1357 } else if (target_type == TypeManager.uint32_type){
1359 return new UIntConstant ((uint) value, ic.Location);
1360 } else if (target_type == TypeManager.uint64_type){
1362 // we can optimize this case: a positive int32
1363 // always fits on a uint64. But we need an opcode
1367 return new ULongConstant ((ulong) value, ic.Location);
1368 } else if (target_type == TypeManager.double_type)
1369 return new DoubleConstant ((double) value, ic.Location);
1370 else if (target_type == TypeManager.float_type)
1371 return new FloatConstant ((float) value, ic.Location);
1373 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1374 Type underlying = TypeManager.EnumToUnderlying (target_type);
1375 Constant e = (Constant) ic;
1378 // Possibly, we need to create a different 0 literal before passing
1381 if (underlying == TypeManager.int64_type)
1382 e = new LongLiteral (0, ic.Location);
1383 else if (underlying == TypeManager.uint64_type)
1384 e = new ULongLiteral (0, ic.Location);
1386 return new EnumConstant (e, target_type);
1390 // If `target_type' is an interface and the type of `ic' implements the interface
1391 // e.g. target_type is IComparable, IConvertible, IFormattable
1393 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1394 return new BoxedCast (ic, target_type);
1400 /// Attempts to implicitly convert `source' into `target_type', using
1401 /// ImplicitConversion. If there is no implicit conversion, then
1402 /// an error is signaled
1404 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1405 Type target_type, Location loc)
1407 Expression e = ImplicitConversion (ec, source, target_type, loc);
1411 source.Error_ValueCannotBeConverted (loc, target_type, false);
1416 /// Performs the explicit numeric conversions
1418 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1420 Type expr_type = expr.Type;
1421 Type real_target_type = target_type;
1423 if (expr_type == TypeManager.sbyte_type){
1425 // From sbyte to byte, ushort, uint, ulong, char
1427 if (real_target_type == TypeManager.byte_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1429 if (real_target_type == TypeManager.ushort_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1431 if (real_target_type == TypeManager.uint32_type)
1432 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1433 if (real_target_type == TypeManager.uint64_type)
1434 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1435 if (real_target_type == TypeManager.char_type)
1436 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1437 } else if (expr_type == TypeManager.byte_type){
1439 // From byte to sbyte and char
1441 if (real_target_type == TypeManager.sbyte_type)
1442 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1443 if (real_target_type == TypeManager.char_type)
1444 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1445 } else if (expr_type == TypeManager.short_type){
1447 // From short to sbyte, byte, ushort, uint, ulong, char
1449 if (real_target_type == TypeManager.sbyte_type)
1450 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1451 if (real_target_type == TypeManager.byte_type)
1452 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1453 if (real_target_type == TypeManager.ushort_type)
1454 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1455 if (real_target_type == TypeManager.uint32_type)
1456 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1457 if (real_target_type == TypeManager.uint64_type)
1458 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1459 if (real_target_type == TypeManager.char_type)
1460 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1461 } else if (expr_type == TypeManager.ushort_type){
1463 // From ushort to sbyte, byte, short, char
1465 if (real_target_type == TypeManager.sbyte_type)
1466 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1467 if (real_target_type == TypeManager.byte_type)
1468 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1469 if (real_target_type == TypeManager.short_type)
1470 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1471 if (real_target_type == TypeManager.char_type)
1472 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1473 } else if (expr_type == TypeManager.int32_type){
1475 // From int to sbyte, byte, short, ushort, uint, ulong, char
1477 if (real_target_type == TypeManager.sbyte_type)
1478 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1479 if (real_target_type == TypeManager.byte_type)
1480 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1481 if (real_target_type == TypeManager.short_type)
1482 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1483 if (real_target_type == TypeManager.ushort_type)
1484 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1485 if (real_target_type == TypeManager.uint32_type)
1486 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1487 if (real_target_type == TypeManager.uint64_type)
1488 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1489 if (real_target_type == TypeManager.char_type)
1490 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1491 } else if (expr_type == TypeManager.uint32_type){
1493 // From uint to sbyte, byte, short, ushort, int, char
1495 if (real_target_type == TypeManager.sbyte_type)
1496 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1497 if (real_target_type == TypeManager.byte_type)
1498 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1499 if (real_target_type == TypeManager.short_type)
1500 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1501 if (real_target_type == TypeManager.ushort_type)
1502 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1503 if (real_target_type == TypeManager.int32_type)
1504 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1505 if (real_target_type == TypeManager.char_type)
1506 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1507 } else if (expr_type == TypeManager.int64_type){
1509 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1511 if (real_target_type == TypeManager.sbyte_type)
1512 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1513 if (real_target_type == TypeManager.byte_type)
1514 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1515 if (real_target_type == TypeManager.short_type)
1516 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1517 if (real_target_type == TypeManager.ushort_type)
1518 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1519 if (real_target_type == TypeManager.int32_type)
1520 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1521 if (real_target_type == TypeManager.uint32_type)
1522 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1523 if (real_target_type == TypeManager.uint64_type)
1524 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1525 if (real_target_type == TypeManager.char_type)
1526 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1527 } else if (expr_type == TypeManager.uint64_type){
1529 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1531 if (real_target_type == TypeManager.sbyte_type)
1532 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1533 if (real_target_type == TypeManager.byte_type)
1534 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1535 if (real_target_type == TypeManager.short_type)
1536 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1537 if (real_target_type == TypeManager.ushort_type)
1538 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1539 if (real_target_type == TypeManager.int32_type)
1540 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1541 if (real_target_type == TypeManager.uint32_type)
1542 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1543 if (real_target_type == TypeManager.int64_type)
1544 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1545 if (real_target_type == TypeManager.char_type)
1546 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1547 } else if (expr_type == TypeManager.char_type){
1549 // From char to sbyte, byte, short
1551 if (real_target_type == TypeManager.sbyte_type)
1552 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1553 if (real_target_type == TypeManager.byte_type)
1554 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1555 if (real_target_type == TypeManager.short_type)
1556 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1557 } else if (expr_type == TypeManager.float_type){
1559 // From float to sbyte, byte, short,
1560 // ushort, int, uint, long, ulong, char
1563 if (real_target_type == TypeManager.sbyte_type)
1564 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1565 if (real_target_type == TypeManager.byte_type)
1566 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1567 if (real_target_type == TypeManager.short_type)
1568 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1569 if (real_target_type == TypeManager.ushort_type)
1570 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1571 if (real_target_type == TypeManager.int32_type)
1572 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1573 if (real_target_type == TypeManager.uint32_type)
1574 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1575 if (real_target_type == TypeManager.int64_type)
1576 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1577 if (real_target_type == TypeManager.uint64_type)
1578 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1579 if (real_target_type == TypeManager.char_type)
1580 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1581 if (real_target_type == TypeManager.decimal_type)
1582 return new CastToDecimal (expr, true);
1583 } else if (expr_type == TypeManager.double_type){
1585 // From double to sbyte, byte, short,
1586 // ushort, int, uint, long, ulong,
1587 // char, float or decimal
1589 if (real_target_type == TypeManager.sbyte_type)
1590 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1591 if (real_target_type == TypeManager.byte_type)
1592 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1593 if (real_target_type == TypeManager.short_type)
1594 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1595 if (real_target_type == TypeManager.ushort_type)
1596 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1597 if (real_target_type == TypeManager.int32_type)
1598 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1599 if (real_target_type == TypeManager.uint32_type)
1600 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1601 if (real_target_type == TypeManager.int64_type)
1602 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1603 if (real_target_type == TypeManager.uint64_type)
1604 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1605 if (real_target_type == TypeManager.char_type)
1606 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1607 if (real_target_type == TypeManager.float_type)
1608 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1609 if (real_target_type == TypeManager.decimal_type)
1610 return new CastToDecimal (expr, true);
1611 } else if (expr_type == TypeManager.decimal_type) {
1612 return new CastFromDecimal (expr, target_type).Resolve ();
1618 /// Returns whether an explicit reference conversion can be performed
1619 /// from source_type to target_type
1621 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1623 bool target_is_type_param = target_type.IsGenericParameter;
1624 bool target_is_value_type = target_type.IsValueType;
1626 if (source_type == target_type)
1630 // From generic parameter to any type
1632 if (source_type.IsGenericParameter)
1633 return ExplicitTypeParameterConversionExists (source_type, target_type);
1636 // From object to a generic parameter
1638 if (source_type == TypeManager.object_type && target_is_type_param)
1642 // From object to any reference type
1644 if (source_type == TypeManager.object_type && !target_is_value_type)
1648 // From any class S to any class-type T, provided S is a base class of T
1650 if (TypeManager.IsSubclassOf (target_type, source_type))
1654 // From any interface type S to any interface T provided S is not derived from T
1656 if (source_type.IsInterface && target_type.IsInterface){
1657 if (!TypeManager.IsSubclassOf (target_type, source_type))
1662 // From any class type S to any interface T, provided S is not sealed
1663 // and provided S does not implement T.
1665 if (target_type.IsInterface && !source_type.IsSealed &&
1666 !TypeManager.ImplementsInterface (source_type, target_type))
1670 // From any interface-type S to to any class type T, provided T is not
1671 // sealed, or provided T implements S.
1673 if (source_type.IsInterface &&
1674 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1678 // From an array type S with an element type Se to an array type T with an
1679 // element type Te provided all the following are true:
1680 // * S and T differe only in element type, in other words, S and T
1681 // have the same number of dimensions.
1682 // * Both Se and Te are reference types
1683 // * An explicit referenc conversions exist from Se to Te
1685 if (source_type.IsArray && target_type.IsArray) {
1686 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1688 Type source_element_type = TypeManager.GetElementType (source_type);
1689 Type target_element_type = TypeManager.GetElementType (target_type);
1691 if (source_element_type.IsGenericParameter ||
1692 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1693 if (ExplicitReferenceConversionExists (source_element_type,
1694 target_element_type))
1700 // From System.Array to any array-type
1701 if (source_type == TypeManager.array_type &&
1702 target_type.IsArray){
1707 // From System delegate to any delegate-type
1709 if (source_type == TypeManager.delegate_type &&
1710 TypeManager.IsDelegateType (target_type))
1714 // From ICloneable to Array or Delegate types
1716 if (source_type == TypeManager.icloneable_type &&
1717 (target_type == TypeManager.array_type ||
1718 target_type == TypeManager.delegate_type))
1725 /// Implements Explicit Reference conversions
1727 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1729 Type source_type = source.Type;
1730 bool target_is_type_param = target_type.IsGenericParameter;
1731 bool target_is_value_type = target_type.IsValueType;
1734 // From object to a generic parameter
1736 if (source_type == TypeManager.object_type && target_is_type_param)
1737 return new UnboxCast (source, target_type);
1740 // Explicit type parameter conversion.
1743 if (source_type.IsGenericParameter)
1744 return ExplicitTypeParameterConversion (source, target_type);
1747 // From object to any reference type
1749 if (source_type == TypeManager.object_type && !target_is_value_type)
1750 return new ClassCast (source, target_type);
1753 // Unboxing conversion.
1755 if (((source_type == TypeManager.enum_type &&
1756 !(source is EmptyCast)) ||
1757 source_type == TypeManager.value_type) && target_is_value_type)
1758 return new UnboxCast (source, target_type);
1761 // From any class S to any class-type T, provided S is a base class of T
1763 if (TypeManager.IsSubclassOf (target_type, source_type))
1764 return new ClassCast (source, target_type);
1767 // From any interface type S to any interface T provided S is not derived from T
1769 if (source_type.IsInterface && target_type.IsInterface){
1770 if (TypeManager.ImplementsInterface (source_type, target_type))
1773 return new ClassCast (source, target_type);
1777 // From any class type S to any interface T, provides S is not sealed
1778 // and provided S does not implement T.
1780 if (target_type.IsInterface && !source_type.IsSealed) {
1781 if (TypeManager.ImplementsInterface (source_type, target_type))
1784 return new ClassCast (source, target_type);
1789 // From any interface-type S to to any class type T, provided T is not
1790 // sealed, or provided T implements S.
1792 if (source_type.IsInterface) {
1793 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1794 if (target_type.IsClass)
1795 return new ClassCast (source, target_type);
1797 return new UnboxCast (source, target_type);
1803 // From an array type S with an element type Se to an array type T with an
1804 // element type Te provided all the following are true:
1805 // * S and T differe only in element type, in other words, S and T
1806 // have the same number of dimensions.
1807 // * Both Se and Te are reference types
1808 // * An explicit referenc conversions exist from Se to Te
1810 if (source_type.IsArray && target_type.IsArray) {
1811 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1813 Type source_element_type = TypeManager.GetElementType (source_type);
1814 Type target_element_type = TypeManager.GetElementType (target_type);
1816 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1817 if (ExplicitReferenceConversionExists (source_element_type,
1818 target_element_type))
1819 return new ClassCast (source, target_type);
1824 // From System.Array to any array-type
1825 if (source_type == TypeManager.array_type &&
1826 target_type.IsArray) {
1827 return new ClassCast (source, target_type);
1831 // From System delegate to any delegate-type
1833 if (source_type == TypeManager.delegate_type &&
1834 TypeManager.IsDelegateType (target_type))
1835 return new ClassCast (source, target_type);
1838 // From ICloneable to Array or Delegate types
1840 if (source_type == TypeManager.icloneable_type &&
1841 (target_type == TypeManager.array_type ||
1842 target_type == TypeManager.delegate_type))
1843 return new ClassCast (source, target_type);
1849 /// Performs an explicit conversion of the expression `expr' whose
1850 /// type is expr.Type to `target_type'.
1852 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1853 Type target_type, Location loc)
1855 Type expr_type = expr.Type;
1857 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1858 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1863 // Unboxing conversions; only object types can be convertible to enum
1865 if (expr_type == TypeManager.object_type && target_type.IsValueType || expr_type == TypeManager.enum_type)
1866 return new UnboxCast (expr, target_type);
1868 if (TypeManager.IsEnumType (expr_type)) {
1869 if (expr is EnumConstant)
1870 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1872 return ExplicitConversionCore (ec, new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1875 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1876 return new Nullable.LiftedConversion (
1877 expr, target_type, false, true, loc).Resolve (ec);
1879 if (TypeManager.IsEnumType (target_type)){
1880 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1882 return new EmptyCast (ce, target_type);
1885 ne = ExplicitNumericConversion (expr, target_type);
1890 // Skip the ExplicitReferenceConversion because we can not convert
1891 // from Null to a ValueType, and ExplicitReference wont check against
1892 // null literal explicitly
1894 if (expr_type != TypeManager.null_type){
1895 ne = ExplicitReferenceConversion (expr, target_type);
1901 ne = ExplicitUnsafe (expr, target_type);
1906 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1913 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1915 Type expr_type = expr.Type;
1917 if (target_type.IsPointer){
1918 if (expr_type.IsPointer)
1919 return new EmptyCast (expr, target_type);
1921 if (expr_type == TypeManager.sbyte_type ||
1922 expr_type == TypeManager.short_type ||
1923 expr_type == TypeManager.int32_type ||
1924 expr_type == TypeManager.int64_type)
1925 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1927 if (expr_type == TypeManager.ushort_type ||
1928 expr_type == TypeManager.uint32_type ||
1929 expr_type == TypeManager.uint64_type ||
1930 expr_type == TypeManager.byte_type)
1931 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1934 if (expr_type.IsPointer){
1935 if (target_type == TypeManager.sbyte_type)
1936 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1937 else if (target_type == TypeManager.byte_type)
1938 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1939 else if (target_type == TypeManager.short_type)
1940 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1941 else if (target_type == TypeManager.ushort_type)
1942 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1943 else if (target_type == TypeManager.int32_type)
1944 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1945 else if (target_type == TypeManager.uint32_type)
1946 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1947 else if (target_type == TypeManager.uint64_type)
1948 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1949 else if (target_type == TypeManager.int64_type){
1950 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1957 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1959 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1960 Type target_type, Location l)
1962 int errors = Report.Errors;
1963 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1964 if (Report.Errors > errors)
1970 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
1971 return new Nullable.LiftedConversion (
1972 expr, target_type, false, true, l).Resolve (ec);
1974 ne = ExplicitNumericConversion (expr, target_type);
1978 ne = ExplicitReferenceConversion (expr, target_type);
1982 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1983 return new EmptyCast (expr, target_type);
1985 expr.Error_ValueCannotBeConverted (l, target_type, true);
1990 /// Performs an explicit conversion of the expression `expr' whose
1991 /// type is expr.Type to `target_type'.
1993 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1994 Type target_type, Location loc)
1996 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
2000 expr.Error_ValueCannotBeConverted (loc, target_type, true);