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.
385 static public Expression WideningNumericConversion (EmitContext ec, Expression expr,
386 Type target_type, Location loc)
388 Type expr_type = expr.Type;
391 // Attempt to do the implicit constant expression conversions
393 if (expr is Constant){
394 if (expr is IntConstant){
397 e = TryWideningIntConversion (target_type, (IntConstant) expr);
401 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
403 // Try the implicit constant expression conversion
404 // from long to ulong, instead of a nice routine,
407 long v = ((LongConstant) expr).Value;
409 return new ULongConstant ((ulong) v);
413 Type real_target_type = target_type;
415 if (expr_type == TypeManager.byte_type){
417 // From byte to short, ushort, int, uint, long, ulong, float, double
419 if ((real_target_type == TypeManager.short_type) ||
420 (real_target_type == TypeManager.int32_type))
421 return new EmptyCast (expr, target_type);
423 if (real_target_type == TypeManager.int64_type)
424 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
425 if (real_target_type == TypeManager.float_type)
426 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
427 if (real_target_type == TypeManager.double_type)
428 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
429 if (real_target_type == TypeManager.decimal_type)
430 return new ImplicitNew (ec, "System", "Decimal", loc, expr);
431 } else if (expr_type == TypeManager.short_type){
433 // From short to int, long, float, double
435 if (real_target_type == TypeManager.int32_type)
436 return new EmptyCast (expr, target_type);
437 if (real_target_type == TypeManager.int64_type)
438 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
439 if (real_target_type == TypeManager.double_type)
440 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
441 if (real_target_type == TypeManager.float_type)
442 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
443 if (real_target_type == TypeManager.decimal_type)
444 return new ImplicitNew (ec, "System", "Decimal", loc, expr);
445 } else if (expr_type == TypeManager.int32_type){
447 // From int to long, float, double
449 if (real_target_type == TypeManager.int64_type)
450 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
451 if (real_target_type == TypeManager.double_type)
452 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
453 if (real_target_type == TypeManager.float_type)
454 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
455 if (real_target_type == TypeManager.decimal_type)
456 return new ImplicitNew (ec, "System", "Decimal", loc, expr);
457 } else if (expr_type == TypeManager.int64_type){
459 // From long/ulong to float, double
461 if (real_target_type == TypeManager.double_type)
462 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
463 if (real_target_type == TypeManager.float_type)
464 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
465 if (real_target_type == TypeManager.decimal_type)
466 return new ImplicitNew (ec, "System", "Decimal", loc, expr);
467 } else if (expr_type == TypeManager.decimal_type){
469 // From decimal to float, double
471 if (real_target_type == TypeManager.double_type)
472 return new ImplicitInvocation (ec, "System", "Convert", "ToDouble", loc, expr);
473 if (real_target_type == TypeManager.float_type)
474 return new ImplicitInvocation (ec, "System", "Convert" ,"ToSingle", loc, expr);
475 } else if (expr_type == TypeManager.float_type){
479 if (real_target_type == TypeManager.double_type)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
488 /// Same as WideningStandardConversionExists except that it also looks at
489 /// implicit user defined conversions - needed for overload resolution
491 public static bool WideningConversionExists (EmitContext ec, Expression expr, Type target_type)
493 if (expr is NullLiteral) {
494 if (target_type.IsGenericParameter)
495 return TypeParameter_to_Null (target_type);
497 if (TypeManager.IsNullableType (target_type))
501 if (WideningStandardConversionExists (ec, expr, target_type))
505 // VB.NET has no notion of User defined conversions
508 // Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
510 // if (dummy != null)
517 // VB.NET has no notion of User defined conversions
520 // public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
522 // Expression dummy = ImplicitUserConversion (
523 // ec, new EmptyExpression (source), target, Location.Null);
524 // return dummy != null;
528 /// Determines if a standard implicit conversion exists from
529 /// expr_type to target_type
531 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
533 public static bool WideningStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
535 Type expr_type = expr.Type;
537 if (expr_type == TypeManager.void_type)
540 //Console.WriteLine ("Expr is {0}", expr);
541 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
542 if (expr_type.Equals (target_type))
546 // First numeric conversions
548 if (expr_type == TypeManager.sbyte_type){
550 // From sbyte to short, int, long, float, double.
552 if ((target_type == TypeManager.int32_type) ||
553 (target_type == TypeManager.int64_type) ||
554 (target_type == TypeManager.double_type) ||
555 (target_type == TypeManager.float_type) ||
556 (target_type == TypeManager.short_type) ||
557 (target_type == TypeManager.decimal_type))
560 } else if (expr_type == TypeManager.byte_type){
562 // From byte to short, ushort, int, uint, long, ulong, float, double
564 if ((target_type == TypeManager.short_type) ||
565 (target_type == TypeManager.ushort_type) ||
566 (target_type == TypeManager.int32_type) ||
567 (target_type == TypeManager.uint32_type) ||
568 (target_type == TypeManager.uint64_type) ||
569 (target_type == TypeManager.int64_type) ||
570 (target_type == TypeManager.float_type) ||
571 (target_type == TypeManager.double_type) ||
572 (target_type == TypeManager.decimal_type))
575 } else if (expr_type == TypeManager.short_type){
577 // From short to int, long, float, double
579 if ((target_type == TypeManager.int32_type) ||
580 (target_type == TypeManager.int64_type) ||
581 (target_type == TypeManager.double_type) ||
582 (target_type == TypeManager.float_type) ||
583 (target_type == TypeManager.decimal_type))
586 } else if (expr_type == TypeManager.ushort_type){
588 // From ushort to int, uint, long, ulong, float, double
590 if ((target_type == TypeManager.uint32_type) ||
591 (target_type == TypeManager.uint64_type) ||
592 (target_type == TypeManager.int32_type) ||
593 (target_type == TypeManager.int64_type) ||
594 (target_type == TypeManager.double_type) ||
595 (target_type == TypeManager.float_type) ||
596 (target_type == TypeManager.decimal_type))
599 } else if (expr_type == TypeManager.int32_type){
601 // From int to long, float, double
603 if ((target_type == TypeManager.int64_type) ||
604 (target_type == TypeManager.double_type) ||
605 (target_type == TypeManager.float_type) ||
606 (target_type == TypeManager.decimal_type))
609 } else if (expr_type == TypeManager.uint32_type){
611 // From uint to long, ulong, float, double
613 if ((target_type == TypeManager.int64_type) ||
614 (target_type == TypeManager.uint64_type) ||
615 (target_type == TypeManager.double_type) ||
616 (target_type == TypeManager.float_type) ||
617 (target_type == TypeManager.decimal_type))
620 } else if ((expr_type == TypeManager.uint64_type) ||
621 (expr_type == TypeManager.int64_type)) {
623 // From long/ulong to float, double
625 if ((target_type == TypeManager.double_type) ||
626 (target_type == TypeManager.float_type) ||
627 (target_type == TypeManager.decimal_type))
630 } else if (expr_type == TypeManager.char_type){
632 // From char to ushort, int, uint, long, ulong, float, double
634 if ((target_type == TypeManager.ushort_type) ||
635 (target_type == TypeManager.int32_type) ||
636 (target_type == TypeManager.uint32_type) ||
637 (target_type == TypeManager.uint64_type) ||
638 (target_type == TypeManager.int64_type) ||
639 (target_type == TypeManager.float_type) ||
640 (target_type == TypeManager.double_type) ||
641 (target_type == TypeManager.decimal_type))
644 } else if (expr_type == TypeManager.float_type){
648 if (target_type == TypeManager.double_type)
652 if (expr.eclass == ExprClass.MethodGroup){
653 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
654 MethodGroupExpr mg = expr as MethodGroupExpr;
657 // This should not happen frequently, so we can create an object
658 // to test compatibility
660 Expression c = ImplicitDelegateCreation.Create (ec, mg, target_type, Location.Null);
666 if (WideningReferenceConversionExists (ec, expr, target_type))
670 // Implicit Constant Expression Conversions
672 if (expr is IntConstant){
673 int value = ((IntConstant) expr).Value;
675 if (target_type == TypeManager.sbyte_type){
676 if (value >= SByte.MinValue && value <= SByte.MaxValue)
678 } else if (target_type == TypeManager.byte_type){
679 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
681 } else if (target_type == TypeManager.short_type){
682 if (value >= Int16.MinValue && value <= Int16.MaxValue)
684 } else if (target_type == TypeManager.ushort_type){
685 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
687 } else if (target_type == TypeManager.uint32_type){
690 } else if (target_type == TypeManager.uint64_type){
692 // we can optimize this case: a positive int32
693 // always fits on a uint64. But we need an opcode
700 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
704 if (expr is LongConstant && target_type == TypeManager.uint64_type){
706 // Try the implicit constant expression conversion
707 // from long to ulong, instead of a nice routine,
710 long v = ((LongConstant) expr).Value;
715 if ((target_type == TypeManager.enum_type ||
716 target_type.IsSubclassOf (TypeManager.enum_type)) &&
718 IntLiteral i = (IntLiteral) expr;
725 // If `expr_type' implements `target_type' (which is an iface)
726 // see TryWideningIntConversion
728 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
731 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
734 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
737 if (expr_type == TypeManager.anonymous_method_type){
738 if (!TypeManager.IsDelegateType (target_type))
741 AnonymousMethod am = (AnonymousMethod) expr;
743 Expression conv = am.Compatible (ec, target_type, true);
752 // Used internally by FindMostEncompassedType, this is used
753 // to avoid creating lots of objects in the tight loop inside
754 // FindMostEncompassedType
756 static EmptyExpression priv_fmet_param;
759 /// Finds "most encompassed type" according to the spec (13.4.2)
760 /// amongst the methods in the MethodGroupExpr
762 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
766 if (priv_fmet_param == null)
767 priv_fmet_param = new EmptyExpression ();
769 foreach (Type t in types){
770 priv_fmet_param.SetType (t);
777 if (WideningStandardConversionExists (ec, priv_fmet_param, best))
785 // Used internally by FindMostEncompassingType, this is used
786 // to avoid creating lots of objects in the tight loop inside
787 // FindMostEncompassingType
789 static EmptyExpression priv_fmee_ret;
792 /// Finds "most encompassing type" according to the spec (13.4.2)
793 /// amongst the types in the given set
795 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
799 if (priv_fmee_ret == null)
800 priv_fmee_ret = new EmptyExpression ();
802 foreach (Type t in types){
803 priv_fmee_ret.SetType (best);
810 if (WideningStandardConversionExists (ec, priv_fmee_ret, t))
818 // Used to avoid creating too many objects
820 static EmptyExpression priv_fms_expr;
823 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
824 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
825 /// for explicit and implicit conversion operators.
827 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
828 Expression source, bool apply_explicit_conv_rules,
831 ArrayList src_types_set = new ArrayList ();
833 if (priv_fms_expr == null)
834 priv_fms_expr = new EmptyExpression ();
837 // If any operator converts from S then Sx = S
839 Type source_type = source.Type;
840 foreach (MethodBase mb in me.Methods){
841 ParameterData pd = Invocation.GetParameterData (mb);
842 Type param_type = pd.ParameterType (0);
844 if (param_type == source_type)
847 if (apply_explicit_conv_rules) {
850 // Find the set of applicable user-defined conversion operators, U. This set
852 // user-defined implicit or explicit conversion operators declared by
853 // the classes or structs in D that convert from a type encompassing
854 // or encompassed by S to a type encompassing or encompassed by T
856 priv_fms_expr.SetType (param_type);
857 if (WideningStandardConversionExists (ec, priv_fms_expr, source_type))
858 src_types_set.Add (param_type);
860 if (WideningStandardConversionExists (ec, source, param_type))
861 src_types_set.Add (param_type);
865 // Only if S is encompassed by param_type
867 if (WideningStandardConversionExists (ec, source, param_type))
868 src_types_set.Add (param_type);
873 // Explicit Conv rules
875 if (apply_explicit_conv_rules) {
876 ArrayList candidate_set = new ArrayList ();
878 foreach (Type param_type in src_types_set){
879 if (WideningStandardConversionExists (ec, source, param_type))
880 candidate_set.Add (param_type);
883 if (candidate_set.Count != 0)
884 return FindMostEncompassedType (ec, candidate_set);
890 if (apply_explicit_conv_rules)
891 return FindMostEncompassingType (ec, src_types_set);
893 return FindMostEncompassedType (ec, src_types_set);
897 // Useful in avoiding proliferation of objects
899 static EmptyExpression priv_fmt_expr;
902 /// Finds the most specific target Tx according to section 13.4.4
904 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
905 Type target, bool apply_explicit_conv_rules,
908 ArrayList tgt_types_set = new ArrayList ();
910 if (priv_fmt_expr == null)
911 priv_fmt_expr = new EmptyExpression ();
914 // If any operator converts to T then Tx = T
916 foreach (MethodInfo mi in me.Methods){
917 Type ret_type = mi.ReturnType;
919 if (ret_type == target)
922 if (apply_explicit_conv_rules) {
925 // Find the set of applicable user-defined conversion operators, U.
927 // This set consists of the
928 // user-defined implicit or explicit conversion operators declared by
929 // the classes or structs in D that convert from a type encompassing
930 // or encompassed by S to a type encompassing or encompassed by T
932 priv_fms_expr.SetType (ret_type);
933 if (WideningStandardConversionExists (ec, priv_fms_expr, target))
934 tgt_types_set.Add (ret_type);
936 priv_fms_expr.SetType (target);
937 if (WideningStandardConversionExists (ec, priv_fms_expr, ret_type))
938 tgt_types_set.Add (ret_type);
942 // Only if T is encompassed by param_type
944 priv_fms_expr.SetType (ret_type);
945 if (WideningStandardConversionExists (ec, priv_fms_expr, target))
946 tgt_types_set.Add (ret_type);
951 // Explicit conv rules
953 if (apply_explicit_conv_rules) {
954 ArrayList candidate_set = new ArrayList ();
956 foreach (Type ret_type in tgt_types_set){
957 priv_fmt_expr.SetType (ret_type);
959 if (WideningStandardConversionExists (ec, priv_fmt_expr, target))
960 candidate_set.Add (ret_type);
963 if (candidate_set.Count != 0)
964 return FindMostEncompassingType (ec, candidate_set);
968 // Okay, final case !
970 if (apply_explicit_conv_rules)
971 return FindMostEncompassedType (ec, tgt_types_set);
973 return FindMostEncompassingType (ec, tgt_types_set);
977 /// User-defined Implicit conversions
981 // VB.NET has no notion of User defined conversions
984 // static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
985 // Type target, Location loc)
987 // return UserDefinedConversion (ec, source, target, loc, false);
991 /// User-defined Explicit conversions
995 // VB.NET has no notion of User defined conversions
998 // static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
999 // Type target, Location loc)
1001 // return UserDefinedConversion (ec, source, target, loc, true);
1004 static DoubleHash explicit_conv = new DoubleHash (100);
1005 static DoubleHash implicit_conv = new DoubleHash (100);
1007 /// Computes the MethodGroup for the user-defined conversion
1008 /// operators from source_type to target_type. `look_for_explicit'
1009 /// controls whether we should also include the list of explicit
1012 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1013 Type source_type, Type target_type,
1014 Location loc, bool look_for_explicit)
1016 Expression mg1 = null, mg2 = null;
1017 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1020 op_name = "op_Implicit";
1022 MethodGroupExpr union3;
1024 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1025 return (MethodGroupExpr) r;
1027 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1028 if (source_type.BaseType != null)
1029 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1032 union3 = (MethodGroupExpr) mg2;
1033 else if (mg2 == null)
1034 union3 = (MethodGroupExpr) mg1;
1036 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1038 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1041 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1043 union3 = (MethodGroupExpr) mg1;
1046 if (target_type.BaseType != null)
1047 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1051 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1053 union3 = (MethodGroupExpr) mg1;
1056 MethodGroupExpr union4 = null;
1058 if (look_for_explicit) {
1059 op_name = "op_Explicit";
1061 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1062 if (source_type.BaseType != null)
1063 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1065 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1066 if (target_type.BaseType != null)
1067 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1069 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1070 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1072 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1075 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1076 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1081 /// User-defined conversions
1085 // VB.NET has no notion of User defined conversions. This method is not used.
1087 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1088 Type target, Location loc,
1089 bool look_for_explicit)
1091 MethodGroupExpr union;
1092 Type source_type = source.Type;
1093 MethodBase method = null;
1095 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1099 Type most_specific_source, most_specific_target;
1101 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1102 if (most_specific_source == null)
1105 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1106 if (most_specific_target == null)
1112 foreach (MethodBase mb in union.Methods){
1113 ParameterData pd = Invocation.GetParameterData (mb);
1114 MethodInfo mi = (MethodInfo) mb;
1116 if (pd.ParameterType (0) == most_specific_source &&
1117 mi.ReturnType == most_specific_target) {
1123 if (method == null || count > 1)
1128 // This will do the conversion to the best match that we
1129 // found. Now we need to perform an implict standard conversion
1130 // if the best match was not the type that we were requested
1133 if (look_for_explicit)
1134 source = WideningAndNarrowingConversionStandard (ec, source, most_specific_source, loc);
1136 source = WideningConversionStandard (ec, source, most_specific_source, loc);
1142 e = new UserCast ((MethodInfo) method, source, loc);
1143 if (e.Type != target){
1144 if (!look_for_explicit)
1145 e = WideningConversionStandard (ec, e, target, loc);
1147 e = WideningAndNarrowingConversionStandard (ec, e, target, loc);
1154 /// Converts implicitly the resolved expression `expr' into the
1155 /// `target_type'. It returns a new expression that can be used
1156 /// in a context that expects a `target_type'.
1158 static public Expression WideningConversion (EmitContext ec, Expression expr,
1159 Type target_type, Location loc)
1163 if (target_type == null)
1164 throw new Exception ("Target type is null");
1166 e = WideningConversionStandard (ec, expr, target_type, loc);
1171 // VB.NET has no notion of User defined conversions
1174 // e = ImplicitUserConversion (ec, expr, target_type, loc);
1183 /// Attempts to apply the `Standard Implicit
1184 /// Conversion' rules to the expression `expr' into
1185 /// the `target_type'. It returns a new expression
1186 /// that can be used in a context that expects a
1189 /// This is different from `WideningConversion' in that the
1190 /// user defined implicit conversions are excluded.
1192 static public Expression WideningConversionStandard (EmitContext ec, Expression expr,
1193 Type target_type, Location loc)
1195 Type expr_type = expr.Type;
1198 if (expr is NullLiteral) {
1199 if (target_type.IsGenericParameter)
1200 return TypeParameter_to_Null (expr, target_type, loc);
1202 if (TypeManager.IsNullableType (target_type))
1203 return new Nullable.NullLiteral (target_type, loc);
1206 if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1207 return Nullable.LiftedConversion.Create (ec, expr, target_type, false, loc);
1209 if (expr.eclass == ExprClass.MethodGroup){
1210 if (!TypeManager.IsDelegateType (target_type)){
1215 // Only allow anonymous method conversions on post ISO_1
1217 if (RootContext.Version != LanguageVersion.ISO_1){
1218 MethodGroupExpr mg = expr as MethodGroupExpr;
1220 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1224 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1227 e = WideningNumericConversion (ec, expr, target_type, loc);
1231 e = WideningReferenceConversion (ec, expr, target_type);
1235 if ((target_type == TypeManager.enum_type ||
1236 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1237 expr is IntLiteral){
1238 IntLiteral i = (IntLiteral) expr;
1241 return new EnumConstant ((Constant) expr, target_type);
1245 if (expr_type.IsPointer){
1246 if (target_type == TypeManager.void_ptr_type)
1247 return new EmptyCast (expr, target_type);
1250 // yep, comparing pointer types cant be done with
1251 // t1 == t2, we have to compare their element types.
1253 if (target_type.IsPointer){
1254 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1259 if (target_type.IsPointer) {
1260 if (expr_type == TypeManager.null_type)
1261 return new EmptyCast (expr, target_type);
1263 if (expr_type == TypeManager.void_ptr_type)
1264 return new EmptyCast (expr, target_type);
1268 if (expr_type == TypeManager.anonymous_method_type){
1269 if (!TypeManager.IsDelegateType (target_type)){
1270 Report.Error (1660, loc,
1271 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1272 TypeManager.CSharpName (target_type));
1276 AnonymousMethod am = (AnonymousMethod) expr;
1277 int errors = Report.Errors;
1279 Expression conv = am.Compatible (ec, target_type, false);
1284 // We return something instead of null, to avoid
1285 // the duplicate error, since am.Compatible would have
1286 // reported that already
1288 if (errors != Report.Errors)
1289 return new EmptyCast (expr, target_type);
1293 // VB.NET specific conversions
1296 e = WideningStringConversions (ec, expr, target_type, loc);
1305 /// Attempts to perform an implicit constant conversion of the IntConstant
1306 /// into a different data type using casts (See Implicit Constant
1307 /// Expression Conversions)
1309 static public Expression TryWideningIntConversion (Type target_type, IntConstant ic)
1311 int value = ic.Value;
1313 if (target_type == TypeManager.sbyte_type){
1314 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1315 return new SByteConstant ((sbyte) value);
1316 } else if (target_type == TypeManager.byte_type){
1317 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1318 return new ByteConstant ((byte) value);
1319 } else if (target_type == TypeManager.short_type){
1320 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1321 return new ShortConstant ((short) value);
1322 } else if (target_type == TypeManager.ushort_type){
1323 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1324 return new UShortConstant ((ushort) value);
1325 } else if (target_type == TypeManager.uint32_type){
1327 return new UIntConstant ((uint) value);
1328 } else if (target_type == TypeManager.uint64_type){
1330 // we can optimize this case: a positive int32
1331 // always fits on a uint64. But we need an opcode
1335 return new ULongConstant ((ulong) value);
1336 } else if (target_type == TypeManager.double_type)
1337 return new DoubleConstant ((double) value);
1338 else if (target_type == TypeManager.float_type)
1339 return new FloatConstant ((float) value);
1341 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1342 Type underlying = TypeManager.EnumToUnderlying (target_type);
1343 Constant e = (Constant) ic;
1346 // Possibly, we need to create a different 0 literal before passing
1349 if (underlying == TypeManager.int64_type)
1350 e = new LongLiteral (0);
1351 else if (underlying == TypeManager.uint64_type)
1352 e = new ULongLiteral (0);
1354 return new EnumConstant (e, target_type);
1358 // If `target_type' is an interface and the type of `ic' implements the interface
1359 // e.g. target_type is IComparable, IConvertible, IFormattable
1361 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1362 return new BoxedCast (ic);
1367 static public void Error_CannotWideningConversion (Location loc, Type source, Type target)
1369 if (source.Name == target.Name){
1370 Report.ExtraInformation (loc,
1372 "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
1373 source.Assembly.FullName, target.Assembly.FullName));
1376 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1377 source == TypeManager.anonymous_method_type ?
1378 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1379 TypeManager.CSharpName (target));
1383 /// Attempts to implicitly convert `source' into `target_type', using
1384 /// WideningConversion. If there is no implicit conversion, then
1385 /// an error is signaled
1387 static public Expression WideningConversionRequired (EmitContext ec, Expression source,
1388 Type target_type, Location loc)
1392 int errors = Report.Errors;
1393 e = WideningConversion (ec, source, target_type, loc);
1394 if (Report.Errors > errors)
1399 if (source is DoubleLiteral) {
1400 if (target_type == TypeManager.float_type) {
1401 Error_664 (loc, "float", "f");
1404 if (target_type == TypeManager.decimal_type) {
1405 Error_664 (loc, "decimal", "m");
1410 if (source is Constant){
1411 Constant c = (Constant) source;
1413 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1417 Error_CannotWideningConversion (loc, source.Type, target_type);
1422 static void Error_664 (Location loc, string type, string suffix) {
1423 Report.Error (664, loc,
1424 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1429 /// Performs the explicit numeric conversions
1433 /// Performs the explicit numeric conversions
1435 static Expression NarrowingNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1437 Type expr_type = expr.Type;
1440 // If we have an enumeration, extract the underlying type,
1441 // use this during the comparison, but wrap around the original
1444 Type real_target_type = target_type;
1446 if (TypeManager.IsEnumType (real_target_type))
1447 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1449 if (WideningStandardConversionExists (ec, expr, real_target_type)){
1450 Expression ce = WideningConversionStandard (ec, expr, real_target_type, loc);
1452 if (real_target_type != target_type)
1453 return new EmptyCast (ce, target_type);
1457 if (expr_type == TypeManager.short_type){
1459 // From short to byte
1461 if (real_target_type == TypeManager.byte_type)
1462 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1463 } else if (expr_type == TypeManager.int32_type){
1465 // From int to byte, short
1467 if (real_target_type == TypeManager.byte_type)
1468 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1469 if (real_target_type == TypeManager.short_type)
1470 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1471 } else if (expr_type == TypeManager.int64_type){
1473 // From long to byte, short, int
1475 if (real_target_type == TypeManager.byte_type)
1476 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1477 if (real_target_type == TypeManager.short_type)
1478 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1479 if (real_target_type == TypeManager.int32_type)
1480 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1481 } else if (expr_type == TypeManager.decimal_type){
1483 // From decimal to byte, short, int, long
1485 if (real_target_type == TypeManager.byte_type)
1486 return new ImplicitInvocation (ec, "System", "Convert" , "ToByte", loc, expr);
1487 if (real_target_type == TypeManager.short_type)
1488 return new ImplicitInvocation (ec, "System", "Convert", "ToInt16", loc, expr);
1489 if (real_target_type == TypeManager.int32_type)
1490 return new ImplicitInvocation (ec, "System", "Convert", "ToInt32", loc, expr);
1491 if (real_target_type == TypeManager.int64_type)
1492 return new ImplicitInvocation (ec, "System", "Convert", "ToInt64", loc, expr);
1493 } else if (expr_type == TypeManager.float_type){
1495 // From float to byte, short, int, long, decimal
1497 if (real_target_type == TypeManager.byte_type)
1498 return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1499 if (real_target_type == TypeManager.short_type)
1500 return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1501 if (real_target_type == TypeManager.int32_type)
1502 return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1503 if (real_target_type == TypeManager.int64_type)
1504 return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1505 if (real_target_type == TypeManager.decimal_type)
1506 return new ImplicitNew (ec, "System", "Decimal", loc, expr);
1507 } else if (expr_type == TypeManager.double_type){
1509 // From double to byte, short, int, long, float, decimal
1511 if (real_target_type == TypeManager.byte_type)
1512 return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1513 if (real_target_type == TypeManager.short_type)
1514 return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1515 if (real_target_type == TypeManager.int32_type)
1516 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1517 if (real_target_type == TypeManager.int64_type)
1518 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1519 if (real_target_type == TypeManager.float_type)
1520 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1521 if (real_target_type == TypeManager.decimal_type)
1522 return new ImplicitNew (ec, "System", "Decimal", loc, expr);
1529 /// VB.NET specific: Convert to and from boolean
1532 static public Expression BooleanConversions (EmitContext ec, Expression expr,
1533 Type target_type, Location loc)
1535 Type expr_type = expr.Type;
1536 Type real_target_type = target_type;
1538 if (expr_type == TypeManager.bool_type) {
1541 // From boolean to byte, short, int,
1542 // long, float, double, decimal
1545 if (real_target_type == TypeManager.byte_type)
1546 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_U1);
1547 if (real_target_type == TypeManager.short_type)
1548 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I2);
1549 if (real_target_type == TypeManager.int32_type)
1550 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I4);
1551 if (real_target_type == TypeManager.int64_type)
1552 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I8);
1553 if (real_target_type == TypeManager.float_type)
1554 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_R4);
1555 if (real_target_type == TypeManager.double_type)
1556 return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_R8);
1557 if (real_target_type == TypeManager.decimal_type) {
1558 return new ImplicitInvocation (ec, "DecimalType", "FromBoolean", loc, expr);
1560 } if (real_target_type == TypeManager.bool_type) {
1563 // From byte, short, int, long, float,
1564 // double, decimal to boolean
1567 if (expr_type == TypeManager.byte_type ||
1568 expr_type == TypeManager.short_type ||
1569 expr_type == TypeManager.int32_type ||
1570 expr_type == TypeManager.int64_type ||
1571 expr_type == TypeManager.float_type ||
1572 expr_type == TypeManager.double_type)
1573 return new NumericToBooleanCast (expr, expr_type);
1574 if (expr_type == TypeManager.decimal_type) {
1575 return new ImplicitInvocation (ec, "System", "Convert", "ToBoolean", loc, expr);
1583 /// VB.NET specific: Widening conversions to string
1586 static public Expression WideningStringConversions (EmitContext ec, Expression expr,
1587 Type target_type, Location loc)
1590 Type expr_type = expr.Type;
1591 Type real_target_type = target_type;
1593 if (real_target_type == TypeManager.string_type) {
1595 // From char to string
1597 if (expr_type == TypeManager.char_type)
1598 return new ImplicitInvocation (ec, "StringType", "FromChar", loc, expr);
1601 if(expr_type.IsArray && (expr_type.GetElementType() == TypeManager.char_type)) {
1603 // From char array to string
1605 return new ImplicitNew (ec, "System", "String", loc, expr);
1612 /// VB.NET specific: Narrowing conversions involving strings
1615 static public Expression NarrowingStringConversions (EmitContext ec, Expression expr,
1616 Type target_type, Location loc)
1618 Type expr_type = expr.Type;
1619 Type real_target_type = target_type;
1621 // FIXME: Need to take care of Constants
1623 if (expr_type == TypeManager.string_type) {
1626 // From string to chararray, bool,
1627 // byte, short, char, int, long,
1628 // float, double, decimal and date
1631 if (real_target_type.IsArray && (real_target_type.GetElementType() == TypeManager.char_type))
1632 return new ImplicitInvocation (ec, "CharArrayType", "FromString", loc, expr);
1633 if (real_target_type == TypeManager.bool_type)
1634 return new ImplicitInvocation (ec, "BooleanType", "FromString", loc, expr);
1635 if (real_target_type == TypeManager.byte_type)
1636 return new ImplicitInvocation (ec, "ByteType", "FromString", loc, expr);
1637 if (real_target_type == TypeManager.short_type)
1638 return new ImplicitInvocation (ec, "ShortType", "FromString", loc, expr);
1639 if (real_target_type == TypeManager.char_type)
1640 return new ImplicitInvocation (ec, "CharType", "FromString", loc, expr);
1641 if (real_target_type == TypeManager.int32_type)
1642 return new ImplicitInvocation (ec, "IntegerType", "FromString", loc, expr);
1643 if (real_target_type == TypeManager.int64_type)
1644 return new ImplicitInvocation (ec, "LongType", "FromString", loc, expr);
1645 if (real_target_type == TypeManager.float_type)
1646 return new ImplicitInvocation (ec, "SingleType", "FromString", loc, expr);
1647 if (real_target_type == TypeManager.double_type)
1648 return new ImplicitInvocation (ec, "DoubleType", "FromString", loc, expr);
1649 if (real_target_type == TypeManager.decimal_type)
1650 return new ImplicitInvocation (ec, "DecimalType", "FromString", loc, expr);
1651 if (real_target_type == TypeManager.date_type)
1652 return new ImplicitInvocation (ec, "DateType", "FromString", loc, expr);
1653 } if (real_target_type == TypeManager.string_type) {
1656 // From bool, byte, short, char, int,
1657 // long, float, double, decimal and
1661 if (expr_type == TypeManager.bool_type)
1662 return new ImplicitInvocation (ec, "StringType", "FromBoolean", loc, expr);
1663 if (expr_type == TypeManager.byte_type)
1664 return new ImplicitInvocation (ec, "StringType", "FromByte", loc, expr);
1665 if (expr_type == TypeManager.short_type)
1666 return new ImplicitInvocation (ec, "StringType", "FromShort", loc, expr);
1667 if (expr_type == TypeManager.int32_type)
1668 return new ImplicitInvocation (ec, "StringType", "FromInteger", loc, expr);
1669 if (expr_type == TypeManager.int64_type)
1670 return new ImplicitInvocation (ec, "StringType", "FromLong", loc, expr);
1671 if (expr_type == TypeManager.float_type)
1672 return new ImplicitInvocation (ec, "StringType", "FromSingle", loc, expr);
1673 if (expr_type == TypeManager.double_type)
1674 return new ImplicitInvocation (ec, "StringType", "FromDouble", loc, expr);
1675 if (expr_type == TypeManager.decimal_type)
1676 return new ImplicitInvocation (ec, "StringType", "FromDecimal", loc, expr);
1677 if (expr_type == TypeManager.date_type)
1678 return new ImplicitInvocation (ec, "StringType", "FromDate", loc, expr);
1685 /// Returns whether an explicit reference conversion can be performed
1686 /// from source_type to target_type
1688 public static bool NarrowingReferenceConversionExists (Type source_type, Type target_type)
1690 bool target_is_type_param = target_type.IsGenericParameter;
1691 bool target_is_value_type = target_type.IsValueType;
1693 if (source_type == target_type)
1697 // From object to a generic parameter
1699 if (source_type == TypeManager.object_type && target_is_type_param)
1703 // From object to any reference type
1705 if (source_type == TypeManager.object_type && !target_is_value_type)
1709 // From any class S to any class-type T, provided S is a base class of T
1711 if (TypeManager.IsSubclassOf (target_type, source_type))
1715 // From any interface type S to any interface T provided S is not derived from T
1717 if (source_type.IsInterface && target_type.IsInterface){
1718 if (!TypeManager.IsSubclassOf (target_type, source_type))
1723 // From any class type S to any interface T, provided S is not sealed
1724 // and provided S does not implement T.
1726 if (target_type.IsInterface && !source_type.IsSealed &&
1727 !TypeManager.ImplementsInterface (source_type, target_type))
1731 // From any interface-type S to to any class type T, provided T is not
1732 // sealed, or provided T implements S.
1734 if (source_type.IsInterface &&
1735 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1739 // From an array type S with an element type Se to an array type T with an
1740 // element type Te provided all the following are true:
1741 // * S and T differe only in element type, in other words, S and T
1742 // have the same number of dimensions.
1743 // * Both Se and Te are reference types
1744 // * An explicit referenc conversions exist from Se to Te
1746 if (source_type.IsArray && target_type.IsArray) {
1747 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1749 Type source_element_type = TypeManager.GetElementType (source_type);
1750 Type target_element_type = TypeManager.GetElementType (target_type);
1752 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1753 if (NarrowingReferenceConversionExists (source_element_type,
1754 target_element_type))
1760 // From System.Array to any array-type
1761 if (source_type == TypeManager.array_type &&
1762 target_type.IsArray){
1767 // From System delegate to any delegate-type
1769 if (source_type == TypeManager.delegate_type &&
1770 TypeManager.IsDelegateType (target_type))
1774 // From ICloneable to Array or Delegate types
1776 if (source_type == TypeManager.icloneable_type &&
1777 (target_type == TypeManager.array_type ||
1778 target_type == TypeManager.delegate_type))
1785 /// Implements Explicit Reference conversions
1787 static Expression NarrowingReferenceConversion (Expression source, Type target_type)
1789 Type source_type = source.Type;
1790 bool target_is_type_param = target_type.IsGenericParameter;
1791 bool target_is_value_type = target_type.IsValueType;
1794 // From object to a generic parameter
1796 if (source_type == TypeManager.object_type && target_is_type_param)
1797 return new UnboxCast (source, target_type);
1800 // From object to any reference type
1802 if (source_type == TypeManager.object_type && !target_is_value_type)
1803 return new ClassCast (source, target_type);
1806 // Unboxing conversion.
1808 if (((source_type == TypeManager.enum_type &&
1809 !(source is EmptyCast)) ||
1810 source_type == TypeManager.value_type) && target_is_value_type)
1811 return new UnboxCast (source, target_type);
1814 // From any class S to any class-type T, provided S is a base class of T
1816 if (TypeManager.IsSubclassOf (target_type, source_type))
1817 return new ClassCast (source, target_type);
1820 // From any interface type S to any interface T provided S is not derived from T
1822 if (source_type.IsInterface && target_type.IsInterface){
1823 if (TypeManager.ImplementsInterface (source_type, target_type))
1826 return new ClassCast (source, target_type);
1830 // From any class type S to any interface T, provides S is not sealed
1831 // and provided S does not implement T.
1833 if (target_type.IsInterface && !source_type.IsSealed) {
1834 if (TypeManager.ImplementsInterface (source_type, target_type))
1837 return new ClassCast (source, target_type);
1842 // From any interface-type S to to any class type T, provided T is not
1843 // sealed, or provided T implements S.
1845 if (source_type.IsInterface) {
1846 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1847 if (target_type.IsClass)
1848 return new ClassCast (source, target_type);
1850 return new UnboxCast (source, target_type);
1856 // From an array type S with an element type Se to an array type T with an
1857 // element type Te provided all the following are true:
1858 // * S and T differe only in element type, in other words, S and T
1859 // have the same number of dimensions.
1860 // * Both Se and Te are reference types
1861 // * An explicit referenc conversions exist from Se to Te
1863 if (source_type.IsArray && target_type.IsArray) {
1864 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1866 Type source_element_type = TypeManager.GetElementType (source_type);
1867 Type target_element_type = TypeManager.GetElementType (target_type);
1869 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1870 if (NarrowingReferenceConversionExists (source_element_type,
1871 target_element_type))
1872 return new ClassCast (source, target_type);
1877 // From System.Array to any array-type
1878 if (source_type == TypeManager.array_type &&
1879 target_type.IsArray) {
1880 return new ClassCast (source, target_type);
1884 // From System delegate to any delegate-type
1886 if (source_type == TypeManager.delegate_type &&
1887 TypeManager.IsDelegateType (target_type))
1888 return new ClassCast (source, target_type);
1891 // From ICloneable to Array or Delegate types
1893 if (source_type == TypeManager.icloneable_type &&
1894 (target_type == TypeManager.array_type ||
1895 target_type == TypeManager.delegate_type))
1896 return new ClassCast (source, target_type);
1902 /// Performs an explicit conversion of the expression `expr' whose
1903 /// type is expr.Type to `target_type'.
1905 static public Expression WideningAndNarrowingConversion (EmitContext ec, Expression expr,
1906 Type target_type, Location loc)
1908 Type expr_type = expr.Type;
1909 Type original_expr_type = expr_type;
1911 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1912 if (target_type == TypeManager.enum_type ||
1913 target_type == TypeManager.object_type) {
1914 if (expr is EnumConstant)
1915 expr = ((EnumConstant) expr).Child;
1916 // We really need all these casts here .... :-(
1917 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1918 return new EmptyCast (expr, target_type);
1919 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1920 target_type.IsSubclassOf (TypeManager.enum_type))
1921 return new UnboxCast (expr, target_type);
1924 // Notice that we have kept the expr_type unmodified, which is only
1926 if (expr is EnumConstant)
1927 expr = ((EnumConstant) expr).Child;
1929 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1930 expr_type = expr.Type;
1933 int errors = Report.Errors;
1934 Expression ne = WideningConversionStandard (ec, expr, target_type, loc);
1935 if (Report.Errors > errors)
1941 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
1942 return Nullable.LiftedConversion.Create (ec, expr, target_type, true, loc);
1944 ne = NarrowingNumericConversion (ec, expr, target_type, loc);
1949 // Unboxing conversion.
1951 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1952 return new UnboxCast (expr, target_type);
1955 // Skip the NarrowingReferenceConversion because we can not convert
1956 // from Null to a ValueType, and ExplicitReference wont check against
1957 // null literal explicitly
1959 if (expr_type != TypeManager.null_type){
1960 ne = NarrowingReferenceConversion (expr, target_type);
1967 if (target_type.IsPointer){
1968 if (expr_type.IsPointer)
1969 return new EmptyCast (expr, target_type);
1971 if (expr_type == TypeManager.sbyte_type ||
1972 expr_type == TypeManager.byte_type ||
1973 expr_type == TypeManager.short_type ||
1974 expr_type == TypeManager.ushort_type ||
1975 expr_type == TypeManager.int32_type ||
1976 expr_type == TypeManager.uint32_type ||
1977 expr_type == TypeManager.uint64_type ||
1978 expr_type == TypeManager.int64_type)
1979 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1981 if (expr_type.IsPointer){
1982 Expression e = null;
1984 if (target_type == TypeManager.sbyte_type)
1985 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1986 else if (target_type == TypeManager.byte_type)
1987 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1988 else if (target_type == TypeManager.short_type)
1989 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1990 else if (target_type == TypeManager.ushort_type)
1991 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1992 else if (target_type == TypeManager.int32_type)
1993 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1994 else if (target_type == TypeManager.uint32_type)
1995 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1996 else if (target_type == TypeManager.uint64_type)
1997 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1998 else if (target_type == TypeManager.int64_type){
1999 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2005 ci = WideningConversionStandard (ec, e, target_type, loc);
2010 ce = NarrowingNumericConversion (ec, e, target_type, loc);
2014 // We should always be able to go from an uint32
2015 // implicitly or explicitly to the other integral
2018 throw new Exception ("Internal compiler error");
2024 // VB.NET specific conversions
2027 ne = BooleanConversions (ec, expr, target_type, loc);
2031 ne = NarrowingStringConversions (ec, expr, target_type, loc);
2037 // VB.NET has no notion of User defined conversions
2040 // ne = ExplicitUserConversion (ec, expr, target_type, loc);
2044 if (expr is NullLiteral){
2045 Report.Error (37, loc, "Cannot convert null to value type `" +
2046 TypeManager.CSharpName (target_type) + "'");
2050 Error_CannotConvertType (loc, original_expr_type, target_type);
2055 /// Same as WideningAndNarrowingConversion, only it doesn't include user defined conversions
2057 static public Expression WideningAndNarrowingConversionStandard (EmitContext ec, Expression expr,
2058 Type target_type, Location l)
2060 int errors = Report.Errors;
2061 Expression ne = WideningConversionStandard (ec, expr, target_type, l);
2062 if (Report.Errors > errors)
2068 if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
2069 return Nullable.LiftedConversion.Create (ec, expr, target_type, true, l);
2071 ne = NarrowingNumericConversion (ec, expr, target_type, l);
2075 ne = NarrowingReferenceConversion (expr, target_type);
2079 Error_CannotConvertType (l, expr.Type, target_type);