2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
19 // A container class for all the conversion operations
21 public class Convert {
22 static public void Error_CannotConvertType (Location loc, Type source, Type target)
24 Report.Error (30, loc, "Cannot convert type '" +
25 TypeManager.CSharpName (source) + "' to '" +
26 TypeManager.CSharpName (target) + "'");
29 static EmptyExpression MyEmptyExpr;
30 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
32 Type expr_type = expr.Type;
34 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
35 // if we are a method group, emit a warning
40 if (expr_type == TypeManager.void_type)
44 // notice that it is possible to write "ValueType v = 1", the ValueType here
45 // is an abstract class, and not really a value type, so we apply the same rules.
47 if (target_type == TypeManager.object_type) {
49 // A pointer type cannot be converted to object
51 if (expr_type.IsPointer)
54 if (expr_type.IsValueType)
55 return new BoxedCast (expr);
56 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
57 return new EmptyCast (expr, target_type);
58 } else if (target_type == TypeManager.value_type) {
59 if (expr_type.IsValueType)
60 return new BoxedCast (expr);
61 if (expr is NullLiteral)
62 return new NullCast (expr, target_type);
63 } else if (expr_type.IsSubclassOf (target_type)) {
65 // Special case: enumeration to System.Enum.
66 // System.Enum is not a value type, it is a class, so we need
67 // a boxing conversion
70 return new BoxedCast (expr);
72 return new EmptyCast (expr, target_type);
75 // This code is kind of mirrored inside ImplicitStandardConversionExists
76 // with the small distinction that we only probe there
78 // Always ensure that the code here and there is in sync
80 // from the null type to any reference-type.
81 if (expr is NullLiteral){
82 if (target_type.IsPointer)
83 return NullPointer.Null;
85 if (!target_type.IsValueType)
86 return new NullCast (expr, target_type);
89 // from any class-type S to any interface-type T.
90 if (target_type.IsInterface) {
91 if (target_type != TypeManager.iconvertible_type &&
92 expr_type.IsValueType && (expr is Constant) &&
93 !(expr is IntLiteral || expr is BoolLiteral ||
94 expr is FloatLiteral || expr is DoubleLiteral ||
95 expr is LongLiteral || expr is CharLiteral ||
96 expr is StringLiteral || expr is DecimalLiteral ||
97 expr is UIntLiteral || expr is ULongLiteral)) {
101 if (TypeManager.ImplementsInterface (expr_type, target_type)){
102 if (expr_type.IsClass)
103 return new EmptyCast (expr, target_type);
104 else if (expr_type.IsValueType)
105 return new BoxedCast (expr, target_type);
107 return new EmptyCast (expr, target_type);
111 // from any interface type S to interface-type T.
112 if (expr_type.IsInterface && target_type.IsInterface) {
113 if (TypeManager.ImplementsInterface (expr_type, target_type))
114 return new EmptyCast (expr, target_type);
119 // from an array-type S to an array-type of type T
120 if (expr_type.IsArray && target_type.IsArray) {
121 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
123 Type expr_element_type = TypeManager.GetElementType (expr_type);
125 if (MyEmptyExpr == null)
126 MyEmptyExpr = new EmptyExpression ();
128 MyEmptyExpr.SetType (expr_element_type);
129 Type target_element_type = TypeManager.GetElementType (target_type);
131 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
132 if (ImplicitStandardConversionExists (MyEmptyExpr,
133 target_element_type))
134 return new EmptyCast (expr, target_type);
139 // from an array-type to System.Array
140 if (expr_type.IsArray && target_type == TypeManager.array_type)
141 return new EmptyCast (expr, target_type);
143 // from any delegate type to System.Delegate
144 if ((expr_type == TypeManager.delegate_type ||
145 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
146 target_type == TypeManager.delegate_type)
147 return new EmptyCast (expr, target_type);
149 // from any array-type or delegate type into System.ICloneable.
150 if (expr_type.IsArray ||
151 expr_type == TypeManager.delegate_type ||
152 expr_type.IsSubclassOf (TypeManager.delegate_type))
153 if (target_type == TypeManager.icloneable_type)
154 return new EmptyCast (expr, target_type);
164 // Tests whether an implicit reference conversion exists between expr_type
167 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
169 Type expr_type = expr.Type;
172 // This is the boxed case.
174 if (target_type == TypeManager.object_type) {
175 if (expr_type.IsClass || expr_type.IsValueType ||
176 expr_type.IsInterface || expr_type == TypeManager.enum_type)
178 } else if (expr_type.IsSubclassOf (target_type))
181 // Please remember that all code below actually comes
182 // from ImplicitReferenceConversion so make sure code remains in sync
184 // from any class-type S to any interface-type T.
185 if (target_type.IsInterface) {
186 if (TypeManager.ImplementsInterface (expr_type, target_type))
190 // from any interface type S to interface-type T.
191 if (expr_type.IsInterface && target_type.IsInterface)
192 if (TypeManager.ImplementsInterface (expr_type, target_type))
195 // from an array-type S to an array-type of type T
196 if (expr_type.IsArray && target_type.IsArray) {
197 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
199 Type expr_element_type = expr_type.GetElementType ();
201 if (MyEmptyExpr == null)
202 MyEmptyExpr = new EmptyExpression ();
204 MyEmptyExpr.SetType (expr_element_type);
205 Type target_element_type = TypeManager.GetElementType (target_type);
207 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
208 if (ImplicitStandardConversionExists (MyEmptyExpr,
209 target_element_type))
214 // from an array-type to System.Array
215 if (expr_type.IsArray && (target_type == TypeManager.array_type))
218 // from any delegate type to System.Delegate
219 if ((expr_type == TypeManager.delegate_type ||
220 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
221 target_type == TypeManager.delegate_type)
222 if (target_type.IsAssignableFrom (expr_type))
225 // from any array-type or delegate type into System.ICloneable.
226 if (expr_type.IsArray ||
227 expr_type == TypeManager.delegate_type ||
228 expr_type.IsSubclassOf (TypeManager.delegate_type))
229 if (target_type == TypeManager.icloneable_type)
232 // from the null type to any reference-type.
233 if (expr is NullLiteral && !target_type.IsValueType &&
234 !TypeManager.IsEnumType (target_type))
242 /// Implicit Numeric Conversions.
244 /// expr is the expression to convert, returns a new expression of type
245 /// target_type or null if an implicit conversion is not possible.
247 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
248 Type target_type, Location loc)
250 Type expr_type = expr.Type;
253 // Attempt to do the implicit constant expression conversions
255 if (expr is Constant){
256 if (expr is IntConstant){
259 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
263 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
265 // Try the implicit constant expression conversion
266 // from long to ulong, instead of a nice routine,
269 long v = ((LongConstant) expr).Value;
271 return new ULongConstant ((ulong) v);
275 Type real_target_type = target_type;
277 if (expr_type == TypeManager.sbyte_type){
279 // From sbyte to short, int, long, float, double.
281 if (real_target_type == TypeManager.int32_type)
282 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
283 if (real_target_type == TypeManager.int64_type)
284 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
285 if (real_target_type == TypeManager.double_type)
286 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
287 if (real_target_type == TypeManager.float_type)
288 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
289 if (real_target_type == TypeManager.short_type)
290 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
291 } else if (expr_type == TypeManager.byte_type){
293 // From byte to short, ushort, int, uint, long, ulong, float, double
295 if ((real_target_type == TypeManager.short_type) ||
296 (real_target_type == TypeManager.ushort_type) ||
297 (real_target_type == TypeManager.int32_type) ||
298 (real_target_type == TypeManager.uint32_type))
299 return new EmptyCast (expr, target_type);
301 if (real_target_type == TypeManager.uint64_type)
302 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
303 if (real_target_type == TypeManager.int64_type)
304 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
305 if (real_target_type == TypeManager.float_type)
306 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
307 if (real_target_type == TypeManager.double_type)
308 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
309 } else if (expr_type == TypeManager.short_type){
311 // From short to int, long, float, double
313 if (real_target_type == TypeManager.int32_type)
314 return new EmptyCast (expr, target_type);
315 if (real_target_type == TypeManager.int64_type)
316 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
317 if (real_target_type == TypeManager.double_type)
318 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
319 if (real_target_type == TypeManager.float_type)
320 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
321 } else if (expr_type == TypeManager.ushort_type){
323 // From ushort to int, uint, long, ulong, float, double
325 if (real_target_type == TypeManager.uint32_type)
326 return new EmptyCast (expr, target_type);
328 if (real_target_type == TypeManager.uint64_type)
329 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
330 if (real_target_type == TypeManager.int32_type)
331 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
332 if (real_target_type == TypeManager.int64_type)
333 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
334 if (real_target_type == TypeManager.double_type)
335 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
336 if (real_target_type == TypeManager.float_type)
337 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
338 } else if (expr_type == TypeManager.int32_type){
340 // From int to long, float, double
342 if (real_target_type == TypeManager.int64_type)
343 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
344 if (real_target_type == TypeManager.double_type)
345 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
346 if (real_target_type == TypeManager.float_type)
347 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
348 } else if (expr_type == TypeManager.uint32_type){
350 // From uint to long, ulong, float, double
352 if (real_target_type == TypeManager.int64_type)
353 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
354 if (real_target_type == TypeManager.uint64_type)
355 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
356 if (real_target_type == TypeManager.double_type)
357 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
359 if (real_target_type == TypeManager.float_type)
360 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
362 } else if (expr_type == TypeManager.int64_type){
364 // From long/ulong to float, double
366 if (real_target_type == TypeManager.double_type)
367 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
368 if (real_target_type == TypeManager.float_type)
369 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
370 } else if (expr_type == TypeManager.uint64_type){
372 // From ulong to float, double
374 if (real_target_type == TypeManager.double_type)
375 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
377 if (real_target_type == TypeManager.float_type)
378 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
380 } else if (expr_type == TypeManager.char_type){
382 // From char to ushort, int, uint, long, ulong, float, double
384 if ((real_target_type == TypeManager.ushort_type) ||
385 (real_target_type == TypeManager.int32_type) ||
386 (real_target_type == TypeManager.uint32_type))
387 return new EmptyCast (expr, target_type);
388 if (real_target_type == TypeManager.uint64_type)
389 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
390 if (real_target_type == TypeManager.int64_type)
391 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
392 if (real_target_type == TypeManager.float_type)
393 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
394 if (real_target_type == TypeManager.double_type)
395 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
396 } else if (expr_type == TypeManager.float_type){
400 if (real_target_type == TypeManager.double_type)
401 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
409 /// Same as ImplicitStandardConversionExists except that it also looks at
410 /// implicit user defined conversions - needed for overload resolution
412 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
414 if (ImplicitStandardConversionExists (expr, target_type))
417 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
425 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
427 Expression dummy = ImplicitUserConversion (
428 ec, new EmptyExpression (source), target, Location.Null);
429 return dummy != null;
433 /// Determines if a standard implicit conversion exists from
434 /// expr_type to target_type
436 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
438 Type expr_type = expr.Type;
440 if (expr_type == TypeManager.void_type)
443 if (expr_type == target_type)
446 // First numeric conversions
448 if (expr_type == TypeManager.sbyte_type){
450 // From sbyte to short, int, long, float, double.
452 if ((target_type == TypeManager.int32_type) ||
453 (target_type == TypeManager.int64_type) ||
454 (target_type == TypeManager.double_type) ||
455 (target_type == TypeManager.float_type) ||
456 (target_type == TypeManager.short_type) ||
457 (target_type == TypeManager.decimal_type))
460 } else if (expr_type == TypeManager.byte_type){
462 // From byte to short, ushort, int, uint, long, ulong, float, double
464 if ((target_type == TypeManager.short_type) ||
465 (target_type == TypeManager.ushort_type) ||
466 (target_type == TypeManager.int32_type) ||
467 (target_type == TypeManager.uint32_type) ||
468 (target_type == TypeManager.uint64_type) ||
469 (target_type == TypeManager.int64_type) ||
470 (target_type == TypeManager.float_type) ||
471 (target_type == TypeManager.double_type) ||
472 (target_type == TypeManager.decimal_type))
475 } else if (expr_type == TypeManager.short_type){
477 // From short to int, long, float, double
479 if ((target_type == TypeManager.int32_type) ||
480 (target_type == TypeManager.int64_type) ||
481 (target_type == TypeManager.double_type) ||
482 (target_type == TypeManager.float_type) ||
483 (target_type == TypeManager.decimal_type))
486 } else if (expr_type == TypeManager.ushort_type){
488 // From ushort to int, uint, long, ulong, float, double
490 if ((target_type == TypeManager.uint32_type) ||
491 (target_type == TypeManager.uint64_type) ||
492 (target_type == TypeManager.int32_type) ||
493 (target_type == TypeManager.int64_type) ||
494 (target_type == TypeManager.double_type) ||
495 (target_type == TypeManager.float_type) ||
496 (target_type == TypeManager.decimal_type))
499 } else if (expr_type == TypeManager.int32_type){
501 // From int to long, float, double
503 if ((target_type == TypeManager.int64_type) ||
504 (target_type == TypeManager.double_type) ||
505 (target_type == TypeManager.float_type) ||
506 (target_type == TypeManager.decimal_type))
509 } else if (expr_type == TypeManager.uint32_type){
511 // From uint to long, ulong, float, double
513 if ((target_type == TypeManager.int64_type) ||
514 (target_type == TypeManager.uint64_type) ||
515 (target_type == TypeManager.double_type) ||
516 (target_type == TypeManager.float_type) ||
517 (target_type == TypeManager.decimal_type))
520 } else if ((expr_type == TypeManager.uint64_type) ||
521 (expr_type == TypeManager.int64_type)) {
523 // From long/ulong to float, double
525 if ((target_type == TypeManager.double_type) ||
526 (target_type == TypeManager.float_type) ||
527 (target_type == TypeManager.decimal_type))
530 } else if (expr_type == TypeManager.char_type){
532 // From char to ushort, int, uint, long, ulong, float, double
534 if ((target_type == TypeManager.ushort_type) ||
535 (target_type == TypeManager.int32_type) ||
536 (target_type == TypeManager.uint32_type) ||
537 (target_type == TypeManager.uint64_type) ||
538 (target_type == TypeManager.int64_type) ||
539 (target_type == TypeManager.float_type) ||
540 (target_type == TypeManager.double_type) ||
541 (target_type == TypeManager.decimal_type))
544 } else if (expr_type == TypeManager.float_type){
548 if (target_type == TypeManager.double_type)
552 if (ImplicitReferenceConversionExists (expr, target_type))
556 // Implicit Constant Expression Conversions
558 if (expr is IntConstant){
559 int value = ((IntConstant) expr).Value;
561 if (target_type == TypeManager.sbyte_type){
562 if (value >= SByte.MinValue && value <= SByte.MaxValue)
564 } else if (target_type == TypeManager.byte_type){
565 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
567 } else if (target_type == TypeManager.short_type){
568 if (value >= Int16.MinValue && value <= Int16.MaxValue)
570 } else if (target_type == TypeManager.ushort_type){
571 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
573 } else if (target_type == TypeManager.uint32_type){
576 } else if (target_type == TypeManager.uint64_type){
578 // we can optimize this case: a positive int32
579 // always fits on a uint64. But we need an opcode
586 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
590 if (expr is LongConstant && target_type == TypeManager.uint64_type){
592 // Try the implicit constant expression conversion
593 // from long to ulong, instead of a nice routine,
596 long v = ((LongConstant) expr).Value;
601 if ((target_type == TypeManager.enum_type ||
602 target_type.IsSubclassOf (TypeManager.enum_type)) &&
604 IntLiteral i = (IntLiteral) expr;
610 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
617 // Used internally by FindMostEncompassedType, this is used
618 // to avoid creating lots of objects in the tight loop inside
619 // FindMostEncompassedType
621 static EmptyExpression priv_fmet_param;
624 /// Finds "most encompassed type" according to the spec (13.4.2)
625 /// amongst the methods in the MethodGroupExpr
627 static Type FindMostEncompassedType (ArrayList types)
631 if (priv_fmet_param == null)
632 priv_fmet_param = new EmptyExpression ();
634 foreach (Type t in types){
635 priv_fmet_param.SetType (t);
642 if (ImplicitStandardConversionExists (priv_fmet_param, best))
650 // Used internally by FindMostEncompassingType, this is used
651 // to avoid creating lots of objects in the tight loop inside
652 // FindMostEncompassingType
654 static EmptyExpression priv_fmee_ret;
657 /// Finds "most encompassing type" according to the spec (13.4.2)
658 /// amongst the types in the given set
660 static Type FindMostEncompassingType (ArrayList types)
664 if (priv_fmee_ret == null)
665 priv_fmee_ret = new EmptyExpression ();
667 foreach (Type t in types){
668 priv_fmee_ret.SetType (best);
675 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
683 // Used to avoid creating too many objects
685 static EmptyExpression priv_fms_expr;
688 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
689 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
690 /// for explicit and implicit conversion operators.
692 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
693 bool apply_explicit_conv_rules,
696 ArrayList src_types_set = new ArrayList ();
698 if (priv_fms_expr == null)
699 priv_fms_expr = new EmptyExpression ();
702 // If any operator converts from S then Sx = S
704 Type source_type = source.Type;
705 foreach (MethodBase mb in me.Methods){
706 ParameterData pd = Invocation.GetParameterData (mb);
707 Type param_type = pd.ParameterType (0);
709 if (param_type == source_type)
712 if (apply_explicit_conv_rules) {
715 // Find the set of applicable user-defined conversion operators, U. This set
717 // user-defined implicit or explicit conversion operators declared by
718 // the classes or structs in D that convert from a type encompassing
719 // or encompassed by S to a type encompassing or encompassed by T
721 priv_fms_expr.SetType (param_type);
722 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
723 src_types_set.Add (param_type);
725 if (ImplicitStandardConversionExists (source, param_type))
726 src_types_set.Add (param_type);
730 // Only if S is encompassed by param_type
732 if (ImplicitStandardConversionExists (source, param_type))
733 src_types_set.Add (param_type);
738 // Explicit Conv rules
740 if (apply_explicit_conv_rules) {
741 ArrayList candidate_set = new ArrayList ();
743 foreach (Type param_type in src_types_set){
744 if (ImplicitStandardConversionExists (source, param_type))
745 candidate_set.Add (param_type);
748 if (candidate_set.Count != 0)
749 return FindMostEncompassedType (candidate_set);
755 if (apply_explicit_conv_rules)
756 return FindMostEncompassingType (src_types_set);
758 return FindMostEncompassedType (src_types_set);
762 // Useful in avoiding proliferation of objects
764 static EmptyExpression priv_fmt_expr;
767 /// Finds the most specific target Tx according to section 13.4.4
769 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
770 bool apply_explicit_conv_rules,
773 ArrayList tgt_types_set = new ArrayList ();
775 if (priv_fmt_expr == null)
776 priv_fmt_expr = new EmptyExpression ();
779 // If any operator converts to T then Tx = T
781 foreach (MethodInfo mi in me.Methods){
782 Type ret_type = mi.ReturnType;
784 if (ret_type == target)
787 if (apply_explicit_conv_rules) {
790 // Find the set of applicable user-defined conversion operators, U.
792 // This set consists of the
793 // user-defined implicit or explicit conversion operators declared by
794 // the classes or structs in D that convert from a type encompassing
795 // or encompassed by S to a type encompassing or encompassed by T
797 priv_fms_expr.SetType (ret_type);
798 if (ImplicitStandardConversionExists (priv_fms_expr, target))
799 tgt_types_set.Add (ret_type);
801 priv_fms_expr.SetType (target);
802 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
803 tgt_types_set.Add (ret_type);
807 // Only if T is encompassed by param_type
809 priv_fms_expr.SetType (ret_type);
810 if (ImplicitStandardConversionExists (priv_fms_expr, target))
811 tgt_types_set.Add (ret_type);
816 // Explicit conv rules
818 if (apply_explicit_conv_rules) {
819 ArrayList candidate_set = new ArrayList ();
821 foreach (Type ret_type in tgt_types_set){
822 priv_fmt_expr.SetType (ret_type);
824 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
825 candidate_set.Add (ret_type);
828 if (candidate_set.Count != 0)
829 return FindMostEncompassingType (candidate_set);
833 // Okay, final case !
835 if (apply_explicit_conv_rules)
836 return FindMostEncompassedType (tgt_types_set);
838 return FindMostEncompassingType (tgt_types_set);
842 /// User-defined Implicit conversions
844 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
845 Type target, Location loc)
847 return UserDefinedConversion (ec, source, target, loc, false);
851 /// User-defined Explicit conversions
853 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
854 Type target, Location loc)
856 return UserDefinedConversion (ec, source, target, loc, true);
859 static DoubleHash explicit_conv = new DoubleHash (100);
860 static DoubleHash implicit_conv = new DoubleHash (100);
862 /// Computes the MethodGroup for the user-defined conversion
863 /// operators from source_type to target_type. `look_for_explicit'
864 /// controls whether we should also include the list of explicit
867 static MethodGroupExpr GetConversionOperators (EmitContext ec,
868 Type source_type, Type target_type,
869 Location loc, bool look_for_explicit)
871 Expression mg1 = null, mg2 = null;
872 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
875 op_name = "op_Implicit";
877 MethodGroupExpr union3;
879 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
880 return (MethodGroupExpr) r;
882 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
883 if (source_type.BaseType != null)
884 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
887 union3 = (MethodGroupExpr) mg2;
888 else if (mg2 == null)
889 union3 = (MethodGroupExpr) mg1;
891 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
893 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
896 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
898 union3 = (MethodGroupExpr) mg1;
901 if (target_type.BaseType != null)
902 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
906 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
908 union3 = (MethodGroupExpr) mg1;
911 MethodGroupExpr union4 = null;
913 if (look_for_explicit) {
914 op_name = "op_Explicit";
916 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
917 if (source_type.BaseType != null)
918 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
920 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
921 if (target_type.BaseType != null)
922 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
924 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
925 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
927 union4 = Invocation.MakeUnionSet (union5, union6, loc);
930 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
931 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
936 /// User-defined conversions
938 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
939 Type target, Location loc,
940 bool look_for_explicit)
942 MethodGroupExpr union;
943 Type source_type = source.Type;
944 MethodBase method = null;
946 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
950 Type most_specific_source, most_specific_target;
952 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
953 if (most_specific_source == null)
956 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
957 if (most_specific_target == null)
963 foreach (MethodBase mb in union.Methods){
964 ParameterData pd = Invocation.GetParameterData (mb);
965 MethodInfo mi = (MethodInfo) mb;
967 if (pd.ParameterType (0) == most_specific_source &&
968 mi.ReturnType == most_specific_target) {
974 if (method == null || count > 1)
979 // This will do the conversion to the best match that we
980 // found. Now we need to perform an implict standard conversion
981 // if the best match was not the type that we were requested
984 if (look_for_explicit)
985 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
987 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
993 e = new UserCast ((MethodInfo) method, source, loc);
994 if (e.Type != target){
995 if (!look_for_explicit)
996 e = ImplicitConversionStandard (ec, e, target, loc);
998 e = ExplicitConversionStandard (ec, e, target, loc);
1005 /// Converts implicitly the resolved expression `expr' into the
1006 /// `target_type'. It returns a new expression that can be used
1007 /// in a context that expects a `target_type'.
1009 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1010 Type target_type, Location loc)
1014 if (target_type == null)
1015 throw new Exception ("Target type is null");
1017 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1021 e = ImplicitUserConversion (ec, expr, target_type, loc);
1030 /// Attempts to apply the `Standard Implicit
1031 /// Conversion' rules to the expression `expr' into
1032 /// the `target_type'. It returns a new expression
1033 /// that can be used in a context that expects a
1036 /// This is different from `ImplicitConversion' in that the
1037 /// user defined implicit conversions are excluded.
1039 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1040 Type target_type, Location loc)
1042 Type expr_type = expr.Type;
1045 if (expr.eclass == ExprClass.MethodGroup){
1046 if (!TypeManager.IsDelegateType (target_type)){
1047 Report.Error (428, loc,
1049 "Cannot convert method group to `{0}', since it is not a delegate",
1050 TypeManager.CSharpName (target_type)));
1054 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1057 if (expr_type == target_type && !(expr is NullLiteral))
1060 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1064 e = ImplicitReferenceConversion (expr, target_type);
1068 if ((target_type == TypeManager.enum_type ||
1069 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1070 expr is IntLiteral){
1071 IntLiteral i = (IntLiteral) expr;
1074 return new EnumConstant ((Constant) expr, target_type);
1078 if (expr_type.IsPointer){
1079 if (target_type == TypeManager.void_ptr_type)
1080 return new EmptyCast (expr, target_type);
1083 // yep, comparing pointer types cant be done with
1084 // t1 == t2, we have to compare their element types.
1086 if (target_type.IsPointer){
1087 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1092 if (target_type.IsPointer) {
1093 if (expr is NullLiteral)
1094 return new EmptyCast (expr, target_type);
1096 if (expr_type == TypeManager.void_ptr_type)
1097 return new EmptyCast (expr, target_type);
1105 /// Attemps to perform an implict constant conversion of the IntConstant
1106 /// into a different data type using casts (See Implicit Constant
1107 /// Expression Conversions)
1109 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1111 int value = ic.Value;
1113 if (target_type == TypeManager.sbyte_type){
1114 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1115 return new SByteConstant ((sbyte) value);
1116 } else if (target_type == TypeManager.byte_type){
1117 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1118 return new ByteConstant ((byte) value);
1119 } else if (target_type == TypeManager.short_type){
1120 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1121 return new ShortConstant ((short) value);
1122 } else if (target_type == TypeManager.ushort_type){
1123 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1124 return new UShortConstant ((ushort) value);
1125 } else if (target_type == TypeManager.uint32_type){
1127 return new UIntConstant ((uint) value);
1128 } else if (target_type == TypeManager.uint64_type){
1130 // we can optimize this case: a positive int32
1131 // always fits on a uint64. But we need an opcode
1135 return new ULongConstant ((ulong) value);
1136 } else if (target_type == TypeManager.double_type)
1137 return new DoubleConstant ((double) value);
1138 else if (target_type == TypeManager.float_type)
1139 return new FloatConstant ((float) value);
1141 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1142 Type underlying = TypeManager.EnumToUnderlying (target_type);
1143 Constant e = (Constant) ic;
1146 // Possibly, we need to create a different 0 literal before passing
1149 if (underlying == TypeManager.int64_type)
1150 e = new LongLiteral (0);
1151 else if (underlying == TypeManager.uint64_type)
1152 e = new ULongLiteral (0);
1154 return new EnumConstant (e, target_type);
1159 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1161 string msg = "Cannot convert implicitly from `"+
1162 TypeManager.CSharpName (source) + "' to `" +
1163 TypeManager.CSharpName (target) + "'";
1165 Report.Error (29, loc, msg);
1169 /// Attemptes to implicityly convert `target' into `type', using
1170 /// ImplicitConversion. If there is no implicit conversion, then
1171 /// an error is signaled
1173 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1174 Type target_type, Location loc)
1178 e = ImplicitConversion (ec, source, target_type, loc);
1182 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1183 Report.Error (664, loc,
1184 "Double literal cannot be implicitly converted to " +
1185 "float type, use F suffix to create a float literal");
1188 if (source is Constant){
1189 Constant c = (Constant) source;
1191 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1195 Error_CannotImplicitConversion (loc, source.Type, target_type);
1201 /// Performs the explicit numeric conversions
1203 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1205 Type expr_type = expr.Type;
1208 // If we have an enumeration, extract the underlying type,
1209 // use this during the comparison, but wrap around the original
1212 Type real_target_type = target_type;
1214 if (TypeManager.IsEnumType (real_target_type))
1215 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1217 if (ImplicitStandardConversionExists (expr, real_target_type)){
1218 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1220 if (real_target_type != target_type)
1221 return new EmptyCast (ce, target_type);
1225 if (expr_type == TypeManager.sbyte_type){
1227 // From sbyte to byte, ushort, uint, ulong, char
1229 if (real_target_type == TypeManager.byte_type)
1230 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1231 if (real_target_type == TypeManager.ushort_type)
1232 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1233 if (real_target_type == TypeManager.uint32_type)
1234 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1235 if (real_target_type == TypeManager.uint64_type)
1236 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1237 if (real_target_type == TypeManager.char_type)
1238 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1239 } else if (expr_type == TypeManager.byte_type){
1241 // From byte to sbyte and char
1243 if (real_target_type == TypeManager.sbyte_type)
1244 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1245 if (real_target_type == TypeManager.char_type)
1246 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1247 } else if (expr_type == TypeManager.short_type){
1249 // From short to sbyte, byte, ushort, uint, ulong, char
1251 if (real_target_type == TypeManager.sbyte_type)
1252 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1253 if (real_target_type == TypeManager.byte_type)
1254 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1255 if (real_target_type == TypeManager.ushort_type)
1256 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1257 if (real_target_type == TypeManager.uint32_type)
1258 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1259 if (real_target_type == TypeManager.uint64_type)
1260 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1261 if (real_target_type == TypeManager.char_type)
1262 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1263 } else if (expr_type == TypeManager.ushort_type){
1265 // From ushort to sbyte, byte, short, char
1267 if (real_target_type == TypeManager.sbyte_type)
1268 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1269 if (real_target_type == TypeManager.byte_type)
1270 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1271 if (real_target_type == TypeManager.short_type)
1272 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1273 if (real_target_type == TypeManager.char_type)
1274 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1275 } else if (expr_type == TypeManager.int32_type){
1277 // From int to sbyte, byte, short, ushort, uint, ulong, char
1279 if (real_target_type == TypeManager.sbyte_type)
1280 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1281 if (real_target_type == TypeManager.byte_type)
1282 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1283 if (real_target_type == TypeManager.short_type)
1284 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1285 if (real_target_type == TypeManager.ushort_type)
1286 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1287 if (real_target_type == TypeManager.uint32_type)
1288 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1289 if (real_target_type == TypeManager.uint64_type)
1290 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1291 if (real_target_type == TypeManager.char_type)
1292 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1293 } else if (expr_type == TypeManager.uint32_type){
1295 // From uint to sbyte, byte, short, ushort, int, char
1297 if (real_target_type == TypeManager.sbyte_type)
1298 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1299 if (real_target_type == TypeManager.byte_type)
1300 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1301 if (real_target_type == TypeManager.short_type)
1302 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1303 if (real_target_type == TypeManager.ushort_type)
1304 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1305 if (real_target_type == TypeManager.int32_type)
1306 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1307 if (real_target_type == TypeManager.char_type)
1308 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1309 } else if (expr_type == TypeManager.int64_type){
1311 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1313 if (real_target_type == TypeManager.sbyte_type)
1314 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1315 if (real_target_type == TypeManager.byte_type)
1316 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1317 if (real_target_type == TypeManager.short_type)
1318 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1319 if (real_target_type == TypeManager.ushort_type)
1320 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1321 if (real_target_type == TypeManager.int32_type)
1322 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1323 if (real_target_type == TypeManager.uint32_type)
1324 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1325 if (real_target_type == TypeManager.uint64_type)
1326 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1327 if (real_target_type == TypeManager.char_type)
1328 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1329 } else if (expr_type == TypeManager.uint64_type){
1331 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1333 if (real_target_type == TypeManager.sbyte_type)
1334 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1335 if (real_target_type == TypeManager.byte_type)
1336 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1337 if (real_target_type == TypeManager.short_type)
1338 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1339 if (real_target_type == TypeManager.ushort_type)
1340 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1341 if (real_target_type == TypeManager.int32_type)
1342 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1343 if (real_target_type == TypeManager.uint32_type)
1344 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1345 if (real_target_type == TypeManager.int64_type)
1346 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1347 if (real_target_type == TypeManager.char_type)
1348 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1349 } else if (expr_type == TypeManager.char_type){
1351 // From char to sbyte, byte, short
1353 if (real_target_type == TypeManager.sbyte_type)
1354 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1355 if (real_target_type == TypeManager.byte_type)
1356 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1357 if (real_target_type == TypeManager.short_type)
1358 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1359 } else if (expr_type == TypeManager.float_type){
1361 // From float to sbyte, byte, short,
1362 // ushort, int, uint, long, ulong, char
1365 if (real_target_type == TypeManager.sbyte_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1367 if (real_target_type == TypeManager.byte_type)
1368 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1369 if (real_target_type == TypeManager.short_type)
1370 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1371 if (real_target_type == TypeManager.ushort_type)
1372 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1373 if (real_target_type == TypeManager.int32_type)
1374 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1375 if (real_target_type == TypeManager.uint32_type)
1376 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1377 if (real_target_type == TypeManager.int64_type)
1378 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1379 if (real_target_type == TypeManager.uint64_type)
1380 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1381 if (real_target_type == TypeManager.char_type)
1382 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1383 } else if (expr_type == TypeManager.double_type){
1385 // From double to byte, byte, short,
1386 // ushort, int, uint, long, ulong,
1387 // char, float or decimal
1389 if (real_target_type == TypeManager.sbyte_type)
1390 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1391 if (real_target_type == TypeManager.byte_type)
1392 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1393 if (real_target_type == TypeManager.short_type)
1394 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1395 if (real_target_type == TypeManager.ushort_type)
1396 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1397 if (real_target_type == TypeManager.int32_type)
1398 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1399 if (real_target_type == TypeManager.uint32_type)
1400 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1401 if (real_target_type == TypeManager.int64_type)
1402 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1403 if (real_target_type == TypeManager.uint64_type)
1404 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1405 if (real_target_type == TypeManager.char_type)
1406 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1407 if (real_target_type == TypeManager.float_type)
1408 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1411 // decimal is taken care of by the op_Explicit methods.
1417 /// Returns whether an explicit reference conversion can be performed
1418 /// from source_type to target_type
1420 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1422 bool target_is_value_type = target_type.IsValueType;
1424 if (source_type == target_type)
1428 // From object to any reference type
1430 if (source_type == TypeManager.object_type && !target_is_value_type)
1434 // From any class S to any class-type T, provided S is a base class of T
1436 if (target_type.IsSubclassOf (source_type))
1440 // From any interface type S to any interface T provided S is not derived from T
1442 if (source_type.IsInterface && target_type.IsInterface){
1443 if (!target_type.IsSubclassOf (source_type))
1448 // From any class type S to any interface T, provided S is not sealed
1449 // and provided S does not implement T.
1451 if (target_type.IsInterface && !source_type.IsSealed &&
1452 !TypeManager.ImplementsInterface (source_type, target_type))
1456 // From any interface-type S to to any class type T, provided T is not
1457 // sealed, or provided T implements S.
1459 if (source_type.IsInterface &&
1460 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1464 // From an array type S with an element type Se to an array type T with an
1465 // element type Te provided all the following are true:
1466 // * S and T differe only in element type, in other words, S and T
1467 // have the same number of dimensions.
1468 // * Both Se and Te are reference types
1469 // * An explicit referenc conversions exist from Se to Te
1471 if (source_type.IsArray && target_type.IsArray) {
1472 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1474 Type source_element_type = TypeManager.GetElementType (source_type);
1475 Type target_element_type = TypeManager.GetElementType (target_type);
1477 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1478 if (ExplicitReferenceConversionExists (source_element_type,
1479 target_element_type))
1485 // From System.Array to any array-type
1486 if (source_type == TypeManager.array_type &&
1487 target_type.IsArray){
1492 // From System delegate to any delegate-type
1494 if (source_type == TypeManager.delegate_type &&
1495 target_type.IsSubclassOf (TypeManager.delegate_type))
1499 // From ICloneable to Array or Delegate types
1501 if (source_type == TypeManager.icloneable_type &&
1502 (target_type == TypeManager.array_type ||
1503 target_type == TypeManager.delegate_type))
1510 /// Implements Explicit Reference conversions
1512 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1514 Type source_type = source.Type;
1515 bool target_is_value_type = target_type.IsValueType;
1518 // From object to any reference type
1520 if (source_type == TypeManager.object_type && !target_is_value_type)
1521 return new ClassCast (source, target_type);
1524 // Unboxing conversion.
1526 if (((source_type == TypeManager.enum_type &&
1527 !(source is EmptyCast)) ||
1528 source_type == TypeManager.value_type) && target_is_value_type)
1529 return new UnboxCast (source, target_type);
1532 // From any class S to any class-type T, provided S is a base class of T
1534 if (target_type.IsSubclassOf (source_type))
1535 return new ClassCast (source, target_type);
1538 // From any interface type S to any interface T provided S is not derived from T
1540 if (source_type.IsInterface && target_type.IsInterface){
1541 if (TypeManager.ImplementsInterface (source_type, target_type))
1544 return new ClassCast (source, target_type);
1548 // From any class type S to any interface T, provides S is not sealed
1549 // and provided S does not implement T.
1551 if (target_type.IsInterface && !source_type.IsSealed) {
1552 if (TypeManager.ImplementsInterface (source_type, target_type))
1555 return new ClassCast (source, target_type);
1560 // From any interface-type S to to any class type T, provided T is not
1561 // sealed, or provided T implements S.
1563 if (source_type.IsInterface) {
1564 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1565 if (target_type.IsClass)
1566 return new ClassCast (source, target_type);
1568 return new UnboxCast (source, target_type);
1574 // From an array type S with an element type Se to an array type T with an
1575 // element type Te provided all the following are true:
1576 // * S and T differe only in element type, in other words, S and T
1577 // have the same number of dimensions.
1578 // * Both Se and Te are reference types
1579 // * An explicit referenc conversions exist from Se to Te
1581 if (source_type.IsArray && target_type.IsArray) {
1582 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1584 Type source_element_type = TypeManager.GetElementType (source_type);
1585 Type target_element_type = TypeManager.GetElementType (target_type);
1587 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1588 if (ExplicitReferenceConversionExists (source_element_type,
1589 target_element_type))
1590 return new ClassCast (source, target_type);
1595 // From System.Array to any array-type
1596 if (source_type == TypeManager.array_type &&
1597 target_type.IsArray) {
1598 return new ClassCast (source, target_type);
1602 // From System delegate to any delegate-type
1604 if (source_type == TypeManager.delegate_type &&
1605 target_type.IsSubclassOf (TypeManager.delegate_type))
1606 return new ClassCast (source, target_type);
1609 // From ICloneable to Array or Delegate types
1611 if (source_type == TypeManager.icloneable_type &&
1612 (target_type == TypeManager.array_type ||
1613 target_type == TypeManager.delegate_type))
1614 return new ClassCast (source, target_type);
1620 /// Performs an explicit conversion of the expression `expr' whose
1621 /// type is expr.Type to `target_type'.
1623 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1624 Type target_type, Location loc)
1626 Type expr_type = expr.Type;
1627 Type original_expr_type = expr_type;
1629 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1630 if (target_type == TypeManager.enum_type ||
1631 target_type == TypeManager.object_type) {
1632 if (expr is EnumConstant)
1633 expr = ((EnumConstant) expr).Child;
1634 // We really need all these casts here .... :-(
1635 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1636 return new EmptyCast (expr, target_type);
1637 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1638 target_type.IsSubclassOf (TypeManager.enum_type))
1639 return new UnboxCast (expr, target_type);
1642 // Notice that we have kept the expr_type unmodified, which is only
1644 if (expr is EnumConstant)
1645 expr = ((EnumConstant) expr).Child;
1647 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1648 expr_type = expr.Type;
1651 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1656 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1661 // Unboxing conversion.
1663 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1664 if (expr is NullLiteral){
1666 // Skip the ExplicitReferenceConversion because we can not convert
1667 // from Null to a ValueType, and ExplicitReference wont check against
1668 // null literal explicitly
1672 return new UnboxCast (expr, target_type);
1676 ne = ExplicitReferenceConversion (expr, target_type);
1682 if (target_type.IsPointer){
1683 if (expr_type.IsPointer)
1684 return new EmptyCast (expr, target_type);
1686 if (expr_type == TypeManager.sbyte_type ||
1687 expr_type == TypeManager.byte_type ||
1688 expr_type == TypeManager.short_type ||
1689 expr_type == TypeManager.ushort_type ||
1690 expr_type == TypeManager.int32_type ||
1691 expr_type == TypeManager.uint32_type ||
1692 expr_type == TypeManager.uint64_type ||
1693 expr_type == TypeManager.int64_type)
1694 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1696 if (expr_type.IsPointer){
1697 Expression e = null;
1699 if (target_type == TypeManager.sbyte_type)
1700 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1701 else if (target_type == TypeManager.byte_type)
1702 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1703 else if (target_type == TypeManager.short_type)
1704 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1705 else if (target_type == TypeManager.ushort_type)
1706 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1707 else if (target_type == TypeManager.int32_type)
1708 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1709 else if (target_type == TypeManager.uint32_type)
1710 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1711 else if (target_type == TypeManager.uint64_type)
1712 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1713 else if (target_type == TypeManager.int64_type){
1714 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1720 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1725 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1729 // We should always be able to go from an uint32
1730 // implicitly or explicitly to the other integral
1733 throw new Exception ("Internal compiler error");
1738 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1742 if (expr is NullLiteral){
1743 Report.Error (37, loc, "Cannot convert null to value type `" +
1744 TypeManager.CSharpName (target_type) + "'");
1748 Error_CannotConvertType (loc, original_expr_type, target_type);
1753 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1755 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1756 Type target_type, Location l)
1758 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1763 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1767 ne = ExplicitReferenceConversion (expr, target_type);
1771 Error_CannotConvertType (l, expr.Type, target_type);