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 BoxedCast (expr);
63 } else if (expr_type.IsSubclassOf (target_type)) {
65 // Special case: enumeration to System.Enum.
66 // System.Enum is not a value type, it is a class, so we need
67 // a boxing conversion
70 return new BoxedCast (expr);
72 return new EmptyCast (expr, target_type);
75 // This code is kind of mirrored inside ImplicitStandardConversionExists
76 // with the small distinction that we only probe there
78 // Always ensure that the code here and there is in sync
80 // from the null type to any reference-type.
81 if (expr is NullLiteral){
82 if (target_type.IsPointer)
83 return NullPointer.Null;
85 if (!target_type.IsValueType)
86 return new NullCast (expr, target_type);
89 // from any class-type S to any interface-type T.
90 if (target_type.IsInterface) {
91 if (TypeManager.ImplementsInterface (expr_type, target_type)){
92 if (expr_type.IsClass)
93 return new EmptyCast (expr, target_type);
94 else if (expr_type.IsValueType)
95 return new BoxedCast (expr, target_type);
99 // from any interface type S to interface-type T.
100 if (expr_type.IsInterface && target_type.IsInterface) {
101 if (TypeManager.ImplementsInterface (expr_type, target_type))
102 return new EmptyCast (expr, target_type);
107 // from an array-type S to an array-type of type T
108 if (expr_type.IsArray && target_type.IsArray) {
109 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
111 Type expr_element_type = TypeManager.GetElementType (expr_type);
113 if (MyEmptyExpr == null)
114 MyEmptyExpr = new EmptyExpression ();
116 MyEmptyExpr.SetType (expr_element_type);
117 Type target_element_type = TypeManager.GetElementType (target_type);
119 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
120 if (ImplicitStandardConversionExists (MyEmptyExpr,
121 target_element_type))
122 return new EmptyCast (expr, target_type);
127 // from an array-type to System.Array
128 if (expr_type.IsArray && target_type == TypeManager.array_type)
129 return new EmptyCast (expr, target_type);
131 // from any delegate type to System.Delegate
132 if ((expr_type == TypeManager.delegate_type ||
133 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
134 target_type == TypeManager.delegate_type)
135 return new EmptyCast (expr, target_type);
137 // from any array-type or delegate type into System.ICloneable.
138 if (expr_type.IsArray ||
139 expr_type == TypeManager.delegate_type ||
140 expr_type.IsSubclassOf (TypeManager.delegate_type))
141 if (target_type == TypeManager.icloneable_type)
142 return new EmptyCast (expr, target_type);
152 // Tests whether an implicit reference conversion exists between expr_type
155 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
157 Type expr_type = expr.Type;
160 // This is the boxed case.
162 if (target_type == TypeManager.object_type) {
163 if (expr_type.IsClass || expr_type.IsValueType ||
164 expr_type.IsInterface || expr_type == TypeManager.enum_type)
166 } else if (expr_type.IsSubclassOf (target_type)) {
169 // Please remember that all code below actually comes
170 // from ImplicitReferenceConversion so make sure code remains in sync
172 // from any class-type S to any interface-type T.
173 if (target_type.IsInterface) {
174 if (TypeManager.ImplementsInterface (expr_type, target_type))
178 // from any interface type S to interface-type T.
179 if (expr_type.IsInterface && target_type.IsInterface)
180 if (TypeManager.ImplementsInterface (expr_type, target_type))
183 // from an array-type S to an array-type of type T
184 if (expr_type.IsArray && target_type.IsArray) {
185 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
187 Type expr_element_type = expr_type.GetElementType ();
189 if (MyEmptyExpr == null)
190 MyEmptyExpr = new EmptyExpression ();
192 MyEmptyExpr.SetType (expr_element_type);
193 Type target_element_type = TypeManager.GetElementType (target_type);
195 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
196 if (ImplicitStandardConversionExists (MyEmptyExpr,
197 target_element_type))
202 // from an array-type to System.Array
203 if (expr_type.IsArray && (target_type == TypeManager.array_type))
206 // from any delegate type to System.Delegate
207 if ((expr_type == TypeManager.delegate_type ||
208 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
209 target_type == TypeManager.delegate_type)
210 if (target_type.IsAssignableFrom (expr_type))
213 // from any array-type or delegate type into System.ICloneable.
214 if (expr_type.IsArray ||
215 expr_type == TypeManager.delegate_type ||
216 expr_type.IsSubclassOf (TypeManager.delegate_type))
217 if (target_type == TypeManager.icloneable_type)
220 // from the null type to any reference-type.
221 if (expr is NullLiteral && !target_type.IsValueType &&
222 !TypeManager.IsEnumType (target_type))
230 /// Implicit Numeric Conversions.
232 /// expr is the expression to convert, returns a new expression of type
233 /// target_type or null if an implicit conversion is not possible.
235 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
236 Type target_type, Location loc)
238 Type expr_type = expr.Type;
241 // Attempt to do the implicit constant expression conversions
243 if (expr is Constant){
245 if (expr is IntConstant){
248 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
252 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
254 // Try the implicit constant expression conversion
255 // from long to ulong, instead of a nice routine,
258 long v = ((LongConstant) expr).Value;
260 return new ULongConstant ((ulong) v);
264 Type real_target_type = target_type;
266 if (expr_type == TypeManager.sbyte_type){
268 // From sbyte to short, int, long, float, double.
270 if (real_target_type == TypeManager.int32_type)
271 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
272 if (real_target_type == TypeManager.int64_type)
273 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
274 if (real_target_type == TypeManager.double_type)
275 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
276 if (real_target_type == TypeManager.float_type)
277 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
278 if (real_target_type == TypeManager.short_type)
279 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
280 } else if (expr_type == TypeManager.byte_type){
282 // From byte to short, ushort, int, uint, long, ulong, float, double
284 if ((real_target_type == TypeManager.short_type) ||
285 (real_target_type == TypeManager.ushort_type) ||
286 (real_target_type == TypeManager.int32_type) ||
287 (real_target_type == TypeManager.uint32_type))
288 return new EmptyCast (expr, target_type);
290 if (real_target_type == TypeManager.uint64_type)
291 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
292 if (real_target_type == TypeManager.int64_type)
293 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
294 if (real_target_type == TypeManager.float_type)
295 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
296 if (real_target_type == TypeManager.double_type)
297 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
298 } else if (expr_type == TypeManager.short_type){
300 // From short to int, long, float, double
302 if (real_target_type == TypeManager.int32_type)
303 return new EmptyCast (expr, target_type);
304 if (real_target_type == TypeManager.int64_type)
305 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
306 if (real_target_type == TypeManager.double_type)
307 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
308 if (real_target_type == TypeManager.float_type)
309 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
310 } else if (expr_type == TypeManager.ushort_type){
312 // From ushort to int, uint, long, ulong, float, double
314 if (real_target_type == TypeManager.uint32_type)
315 return new EmptyCast (expr, target_type);
317 if (real_target_type == TypeManager.uint64_type)
318 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
319 if (real_target_type == TypeManager.int32_type)
320 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
321 if (real_target_type == TypeManager.int64_type)
322 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
323 if (real_target_type == TypeManager.double_type)
324 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
325 if (real_target_type == TypeManager.float_type)
326 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
327 } else if (expr_type == TypeManager.int32_type){
329 // From int to long, float, double
331 if (real_target_type == TypeManager.int64_type)
332 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
333 if (real_target_type == TypeManager.double_type)
334 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
335 if (real_target_type == TypeManager.float_type)
336 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
337 } else if (expr_type == TypeManager.uint32_type){
339 // From uint to long, ulong, float, double
341 if (real_target_type == TypeManager.int64_type)
342 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
343 if (real_target_type == TypeManager.uint64_type)
344 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
345 if (real_target_type == TypeManager.double_type)
346 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
348 if (real_target_type == TypeManager.float_type)
349 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
351 } else if (expr_type == TypeManager.int64_type){
353 // From long/ulong to float, double
355 if (real_target_type == TypeManager.double_type)
356 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
357 if (real_target_type == TypeManager.float_type)
358 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
359 } else if (expr_type == TypeManager.uint64_type){
361 // From ulong to float, double
363 if (real_target_type == TypeManager.double_type)
364 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
366 if (real_target_type == TypeManager.float_type)
367 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
369 } else if (expr_type == TypeManager.char_type){
371 // From char to ushort, int, uint, long, ulong, float, double
373 if ((real_target_type == TypeManager.ushort_type) ||
374 (real_target_type == TypeManager.int32_type) ||
375 (real_target_type == TypeManager.uint32_type))
376 return new EmptyCast (expr, target_type);
377 if (real_target_type == TypeManager.uint64_type)
378 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
379 if (real_target_type == TypeManager.int64_type)
380 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
381 if (real_target_type == TypeManager.float_type)
382 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
383 if (real_target_type == TypeManager.double_type)
384 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
385 } else if (expr_type == TypeManager.float_type){
389 if (real_target_type == TypeManager.double_type)
390 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
398 /// Same as ImplicitStandardConversionExists except that it also looks at
399 /// implicit user defined conversions - needed for overload resolution
401 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
403 if (ImplicitStandardConversionExists (expr, target_type))
406 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
414 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
416 Expression dummy = ImplicitUserConversion (
417 ec, new EmptyExpression (source), target, Location.Null);
418 return dummy != null;
422 /// Determines if a standard implicit conversion exists from
423 /// expr_type to target_type
425 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
427 Type expr_type = expr.Type;
429 if (expr_type == TypeManager.void_type)
432 if (expr_type == target_type)
435 // First numeric conversions
437 if (expr_type == TypeManager.sbyte_type){
439 // From sbyte to short, int, long, float, double.
441 if ((target_type == TypeManager.int32_type) ||
442 (target_type == TypeManager.int64_type) ||
443 (target_type == TypeManager.double_type) ||
444 (target_type == TypeManager.float_type) ||
445 (target_type == TypeManager.short_type) ||
446 (target_type == TypeManager.decimal_type))
449 } else if (expr_type == TypeManager.byte_type){
451 // From byte to short, ushort, int, uint, long, ulong, float, double
453 if ((target_type == TypeManager.short_type) ||
454 (target_type == TypeManager.ushort_type) ||
455 (target_type == TypeManager.int32_type) ||
456 (target_type == TypeManager.uint32_type) ||
457 (target_type == TypeManager.uint64_type) ||
458 (target_type == TypeManager.int64_type) ||
459 (target_type == TypeManager.float_type) ||
460 (target_type == TypeManager.double_type) ||
461 (target_type == TypeManager.decimal_type))
464 } else if (expr_type == TypeManager.short_type){
466 // From short to int, long, float, double
468 if ((target_type == TypeManager.int32_type) ||
469 (target_type == TypeManager.int64_type) ||
470 (target_type == TypeManager.double_type) ||
471 (target_type == TypeManager.float_type) ||
472 (target_type == TypeManager.decimal_type))
475 } else if (expr_type == TypeManager.ushort_type){
477 // From ushort to int, uint, long, ulong, float, double
479 if ((target_type == TypeManager.uint32_type) ||
480 (target_type == TypeManager.uint64_type) ||
481 (target_type == TypeManager.int32_type) ||
482 (target_type == TypeManager.int64_type) ||
483 (target_type == TypeManager.double_type) ||
484 (target_type == TypeManager.float_type) ||
485 (target_type == TypeManager.decimal_type))
488 } else if (expr_type == TypeManager.int32_type){
490 // From int to long, float, double
492 if ((target_type == TypeManager.int64_type) ||
493 (target_type == TypeManager.double_type) ||
494 (target_type == TypeManager.float_type) ||
495 (target_type == TypeManager.decimal_type))
498 } else if (expr_type == TypeManager.uint32_type){
500 // From uint to long, ulong, float, double
502 if ((target_type == TypeManager.int64_type) ||
503 (target_type == TypeManager.uint64_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.uint64_type) ||
510 (expr_type == TypeManager.int64_type)) {
512 // From long/ulong to float, double
514 if ((target_type == TypeManager.double_type) ||
515 (target_type == TypeManager.float_type) ||
516 (target_type == TypeManager.decimal_type))
519 } else if (expr_type == TypeManager.char_type){
521 // From char to ushort, int, uint, long, ulong, float, double
523 if ((target_type == TypeManager.ushort_type) ||
524 (target_type == TypeManager.int32_type) ||
525 (target_type == TypeManager.uint32_type) ||
526 (target_type == TypeManager.uint64_type) ||
527 (target_type == TypeManager.int64_type) ||
528 (target_type == TypeManager.float_type) ||
529 (target_type == TypeManager.double_type) ||
530 (target_type == TypeManager.decimal_type))
533 } else if (expr_type == TypeManager.float_type){
537 if (target_type == TypeManager.double_type)
541 if (ImplicitReferenceConversionExists (expr, target_type))
545 // Implicit Constant Expression Conversions
547 if (expr is IntConstant){
548 int value = ((IntConstant) expr).Value;
550 if (target_type == TypeManager.sbyte_type){
551 if (value >= SByte.MinValue && value <= SByte.MaxValue)
553 } else if (target_type == TypeManager.byte_type){
554 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
556 } else if (target_type == TypeManager.short_type){
557 if (value >= Int16.MinValue && value <= Int16.MaxValue)
559 } else if (target_type == TypeManager.ushort_type){
560 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
562 } else if (target_type == TypeManager.uint32_type){
565 } else if (target_type == TypeManager.uint64_type){
567 // we can optimize this case: a positive int32
568 // always fits on a uint64. But we need an opcode
575 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
579 if (expr is LongConstant && target_type == TypeManager.uint64_type){
581 // Try the implicit constant expression conversion
582 // from long to ulong, instead of a nice routine,
585 long v = ((LongConstant) expr).Value;
590 if ((target_type == TypeManager.enum_type ||
591 target_type.IsSubclassOf (TypeManager.enum_type)) &&
593 IntLiteral i = (IntLiteral) expr;
599 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
606 // Used internally by FindMostEncompassedType, this is used
607 // to avoid creating lots of objects in the tight loop inside
608 // FindMostEncompassedType
610 static EmptyExpression priv_fmet_param;
613 /// Finds "most encompassed type" according to the spec (13.4.2)
614 /// amongst the methods in the MethodGroupExpr
616 static Type FindMostEncompassedType (ArrayList types)
620 if (priv_fmet_param == null)
621 priv_fmet_param = new EmptyExpression ();
623 foreach (Type t in types){
624 priv_fmet_param.SetType (t);
631 if (ImplicitStandardConversionExists (priv_fmet_param, best))
639 // Used internally by FindMostEncompassingType, this is used
640 // to avoid creating lots of objects in the tight loop inside
641 // FindMostEncompassingType
643 static EmptyExpression priv_fmee_ret;
646 /// Finds "most encompassing type" according to the spec (13.4.2)
647 /// amongst the types in the given set
649 static Type FindMostEncompassingType (ArrayList types)
653 if (priv_fmee_ret == null)
654 priv_fmee_ret = new EmptyExpression ();
656 foreach (Type t in types){
657 priv_fmee_ret.SetType (best);
664 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
672 // Used to avoid creating too many objects
674 static EmptyExpression priv_fms_expr;
677 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
678 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
679 /// for explicit and implicit conversion operators.
681 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
682 bool apply_explicit_conv_rules,
685 ArrayList src_types_set = new ArrayList ();
687 if (priv_fms_expr == null)
688 priv_fms_expr = new EmptyExpression ();
691 // If any operator converts from S then Sx = S
693 Type source_type = source.Type;
694 foreach (MethodBase mb in me.Methods){
695 ParameterData pd = Invocation.GetParameterData (mb);
696 Type param_type = pd.ParameterType (0);
698 if (param_type == source_type)
701 if (apply_explicit_conv_rules) {
704 // Find the set of applicable user-defined conversion operators, U. This set
706 // user-defined implicit or explicit conversion operators declared by
707 // the classes or structs in D that convert from a type encompassing
708 // or encompassed by S to a type encompassing or encompassed by T
710 priv_fms_expr.SetType (param_type);
711 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
712 src_types_set.Add (param_type);
714 if (ImplicitStandardConversionExists (source, param_type))
715 src_types_set.Add (param_type);
719 // Only if S is encompassed by param_type
721 if (ImplicitStandardConversionExists (source, param_type))
722 src_types_set.Add (param_type);
727 // Explicit Conv rules
729 if (apply_explicit_conv_rules) {
730 ArrayList candidate_set = new ArrayList ();
732 foreach (Type param_type in src_types_set){
733 if (ImplicitStandardConversionExists (source, param_type))
734 candidate_set.Add (param_type);
737 if (candidate_set.Count != 0)
738 return FindMostEncompassedType (candidate_set);
744 if (apply_explicit_conv_rules)
745 return FindMostEncompassingType (src_types_set);
747 return FindMostEncompassedType (src_types_set);
751 // Useful in avoiding proliferation of objects
753 static EmptyExpression priv_fmt_expr;
756 /// Finds the most specific target Tx according to section 13.4.4
758 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
759 bool apply_explicit_conv_rules,
762 ArrayList tgt_types_set = new ArrayList ();
764 if (priv_fmt_expr == null)
765 priv_fmt_expr = new EmptyExpression ();
768 // If any operator converts to T then Tx = T
770 foreach (MethodInfo mi in me.Methods){
771 Type ret_type = mi.ReturnType;
773 if (ret_type == target)
776 if (apply_explicit_conv_rules) {
779 // Find the set of applicable user-defined conversion operators, U.
781 // This set consists of the
782 // user-defined implicit or explicit conversion operators declared by
783 // the classes or structs in D that convert from a type encompassing
784 // or encompassed by S to a type encompassing or encompassed by T
786 priv_fms_expr.SetType (ret_type);
787 if (ImplicitStandardConversionExists (priv_fms_expr, target))
788 tgt_types_set.Add (ret_type);
790 priv_fms_expr.SetType (target);
791 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
792 tgt_types_set.Add (ret_type);
796 // Only if T is encompassed by param_type
798 priv_fms_expr.SetType (ret_type);
799 if (ImplicitStandardConversionExists (priv_fms_expr, target))
800 tgt_types_set.Add (ret_type);
805 // Explicit conv rules
807 if (apply_explicit_conv_rules) {
808 ArrayList candidate_set = new ArrayList ();
810 foreach (Type ret_type in tgt_types_set){
811 priv_fmt_expr.SetType (ret_type);
813 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
814 candidate_set.Add (ret_type);
817 if (candidate_set.Count != 0)
818 return FindMostEncompassingType (candidate_set);
822 // Okay, final case !
824 if (apply_explicit_conv_rules)
825 return FindMostEncompassedType (tgt_types_set);
827 return FindMostEncompassingType (tgt_types_set);
831 /// User-defined Implicit conversions
833 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
834 Type target, Location loc)
836 return UserDefinedConversion (ec, source, target, loc, false);
840 /// User-defined Explicit conversions
842 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
843 Type target, Location loc)
845 return UserDefinedConversion (ec, source, target, loc, true);
849 /// Computes the MethodGroup for the user-defined conversion
850 /// operators from source_type to target_type. `look_for_explicit'
851 /// controls whether we should also include the list of explicit
854 static MethodGroupExpr GetConversionOperators (EmitContext ec,
855 Type source_type, Type target_type,
856 Location loc, bool look_for_explicit)
858 Expression mg1 = null, mg2 = null;
859 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
862 op_name = "op_Implicit";
864 MethodGroupExpr union3;
866 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
867 if (source_type.BaseType != null)
868 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
871 union3 = (MethodGroupExpr) mg2;
872 else if (mg2 == null)
873 union3 = (MethodGroupExpr) mg1;
875 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
877 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
880 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
882 union3 = (MethodGroupExpr) mg1;
885 if (target_type.BaseType != null)
886 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
890 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
892 union3 = (MethodGroupExpr) mg1;
895 MethodGroupExpr union4 = null;
897 if (look_for_explicit) {
898 op_name = "op_Explicit";
900 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
901 if (source_type.BaseType != null)
902 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
904 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
905 if (target_type.BaseType != null)
906 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
908 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
909 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
911 union4 = Invocation.MakeUnionSet (union5, union6, loc);
914 return Invocation.MakeUnionSet (union3, union4, loc);
918 /// User-defined conversions
920 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
921 Type target, Location loc,
922 bool look_for_explicit)
924 MethodGroupExpr union;
925 Type source_type = source.Type;
926 MethodBase method = null;
928 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
932 Type most_specific_source, most_specific_target;
934 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
935 if (most_specific_source == null)
938 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
939 if (most_specific_target == null)
945 foreach (MethodBase mb in union.Methods){
946 ParameterData pd = Invocation.GetParameterData (mb);
947 MethodInfo mi = (MethodInfo) mb;
949 if (pd.ParameterType (0) == most_specific_source &&
950 mi.ReturnType == most_specific_target) {
956 if (method == null || count > 1)
961 // This will do the conversion to the best match that we
962 // found. Now we need to perform an implict standard conversion
963 // if the best match was not the type that we were requested
966 if (look_for_explicit)
967 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
969 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
975 e = new UserCast ((MethodInfo) method, source, loc);
976 if (e.Type != target){
977 if (!look_for_explicit)
978 e = ImplicitConversionStandard (ec, e, target, loc);
980 e = ExplicitConversionStandard (ec, e, target, loc);
987 /// Converts implicitly the resolved expression `expr' into the
988 /// `target_type'. It returns a new expression that can be used
989 /// in a context that expects a `target_type'.
991 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
992 Type target_type, Location loc)
994 Type expr_type = expr.Type;
997 if (target_type == null)
998 throw new Exception ("Target type is null");
1000 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1004 e = ImplicitUserConversion (ec, expr, target_type, loc);
1013 /// Attempts to apply the `Standard Implicit
1014 /// Conversion' rules to the expression `expr' into
1015 /// the `target_type'. It returns a new expression
1016 /// that can be used in a context that expects a
1019 /// This is different from `ImplicitConversion' in that the
1020 /// user defined implicit conversions are excluded.
1022 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1023 Type target_type, Location loc)
1025 Type expr_type = expr.Type;
1028 if (expr_type == target_type && !(expr is NullLiteral))
1031 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1035 e = ImplicitReferenceConversion (expr, target_type);
1039 if ((target_type == TypeManager.enum_type ||
1040 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1041 expr is IntLiteral){
1042 IntLiteral i = (IntLiteral) expr;
1045 return new EnumConstant ((Constant) expr, target_type);
1049 if (expr_type.IsPointer){
1050 if (target_type == TypeManager.void_ptr_type)
1051 return new EmptyCast (expr, target_type);
1054 // yep, comparing pointer types cant be done with
1055 // t1 == t2, we have to compare their element types.
1057 if (target_type.IsPointer){
1058 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1063 if (target_type.IsPointer) {
1064 if (expr is NullLiteral)
1065 return new EmptyCast (expr, target_type);
1067 if (expr_type == TypeManager.void_ptr_type)
1068 return new EmptyCast (expr, target_type);
1076 /// Attemps to perform an implict constant conversion of the IntConstant
1077 /// into a different data type using casts (See Implicit Constant
1078 /// Expression Conversions)
1080 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1082 int value = ic.Value;
1084 if (target_type == TypeManager.sbyte_type){
1085 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1086 return new SByteConstant ((sbyte) value);
1087 } else if (target_type == TypeManager.byte_type){
1088 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1089 return new ByteConstant ((byte) value);
1090 } else if (target_type == TypeManager.short_type){
1091 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1092 return new ShortConstant ((short) value);
1093 } else if (target_type == TypeManager.ushort_type){
1094 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1095 return new UShortConstant ((ushort) value);
1096 } else if (target_type == TypeManager.uint32_type){
1098 return new UIntConstant ((uint) value);
1099 } else if (target_type == TypeManager.uint64_type){
1101 // we can optimize this case: a positive int32
1102 // always fits on a uint64. But we need an opcode
1106 return new ULongConstant ((ulong) value);
1107 } else if (target_type == TypeManager.double_type)
1108 return new DoubleConstant ((double) value);
1109 else if (target_type == TypeManager.float_type)
1110 return new FloatConstant ((float) value);
1112 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1113 Type underlying = TypeManager.EnumToUnderlying (target_type);
1114 Constant e = (Constant) ic;
1117 // Possibly, we need to create a different 0 literal before passing
1120 if (underlying == TypeManager.int64_type)
1121 e = new LongLiteral (0);
1122 else if (underlying == TypeManager.uint64_type)
1123 e = new ULongLiteral (0);
1125 return new EnumConstant (e, target_type);
1130 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1132 string msg = "Cannot convert implicitly from `"+
1133 TypeManager.CSharpName (source) + "' to `" +
1134 TypeManager.CSharpName (target) + "'";
1136 Report.Error (29, loc, msg);
1140 /// Attemptes to implicityly convert `target' into `type', using
1141 /// ImplicitConversion. If there is no implicit conversion, then
1142 /// an error is signaled
1144 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1145 Type target_type, Location loc)
1149 e = ImplicitConversion (ec, source, target_type, loc);
1153 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1154 Report.Error (664, loc,
1155 "Double literal cannot be implicitly converted to " +
1156 "float type, use F suffix to create a float literal");
1159 Error_CannotImplicitConversion (loc, source.Type, target_type);
1165 /// Performs the explicit numeric conversions
1167 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1169 Type expr_type = expr.Type;
1172 // If we have an enumeration, extract the underlying type,
1173 // use this during the comparison, but wrap around the original
1176 Type real_target_type = target_type;
1178 if (TypeManager.IsEnumType (real_target_type))
1179 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1181 if (ImplicitStandardConversionExists (expr, real_target_type)){
1182 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1184 if (real_target_type != target_type)
1185 return new EmptyCast (ce, target_type);
1189 if (expr_type == TypeManager.sbyte_type){
1191 // From sbyte to byte, ushort, uint, ulong, char
1193 if (real_target_type == TypeManager.byte_type)
1194 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1195 if (real_target_type == TypeManager.ushort_type)
1196 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1197 if (real_target_type == TypeManager.uint32_type)
1198 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1199 if (real_target_type == TypeManager.uint64_type)
1200 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1201 if (real_target_type == TypeManager.char_type)
1202 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1203 } else if (expr_type == TypeManager.byte_type){
1205 // From byte to sbyte and char
1207 if (real_target_type == TypeManager.sbyte_type)
1208 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1209 if (real_target_type == TypeManager.char_type)
1210 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1211 } else if (expr_type == TypeManager.short_type){
1213 // From short to sbyte, byte, ushort, uint, ulong, char
1215 if (real_target_type == TypeManager.sbyte_type)
1216 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1217 if (real_target_type == TypeManager.byte_type)
1218 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1219 if (real_target_type == TypeManager.ushort_type)
1220 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1221 if (real_target_type == TypeManager.uint32_type)
1222 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1223 if (real_target_type == TypeManager.uint64_type)
1224 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1225 if (real_target_type == TypeManager.char_type)
1226 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1227 } else if (expr_type == TypeManager.ushort_type){
1229 // From ushort to sbyte, byte, short, char
1231 if (real_target_type == TypeManager.sbyte_type)
1232 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1233 if (real_target_type == TypeManager.byte_type)
1234 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1235 if (real_target_type == TypeManager.short_type)
1236 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1237 if (real_target_type == TypeManager.char_type)
1238 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1239 } else if (expr_type == TypeManager.int32_type){
1241 // From int to sbyte, byte, short, ushort, uint, ulong, char
1243 if (real_target_type == TypeManager.sbyte_type)
1244 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1245 if (real_target_type == TypeManager.byte_type)
1246 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1247 if (real_target_type == TypeManager.short_type)
1248 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1249 if (real_target_type == TypeManager.ushort_type)
1250 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1251 if (real_target_type == TypeManager.uint32_type)
1252 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1253 if (real_target_type == TypeManager.uint64_type)
1254 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1255 if (real_target_type == TypeManager.char_type)
1256 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1257 } else if (expr_type == TypeManager.uint32_type){
1259 // From uint to sbyte, byte, short, ushort, int, char
1261 if (real_target_type == TypeManager.sbyte_type)
1262 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1263 if (real_target_type == TypeManager.byte_type)
1264 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1265 if (real_target_type == TypeManager.short_type)
1266 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1267 if (real_target_type == TypeManager.ushort_type)
1268 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1269 if (real_target_type == TypeManager.int32_type)
1270 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1271 if (real_target_type == TypeManager.char_type)
1272 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1273 } else if (expr_type == TypeManager.int64_type){
1275 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1277 if (real_target_type == TypeManager.sbyte_type)
1278 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1279 if (real_target_type == TypeManager.byte_type)
1280 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1281 if (real_target_type == TypeManager.short_type)
1282 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1283 if (real_target_type == TypeManager.ushort_type)
1284 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1285 if (real_target_type == TypeManager.int32_type)
1286 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1287 if (real_target_type == TypeManager.uint32_type)
1288 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1289 if (real_target_type == TypeManager.uint64_type)
1290 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1291 if (real_target_type == TypeManager.char_type)
1292 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1293 } else if (expr_type == TypeManager.uint64_type){
1295 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1297 if (real_target_type == TypeManager.sbyte_type)
1298 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1299 if (real_target_type == TypeManager.byte_type)
1300 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1301 if (real_target_type == TypeManager.short_type)
1302 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1303 if (real_target_type == TypeManager.ushort_type)
1304 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1305 if (real_target_type == TypeManager.int32_type)
1306 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1307 if (real_target_type == TypeManager.uint32_type)
1308 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1309 if (real_target_type == TypeManager.int64_type)
1310 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1311 if (real_target_type == TypeManager.char_type)
1312 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1313 } else if (expr_type == TypeManager.char_type){
1315 // From char to sbyte, byte, short
1317 if (real_target_type == TypeManager.sbyte_type)
1318 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1319 if (real_target_type == TypeManager.byte_type)
1320 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1321 if (real_target_type == TypeManager.short_type)
1322 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1323 } else if (expr_type == TypeManager.float_type){
1325 // From float to sbyte, byte, short,
1326 // ushort, int, uint, long, ulong, char
1329 if (real_target_type == TypeManager.sbyte_type)
1330 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1331 if (real_target_type == TypeManager.byte_type)
1332 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1333 if (real_target_type == TypeManager.short_type)
1334 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1335 if (real_target_type == TypeManager.ushort_type)
1336 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1337 if (real_target_type == TypeManager.int32_type)
1338 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1339 if (real_target_type == TypeManager.uint32_type)
1340 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1341 if (real_target_type == TypeManager.int64_type)
1342 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1343 if (real_target_type == TypeManager.uint64_type)
1344 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1345 if (real_target_type == TypeManager.char_type)
1346 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1347 } else if (expr_type == TypeManager.double_type){
1349 // From double to byte, byte, short,
1350 // ushort, int, uint, long, ulong,
1351 // char, float or decimal
1353 if (real_target_type == TypeManager.sbyte_type)
1354 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1355 if (real_target_type == TypeManager.byte_type)
1356 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1357 if (real_target_type == TypeManager.short_type)
1358 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1359 if (real_target_type == TypeManager.ushort_type)
1360 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1361 if (real_target_type == TypeManager.int32_type)
1362 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1363 if (real_target_type == TypeManager.uint32_type)
1364 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1365 if (real_target_type == TypeManager.int64_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1367 if (real_target_type == TypeManager.uint64_type)
1368 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1369 if (real_target_type == TypeManager.char_type)
1370 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1371 if (real_target_type == TypeManager.float_type)
1372 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1375 // decimal is taken care of by the op_Explicit methods.
1381 /// Returns whether an explicit reference conversion can be performed
1382 /// from source_type to target_type
1384 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1386 bool target_is_value_type = target_type.IsValueType;
1388 if (source_type == target_type)
1392 // From object to any reference type
1394 if (source_type == TypeManager.object_type && !target_is_value_type)
1398 // From any class S to any class-type T, provided S is a base class of T
1400 if (target_type.IsSubclassOf (source_type))
1404 // From any interface type S to any interface T provided S is not derived from T
1406 if (source_type.IsInterface && target_type.IsInterface){
1407 if (!target_type.IsSubclassOf (source_type))
1412 // From any class type S to any interface T, provided S is not sealed
1413 // and provided S does not implement T.
1415 if (target_type.IsInterface && !source_type.IsSealed &&
1416 !TypeManager.ImplementsInterface (source_type, target_type))
1420 // From any interface-type S to to any class type T, provided T is not
1421 // sealed, or provided T implements S.
1423 if (source_type.IsInterface &&
1424 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1428 // From an array type S with an element type Se to an array type T with an
1429 // element type Te provided all the following are true:
1430 // * S and T differe only in element type, in other words, S and T
1431 // have the same number of dimensions.
1432 // * Both Se and Te are reference types
1433 // * An explicit referenc conversions exist from Se to Te
1435 if (source_type.IsArray && target_type.IsArray) {
1436 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1438 Type source_element_type = TypeManager.GetElementType (source_type);
1439 Type target_element_type = TypeManager.GetElementType (target_type);
1441 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1442 if (ExplicitReferenceConversionExists (source_element_type,
1443 target_element_type))
1449 // From System.Array to any array-type
1450 if (source_type == TypeManager.array_type &&
1451 target_type.IsArray){
1456 // From System delegate to any delegate-type
1458 if (source_type == TypeManager.delegate_type &&
1459 target_type.IsSubclassOf (TypeManager.delegate_type))
1463 // From ICloneable to Array or Delegate types
1465 if (source_type == TypeManager.icloneable_type &&
1466 (target_type == TypeManager.array_type ||
1467 target_type == TypeManager.delegate_type))
1474 /// Implements Explicit Reference conversions
1476 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1478 Type source_type = source.Type;
1479 bool target_is_value_type = target_type.IsValueType;
1482 // From object to any reference type
1484 if (source_type == TypeManager.object_type && !target_is_value_type)
1485 return new ClassCast (source, target_type);
1489 // From any class S to any class-type T, provided S is a base class of T
1491 if (target_type.IsSubclassOf (source_type))
1492 return new ClassCast (source, target_type);
1495 // From any interface type S to any interface T provided S is not derived from T
1497 if (source_type.IsInterface && target_type.IsInterface){
1498 if (TypeManager.ImplementsInterface (source_type, target_type))
1501 return new ClassCast (source, target_type);
1505 // From any class type S to any interface T, provides S is not sealed
1506 // and provided S does not implement T.
1508 if (target_type.IsInterface && !source_type.IsSealed) {
1509 if (TypeManager.ImplementsInterface (source_type, target_type))
1512 return new ClassCast (source, target_type);
1517 // From any interface-type S to to any class type T, provided T is not
1518 // sealed, or provided T implements S.
1520 if (source_type.IsInterface) {
1521 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1522 if (target_type.IsClass)
1523 return new ClassCast (source, target_type);
1525 return new UnboxCast (source, target_type);
1531 // From an array type S with an element type Se to an array type T with an
1532 // element type Te provided all the following are true:
1533 // * S and T differe only in element type, in other words, S and T
1534 // have the same number of dimensions.
1535 // * Both Se and Te are reference types
1536 // * An explicit referenc conversions exist from Se to Te
1538 if (source_type.IsArray && target_type.IsArray) {
1539 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1541 Type source_element_type = TypeManager.GetElementType (source_type);
1542 Type target_element_type = TypeManager.GetElementType (target_type);
1544 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1545 if (ExplicitReferenceConversionExists (source_element_type,
1546 target_element_type))
1547 return new ClassCast (source, target_type);
1552 // From System.Array to any array-type
1553 if (source_type == TypeManager.array_type &&
1554 target_type.IsArray) {
1555 return new ClassCast (source, target_type);
1559 // From System delegate to any delegate-type
1561 if (source_type == TypeManager.delegate_type &&
1562 target_type.IsSubclassOf (TypeManager.delegate_type))
1563 return new ClassCast (source, target_type);
1566 // From ICloneable to Array or Delegate types
1568 if (source_type == TypeManager.icloneable_type &&
1569 (target_type == TypeManager.array_type ||
1570 target_type == TypeManager.delegate_type))
1571 return new ClassCast (source, target_type);
1577 /// Performs an explicit conversion of the expression `expr' whose
1578 /// type is expr.Type to `target_type'.
1580 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1581 Type target_type, Location loc)
1583 Type expr_type = expr.Type;
1584 Type original_expr_type = expr_type;
1586 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1587 if (target_type == TypeManager.enum_type ||
1588 target_type == TypeManager.object_type) {
1589 if (expr is EnumConstant)
1590 expr = ((EnumConstant) expr).Child;
1591 // We really need all these casts here .... :-(
1592 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1593 return new EmptyCast (expr, target_type);
1594 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1595 target_type.IsSubclassOf (TypeManager.enum_type))
1596 return new UnboxCast (expr, target_type);
1599 // Notice that we have kept the expr_type unmodified, which is only
1601 if (expr is EnumConstant)
1602 expr = ((EnumConstant) expr).Child;
1604 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1605 expr_type = expr.Type;
1608 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1613 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1618 // Unboxing conversion.
1620 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1621 if (expr is NullLiteral){
1622 Report.Error (37, loc, "Cannot convert null to value type `" +
1623 TypeManager.CSharpName (target_type) + "'");
1626 return new UnboxCast (expr, target_type);
1630 ne = ExplicitReferenceConversion (expr, target_type);
1635 if (target_type.IsPointer){
1636 if (expr_type.IsPointer)
1637 return new EmptyCast (expr, target_type);
1639 if (expr_type == TypeManager.sbyte_type ||
1640 expr_type == TypeManager.byte_type ||
1641 expr_type == TypeManager.short_type ||
1642 expr_type == TypeManager.ushort_type ||
1643 expr_type == TypeManager.int32_type ||
1644 expr_type == TypeManager.uint32_type ||
1645 expr_type == TypeManager.uint64_type ||
1646 expr_type == TypeManager.int64_type)
1647 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1649 if (expr_type.IsPointer){
1650 Expression e = null;
1652 if (target_type == TypeManager.sbyte_type)
1653 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1654 else if (target_type == TypeManager.byte_type)
1655 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1656 else if (target_type == TypeManager.short_type)
1657 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1658 else if (target_type == TypeManager.ushort_type)
1659 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1660 else if (target_type == TypeManager.int32_type)
1661 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1662 else if (target_type == TypeManager.uint32_type)
1663 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1664 else if (target_type == TypeManager.uint64_type)
1665 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1666 else if (target_type == TypeManager.int64_type){
1667 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1673 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1678 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1682 // We should always be able to go from an uint32
1683 // implicitly or explicitly to the other integral
1686 throw new Exception ("Internal compiler error");
1691 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1695 Error_CannotConvertType (loc, original_expr_type, target_type);
1700 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1702 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1703 Type target_type, Location l)
1705 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1710 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1714 ne = ExplicitReferenceConversion (expr, target_type);
1718 Error_CannotConvertType (l, expr.Type, target_type);