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 {
22 static public void Error_CannotConvertType (Location loc, Type source, Type target)
24 Report.Error (30, loc, "Cannot convert type '" +
25 TypeManager.CSharpName (source) + "' to '" +
26 TypeManager.CSharpName (target) + "'");
29 static EmptyExpression MyEmptyExpr;
30 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
32 Type expr_type = expr.Type;
34 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
35 // if we are a method group, emit a warning
40 if (expr_type == TypeManager.void_type)
44 // notice that it is possible to write "ValueType v = 1", the ValueType here
45 // is an abstract class, and not really a value type, so we apply the same rules.
47 if (target_type == TypeManager.object_type) {
49 // A pointer type cannot be converted to object
51 if (expr_type.IsPointer)
54 if (expr_type.IsValueType)
55 return new BoxedCast (expr);
56 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
57 return new EmptyCast (expr, target_type);
58 } else if (target_type == TypeManager.value_type) {
59 if (expr_type.IsValueType)
60 return new BoxedCast (expr);
61 if (expr is NullLiteral)
62 return new NullCast (expr, target_type);
63 } else if (expr_type.IsSubclassOf (target_type)) {
65 // Special case: enumeration to System.Enum.
66 // System.Enum is not a value type, it is a class, so we need
67 // a boxing conversion
70 return new BoxedCast (expr);
72 return new EmptyCast (expr, target_type);
75 // This code is kind of mirrored inside ImplicitStandardConversionExists
76 // with the small distinction that we only probe there
78 // Always ensure that the code here and there is in sync
80 // from the null type to any reference-type.
81 if (expr is NullLiteral){
82 if (target_type.IsPointer)
83 return NullPointer.Null;
85 if (!target_type.IsValueType)
86 return new NullCast (expr, target_type);
89 // from any class-type S to any interface-type T.
90 if (target_type.IsInterface) {
91 if (TypeManager.ImplementsInterface (expr_type, target_type)){
92 if (expr_type.IsClass)
93 return new EmptyCast (expr, target_type);
94 else if (expr_type.IsValueType || expr_type == TypeManager.enum_type)
95 return new BoxedCast (expr, target_type);
97 return new EmptyCast (expr, target_type);
101 // from any interface type S to interface-type T.
102 if (expr_type.IsInterface && target_type.IsInterface) {
103 if (TypeManager.ImplementsInterface (expr_type, target_type))
104 return new EmptyCast (expr, target_type);
109 // from an array-type S to an array-type of type T
110 if (expr_type.IsArray && target_type.IsArray) {
111 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
113 Type expr_element_type = TypeManager.GetElementType (expr_type);
115 if (MyEmptyExpr == null)
116 MyEmptyExpr = new EmptyExpression ();
118 MyEmptyExpr.SetType (expr_element_type);
119 Type target_element_type = TypeManager.GetElementType (target_type);
121 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
122 if (ImplicitStandardConversionExists (MyEmptyExpr,
123 target_element_type))
124 return new EmptyCast (expr, target_type);
129 // from an array-type to System.Array
130 if (expr_type.IsArray && target_type == TypeManager.array_type)
131 return new EmptyCast (expr, target_type);
133 // from any delegate type to System.Delegate
134 if ((expr_type == TypeManager.delegate_type ||
135 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
136 target_type == TypeManager.delegate_type)
137 return new EmptyCast (expr, target_type);
139 // from any array-type or delegate type into System.ICloneable.
140 if (expr_type.IsArray ||
141 expr_type == TypeManager.delegate_type ||
142 expr_type.IsSubclassOf (TypeManager.delegate_type))
143 if (target_type == TypeManager.icloneable_type)
144 return new EmptyCast (expr, target_type);
154 // Tests whether an implicit reference conversion exists between expr_type
157 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
159 Type expr_type = expr.Type;
162 // This is the boxed case.
164 if (target_type == TypeManager.object_type) {
165 if (expr_type.IsClass || expr_type.IsValueType ||
166 expr_type.IsInterface || expr_type == TypeManager.enum_type)
168 } else if (expr_type.IsSubclassOf (target_type))
171 // Please remember that all code below actually comes
172 // from ImplicitReferenceConversion so make sure code remains in sync
174 // from any class-type S to any interface-type T.
175 if (target_type.IsInterface) {
176 if (TypeManager.ImplementsInterface (expr_type, target_type))
180 // from any interface type S to interface-type T.
181 if (expr_type.IsInterface && target_type.IsInterface)
182 if (TypeManager.ImplementsInterface (expr_type, target_type))
185 // from an array-type S to an array-type of type T
186 if (expr_type.IsArray && target_type.IsArray) {
187 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
189 Type expr_element_type = expr_type.GetElementType ();
191 if (MyEmptyExpr == null)
192 MyEmptyExpr = new EmptyExpression ();
194 MyEmptyExpr.SetType (expr_element_type);
195 Type target_element_type = TypeManager.GetElementType (target_type);
197 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
198 if (ImplicitStandardConversionExists (MyEmptyExpr,
199 target_element_type))
204 // from an array-type to System.Array
205 if (expr_type.IsArray && (target_type == TypeManager.array_type))
208 // from any delegate type to System.Delegate
209 if ((expr_type == TypeManager.delegate_type ||
210 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
211 target_type == TypeManager.delegate_type)
212 if (target_type.IsAssignableFrom (expr_type))
215 // from any array-type or delegate type into System.ICloneable.
216 if (expr_type.IsArray ||
217 expr_type == TypeManager.delegate_type ||
218 expr_type.IsSubclassOf (TypeManager.delegate_type))
219 if (target_type == TypeManager.icloneable_type)
222 // from the null type to any reference-type.
223 if (expr is NullLiteral && !target_type.IsValueType &&
224 !TypeManager.IsEnumType (target_type))
232 /// Implicit Numeric Conversions.
234 /// expr is the expression to convert, returns a new expression of type
235 /// target_type or null if an implicit conversion is not possible.
237 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
238 Type target_type, Location loc)
240 Type expr_type = expr.Type;
243 // Attempt to do the implicit constant expression conversions
245 if (expr is Constant){
247 if (expr is IntConstant){
250 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
254 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
256 // Try the implicit constant expression conversion
257 // from long to ulong, instead of a nice routine,
260 long v = ((LongConstant) expr).Value;
262 return new ULongConstant ((ulong) v);
266 Type real_target_type = target_type;
268 if (expr_type == TypeManager.sbyte_type){
270 // From sbyte to short, int, long, float, double.
272 if (real_target_type == TypeManager.int32_type)
273 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
274 if (real_target_type == TypeManager.int64_type)
275 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
276 if (real_target_type == TypeManager.double_type)
277 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
278 if (real_target_type == TypeManager.float_type)
279 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
280 if (real_target_type == TypeManager.short_type)
281 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
282 } else if (expr_type == TypeManager.byte_type){
284 // From byte to short, ushort, int, uint, long, ulong, float, double
286 if ((real_target_type == TypeManager.short_type) ||
287 (real_target_type == TypeManager.ushort_type) ||
288 (real_target_type == TypeManager.int32_type) ||
289 (real_target_type == TypeManager.uint32_type))
290 return new EmptyCast (expr, target_type);
292 if (real_target_type == TypeManager.uint64_type)
293 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
294 if (real_target_type == TypeManager.int64_type)
295 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
296 if (real_target_type == TypeManager.float_type)
297 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
298 if (real_target_type == TypeManager.double_type)
299 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
300 } else if (expr_type == TypeManager.short_type){
302 // From short to int, long, float, double
304 if (real_target_type == TypeManager.int32_type)
305 return new EmptyCast (expr, target_type);
306 if (real_target_type == TypeManager.int64_type)
307 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
308 if (real_target_type == TypeManager.double_type)
309 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
310 if (real_target_type == TypeManager.float_type)
311 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
312 } else if (expr_type == TypeManager.ushort_type){
314 // From ushort to int, uint, long, ulong, float, double
316 if (real_target_type == TypeManager.uint32_type)
317 return new EmptyCast (expr, target_type);
319 if (real_target_type == TypeManager.uint64_type)
320 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
321 if (real_target_type == TypeManager.int32_type)
322 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
323 if (real_target_type == TypeManager.int64_type)
324 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
325 if (real_target_type == TypeManager.double_type)
326 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
327 if (real_target_type == TypeManager.float_type)
328 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
329 } else if (expr_type == TypeManager.int32_type){
331 // From int to long, float, double
333 if (real_target_type == TypeManager.int64_type)
334 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
335 if (real_target_type == TypeManager.double_type)
336 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
337 if (real_target_type == TypeManager.float_type)
338 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
339 } else if (expr_type == TypeManager.uint32_type){
341 // From uint to long, ulong, float, double
343 if (real_target_type == TypeManager.int64_type)
344 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
345 if (real_target_type == TypeManager.uint64_type)
346 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
347 if (real_target_type == TypeManager.double_type)
348 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
350 if (real_target_type == TypeManager.float_type)
351 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
353 } else if (expr_type == TypeManager.int64_type){
355 // From long/ulong to float, double
357 if (real_target_type == TypeManager.double_type)
358 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
359 if (real_target_type == TypeManager.float_type)
360 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
361 } else if (expr_type == TypeManager.uint64_type){
363 // From ulong to float, double
365 if (real_target_type == TypeManager.double_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
368 if (real_target_type == TypeManager.float_type)
369 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
371 } else if (expr_type == TypeManager.char_type){
373 // From char to ushort, int, uint, long, ulong, float, double
375 if ((real_target_type == TypeManager.ushort_type) ||
376 (real_target_type == TypeManager.int32_type) ||
377 (real_target_type == TypeManager.uint32_type))
378 return new EmptyCast (expr, target_type);
379 if (real_target_type == TypeManager.uint64_type)
380 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
381 if (real_target_type == TypeManager.int64_type)
382 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
383 if (real_target_type == TypeManager.float_type)
384 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
385 if (real_target_type == TypeManager.double_type)
386 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
387 } else if (expr_type == TypeManager.float_type){
391 if (real_target_type == TypeManager.double_type)
392 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
400 /// Same as ImplicitStandardConversionExists except that it also looks at
401 /// implicit user defined conversions - needed for overload resolution
403 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
405 if (ImplicitStandardConversionExists (expr, target_type))
408 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
416 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
418 Expression dummy = ImplicitUserConversion (
419 ec, new EmptyExpression (source), target, Location.Null);
420 return dummy != null;
424 /// Determines if a standard implicit conversion exists from
425 /// expr_type to target_type
427 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
429 Type expr_type = expr.Type;
431 if (expr_type == TypeManager.void_type)
434 if (expr_type == target_type)
437 // First numeric conversions
439 if (expr_type == TypeManager.sbyte_type){
441 // From sbyte to short, int, long, float, double.
443 if ((target_type == TypeManager.int32_type) ||
444 (target_type == TypeManager.int64_type) ||
445 (target_type == TypeManager.double_type) ||
446 (target_type == TypeManager.float_type) ||
447 (target_type == TypeManager.short_type) ||
448 (target_type == TypeManager.decimal_type))
451 } else if (expr_type == TypeManager.byte_type){
453 // From byte to short, ushort, int, uint, long, ulong, float, double
455 if ((target_type == TypeManager.short_type) ||
456 (target_type == TypeManager.ushort_type) ||
457 (target_type == TypeManager.int32_type) ||
458 (target_type == TypeManager.uint32_type) ||
459 (target_type == TypeManager.uint64_type) ||
460 (target_type == TypeManager.int64_type) ||
461 (target_type == TypeManager.float_type) ||
462 (target_type == TypeManager.double_type) ||
463 (target_type == TypeManager.decimal_type))
466 } else if (expr_type == TypeManager.short_type){
468 // From short to int, long, float, double
470 if ((target_type == TypeManager.int32_type) ||
471 (target_type == TypeManager.int64_type) ||
472 (target_type == TypeManager.double_type) ||
473 (target_type == TypeManager.float_type) ||
474 (target_type == TypeManager.decimal_type))
477 } else if (expr_type == TypeManager.ushort_type){
479 // From ushort to int, uint, long, ulong, float, double
481 if ((target_type == TypeManager.uint32_type) ||
482 (target_type == TypeManager.uint64_type) ||
483 (target_type == TypeManager.int32_type) ||
484 (target_type == TypeManager.int64_type) ||
485 (target_type == TypeManager.double_type) ||
486 (target_type == TypeManager.float_type) ||
487 (target_type == TypeManager.decimal_type))
490 } else if (expr_type == TypeManager.int32_type){
492 // From int to long, float, double
494 if ((target_type == TypeManager.int64_type) ||
495 (target_type == TypeManager.double_type) ||
496 (target_type == TypeManager.float_type) ||
497 (target_type == TypeManager.decimal_type))
500 } else if (expr_type == TypeManager.uint32_type){
502 // From uint to long, ulong, float, double
504 if ((target_type == TypeManager.int64_type) ||
505 (target_type == TypeManager.uint64_type) ||
506 (target_type == TypeManager.double_type) ||
507 (target_type == TypeManager.float_type) ||
508 (target_type == TypeManager.decimal_type))
511 } else if ((expr_type == TypeManager.uint64_type) ||
512 (expr_type == TypeManager.int64_type)) {
514 // From long/ulong to float, double
516 if ((target_type == TypeManager.double_type) ||
517 (target_type == TypeManager.float_type) ||
518 (target_type == TypeManager.decimal_type))
521 } else if (expr_type == TypeManager.char_type){
523 // From char to ushort, int, uint, long, ulong, float, double
525 if ((target_type == TypeManager.ushort_type) ||
526 (target_type == TypeManager.int32_type) ||
527 (target_type == TypeManager.uint32_type) ||
528 (target_type == TypeManager.uint64_type) ||
529 (target_type == TypeManager.int64_type) ||
530 (target_type == TypeManager.float_type) ||
531 (target_type == TypeManager.double_type) ||
532 (target_type == TypeManager.decimal_type))
535 } else if (expr_type == TypeManager.float_type){
539 if (target_type == TypeManager.double_type)
543 if (ImplicitReferenceConversionExists (expr, target_type))
547 // Implicit Constant Expression Conversions
549 if (expr is IntConstant){
550 int value = ((IntConstant) expr).Value;
552 if (target_type == TypeManager.sbyte_type){
553 if (value >= SByte.MinValue && value <= SByte.MaxValue)
555 } else if (target_type == TypeManager.byte_type){
556 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
558 } else if (target_type == TypeManager.short_type){
559 if (value >= Int16.MinValue && value <= Int16.MaxValue)
561 } else if (target_type == TypeManager.ushort_type){
562 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
564 } else if (target_type == TypeManager.uint32_type){
567 } else if (target_type == TypeManager.uint64_type){
569 // we can optimize this case: a positive int32
570 // always fits on a uint64. But we need an opcode
577 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
581 if (expr is LongConstant && target_type == TypeManager.uint64_type){
583 // Try the implicit constant expression conversion
584 // from long to ulong, instead of a nice routine,
587 long v = ((LongConstant) expr).Value;
592 if ((target_type == TypeManager.enum_type ||
593 target_type.IsSubclassOf (TypeManager.enum_type)) &&
595 IntLiteral i = (IntLiteral) expr;
601 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
608 // Used internally by FindMostEncompassedType, this is used
609 // to avoid creating lots of objects in the tight loop inside
610 // FindMostEncompassedType
612 static EmptyExpression priv_fmet_param;
615 /// Finds "most encompassed type" according to the spec (13.4.2)
616 /// amongst the methods in the MethodGroupExpr
618 static Type FindMostEncompassedType (ArrayList types)
622 if (priv_fmet_param == null)
623 priv_fmet_param = new EmptyExpression ();
625 foreach (Type t in types){
626 priv_fmet_param.SetType (t);
633 if (ImplicitStandardConversionExists (priv_fmet_param, best))
641 // Used internally by FindMostEncompassingType, this is used
642 // to avoid creating lots of objects in the tight loop inside
643 // FindMostEncompassingType
645 static EmptyExpression priv_fmee_ret;
648 /// Finds "most encompassing type" according to the spec (13.4.2)
649 /// amongst the types in the given set
651 static Type FindMostEncompassingType (ArrayList types)
655 if (priv_fmee_ret == null)
656 priv_fmee_ret = new EmptyExpression ();
658 foreach (Type t in types){
659 priv_fmee_ret.SetType (best);
666 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
674 // Used to avoid creating too many objects
676 static EmptyExpression priv_fms_expr;
679 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
680 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
681 /// for explicit and implicit conversion operators.
683 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
684 bool apply_explicit_conv_rules,
687 ArrayList src_types_set = new ArrayList ();
689 if (priv_fms_expr == null)
690 priv_fms_expr = new EmptyExpression ();
693 // If any operator converts from S then Sx = S
695 Type source_type = source.Type;
696 foreach (MethodBase mb in me.Methods){
697 ParameterData pd = Invocation.GetParameterData (mb);
698 Type param_type = pd.ParameterType (0);
700 if (param_type == source_type)
703 if (apply_explicit_conv_rules) {
706 // Find the set of applicable user-defined conversion operators, U. This set
708 // user-defined implicit or explicit conversion operators declared by
709 // the classes or structs in D that convert from a type encompassing
710 // or encompassed by S to a type encompassing or encompassed by T
712 priv_fms_expr.SetType (param_type);
713 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
714 src_types_set.Add (param_type);
716 if (ImplicitStandardConversionExists (source, param_type))
717 src_types_set.Add (param_type);
721 // Only if S is encompassed by param_type
723 if (ImplicitStandardConversionExists (source, param_type))
724 src_types_set.Add (param_type);
729 // Explicit Conv rules
731 if (apply_explicit_conv_rules) {
732 ArrayList candidate_set = new ArrayList ();
734 foreach (Type param_type in src_types_set){
735 if (ImplicitStandardConversionExists (source, param_type))
736 candidate_set.Add (param_type);
739 if (candidate_set.Count != 0)
740 return FindMostEncompassedType (candidate_set);
746 if (apply_explicit_conv_rules)
747 return FindMostEncompassingType (src_types_set);
749 return FindMostEncompassedType (src_types_set);
753 // Useful in avoiding proliferation of objects
755 static EmptyExpression priv_fmt_expr;
758 /// Finds the most specific target Tx according to section 13.4.4
760 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
761 bool apply_explicit_conv_rules,
764 ArrayList tgt_types_set = new ArrayList ();
766 if (priv_fmt_expr == null)
767 priv_fmt_expr = new EmptyExpression ();
770 // If any operator converts to T then Tx = T
772 foreach (MethodInfo mi in me.Methods){
773 Type ret_type = mi.ReturnType;
775 if (ret_type == target)
778 if (apply_explicit_conv_rules) {
781 // Find the set of applicable user-defined conversion operators, U.
783 // This set consists of the
784 // user-defined implicit or explicit conversion operators declared by
785 // the classes or structs in D that convert from a type encompassing
786 // or encompassed by S to a type encompassing or encompassed by T
788 priv_fms_expr.SetType (ret_type);
789 if (ImplicitStandardConversionExists (priv_fms_expr, target))
790 tgt_types_set.Add (ret_type);
792 priv_fms_expr.SetType (target);
793 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
794 tgt_types_set.Add (ret_type);
798 // Only if T is encompassed by param_type
800 priv_fms_expr.SetType (ret_type);
801 if (ImplicitStandardConversionExists (priv_fms_expr, target))
802 tgt_types_set.Add (ret_type);
807 // Explicit conv rules
809 if (apply_explicit_conv_rules) {
810 ArrayList candidate_set = new ArrayList ();
812 foreach (Type ret_type in tgt_types_set){
813 priv_fmt_expr.SetType (ret_type);
815 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
816 candidate_set.Add (ret_type);
819 if (candidate_set.Count != 0)
820 return FindMostEncompassingType (candidate_set);
824 // Okay, final case !
826 if (apply_explicit_conv_rules)
827 return FindMostEncompassedType (tgt_types_set);
829 return FindMostEncompassingType (tgt_types_set);
833 /// User-defined Implicit conversions
835 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
836 Type target, Location loc)
838 return UserDefinedConversion (ec, source, target, loc, false);
842 /// User-defined Explicit conversions
844 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
845 Type target, Location loc)
847 return UserDefinedConversion (ec, source, target, loc, true);
851 /// Computes the MethodGroup for the user-defined conversion
852 /// operators from source_type to target_type. `look_for_explicit'
853 /// controls whether we should also include the list of explicit
856 static MethodGroupExpr GetConversionOperators (EmitContext ec,
857 Type source_type, Type target_type,
858 Location loc, bool look_for_explicit)
860 Expression mg1 = null, mg2 = null;
861 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
864 op_name = "op_Implicit";
866 MethodGroupExpr union3;
868 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
869 if (source_type.BaseType != null)
870 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
873 union3 = (MethodGroupExpr) mg2;
874 else if (mg2 == null)
875 union3 = (MethodGroupExpr) mg1;
877 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
879 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
882 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
884 union3 = (MethodGroupExpr) mg1;
887 if (target_type.BaseType != null)
888 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
892 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
894 union3 = (MethodGroupExpr) mg1;
897 MethodGroupExpr union4 = null;
899 if (look_for_explicit) {
900 op_name = "op_Explicit";
902 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
903 if (source_type.BaseType != null)
904 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
906 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
907 if (target_type.BaseType != null)
908 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
910 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
911 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
913 union4 = Invocation.MakeUnionSet (union5, union6, loc);
916 return Invocation.MakeUnionSet (union3, union4, loc);
920 /// User-defined conversions
922 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
923 Type target, Location loc,
924 bool look_for_explicit)
926 MethodGroupExpr union;
927 Type source_type = source.Type;
928 MethodBase method = null;
930 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
934 Type most_specific_source, most_specific_target;
936 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
937 if (most_specific_source == null)
940 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
941 if (most_specific_target == null)
947 foreach (MethodBase mb in union.Methods){
948 ParameterData pd = Invocation.GetParameterData (mb);
949 MethodInfo mi = (MethodInfo) mb;
951 if (pd.ParameterType (0) == most_specific_source &&
952 mi.ReturnType == most_specific_target) {
958 if (method == null || count > 1)
963 // This will do the conversion to the best match that we
964 // found. Now we need to perform an implict standard conversion
965 // if the best match was not the type that we were requested
968 if (look_for_explicit)
969 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
971 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
977 e = new UserCast ((MethodInfo) method, source, loc);
978 if (e.Type != target){
979 if (!look_for_explicit)
980 e = ImplicitConversionStandard (ec, e, target, loc);
982 e = ExplicitConversionStandard (ec, e, target, loc);
989 /// Converts implicitly the resolved expression `expr' into the
990 /// `target_type'. It returns a new expression that can be used
991 /// in a context that expects a `target_type'.
993 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
994 Type target_type, Location loc)
998 if (target_type == null)
999 throw new Exception ("Target type is null");
1001 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1005 e = ImplicitUserConversion (ec, expr, target_type, loc);
1014 /// Attempts to apply the `Standard Implicit
1015 /// Conversion' rules to the expression `expr' into
1016 /// the `target_type'. It returns a new expression
1017 /// that can be used in a context that expects a
1020 /// This is different from `ImplicitConversion' in that the
1021 /// user defined implicit conversions are excluded.
1023 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1024 Type target_type, Location loc)
1026 Type expr_type = expr.Type;
1029 if (expr.eclass == ExprClass.MethodGroup){
1030 if (!TypeManager.IsDelegateType (target_type)){
1031 Report.Error (428, loc,
1033 "Cannot convert method group to `{0}', since it is not a delegate",
1034 TypeManager.CSharpName (target_type)));
1038 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1041 if (expr_type == target_type && !(expr is NullLiteral))
1044 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1048 e = ImplicitReferenceConversion (expr, target_type);
1052 if ((target_type == TypeManager.enum_type ||
1053 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1054 expr is IntLiteral){
1055 IntLiteral i = (IntLiteral) expr;
1058 return new EnumConstant ((Constant) expr, target_type);
1062 if (expr_type.IsPointer){
1063 if (target_type == TypeManager.void_ptr_type)
1064 return new EmptyCast (expr, target_type);
1067 // yep, comparing pointer types cant be done with
1068 // t1 == t2, we have to compare their element types.
1070 if (target_type.IsPointer){
1071 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1076 if (target_type.IsPointer) {
1077 if (expr is NullLiteral)
1078 return new EmptyCast (expr, target_type);
1080 if (expr_type == TypeManager.void_ptr_type)
1081 return new EmptyCast (expr, target_type);
1089 /// Attemps to perform an implict constant conversion of the IntConstant
1090 /// into a different data type using casts (See Implicit Constant
1091 /// Expression Conversions)
1093 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1095 int value = ic.Value;
1097 if (target_type == TypeManager.sbyte_type){
1098 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1099 return new SByteConstant ((sbyte) value);
1100 } else if (target_type == TypeManager.byte_type){
1101 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1102 return new ByteConstant ((byte) value);
1103 } else if (target_type == TypeManager.short_type){
1104 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1105 return new ShortConstant ((short) value);
1106 } else if (target_type == TypeManager.ushort_type){
1107 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1108 return new UShortConstant ((ushort) value);
1109 } else if (target_type == TypeManager.uint32_type){
1111 return new UIntConstant ((uint) value);
1112 } else if (target_type == TypeManager.uint64_type){
1114 // we can optimize this case: a positive int32
1115 // always fits on a uint64. But we need an opcode
1119 return new ULongConstant ((ulong) value);
1120 } else if (target_type == TypeManager.double_type)
1121 return new DoubleConstant ((double) value);
1122 else if (target_type == TypeManager.float_type)
1123 return new FloatConstant ((float) value);
1125 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1126 Type underlying = TypeManager.EnumToUnderlying (target_type);
1127 Constant e = (Constant) ic;
1130 // Possibly, we need to create a different 0 literal before passing
1133 if (underlying == TypeManager.int64_type)
1134 e = new LongLiteral (0);
1135 else if (underlying == TypeManager.uint64_type)
1136 e = new ULongLiteral (0);
1138 return new EnumConstant (e, target_type);
1143 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1145 string msg = "Cannot convert implicitly from `"+
1146 TypeManager.CSharpName (source) + "' to `" +
1147 TypeManager.CSharpName (target) + "'";
1149 Report.Error (29, loc, msg);
1153 /// Attemptes to implicityly convert `target' into `type', using
1154 /// ImplicitConversion. If there is no implicit conversion, then
1155 /// an error is signaled
1157 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1158 Type target_type, Location loc)
1162 e = ImplicitConversion (ec, source, target_type, loc);
1166 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1167 Report.Error (664, loc,
1168 "Double literal cannot be implicitly converted to " +
1169 "float type, use F suffix to create a float literal");
1172 if (source is Constant){
1173 Constant c = (Constant) source;
1175 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1179 Error_CannotImplicitConversion (loc, source.Type, target_type);
1185 /// Performs the explicit numeric conversions
1187 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1189 Type expr_type = expr.Type;
1192 // If we have an enumeration, extract the underlying type,
1193 // use this during the comparison, but wrap around the original
1196 Type real_target_type = target_type;
1198 if (TypeManager.IsEnumType (real_target_type))
1199 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1201 if (ImplicitStandardConversionExists (expr, real_target_type)){
1202 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1204 if (real_target_type != target_type)
1205 return new EmptyCast (ce, target_type);
1209 if (expr_type == TypeManager.sbyte_type){
1211 // From sbyte to byte, ushort, uint, ulong, char
1213 if (real_target_type == TypeManager.byte_type)
1214 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1215 if (real_target_type == TypeManager.ushort_type)
1216 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1217 if (real_target_type == TypeManager.uint32_type)
1218 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1219 if (real_target_type == TypeManager.uint64_type)
1220 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1221 if (real_target_type == TypeManager.char_type)
1222 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1223 } else if (expr_type == TypeManager.byte_type){
1225 // From byte to sbyte and char
1227 if (real_target_type == TypeManager.sbyte_type)
1228 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1229 if (real_target_type == TypeManager.char_type)
1230 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1231 } else if (expr_type == TypeManager.short_type){
1233 // From short to sbyte, byte, ushort, uint, ulong, char
1235 if (real_target_type == TypeManager.sbyte_type)
1236 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1237 if (real_target_type == TypeManager.byte_type)
1238 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1239 if (real_target_type == TypeManager.ushort_type)
1240 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1241 if (real_target_type == TypeManager.uint32_type)
1242 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1243 if (real_target_type == TypeManager.uint64_type)
1244 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1245 if (real_target_type == TypeManager.char_type)
1246 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1247 } else if (expr_type == TypeManager.ushort_type){
1249 // From ushort to sbyte, byte, short, char
1251 if (real_target_type == TypeManager.sbyte_type)
1252 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1253 if (real_target_type == TypeManager.byte_type)
1254 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1255 if (real_target_type == TypeManager.short_type)
1256 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1257 if (real_target_type == TypeManager.char_type)
1258 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1259 } else if (expr_type == TypeManager.int32_type){
1261 // From int to sbyte, byte, short, ushort, uint, ulong, char
1263 if (real_target_type == TypeManager.sbyte_type)
1264 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1265 if (real_target_type == TypeManager.byte_type)
1266 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1267 if (real_target_type == TypeManager.short_type)
1268 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1269 if (real_target_type == TypeManager.ushort_type)
1270 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1271 if (real_target_type == TypeManager.uint32_type)
1272 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1273 if (real_target_type == TypeManager.uint64_type)
1274 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1275 if (real_target_type == TypeManager.char_type)
1276 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1277 } else if (expr_type == TypeManager.uint32_type){
1279 // From uint to sbyte, byte, short, ushort, int, char
1281 if (real_target_type == TypeManager.sbyte_type)
1282 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1283 if (real_target_type == TypeManager.byte_type)
1284 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1285 if (real_target_type == TypeManager.short_type)
1286 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1287 if (real_target_type == TypeManager.ushort_type)
1288 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1289 if (real_target_type == TypeManager.int32_type)
1290 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1291 if (real_target_type == TypeManager.char_type)
1292 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1293 } else if (expr_type == TypeManager.int64_type){
1295 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1297 if (real_target_type == TypeManager.sbyte_type)
1298 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1299 if (real_target_type == TypeManager.byte_type)
1300 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1301 if (real_target_type == TypeManager.short_type)
1302 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1303 if (real_target_type == TypeManager.ushort_type)
1304 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1305 if (real_target_type == TypeManager.int32_type)
1306 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1307 if (real_target_type == TypeManager.uint32_type)
1308 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1309 if (real_target_type == TypeManager.uint64_type)
1310 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1311 if (real_target_type == TypeManager.char_type)
1312 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1313 } else if (expr_type == TypeManager.uint64_type){
1315 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1317 if (real_target_type == TypeManager.sbyte_type)
1318 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1319 if (real_target_type == TypeManager.byte_type)
1320 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1321 if (real_target_type == TypeManager.short_type)
1322 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1323 if (real_target_type == TypeManager.ushort_type)
1324 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1325 if (real_target_type == TypeManager.int32_type)
1326 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1327 if (real_target_type == TypeManager.uint32_type)
1328 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1329 if (real_target_type == TypeManager.int64_type)
1330 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1331 if (real_target_type == TypeManager.char_type)
1332 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1333 } else if (expr_type == TypeManager.char_type){
1335 // From char to sbyte, byte, short
1337 if (real_target_type == TypeManager.sbyte_type)
1338 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1339 if (real_target_type == TypeManager.byte_type)
1340 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1341 if (real_target_type == TypeManager.short_type)
1342 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1343 } else if (expr_type == TypeManager.float_type){
1345 // From float to sbyte, byte, short,
1346 // ushort, int, uint, long, ulong, char
1349 if (real_target_type == TypeManager.sbyte_type)
1350 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1351 if (real_target_type == TypeManager.byte_type)
1352 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1353 if (real_target_type == TypeManager.short_type)
1354 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1355 if (real_target_type == TypeManager.ushort_type)
1356 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1357 if (real_target_type == TypeManager.int32_type)
1358 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1359 if (real_target_type == TypeManager.uint32_type)
1360 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1361 if (real_target_type == TypeManager.int64_type)
1362 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1363 if (real_target_type == TypeManager.uint64_type)
1364 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1365 if (real_target_type == TypeManager.char_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1367 } else if (expr_type == TypeManager.double_type){
1369 // From double to byte, byte, short,
1370 // ushort, int, uint, long, ulong,
1371 // char, float or decimal
1373 if (real_target_type == TypeManager.sbyte_type)
1374 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1375 if (real_target_type == TypeManager.byte_type)
1376 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1377 if (real_target_type == TypeManager.short_type)
1378 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1379 if (real_target_type == TypeManager.ushort_type)
1380 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1381 if (real_target_type == TypeManager.int32_type)
1382 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1383 if (real_target_type == TypeManager.uint32_type)
1384 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1385 if (real_target_type == TypeManager.int64_type)
1386 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1387 if (real_target_type == TypeManager.uint64_type)
1388 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1389 if (real_target_type == TypeManager.char_type)
1390 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1391 if (real_target_type == TypeManager.float_type)
1392 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1395 // decimal is taken care of by the op_Explicit methods.
1401 /// Returns whether an explicit reference conversion can be performed
1402 /// from source_type to target_type
1404 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1406 bool target_is_value_type = target_type.IsValueType;
1408 if (source_type == target_type)
1412 // From object to any reference type
1414 if (source_type == TypeManager.object_type && !target_is_value_type)
1418 // From any class S to any class-type T, provided S is a base class of T
1420 if (target_type.IsSubclassOf (source_type))
1424 // From any interface type S to any interface T provided S is not derived from T
1426 if (source_type.IsInterface && target_type.IsInterface){
1427 if (!target_type.IsSubclassOf (source_type))
1432 // From any class type S to any interface T, provided S is not sealed
1433 // and provided S does not implement T.
1435 if (target_type.IsInterface && !source_type.IsSealed &&
1436 !TypeManager.ImplementsInterface (source_type, target_type))
1440 // From any interface-type S to to any class type T, provided T is not
1441 // sealed, or provided T implements S.
1443 if (source_type.IsInterface &&
1444 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1448 // From an array type S with an element type Se to an array type T with an
1449 // element type Te provided all the following are true:
1450 // * S and T differe only in element type, in other words, S and T
1451 // have the same number of dimensions.
1452 // * Both Se and Te are reference types
1453 // * An explicit referenc conversions exist from Se to Te
1455 if (source_type.IsArray && target_type.IsArray) {
1456 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1458 Type source_element_type = TypeManager.GetElementType (source_type);
1459 Type target_element_type = TypeManager.GetElementType (target_type);
1461 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1462 if (ExplicitReferenceConversionExists (source_element_type,
1463 target_element_type))
1469 // From System.Array to any array-type
1470 if (source_type == TypeManager.array_type &&
1471 target_type.IsArray){
1476 // From System delegate to any delegate-type
1478 if (source_type == TypeManager.delegate_type &&
1479 target_type.IsSubclassOf (TypeManager.delegate_type))
1483 // From ICloneable to Array or Delegate types
1485 if (source_type == TypeManager.icloneable_type &&
1486 (target_type == TypeManager.array_type ||
1487 target_type == TypeManager.delegate_type))
1494 /// Implements Explicit Reference conversions
1496 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1498 Type source_type = source.Type;
1499 bool target_is_value_type = target_type.IsValueType;
1502 // From object to any reference type
1504 if (source_type == TypeManager.object_type && !target_is_value_type)
1505 return new ClassCast (source, target_type);
1508 // Unboxing conversion.
1510 if (((source_type == TypeManager.enum_type &&
1511 !(source is EmptyCast)) ||
1512 source_type == TypeManager.value_type) && target_is_value_type)
1513 return new UnboxCast (source, target_type);
1516 // From any class S to any class-type T, provided S is a base class of T
1518 if (target_type.IsSubclassOf (source_type))
1519 return new ClassCast (source, target_type);
1522 // From any interface type S to any interface T provided S is not derived from T
1524 if (source_type.IsInterface && target_type.IsInterface){
1525 if (TypeManager.ImplementsInterface (source_type, target_type))
1528 return new ClassCast (source, target_type);
1532 // From any class type S to any interface T, provides S is not sealed
1533 // and provided S does not implement T.
1535 if (target_type.IsInterface && !source_type.IsSealed) {
1536 if (TypeManager.ImplementsInterface (source_type, target_type))
1539 return new ClassCast (source, target_type);
1544 // From any interface-type S to to any class type T, provided T is not
1545 // sealed, or provided T implements S.
1547 if (source_type.IsInterface) {
1548 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1549 if (target_type.IsClass)
1550 return new ClassCast (source, target_type);
1552 return new UnboxCast (source, target_type);
1558 // From an array type S with an element type Se to an array type T with an
1559 // element type Te provided all the following are true:
1560 // * S and T differe only in element type, in other words, S and T
1561 // have the same number of dimensions.
1562 // * Both Se and Te are reference types
1563 // * An explicit referenc conversions exist from Se to Te
1565 if (source_type.IsArray && target_type.IsArray) {
1566 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1568 Type source_element_type = TypeManager.GetElementType (source_type);
1569 Type target_element_type = TypeManager.GetElementType (target_type);
1571 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1572 if (ExplicitReferenceConversionExists (source_element_type,
1573 target_element_type))
1574 return new ClassCast (source, target_type);
1579 // From System.Array to any array-type
1580 if (source_type == TypeManager.array_type &&
1581 target_type.IsArray) {
1582 return new ClassCast (source, target_type);
1586 // From System delegate to any delegate-type
1588 if (source_type == TypeManager.delegate_type &&
1589 target_type.IsSubclassOf (TypeManager.delegate_type))
1590 return new ClassCast (source, target_type);
1593 // From ICloneable to Array or Delegate types
1595 if (source_type == TypeManager.icloneable_type &&
1596 (target_type == TypeManager.array_type ||
1597 target_type == TypeManager.delegate_type))
1598 return new ClassCast (source, target_type);
1604 /// Performs an explicit conversion of the expression `expr' whose
1605 /// type is expr.Type to `target_type'.
1607 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1608 Type target_type, Location loc)
1610 Type expr_type = expr.Type;
1611 Type original_expr_type = expr_type;
1613 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1614 if (target_type == TypeManager.enum_type ||
1615 target_type == TypeManager.object_type) {
1616 if (expr is EnumConstant)
1617 expr = ((EnumConstant) expr).Child;
1618 // We really need all these casts here .... :-(
1619 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1620 return new EmptyCast (expr, target_type);
1621 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1622 target_type.IsSubclassOf (TypeManager.enum_type))
1623 return new UnboxCast (expr, target_type);
1626 // Notice that we have kept the expr_type unmodified, which is only
1628 if (expr is EnumConstant)
1629 expr = ((EnumConstant) expr).Child;
1631 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1632 expr_type = expr.Type;
1635 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1640 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1645 // Unboxing conversion.
1647 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1648 if (expr is NullLiteral){
1649 Report.Error (37, loc, "Cannot convert null to value type `" +
1650 TypeManager.CSharpName (target_type) + "'");
1653 return new UnboxCast (expr, target_type);
1656 ne = ExplicitReferenceConversion (expr, target_type);
1661 if (target_type.IsPointer){
1662 if (expr_type.IsPointer)
1663 return new EmptyCast (expr, target_type);
1665 if (expr_type == TypeManager.sbyte_type ||
1666 expr_type == TypeManager.byte_type ||
1667 expr_type == TypeManager.short_type ||
1668 expr_type == TypeManager.ushort_type ||
1669 expr_type == TypeManager.int32_type ||
1670 expr_type == TypeManager.uint32_type ||
1671 expr_type == TypeManager.uint64_type ||
1672 expr_type == TypeManager.int64_type)
1673 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1675 if (expr_type.IsPointer){
1676 Expression e = null;
1678 if (target_type == TypeManager.sbyte_type)
1679 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1680 else if (target_type == TypeManager.byte_type)
1681 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1682 else if (target_type == TypeManager.short_type)
1683 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1684 else if (target_type == TypeManager.ushort_type)
1685 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1686 else if (target_type == TypeManager.int32_type)
1687 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1688 else if (target_type == TypeManager.uint32_type)
1689 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1690 else if (target_type == TypeManager.uint64_type)
1691 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1692 else if (target_type == TypeManager.int64_type){
1693 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1699 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1704 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1708 // We should always be able to go from an uint32
1709 // implicitly or explicitly to the other integral
1712 throw new Exception ("Internal compiler error");
1717 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1721 Error_CannotConvertType (loc, original_expr_type, target_type);
1726 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1728 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1729 Type target_type, Location l)
1731 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1736 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1740 ne = ExplicitReferenceConversion (expr, target_type);
1744 Error_CannotConvertType (l, expr.Type, target_type);