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 (TypeManager.IsValueType (expr_type))
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 (TypeManager.IsValueType (expr_type))
60 return new BoxedCast (expr);
61 if (expr is NullLiteral)
62 return new NullCast (expr, target_type);
63 } else if (TypeManager.IsSubclassOf (expr_type, 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 (TypeManager.IsValueType (expr_type) ||
95 TypeManager.IsEnumType (expr_type))
96 return new BoxedCast (expr, target_type);
98 return new EmptyCast (expr, target_type);
102 // from any interface type S to interface-type T.
103 if (expr_type.IsInterface && target_type.IsInterface) {
104 if (TypeManager.ImplementsInterface (expr_type, target_type))
105 return new EmptyCast (expr, target_type);
110 // from an array-type S to an array-type of type T
111 if (expr_type.IsArray && target_type.IsArray) {
112 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
114 Type expr_element_type = TypeManager.GetElementType (expr_type);
116 if (MyEmptyExpr == null)
117 MyEmptyExpr = new EmptyExpression ();
119 MyEmptyExpr.SetType (expr_element_type);
120 Type target_element_type = TypeManager.GetElementType (target_type);
122 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
123 if (ImplicitStandardConversionExists (MyEmptyExpr,
124 target_element_type))
125 return new EmptyCast (expr, target_type);
130 // from an array-type to System.Array
131 if (expr_type.IsArray && target_type == TypeManager.array_type)
132 return new EmptyCast (expr, target_type);
134 // from any delegate type to System.Delegate
135 if ((expr_type == TypeManager.delegate_type ||
136 TypeManager.IsDelegateType (expr_type)) &&
137 target_type == TypeManager.delegate_type)
138 return new EmptyCast (expr, target_type);
140 // from any array-type or delegate type into System.ICloneable.
141 if (expr_type.IsArray ||
142 expr_type == TypeManager.delegate_type ||
143 TypeManager.IsDelegateType (expr_type))
144 if (target_type == TypeManager.icloneable_type)
145 return new EmptyCast (expr, target_type);
147 // from a generic type definition to a generic instance.
148 if (TypeManager.IsEqualGenericType (expr_type, target_type))
149 return new EmptyCast (expr, target_type);
159 // Tests whether an implicit reference conversion exists between expr_type
162 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
164 Type expr_type = expr.Type;
167 // This is the boxed case.
169 if (target_type == TypeManager.object_type) {
170 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
171 expr_type.IsInterface || expr_type == TypeManager.enum_type)
173 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
176 // Please remember that all code below actually comes
177 // from ImplicitReferenceConversion so make sure code remains in sync
179 // from any class-type S to any interface-type T.
180 if (target_type.IsInterface) {
181 if (TypeManager.ImplementsInterface (expr_type, target_type))
185 // from any interface type S to interface-type T.
186 if (expr_type.IsInterface && target_type.IsInterface)
187 if (TypeManager.ImplementsInterface (expr_type, target_type))
190 // from an array-type S to an array-type of type T
191 if (expr_type.IsArray && target_type.IsArray) {
192 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
194 Type expr_element_type = expr_type.GetElementType ();
196 if (MyEmptyExpr == null)
197 MyEmptyExpr = new EmptyExpression ();
199 MyEmptyExpr.SetType (expr_element_type);
200 Type target_element_type = TypeManager.GetElementType (target_type);
202 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
203 if (ImplicitStandardConversionExists (MyEmptyExpr,
204 target_element_type))
209 // from an array-type to System.Array
210 if (expr_type.IsArray && (target_type == TypeManager.array_type))
213 // from any delegate type to System.Delegate
214 if ((expr_type == TypeManager.delegate_type ||
215 TypeManager.IsDelegateType (expr_type)) &&
216 target_type == TypeManager.delegate_type)
217 if (target_type.IsAssignableFrom (expr_type))
220 // from any array-type or delegate type into System.ICloneable.
221 if (expr_type.IsArray ||
222 expr_type == TypeManager.delegate_type ||
223 TypeManager.IsDelegateType (expr_type))
224 if (target_type == TypeManager.icloneable_type)
227 // from the null type to any reference-type.
228 if (expr is NullLiteral && !target_type.IsValueType &&
229 !TypeManager.IsEnumType (target_type))
232 // from a generic type definition to a generic instance.
233 if (TypeManager.IsEqualGenericType (expr_type, target_type))
240 /// Implicit Numeric Conversions.
242 /// expr is the expression to convert, returns a new expression of type
243 /// target_type or null if an implicit conversion is not possible.
245 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
246 Type target_type, Location loc)
248 Type expr_type = expr.Type;
251 // Attempt to do the implicit constant expression conversions
253 if (expr is Constant){
255 if (expr is IntConstant){
258 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
262 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
264 // Try the implicit constant expression conversion
265 // from long to ulong, instead of a nice routine,
268 long v = ((LongConstant) expr).Value;
270 return new ULongConstant ((ulong) v);
274 Type real_target_type = target_type;
276 if (expr_type == TypeManager.sbyte_type){
278 // From sbyte to short, int, long, float, double.
280 if (real_target_type == TypeManager.int32_type)
281 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
282 if (real_target_type == TypeManager.int64_type)
283 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
284 if (real_target_type == TypeManager.double_type)
285 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
286 if (real_target_type == TypeManager.float_type)
287 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
288 if (real_target_type == TypeManager.short_type)
289 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
290 } else if (expr_type == TypeManager.byte_type){
292 // From byte to short, ushort, int, uint, long, ulong, float, double
294 if ((real_target_type == TypeManager.short_type) ||
295 (real_target_type == TypeManager.ushort_type) ||
296 (real_target_type == TypeManager.int32_type) ||
297 (real_target_type == TypeManager.uint32_type))
298 return new EmptyCast (expr, target_type);
300 if (real_target_type == TypeManager.uint64_type)
301 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
302 if (real_target_type == TypeManager.int64_type)
303 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
304 if (real_target_type == TypeManager.float_type)
305 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
306 if (real_target_type == TypeManager.double_type)
307 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
308 } else if (expr_type == TypeManager.short_type){
310 // From short to int, long, float, double
312 if (real_target_type == TypeManager.int32_type)
313 return new EmptyCast (expr, target_type);
314 if (real_target_type == TypeManager.int64_type)
315 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
316 if (real_target_type == TypeManager.double_type)
317 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
318 if (real_target_type == TypeManager.float_type)
319 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
320 } else if (expr_type == TypeManager.ushort_type){
322 // From ushort to int, uint, long, ulong, float, double
324 if (real_target_type == TypeManager.uint32_type)
325 return new EmptyCast (expr, target_type);
327 if (real_target_type == TypeManager.uint64_type)
328 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
329 if (real_target_type == TypeManager.int32_type)
330 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
331 if (real_target_type == TypeManager.int64_type)
332 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
333 if (real_target_type == TypeManager.double_type)
334 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
335 if (real_target_type == TypeManager.float_type)
336 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
337 } else if (expr_type == TypeManager.int32_type){
339 // From int to long, float, double
341 if (real_target_type == TypeManager.int64_type)
342 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
343 if (real_target_type == TypeManager.double_type)
344 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
345 if (real_target_type == TypeManager.float_type)
346 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
347 } else if (expr_type == TypeManager.uint32_type){
349 // From uint to long, ulong, float, double
351 if (real_target_type == TypeManager.int64_type)
352 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
353 if (real_target_type == TypeManager.uint64_type)
354 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
355 if (real_target_type == TypeManager.double_type)
356 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
358 if (real_target_type == TypeManager.float_type)
359 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
361 } else if (expr_type == TypeManager.int64_type){
363 // From long/ulong to float, double
365 if (real_target_type == TypeManager.double_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
367 if (real_target_type == TypeManager.float_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
369 } else if (expr_type == TypeManager.uint64_type){
371 // From ulong to float, double
373 if (real_target_type == TypeManager.double_type)
374 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
376 if (real_target_type == TypeManager.float_type)
377 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
379 } else if (expr_type == TypeManager.char_type){
381 // From char to ushort, int, uint, long, ulong, float, double
383 if ((real_target_type == TypeManager.ushort_type) ||
384 (real_target_type == TypeManager.int32_type) ||
385 (real_target_type == TypeManager.uint32_type))
386 return new EmptyCast (expr, target_type);
387 if (real_target_type == TypeManager.uint64_type)
388 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
389 if (real_target_type == TypeManager.int64_type)
390 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
391 if (real_target_type == TypeManager.float_type)
392 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
393 if (real_target_type == TypeManager.double_type)
394 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
395 } else if (expr_type == TypeManager.float_type){
399 if (real_target_type == TypeManager.double_type)
400 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
408 /// Same as ImplicitStandardConversionExists except that it also looks at
409 /// implicit user defined conversions - needed for overload resolution
411 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
413 if (ImplicitStandardConversionExists (expr, target_type))
416 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
424 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
426 Expression dummy = ImplicitUserConversion (
427 ec, new EmptyExpression (source), target, Location.Null);
428 return dummy != null;
432 /// Determines if a standard implicit conversion exists from
433 /// expr_type to target_type
435 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
437 Type expr_type = expr.Type;
439 if (expr_type == TypeManager.void_type)
442 //Console.WriteLine ("Expr is {0}", expr);
443 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
444 if (expr_type.Equals (target_type))
447 // First numeric conversions
449 if (expr_type == TypeManager.sbyte_type){
451 // From sbyte to short, int, long, float, double.
453 if ((target_type == TypeManager.int32_type) ||
454 (target_type == TypeManager.int64_type) ||
455 (target_type == TypeManager.double_type) ||
456 (target_type == TypeManager.float_type) ||
457 (target_type == TypeManager.short_type) ||
458 (target_type == TypeManager.decimal_type))
461 } else if (expr_type == TypeManager.byte_type){
463 // From byte to short, ushort, int, uint, long, ulong, float, double
465 if ((target_type == TypeManager.short_type) ||
466 (target_type == TypeManager.ushort_type) ||
467 (target_type == TypeManager.int32_type) ||
468 (target_type == TypeManager.uint32_type) ||
469 (target_type == TypeManager.uint64_type) ||
470 (target_type == TypeManager.int64_type) ||
471 (target_type == TypeManager.float_type) ||
472 (target_type == TypeManager.double_type) ||
473 (target_type == TypeManager.decimal_type))
476 } else if (expr_type == TypeManager.short_type){
478 // From short to int, long, float, double
480 if ((target_type == TypeManager.int32_type) ||
481 (target_type == TypeManager.int64_type) ||
482 (target_type == TypeManager.double_type) ||
483 (target_type == TypeManager.float_type) ||
484 (target_type == TypeManager.decimal_type))
487 } else if (expr_type == TypeManager.ushort_type){
489 // From ushort to int, uint, long, ulong, float, double
491 if ((target_type == TypeManager.uint32_type) ||
492 (target_type == TypeManager.uint64_type) ||
493 (target_type == TypeManager.int32_type) ||
494 (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.int32_type){
502 // From int to long, float, double
504 if ((target_type == TypeManager.int64_type) ||
505 (target_type == TypeManager.double_type) ||
506 (target_type == TypeManager.float_type) ||
507 (target_type == TypeManager.decimal_type))
510 } else if (expr_type == TypeManager.uint32_type){
512 // From uint to long, ulong, float, double
514 if ((target_type == TypeManager.int64_type) ||
515 (target_type == TypeManager.uint64_type) ||
516 (target_type == TypeManager.double_type) ||
517 (target_type == TypeManager.float_type) ||
518 (target_type == TypeManager.decimal_type))
521 } else if ((expr_type == TypeManager.uint64_type) ||
522 (expr_type == TypeManager.int64_type)) {
524 // From long/ulong to float, double
526 if ((target_type == TypeManager.double_type) ||
527 (target_type == TypeManager.float_type) ||
528 (target_type == TypeManager.decimal_type))
531 } else if (expr_type == TypeManager.char_type){
533 // From char to ushort, int, uint, long, ulong, float, double
535 if ((target_type == TypeManager.ushort_type) ||
536 (target_type == TypeManager.int32_type) ||
537 (target_type == TypeManager.uint32_type) ||
538 (target_type == TypeManager.uint64_type) ||
539 (target_type == TypeManager.int64_type) ||
540 (target_type == TypeManager.float_type) ||
541 (target_type == TypeManager.double_type) ||
542 (target_type == TypeManager.decimal_type))
545 } else if (expr_type == TypeManager.float_type){
549 if (target_type == TypeManager.double_type)
553 if (ImplicitReferenceConversionExists (expr, target_type))
557 // Implicit Constant Expression Conversions
559 if (expr is IntConstant){
560 int value = ((IntConstant) expr).Value;
562 if (target_type == TypeManager.sbyte_type){
563 if (value >= SByte.MinValue && value <= SByte.MaxValue)
565 } else if (target_type == TypeManager.byte_type){
566 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
568 } else if (target_type == TypeManager.short_type){
569 if (value >= Int16.MinValue && value <= Int16.MaxValue)
571 } else if (target_type == TypeManager.ushort_type){
572 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
574 } else if (target_type == TypeManager.uint32_type){
577 } else if (target_type == TypeManager.uint64_type){
579 // we can optimize this case: a positive int32
580 // always fits on a uint64. But we need an opcode
587 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
591 if (expr is LongConstant && target_type == TypeManager.uint64_type){
593 // Try the implicit constant expression conversion
594 // from long to ulong, instead of a nice routine,
597 long v = ((LongConstant) expr).Value;
602 if ((target_type == TypeManager.enum_type ||
603 target_type.IsSubclassOf (TypeManager.enum_type)) &&
605 IntLiteral i = (IntLiteral) expr;
611 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
618 // Used internally by FindMostEncompassedType, this is used
619 // to avoid creating lots of objects in the tight loop inside
620 // FindMostEncompassedType
622 static EmptyExpression priv_fmet_param;
625 /// Finds "most encompassed type" according to the spec (13.4.2)
626 /// amongst the methods in the MethodGroupExpr
628 static Type FindMostEncompassedType (ArrayList types)
632 if (priv_fmet_param == null)
633 priv_fmet_param = new EmptyExpression ();
635 foreach (Type t in types){
636 priv_fmet_param.SetType (t);
643 if (ImplicitStandardConversionExists (priv_fmet_param, best))
651 // Used internally by FindMostEncompassingType, this is used
652 // to avoid creating lots of objects in the tight loop inside
653 // FindMostEncompassingType
655 static EmptyExpression priv_fmee_ret;
658 /// Finds "most encompassing type" according to the spec (13.4.2)
659 /// amongst the types in the given set
661 static Type FindMostEncompassingType (ArrayList types)
665 if (priv_fmee_ret == null)
666 priv_fmee_ret = new EmptyExpression ();
668 foreach (Type t in types){
669 priv_fmee_ret.SetType (best);
676 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
684 // Used to avoid creating too many objects
686 static EmptyExpression priv_fms_expr;
689 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
690 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
691 /// for explicit and implicit conversion operators.
693 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
694 bool apply_explicit_conv_rules,
697 ArrayList src_types_set = new ArrayList ();
699 if (priv_fms_expr == null)
700 priv_fms_expr = new EmptyExpression ();
703 // If any operator converts from S then Sx = S
705 Type source_type = source.Type;
706 foreach (MethodBase mb in me.Methods){
707 ParameterData pd = Invocation.GetParameterData (mb);
708 Type param_type = pd.ParameterType (0);
710 if (param_type == source_type)
713 if (apply_explicit_conv_rules) {
716 // Find the set of applicable user-defined conversion operators, U. This set
718 // user-defined implicit or explicit conversion operators declared by
719 // the classes or structs in D that convert from a type encompassing
720 // or encompassed by S to a type encompassing or encompassed by T
722 priv_fms_expr.SetType (param_type);
723 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
724 src_types_set.Add (param_type);
726 if (ImplicitStandardConversionExists (source, param_type))
727 src_types_set.Add (param_type);
731 // Only if S is encompassed by param_type
733 if (ImplicitStandardConversionExists (source, param_type))
734 src_types_set.Add (param_type);
739 // Explicit Conv rules
741 if (apply_explicit_conv_rules) {
742 ArrayList candidate_set = new ArrayList ();
744 foreach (Type param_type in src_types_set){
745 if (ImplicitStandardConversionExists (source, param_type))
746 candidate_set.Add (param_type);
749 if (candidate_set.Count != 0)
750 return FindMostEncompassedType (candidate_set);
756 if (apply_explicit_conv_rules)
757 return FindMostEncompassingType (src_types_set);
759 return FindMostEncompassedType (src_types_set);
763 // Useful in avoiding proliferation of objects
765 static EmptyExpression priv_fmt_expr;
768 /// Finds the most specific target Tx according to section 13.4.4
770 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
771 bool apply_explicit_conv_rules,
774 ArrayList tgt_types_set = new ArrayList ();
776 if (priv_fmt_expr == null)
777 priv_fmt_expr = new EmptyExpression ();
780 // If any operator converts to T then Tx = T
782 foreach (MethodInfo mi in me.Methods){
783 Type ret_type = mi.ReturnType;
785 if (ret_type == target)
788 if (apply_explicit_conv_rules) {
791 // Find the set of applicable user-defined conversion operators, U.
793 // This set consists of the
794 // user-defined implicit or explicit conversion operators declared by
795 // the classes or structs in D that convert from a type encompassing
796 // or encompassed by S to a type encompassing or encompassed by T
798 priv_fms_expr.SetType (ret_type);
799 if (ImplicitStandardConversionExists (priv_fms_expr, target))
800 tgt_types_set.Add (ret_type);
802 priv_fms_expr.SetType (target);
803 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
804 tgt_types_set.Add (ret_type);
808 // Only if T is encompassed by param_type
810 priv_fms_expr.SetType (ret_type);
811 if (ImplicitStandardConversionExists (priv_fms_expr, target))
812 tgt_types_set.Add (ret_type);
817 // Explicit conv rules
819 if (apply_explicit_conv_rules) {
820 ArrayList candidate_set = new ArrayList ();
822 foreach (Type ret_type in tgt_types_set){
823 priv_fmt_expr.SetType (ret_type);
825 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
826 candidate_set.Add (ret_type);
829 if (candidate_set.Count != 0)
830 return FindMostEncompassingType (candidate_set);
834 // Okay, final case !
836 if (apply_explicit_conv_rules)
837 return FindMostEncompassedType (tgt_types_set);
839 return FindMostEncompassingType (tgt_types_set);
843 /// User-defined Implicit conversions
845 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
846 Type target, Location loc)
848 return UserDefinedConversion (ec, source, target, loc, false);
852 /// User-defined Explicit conversions
854 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
855 Type target, Location loc)
857 return UserDefinedConversion (ec, source, target, loc, true);
861 /// Computes the MethodGroup for the user-defined conversion
862 /// operators from source_type to target_type. `look_for_explicit'
863 /// controls whether we should also include the list of explicit
866 static MethodGroupExpr GetConversionOperators (EmitContext ec,
867 Type source_type, Type target_type,
868 Location loc, bool look_for_explicit)
870 Expression mg1 = null, mg2 = null;
871 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
874 op_name = "op_Implicit";
876 MethodGroupExpr union3;
878 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
879 if (source_type.BaseType != null)
880 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
883 union3 = (MethodGroupExpr) mg2;
884 else if (mg2 == null)
885 union3 = (MethodGroupExpr) mg1;
887 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
889 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
892 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
894 union3 = (MethodGroupExpr) mg1;
897 if (target_type.BaseType != null)
898 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
902 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
904 union3 = (MethodGroupExpr) mg1;
907 MethodGroupExpr union4 = null;
909 if (look_for_explicit) {
910 op_name = "op_Explicit";
912 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
913 if (source_type.BaseType != null)
914 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
916 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
917 if (target_type.BaseType != null)
918 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
920 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
921 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
923 union4 = Invocation.MakeUnionSet (union5, union6, loc);
926 return Invocation.MakeUnionSet (union3, union4, loc);
930 /// User-defined conversions
932 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
933 Type target, Location loc,
934 bool look_for_explicit)
936 MethodGroupExpr union;
937 Type source_type = source.Type;
938 MethodBase method = null;
940 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
944 Type most_specific_source, most_specific_target;
946 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
947 if (most_specific_source == null)
950 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
951 if (most_specific_target == null)
957 foreach (MethodBase mb in union.Methods){
958 ParameterData pd = Invocation.GetParameterData (mb);
959 MethodInfo mi = (MethodInfo) mb;
961 if (pd.ParameterType (0) == most_specific_source &&
962 mi.ReturnType == most_specific_target) {
968 if (method == null || count > 1)
973 // This will do the conversion to the best match that we
974 // found. Now we need to perform an implict standard conversion
975 // if the best match was not the type that we were requested
978 if (look_for_explicit)
979 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
981 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
987 e = new UserCast ((MethodInfo) method, source, loc);
988 if (e.Type != target){
989 if (!look_for_explicit)
990 e = ImplicitConversionStandard (ec, e, target, loc);
992 e = ExplicitConversionStandard (ec, e, target, loc);
999 /// Converts implicitly the resolved expression `expr' into the
1000 /// `target_type'. It returns a new expression that can be used
1001 /// in a context that expects a `target_type'.
1003 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1004 Type target_type, Location loc)
1008 if (target_type == null)
1009 throw new Exception ("Target type is null");
1011 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1015 e = ImplicitUserConversion (ec, expr, target_type, loc);
1024 /// Attempts to apply the `Standard Implicit
1025 /// Conversion' rules to the expression `expr' into
1026 /// the `target_type'. It returns a new expression
1027 /// that can be used in a context that expects a
1030 /// This is different from `ImplicitConversion' in that the
1031 /// user defined implicit conversions are excluded.
1033 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1034 Type target_type, Location loc)
1036 Type expr_type = expr.Type;
1039 if (expr.eclass == ExprClass.MethodGroup){
1040 if (!TypeManager.IsDelegateType (target_type)){
1041 Report.Error (428, loc,
1043 "Cannot convert method group to `{0}', since it is not a delegate",
1044 TypeManager.CSharpName (target_type)));
1048 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1051 if (expr_type.Equals (target_type) && !(expr is NullLiteral))
1054 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1058 e = ImplicitReferenceConversion (expr, target_type);
1062 if ((target_type == TypeManager.enum_type ||
1063 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1064 expr is IntLiteral){
1065 IntLiteral i = (IntLiteral) expr;
1068 return new EnumConstant ((Constant) expr, target_type);
1072 if (expr_type.IsPointer){
1073 if (target_type == TypeManager.void_ptr_type)
1074 return new EmptyCast (expr, target_type);
1077 // yep, comparing pointer types cant be done with
1078 // t1 == t2, we have to compare their element types.
1080 if (target_type.IsPointer){
1081 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1086 if (target_type.IsPointer) {
1087 if (expr is NullLiteral)
1088 return new EmptyCast (expr, target_type);
1090 if (expr_type == TypeManager.void_ptr_type)
1091 return new EmptyCast (expr, target_type);
1099 /// Attemps to perform an implict constant conversion of the IntConstant
1100 /// into a different data type using casts (See Implicit Constant
1101 /// Expression Conversions)
1103 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1105 int value = ic.Value;
1107 if (target_type == TypeManager.sbyte_type){
1108 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1109 return new SByteConstant ((sbyte) value);
1110 } else if (target_type == TypeManager.byte_type){
1111 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1112 return new ByteConstant ((byte) value);
1113 } else if (target_type == TypeManager.short_type){
1114 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1115 return new ShortConstant ((short) value);
1116 } else if (target_type == TypeManager.ushort_type){
1117 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1118 return new UShortConstant ((ushort) value);
1119 } else if (target_type == TypeManager.uint32_type){
1121 return new UIntConstant ((uint) value);
1122 } else if (target_type == TypeManager.uint64_type){
1124 // we can optimize this case: a positive int32
1125 // always fits on a uint64. But we need an opcode
1129 return new ULongConstant ((ulong) value);
1130 } else if (target_type == TypeManager.double_type)
1131 return new DoubleConstant ((double) value);
1132 else if (target_type == TypeManager.float_type)
1133 return new FloatConstant ((float) value);
1135 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1136 Type underlying = TypeManager.EnumToUnderlying (target_type);
1137 Constant e = (Constant) ic;
1140 // Possibly, we need to create a different 0 literal before passing
1143 if (underlying == TypeManager.int64_type)
1144 e = new LongLiteral (0);
1145 else if (underlying == TypeManager.uint64_type)
1146 e = new ULongLiteral (0);
1148 return new EnumConstant (e, target_type);
1153 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1155 string msg = "Cannot convert implicitly from `"+
1156 TypeManager.CSharpName (source) + "' to `" +
1157 TypeManager.CSharpName (target) + "'";
1159 Report.Error (29, loc, msg);
1163 /// Attemptes to implicityly convert `target' into `type', using
1164 /// ImplicitConversion. If there is no implicit conversion, then
1165 /// an error is signaled
1167 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1168 Type target_type, Location loc)
1172 e = ImplicitConversion (ec, source, target_type, loc);
1176 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1177 Report.Error (664, loc,
1178 "Double literal cannot be implicitly converted to " +
1179 "float type, use F suffix to create a float literal");
1182 if (source is Constant){
1183 Constant c = (Constant) source;
1185 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1189 Error_CannotImplicitConversion (loc, source.Type, target_type);
1195 /// Performs the explicit numeric conversions
1197 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1199 Type expr_type = expr.Type;
1202 // If we have an enumeration, extract the underlying type,
1203 // use this during the comparison, but wrap around the original
1206 Type real_target_type = target_type;
1208 if (TypeManager.IsEnumType (real_target_type))
1209 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1211 if (ImplicitStandardConversionExists (expr, real_target_type)){
1212 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1214 if (real_target_type != target_type)
1215 return new EmptyCast (ce, target_type);
1219 if (expr_type == TypeManager.sbyte_type){
1221 // From sbyte to byte, ushort, uint, ulong, char
1223 if (real_target_type == TypeManager.byte_type)
1224 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1225 if (real_target_type == TypeManager.ushort_type)
1226 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1227 if (real_target_type == TypeManager.uint32_type)
1228 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1229 if (real_target_type == TypeManager.uint64_type)
1230 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1231 if (real_target_type == TypeManager.char_type)
1232 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1233 } else if (expr_type == TypeManager.byte_type){
1235 // From byte to sbyte and char
1237 if (real_target_type == TypeManager.sbyte_type)
1238 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1239 if (real_target_type == TypeManager.char_type)
1240 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1241 } else if (expr_type == TypeManager.short_type){
1243 // From short to sbyte, byte, ushort, uint, ulong, char
1245 if (real_target_type == TypeManager.sbyte_type)
1246 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1247 if (real_target_type == TypeManager.byte_type)
1248 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1249 if (real_target_type == TypeManager.ushort_type)
1250 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1251 if (real_target_type == TypeManager.uint32_type)
1252 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1253 if (real_target_type == TypeManager.uint64_type)
1254 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1255 if (real_target_type == TypeManager.char_type)
1256 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1257 } else if (expr_type == TypeManager.ushort_type){
1259 // From ushort to sbyte, byte, short, char
1261 if (real_target_type == TypeManager.sbyte_type)
1262 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1263 if (real_target_type == TypeManager.byte_type)
1264 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1265 if (real_target_type == TypeManager.short_type)
1266 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1267 if (real_target_type == TypeManager.char_type)
1268 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1269 } else if (expr_type == TypeManager.int32_type){
1271 // From int to sbyte, byte, short, ushort, uint, ulong, char
1273 if (real_target_type == TypeManager.sbyte_type)
1274 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1275 if (real_target_type == TypeManager.byte_type)
1276 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1277 if (real_target_type == TypeManager.short_type)
1278 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1279 if (real_target_type == TypeManager.ushort_type)
1280 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1281 if (real_target_type == TypeManager.uint32_type)
1282 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1283 if (real_target_type == TypeManager.uint64_type)
1284 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1285 if (real_target_type == TypeManager.char_type)
1286 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1287 } else if (expr_type == TypeManager.uint32_type){
1289 // From uint to sbyte, byte, short, ushort, int, char
1291 if (real_target_type == TypeManager.sbyte_type)
1292 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1293 if (real_target_type == TypeManager.byte_type)
1294 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1295 if (real_target_type == TypeManager.short_type)
1296 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1297 if (real_target_type == TypeManager.ushort_type)
1298 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1299 if (real_target_type == TypeManager.int32_type)
1300 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1301 if (real_target_type == TypeManager.char_type)
1302 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1303 } else if (expr_type == TypeManager.int64_type){
1305 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1307 if (real_target_type == TypeManager.sbyte_type)
1308 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1309 if (real_target_type == TypeManager.byte_type)
1310 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1311 if (real_target_type == TypeManager.short_type)
1312 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1313 if (real_target_type == TypeManager.ushort_type)
1314 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1315 if (real_target_type == TypeManager.int32_type)
1316 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1317 if (real_target_type == TypeManager.uint32_type)
1318 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1319 if (real_target_type == TypeManager.uint64_type)
1320 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1321 if (real_target_type == TypeManager.char_type)
1322 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1323 } else if (expr_type == TypeManager.uint64_type){
1325 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1327 if (real_target_type == TypeManager.sbyte_type)
1328 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1329 if (real_target_type == TypeManager.byte_type)
1330 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1331 if (real_target_type == TypeManager.short_type)
1332 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1333 if (real_target_type == TypeManager.ushort_type)
1334 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1335 if (real_target_type == TypeManager.int32_type)
1336 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1337 if (real_target_type == TypeManager.uint32_type)
1338 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1339 if (real_target_type == TypeManager.int64_type)
1340 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1341 if (real_target_type == TypeManager.char_type)
1342 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1343 } else if (expr_type == TypeManager.char_type){
1345 // From char to sbyte, byte, short
1347 if (real_target_type == TypeManager.sbyte_type)
1348 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1349 if (real_target_type == TypeManager.byte_type)
1350 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1351 if (real_target_type == TypeManager.short_type)
1352 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1353 } else if (expr_type == TypeManager.float_type){
1355 // From float to sbyte, byte, short,
1356 // ushort, int, uint, long, ulong, char
1359 if (real_target_type == TypeManager.sbyte_type)
1360 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1361 if (real_target_type == TypeManager.byte_type)
1362 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1363 if (real_target_type == TypeManager.short_type)
1364 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1365 if (real_target_type == TypeManager.ushort_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1367 if (real_target_type == TypeManager.int32_type)
1368 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1369 if (real_target_type == TypeManager.uint32_type)
1370 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1371 if (real_target_type == TypeManager.int64_type)
1372 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1373 if (real_target_type == TypeManager.uint64_type)
1374 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1375 if (real_target_type == TypeManager.char_type)
1376 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1377 } else if (expr_type == TypeManager.double_type){
1379 // From double to byte, byte, short,
1380 // ushort, int, uint, long, ulong,
1381 // char, float or decimal
1383 if (real_target_type == TypeManager.sbyte_type)
1384 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1385 if (real_target_type == TypeManager.byte_type)
1386 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1387 if (real_target_type == TypeManager.short_type)
1388 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1389 if (real_target_type == TypeManager.ushort_type)
1390 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1391 if (real_target_type == TypeManager.int32_type)
1392 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1393 if (real_target_type == TypeManager.uint32_type)
1394 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1395 if (real_target_type == TypeManager.int64_type)
1396 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1397 if (real_target_type == TypeManager.uint64_type)
1398 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1399 if (real_target_type == TypeManager.char_type)
1400 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1401 if (real_target_type == TypeManager.float_type)
1402 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1405 // decimal is taken care of by the op_Explicit methods.
1411 /// Returns whether an explicit reference conversion can be performed
1412 /// from source_type to target_type
1414 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1416 bool target_is_value_type = target_type.IsValueType;
1418 if (source_type == target_type)
1422 // From object to any reference type
1424 if (source_type == TypeManager.object_type && !target_is_value_type)
1428 // From any class S to any class-type T, provided S is a base class of T
1430 if (TypeManager.IsSubclassOf (target_type, source_type))
1434 // From any interface type S to any interface T provided S is not derived from T
1436 if (source_type.IsInterface && target_type.IsInterface){
1437 if (!TypeManager.IsSubclassOf (target_type, source_type))
1442 // From any class type S to any interface T, provided S is not sealed
1443 // and provided S does not implement T.
1445 if (target_type.IsInterface && !source_type.IsSealed &&
1446 !TypeManager.ImplementsInterface (source_type, target_type))
1450 // From any interface-type S to to any class type T, provided T is not
1451 // sealed, or provided T implements S.
1453 if (source_type.IsInterface &&
1454 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1458 // From an array type S with an element type Se to an array type T with an
1459 // element type Te provided all the following are true:
1460 // * S and T differe only in element type, in other words, S and T
1461 // have the same number of dimensions.
1462 // * Both Se and Te are reference types
1463 // * An explicit referenc conversions exist from Se to Te
1465 if (source_type.IsArray && target_type.IsArray) {
1466 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1468 Type source_element_type = TypeManager.GetElementType (source_type);
1469 Type target_element_type = TypeManager.GetElementType (target_type);
1471 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1472 if (ExplicitReferenceConversionExists (source_element_type,
1473 target_element_type))
1479 // From System.Array to any array-type
1480 if (source_type == TypeManager.array_type &&
1481 target_type.IsArray){
1486 // From System delegate to any delegate-type
1488 if (source_type == TypeManager.delegate_type &&
1489 TypeManager.IsDelegateType (target_type))
1493 // From ICloneable to Array or Delegate types
1495 if (source_type == TypeManager.icloneable_type &&
1496 (target_type == TypeManager.array_type ||
1497 target_type == TypeManager.delegate_type))
1504 /// Implements Explicit Reference conversions
1506 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1508 Type source_type = source.Type;
1509 bool target_is_value_type = target_type.IsValueType;
1512 // From object to any reference type
1514 if (source_type == TypeManager.object_type && !target_is_value_type)
1515 return new ClassCast (source, target_type);
1518 // Unboxing conversion.
1520 if (((source_type == TypeManager.enum_type &&
1521 !(source is EmptyCast)) ||
1522 source_type == TypeManager.value_type) && target_is_value_type)
1523 return new UnboxCast (source, target_type);
1526 // From any class S to any class-type T, provided S is a base class of T
1528 if (TypeManager.IsSubclassOf (target_type, source_type))
1529 return new ClassCast (source, target_type);
1532 // From any interface type S to any interface T provided S is not derived from T
1534 if (source_type.IsInterface && target_type.IsInterface){
1535 if (TypeManager.ImplementsInterface (source_type, target_type))
1538 return new ClassCast (source, target_type);
1542 // From any class type S to any interface T, provides S is not sealed
1543 // and provided S does not implement T.
1545 if (target_type.IsInterface && !source_type.IsSealed) {
1546 if (TypeManager.ImplementsInterface (source_type, target_type))
1549 return new ClassCast (source, target_type);
1554 // From any interface-type S to to any class type T, provided T is not
1555 // sealed, or provided T implements S.
1557 if (source_type.IsInterface) {
1558 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1559 if (target_type.IsClass)
1560 return new ClassCast (source, target_type);
1562 return new UnboxCast (source, target_type);
1568 // From an array type S with an element type Se to an array type T with an
1569 // element type Te provided all the following are true:
1570 // * S and T differe only in element type, in other words, S and T
1571 // have the same number of dimensions.
1572 // * Both Se and Te are reference types
1573 // * An explicit referenc conversions exist from Se to Te
1575 if (source_type.IsArray && target_type.IsArray) {
1576 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1578 Type source_element_type = TypeManager.GetElementType (source_type);
1579 Type target_element_type = TypeManager.GetElementType (target_type);
1581 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1582 if (ExplicitReferenceConversionExists (source_element_type,
1583 target_element_type))
1584 return new ClassCast (source, target_type);
1589 // From System.Array to any array-type
1590 if (source_type == TypeManager.array_type &&
1591 target_type.IsArray) {
1592 return new ClassCast (source, target_type);
1596 // From System delegate to any delegate-type
1598 if (source_type == TypeManager.delegate_type &&
1599 TypeManager.IsDelegateType (target_type))
1600 return new ClassCast (source, target_type);
1603 // From ICloneable to Array or Delegate types
1605 if (source_type == TypeManager.icloneable_type &&
1606 (target_type == TypeManager.array_type ||
1607 target_type == TypeManager.delegate_type))
1608 return new ClassCast (source, target_type);
1614 /// Performs an explicit conversion of the expression `expr' whose
1615 /// type is expr.Type to `target_type'.
1617 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1618 Type target_type, Location loc)
1620 Type expr_type = expr.Type;
1621 Type original_expr_type = expr_type;
1623 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1624 if (target_type == TypeManager.enum_type ||
1625 target_type == TypeManager.object_type) {
1626 if (expr is EnumConstant)
1627 expr = ((EnumConstant) expr).Child;
1628 // We really need all these casts here .... :-(
1629 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1630 return new EmptyCast (expr, target_type);
1631 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1632 target_type.IsSubclassOf (TypeManager.enum_type))
1633 return new UnboxCast (expr, target_type);
1636 // Notice that we have kept the expr_type unmodified, which is only
1638 if (expr is EnumConstant)
1639 expr = ((EnumConstant) expr).Child;
1641 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1642 expr_type = expr.Type;
1645 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1650 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1655 // Unboxing conversion.
1657 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1658 if (expr is NullLiteral){
1660 // Skip the ExplicitReferenceConversion because we can not convert
1661 // from Null to a ValueType, and ExplicitReference wont check against
1662 // null literal explicitly
1666 return new UnboxCast (expr, target_type);
1670 ne = ExplicitReferenceConversion (expr, target_type);
1676 if (target_type.IsPointer){
1677 if (expr_type.IsPointer)
1678 return new EmptyCast (expr, target_type);
1680 if (expr_type == TypeManager.sbyte_type ||
1681 expr_type == TypeManager.byte_type ||
1682 expr_type == TypeManager.short_type ||
1683 expr_type == TypeManager.ushort_type ||
1684 expr_type == TypeManager.int32_type ||
1685 expr_type == TypeManager.uint32_type ||
1686 expr_type == TypeManager.uint64_type ||
1687 expr_type == TypeManager.int64_type)
1688 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1690 if (expr_type.IsPointer){
1691 Expression e = null;
1693 if (target_type == TypeManager.sbyte_type)
1694 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1695 else if (target_type == TypeManager.byte_type)
1696 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1697 else if (target_type == TypeManager.short_type)
1698 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1699 else if (target_type == TypeManager.ushort_type)
1700 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1701 else if (target_type == TypeManager.int32_type)
1702 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1703 else if (target_type == TypeManager.uint32_type)
1704 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1705 else if (target_type == TypeManager.uint64_type)
1706 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1707 else if (target_type == TypeManager.int64_type){
1708 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1714 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1719 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1723 // We should always be able to go from an uint32
1724 // implicitly or explicitly to the other integral
1727 throw new Exception ("Internal compiler error");
1732 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1736 if (expr is NullLiteral){
1737 Report.Error (37, loc, "Cannot convert null to value type `" +
1738 TypeManager.CSharpName (target_type) + "'");
1742 Error_CannotConvertType (loc, original_expr_type, target_type);
1747 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1749 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1750 Type target_type, Location l)
1752 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1757 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1761 ne = ExplicitReferenceConversion (expr, target_type);
1765 Error_CannotConvertType (l, expr.Type, target_type);