2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
19 // A container class for all the conversion operations
21 public class Convert {
23 // This is used to prettify the code: a null argument is allowed
24 // for ImplicitStandardConversion as long as it is known that
25 // no anonymous method will play a role.
27 // FIXME: renamed from `const' to `static' to allow bootstraping from older
28 // versions of the compiler that could not cope with this construct.
30 public static EmitContext ConstantEC = null;
32 static public void Error_CannotConvertType (Location loc, Type source, Type target)
34 Report.Error (30, loc, "Cannot convert type '" +
35 TypeManager.CSharpName (source) + "' to '" +
36 TypeManager.CSharpName (target) + "'");
39 static Expression TypeParameter_to_Null (Expression expr, Type target_type,
42 if (!TypeParameter_to_Null (target_type)) {
43 Report.Error (403, loc, "Cannot convert null to the type " +
44 "parameter `{0}' becaues it could be a value " +
45 "type. Consider using `default ({0})' instead.",
50 return new NullCast (expr, target_type);
53 static bool TypeParameter_to_Null (Type target_type)
55 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
59 if (gc.HasReferenceTypeConstraint)
61 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
67 static Type TypeParam_EffectiveBaseType (EmitContext ec, Type t)
69 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
71 return TypeManager.object_type;
73 return TypeParam_EffectiveBaseType (ec, gc);
76 static Type TypeParam_EffectiveBaseType (EmitContext ec, GenericConstraints gc)
78 ArrayList list = new ArrayList ();
79 list.Add (gc.EffectiveBaseClass);
80 foreach (Type t in gc.InterfaceConstraints) {
81 if (!t.IsGenericParameter)
84 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
86 list.Add (TypeParam_EffectiveBaseType (ec, new_gc));
88 return FindMostEncompassedType (ec, list);
91 static Expression TypeParameterConversion (Expression expr, bool is_reference, Type target_type)
94 return new EmptyCast (expr, target_type);
96 return new BoxedCast (expr, target_type);
99 static Expression ImplicitTypeParameterConversion (EmitContext ec, Expression expr,
102 Type expr_type = expr.Type;
104 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
107 if (target_type == TypeManager.object_type)
108 return new BoxedCast (expr);
113 // We're converting from a type parameter which is known to be a reference type.
114 bool is_reference = gc.IsReferenceType;
115 Type base_type = TypeParam_EffectiveBaseType (ec, gc);
117 if (TypeManager.IsSubclassOf (base_type, target_type))
118 return TypeParameterConversion (expr, is_reference, target_type);
120 if (target_type.IsInterface) {
121 if (TypeManager.ImplementsInterface (base_type, target_type))
122 return TypeParameterConversion (expr, is_reference, target_type);
124 foreach (Type t in gc.InterfaceConstraints) {
125 if (TypeManager.IsSubclassOf (t, target_type))
126 return TypeParameterConversion (expr, is_reference, target_type);
130 foreach (Type t in gc.InterfaceConstraints) {
131 if (!t.IsGenericParameter)
133 if (TypeManager.IsSubclassOf (t, target_type))
134 return TypeParameterConversion (expr, is_reference, target_type);
140 static EmptyExpression MyEmptyExpr;
141 static public Expression WideningReferenceConversion (EmitContext ec, Expression expr, Type target_type)
143 Type expr_type = expr.Type;
145 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
146 // if we are a method group, emit a warning
151 if (expr_type == TypeManager.void_type)
154 if (expr_type.IsGenericParameter)
155 return ImplicitTypeParameterConversion (ec, expr, target_type);
158 // notice that it is possible to write "ValueType v = 1", the ValueType here
159 // is an abstract class, and not really a value type, so we apply the same rules.
161 if (target_type == TypeManager.object_type) {
163 // A pointer type cannot be converted to object
165 if (expr_type.IsPointer)
168 if (TypeManager.IsValueType (expr_type))
169 return new BoxedCast (expr);
170 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
171 if (expr_type == TypeManager.anonymous_method_type)
173 return new EmptyCast (expr, target_type);
177 } else if (target_type == TypeManager.value_type) {
178 if (TypeManager.IsValueType (expr_type))
179 return new BoxedCast (expr);
180 if (expr_type == TypeManager.null_type)
181 return new NullCast (expr, target_type);
184 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
186 // Special case: enumeration to System.Enum.
187 // System.Enum is not a value type, it is a class, so we need
188 // a boxing conversion
190 if (expr_type.IsEnum || expr_type.IsGenericParameter)
191 return new BoxedCast (expr);
193 return new EmptyCast (expr, target_type);
196 // This code is kind of mirrored inside WideningStandardConversionExists
197 // with the small distinction that we only probe there
199 // Always ensure that the code here and there is in sync
201 // from the null type to any reference-type.
202 if (expr_type == TypeManager.null_type){
203 if (target_type.IsPointer)
204 return new EmptyCast (expr, target_type);
206 if (!target_type.IsValueType)
207 return new NullCast (expr, target_type);
210 // from any class-type S to any interface-type T.
211 if (target_type.IsInterface) {
212 if (target_type != TypeManager.iconvertible_type &&
213 expr_type.IsValueType && (expr is Constant) &&
214 !(expr is IntLiteral || expr is BoolLiteral ||
215 expr is FloatLiteral || expr is DoubleLiteral ||
216 expr is LongLiteral || expr is CharLiteral ||
217 expr is StringLiteral || expr is DecimalLiteral ||
218 expr is UIntLiteral || expr is ULongLiteral)) {
222 if (TypeManager.ImplementsInterface (expr_type, target_type)){
223 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
224 return new BoxedCast (expr, target_type);
226 return new EmptyCast (expr, target_type);
230 // from any interface type S to interface-type T.
231 if (expr_type.IsInterface && target_type.IsInterface) {
232 if (TypeManager.ImplementsInterface (expr_type, target_type))
233 return new EmptyCast (expr, target_type);
238 // from an array-type S to an array-type of type T
239 if (expr_type.IsArray && target_type.IsArray) {
240 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
242 Type expr_element_type = TypeManager.GetElementType (expr_type);
244 if (MyEmptyExpr == null)
245 MyEmptyExpr = new EmptyExpression ();
247 MyEmptyExpr.SetType (expr_element_type);
248 Type target_element_type = TypeManager.GetElementType (target_type);
250 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
251 if (WideningStandardConversionExists (ConstantEC, MyEmptyExpr,
252 target_element_type))
253 return new EmptyCast (expr, target_type);
257 // from an array-type to System.Array
258 if (expr_type.IsArray && target_type == TypeManager.array_type)
259 return new EmptyCast (expr, target_type);
261 // from any delegate type to System.Delegate
262 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
263 target_type == TypeManager.delegate_type)
264 return new EmptyCast (expr, target_type);
266 // from any array-type or delegate type into System.ICloneable.
267 if (expr_type.IsArray ||
268 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
269 if (target_type == TypeManager.icloneable_type)
270 return new EmptyCast (expr, target_type);
272 // from a generic type definition to a generic instance.
273 if (TypeManager.IsEqual (expr_type, target_type))
274 return new EmptyCast (expr, target_type);
280 // Tests whether an implicit reference conversion exists between expr_type
283 public static bool WideningReferenceConversionExists (EmitContext ec, Expression expr, Type target_type)
285 Type expr_type = expr.Type;
287 if (expr_type.IsGenericParameter)
288 return ImplicitTypeParameterConversion (ec, expr, target_type) != null;
291 // This is the boxed case.
293 if (target_type == TypeManager.object_type) {
294 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
295 expr_type.IsInterface || expr_type == TypeManager.enum_type)
296 if (target_type != TypeManager.anonymous_method_type)
300 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
303 // Please remember that all code below actually comes
304 // from WideningReferenceConversion so make sure code remains in sync
306 // from any class-type S to any interface-type T.
307 if (target_type.IsInterface) {
308 if (target_type != TypeManager.iconvertible_type &&
309 expr_type.IsValueType && (expr is Constant) &&
310 !(expr is IntLiteral || expr is BoolLiteral ||
311 expr is FloatLiteral || expr is DoubleLiteral ||
312 expr is LongLiteral || expr is CharLiteral ||
313 expr is StringLiteral || expr is DecimalLiteral ||
314 expr is UIntLiteral || expr is ULongLiteral)) {
318 if (TypeManager.ImplementsInterface (expr_type, target_type))
322 // from any interface type S to interface-type T.
323 if (expr_type.IsInterface && target_type.IsInterface)
324 if (TypeManager.ImplementsInterface (expr_type, target_type))
327 // from an array-type S to an array-type of type T
328 if (expr_type.IsArray && target_type.IsArray) {
329 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
331 Type expr_element_type = expr_type.GetElementType ();
333 if (MyEmptyExpr == null)
334 MyEmptyExpr = new EmptyExpression ();
336 MyEmptyExpr.SetType (expr_element_type);
337 Type target_element_type = TypeManager.GetElementType (target_type);
339 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
340 if (WideningStandardConversionExists (ConstantEC, MyEmptyExpr,
341 target_element_type))
346 // from an array-type to System.Array
347 if (expr_type.IsArray && (target_type == TypeManager.array_type))
350 // from any delegate type to System.Delegate
351 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
352 target_type == TypeManager.delegate_type)
353 if (target_type.IsAssignableFrom (expr_type))
356 // from any array-type or delegate type into System.ICloneable.
357 if (expr_type.IsArray ||
358 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
359 if (target_type == TypeManager.icloneable_type)
362 // from the null type to any reference-type.
363 if (expr_type == TypeManager.null_type){
364 if (target_type.IsPointer)
367 if (!target_type.IsValueType)
371 // from a generic type definition to a generic instance.
372 if (TypeManager.IsEqual (expr_type, target_type))
379 /// Implicit Numeric Conversions.
381 /// expr is the expression to convert, returns a new expression of type
382 /// target_type or null if an implicit conversion is not possible.
384 static public Expression WideningNumericConversion (EmitContext ec, Expression expr,
385 Type target_type, Location loc)
387 Type expr_type = expr.Type;
390 // Attempt to do the implicit constant expression conversions
392 if (expr is Constant){
393 if (expr is IntConstant){
396 e = TryWideningIntConversion (target_type, (IntConstant) expr);
400 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
402 // Try the implicit constant expression conversion
403 // from long to ulong, instead of a nice routine,
406 long v = ((LongConstant) expr).Value;
408 return new ULongConstant ((ulong) v);
412 Type real_target_type = target_type;
414 if (expr_type == TypeManager.sbyte_type){
416 // From sbyte to short, int, long, float, double.
418 if (real_target_type == TypeManager.int32_type)
419 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
420 if (real_target_type == TypeManager.int64_type)
421 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
422 if (real_target_type == TypeManager.double_type)
423 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
424 if (real_target_type == TypeManager.float_type)
425 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
426 if (real_target_type == TypeManager.short_type)
427 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
428 } else if (expr_type == TypeManager.byte_type){
430 // From byte to short, ushort, int, uint, long, ulong, float, double
432 if ((real_target_type == TypeManager.short_type) ||
433 (real_target_type == TypeManager.ushort_type) ||
434 (real_target_type == TypeManager.int32_type) ||
435 (real_target_type == TypeManager.uint32_type))
436 return new EmptyCast (expr, target_type);
438 if (real_target_type == TypeManager.uint64_type)
439 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
440 if (real_target_type == TypeManager.int64_type)
441 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
442 if (real_target_type == TypeManager.float_type)
443 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
444 if (real_target_type == TypeManager.double_type)
445 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
446 } else if (expr_type == TypeManager.short_type){
448 // From short to int, long, float, double
450 if (real_target_type == TypeManager.int32_type)
451 return new EmptyCast (expr, target_type);
452 if (real_target_type == TypeManager.int64_type)
453 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
454 if (real_target_type == TypeManager.double_type)
455 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
456 if (real_target_type == TypeManager.float_type)
457 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
458 } else if (expr_type == TypeManager.ushort_type){
460 // From ushort to int, uint, long, ulong, float, double
462 if (real_target_type == TypeManager.uint32_type)
463 return new EmptyCast (expr, target_type);
465 if (real_target_type == TypeManager.uint64_type)
466 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
467 if (real_target_type == TypeManager.int32_type)
468 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
469 if (real_target_type == TypeManager.int64_type)
470 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
471 if (real_target_type == TypeManager.double_type)
472 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
473 if (real_target_type == TypeManager.float_type)
474 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
475 } else if (expr_type == TypeManager.int32_type){
477 // From int to long, float, double
479 if (real_target_type == TypeManager.int64_type)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
481 if (real_target_type == TypeManager.double_type)
482 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
483 if (real_target_type == TypeManager.float_type)
484 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
485 } else if (expr_type == TypeManager.uint32_type){
487 // From uint to long, ulong, float, double
489 if (real_target_type == TypeManager.int64_type)
490 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
491 if (real_target_type == TypeManager.uint64_type)
492 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
493 if (real_target_type == TypeManager.double_type)
494 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
496 if (real_target_type == TypeManager.float_type)
497 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
499 } else if (expr_type == TypeManager.int64_type){
501 // From long/ulong to float, double
503 if (real_target_type == TypeManager.double_type)
504 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
505 if (real_target_type == TypeManager.float_type)
506 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
507 } else if (expr_type == TypeManager.uint64_type){
509 // From ulong to float, double
511 if (real_target_type == TypeManager.double_type)
512 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
514 if (real_target_type == TypeManager.float_type)
515 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
517 } else if (expr_type == TypeManager.char_type){
519 // From char to ushort, int, uint, long, ulong, float, double
521 if ((real_target_type == TypeManager.ushort_type) ||
522 (real_target_type == TypeManager.int32_type) ||
523 (real_target_type == TypeManager.uint32_type))
524 return new EmptyCast (expr, target_type);
525 if (real_target_type == TypeManager.uint64_type)
526 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
527 if (real_target_type == TypeManager.int64_type)
528 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
529 if (real_target_type == TypeManager.float_type)
530 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
531 if (real_target_type == TypeManager.double_type)
532 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
533 } else if (expr_type == TypeManager.float_type){
537 if (real_target_type == TypeManager.double_type)
538 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
546 /// Same as WideningStandardConversionExists except that it also looks at
547 /// implicit user defined conversions - needed for overload resolution
549 public static bool WideningConversionExists (EmitContext ec, Expression expr, Type target_type)
551 if ((expr is NullLiteral) && target_type.IsGenericParameter)
552 return TypeParameter_to_Null (target_type);
554 if (WideningStandardConversionExists (ec, expr, target_type))
558 // VB.NET has no notion of User defined conversions
561 // Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
563 // if (dummy != null)
570 // VB.NET has no notion of User defined conversions
573 // public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
575 // Expression dummy = ImplicitUserConversion (
576 // ec, new EmptyExpression (source), target, Location.Null);
577 // return dummy != 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 WideningStandardConversionExists (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.
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
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, float, double
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, float, double
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, float, double
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, float, double
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 float, double
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, long, ulong, float, double
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 (ec, mg, target_type, Location.Null);
719 if (WideningReferenceConversionExists (ec, expr, target_type))
723 // Implicit Constant Expression Conversions
725 if (expr is IntConstant){
726 int value = ((IntConstant) expr).Value;
728 if (target_type == TypeManager.sbyte_type){
729 if (value >= SByte.MinValue && value <= SByte.MaxValue)
731 } else if (target_type == TypeManager.byte_type){
732 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
734 } else if (target_type == TypeManager.short_type){
735 if (value >= Int16.MinValue && value <= Int16.MaxValue)
737 } else if (target_type == TypeManager.ushort_type){
738 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
740 } else if (target_type == TypeManager.uint32_type){
743 } else if (target_type == TypeManager.uint64_type){
745 // we can optimize this case: a positive int32
746 // always fits on a uint64. But we need an opcode
753 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
757 if (expr is LongConstant && target_type == TypeManager.uint64_type){
759 // Try the implicit constant expression conversion
760 // from long to ulong, instead of a nice routine,
763 long v = ((LongConstant) expr).Value;
768 if ((target_type == TypeManager.enum_type ||
769 target_type.IsSubclassOf (TypeManager.enum_type)) &&
771 IntLiteral i = (IntLiteral) expr;
778 // If `expr_type' implements `target_type' (which is an iface)
779 // see TryWideningIntConversion
781 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
784 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
787 if (expr_type == TypeManager.anonymous_method_type){
788 if (!TypeManager.IsDelegateType (target_type))
791 AnonymousMethod am = (AnonymousMethod) expr;
793 Expression conv = am.Compatible (ec, target_type, true);
802 // Used internally by FindMostEncompassedType, this is used
803 // to avoid creating lots of objects in the tight loop inside
804 // FindMostEncompassedType
806 static EmptyExpression priv_fmet_param;
809 /// Finds "most encompassed type" according to the spec (13.4.2)
810 /// amongst the methods in the MethodGroupExpr
812 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
816 if (priv_fmet_param == null)
817 priv_fmet_param = new EmptyExpression ();
819 foreach (Type t in types){
820 priv_fmet_param.SetType (t);
827 if (WideningStandardConversionExists (ec, priv_fmet_param, best))
835 // Used internally by FindMostEncompassingType, this is used
836 // to avoid creating lots of objects in the tight loop inside
837 // FindMostEncompassingType
839 static EmptyExpression priv_fmee_ret;
842 /// Finds "most encompassing type" according to the spec (13.4.2)
843 /// amongst the types in the given set
845 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
849 if (priv_fmee_ret == null)
850 priv_fmee_ret = new EmptyExpression ();
852 foreach (Type t in types){
853 priv_fmee_ret.SetType (best);
860 if (WideningStandardConversionExists (ec, priv_fmee_ret, t))
868 // Used to avoid creating too many objects
870 static EmptyExpression priv_fms_expr;
873 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
874 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
875 /// for explicit and implicit conversion operators.
877 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
878 Expression source, bool apply_explicit_conv_rules,
881 ArrayList src_types_set = new ArrayList ();
883 if (priv_fms_expr == null)
884 priv_fms_expr = new EmptyExpression ();
887 // If any operator converts from S then Sx = S
889 Type source_type = source.Type;
890 foreach (MethodBase mb in me.Methods){
891 ParameterData pd = Invocation.GetParameterData (mb);
892 Type param_type = pd.ParameterType (0);
894 if (param_type == source_type)
897 if (apply_explicit_conv_rules) {
900 // Find the set of applicable user-defined conversion operators, U. This set
902 // user-defined implicit or explicit conversion operators declared by
903 // the classes or structs in D that convert from a type encompassing
904 // or encompassed by S to a type encompassing or encompassed by T
906 priv_fms_expr.SetType (param_type);
907 if (WideningStandardConversionExists (ec, priv_fms_expr, source_type))
908 src_types_set.Add (param_type);
910 if (WideningStandardConversionExists (ec, source, param_type))
911 src_types_set.Add (param_type);
915 // Only if S is encompassed by param_type
917 if (WideningStandardConversionExists (ec, source, param_type))
918 src_types_set.Add (param_type);
923 // Explicit Conv rules
925 if (apply_explicit_conv_rules) {
926 ArrayList candidate_set = new ArrayList ();
928 foreach (Type param_type in src_types_set){
929 if (WideningStandardConversionExists (ec, source, param_type))
930 candidate_set.Add (param_type);
933 if (candidate_set.Count != 0)
934 return FindMostEncompassedType (ec, candidate_set);
940 if (apply_explicit_conv_rules)
941 return FindMostEncompassingType (ec, src_types_set);
943 return FindMostEncompassedType (ec, src_types_set);
947 // Useful in avoiding proliferation of objects
949 static EmptyExpression priv_fmt_expr;
952 /// Finds the most specific target Tx according to section 13.4.4
954 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
955 Type target, bool apply_explicit_conv_rules,
958 ArrayList tgt_types_set = new ArrayList ();
960 if (priv_fmt_expr == null)
961 priv_fmt_expr = new EmptyExpression ();
964 // If any operator converts to T then Tx = T
966 foreach (MethodInfo mi in me.Methods){
967 Type ret_type = mi.ReturnType;
969 if (ret_type == target)
972 if (apply_explicit_conv_rules) {
975 // Find the set of applicable user-defined conversion operators, U.
977 // This set consists of the
978 // user-defined implicit or explicit conversion operators declared by
979 // the classes or structs in D that convert from a type encompassing
980 // or encompassed by S to a type encompassing or encompassed by T
982 priv_fms_expr.SetType (ret_type);
983 if (WideningStandardConversionExists (ec, priv_fms_expr, target))
984 tgt_types_set.Add (ret_type);
986 priv_fms_expr.SetType (target);
987 if (WideningStandardConversionExists (ec, priv_fms_expr, ret_type))
988 tgt_types_set.Add (ret_type);
992 // Only if T is encompassed by param_type
994 priv_fms_expr.SetType (ret_type);
995 if (WideningStandardConversionExists (ec, priv_fms_expr, target))
996 tgt_types_set.Add (ret_type);
1001 // Explicit conv rules
1003 if (apply_explicit_conv_rules) {
1004 ArrayList candidate_set = new ArrayList ();
1006 foreach (Type ret_type in tgt_types_set){
1007 priv_fmt_expr.SetType (ret_type);
1009 if (WideningStandardConversionExists (ec, priv_fmt_expr, target))
1010 candidate_set.Add (ret_type);
1013 if (candidate_set.Count != 0)
1014 return FindMostEncompassingType (ec, candidate_set);
1018 // Okay, final case !
1020 if (apply_explicit_conv_rules)
1021 return FindMostEncompassedType (ec, tgt_types_set);
1023 return FindMostEncompassingType (ec, tgt_types_set);
1027 /// User-defined Implicit conversions
1031 // VB.NET has no notion of User defined conversions
1034 // static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1035 // Type target, Location loc)
1037 // return UserDefinedConversion (ec, source, target, loc, false);
1041 /// User-defined Explicit conversions
1045 // VB.NET has no notion of User defined conversions
1048 // static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1049 // Type target, Location loc)
1051 // return UserDefinedConversion (ec, source, target, loc, true);
1054 static DoubleHash explicit_conv = new DoubleHash (100);
1055 static DoubleHash implicit_conv = new DoubleHash (100);
1057 /// Computes the MethodGroup for the user-defined conversion
1058 /// operators from source_type to target_type. `look_for_explicit'
1059 /// controls whether we should also include the list of explicit
1062 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1063 Type source_type, Type target_type,
1064 Location loc, bool look_for_explicit)
1066 Expression mg1 = null, mg2 = null;
1067 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1070 op_name = "op_Implicit";
1072 MethodGroupExpr union3;
1074 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1075 return (MethodGroupExpr) r;
1077 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1078 if (source_type.BaseType != null)
1079 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1082 union3 = (MethodGroupExpr) mg2;
1083 else if (mg2 == null)
1084 union3 = (MethodGroupExpr) mg1;
1086 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1088 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1091 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1093 union3 = (MethodGroupExpr) mg1;
1096 if (target_type.BaseType != null)
1097 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1101 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1103 union3 = (MethodGroupExpr) mg1;
1106 MethodGroupExpr union4 = null;
1108 if (look_for_explicit) {
1109 op_name = "op_Explicit";
1111 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1112 if (source_type.BaseType != null)
1113 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1115 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1116 if (target_type.BaseType != null)
1117 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1119 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1120 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1122 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1125 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1126 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1131 /// User-defined conversions
1135 // VB.NET has no notion of User defined conversions. This method is not used.
1137 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1138 Type target, Location loc,
1139 bool look_for_explicit)
1141 MethodGroupExpr union;
1142 Type source_type = source.Type;
1143 MethodBase method = null;
1145 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1149 Type most_specific_source, most_specific_target;
1151 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1152 if (most_specific_source == null)
1155 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1156 if (most_specific_target == null)
1162 foreach (MethodBase mb in union.Methods){
1163 ParameterData pd = Invocation.GetParameterData (mb);
1164 MethodInfo mi = (MethodInfo) mb;
1166 if (pd.ParameterType (0) == most_specific_source &&
1167 mi.ReturnType == most_specific_target) {
1173 if (method == null || count > 1)
1178 // This will do the conversion to the best match that we
1179 // found. Now we need to perform an implict standard conversion
1180 // if the best match was not the type that we were requested
1183 if (look_for_explicit)
1184 source = WideningAndNarrowingConversionStandard (ec, source, most_specific_source, loc);
1186 source = WideningConversionStandard (ec, source, most_specific_source, loc);
1192 e = new UserCast ((MethodInfo) method, source, loc);
1193 if (e.Type != target){
1194 if (!look_for_explicit)
1195 e = WideningConversionStandard (ec, e, target, loc);
1197 e = WideningAndNarrowingConversionStandard (ec, e, target, loc);
1204 /// Converts implicitly the resolved expression `expr' into the
1205 /// `target_type'. It returns a new expression that can be used
1206 /// in a context that expects a `target_type'.
1208 static public Expression WideningConversion (EmitContext ec, Expression expr,
1209 Type target_type, Location loc)
1213 if (target_type == null)
1214 throw new Exception ("Target type is null");
1216 e = WideningConversionStandard (ec, expr, target_type, loc);
1221 // VB.NET has no notion of User defined conversions
1224 // e = ImplicitUserConversion (ec, expr, target_type, loc);
1233 /// Attempts to apply the `Standard Implicit
1234 /// Conversion' rules to the expression `expr' into
1235 /// the `target_type'. It returns a new expression
1236 /// that can be used in a context that expects a
1239 /// This is different from `WideningConversion' in that the
1240 /// user defined implicit conversions are excluded.
1242 static public Expression WideningConversionStandard (EmitContext ec, Expression expr,
1243 Type target_type, Location loc)
1245 Type expr_type = expr.Type;
1248 if ((expr is NullLiteral) && target_type.IsGenericParameter)
1249 return TypeParameter_to_Null (expr, target_type, loc);
1251 if (expr.eclass == ExprClass.MethodGroup){
1252 if (!TypeManager.IsDelegateType (target_type)){
1257 // Only allow anonymous method conversions on post ISO_1
1259 if (RootContext.Version != LanguageVersion.ISO_1){
1260 MethodGroupExpr mg = expr as MethodGroupExpr;
1262 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1266 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1269 e = WideningNumericConversion (ec, expr, target_type, loc);
1273 e = WideningReferenceConversion (ec, expr, target_type);
1277 if ((target_type == TypeManager.enum_type ||
1278 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1279 expr is IntLiteral){
1280 IntLiteral i = (IntLiteral) expr;
1283 return new EnumConstant ((Constant) expr, target_type);
1287 if (expr_type.IsPointer){
1288 if (target_type == TypeManager.void_ptr_type)
1289 return new EmptyCast (expr, target_type);
1292 // yep, comparing pointer types cant be done with
1293 // t1 == t2, we have to compare their element types.
1295 if (target_type.IsPointer){
1296 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1301 if (target_type.IsPointer) {
1302 if (expr_type == TypeManager.null_type)
1303 return new EmptyCast (expr, target_type);
1305 if (expr_type == TypeManager.void_ptr_type)
1306 return new EmptyCast (expr, target_type);
1310 if (expr_type == TypeManager.anonymous_method_type){
1311 if (!TypeManager.IsDelegateType (target_type)){
1312 Report.Error (1660, loc,
1313 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1314 TypeManager.CSharpName (target_type));
1318 AnonymousMethod am = (AnonymousMethod) expr;
1319 int errors = Report.Errors;
1321 Expression conv = am.Compatible (ec, target_type, false);
1326 // We return something instead of null, to avoid
1327 // the duplicate error, since am.Compatible would have
1328 // reported that already
1330 if (errors != Report.Errors)
1331 return new EmptyCast (expr, target_type);
1338 /// Attempts to perform an implicit constant conversion of the IntConstant
1339 /// into a different data type using casts (See Implicit Constant
1340 /// Expression Conversions)
1342 static public Expression TryWideningIntConversion (Type target_type, IntConstant ic)
1344 int value = ic.Value;
1346 if (target_type == TypeManager.sbyte_type){
1347 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1348 return new SByteConstant ((sbyte) value);
1349 } else if (target_type == TypeManager.byte_type){
1350 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1351 return new ByteConstant ((byte) value);
1352 } else if (target_type == TypeManager.short_type){
1353 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1354 return new ShortConstant ((short) value);
1355 } else if (target_type == TypeManager.ushort_type){
1356 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1357 return new UShortConstant ((ushort) value);
1358 } else if (target_type == TypeManager.uint32_type){
1360 return new UIntConstant ((uint) value);
1361 } else if (target_type == TypeManager.uint64_type){
1363 // we can optimize this case: a positive int32
1364 // always fits on a uint64. But we need an opcode
1368 return new ULongConstant ((ulong) value);
1369 } else if (target_type == TypeManager.double_type)
1370 return new DoubleConstant ((double) value);
1371 else if (target_type == TypeManager.float_type)
1372 return new FloatConstant ((float) value);
1374 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1375 Type underlying = TypeManager.EnumToUnderlying (target_type);
1376 Constant e = (Constant) ic;
1379 // Possibly, we need to create a different 0 literal before passing
1382 if (underlying == TypeManager.int64_type)
1383 e = new LongLiteral (0);
1384 else if (underlying == TypeManager.uint64_type)
1385 e = new ULongLiteral (0);
1387 return new EnumConstant (e, target_type);
1391 // If `target_type' is an interface and the type of `ic' implements the interface
1392 // e.g. target_type is IComparable, IConvertible, IFormattable
1394 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1395 return new BoxedCast (ic);
1400 static public void Error_CannotWideningConversion (Location loc, Type source, Type target)
1402 if (source.Name == target.Name){
1403 Report.ExtraInformation (loc,
1405 "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
1406 source.Assembly.FullName, target.Assembly.FullName));
1409 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1410 source == TypeManager.anonymous_method_type ?
1411 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1412 TypeManager.CSharpName (target));
1416 /// Attempts to implicitly convert `source' into `target_type', using
1417 /// WideningConversion. If there is no implicit conversion, then
1418 /// an error is signaled
1420 static public Expression WideningConversionRequired (EmitContext ec, Expression source,
1421 Type target_type, Location loc)
1425 int errors = Report.Errors;
1426 e = WideningConversion (ec, source, target_type, loc);
1427 if (Report.Errors > errors)
1432 if (source is DoubleLiteral) {
1433 if (target_type == TypeManager.float_type) {
1434 Error_664 (loc, "float", "f");
1437 if (target_type == TypeManager.decimal_type) {
1438 Error_664 (loc, "decimal", "m");
1443 if (source is Constant){
1444 Constant c = (Constant) source;
1446 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1450 Error_CannotWideningConversion (loc, source.Type, target_type);
1455 static void Error_664 (Location loc, string type, string suffix) {
1456 Report.Error (664, loc,
1457 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1462 /// Performs the explicit numeric conversions
1464 static Expression NarrowingNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1466 Type expr_type = expr.Type;
1469 // If we have an enumeration, extract the underlying type,
1470 // use this during the comparison, but wrap around the original
1473 Type real_target_type = target_type;
1475 if (TypeManager.IsEnumType (real_target_type))
1476 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1478 if (WideningStandardConversionExists (ec, expr, real_target_type)){
1479 Expression ce = WideningConversionStandard (ec, expr, real_target_type, loc);
1481 if (real_target_type != target_type)
1482 return new EmptyCast (ce, target_type);
1486 if (expr_type == TypeManager.sbyte_type){
1488 // From sbyte to byte, ushort, uint, ulong, char
1490 if (real_target_type == TypeManager.byte_type)
1491 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1492 if (real_target_type == TypeManager.ushort_type)
1493 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1494 if (real_target_type == TypeManager.uint32_type)
1495 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1496 if (real_target_type == TypeManager.uint64_type)
1497 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1498 if (real_target_type == TypeManager.char_type)
1499 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1500 } else if (expr_type == TypeManager.byte_type){
1502 // From byte to sbyte and char
1504 if (real_target_type == TypeManager.sbyte_type)
1505 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1506 if (real_target_type == TypeManager.char_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1508 } else if (expr_type == TypeManager.short_type){
1510 // From short to sbyte, byte, ushort, uint, ulong, char
1512 if (real_target_type == TypeManager.sbyte_type)
1513 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1514 if (real_target_type == TypeManager.byte_type)
1515 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1516 if (real_target_type == TypeManager.ushort_type)
1517 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1518 if (real_target_type == TypeManager.uint32_type)
1519 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1520 if (real_target_type == TypeManager.uint64_type)
1521 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1522 if (real_target_type == TypeManager.char_type)
1523 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1524 } else if (expr_type == TypeManager.ushort_type){
1526 // From ushort to sbyte, byte, short, char
1528 if (real_target_type == TypeManager.sbyte_type)
1529 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1530 if (real_target_type == TypeManager.byte_type)
1531 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1532 if (real_target_type == TypeManager.short_type)
1533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1534 if (real_target_type == TypeManager.char_type)
1535 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1536 } else if (expr_type == TypeManager.int32_type){
1538 // From int to sbyte, byte, short, ushort, uint, ulong, char
1540 if (real_target_type == TypeManager.sbyte_type)
1541 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1542 if (real_target_type == TypeManager.byte_type)
1543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1544 if (real_target_type == TypeManager.short_type)
1545 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1546 if (real_target_type == TypeManager.ushort_type)
1547 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1548 if (real_target_type == TypeManager.uint32_type)
1549 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1550 if (real_target_type == TypeManager.uint64_type)
1551 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1552 if (real_target_type == TypeManager.char_type)
1553 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1554 } else if (expr_type == TypeManager.uint32_type){
1556 // From uint to sbyte, byte, short, ushort, int, char
1558 if (real_target_type == TypeManager.sbyte_type)
1559 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1560 if (real_target_type == TypeManager.byte_type)
1561 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1562 if (real_target_type == TypeManager.short_type)
1563 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1564 if (real_target_type == TypeManager.ushort_type)
1565 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1566 if (real_target_type == TypeManager.int32_type)
1567 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1568 if (real_target_type == TypeManager.char_type)
1569 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1570 } else if (expr_type == TypeManager.int64_type){
1572 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1574 if (real_target_type == TypeManager.sbyte_type)
1575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1576 if (real_target_type == TypeManager.byte_type)
1577 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1578 if (real_target_type == TypeManager.short_type)
1579 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1580 if (real_target_type == TypeManager.ushort_type)
1581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1582 if (real_target_type == TypeManager.int32_type)
1583 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1584 if (real_target_type == TypeManager.uint32_type)
1585 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1586 if (real_target_type == TypeManager.uint64_type)
1587 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1588 if (real_target_type == TypeManager.char_type)
1589 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1590 } else if (expr_type == TypeManager.uint64_type){
1592 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1594 if (real_target_type == TypeManager.sbyte_type)
1595 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1596 if (real_target_type == TypeManager.byte_type)
1597 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1598 if (real_target_type == TypeManager.short_type)
1599 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1600 if (real_target_type == TypeManager.ushort_type)
1601 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1602 if (real_target_type == TypeManager.int32_type)
1603 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1604 if (real_target_type == TypeManager.uint32_type)
1605 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1606 if (real_target_type == TypeManager.int64_type)
1607 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1608 if (real_target_type == TypeManager.char_type)
1609 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1610 } else if (expr_type == TypeManager.char_type){
1612 // From char to sbyte, byte, short
1614 if (real_target_type == TypeManager.sbyte_type)
1615 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1616 if (real_target_type == TypeManager.byte_type)
1617 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1618 if (real_target_type == TypeManager.short_type)
1619 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1620 } else if (expr_type == TypeManager.float_type){
1622 // From float to sbyte, byte, short,
1623 // ushort, int, uint, long, ulong, char
1626 if (real_target_type == TypeManager.sbyte_type)
1627 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1628 if (real_target_type == TypeManager.byte_type)
1629 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1630 if (real_target_type == TypeManager.short_type)
1631 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1632 if (real_target_type == TypeManager.ushort_type)
1633 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1634 if (real_target_type == TypeManager.int32_type)
1635 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1636 if (real_target_type == TypeManager.uint32_type)
1637 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1638 if (real_target_type == TypeManager.int64_type)
1639 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1640 if (real_target_type == TypeManager.uint64_type)
1641 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1642 if (real_target_type == TypeManager.char_type)
1643 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1644 } else if (expr_type == TypeManager.double_type){
1646 // From double to byte, byte, short,
1647 // ushort, int, uint, long, ulong,
1648 // char, float or decimal
1650 if (real_target_type == TypeManager.sbyte_type)
1651 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1652 if (real_target_type == TypeManager.byte_type)
1653 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1654 if (real_target_type == TypeManager.short_type)
1655 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1656 if (real_target_type == TypeManager.ushort_type)
1657 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1658 if (real_target_type == TypeManager.int32_type)
1659 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1660 if (real_target_type == TypeManager.uint32_type)
1661 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1662 if (real_target_type == TypeManager.int64_type)
1663 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1664 if (real_target_type == TypeManager.uint64_type)
1665 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1666 if (real_target_type == TypeManager.char_type)
1667 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1668 if (real_target_type == TypeManager.float_type)
1669 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1672 // decimal is taken care of by the op_Explicit methods.
1678 /// VB.NET specific: Convert to and from boolean
1681 static public Expression BooleanConversions (EmitContext ec, Expression expr,
1682 Type target_type, Location loc)
1684 Type expr_type = expr.Type;
1685 Type real_target_type = target_type;
1687 if (expr_type == TypeManager.bool_type) {
1690 // From boolean to byte, short, int,
1691 // long, float, double, decimal
1694 if (real_target_type == TypeManager.byte_type)
1695 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_U1);
1696 if (real_target_type == TypeManager.short_type)
1697 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I2);
1698 if (real_target_type == TypeManager.int32_type)
1699 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I4);
1700 if (real_target_type == TypeManager.int64_type)
1701 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I8);
1702 if (real_target_type == TypeManager.float_type)
1703 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_R4);
1704 if (real_target_type == TypeManager.double_type)
1705 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_R8);
1706 if (real_target_type == TypeManager.decimal_type) {
1707 return new ImplicitInvocation("DecimalType", "FromBoolean", loc, expr);
1709 } if (real_target_type == TypeManager.bool_type) {
1712 // From byte, short, int, long, float,
1713 // double, decimal to boolean
1716 if (expr_type == TypeManager.byte_type ||
1717 expr_type == TypeManager.short_type ||
1718 expr_type == TypeManager.int32_type ||
1719 expr_type == TypeManager.int64_type ||
1720 expr_type == TypeManager.float_type ||
1721 expr_type == TypeManager.double_type)
1722 return new NumericToBooleanCast (expr, expr_type);
1723 if (expr_type == TypeManager.decimal_type) {
1724 return new ImplicitInvocation("System", "Convert", "ToBoolean", loc, expr);
1732 /// VB.NET specific: Widening conversions to string
1735 static public Expression WideningStringConversions (EmitContext ec, Expression expr,
1736 Type target_type, Location loc)
1738 Expression ret_expr = DoWideningStringConversions (ec, expr, target_type, loc);
1740 if (ret_expr != null)
1741 ret_expr = ret_expr.Resolve (ec);
1746 static public Expression DoWideningStringConversions (EmitContext ec, Expression expr,
1747 Type target_type, Location loc)
1750 Type expr_type = expr.Type;
1751 Type real_target_type = target_type;
1753 if (real_target_type == TypeManager.string_type) {
1755 // From char to string
1757 if (expr_type == TypeManager.char_type)
1758 return new ImplicitInvocation ("StringType", "FromChar", loc, expr);
1761 if(expr_type.IsArray && (expr_type.GetElementType() == TypeManager.char_type)) {
1763 // From char array to string
1765 return new ImplicitNew ("System", "String", loc, expr);
1772 /// VB.NET specific: Narrowing conversions involving strings
1775 static public Expression NarrowingStringConversions (EmitContext ec, Expression expr,
1776 Type target_type, Location loc)
1778 Expression ret_expr = DoNarrowingStringConversions (ec, expr, target_type, loc);
1780 if (ret_expr != null)
1781 ret_expr = ret_expr.Resolve (ec);
1786 static public Expression DoNarrowingStringConversions (EmitContext ec, Expression expr,
1787 Type target_type, Location loc)
1789 Type expr_type = expr.Type;
1790 Type real_target_type = target_type;
1792 // FIXME: Need to take care of Constants
1794 if (expr_type == TypeManager.string_type) {
1797 // From string to chararray, bool,
1798 // byte, short, char, int, long,
1799 // float, double, decimal and date
1802 if (real_target_type.IsArray && (real_target_type.GetElementType() == TypeManager.char_type))
1803 return new ImplicitInvocation("CharArrayType", "FromString", loc, expr);
1804 if (real_target_type == TypeManager.bool_type)
1805 return new ImplicitInvocation("BooleanType", "FromString", loc, expr);
1806 if (real_target_type == TypeManager.byte_type)
1807 return new ImplicitInvocation("ByteType", "FromString", loc, expr);
1808 if (real_target_type == TypeManager.short_type)
1809 return new ImplicitInvocation("ShortType", "FromString", loc, expr);
1810 if (real_target_type == TypeManager.char_type)
1811 return new ImplicitInvocation("CharType", "FromString", loc, expr);
1812 if (real_target_type == TypeManager.int32_type)
1813 return new ImplicitInvocation("IntegerType", "FromString", loc, expr);
1814 if (real_target_type == TypeManager.int64_type)
1815 return new ImplicitInvocation("LongType", "FromString", loc, expr);
1816 if (real_target_type == TypeManager.float_type)
1817 return new ImplicitInvocation("SingleType", "FromString", loc, expr);
1818 if (real_target_type == TypeManager.double_type)
1819 return new ImplicitInvocation("DoubleType", "FromString", loc, expr);
1820 if (real_target_type == TypeManager.decimal_type)
1821 return new ImplicitInvocation("DecimalType", "FromString", loc, expr);
1822 if (real_target_type == TypeManager.date_type)
1823 return new ImplicitInvocation("DateType", "FromString", loc, expr);
1824 } if (real_target_type == TypeManager.string_type) {
1827 // From bool, byte, short, char, int,
1828 // long, float, double, decimal and
1832 if (expr_type == TypeManager.bool_type)
1833 return new ImplicitInvocation("StringType", "FromBoolean", loc, expr);
1834 if (expr_type == TypeManager.byte_type)
1835 return new ImplicitInvocation("StringType", "FromByte", loc, expr);
1836 if (expr_type == TypeManager.short_type)
1837 return new ImplicitInvocation("StringType", "FromShort", loc, expr);
1838 if (expr_type == TypeManager.int32_type)
1839 return new ImplicitInvocation("StringType", "FromInteger", loc, expr);
1840 if (expr_type == TypeManager.int64_type)
1841 return new ImplicitInvocation("StringType", "FromLong", loc, expr);
1842 if (expr_type == TypeManager.float_type)
1843 return new ImplicitInvocation("StringType", "FromSingle", loc, expr);
1844 if (expr_type == TypeManager.double_type)
1845 return new ImplicitInvocation("StringType", "FromDouble", loc, expr);
1846 if (expr_type == TypeManager.decimal_type)
1847 return new ImplicitInvocation("StringType", "FromDecimal", loc, expr);
1848 if (expr_type == TypeManager.date_type)
1849 return new ImplicitInvocation("StringType", "FromDate", loc, expr);
1856 /// Returns whether an explicit reference conversion can be performed
1857 /// from source_type to target_type
1859 public static bool NarrowingReferenceConversionExists (Type source_type, Type target_type)
1861 bool target_is_type_param = target_type.IsGenericParameter;
1862 bool target_is_value_type = target_type.IsValueType;
1864 if (source_type == target_type)
1868 // From object to a generic parameter
1870 if (source_type == TypeManager.object_type && target_is_type_param)
1874 // From object to any reference type
1876 if (source_type == TypeManager.object_type && !target_is_value_type)
1880 // From any class S to any class-type T, provided S is a base class of T
1882 if (TypeManager.IsSubclassOf (target_type, source_type))
1886 // From any interface type S to any interface T provided S is not derived from T
1888 if (source_type.IsInterface && target_type.IsInterface){
1889 if (!TypeManager.IsSubclassOf (target_type, source_type))
1894 // From any class type S to any interface T, provided S is not sealed
1895 // and provided S does not implement T.
1897 if (target_type.IsInterface && !source_type.IsSealed &&
1898 !TypeManager.ImplementsInterface (source_type, target_type))
1902 // From any interface-type S to to any class type T, provided T is not
1903 // sealed, or provided T implements S.
1905 if (source_type.IsInterface &&
1906 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1910 // From an array type S with an element type Se to an array type T with an
1911 // element type Te provided all the following are true:
1912 // * S and T differe only in element type, in other words, S and T
1913 // have the same number of dimensions.
1914 // * Both Se and Te are reference types
1915 // * An explicit referenc conversions exist from Se to Te
1917 if (source_type.IsArray && target_type.IsArray) {
1918 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1920 Type source_element_type = TypeManager.GetElementType (source_type);
1921 Type target_element_type = TypeManager.GetElementType (target_type);
1923 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1924 if (NarrowingReferenceConversionExists (source_element_type,
1925 target_element_type))
1931 // From System.Array to any array-type
1932 if (source_type == TypeManager.array_type &&
1933 target_type.IsArray){
1938 // From System delegate to any delegate-type
1940 if (source_type == TypeManager.delegate_type &&
1941 TypeManager.IsDelegateType (target_type))
1945 // From ICloneable to Array or Delegate types
1947 if (source_type == TypeManager.icloneable_type &&
1948 (target_type == TypeManager.array_type ||
1949 target_type == TypeManager.delegate_type))
1956 /// Implements Explicit Reference conversions
1958 static Expression NarrowingReferenceConversion (Expression source, Type target_type)
1960 Type source_type = source.Type;
1961 bool target_is_type_param = target_type.IsGenericParameter;
1962 bool target_is_value_type = target_type.IsValueType;
1965 // From object to a generic parameter
1967 if (source_type == TypeManager.object_type && target_is_type_param)
1968 return new UnboxCast (source, target_type);
1971 // From object to any reference type
1973 if (source_type == TypeManager.object_type && !target_is_value_type)
1974 return new ClassCast (source, target_type);
1977 // Unboxing conversion.
1979 if (((source_type == TypeManager.enum_type &&
1980 !(source is EmptyCast)) ||
1981 source_type == TypeManager.value_type) && target_is_value_type)
1982 return new UnboxCast (source, target_type);
1985 // From any class S to any class-type T, provided S is a base class of T
1987 if (TypeManager.IsSubclassOf (target_type, source_type))
1988 return new ClassCast (source, target_type);
1991 // From any interface type S to any interface T provided S is not derived from T
1993 if (source_type.IsInterface && target_type.IsInterface){
1994 if (TypeManager.ImplementsInterface (source_type, target_type))
1997 return new ClassCast (source, target_type);
2001 // From any class type S to any interface T, provides S is not sealed
2002 // and provided S does not implement T.
2004 if (target_type.IsInterface && !source_type.IsSealed) {
2005 if (TypeManager.ImplementsInterface (source_type, target_type))
2008 return new ClassCast (source, target_type);
2013 // From any interface-type S to to any class type T, provided T is not
2014 // sealed, or provided T implements S.
2016 if (source_type.IsInterface) {
2017 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
2018 if (target_type.IsClass)
2019 return new ClassCast (source, target_type);
2021 return new UnboxCast (source, target_type);
2027 // From an array type S with an element type Se to an array type T with an
2028 // element type Te provided all the following are true:
2029 // * S and T differe only in element type, in other words, S and T
2030 // have the same number of dimensions.
2031 // * Both Se and Te are reference types
2032 // * An explicit referenc conversions exist from Se to Te
2034 if (source_type.IsArray && target_type.IsArray) {
2035 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2037 Type source_element_type = TypeManager.GetElementType (source_type);
2038 Type target_element_type = TypeManager.GetElementType (target_type);
2040 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2041 if (NarrowingReferenceConversionExists (source_element_type,
2042 target_element_type))
2043 return new ClassCast (source, target_type);
2048 // From System.Array to any array-type
2049 if (source_type == TypeManager.array_type &&
2050 target_type.IsArray) {
2051 return new ClassCast (source, target_type);
2055 // From System delegate to any delegate-type
2057 if (source_type == TypeManager.delegate_type &&
2058 TypeManager.IsDelegateType (target_type))
2059 return new ClassCast (source, target_type);
2062 // From ICloneable to Array or Delegate types
2064 if (source_type == TypeManager.icloneable_type &&
2065 (target_type == TypeManager.array_type ||
2066 target_type == TypeManager.delegate_type))
2067 return new ClassCast (source, target_type);
2073 /// Performs an explicit conversion of the expression `expr' whose
2074 /// type is expr.Type to `target_type'.
2076 static public Expression WideningAndNarrowingConversion (EmitContext ec, Expression expr,
2077 Type target_type, Location loc)
2079 Type expr_type = expr.Type;
2080 Type original_expr_type = expr_type;
2082 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
2083 if (target_type == TypeManager.enum_type ||
2084 target_type == TypeManager.object_type) {
2085 if (expr is EnumConstant)
2086 expr = ((EnumConstant) expr).Child;
2087 // We really need all these casts here .... :-(
2088 expr = new BoxedCast (new EmptyCast (expr, expr_type));
2089 return new EmptyCast (expr, target_type);
2090 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
2091 target_type.IsSubclassOf (TypeManager.enum_type))
2092 return new UnboxCast (expr, target_type);
2095 // Notice that we have kept the expr_type unmodified, which is only
2097 if (expr is EnumConstant)
2098 expr = ((EnumConstant) expr).Child;
2100 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2101 expr_type = expr.Type;
2104 int errors = Report.Errors;
2105 Expression ne = WideningConversionStandard (ec, expr, target_type, loc);
2106 if (Report.Errors > errors)
2112 ne = NarrowingNumericConversion (ec, expr, target_type, loc);
2117 // Unboxing conversion.
2119 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2120 return new UnboxCast (expr, target_type);
2123 // Skip the NarrowingReferenceConversion because we can not convert
2124 // from Null to a ValueType, and ExplicitReference wont check against
2125 // null literal explicitly
2127 if (expr_type != TypeManager.null_type){
2128 ne = NarrowingReferenceConversion (expr, target_type);
2135 if (target_type.IsPointer){
2136 if (expr_type.IsPointer)
2137 return new EmptyCast (expr, target_type);
2139 if (expr_type == TypeManager.sbyte_type ||
2140 expr_type == TypeManager.byte_type ||
2141 expr_type == TypeManager.short_type ||
2142 expr_type == TypeManager.ushort_type ||
2143 expr_type == TypeManager.int32_type ||
2144 expr_type == TypeManager.uint32_type ||
2145 expr_type == TypeManager.uint64_type ||
2146 expr_type == TypeManager.int64_type)
2147 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2149 if (expr_type.IsPointer){
2150 Expression e = null;
2152 if (target_type == TypeManager.sbyte_type)
2153 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2154 else if (target_type == TypeManager.byte_type)
2155 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2156 else if (target_type == TypeManager.short_type)
2157 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2158 else if (target_type == TypeManager.ushort_type)
2159 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2160 else if (target_type == TypeManager.int32_type)
2161 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2162 else if (target_type == TypeManager.uint32_type)
2163 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2164 else if (target_type == TypeManager.uint64_type)
2165 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2166 else if (target_type == TypeManager.int64_type){
2167 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2173 ci = WideningConversionStandard (ec, e, target_type, loc);
2178 ce = NarrowingNumericConversion (ec, e, target_type, loc);
2182 // We should always be able to go from an uint32
2183 // implicitly or explicitly to the other integral
2186 throw new Exception ("Internal compiler error");
2192 // VB.NET has no notion of User defined conversions
2195 // ne = ExplicitUserConversion (ec, expr, target_type, loc);
2199 if (expr is NullLiteral){
2200 Report.Error (37, loc, "Cannot convert null to value type `" +
2201 TypeManager.CSharpName (target_type) + "'");
2205 Error_CannotConvertType (loc, original_expr_type, target_type);
2210 /// Same as WideningAndNarrowingConversion, only it doesn't include user defined conversions
2212 static public Expression WideningAndNarrowingConversionStandard (EmitContext ec, Expression expr,
2213 Type target_type, Location l)
2215 int errors = Report.Errors;
2216 Expression ne = WideningConversionStandard (ec, expr, target_type, l);
2217 if (Report.Errors > errors)
2223 ne = NarrowingNumericConversion (ec, expr, target_type, l);
2227 ne = NarrowingReferenceConversion (expr, target_type);
2231 Error_CannotConvertType (l, expr.Type, target_type);