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 (Constant 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.",
39 target_type.ToString ());
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 ClassCast (expr, target_type);
95 if (target_type.IsInterface) {
96 if (TypeManager.ImplementsInterface (base_type, target_type))
97 return new ClassCast (expr, target_type);
99 foreach (Type t in gc.InterfaceConstraints) {
100 if (TypeManager.IsSubclassOf (t, target_type))
101 return new ClassCast (expr, target_type);
102 if (TypeManager.ImplementsInterface (t, target_type))
103 return new ClassCast (expr, target_type);
107 foreach (Type t in gc.InterfaceConstraints) {
108 if (!t.IsGenericParameter)
110 if (TypeManager.IsSubclassOf (t, target_type))
111 return new ClassCast (expr, target_type);
112 if (TypeManager.ImplementsInterface (t, target_type))
113 return new ClassCast (expr, target_type);
119 static EmptyExpression MyEmptyExpr;
120 static public Expression ImplicitReferenceConversion (EmitContext ec, Expression expr, Type target_type)
122 Type expr_type = expr.Type;
124 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
125 // if we are a method group, emit a warning
130 if (expr_type == TypeManager.void_type)
133 if (expr_type.IsGenericParameter)
134 return ImplicitTypeParameterConversion (ec, expr, target_type);
137 // notice that it is possible to write "ValueType v = 1", the ValueType here
138 // is an abstract class, and not really a value type, so we apply the same rules.
140 if (target_type == TypeManager.object_type) {
142 // A pointer type cannot be converted to object
144 if (expr_type.IsPointer)
147 if (TypeManager.IsValueType (expr_type))
148 return new BoxedCast (expr, target_type);
149 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
150 if (expr_type == TypeManager.anonymous_method_type)
152 return new EmptyCast (expr, target_type);
156 } else if (target_type == TypeManager.value_type) {
157 if (TypeManager.IsValueType (expr_type))
158 return new BoxedCast (expr, target_type);
159 if (expr_type == TypeManager.null_type)
160 return new NullCast ((Constant)expr, target_type);
163 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
165 // Special case: enumeration to System.Enum.
166 // System.Enum is not a value type, it is a class, so we need
167 // a boxing conversion
169 if (expr_type.IsEnum || expr_type.IsGenericParameter)
170 return new BoxedCast (expr, target_type);
172 return new EmptyCast (expr, target_type);
175 // This code is kind of mirrored inside ImplicitStandardConversionExists
176 // with the small distinction that we only probe there
178 // Always ensure that the code here and there is in sync
180 // from the null type to any reference-type.
181 if (expr_type == TypeManager.null_type){
182 if (target_type.IsPointer)
183 return new EmptyCast (NullPointer.Null, target_type);
185 if (!target_type.IsValueType) {
186 if (expr is Constant)
187 return new NullCast ((Constant)expr, target_type);
189 // I found only one case when it happens -- Foo () ? null : null;
190 Report.Warning (-100, 1, expr.Location, "The result of the expression is always `null'");
191 return new NullCast (new NullLiteral (expr.Location), target_type);
195 // from any class-type S to any interface-type T.
196 if (target_type.IsInterface) {
197 if (target_type != TypeManager.iconvertible_type &&
198 expr_type.IsValueType && (expr is Constant) &&
199 !(expr is IntLiteral || expr is BoolLiteral ||
200 expr is FloatLiteral || expr is DoubleLiteral ||
201 expr is LongLiteral || expr is CharLiteral ||
202 expr is StringLiteral || expr is DecimalLiteral ||
203 expr is UIntLiteral || expr is ULongLiteral)) {
207 if (TypeManager.ImplementsInterface (expr_type, target_type)){
208 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
209 return new BoxedCast (expr, target_type);
211 return new EmptyCast (expr, target_type);
215 // from any interface type S to interface-type T.
216 if (expr_type.IsInterface && target_type.IsInterface) {
217 if (TypeManager.ImplementsInterface (expr_type, target_type))
218 return new EmptyCast (expr, target_type);
223 // from an array-type S to an array-type of type T
224 if (expr_type.IsArray && target_type.IsArray) {
225 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
227 Type expr_element_type = TypeManager.GetElementType (expr_type);
229 if (MyEmptyExpr == null)
230 MyEmptyExpr = new EmptyExpression ();
232 MyEmptyExpr.SetType (expr_element_type);
233 Type target_element_type = TypeManager.GetElementType (target_type);
235 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
236 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
237 target_element_type))
238 return new EmptyCast (expr, target_type);
242 // from an array-type to System.Array
243 if (expr_type.IsArray && target_type == TypeManager.array_type)
244 return new EmptyCast (expr, target_type);
246 // from an array-type of type T to IEnumerable<T>
247 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
248 return new EmptyCast (expr, target_type);
250 // from any delegate type to System.Delegate
251 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
252 target_type == TypeManager.delegate_type)
253 return new EmptyCast (expr, target_type);
255 // from any array-type or delegate type into System.ICloneable.
256 if (expr_type.IsArray ||
257 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
258 if (target_type == TypeManager.icloneable_type)
259 return new EmptyCast (expr, target_type);
261 // from a generic type definition to a generic instance.
262 if (TypeManager.IsEqual (expr_type, target_type))
263 return new EmptyCast (expr, target_type);
269 // Tests whether an implicit reference conversion exists between expr_type
272 public static bool ImplicitReferenceConversionExists (EmitContext ec, Expression expr, Type target_type)
274 if (target_type.IsValueType)
277 Type expr_type = expr.Type;
279 if (expr_type.IsGenericParameter)
280 return ImplicitTypeParameterConversion (ec, expr, target_type) != null;
283 // This is the boxed case.
285 if (target_type == TypeManager.object_type) {
286 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
287 expr_type.IsInterface || expr_type == TypeManager.enum_type)
288 if (target_type != TypeManager.anonymous_method_type)
292 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
295 // Please remember that all code below actually comes
296 // from ImplicitReferenceConversion so make sure code remains in sync
298 // from any class-type S to any interface-type T.
299 if (target_type.IsInterface) {
300 if (target_type != TypeManager.iconvertible_type &&
301 expr_type.IsValueType && (expr is Constant) &&
302 !(expr is IntLiteral || expr is BoolLiteral ||
303 expr is FloatLiteral || expr is DoubleLiteral ||
304 expr is LongLiteral || expr is CharLiteral ||
305 expr is StringLiteral || expr is DecimalLiteral ||
306 expr is UIntLiteral || expr is ULongLiteral)) {
310 if (TypeManager.ImplementsInterface (expr_type, target_type))
314 // from any interface type S to interface-type T.
315 if (expr_type.IsInterface && target_type.IsInterface)
316 if (TypeManager.ImplementsInterface (expr_type, target_type))
319 // from an array-type S to an array-type of type T
320 if (expr_type.IsArray && target_type.IsArray) {
321 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
323 Type expr_element_type = expr_type.GetElementType ();
325 if (MyEmptyExpr == null)
326 MyEmptyExpr = new EmptyExpression ();
328 MyEmptyExpr.SetType (expr_element_type);
329 Type target_element_type = TypeManager.GetElementType (target_type);
331 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
332 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
333 target_element_type))
338 // from an array-type to System.Array
339 if (expr_type.IsArray && (target_type == TypeManager.array_type))
342 // from an array-type of type T to IEnumerable<T>
343 if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
346 // from any delegate type to System.Delegate
347 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
348 target_type == TypeManager.delegate_type)
349 if (target_type.IsAssignableFrom (expr_type))
352 // from any array-type or delegate type into System.ICloneable.
353 if (expr_type.IsArray ||
354 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
355 if (target_type == TypeManager.icloneable_type)
358 // from the null type to any reference-type.
359 if (expr_type == TypeManager.null_type){
360 if (target_type.IsPointer)
363 if (!target_type.IsValueType)
367 // from a generic type definition to a generic instance.
368 if (TypeManager.IsEqual (expr_type, target_type))
375 /// Implicit Numeric Conversions.
377 /// expr is the expression to convert, returns a new expression of type
378 /// target_type or null if an implicit conversion is not possible.
380 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
383 Type expr_type = expr.Type;
386 // Attempt to do the implicit constant expression conversions
388 if (expr is Constant){
389 if (expr is IntConstant){
392 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
396 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
398 // Try the implicit constant expression conversion
399 // from long to ulong, instead of a nice routine,
402 long v = ((LongConstant) expr).Value;
404 return new ULongConstant ((ulong) v, expr.Location);
408 Type real_target_type = target_type;
410 if (expr_type == TypeManager.sbyte_type){
412 // From sbyte to short, int, long, float, double, decimal
414 if (real_target_type == TypeManager.int32_type)
415 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
416 if (real_target_type == TypeManager.int64_type)
417 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
418 if (real_target_type == TypeManager.double_type)
419 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
420 if (real_target_type == TypeManager.float_type)
421 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
422 if (real_target_type == TypeManager.short_type)
423 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
424 if (real_target_type == TypeManager.decimal_type)
425 return new CastToDecimal (expr);
426 } else if (expr_type == TypeManager.byte_type){
428 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
430 if ((real_target_type == TypeManager.short_type) ||
431 (real_target_type == TypeManager.ushort_type) ||
432 (real_target_type == TypeManager.int32_type) ||
433 (real_target_type == TypeManager.uint32_type))
434 return new EmptyCast (expr, target_type);
436 if (real_target_type == TypeManager.uint64_type)
437 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
438 if (real_target_type == TypeManager.int64_type)
439 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
440 if (real_target_type == TypeManager.float_type)
441 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
442 if (real_target_type == TypeManager.double_type)
443 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
444 if (real_target_type == TypeManager.decimal_type)
445 return new CastToDecimal (expr);
447 } else if (expr_type == TypeManager.short_type){
449 // From short to int, long, float, double, decimal
451 if (real_target_type == TypeManager.int32_type)
452 return new EmptyCast (expr, target_type);
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.decimal_type)
460 return new CastToDecimal (expr);
462 } else if (expr_type == TypeManager.ushort_type){
464 // From ushort to int, uint, long, ulong, float, double, decimal
466 if (real_target_type == TypeManager.uint32_type)
467 return new EmptyCast (expr, target_type);
469 if (real_target_type == TypeManager.uint64_type)
470 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
471 if (real_target_type == TypeManager.int32_type)
472 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
473 if (real_target_type == TypeManager.int64_type)
474 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
475 if (real_target_type == TypeManager.double_type)
476 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
477 if (real_target_type == TypeManager.float_type)
478 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
479 if (real_target_type == TypeManager.decimal_type)
480 return new CastToDecimal (expr);
481 } else if (expr_type == TypeManager.int32_type){
483 // From int to long, float, double, decimal
485 if (real_target_type == TypeManager.int64_type)
486 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
487 if (real_target_type == TypeManager.double_type)
488 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
489 if (real_target_type == TypeManager.float_type)
490 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
491 if (real_target_type == TypeManager.decimal_type)
492 return new CastToDecimal (expr);
493 } else if (expr_type == TypeManager.uint32_type){
495 // From uint to long, ulong, float, double, decimal
497 if (real_target_type == TypeManager.int64_type)
498 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
499 if (real_target_type == TypeManager.uint64_type)
500 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
501 if (real_target_type == TypeManager.double_type)
502 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
504 if (real_target_type == TypeManager.float_type)
505 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
507 if (real_target_type == TypeManager.decimal_type)
508 return new CastToDecimal (expr);
509 } else if (expr_type == TypeManager.int64_type){
511 // From long/ulong to float, double
513 if (real_target_type == TypeManager.double_type)
514 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
515 if (real_target_type == TypeManager.float_type)
516 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
517 if (real_target_type == TypeManager.decimal_type)
518 return new CastToDecimal (expr);
519 } else if (expr_type == TypeManager.uint64_type){
521 // From ulong to float, double
523 if (real_target_type == TypeManager.double_type)
524 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
526 if (real_target_type == TypeManager.float_type)
527 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
529 if (real_target_type == TypeManager.decimal_type)
530 return new CastToDecimal (expr);
531 } else if (expr_type == TypeManager.char_type){
533 // From char to ushort, int, uint, long, ulong, float, double, decimal
535 if ((real_target_type == TypeManager.ushort_type) ||
536 (real_target_type == TypeManager.int32_type) ||
537 (real_target_type == TypeManager.uint32_type))
538 return new EmptyCast (expr, target_type);
539 if (real_target_type == TypeManager.uint64_type)
540 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
541 if (real_target_type == TypeManager.int64_type)
542 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
543 if (real_target_type == TypeManager.float_type)
544 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
545 if (real_target_type == TypeManager.double_type)
546 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
547 if (real_target_type == TypeManager.decimal_type)
548 return new CastToDecimal (expr);
549 } else if (expr_type == TypeManager.float_type){
553 if (real_target_type == TypeManager.double_type)
554 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
561 /// Same as ImplicitStandardConversionExists except that it also looks at
562 /// implicit user defined conversions - needed for overload resolution
564 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
566 if (expr is NullLiteral) {
567 if (target_type.IsGenericParameter)
568 return TypeParameter_to_Null (target_type);
570 if (TypeManager.IsNullableType (target_type))
574 if (ImplicitStandardConversionExists (ec, expr, target_type))
577 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
585 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
587 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
591 /// Determines if a standard implicit conversion exists from
592 /// expr_type to target_type
594 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
596 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
598 Type expr_type = expr.Type;
600 if (expr_type == TypeManager.void_type)
603 //Console.WriteLine ("Expr is {0}", expr);
604 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
605 if (expr_type.Equals (target_type))
609 // First numeric conversions
611 if (expr_type == TypeManager.sbyte_type){
613 // From sbyte to short, int, long, float, double, decimal
615 if ((target_type == TypeManager.int32_type) ||
616 (target_type == TypeManager.int64_type) ||
617 (target_type == TypeManager.double_type) ||
618 (target_type == TypeManager.float_type) ||
619 (target_type == TypeManager.short_type) ||
620 (target_type == TypeManager.decimal_type))
623 } else if (expr_type == TypeManager.byte_type){
625 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
627 if ((target_type == TypeManager.short_type) ||
628 (target_type == TypeManager.ushort_type) ||
629 (target_type == TypeManager.int32_type) ||
630 (target_type == TypeManager.uint32_type) ||
631 (target_type == TypeManager.uint64_type) ||
632 (target_type == TypeManager.int64_type) ||
633 (target_type == TypeManager.float_type) ||
634 (target_type == TypeManager.double_type) ||
635 (target_type == TypeManager.decimal_type))
638 } else if (expr_type == TypeManager.short_type){
640 // From short to int, long, double, float, decimal
642 if ((target_type == TypeManager.int32_type) ||
643 (target_type == TypeManager.int64_type) ||
644 (target_type == TypeManager.double_type) ||
645 (target_type == TypeManager.float_type) ||
646 (target_type == TypeManager.decimal_type))
649 } else if (expr_type == TypeManager.ushort_type){
651 // From ushort to int, uint, long, ulong, double, float, decimal
653 if ((target_type == TypeManager.uint32_type) ||
654 (target_type == TypeManager.uint64_type) ||
655 (target_type == TypeManager.int32_type) ||
656 (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.int32_type){
664 // From int to long, double, float, decimal
666 if ((target_type == TypeManager.int64_type) ||
667 (target_type == TypeManager.double_type) ||
668 (target_type == TypeManager.float_type) ||
669 (target_type == TypeManager.decimal_type))
672 } else if (expr_type == TypeManager.uint32_type){
674 // From uint to long, ulong, double, float, decimal
676 if ((target_type == TypeManager.int64_type) ||
677 (target_type == TypeManager.uint64_type) ||
678 (target_type == TypeManager.double_type) ||
679 (target_type == TypeManager.float_type) ||
680 (target_type == TypeManager.decimal_type))
683 } else if ((expr_type == TypeManager.uint64_type) ||
684 (expr_type == TypeManager.int64_type)) {
686 // From long/ulong to double, float, decimal
688 if ((target_type == TypeManager.double_type) ||
689 (target_type == TypeManager.float_type) ||
690 (target_type == TypeManager.decimal_type))
693 } else if (expr_type == TypeManager.char_type){
695 // From char to ushort, int, uint, ulong, long, float, double, decimal
697 if ((target_type == TypeManager.ushort_type) ||
698 (target_type == TypeManager.int32_type) ||
699 (target_type == TypeManager.uint32_type) ||
700 (target_type == TypeManager.uint64_type) ||
701 (target_type == TypeManager.int64_type) ||
702 (target_type == TypeManager.float_type) ||
703 (target_type == TypeManager.double_type) ||
704 (target_type == TypeManager.decimal_type))
707 } else if (expr_type == TypeManager.float_type){
711 if (target_type == TypeManager.double_type)
715 if (expr.eclass == ExprClass.MethodGroup){
716 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
717 MethodGroupExpr mg = expr as MethodGroupExpr;
720 // This should not happen frequently, so we can create an object
721 // to test compatibility
723 Expression c = ImplicitDelegateCreation.Create (
724 ec, mg, target_type, true, Location.Null);
730 if (ImplicitReferenceConversionExists (ec, expr, target_type))
734 // Implicit Constant Expression Conversions
736 if (expr is IntConstant){
737 int value = ((IntConstant) expr).Value;
739 if (target_type == TypeManager.sbyte_type){
740 if (value >= SByte.MinValue && value <= SByte.MaxValue)
742 } else if (target_type == TypeManager.byte_type){
743 if (value >= 0 && value <= Byte.MaxValue)
745 } else if (target_type == TypeManager.short_type){
746 if (value >= Int16.MinValue && value <= Int16.MaxValue)
748 } else if (target_type == TypeManager.ushort_type){
749 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
751 } else if (target_type == TypeManager.uint32_type){
754 } else if (target_type == TypeManager.uint64_type){
756 // we can optimize this case: a positive int32
757 // always fits on a uint64. But we need an opcode
764 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
768 if (expr is LongConstant && target_type == TypeManager.uint64_type){
770 // Try the implicit constant expression conversion
771 // from long to ulong, instead of a nice routine,
774 long v = ((LongConstant) expr).Value;
779 if ((target_type == TypeManager.enum_type ||
780 target_type.IsSubclassOf (TypeManager.enum_type)) &&
782 IntLiteral i = (IntLiteral) expr;
789 // If `expr_type' implements `target_type' (which is an iface)
790 // see TryImplicitIntConversion
792 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
795 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
798 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
801 if (expr_type == TypeManager.anonymous_method_type){
802 if (!TypeManager.IsDelegateType (target_type))
805 AnonymousMethod am = (AnonymousMethod) expr;
807 Expression conv = am.Compatible (ec, target_type, true);
816 /// Finds "most encompassed type" according to the spec (13.4.2)
817 /// amongst the methods in the MethodGroupExpr
819 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
823 if (types.Count == 0)
826 if (types.Count == 1)
827 return (Type) types [0];
829 EmptyExpression expr = EmptyExpression.Grab ();
831 foreach (Type t in types) {
838 if (ImplicitStandardConversionExists (ec, expr, best))
843 foreach (Type t in types) {
846 if (!ImplicitStandardConversionExists (ec, expr, t)) {
852 EmptyExpression.Release (expr);
858 /// Finds "most encompassing type" according to the spec (13.4.2)
859 /// amongst the types in the given set
861 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
865 if (types.Count == 0)
868 if (types.Count == 1)
869 return (Type) types [0];
871 EmptyExpression expr = EmptyExpression.Grab ();
873 foreach (Type t in types) {
880 if (ImplicitStandardConversionExists (ec, expr, t))
884 foreach (Type t in types) {
888 if (!ImplicitStandardConversionExists (ec, expr, best)) {
894 EmptyExpression.Release (expr);
900 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
901 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
902 /// for explicit and implicit conversion operators.
904 static public Type FindMostSpecificSource (EmitContext ec, IList list,
905 Expression source, bool apply_explicit_conv_rules)
907 ArrayList src_types_set = new ArrayList ();
910 // If any operator converts from S then Sx = S
912 Type source_type = source.Type;
913 foreach (MethodBase mb in list){
914 ParameterData pd = TypeManager.GetParameterData (mb);
915 Type param_type = pd.ParameterType (0);
917 if (param_type == source_type)
920 src_types_set.Add (param_type);
924 // Explicit Conv rules
926 if (apply_explicit_conv_rules) {
927 ArrayList candidate_set = new ArrayList ();
929 foreach (Type param_type in src_types_set){
930 if (ImplicitStandardConversionExists (ec, source, param_type))
931 candidate_set.Add (param_type);
934 if (candidate_set.Count != 0)
935 return FindMostEncompassedType (ec, candidate_set);
941 if (apply_explicit_conv_rules)
942 return FindMostEncompassingType (ec, src_types_set);
944 return FindMostEncompassedType (ec, src_types_set);
948 /// Finds the most specific target Tx according to section 13.4.4
950 static public Type FindMostSpecificTarget (EmitContext ec, IList list,
951 Type target, bool apply_explicit_conv_rules)
953 ArrayList tgt_types_set = new ArrayList ();
956 // If any operator converts to T then Tx = T
958 foreach (MethodInfo mi in list){
959 Type ret_type = mi.ReturnType;
960 if (ret_type == target)
963 tgt_types_set.Add (ret_type);
967 // Explicit conv rules
969 if (apply_explicit_conv_rules) {
970 ArrayList candidate_set = new ArrayList ();
972 EmptyExpression expr = EmptyExpression.Grab ();
974 foreach (Type ret_type in tgt_types_set){
975 expr.SetType (ret_type);
977 if (ImplicitStandardConversionExists (ec, expr, target))
978 candidate_set.Add (ret_type);
981 EmptyExpression.Release (expr);
983 if (candidate_set.Count != 0)
984 return FindMostEncompassingType (ec, candidate_set);
988 // Okay, final case !
990 if (apply_explicit_conv_rules)
991 return FindMostEncompassedType (ec, tgt_types_set);
993 return FindMostEncompassingType (ec, tgt_types_set);
997 /// User-defined Implicit conversions
999 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1000 Type target, Location loc)
1002 return UserDefinedConversion (ec, source, target, loc, false);
1006 /// User-defined Explicit conversions
1008 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1009 Type target, Location loc)
1011 return UserDefinedConversion (ec, source, target, loc, true);
1014 static void AddConversionOperators (EmitContext ec, ArrayList list,
1015 Expression source, Type target_type,
1016 bool look_for_explicit,
1022 Type source_type = source.Type;
1023 EmptyExpression expr = EmptyExpression.Grab ();
1024 foreach (MethodInfo m in mg.Methods) {
1025 ParameterData pd = TypeManager.GetParameterData (m);
1026 Type return_type = m.ReturnType;
1027 Type arg_type = pd.ParameterType (0);
1029 if (source_type != arg_type) {
1030 if (!ImplicitStandardConversionExists (ec, source, arg_type)) {
1031 if (!look_for_explicit)
1033 expr.SetType (arg_type);
1034 if (!ImplicitStandardConversionExists (ec, expr, source_type))
1039 if (target_type != return_type) {
1040 expr.SetType (return_type);
1041 if (!ImplicitStandardConversionExists (ec, expr, target_type)) {
1042 if (!look_for_explicit)
1044 expr.SetType (target_type);
1045 if (!ImplicitStandardConversionExists (ec, expr, return_type))
1053 EmptyExpression.Release (expr);
1057 /// Compute the user-defined conversion operator from source_type to target_type.
1058 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1060 static MethodInfo GetConversionOperator (EmitContext ec, Expression source, Type target_type, bool look_for_explicit)
1062 ArrayList ops = new ArrayList (4);
1064 Type source_type = source.Type;
1066 if (source_type != TypeManager.decimal_type) {
1067 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
1068 Expression.MethodLookup (ec, source_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, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1076 if (target_type != TypeManager.decimal_type) {
1077 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
1078 Expression.MethodLookup (ec, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1079 if (look_for_explicit) {
1080 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
1081 Expression.MethodLookup (
1082 ec, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1089 Type most_specific_source = FindMostSpecificSource (ec, ops, source, look_for_explicit);
1090 if (most_specific_source == null)
1093 Type most_specific_target = FindMostSpecificTarget (ec, ops, target_type, look_for_explicit);
1094 if (most_specific_target == null)
1097 MethodInfo method = null;
1099 foreach (MethodInfo m in ops) {
1100 if (m.ReturnType != most_specific_target)
1102 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1104 // Ambiguous: more than one conversion operator satisfies the signature.
1113 static DoubleHash explicit_conv = new DoubleHash (100);
1114 static DoubleHash implicit_conv = new DoubleHash (100);
1117 /// User-defined conversions
1119 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1120 Type target, Location loc,
1121 bool look_for_explicit)
1123 Type source_type = source.Type;
1124 MethodInfo method = null;
1126 if (TypeManager.IsNullableType (source_type) && TypeManager.IsNullableType (target))
1127 return new Nullable.LiftedConversion (
1128 source, target, true, look_for_explicit, loc).Resolve (ec);
1131 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1133 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1134 method = (MethodInfo) o;
1136 method = GetConversionOperator (ec, source, target, look_for_explicit);
1137 if (!(source is Constant))
1138 hash.Insert (source_type, target, method);
1144 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1147 // This will do the conversion to the best match that we
1148 // found. Now we need to perform an implict standard conversion
1149 // if the best match was not the type that we were requested
1152 if (look_for_explicit)
1153 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1155 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1161 e = new UserCast (method, source, loc);
1162 if (e.Type != target){
1163 if (!look_for_explicit)
1164 e = ImplicitConversionStandard (ec, e, target, loc);
1166 e = ExplicitConversionStandard (ec, e, target, loc);
1173 /// Converts implicitly the resolved expression `expr' into the
1174 /// `target_type'. It returns a new expression that can be used
1175 /// in a context that expects a `target_type'.
1177 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1178 Type target_type, Location loc)
1182 if (target_type == null)
1183 throw new Exception ("Target type is null");
1185 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1189 e = ImplicitUserConversion (ec, expr, target_type, loc);
1198 /// Attempts to apply the `Standard Implicit
1199 /// Conversion' rules to the expression `expr' into
1200 /// the `target_type'. It returns a new expression
1201 /// that can be used in a context that expects a
1204 /// This is different from `ImplicitConversion' in that the
1205 /// user defined implicit conversions are excluded.
1207 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1208 Type target_type, Location loc)
1210 Type expr_type = expr.Type;
1213 if (expr is NullLiteral) {
1214 if (target_type.IsGenericParameter)
1215 return TypeParameter_to_Null ((Constant) expr, target_type, loc);
1217 if (TypeManager.IsNullableType (target_type))
1218 return new Nullable.NullableLiteral (target_type, loc);
1221 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1222 return new Nullable.LiftedConversion (
1223 expr, target_type, false, false, loc).Resolve (ec);
1225 if (expr.eclass == ExprClass.MethodGroup){
1226 if (!TypeManager.IsDelegateType (target_type)){
1231 // Only allow anonymous method conversions on post ISO_1
1233 if (RootContext.Version != LanguageVersion.ISO_1){
1234 MethodGroupExpr mg = expr as MethodGroupExpr;
1236 return ImplicitDelegateCreation.Create (
1237 ec, mg, target_type, false, loc);
1241 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1244 e = ImplicitNumericConversion (ec, expr, target_type);
1248 e = ImplicitReferenceConversion (ec, expr, target_type);
1252 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1253 IntLiteral i = (IntLiteral) expr;
1256 return new EnumConstant ((Constant) expr, target_type);
1260 if (expr_type.IsPointer){
1261 if (target_type == TypeManager.void_ptr_type)
1262 return new EmptyCast (expr, target_type);
1265 // yep, comparing pointer types cant be done with
1266 // t1 == t2, we have to compare their element types.
1268 if (target_type.IsPointer){
1269 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1276 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1277 return new EmptyCast (NullPointer.Null, target_type);
1280 if (expr_type == TypeManager.anonymous_method_type){
1281 if (!TypeManager.IsDelegateType (target_type)){
1282 Report.Error (1660, loc,
1283 "Cannot convert anonymous method block to type `{0}' because it is not a delegate type",
1284 TypeManager.CSharpName (target_type));
1288 AnonymousMethod am = (AnonymousMethod) expr;
1289 int errors = Report.Errors;
1291 Expression conv = am.Compatible (ec, target_type, false);
1296 // We return something instead of null, to avoid
1297 // the duplicate error, since am.Compatible would have
1298 // reported that already
1300 if (errors != Report.Errors)
1301 return new EmptyCast (expr, target_type);
1308 /// Attempts to perform an implicit constant conversion of the IntConstant
1309 /// into a different data type using casts (See Implicit Constant
1310 /// Expression Conversions)
1312 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1314 int value = ic.Value;
1316 if (target_type == TypeManager.sbyte_type){
1317 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1318 return new SByteConstant ((sbyte) value, ic.Location);
1319 } else if (target_type == TypeManager.byte_type){
1320 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1321 return new ByteConstant ((byte) value, ic.Location);
1322 } else if (target_type == TypeManager.short_type){
1323 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1324 return new ShortConstant ((short) value, ic.Location);
1325 } else if (target_type == TypeManager.ushort_type){
1326 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1327 return new UShortConstant ((ushort) value, ic.Location);
1328 } else if (target_type == TypeManager.uint32_type){
1330 return new UIntConstant ((uint) value, ic.Location);
1331 } else if (target_type == TypeManager.uint64_type){
1333 // we can optimize this case: a positive int32
1334 // always fits on a uint64. But we need an opcode
1338 return new ULongConstant ((ulong) value, ic.Location);
1339 } else if (target_type == TypeManager.double_type)
1340 return new DoubleConstant ((double) value, ic.Location);
1341 else if (target_type == TypeManager.float_type)
1342 return new FloatConstant ((float) value, ic.Location);
1344 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1345 Type underlying = TypeManager.EnumToUnderlying (target_type);
1346 Constant e = (Constant) ic;
1349 // Possibly, we need to create a different 0 literal before passing
1352 if (underlying == TypeManager.int64_type)
1353 e = new LongLiteral (0, ic.Location);
1354 else if (underlying == TypeManager.uint64_type)
1355 e = new ULongLiteral (0, ic.Location);
1357 return new EnumConstant (e, target_type);
1361 // If `target_type' is an interface and the type of `ic' implements the interface
1362 // e.g. target_type is IComparable, IConvertible, IFormattable
1364 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1365 return new BoxedCast (ic, target_type);
1371 /// Attempts to implicitly convert `source' into `target_type', using
1372 /// ImplicitConversion. If there is no implicit conversion, then
1373 /// an error is signaled
1375 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1376 Type target_type, Location loc)
1380 int errors = Report.Errors;
1381 e = ImplicitConversion (ec, source, target_type, loc);
1382 if (Report.Errors > errors)
1387 if (source is DoubleLiteral) {
1388 if (target_type == TypeManager.float_type) {
1389 Error_664 (loc, "float", "f");
1392 if (target_type == TypeManager.decimal_type) {
1393 Error_664 (loc, "decimal", "m");
1398 source.Error_ValueCannotBeConverted (loc, target_type, false);
1402 static void Error_664 (Location loc, string type, string suffix) {
1403 Report.Error (664, loc,
1404 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
1409 /// Performs the explicit numeric conversions
1411 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1413 Type expr_type = expr.Type;
1414 Type real_target_type = target_type;
1416 if (expr_type == TypeManager.sbyte_type){
1418 // From sbyte to byte, ushort, uint, ulong, char
1420 if (real_target_type == TypeManager.byte_type)
1421 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1422 if (real_target_type == TypeManager.ushort_type)
1423 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1424 if (real_target_type == TypeManager.uint32_type)
1425 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1426 if (real_target_type == TypeManager.uint64_type)
1427 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1428 if (real_target_type == TypeManager.char_type)
1429 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1430 } else if (expr_type == TypeManager.byte_type){
1432 // From byte to sbyte and char
1434 if (real_target_type == TypeManager.sbyte_type)
1435 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1436 if (real_target_type == TypeManager.char_type)
1437 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1438 } else if (expr_type == TypeManager.short_type){
1440 // From short to sbyte, byte, ushort, uint, ulong, char
1442 if (real_target_type == TypeManager.sbyte_type)
1443 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1444 if (real_target_type == TypeManager.byte_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1446 if (real_target_type == TypeManager.ushort_type)
1447 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1448 if (real_target_type == TypeManager.uint32_type)
1449 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1450 if (real_target_type == TypeManager.uint64_type)
1451 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1452 if (real_target_type == TypeManager.char_type)
1453 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1454 } else if (expr_type == TypeManager.ushort_type){
1456 // From ushort to sbyte, byte, short, char
1458 if (real_target_type == TypeManager.sbyte_type)
1459 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1460 if (real_target_type == TypeManager.byte_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1462 if (real_target_type == TypeManager.short_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1464 if (real_target_type == TypeManager.char_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1466 } else if (expr_type == TypeManager.int32_type){
1468 // From int to sbyte, byte, short, ushort, uint, ulong, char
1470 if (real_target_type == TypeManager.sbyte_type)
1471 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1472 if (real_target_type == TypeManager.byte_type)
1473 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1474 if (real_target_type == TypeManager.short_type)
1475 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1476 if (real_target_type == TypeManager.ushort_type)
1477 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1478 if (real_target_type == TypeManager.uint32_type)
1479 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1480 if (real_target_type == TypeManager.uint64_type)
1481 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1482 if (real_target_type == TypeManager.char_type)
1483 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1484 } else if (expr_type == TypeManager.uint32_type){
1486 // From uint to sbyte, byte, short, ushort, int, char
1488 if (real_target_type == TypeManager.sbyte_type)
1489 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1490 if (real_target_type == TypeManager.byte_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1492 if (real_target_type == TypeManager.short_type)
1493 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1494 if (real_target_type == TypeManager.ushort_type)
1495 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1496 if (real_target_type == TypeManager.int32_type)
1497 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1498 if (real_target_type == TypeManager.char_type)
1499 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1500 } else if (expr_type == TypeManager.int64_type){
1502 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1504 if (real_target_type == TypeManager.sbyte_type)
1505 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1506 if (real_target_type == TypeManager.byte_type)
1507 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1508 if (real_target_type == TypeManager.short_type)
1509 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1510 if (real_target_type == TypeManager.ushort_type)
1511 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1512 if (real_target_type == TypeManager.int32_type)
1513 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1514 if (real_target_type == TypeManager.uint32_type)
1515 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1516 if (real_target_type == TypeManager.uint64_type)
1517 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1518 if (real_target_type == TypeManager.char_type)
1519 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1520 } else if (expr_type == TypeManager.uint64_type){
1522 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1524 if (real_target_type == TypeManager.sbyte_type)
1525 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1526 if (real_target_type == TypeManager.byte_type)
1527 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1528 if (real_target_type == TypeManager.short_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1530 if (real_target_type == TypeManager.ushort_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1532 if (real_target_type == TypeManager.int32_type)
1533 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1534 if (real_target_type == TypeManager.uint32_type)
1535 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1536 if (real_target_type == TypeManager.int64_type)
1537 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1538 if (real_target_type == TypeManager.char_type)
1539 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1540 } else if (expr_type == TypeManager.char_type){
1542 // From char to sbyte, byte, short
1544 if (real_target_type == TypeManager.sbyte_type)
1545 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1546 if (real_target_type == TypeManager.byte_type)
1547 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1548 if (real_target_type == TypeManager.short_type)
1549 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1550 } else if (expr_type == TypeManager.float_type){
1552 // From float to sbyte, byte, short,
1553 // ushort, int, uint, long, ulong, char
1556 if (real_target_type == TypeManager.sbyte_type)
1557 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1558 if (real_target_type == TypeManager.byte_type)
1559 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1560 if (real_target_type == TypeManager.short_type)
1561 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1562 if (real_target_type == TypeManager.ushort_type)
1563 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1564 if (real_target_type == TypeManager.int32_type)
1565 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1566 if (real_target_type == TypeManager.uint32_type)
1567 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1568 if (real_target_type == TypeManager.int64_type)
1569 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1570 if (real_target_type == TypeManager.uint64_type)
1571 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1572 if (real_target_type == TypeManager.char_type)
1573 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1574 if (real_target_type == TypeManager.decimal_type)
1575 return new CastToDecimal (expr, true);
1576 } else if (expr_type == TypeManager.double_type){
1578 // From double to sbyte, byte, short,
1579 // ushort, int, uint, long, ulong,
1580 // char, float or decimal
1582 if (real_target_type == TypeManager.sbyte_type)
1583 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1584 if (real_target_type == TypeManager.byte_type)
1585 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1586 if (real_target_type == TypeManager.short_type)
1587 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1588 if (real_target_type == TypeManager.ushort_type)
1589 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1590 if (real_target_type == TypeManager.int32_type)
1591 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1592 if (real_target_type == TypeManager.uint32_type)
1593 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1594 if (real_target_type == TypeManager.int64_type)
1595 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1596 if (real_target_type == TypeManager.uint64_type)
1597 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1598 if (real_target_type == TypeManager.char_type)
1599 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1600 if (real_target_type == TypeManager.float_type)
1601 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1602 if (real_target_type == TypeManager.decimal_type)
1603 return new CastToDecimal (expr, true);
1604 } else if (expr_type == TypeManager.decimal_type) {
1605 return new CastFromDecimal (expr, target_type).Resolve ();
1611 /// Returns whether an explicit reference conversion can be performed
1612 /// from source_type to target_type
1614 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1616 bool target_is_type_param = target_type.IsGenericParameter;
1617 bool target_is_value_type = target_type.IsValueType;
1619 if (source_type == target_type)
1623 // From generic parameter to any type
1625 if (source_type.IsGenericParameter)
1629 // From object to a generic parameter
1631 if (source_type == TypeManager.object_type && target_is_type_param)
1635 // From object to any reference type
1637 if (source_type == TypeManager.object_type && !target_is_value_type)
1641 // From any class S to any class-type T, provided S is a base class of T
1643 if (TypeManager.IsSubclassOf (target_type, source_type))
1647 // From any interface type S to any interface T provided S is not derived from T
1649 if (source_type.IsInterface && target_type.IsInterface){
1650 if (!TypeManager.IsSubclassOf (target_type, source_type))
1655 // From any class type S to any interface T, provided S is not sealed
1656 // and provided S does not implement T.
1658 if (target_type.IsInterface && !source_type.IsSealed &&
1659 !TypeManager.ImplementsInterface (source_type, target_type))
1663 // From any interface-type S to to any class type T, provided T is not
1664 // sealed, or provided T implements S.
1666 if (source_type.IsInterface &&
1667 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1671 // From an array type S with an element type Se to an array type T with an
1672 // element type Te provided all the following are true:
1673 // * S and T differe only in element type, in other words, S and T
1674 // have the same number of dimensions.
1675 // * Both Se and Te are reference types
1676 // * An explicit referenc conversions exist from Se to Te
1678 if (source_type.IsArray && target_type.IsArray) {
1679 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1681 Type source_element_type = TypeManager.GetElementType (source_type);
1682 Type target_element_type = TypeManager.GetElementType (target_type);
1684 if (source_element_type.IsGenericParameter ||
1685 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1686 if (ExplicitReferenceConversionExists (source_element_type,
1687 target_element_type))
1693 // From System.Array to any array-type
1694 if (source_type == TypeManager.array_type &&
1695 target_type.IsArray){
1700 // From System delegate to any delegate-type
1702 if (source_type == TypeManager.delegate_type &&
1703 TypeManager.IsDelegateType (target_type))
1707 // From ICloneable to Array or Delegate types
1709 if (source_type == TypeManager.icloneable_type &&
1710 (target_type == TypeManager.array_type ||
1711 target_type == TypeManager.delegate_type))
1718 /// Implements Explicit Reference conversions
1720 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1722 Type source_type = source.Type;
1723 bool target_is_type_param = target_type.IsGenericParameter;
1724 bool target_is_value_type = target_type.IsValueType;
1727 // From object to a generic parameter
1729 if (source_type == TypeManager.object_type && target_is_type_param)
1730 return new UnboxCast (source, target_type);
1733 // From object to any reference type
1735 if (source_type == TypeManager.object_type && !target_is_value_type)
1736 return new ClassCast (source, target_type);
1739 // Unboxing conversion.
1741 if (((source_type == TypeManager.enum_type &&
1742 !(source is EmptyCast)) ||
1743 source_type == TypeManager.value_type) && target_is_value_type)
1744 return new UnboxCast (source, target_type);
1747 // From any class S to any class-type T, provided S is a base class of T
1749 if (TypeManager.IsSubclassOf (target_type, source_type))
1750 return new ClassCast (source, target_type);
1753 // From any interface type S to any interface T provided S is not derived from T
1755 if (source_type.IsInterface && target_type.IsInterface){
1756 if (TypeManager.ImplementsInterface (source_type, target_type))
1759 return new ClassCast (source, target_type);
1763 // From any class type S to any interface T, provides S is not sealed
1764 // and provided S does not implement T.
1766 if (target_type.IsInterface && !source_type.IsSealed) {
1767 if (TypeManager.ImplementsInterface (source_type, target_type))
1770 return new ClassCast (source, target_type);
1775 // From any interface-type S to to any class type T, provided T is not
1776 // sealed, or provided T implements S.
1778 if (source_type.IsInterface) {
1779 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1780 if (target_type.IsClass)
1781 return new ClassCast (source, target_type);
1783 return new UnboxCast (source, target_type);
1789 // From an array type S with an element type Se to an array type T with an
1790 // element type Te provided all the following are true:
1791 // * S and T differe only in element type, in other words, S and T
1792 // have the same number of dimensions.
1793 // * Both Se and Te are reference types
1794 // * An explicit referenc conversions exist from Se to Te
1796 if (source_type.IsArray && target_type.IsArray) {
1797 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1799 Type source_element_type = TypeManager.GetElementType (source_type);
1800 Type target_element_type = TypeManager.GetElementType (target_type);
1802 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1803 if (ExplicitReferenceConversionExists (source_element_type,
1804 target_element_type))
1805 return new ClassCast (source, target_type);
1810 // From System.Array to any array-type
1811 if (source_type == TypeManager.array_type &&
1812 target_type.IsArray) {
1813 return new ClassCast (source, target_type);
1817 // From System delegate to any delegate-type
1819 if (source_type == TypeManager.delegate_type &&
1820 TypeManager.IsDelegateType (target_type))
1821 return new ClassCast (source, target_type);
1824 // From ICloneable to Array or Delegate types
1826 if (source_type == TypeManager.icloneable_type &&
1827 (target_type == TypeManager.array_type ||
1828 target_type == TypeManager.delegate_type))
1829 return new ClassCast (source, target_type);
1835 /// Performs an explicit conversion of the expression `expr' whose
1836 /// type is expr.Type to `target_type'.
1838 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1839 Type target_type, Location loc)
1841 Type expr_type = expr.Type;
1843 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1844 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1849 // Unboxing conversions; only object types can be convertible to enum
1851 if (expr_type == TypeManager.object_type && target_type.IsValueType || expr_type == TypeManager.enum_type)
1852 return new UnboxCast (expr, target_type);
1854 if (TypeManager.IsEnumType (expr_type)) {
1855 if (expr is EnumConstant)
1856 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1858 return ExplicitConversionCore (ec, new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1861 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1862 return new Nullable.LiftedConversion (
1863 expr, target_type, false, true, loc).Resolve (ec);
1865 if (TypeManager.IsEnumType (target_type)){
1866 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1868 return new EmptyCast (ce, target_type);
1871 ne = ExplicitNumericConversion (expr, target_type);
1876 // Skip the ExplicitReferenceConversion because we can not convert
1877 // from Null to a ValueType, and ExplicitReference wont check against
1878 // null literal explicitly
1880 if (expr_type != TypeManager.null_type){
1881 ne = ExplicitReferenceConversion (expr, target_type);
1887 ne = ExplicitUnsafe (expr, target_type);
1892 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1899 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1901 Type expr_type = expr.Type;
1903 if (target_type.IsPointer){
1904 if (expr_type.IsPointer)
1905 return new EmptyCast (expr, target_type);
1907 if (expr_type == TypeManager.sbyte_type ||
1908 expr_type == TypeManager.short_type ||
1909 expr_type == TypeManager.int32_type ||
1910 expr_type == TypeManager.int64_type)
1911 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1913 if (expr_type == TypeManager.ushort_type ||
1914 expr_type == TypeManager.uint32_type ||
1915 expr_type == TypeManager.uint64_type ||
1916 expr_type == TypeManager.byte_type)
1917 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1920 if (expr_type.IsPointer){
1921 if (target_type == TypeManager.sbyte_type)
1922 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1923 else if (target_type == TypeManager.byte_type)
1924 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1925 else if (target_type == TypeManager.short_type)
1926 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1927 else if (target_type == TypeManager.ushort_type)
1928 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1929 else if (target_type == TypeManager.int32_type)
1930 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1931 else if (target_type == TypeManager.uint32_type)
1932 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1933 else if (target_type == TypeManager.uint64_type)
1934 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1935 else if (target_type == TypeManager.int64_type){
1936 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1943 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1945 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1946 Type target_type, Location l)
1948 int errors = Report.Errors;
1949 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1950 if (Report.Errors > errors)
1956 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
1957 return new Nullable.LiftedConversion (
1958 expr, target_type, false, true, l).Resolve (ec);
1960 ne = ExplicitNumericConversion (expr, target_type);
1964 ne = ExplicitReferenceConversion (expr, target_type);
1968 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1969 return new EmptyCast (expr, target_type);
1971 expr.Error_ValueCannotBeConverted (l, target_type, true);
1976 /// Performs an explicit conversion of the expression `expr' whose
1977 /// type is expr.Type to `target_type'.
1979 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1980 Type target_type, Location loc)
1982 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1986 expr.Error_ValueCannotBeConverted (loc, target_type, true);