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 Expression TypeParameter_to_Null (Expression expr, Type target_type,
35 if (!TypeParameter_to_Null (target_type)) {
36 Report.Error (403, loc, "Cannot convert null to the type " +
37 "parameter `{0}' becaues it could be a value " +
38 "type. Consider using `default ({0})' instead.",
43 return new NullCast (expr, target_type);
46 static bool TypeParameter_to_Null (Type target_type)
48 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
52 if (gc.HasReferenceTypeConstraint)
54 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
60 static Type TypeParam_EffectiveBaseType (EmitContext ec, GenericConstraints gc)
62 ArrayList list = new ArrayList ();
63 list.Add (gc.EffectiveBaseClass);
64 foreach (Type t in gc.InterfaceConstraints) {
65 if (!t.IsGenericParameter)
68 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
70 list.Add (TypeParam_EffectiveBaseType (ec, new_gc));
72 return FindMostEncompassedType (ec, list);
75 static Expression ImplicitTypeParameterConversion (EmitContext ec, Expression expr,
78 Type expr_type = expr.Type;
80 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
83 if (target_type == TypeManager.object_type)
84 return new BoxedCast (expr, target_type);
89 // We're converting from a type parameter which is known to be a reference type.
90 Type base_type = TypeParam_EffectiveBaseType (ec, gc);
92 if (TypeManager.IsSubclassOf (base_type, target_type))
93 return new BoxedCast (expr, target_type);
95 if (target_type.IsInterface) {
96 if (TypeManager.ImplementsInterface (base_type, target_type))
97 return new BoxedCast (expr, target_type);
99 foreach (Type t in gc.InterfaceConstraints) {
100 if (TypeManager.IsSubclassOf (t, target_type))
101 return new BoxedCast (expr, target_type);
105 foreach (Type t in gc.InterfaceConstraints) {
106 if (!t.IsGenericParameter)
108 if (TypeManager.IsSubclassOf (t, target_type))
109 return new BoxedCast (expr, target_type);
115 static EmptyExpression MyEmptyExpr;
116 static public Expression ImplicitReferenceConversion (EmitContext ec, Expression expr, Type target_type)
118 Type expr_type = expr.Type;
120 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
121 // if we are a method group, emit a warning
126 if (expr_type == TypeManager.void_type)
129 if (expr_type.IsGenericParameter)
130 return ImplicitTypeParameterConversion (ec, expr, target_type);
133 // notice that it is possible to write "ValueType v = 1", the ValueType here
134 // is an abstract class, and not really a value type, so we apply the same rules.
136 if (target_type == TypeManager.object_type) {
138 // A pointer type cannot be converted to object
140 if (expr_type.IsPointer)
143 if (TypeManager.IsValueType (expr_type))
144 return new BoxedCast (expr, target_type);
145 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
146 if (expr_type == TypeManager.anonymous_method_type)
148 return new EmptyCast (expr, target_type);
152 } else if (target_type == TypeManager.value_type) {
153 if (TypeManager.IsValueType (expr_type))
154 return new BoxedCast (expr, target_type);
155 if (expr_type == TypeManager.null_type)
156 return new NullCast (expr, target_type);
159 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
161 // Special case: enumeration to System.Enum.
162 // System.Enum is not a value type, it is a class, so we need
163 // a boxing conversion
165 if (expr_type.IsEnum || expr_type.IsGenericParameter)
166 return new BoxedCast (expr, target_type);
168 return new EmptyCast (expr, target_type);
171 // This code is kind of mirrored inside ImplicitStandardConversionExists
172 // with the small distinction that we only probe there
174 // Always ensure that the code here and there is in sync
176 // from the null type to any reference-type.
177 if (expr_type == TypeManager.null_type){
178 if (target_type.IsPointer)
179 return new EmptyCast (NullPointer.Null, target_type);
181 if (!target_type.IsValueType)
182 return new NullCast (expr, target_type);
185 // from any class-type S to any interface-type T.
186 if (target_type.IsInterface) {
187 if (target_type != TypeManager.iconvertible_type &&
188 expr_type.IsValueType && (expr is Constant) &&
189 !(expr is IntLiteral || expr is BoolLiteral ||
190 expr is FloatLiteral || expr is DoubleLiteral ||
191 expr is LongLiteral || expr is CharLiteral ||
192 expr is StringLiteral || expr is DecimalLiteral ||
193 expr is UIntLiteral || expr is ULongLiteral)) {
197 if (TypeManager.ImplementsInterface (expr_type, target_type)){
198 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
199 return new BoxedCast (expr, target_type);
201 return new EmptyCast (expr, target_type);
205 // from any interface type S to interface-type T.
206 if (expr_type.IsInterface && target_type.IsInterface) {
207 if (TypeManager.ImplementsInterface (expr_type, target_type))
208 return new EmptyCast (expr, target_type);
213 // from an array-type S to an array-type of type T
214 if (expr_type.IsArray && target_type.IsArray) {
215 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
217 Type expr_element_type = TypeManager.GetElementType (expr_type);
219 if (MyEmptyExpr == null)
220 MyEmptyExpr = new EmptyExpression ();
222 MyEmptyExpr.SetType (expr_element_type);
223 Type target_element_type = TypeManager.GetElementType (target_type);
225 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
226 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
227 target_element_type))
228 return new EmptyCast (expr, target_type);
232 // from an array-type to System.Array
233 if (expr_type.IsArray && target_type == TypeManager.array_type)
234 return new EmptyCast (expr, target_type);
236 // from an array-type of type T to IEnumerable<T>
237 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
238 return new EmptyCast (expr, target_type);
240 // from any delegate type to System.Delegate
241 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
242 target_type == TypeManager.delegate_type)
243 return new EmptyCast (expr, target_type);
245 // from any array-type or delegate type into System.ICloneable.
246 if (expr_type.IsArray ||
247 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
248 if (target_type == TypeManager.icloneable_type)
249 return new EmptyCast (expr, target_type);
251 // from a generic type definition to a generic instance.
252 if (TypeManager.IsEqual (expr_type, target_type))
253 return new EmptyCast (expr, target_type);
259 // Tests whether an implicit reference conversion exists between expr_type
262 public static bool ImplicitReferenceConversionExists (EmitContext ec, Expression expr, Type target_type)
264 if (target_type.IsValueType)
267 Type expr_type = expr.Type;
269 if (expr_type.IsGenericParameter)
270 return ImplicitTypeParameterConversion (ec, expr, target_type) != null;
273 // This is the boxed case.
275 if (target_type == TypeManager.object_type) {
276 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
277 expr_type.IsInterface || expr_type == TypeManager.enum_type)
278 if (target_type != TypeManager.anonymous_method_type)
282 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
285 // Please remember that all code below actually comes
286 // from ImplicitReferenceConversion so make sure code remains in sync
288 // from any class-type S to any interface-type T.
289 if (target_type.IsInterface) {
290 if (target_type != TypeManager.iconvertible_type &&
291 expr_type.IsValueType && (expr is Constant) &&
292 !(expr is IntLiteral || expr is BoolLiteral ||
293 expr is FloatLiteral || expr is DoubleLiteral ||
294 expr is LongLiteral || expr is CharLiteral ||
295 expr is StringLiteral || expr is DecimalLiteral ||
296 expr is UIntLiteral || expr is ULongLiteral)) {
300 if (TypeManager.ImplementsInterface (expr_type, target_type))
304 // from any interface type S to interface-type T.
305 if (expr_type.IsInterface && target_type.IsInterface)
306 if (TypeManager.ImplementsInterface (expr_type, target_type))
309 // from an array-type S to an array-type of type T
310 if (expr_type.IsArray && target_type.IsArray) {
311 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
313 Type expr_element_type = expr_type.GetElementType ();
315 if (MyEmptyExpr == null)
316 MyEmptyExpr = new EmptyExpression ();
318 MyEmptyExpr.SetType (expr_element_type);
319 Type target_element_type = TypeManager.GetElementType (target_type);
321 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
322 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
323 target_element_type))
328 // from an array-type to System.Array
329 if (expr_type.IsArray && (target_type == TypeManager.array_type))
332 // from an array-type of type T to IEnumerable<T>
333 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
336 // from any delegate type to System.Delegate
337 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
338 target_type == TypeManager.delegate_type)
339 if (target_type.IsAssignableFrom (expr_type))
342 // from any array-type or delegate type into System.ICloneable.
343 if (expr_type.IsArray ||
344 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
345 if (target_type == TypeManager.icloneable_type)
348 // from the null type to any reference-type.
349 if (expr_type == TypeManager.null_type){
350 if (target_type.IsPointer)
353 if (!target_type.IsValueType)
357 // from a generic type definition to a generic instance.
358 if (TypeManager.IsEqual (expr_type, target_type))
365 /// Implicit Numeric Conversions.
367 /// expr is the expression to convert, returns a new expression of type
368 /// target_type or null if an implicit conversion is not possible.
370 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
373 Type expr_type = expr.Type;
376 // Attempt to do the implicit constant expression conversions
378 if (expr is Constant){
379 if (expr is IntConstant){
382 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
386 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
388 // Try the implicit constant expression conversion
389 // from long to ulong, instead of a nice routine,
392 long v = ((LongConstant) expr).Value;
394 return new ULongConstant ((ulong) v, expr.Location);
398 Type real_target_type = target_type;
400 if (expr_type == TypeManager.sbyte_type){
402 // From sbyte to short, int, long, float, double, decimal
404 if (real_target_type == TypeManager.int32_type)
405 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
406 if (real_target_type == TypeManager.int64_type)
407 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
408 if (real_target_type == TypeManager.double_type)
409 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
410 if (real_target_type == TypeManager.float_type)
411 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
412 if (real_target_type == TypeManager.short_type)
413 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
414 if (real_target_type == TypeManager.decimal_type)
415 return new CastToDecimal (expr);
416 } else if (expr_type == TypeManager.byte_type){
418 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
420 if ((real_target_type == TypeManager.short_type) ||
421 (real_target_type == TypeManager.ushort_type) ||
422 (real_target_type == TypeManager.int32_type) ||
423 (real_target_type == TypeManager.uint32_type))
424 return new EmptyCast (expr, target_type);
426 if (real_target_type == TypeManager.uint64_type)
427 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
428 if (real_target_type == TypeManager.int64_type)
429 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
430 if (real_target_type == TypeManager.float_type)
431 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
432 if (real_target_type == TypeManager.double_type)
433 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
434 if (real_target_type == TypeManager.decimal_type)
435 return new CastToDecimal (expr);
437 } else if (expr_type == TypeManager.short_type){
439 // From short to int, long, float, double, decimal
441 if (real_target_type == TypeManager.int32_type)
442 return new EmptyCast (expr, target_type);
443 if (real_target_type == TypeManager.int64_type)
444 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
445 if (real_target_type == TypeManager.double_type)
446 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
447 if (real_target_type == TypeManager.float_type)
448 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
449 if (real_target_type == TypeManager.decimal_type)
450 return new CastToDecimal (expr);
452 } else if (expr_type == TypeManager.ushort_type){
454 // From ushort to int, uint, long, ulong, float, double, decimal
456 if (real_target_type == TypeManager.uint32_type)
457 return new EmptyCast (expr, target_type);
459 if (real_target_type == TypeManager.uint64_type)
460 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
461 if (real_target_type == TypeManager.int32_type)
462 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
463 if (real_target_type == TypeManager.int64_type)
464 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
465 if (real_target_type == TypeManager.double_type)
466 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
467 if (real_target_type == TypeManager.float_type)
468 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
469 if (real_target_type == TypeManager.decimal_type)
470 return new CastToDecimal (expr);
471 } else if (expr_type == TypeManager.int32_type){
473 // From int to long, float, double, decimal
475 if (real_target_type == TypeManager.int64_type)
476 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
477 if (real_target_type == TypeManager.double_type)
478 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
479 if (real_target_type == TypeManager.float_type)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
481 if (real_target_type == TypeManager.decimal_type)
482 return new CastToDecimal (expr);
483 } else if (expr_type == TypeManager.uint32_type){
485 // From uint to long, ulong, float, double, decimal
487 if (real_target_type == TypeManager.int64_type)
488 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
489 if (real_target_type == TypeManager.uint64_type)
490 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
491 if (real_target_type == TypeManager.double_type)
492 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
494 if (real_target_type == TypeManager.float_type)
495 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
497 if (real_target_type == TypeManager.decimal_type)
498 return new CastToDecimal (expr);
499 } else if (expr_type == TypeManager.int64_type){
501 // From long/ulong to float, double
503 if (real_target_type == TypeManager.double_type)
504 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
505 if (real_target_type == TypeManager.float_type)
506 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
507 if (real_target_type == TypeManager.decimal_type)
508 return new CastToDecimal (expr);
509 } else if (expr_type == TypeManager.uint64_type){
511 // From ulong to float, double
513 if (real_target_type == TypeManager.double_type)
514 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
516 if (real_target_type == TypeManager.float_type)
517 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
519 if (real_target_type == TypeManager.decimal_type)
520 return new CastToDecimal (expr);
521 } else if (expr_type == TypeManager.char_type){
523 // From char to ushort, int, uint, long, ulong, float, double, decimal
525 if ((real_target_type == TypeManager.ushort_type) ||
526 (real_target_type == TypeManager.int32_type) ||
527 (real_target_type == TypeManager.uint32_type))
528 return new EmptyCast (expr, target_type);
529 if (real_target_type == TypeManager.uint64_type)
530 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
531 if (real_target_type == TypeManager.int64_type)
532 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
533 if (real_target_type == TypeManager.float_type)
534 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
535 if (real_target_type == TypeManager.double_type)
536 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
537 if (real_target_type == TypeManager.decimal_type)
538 return new CastToDecimal (expr);
539 } else if (expr_type == TypeManager.float_type){
543 if (real_target_type == TypeManager.double_type)
544 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
551 /// Same as ImplicitStandardConversionExists except that it also looks at
552 /// implicit user defined conversions - needed for overload resolution
554 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
556 if (expr is NullLiteral) {
557 if (target_type.IsGenericParameter)
558 return TypeParameter_to_Null (target_type);
560 if (TypeManager.IsNullableType (target_type))
564 if (ImplicitStandardConversionExists (ec, expr, target_type))
567 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
575 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
577 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
581 /// Determines if a standard implicit conversion exists from
582 /// expr_type to target_type
584 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
586 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
588 Type expr_type = expr.Type;
590 if (expr_type == TypeManager.void_type)
593 //Console.WriteLine ("Expr is {0}", expr);
594 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
595 if (expr_type.Equals (target_type))
599 // First numeric conversions
601 if (expr_type == TypeManager.sbyte_type){
603 // From sbyte to short, int, long, float, double, decimal
605 if ((target_type == TypeManager.int32_type) ||
606 (target_type == TypeManager.int64_type) ||
607 (target_type == TypeManager.double_type) ||
608 (target_type == TypeManager.float_type) ||
609 (target_type == TypeManager.short_type) ||
610 (target_type == TypeManager.decimal_type))
613 } else if (expr_type == TypeManager.byte_type){
615 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
617 if ((target_type == TypeManager.short_type) ||
618 (target_type == TypeManager.ushort_type) ||
619 (target_type == TypeManager.int32_type) ||
620 (target_type == TypeManager.uint32_type) ||
621 (target_type == TypeManager.uint64_type) ||
622 (target_type == TypeManager.int64_type) ||
623 (target_type == TypeManager.float_type) ||
624 (target_type == TypeManager.double_type) ||
625 (target_type == TypeManager.decimal_type))
628 } else if (expr_type == TypeManager.short_type){
630 // From short to int, long, double, float, decimal
632 if ((target_type == TypeManager.int32_type) ||
633 (target_type == TypeManager.int64_type) ||
634 (target_type == TypeManager.double_type) ||
635 (target_type == TypeManager.float_type) ||
636 (target_type == TypeManager.decimal_type))
639 } else if (expr_type == TypeManager.ushort_type){
641 // From ushort to int, uint, long, ulong, double, float, decimal
643 if ((target_type == TypeManager.uint32_type) ||
644 (target_type == TypeManager.uint64_type) ||
645 (target_type == TypeManager.int32_type) ||
646 (target_type == TypeManager.int64_type) ||
647 (target_type == TypeManager.double_type) ||
648 (target_type == TypeManager.float_type) ||
649 (target_type == TypeManager.decimal_type))
652 } else if (expr_type == TypeManager.int32_type){
654 // From int to long, double, float, decimal
656 if ((target_type == TypeManager.int64_type) ||
657 (target_type == TypeManager.double_type) ||
658 (target_type == TypeManager.float_type) ||
659 (target_type == TypeManager.decimal_type))
662 } else if (expr_type == TypeManager.uint32_type){
664 // From uint to long, ulong, double, float, decimal
666 if ((target_type == TypeManager.int64_type) ||
667 (target_type == TypeManager.uint64_type) ||
668 (target_type == TypeManager.double_type) ||
669 (target_type == TypeManager.float_type) ||
670 (target_type == TypeManager.decimal_type))
673 } else if ((expr_type == TypeManager.uint64_type) ||
674 (expr_type == TypeManager.int64_type)) {
676 // From long/ulong to double, float, decimal
678 if ((target_type == TypeManager.double_type) ||
679 (target_type == TypeManager.float_type) ||
680 (target_type == TypeManager.decimal_type))
683 } else if (expr_type == TypeManager.char_type){
685 // From char to ushort, int, uint, ulong, long, float, double, decimal
687 if ((target_type == TypeManager.ushort_type) ||
688 (target_type == TypeManager.int32_type) ||
689 (target_type == TypeManager.uint32_type) ||
690 (target_type == TypeManager.uint64_type) ||
691 (target_type == TypeManager.int64_type) ||
692 (target_type == TypeManager.float_type) ||
693 (target_type == TypeManager.double_type) ||
694 (target_type == TypeManager.decimal_type))
697 } else if (expr_type == TypeManager.float_type){
701 if (target_type == TypeManager.double_type)
705 if (expr.eclass == ExprClass.MethodGroup){
706 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
707 MethodGroupExpr mg = expr as MethodGroupExpr;
710 // This should not happen frequently, so we can create an object
711 // to test compatibility
713 Expression c = ImplicitDelegateCreation.Create (
714 ec, mg, target_type, true, Location.Null);
720 if (ImplicitReferenceConversionExists (ec, expr, target_type))
724 // Implicit Constant Expression Conversions
726 if (expr is IntConstant){
727 int value = ((IntConstant) expr).Value;
729 if (target_type == TypeManager.sbyte_type){
730 if (value >= SByte.MinValue && value <= SByte.MaxValue)
732 } else if (target_type == TypeManager.byte_type){
733 if (value >= 0 && value <= Byte.MaxValue)
735 } else if (target_type == TypeManager.short_type){
736 if (value >= Int16.MinValue && value <= Int16.MaxValue)
738 } else if (target_type == TypeManager.ushort_type){
739 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
741 } else if (target_type == TypeManager.uint32_type){
744 } else if (target_type == TypeManager.uint64_type){
746 // we can optimize this case: a positive int32
747 // always fits on a uint64. But we need an opcode
754 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
758 if (expr is LongConstant && target_type == TypeManager.uint64_type){
760 // Try the implicit constant expression conversion
761 // from long to ulong, instead of a nice routine,
764 long v = ((LongConstant) expr).Value;
769 if ((target_type == TypeManager.enum_type ||
770 target_type.IsSubclassOf (TypeManager.enum_type)) &&
772 IntLiteral i = (IntLiteral) expr;
779 // If `expr_type' implements `target_type' (which is an iface)
780 // see TryImplicitIntConversion
782 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
785 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
788 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
791 if (expr_type == TypeManager.anonymous_method_type){
792 if (!TypeManager.IsDelegateType (target_type))
795 AnonymousMethod am = (AnonymousMethod) expr;
797 Expression conv = am.Compatible (ec, target_type, true);
806 /// Finds "most encompassed type" according to the spec (13.4.2)
807 /// amongst the methods in the MethodGroupExpr
809 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
813 if (types.Count == 0)
816 if (types.Count == 1)
817 return (Type) types [0];
819 EmptyExpression expr = EmptyExpression.Grab ();
821 foreach (Type t in types) {
828 if (ImplicitStandardConversionExists (ec, expr, best))
833 foreach (Type t in types) {
836 if (!ImplicitStandardConversionExists (ec, expr, t)) {
842 EmptyExpression.Release (expr);
848 /// Finds "most encompassing type" according to the spec (13.4.2)
849 /// amongst the types in the given set
851 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
855 if (types.Count == 0)
858 if (types.Count == 1)
859 return (Type) types [0];
861 EmptyExpression expr = EmptyExpression.Grab ();
863 foreach (Type t in types) {
870 if (ImplicitStandardConversionExists (ec, expr, t))
874 foreach (Type t in types) {
878 if (!ImplicitStandardConversionExists (ec, expr, best)) {
884 EmptyExpression.Release (expr);
890 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
891 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
892 /// for explicit and implicit conversion operators.
894 static public Type FindMostSpecificSource (EmitContext ec, IList list,
895 Expression source, bool apply_explicit_conv_rules)
897 ArrayList src_types_set = new ArrayList ();
900 // If any operator converts from S then Sx = S
902 Type source_type = source.Type;
903 foreach (MethodBase mb in list){
904 ParameterData pd = TypeManager.GetParameterData (mb);
905 Type param_type = pd.ParameterType (0);
907 if (param_type == source_type)
910 src_types_set.Add (param_type);
914 // Explicit Conv rules
916 if (apply_explicit_conv_rules) {
917 ArrayList candidate_set = new ArrayList ();
919 foreach (Type param_type in src_types_set){
920 if (ImplicitStandardConversionExists (ec, source, param_type))
921 candidate_set.Add (param_type);
924 if (candidate_set.Count != 0)
925 return FindMostEncompassedType (ec, candidate_set);
931 if (apply_explicit_conv_rules)
932 return FindMostEncompassingType (ec, src_types_set);
934 return FindMostEncompassedType (ec, src_types_set);
938 /// Finds the most specific target Tx according to section 13.4.4
940 static public Type FindMostSpecificTarget (EmitContext ec, IList list,
941 Type target, bool apply_explicit_conv_rules)
943 ArrayList tgt_types_set = new ArrayList ();
946 // If any operator converts to T then Tx = T
948 foreach (MethodInfo mi in list){
949 Type ret_type = mi.ReturnType;
950 if (ret_type == target)
953 tgt_types_set.Add (ret_type);
957 // Explicit conv rules
959 if (apply_explicit_conv_rules) {
960 ArrayList candidate_set = new ArrayList ();
962 EmptyExpression expr = EmptyExpression.Grab ();
964 foreach (Type ret_type in tgt_types_set){
965 expr.SetType (ret_type);
967 if (ImplicitStandardConversionExists (ec, expr, target))
968 candidate_set.Add (ret_type);
971 EmptyExpression.Release (expr);
973 if (candidate_set.Count != 0)
974 return FindMostEncompassingType (ec, candidate_set);
978 // Okay, final case !
980 if (apply_explicit_conv_rules)
981 return FindMostEncompassedType (ec, tgt_types_set);
983 return FindMostEncompassingType (ec, tgt_types_set);
987 /// User-defined Implicit conversions
989 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
990 Type target, Location loc)
992 return UserDefinedConversion (ec, source, target, loc, false);
996 /// User-defined Explicit conversions
998 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
999 Type target, Location loc)
1001 return UserDefinedConversion (ec, source, target, loc, true);
1004 static void AddConversionOperators (EmitContext ec, ArrayList list,
1005 Expression source, Type target_type,
1006 bool look_for_explicit,
1012 Type source_type = source.Type;
1013 EmptyExpression expr = EmptyExpression.Grab ();
1014 foreach (MethodInfo m in mg.Methods) {
1015 ParameterData pd = TypeManager.GetParameterData (m);
1016 Type return_type = m.ReturnType;
1017 Type arg_type = pd.ParameterType (0);
1019 if (source_type != arg_type) {
1020 if (!ImplicitStandardConversionExists (ec, source, arg_type)) {
1021 if (!look_for_explicit)
1023 expr.SetType (arg_type);
1024 if (!ImplicitStandardConversionExists (ec, expr, source_type))
1029 if (target_type != return_type) {
1030 expr.SetType (return_type);
1031 if (!ImplicitStandardConversionExists (ec, expr, target_type)) {
1032 if (!look_for_explicit)
1034 expr.SetType (target_type);
1035 if (!ImplicitStandardConversionExists (ec, expr, return_type))
1043 EmptyExpression.Release (expr);
1047 /// Compute the user-defined conversion operator from source_type to target_type.
1048 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1050 static MethodInfo GetConversionOperator (EmitContext ec, Expression source, Type target_type, bool look_for_explicit)
1052 ArrayList ops = new ArrayList (4);
1054 Type source_type = source.Type;
1056 if (source_type != TypeManager.decimal_type) {
1057 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
1058 Expression.MethodLookup (ec, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1059 if (look_for_explicit) {
1060 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
1061 Expression.MethodLookup (
1062 ec, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1066 if (target_type != TypeManager.decimal_type) {
1067 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
1068 Expression.MethodLookup (ec, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1069 if (look_for_explicit) {
1070 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
1071 Expression.MethodLookup (
1072 ec, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1079 Type most_specific_source = FindMostSpecificSource (ec, ops, source, look_for_explicit);
1080 if (most_specific_source == null)
1083 Type most_specific_target = FindMostSpecificTarget (ec, ops, target_type, look_for_explicit);
1084 if (most_specific_target == null)
1087 MethodInfo method = null;
1089 foreach (MethodInfo m in ops) {
1090 if (m.ReturnType != most_specific_target)
1092 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1094 // Ambiguous: more than one conversion operator satisfies the signature.
1103 static DoubleHash explicit_conv = new DoubleHash (100);
1104 static DoubleHash implicit_conv = new DoubleHash (100);
1107 /// User-defined conversions
1109 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1110 Type target, Location loc,
1111 bool look_for_explicit)
1113 Type source_type = source.Type;
1114 MethodInfo method = null;
1116 if (TypeManager.IsNullableType (source_type) && TypeManager.IsNullableType (target))
1117 return new Nullable.LiftedConversion (
1118 source, target, true, look_for_explicit, loc).Resolve (ec);
1121 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1123 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1124 method = (MethodInfo) o;
1126 method = GetConversionOperator (ec, source, target, look_for_explicit);
1127 if (!(source is Constant))
1128 hash.Insert (source_type, target, method);
1134 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1137 // This will do the conversion to the best match that we
1138 // found. Now we need to perform an implict standard conversion
1139 // if the best match was not the type that we were requested
1142 if (look_for_explicit)
1143 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1145 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1151 e = new UserCast (method, source, loc);
1152 if (e.Type != target){
1153 if (!look_for_explicit)
1154 e = ImplicitConversionStandard (ec, e, target, loc);
1156 e = ExplicitConversionStandard (ec, e, target, loc);
1163 /// Converts implicitly the resolved expression `expr' into the
1164 /// `target_type'. It returns a new expression that can be used
1165 /// in a context that expects a `target_type'.
1167 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1168 Type target_type, Location loc)
1172 if (target_type == null)
1173 throw new Exception ("Target type is null");
1175 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1179 e = ImplicitUserConversion (ec, expr, target_type, loc);
1188 /// Attempts to apply the `Standard Implicit
1189 /// Conversion' rules to the expression `expr' into
1190 /// the `target_type'. It returns a new expression
1191 /// that can be used in a context that expects a
1194 /// This is different from `ImplicitConversion' in that the
1195 /// user defined implicit conversions are excluded.
1197 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1198 Type target_type, Location loc)
1200 Type expr_type = expr.Type;
1203 if (expr is NullLiteral) {
1204 if (target_type.IsGenericParameter)
1205 return TypeParameter_to_Null (expr, target_type, loc);
1207 if (TypeManager.IsNullableType (target_type))
1208 return new Nullable.NullableLiteral (target_type, loc);
1211 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1212 return new Nullable.LiftedConversion (
1213 expr, target_type, false, false, loc).Resolve (ec);
1215 if (expr.eclass == ExprClass.MethodGroup){
1216 if (!TypeManager.IsDelegateType (target_type)){
1221 // Only allow anonymous method conversions on post ISO_1
1223 if (RootContext.Version != LanguageVersion.ISO_1){
1224 MethodGroupExpr mg = expr as MethodGroupExpr;
1226 return ImplicitDelegateCreation.Create (
1227 ec, mg, target_type, false, loc);
1231 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1234 e = ImplicitNumericConversion (ec, expr, target_type);
1238 e = ImplicitReferenceConversion (ec, expr, target_type);
1242 if ((target_type == TypeManager.enum_type ||
1243 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1244 expr is IntLiteral){
1245 IntLiteral i = (IntLiteral) expr;
1248 return new EnumConstant ((Constant) expr, target_type);
1252 if (expr_type.IsPointer){
1253 if (target_type == TypeManager.void_ptr_type)
1254 return new EmptyCast (expr, target_type);
1257 // yep, comparing pointer types cant be done with
1258 // t1 == t2, we have to compare their element types.
1260 if (target_type.IsPointer){
1261 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1268 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1269 return new EmptyCast (NullPointer.Null, target_type);
1272 if (expr_type == TypeManager.anonymous_method_type){
1273 if (!TypeManager.IsDelegateType (target_type)){
1274 Report.Error (1660, loc,
1275 "Cannot convert anonymous method block to type `{0}' because it is not a delegate type",
1276 TypeManager.CSharpName (target_type));
1280 AnonymousMethod am = (AnonymousMethod) expr;
1281 int errors = Report.Errors;
1283 Expression conv = am.Compatible (ec, target_type, false);
1288 // We return something instead of null, to avoid
1289 // the duplicate error, since am.Compatible would have
1290 // reported that already
1292 if (errors != Report.Errors)
1293 return new EmptyCast (expr, target_type);
1300 /// Attempts to perform an implicit constant conversion of the IntConstant
1301 /// into a different data type using casts (See Implicit Constant
1302 /// Expression Conversions)
1304 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1306 int value = ic.Value;
1308 if (target_type == TypeManager.sbyte_type){
1309 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1310 return new SByteConstant ((sbyte) value, ic.Location);
1311 } else if (target_type == TypeManager.byte_type){
1312 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1313 return new ByteConstant ((byte) value, ic.Location);
1314 } else if (target_type == TypeManager.short_type){
1315 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1316 return new ShortConstant ((short) value, ic.Location);
1317 } else if (target_type == TypeManager.ushort_type){
1318 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1319 return new UShortConstant ((ushort) value, ic.Location);
1320 } else if (target_type == TypeManager.uint32_type){
1322 return new UIntConstant ((uint) value, ic.Location);
1323 } else if (target_type == TypeManager.uint64_type){
1325 // we can optimize this case: a positive int32
1326 // always fits on a uint64. But we need an opcode
1330 return new ULongConstant ((ulong) value, ic.Location);
1331 } else if (target_type == TypeManager.double_type)
1332 return new DoubleConstant ((double) value, ic.Location);
1333 else if (target_type == TypeManager.float_type)
1334 return new FloatConstant ((float) value, ic.Location);
1336 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1337 Type underlying = TypeManager.EnumToUnderlying (target_type);
1338 Constant e = (Constant) ic;
1341 // Possibly, we need to create a different 0 literal before passing
1344 if (underlying == TypeManager.int64_type)
1345 e = new LongLiteral (0, ic.Location);
1346 else if (underlying == TypeManager.uint64_type)
1347 e = new ULongLiteral (0, ic.Location);
1349 return new EnumConstant (e, target_type);
1353 // If `target_type' is an interface and the type of `ic' implements the interface
1354 // e.g. target_type is IComparable, IConvertible, IFormattable
1356 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1357 return new BoxedCast (ic, target_type);
1363 /// Attempts to implicitly convert `source' into `target_type', using
1364 /// ImplicitConversion. If there is no implicit conversion, then
1365 /// an error is signaled
1367 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1368 Type target_type, Location loc)
1372 int errors = Report.Errors;
1373 e = ImplicitConversion (ec, source, target_type, loc);
1374 if (Report.Errors > errors)
1379 if (source is DoubleLiteral) {
1380 if (target_type == TypeManager.float_type) {
1381 Error_664 (loc, "float", "f");
1384 if (target_type == TypeManager.decimal_type) {
1385 Error_664 (loc, "decimal", "m");
1390 source.Error_ValueCannotBeConverted (loc, target_type, false);
1394 static void Error_664 (Location loc, string type, string suffix) {
1395 Report.Error (664, loc,
1396 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
1401 /// Performs the explicit numeric conversions
1403 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1405 Type expr_type = expr.Type;
1406 Type real_target_type = target_type;
1408 if (expr_type == TypeManager.sbyte_type){
1410 // From sbyte to byte, ushort, uint, ulong, char
1412 if (real_target_type == TypeManager.byte_type)
1413 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1414 if (real_target_type == TypeManager.ushort_type)
1415 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1416 if (real_target_type == TypeManager.uint32_type)
1417 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1418 if (real_target_type == TypeManager.uint64_type)
1419 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1420 if (real_target_type == TypeManager.char_type)
1421 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1422 } else if (expr_type == TypeManager.byte_type){
1424 // From byte to sbyte and char
1426 if (real_target_type == TypeManager.sbyte_type)
1427 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1428 if (real_target_type == TypeManager.char_type)
1429 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1430 } else if (expr_type == TypeManager.short_type){
1432 // From short to sbyte, byte, ushort, uint, ulong, char
1434 if (real_target_type == TypeManager.sbyte_type)
1435 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1436 if (real_target_type == TypeManager.byte_type)
1437 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1438 if (real_target_type == TypeManager.ushort_type)
1439 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1440 if (real_target_type == TypeManager.uint32_type)
1441 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1442 if (real_target_type == TypeManager.uint64_type)
1443 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1444 if (real_target_type == TypeManager.char_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1446 } else if (expr_type == TypeManager.ushort_type){
1448 // From ushort to sbyte, byte, short, char
1450 if (real_target_type == TypeManager.sbyte_type)
1451 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1452 if (real_target_type == TypeManager.byte_type)
1453 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1454 if (real_target_type == TypeManager.short_type)
1455 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1456 if (real_target_type == TypeManager.char_type)
1457 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1458 } else if (expr_type == TypeManager.int32_type){
1460 // From int to sbyte, byte, short, ushort, uint, ulong, char
1462 if (real_target_type == TypeManager.sbyte_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1464 if (real_target_type == TypeManager.byte_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1466 if (real_target_type == TypeManager.short_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1468 if (real_target_type == TypeManager.ushort_type)
1469 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1470 if (real_target_type == TypeManager.uint32_type)
1471 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1472 if (real_target_type == TypeManager.uint64_type)
1473 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1474 if (real_target_type == TypeManager.char_type)
1475 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1476 } else if (expr_type == TypeManager.uint32_type){
1478 // From uint to sbyte, byte, short, ushort, int, char
1480 if (real_target_type == TypeManager.sbyte_type)
1481 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1482 if (real_target_type == TypeManager.byte_type)
1483 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1484 if (real_target_type == TypeManager.short_type)
1485 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1486 if (real_target_type == TypeManager.ushort_type)
1487 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1488 if (real_target_type == TypeManager.int32_type)
1489 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1490 if (real_target_type == TypeManager.char_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1492 } else if (expr_type == TypeManager.int64_type){
1494 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1496 if (real_target_type == TypeManager.sbyte_type)
1497 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1498 if (real_target_type == TypeManager.byte_type)
1499 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1500 if (real_target_type == TypeManager.short_type)
1501 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1502 if (real_target_type == TypeManager.ushort_type)
1503 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1504 if (real_target_type == TypeManager.int32_type)
1505 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1506 if (real_target_type == TypeManager.uint32_type)
1507 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1508 if (real_target_type == TypeManager.uint64_type)
1509 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1510 if (real_target_type == TypeManager.char_type)
1511 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1512 } else if (expr_type == TypeManager.uint64_type){
1514 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1516 if (real_target_type == TypeManager.sbyte_type)
1517 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1518 if (real_target_type == TypeManager.byte_type)
1519 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1520 if (real_target_type == TypeManager.short_type)
1521 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1522 if (real_target_type == TypeManager.ushort_type)
1523 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1524 if (real_target_type == TypeManager.int32_type)
1525 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1526 if (real_target_type == TypeManager.uint32_type)
1527 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1528 if (real_target_type == TypeManager.int64_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1530 if (real_target_type == TypeManager.char_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1532 } else if (expr_type == TypeManager.char_type){
1534 // From char to sbyte, byte, short
1536 if (real_target_type == TypeManager.sbyte_type)
1537 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1538 if (real_target_type == TypeManager.byte_type)
1539 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1540 if (real_target_type == TypeManager.short_type)
1541 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1542 } else if (expr_type == TypeManager.float_type){
1544 // From float to sbyte, byte, short,
1545 // ushort, int, uint, long, ulong, char
1548 if (real_target_type == TypeManager.sbyte_type)
1549 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1550 if (real_target_type == TypeManager.byte_type)
1551 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1552 if (real_target_type == TypeManager.short_type)
1553 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1554 if (real_target_type == TypeManager.ushort_type)
1555 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1556 if (real_target_type == TypeManager.int32_type)
1557 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1558 if (real_target_type == TypeManager.uint32_type)
1559 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1560 if (real_target_type == TypeManager.int64_type)
1561 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1562 if (real_target_type == TypeManager.uint64_type)
1563 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1564 if (real_target_type == TypeManager.char_type)
1565 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1566 if (real_target_type == TypeManager.decimal_type)
1567 return new CastToDecimal (expr, true);
1568 } else if (expr_type == TypeManager.double_type){
1570 // From double to sbyte, byte, short,
1571 // ushort, int, uint, long, ulong,
1572 // char, float or decimal
1574 if (real_target_type == TypeManager.sbyte_type)
1575 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1576 if (real_target_type == TypeManager.byte_type)
1577 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1578 if (real_target_type == TypeManager.short_type)
1579 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1580 if (real_target_type == TypeManager.ushort_type)
1581 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1582 if (real_target_type == TypeManager.int32_type)
1583 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1584 if (real_target_type == TypeManager.uint32_type)
1585 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1586 if (real_target_type == TypeManager.int64_type)
1587 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1588 if (real_target_type == TypeManager.uint64_type)
1589 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1590 if (real_target_type == TypeManager.char_type)
1591 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1592 if (real_target_type == TypeManager.float_type)
1593 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1594 if (real_target_type == TypeManager.decimal_type)
1595 return new CastToDecimal (expr, true);
1596 } else if (expr_type == TypeManager.decimal_type) {
1597 return new CastFromDecimal (expr, target_type).Resolve ();
1603 /// Returns whether an explicit reference conversion can be performed
1604 /// from source_type to target_type
1606 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1608 bool target_is_type_param = target_type.IsGenericParameter;
1609 bool target_is_value_type = target_type.IsValueType;
1611 if (source_type == target_type)
1615 // From generic parameter to any type
1617 if (source_type.IsGenericParameter)
1621 // From object to a generic parameter
1623 if (source_type == TypeManager.object_type && target_is_type_param)
1627 // From object to any reference type
1629 if (source_type == TypeManager.object_type && !target_is_value_type)
1633 // From any class S to any class-type T, provided S is a base class of T
1635 if (TypeManager.IsSubclassOf (target_type, source_type))
1639 // From any interface type S to any interface T provided S is not derived from T
1641 if (source_type.IsInterface && target_type.IsInterface){
1642 if (!TypeManager.IsSubclassOf (target_type, source_type))
1647 // From any class type S to any interface T, provided S is not sealed
1648 // and provided S does not implement T.
1650 if (target_type.IsInterface && !source_type.IsSealed &&
1651 !TypeManager.ImplementsInterface (source_type, target_type))
1655 // From any interface-type S to to any class type T, provided T is not
1656 // sealed, or provided T implements S.
1658 if (source_type.IsInterface &&
1659 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1663 // From an array type S with an element type Se to an array type T with an
1664 // element type Te provided all the following are true:
1665 // * S and T differe only in element type, in other words, S and T
1666 // have the same number of dimensions.
1667 // * Both Se and Te are reference types
1668 // * An explicit referenc conversions exist from Se to Te
1670 if (source_type.IsArray && target_type.IsArray) {
1671 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1673 Type source_element_type = TypeManager.GetElementType (source_type);
1674 Type target_element_type = TypeManager.GetElementType (target_type);
1676 if (source_element_type.IsGenericParameter ||
1677 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1678 if (ExplicitReferenceConversionExists (source_element_type,
1679 target_element_type))
1685 // From System.Array to any array-type
1686 if (source_type == TypeManager.array_type &&
1687 target_type.IsArray){
1692 // From System delegate to any delegate-type
1694 if (source_type == TypeManager.delegate_type &&
1695 TypeManager.IsDelegateType (target_type))
1699 // From ICloneable to Array or Delegate types
1701 if (source_type == TypeManager.icloneable_type &&
1702 (target_type == TypeManager.array_type ||
1703 target_type == TypeManager.delegate_type))
1710 /// Implements Explicit Reference conversions
1712 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1714 Type source_type = source.Type;
1715 bool target_is_type_param = target_type.IsGenericParameter;
1716 bool target_is_value_type = target_type.IsValueType;
1719 // From object to a generic parameter
1721 if (source_type == TypeManager.object_type && target_is_type_param)
1722 return new UnboxCast (source, target_type);
1725 // From object to any reference type
1727 if (source_type == TypeManager.object_type && !target_is_value_type)
1728 return new ClassCast (source, target_type);
1731 // Unboxing conversion.
1733 if (((source_type == TypeManager.enum_type &&
1734 !(source is EmptyCast)) ||
1735 source_type == TypeManager.value_type) && target_is_value_type)
1736 return new UnboxCast (source, target_type);
1739 // From any class S to any class-type T, provided S is a base class of T
1741 if (TypeManager.IsSubclassOf (target_type, source_type))
1742 return new ClassCast (source, target_type);
1745 // From any interface type S to any interface T provided S is not derived from T
1747 if (source_type.IsInterface && target_type.IsInterface){
1748 if (TypeManager.ImplementsInterface (source_type, target_type))
1751 return new ClassCast (source, target_type);
1755 // From any class type S to any interface T, provides S is not sealed
1756 // and provided S does not implement T.
1758 if (target_type.IsInterface && !source_type.IsSealed) {
1759 if (TypeManager.ImplementsInterface (source_type, target_type))
1762 return new ClassCast (source, target_type);
1767 // From any interface-type S to to any class type T, provided T is not
1768 // sealed, or provided T implements S.
1770 if (source_type.IsInterface) {
1771 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1772 if (target_type.IsClass)
1773 return new ClassCast (source, target_type);
1775 return new UnboxCast (source, target_type);
1781 // From an array type S with an element type Se to an array type T with an
1782 // element type Te provided all the following are true:
1783 // * S and T differe only in element type, in other words, S and T
1784 // have the same number of dimensions.
1785 // * Both Se and Te are reference types
1786 // * An explicit referenc conversions exist from Se to Te
1788 if (source_type.IsArray && target_type.IsArray) {
1789 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1791 Type source_element_type = TypeManager.GetElementType (source_type);
1792 Type target_element_type = TypeManager.GetElementType (target_type);
1794 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1795 if (ExplicitReferenceConversionExists (source_element_type,
1796 target_element_type))
1797 return new ClassCast (source, target_type);
1802 // From System.Array to any array-type
1803 if (source_type == TypeManager.array_type &&
1804 target_type.IsArray) {
1805 return new ClassCast (source, target_type);
1809 // From System delegate to any delegate-type
1811 if (source_type == TypeManager.delegate_type &&
1812 TypeManager.IsDelegateType (target_type))
1813 return new ClassCast (source, target_type);
1816 // From ICloneable to Array or Delegate types
1818 if (source_type == TypeManager.icloneable_type &&
1819 (target_type == TypeManager.array_type ||
1820 target_type == TypeManager.delegate_type))
1821 return new ClassCast (source, target_type);
1827 /// Performs an explicit conversion of the expression `expr' whose
1828 /// type is expr.Type to `target_type'.
1830 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1831 Type target_type, Location loc)
1833 Type expr_type = expr.Type;
1835 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1836 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1841 // Unboxing conversions; only object types can be convertible to enum
1843 if (expr_type == TypeManager.object_type && target_type.IsValueType || expr_type == TypeManager.enum_type)
1844 return new UnboxCast (expr, target_type);
1846 if (TypeManager.IsEnumType (expr_type)) {
1847 if (expr is EnumConstant)
1848 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1850 return ExplicitConversionCore (ec, new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1853 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1854 return new Nullable.LiftedConversion (
1855 expr, target_type, false, true, loc).Resolve (ec);
1857 if (TypeManager.IsEnumType (target_type))
1858 return new EmptyCast (ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc), target_type);
1860 ne = ExplicitNumericConversion (expr, target_type);
1865 // Skip the ExplicitReferenceConversion because we can not convert
1866 // from Null to a ValueType, and ExplicitReference wont check against
1867 // null literal explicitly
1869 if (expr_type != TypeManager.null_type){
1870 ne = ExplicitReferenceConversion (expr, target_type);
1876 ne = ExplicitUnsafe (expr, target_type);
1881 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1888 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1890 Type expr_type = expr.Type;
1892 if (target_type.IsPointer){
1893 if (expr_type.IsPointer)
1894 return new EmptyCast (expr, target_type);
1896 if (expr_type == TypeManager.sbyte_type ||
1897 expr_type == TypeManager.short_type ||
1898 expr_type == TypeManager.int32_type ||
1899 expr_type == TypeManager.int64_type)
1900 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1902 if (expr_type == TypeManager.ushort_type ||
1903 expr_type == TypeManager.uint32_type ||
1904 expr_type == TypeManager.uint64_type ||
1905 expr_type == TypeManager.byte_type)
1906 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1909 if (expr_type.IsPointer){
1910 if (target_type == TypeManager.sbyte_type)
1911 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1912 else if (target_type == TypeManager.byte_type)
1913 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1914 else if (target_type == TypeManager.short_type)
1915 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1916 else if (target_type == TypeManager.ushort_type)
1917 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1918 else if (target_type == TypeManager.int32_type)
1919 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1920 else if (target_type == TypeManager.uint32_type)
1921 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1922 else if (target_type == TypeManager.uint64_type)
1923 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1924 else if (target_type == TypeManager.int64_type){
1925 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1932 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1934 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1935 Type target_type, Location l)
1937 int errors = Report.Errors;
1938 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1939 if (Report.Errors > errors)
1945 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
1946 return new Nullable.LiftedConversion (
1947 expr, target_type, false, true, l).Resolve (ec);
1949 ne = ExplicitNumericConversion (expr, target_type);
1953 ne = ExplicitReferenceConversion (expr, target_type);
1957 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1958 return new EmptyCast (expr, target_type);
1960 expr.Error_ValueCannotBeConverted (l, target_type, true);
1965 /// Performs an explicit conversion of the expression `expr' whose
1966 /// type is expr.Type to `target_type'.
1968 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1969 Type target_type, Location loc)
1971 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1975 expr.Error_ValueCannotBeConverted (loc, target_type, true);