2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
19 // A container class for all the conversion operations
21 public class Convert {
23 // This is used to prettify the code: a null argument is allowed
24 // for ImplicitStandardConversion as long as it is known that
25 // no anonymous method will play a role.
27 // FIXME: renamed from `const' to `static' to allow bootstraping from older
28 // versions of the compiler that could not cope with this construct.
30 public static EmitContext ConstantEC = null;
32 static public void Error_CannotConvertType (Location loc, Type source, Type target)
34 Report.Error (30, loc, "Cannot convert type '" +
35 TypeManager.CSharpName (source) + "' to '" +
36 TypeManager.CSharpName (target) + "'");
39 static EmptyExpression MyEmptyExpr;
40 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
42 Type expr_type = expr.Type;
44 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
45 // if we are a method group, emit a warning
50 if (expr_type == TypeManager.void_type)
54 // notice that it is possible to write "ValueType v = 1", the ValueType here
55 // is an abstract class, and not really a value type, so we apply the same rules.
57 if (target_type == TypeManager.object_type) {
59 // A pointer type cannot be converted to object
61 if (expr_type.IsPointer)
64 if (expr_type.IsValueType)
65 return new BoxedCast (expr);
66 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
67 if (target_type == TypeManager.anonymous_method_type)
69 return new EmptyCast (expr, target_type);
73 } else if (target_type == TypeManager.value_type) {
74 if (expr_type.IsValueType)
75 return new BoxedCast (expr);
76 if (expr_type == TypeManager.null_type)
77 return new NullCast (expr, target_type);
80 } else if (expr_type.IsSubclassOf (target_type)) {
82 // Special case: enumeration to System.Enum.
83 // System.Enum is not a value type, it is a class, so we need
84 // a boxing conversion
87 return new BoxedCast (expr);
89 return new EmptyCast (expr, target_type);
92 // This code is kind of mirrored inside ImplicitStandardConversionExists
93 // with the small distinction that we only probe there
95 // Always ensure that the code here and there is in sync
97 // from the null type to any reference-type.
98 if (expr_type == TypeManager.null_type){
99 if (target_type.IsPointer)
100 return NullPointer.Null;
102 if (!target_type.IsValueType)
103 return new NullCast (expr, target_type);
106 // from any class-type S to any interface-type T.
107 if (target_type.IsInterface) {
108 if (target_type != TypeManager.iconvertible_type &&
109 expr_type.IsValueType && (expr is Constant) &&
110 !(expr is IntLiteral || expr is BoolLiteral ||
111 expr is FloatLiteral || expr is DoubleLiteral ||
112 expr is LongLiteral || expr is CharLiteral ||
113 expr is StringLiteral || expr is DecimalLiteral ||
114 expr is UIntLiteral || expr is ULongLiteral)) {
118 if (TypeManager.ImplementsInterface (expr_type, target_type)){
119 if (expr_type.IsClass)
120 return new EmptyCast (expr, target_type);
121 else if (expr_type.IsValueType)
122 return new BoxedCast (expr, target_type);
124 return new EmptyCast (expr, target_type);
128 // from any interface type S to interface-type T.
129 if (expr_type.IsInterface && target_type.IsInterface) {
130 if (TypeManager.ImplementsInterface (expr_type, target_type))
131 return new EmptyCast (expr, target_type);
136 // from an array-type S to an array-type of type T
137 if (expr_type.IsArray && target_type.IsArray) {
138 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
140 Type expr_element_type = TypeManager.GetElementType (expr_type);
142 if (MyEmptyExpr == null)
143 MyEmptyExpr = new EmptyExpression ();
145 MyEmptyExpr.SetType (expr_element_type);
146 Type target_element_type = TypeManager.GetElementType (target_type);
148 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
149 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
150 target_element_type))
151 return new EmptyCast (expr, target_type);
155 // from an array-type to System.Array
156 if (expr_type.IsArray && target_type == TypeManager.array_type)
157 return new EmptyCast (expr, target_type);
159 // from any delegate type to System.Delegate
160 if ((expr_type == TypeManager.delegate_type ||
161 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
162 target_type == TypeManager.delegate_type)
163 return new EmptyCast (expr, target_type);
165 // from any array-type or delegate type into System.ICloneable.
166 if (expr_type.IsArray ||
167 expr_type == TypeManager.delegate_type ||
168 expr_type.IsSubclassOf (TypeManager.delegate_type))
169 if (target_type == TypeManager.icloneable_type)
170 return new EmptyCast (expr, target_type);
176 // Tests whether an implicit reference conversion exists between expr_type
179 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
181 Type expr_type = expr.Type;
184 // This is the boxed case.
186 if (target_type == TypeManager.object_type) {
187 if (expr_type.IsClass || expr_type.IsValueType ||
188 expr_type.IsInterface || expr_type == TypeManager.enum_type)
189 if (target_type != TypeManager.anonymous_method_type)
193 } else if (expr_type.IsSubclassOf (target_type))
196 // Please remember that all code below actually comes
197 // from ImplicitReferenceConversion so make sure code remains in sync
199 // from any class-type S to any interface-type T.
200 if (target_type.IsInterface) {
201 if (TypeManager.ImplementsInterface (expr_type, target_type))
205 // from any interface type S to interface-type T.
206 if (expr_type.IsInterface && target_type.IsInterface)
207 if (TypeManager.ImplementsInterface (expr_type, target_type))
210 // from an array-type S to an array-type of type T
211 if (expr_type.IsArray && target_type.IsArray) {
212 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
214 Type expr_element_type = expr_type.GetElementType ();
216 if (MyEmptyExpr == null)
217 MyEmptyExpr = new EmptyExpression ();
219 MyEmptyExpr.SetType (expr_element_type);
220 Type target_element_type = TypeManager.GetElementType (target_type);
222 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
223 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
224 target_element_type))
229 // from an array-type to System.Array
230 if (expr_type.IsArray && (target_type == TypeManager.array_type))
233 // from any delegate type to System.Delegate
234 if ((expr_type == TypeManager.delegate_type ||
235 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
236 target_type == TypeManager.delegate_type)
237 if (target_type.IsAssignableFrom (expr_type))
240 // from any array-type or delegate type into System.ICloneable.
241 if (expr_type.IsArray ||
242 expr_type == TypeManager.delegate_type ||
243 expr_type.IsSubclassOf (TypeManager.delegate_type))
244 if (target_type == TypeManager.icloneable_type)
247 // from the null type to any reference-type.
248 if (expr_type == TypeManager.null_type && !target_type.IsValueType && !TypeManager.IsEnumType (target_type))
255 /// Implicit Numeric Conversions.
257 /// expr is the expression to convert, returns a new expression of type
258 /// target_type or null if an implicit conversion is not possible.
260 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
261 Type target_type, Location loc)
263 Type expr_type = expr.Type;
266 // Attempt to do the implicit constant expression conversions
268 if (expr is Constant){
269 if (expr is IntConstant){
272 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
276 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
278 // Try the implicit constant expression conversion
279 // from long to ulong, instead of a nice routine,
282 long v = ((LongConstant) expr).Value;
284 return new ULongConstant ((ulong) v);
288 Type real_target_type = target_type;
290 if (expr_type == TypeManager.sbyte_type){
292 // From sbyte to short, int, long, float, double.
294 if (real_target_type == TypeManager.int32_type)
295 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
296 if (real_target_type == TypeManager.int64_type)
297 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
298 if (real_target_type == TypeManager.double_type)
299 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
300 if (real_target_type == TypeManager.float_type)
301 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
302 if (real_target_type == TypeManager.short_type)
303 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
304 } else if (expr_type == TypeManager.byte_type){
306 // From byte to short, ushort, int, uint, long, ulong, float, double
308 if ((real_target_type == TypeManager.short_type) ||
309 (real_target_type == TypeManager.ushort_type) ||
310 (real_target_type == TypeManager.int32_type) ||
311 (real_target_type == TypeManager.uint32_type))
312 return new EmptyCast (expr, target_type);
314 if (real_target_type == TypeManager.uint64_type)
315 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
316 if (real_target_type == TypeManager.int64_type)
317 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
318 if (real_target_type == TypeManager.float_type)
319 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
320 if (real_target_type == TypeManager.double_type)
321 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
322 } else if (expr_type == TypeManager.short_type){
324 // From short to int, long, float, double
326 if (real_target_type == TypeManager.int32_type)
327 return new EmptyCast (expr, target_type);
328 if (real_target_type == TypeManager.int64_type)
329 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
330 if (real_target_type == TypeManager.double_type)
331 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
332 if (real_target_type == TypeManager.float_type)
333 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
334 } else if (expr_type == TypeManager.ushort_type){
336 // From ushort to int, uint, long, ulong, float, double
338 if (real_target_type == TypeManager.uint32_type)
339 return new EmptyCast (expr, target_type);
341 if (real_target_type == TypeManager.uint64_type)
342 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
343 if (real_target_type == TypeManager.int32_type)
344 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
345 if (real_target_type == TypeManager.int64_type)
346 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
347 if (real_target_type == TypeManager.double_type)
348 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
349 if (real_target_type == TypeManager.float_type)
350 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
351 } else if (expr_type == TypeManager.int32_type){
353 // From int to long, float, double
355 if (real_target_type == TypeManager.int64_type)
356 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
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.uint32_type){
363 // From uint to long, ulong, float, double
365 if (real_target_type == TypeManager.int64_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
367 if (real_target_type == TypeManager.uint64_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
369 if (real_target_type == TypeManager.double_type)
370 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
372 if (real_target_type == TypeManager.float_type)
373 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
375 } else if (expr_type == TypeManager.int64_type){
377 // From long/ulong to float, double
379 if (real_target_type == TypeManager.double_type)
380 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
381 if (real_target_type == TypeManager.float_type)
382 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
383 } else if (expr_type == TypeManager.uint64_type){
385 // From ulong to float, double
387 if (real_target_type == TypeManager.double_type)
388 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
390 if (real_target_type == TypeManager.float_type)
391 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
393 } else if (expr_type == TypeManager.char_type){
395 // From char to ushort, int, uint, long, ulong, float, double
397 if ((real_target_type == TypeManager.ushort_type) ||
398 (real_target_type == TypeManager.int32_type) ||
399 (real_target_type == TypeManager.uint32_type))
400 return new EmptyCast (expr, target_type);
401 if (real_target_type == TypeManager.uint64_type)
402 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
403 if (real_target_type == TypeManager.int64_type)
404 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
405 if (real_target_type == TypeManager.float_type)
406 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
407 if (real_target_type == TypeManager.double_type)
408 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
409 } else if (expr_type == TypeManager.float_type){
413 if (real_target_type == TypeManager.double_type)
414 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
422 /// Same as ImplicitStandardConversionExists except that it also looks at
423 /// implicit user defined conversions - needed for overload resolution
425 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
427 if (ImplicitStandardConversionExists (ec, expr, target_type))
430 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
438 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
440 Expression dummy = ImplicitUserConversion (
441 ec, new EmptyExpression (source), target, Location.Null);
442 return dummy != null;
446 /// Determines if a standard implicit conversion exists from
447 /// expr_type to target_type
449 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
451 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
453 Type expr_type = expr.Type;
455 if (expr_type == TypeManager.void_type)
458 if (expr_type == target_type)
461 // First numeric conversions
463 if (expr_type == TypeManager.sbyte_type){
465 // From sbyte to short, int, long, float, double.
467 if ((target_type == TypeManager.int32_type) ||
468 (target_type == TypeManager.int64_type) ||
469 (target_type == TypeManager.double_type) ||
470 (target_type == TypeManager.float_type) ||
471 (target_type == TypeManager.short_type) ||
472 (target_type == TypeManager.decimal_type))
475 } else if (expr_type == TypeManager.byte_type){
477 // From byte to short, ushort, int, uint, long, ulong, float, double
479 if ((target_type == TypeManager.short_type) ||
480 (target_type == TypeManager.ushort_type) ||
481 (target_type == TypeManager.int32_type) ||
482 (target_type == TypeManager.uint32_type) ||
483 (target_type == TypeManager.uint64_type) ||
484 (target_type == TypeManager.int64_type) ||
485 (target_type == TypeManager.float_type) ||
486 (target_type == TypeManager.double_type) ||
487 (target_type == TypeManager.decimal_type))
490 } else if (expr_type == TypeManager.short_type){
492 // From short to int, long, float, double
494 if ((target_type == TypeManager.int32_type) ||
495 (target_type == TypeManager.int64_type) ||
496 (target_type == TypeManager.double_type) ||
497 (target_type == TypeManager.float_type) ||
498 (target_type == TypeManager.decimal_type))
501 } else if (expr_type == TypeManager.ushort_type){
503 // From ushort to int, uint, long, ulong, float, double
505 if ((target_type == TypeManager.uint32_type) ||
506 (target_type == TypeManager.uint64_type) ||
507 (target_type == TypeManager.int32_type) ||
508 (target_type == TypeManager.int64_type) ||
509 (target_type == TypeManager.double_type) ||
510 (target_type == TypeManager.float_type) ||
511 (target_type == TypeManager.decimal_type))
514 } else if (expr_type == TypeManager.int32_type){
516 // From int to long, float, double
518 if ((target_type == TypeManager.int64_type) ||
519 (target_type == TypeManager.double_type) ||
520 (target_type == TypeManager.float_type) ||
521 (target_type == TypeManager.decimal_type))
524 } else if (expr_type == TypeManager.uint32_type){
526 // From uint to long, ulong, float, double
528 if ((target_type == TypeManager.int64_type) ||
529 (target_type == TypeManager.uint64_type) ||
530 (target_type == TypeManager.double_type) ||
531 (target_type == TypeManager.float_type) ||
532 (target_type == TypeManager.decimal_type))
535 } else if ((expr_type == TypeManager.uint64_type) ||
536 (expr_type == TypeManager.int64_type)) {
538 // From long/ulong to float, double
540 if ((target_type == TypeManager.double_type) ||
541 (target_type == TypeManager.float_type) ||
542 (target_type == TypeManager.decimal_type))
545 } else if (expr_type == TypeManager.char_type){
547 // From char to ushort, int, uint, long, ulong, float, double
549 if ((target_type == TypeManager.ushort_type) ||
550 (target_type == TypeManager.int32_type) ||
551 (target_type == TypeManager.uint32_type) ||
552 (target_type == TypeManager.uint64_type) ||
553 (target_type == TypeManager.int64_type) ||
554 (target_type == TypeManager.float_type) ||
555 (target_type == TypeManager.double_type) ||
556 (target_type == TypeManager.decimal_type))
559 } else if (expr_type == TypeManager.float_type){
563 if (target_type == TypeManager.double_type)
567 if (ImplicitReferenceConversionExists (expr, target_type))
571 // Implicit Constant Expression Conversions
573 if (expr is IntConstant){
574 int value = ((IntConstant) expr).Value;
576 if (target_type == TypeManager.sbyte_type){
577 if (value >= SByte.MinValue && value <= SByte.MaxValue)
579 } else if (target_type == TypeManager.byte_type){
580 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
582 } else if (target_type == TypeManager.short_type){
583 if (value >= Int16.MinValue && value <= Int16.MaxValue)
585 } else if (target_type == TypeManager.ushort_type){
586 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
588 } else if (target_type == TypeManager.uint32_type){
591 } else if (target_type == TypeManager.uint64_type){
593 // we can optimize this case: a positive int32
594 // always fits on a uint64. But we need an opcode
601 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
605 if (expr is LongConstant && target_type == TypeManager.uint64_type){
607 // Try the implicit constant expression conversion
608 // from long to ulong, instead of a nice routine,
611 long v = ((LongConstant) expr).Value;
616 if ((target_type == TypeManager.enum_type ||
617 target_type.IsSubclassOf (TypeManager.enum_type)) &&
619 IntLiteral i = (IntLiteral) expr;
625 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
628 if (expr_type == TypeManager.anonymous_method_type){
629 if (!TypeManager.IsDelegateType (target_type))
632 AnonymousMethod am = (AnonymousMethod) expr;
633 int errors = Report.Errors;
635 Expression conv = am.Compatible (ec, target_type, true);
644 // Used internally by FindMostEncompassedType, this is used
645 // to avoid creating lots of objects in the tight loop inside
646 // FindMostEncompassedType
648 static EmptyExpression priv_fmet_param;
651 /// Finds "most encompassed type" according to the spec (13.4.2)
652 /// amongst the methods in the MethodGroupExpr
654 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
658 if (priv_fmet_param == null)
659 priv_fmet_param = new EmptyExpression ();
661 foreach (Type t in types){
662 priv_fmet_param.SetType (t);
669 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
677 // Used internally by FindMostEncompassingType, this is used
678 // to avoid creating lots of objects in the tight loop inside
679 // FindMostEncompassingType
681 static EmptyExpression priv_fmee_ret;
684 /// Finds "most encompassing type" according to the spec (13.4.2)
685 /// amongst the types in the given set
687 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
691 if (priv_fmee_ret == null)
692 priv_fmee_ret = new EmptyExpression ();
694 foreach (Type t in types){
695 priv_fmee_ret.SetType (best);
702 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
710 // Used to avoid creating too many objects
712 static EmptyExpression priv_fms_expr;
715 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
716 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
717 /// for explicit and implicit conversion operators.
719 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
720 Expression source, bool apply_explicit_conv_rules,
723 ArrayList src_types_set = new ArrayList ();
725 if (priv_fms_expr == null)
726 priv_fms_expr = new EmptyExpression ();
729 // If any operator converts from S then Sx = S
731 Type source_type = source.Type;
732 foreach (MethodBase mb in me.Methods){
733 ParameterData pd = Invocation.GetParameterData (mb);
734 Type param_type = pd.ParameterType (0);
736 if (param_type == source_type)
739 if (apply_explicit_conv_rules) {
742 // Find the set of applicable user-defined conversion operators, U. This set
744 // user-defined implicit or explicit conversion operators declared by
745 // the classes or structs in D that convert from a type encompassing
746 // or encompassed by S to a type encompassing or encompassed by T
748 priv_fms_expr.SetType (param_type);
749 if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
750 src_types_set.Add (param_type);
752 if (ImplicitStandardConversionExists (ec, source, param_type))
753 src_types_set.Add (param_type);
757 // Only if S is encompassed by param_type
759 if (ImplicitStandardConversionExists (ec, source, param_type))
760 src_types_set.Add (param_type);
765 // Explicit Conv rules
767 if (apply_explicit_conv_rules) {
768 ArrayList candidate_set = new ArrayList ();
770 foreach (Type param_type in src_types_set){
771 if (ImplicitStandardConversionExists (ec, source, param_type))
772 candidate_set.Add (param_type);
775 if (candidate_set.Count != 0)
776 return FindMostEncompassedType (ec, candidate_set);
782 if (apply_explicit_conv_rules)
783 return FindMostEncompassingType (ec, src_types_set);
785 return FindMostEncompassedType (ec, src_types_set);
789 // Useful in avoiding proliferation of objects
791 static EmptyExpression priv_fmt_expr;
794 /// Finds the most specific target Tx according to section 13.4.4
796 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
797 Type target, bool apply_explicit_conv_rules,
800 ArrayList tgt_types_set = new ArrayList ();
802 if (priv_fmt_expr == null)
803 priv_fmt_expr = new EmptyExpression ();
806 // If any operator converts to T then Tx = T
808 foreach (MethodInfo mi in me.Methods){
809 Type ret_type = mi.ReturnType;
811 if (ret_type == target)
814 if (apply_explicit_conv_rules) {
817 // Find the set of applicable user-defined conversion operators, U.
819 // This set consists of the
820 // user-defined implicit or explicit conversion operators declared by
821 // the classes or structs in D that convert from a type encompassing
822 // or encompassed by S to a type encompassing or encompassed by T
824 priv_fms_expr.SetType (ret_type);
825 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
826 tgt_types_set.Add (ret_type);
828 priv_fms_expr.SetType (target);
829 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
830 tgt_types_set.Add (ret_type);
834 // Only if T is encompassed by param_type
836 priv_fms_expr.SetType (ret_type);
837 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
838 tgt_types_set.Add (ret_type);
843 // Explicit conv rules
845 if (apply_explicit_conv_rules) {
846 ArrayList candidate_set = new ArrayList ();
848 foreach (Type ret_type in tgt_types_set){
849 priv_fmt_expr.SetType (ret_type);
851 if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
852 candidate_set.Add (ret_type);
855 if (candidate_set.Count != 0)
856 return FindMostEncompassingType (ec, candidate_set);
860 // Okay, final case !
862 if (apply_explicit_conv_rules)
863 return FindMostEncompassedType (ec, tgt_types_set);
865 return FindMostEncompassingType (ec, tgt_types_set);
869 /// User-defined Implicit conversions
871 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
872 Type target, Location loc)
874 return UserDefinedConversion (ec, source, target, loc, false);
878 /// User-defined Explicit conversions
880 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
881 Type target, Location loc)
883 return UserDefinedConversion (ec, source, target, loc, true);
886 static DoubleHash explicit_conv = new DoubleHash (100);
887 static DoubleHash implicit_conv = new DoubleHash (100);
889 /// Computes the MethodGroup for the user-defined conversion
890 /// operators from source_type to target_type. `look_for_explicit'
891 /// controls whether we should also include the list of explicit
894 static MethodGroupExpr GetConversionOperators (EmitContext ec,
895 Type source_type, Type target_type,
896 Location loc, bool look_for_explicit)
898 Expression mg1 = null, mg2 = null;
899 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
902 op_name = "op_Implicit";
904 MethodGroupExpr union3;
906 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
907 return (MethodGroupExpr) r;
909 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
910 if (source_type.BaseType != null)
911 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
914 union3 = (MethodGroupExpr) mg2;
915 else if (mg2 == null)
916 union3 = (MethodGroupExpr) mg1;
918 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
920 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
923 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
925 union3 = (MethodGroupExpr) mg1;
928 if (target_type.BaseType != null)
929 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
933 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
935 union3 = (MethodGroupExpr) mg1;
938 MethodGroupExpr union4 = null;
940 if (look_for_explicit) {
941 op_name = "op_Explicit";
943 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
944 if (source_type.BaseType != null)
945 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
947 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
948 if (target_type.BaseType != null)
949 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
951 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
952 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
954 union4 = Invocation.MakeUnionSet (union5, union6, loc);
957 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
958 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
963 /// User-defined conversions
965 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
966 Type target, Location loc,
967 bool look_for_explicit)
969 MethodGroupExpr union;
970 Type source_type = source.Type;
971 MethodBase method = null;
973 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
977 Type most_specific_source, most_specific_target;
979 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
980 if (most_specific_source == null)
983 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
984 if (most_specific_target == null)
990 foreach (MethodBase mb in union.Methods){
991 ParameterData pd = Invocation.GetParameterData (mb);
992 MethodInfo mi = (MethodInfo) mb;
994 if (pd.ParameterType (0) == most_specific_source &&
995 mi.ReturnType == most_specific_target) {
1001 if (method == null || count > 1)
1006 // This will do the conversion to the best match that we
1007 // found. Now we need to perform an implict standard conversion
1008 // if the best match was not the type that we were requested
1011 if (look_for_explicit)
1012 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1014 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1020 e = new UserCast ((MethodInfo) method, source, loc);
1021 if (e.Type != target){
1022 if (!look_for_explicit)
1023 e = ImplicitConversionStandard (ec, e, target, loc);
1025 e = ExplicitConversionStandard (ec, e, target, loc);
1032 /// Converts implicitly the resolved expression `expr' into the
1033 /// `target_type'. It returns a new expression that can be used
1034 /// in a context that expects a `target_type'.
1036 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1037 Type target_type, Location loc)
1041 if (target_type == null)
1042 throw new Exception ("Target type is null");
1044 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1048 e = ImplicitUserConversion (ec, expr, target_type, loc);
1057 /// Attempts to apply the `Standard Implicit
1058 /// Conversion' rules to the expression `expr' into
1059 /// the `target_type'. It returns a new expression
1060 /// that can be used in a context that expects a
1063 /// This is different from `ImplicitConversion' in that the
1064 /// user defined implicit conversions are excluded.
1066 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1067 Type target_type, Location loc)
1069 Type expr_type = expr.Type;
1072 if (expr.eclass == ExprClass.MethodGroup){
1073 if (!TypeManager.IsDelegateType (target_type)){
1078 // Only allow anonymous method conversions on post ISO_1
1080 if (RootContext.Version != LanguageVersion.ISO_1){
1081 MethodGroupExpr mg = expr as MethodGroupExpr;
1083 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1087 if (expr_type == target_type && expr_type != TypeManager.null_type)
1090 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1094 e = ImplicitReferenceConversion (expr, target_type);
1098 if ((target_type == TypeManager.enum_type ||
1099 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1100 expr is IntLiteral){
1101 IntLiteral i = (IntLiteral) expr;
1104 return new EnumConstant ((Constant) expr, target_type);
1108 if (expr_type.IsPointer){
1109 if (target_type == TypeManager.void_ptr_type)
1110 return new EmptyCast (expr, target_type);
1113 // yep, comparing pointer types cant be done with
1114 // t1 == t2, we have to compare their element types.
1116 if (target_type.IsPointer){
1117 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1122 if (target_type.IsPointer) {
1123 if (expr_type == TypeManager.null_type)
1124 return new EmptyCast (expr, target_type);
1126 if (expr_type == TypeManager.void_ptr_type)
1127 return new EmptyCast (expr, target_type);
1131 if (expr_type == TypeManager.anonymous_method_type){
1132 if (!TypeManager.IsDelegateType (target_type)){
1133 Report.Error (1660, loc,
1134 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1135 TypeManager.CSharpName (target_type));
1139 AnonymousMethod am = (AnonymousMethod) expr;
1140 int errors = Report.Errors;
1142 Expression conv = am.Compatible (ec, target_type, false);
1147 // We return something instead of null, to avoid
1148 // the duplicate error, since am.Compatible would have
1149 // reported that already
1151 if (errors != Report.Errors)
1152 return new EmptyCast (expr, target_type);
1159 /// Attemps to perform an implict constant conversion of the IntConstant
1160 /// into a different data type using casts (See Implicit Constant
1161 /// Expression Conversions)
1163 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1165 int value = ic.Value;
1167 if (target_type == TypeManager.sbyte_type){
1168 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1169 return new SByteConstant ((sbyte) value);
1170 } else if (target_type == TypeManager.byte_type){
1171 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1172 return new ByteConstant ((byte) value);
1173 } else if (target_type == TypeManager.short_type){
1174 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1175 return new ShortConstant ((short) value);
1176 } else if (target_type == TypeManager.ushort_type){
1177 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1178 return new UShortConstant ((ushort) value);
1179 } else if (target_type == TypeManager.uint32_type){
1181 return new UIntConstant ((uint) value);
1182 } else if (target_type == TypeManager.uint64_type){
1184 // we can optimize this case: a positive int32
1185 // always fits on a uint64. But we need an opcode
1189 return new ULongConstant ((ulong) value);
1190 } else if (target_type == TypeManager.double_type)
1191 return new DoubleConstant ((double) value);
1192 else if (target_type == TypeManager.float_type)
1193 return new FloatConstant ((float) value);
1195 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1196 Type underlying = TypeManager.EnumToUnderlying (target_type);
1197 Constant e = (Constant) ic;
1200 // Possibly, we need to create a different 0 literal before passing
1203 if (underlying == TypeManager.int64_type)
1204 e = new LongLiteral (0);
1205 else if (underlying == TypeManager.uint64_type)
1206 e = new ULongLiteral (0);
1208 return new EnumConstant (e, target_type);
1213 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1215 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1216 source == TypeManager.anonymous_method_type ?
1217 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1218 TypeManager.CSharpName (target));
1222 /// Attemptes to implicityly convert `target' into `type', using
1223 /// ImplicitConversion. If there is no implicit conversion, then
1224 /// an error is signaled
1226 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1227 Type target_type, Location loc)
1231 e = ImplicitConversion (ec, source, target_type, loc);
1235 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1236 Report.Error (664, loc,
1237 "Double literal cannot be implicitly converted to " +
1238 "float type, use F suffix to create a float literal");
1241 if (source is Constant){
1242 Constant c = (Constant) source;
1244 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1248 Error_CannotImplicitConversion (loc, source.Type, target_type);
1254 /// Performs the explicit numeric conversions
1256 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1258 Type expr_type = expr.Type;
1261 // If we have an enumeration, extract the underlying type,
1262 // use this during the comparison, but wrap around the original
1265 Type real_target_type = target_type;
1267 if (TypeManager.IsEnumType (real_target_type))
1268 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1270 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1271 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1273 if (real_target_type != target_type)
1274 return new EmptyCast (ce, target_type);
1278 if (expr_type == TypeManager.sbyte_type){
1280 // From sbyte to byte, ushort, uint, ulong, char
1282 if (real_target_type == TypeManager.byte_type)
1283 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1284 if (real_target_type == TypeManager.ushort_type)
1285 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1286 if (real_target_type == TypeManager.uint32_type)
1287 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1288 if (real_target_type == TypeManager.uint64_type)
1289 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1290 if (real_target_type == TypeManager.char_type)
1291 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1292 } else if (expr_type == TypeManager.byte_type){
1294 // From byte to sbyte and char
1296 if (real_target_type == TypeManager.sbyte_type)
1297 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1298 if (real_target_type == TypeManager.char_type)
1299 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1300 } else if (expr_type == TypeManager.short_type){
1302 // From short to sbyte, byte, ushort, uint, ulong, char
1304 if (real_target_type == TypeManager.sbyte_type)
1305 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1306 if (real_target_type == TypeManager.byte_type)
1307 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1308 if (real_target_type == TypeManager.ushort_type)
1309 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1310 if (real_target_type == TypeManager.uint32_type)
1311 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1312 if (real_target_type == TypeManager.uint64_type)
1313 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1314 if (real_target_type == TypeManager.char_type)
1315 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1316 } else if (expr_type == TypeManager.ushort_type){
1318 // From ushort to sbyte, byte, short, char
1320 if (real_target_type == TypeManager.sbyte_type)
1321 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1322 if (real_target_type == TypeManager.byte_type)
1323 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1324 if (real_target_type == TypeManager.short_type)
1325 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1326 if (real_target_type == TypeManager.char_type)
1327 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1328 } else if (expr_type == TypeManager.int32_type){
1330 // From int to sbyte, byte, short, ushort, uint, ulong, char
1332 if (real_target_type == TypeManager.sbyte_type)
1333 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1334 if (real_target_type == TypeManager.byte_type)
1335 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1336 if (real_target_type == TypeManager.short_type)
1337 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1338 if (real_target_type == TypeManager.ushort_type)
1339 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1340 if (real_target_type == TypeManager.uint32_type)
1341 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1342 if (real_target_type == TypeManager.uint64_type)
1343 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1344 if (real_target_type == TypeManager.char_type)
1345 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1346 } else if (expr_type == TypeManager.uint32_type){
1348 // From uint to sbyte, byte, short, ushort, int, char
1350 if (real_target_type == TypeManager.sbyte_type)
1351 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1352 if (real_target_type == TypeManager.byte_type)
1353 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1354 if (real_target_type == TypeManager.short_type)
1355 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1356 if (real_target_type == TypeManager.ushort_type)
1357 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1358 if (real_target_type == TypeManager.int32_type)
1359 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1360 if (real_target_type == TypeManager.char_type)
1361 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1362 } else if (expr_type == TypeManager.int64_type){
1364 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1366 if (real_target_type == TypeManager.sbyte_type)
1367 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1368 if (real_target_type == TypeManager.byte_type)
1369 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1370 if (real_target_type == TypeManager.short_type)
1371 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1372 if (real_target_type == TypeManager.ushort_type)
1373 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1374 if (real_target_type == TypeManager.int32_type)
1375 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1376 if (real_target_type == TypeManager.uint32_type)
1377 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1378 if (real_target_type == TypeManager.uint64_type)
1379 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1380 if (real_target_type == TypeManager.char_type)
1381 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1382 } else if (expr_type == TypeManager.uint64_type){
1384 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1386 if (real_target_type == TypeManager.sbyte_type)
1387 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1388 if (real_target_type == TypeManager.byte_type)
1389 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1390 if (real_target_type == TypeManager.short_type)
1391 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1392 if (real_target_type == TypeManager.ushort_type)
1393 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1394 if (real_target_type == TypeManager.int32_type)
1395 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1396 if (real_target_type == TypeManager.uint32_type)
1397 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1398 if (real_target_type == TypeManager.int64_type)
1399 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1400 if (real_target_type == TypeManager.char_type)
1401 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1402 } else if (expr_type == TypeManager.char_type){
1404 // From char to sbyte, byte, short
1406 if (real_target_type == TypeManager.sbyte_type)
1407 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1408 if (real_target_type == TypeManager.byte_type)
1409 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1410 if (real_target_type == TypeManager.short_type)
1411 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1412 } else if (expr_type == TypeManager.float_type){
1414 // From float to sbyte, byte, short,
1415 // ushort, int, uint, long, ulong, char
1418 if (real_target_type == TypeManager.sbyte_type)
1419 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1420 if (real_target_type == TypeManager.byte_type)
1421 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1422 if (real_target_type == TypeManager.short_type)
1423 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1424 if (real_target_type == TypeManager.ushort_type)
1425 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1426 if (real_target_type == TypeManager.int32_type)
1427 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1428 if (real_target_type == TypeManager.uint32_type)
1429 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1430 if (real_target_type == TypeManager.int64_type)
1431 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1432 if (real_target_type == TypeManager.uint64_type)
1433 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1434 if (real_target_type == TypeManager.char_type)
1435 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1436 } else if (expr_type == TypeManager.double_type){
1438 // From double to byte, byte, short,
1439 // ushort, int, uint, long, ulong,
1440 // char, float or decimal
1442 if (real_target_type == TypeManager.sbyte_type)
1443 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1444 if (real_target_type == TypeManager.byte_type)
1445 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1446 if (real_target_type == TypeManager.short_type)
1447 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1448 if (real_target_type == TypeManager.ushort_type)
1449 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1450 if (real_target_type == TypeManager.int32_type)
1451 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1452 if (real_target_type == TypeManager.uint32_type)
1453 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1454 if (real_target_type == TypeManager.int64_type)
1455 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1456 if (real_target_type == TypeManager.uint64_type)
1457 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1458 if (real_target_type == TypeManager.char_type)
1459 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1460 if (real_target_type == TypeManager.float_type)
1461 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1464 // decimal is taken care of by the op_Explicit methods.
1470 /// Returns whether an explicit reference conversion can be performed
1471 /// from source_type to target_type
1473 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1475 bool target_is_value_type = target_type.IsValueType;
1477 if (source_type == target_type)
1481 // From object to any reference type
1483 if (source_type == TypeManager.object_type && !target_is_value_type)
1487 // From any class S to any class-type T, provided S is a base class of T
1489 if (target_type.IsSubclassOf (source_type))
1493 // From any interface type S to any interface T provided S is not derived from T
1495 if (source_type.IsInterface && target_type.IsInterface){
1496 if (!target_type.IsSubclassOf (source_type))
1501 // From any class type S to any interface T, provided S is not sealed
1502 // and provided S does not implement T.
1504 if (target_type.IsInterface && !source_type.IsSealed &&
1505 !TypeManager.ImplementsInterface (source_type, target_type))
1509 // From any interface-type S to to any class type T, provided T is not
1510 // sealed, or provided T implements S.
1512 if (source_type.IsInterface &&
1513 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1517 // From an array type S with an element type Se to an array type T with an
1518 // element type Te provided all the following are true:
1519 // * S and T differe only in element type, in other words, S and T
1520 // have the same number of dimensions.
1521 // * Both Se and Te are reference types
1522 // * An explicit referenc conversions exist from Se to Te
1524 if (source_type.IsArray && target_type.IsArray) {
1525 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1527 Type source_element_type = TypeManager.GetElementType (source_type);
1528 Type target_element_type = TypeManager.GetElementType (target_type);
1530 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1531 if (ExplicitReferenceConversionExists (source_element_type,
1532 target_element_type))
1538 // From System.Array to any array-type
1539 if (source_type == TypeManager.array_type &&
1540 target_type.IsArray){
1545 // From System delegate to any delegate-type
1547 if (source_type == TypeManager.delegate_type &&
1548 target_type.IsSubclassOf (TypeManager.delegate_type))
1552 // From ICloneable to Array or Delegate types
1554 if (source_type == TypeManager.icloneable_type &&
1555 (target_type == TypeManager.array_type ||
1556 target_type == TypeManager.delegate_type))
1563 /// Implements Explicit Reference conversions
1565 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1567 Type source_type = source.Type;
1568 bool target_is_value_type = target_type.IsValueType;
1571 // From object to any reference type
1573 if (source_type == TypeManager.object_type && !target_is_value_type)
1574 return new ClassCast (source, target_type);
1577 // Unboxing conversion.
1579 if (((source_type == TypeManager.enum_type &&
1580 !(source is EmptyCast)) ||
1581 source_type == TypeManager.value_type) && target_is_value_type)
1582 return new UnboxCast (source, target_type);
1585 // From any class S to any class-type T, provided S is a base class of T
1587 if (target_type.IsSubclassOf (source_type))
1588 return new ClassCast (source, target_type);
1591 // From any interface type S to any interface T provided S is not derived from T
1593 if (source_type.IsInterface && target_type.IsInterface){
1594 if (TypeManager.ImplementsInterface (source_type, target_type))
1597 return new ClassCast (source, target_type);
1601 // From any class type S to any interface T, provides S is not sealed
1602 // and provided S does not implement T.
1604 if (target_type.IsInterface && !source_type.IsSealed) {
1605 if (TypeManager.ImplementsInterface (source_type, target_type))
1608 return new ClassCast (source, target_type);
1613 // From any interface-type S to to any class type T, provided T is not
1614 // sealed, or provided T implements S.
1616 if (source_type.IsInterface) {
1617 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1618 if (target_type.IsClass)
1619 return new ClassCast (source, target_type);
1621 return new UnboxCast (source, target_type);
1627 // From an array type S with an element type Se to an array type T with an
1628 // element type Te provided all the following are true:
1629 // * S and T differe only in element type, in other words, S and T
1630 // have the same number of dimensions.
1631 // * Both Se and Te are reference types
1632 // * An explicit referenc conversions exist from Se to Te
1634 if (source_type.IsArray && target_type.IsArray) {
1635 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1637 Type source_element_type = TypeManager.GetElementType (source_type);
1638 Type target_element_type = TypeManager.GetElementType (target_type);
1640 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1641 if (ExplicitReferenceConversionExists (source_element_type,
1642 target_element_type))
1643 return new ClassCast (source, target_type);
1648 // From System.Array to any array-type
1649 if (source_type == TypeManager.array_type &&
1650 target_type.IsArray) {
1651 return new ClassCast (source, target_type);
1655 // From System delegate to any delegate-type
1657 if (source_type == TypeManager.delegate_type &&
1658 target_type.IsSubclassOf (TypeManager.delegate_type))
1659 return new ClassCast (source, target_type);
1662 // From ICloneable to Array or Delegate types
1664 if (source_type == TypeManager.icloneable_type &&
1665 (target_type == TypeManager.array_type ||
1666 target_type == TypeManager.delegate_type))
1667 return new ClassCast (source, target_type);
1673 /// Performs an explicit conversion of the expression `expr' whose
1674 /// type is expr.Type to `target_type'.
1676 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1677 Type target_type, Location loc)
1679 Type expr_type = expr.Type;
1680 Type original_expr_type = expr_type;
1682 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1683 if (target_type == TypeManager.enum_type ||
1684 target_type == TypeManager.object_type) {
1685 if (expr is EnumConstant)
1686 expr = ((EnumConstant) expr).Child;
1687 // We really need all these casts here .... :-(
1688 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1689 return new EmptyCast (expr, target_type);
1690 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1691 target_type.IsSubclassOf (TypeManager.enum_type))
1692 return new UnboxCast (expr, target_type);
1695 // Notice that we have kept the expr_type unmodified, which is only
1697 if (expr is EnumConstant)
1698 expr = ((EnumConstant) expr).Child;
1700 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1701 expr_type = expr.Type;
1704 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1709 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1714 // Unboxing conversion.
1716 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1717 return new UnboxCast (expr, target_type);
1720 // Skip the ExplicitReferenceConversion because we can not convert
1721 // from Null to a ValueType, and ExplicitReference wont check against
1722 // null literal explicitly
1724 if (expr_type != TypeManager.null_type){
1725 ne = ExplicitReferenceConversion (expr, target_type);
1732 if (target_type.IsPointer){
1733 if (expr_type.IsPointer)
1734 return new EmptyCast (expr, target_type);
1736 if (expr_type == TypeManager.sbyte_type ||
1737 expr_type == TypeManager.byte_type ||
1738 expr_type == TypeManager.short_type ||
1739 expr_type == TypeManager.ushort_type ||
1740 expr_type == TypeManager.int32_type ||
1741 expr_type == TypeManager.uint32_type ||
1742 expr_type == TypeManager.uint64_type ||
1743 expr_type == TypeManager.int64_type)
1744 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1746 if (expr_type.IsPointer){
1747 Expression e = null;
1749 if (target_type == TypeManager.sbyte_type)
1750 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1751 else if (target_type == TypeManager.byte_type)
1752 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1753 else if (target_type == TypeManager.short_type)
1754 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1755 else if (target_type == TypeManager.ushort_type)
1756 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1757 else if (target_type == TypeManager.int32_type)
1758 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1759 else if (target_type == TypeManager.uint32_type)
1760 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1761 else if (target_type == TypeManager.uint64_type)
1762 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1763 else if (target_type == TypeManager.int64_type){
1764 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1770 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1775 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1779 // We should always be able to go from an uint32
1780 // implicitly or explicitly to the other integral
1783 throw new Exception ("Internal compiler error");
1788 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1792 if (expr is NullLiteral){
1793 Report.Error (37, loc, "Cannot convert null to value type `" +
1794 TypeManager.CSharpName (target_type) + "'");
1798 Error_CannotConvertType (loc, original_expr_type, target_type);
1803 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1805 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1806 Type target_type, Location l)
1808 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1813 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1817 ne = ExplicitReferenceConversion (expr, target_type);
1821 Error_CannotConvertType (l, expr.Type, target_type);