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
41 // notice that it is possible to write "ValueType v = 1", the ValueType here
42 // is an abstract class, and not really a value type, so we apply the same rules.
44 if (target_type == TypeManager.object_type) {
46 // A pointer type cannot be converted to object
48 if (expr_type.IsPointer)
51 if (expr_type.IsValueType)
52 return new BoxedCast (expr);
53 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
54 return new EmptyCast (expr, target_type);
55 } else if (target_type == TypeManager.value_type) {
56 if (expr_type.IsValueType)
57 return new BoxedCast (expr);
58 if (expr is NullLiteral)
59 return new BoxedCast (expr);
60 } else if (expr_type.IsSubclassOf (target_type)) {
62 // Special case: enumeration to System.Enum.
63 // System.Enum is not a value type, it is a class, so we need
64 // a boxing conversion
67 return new BoxedCast (expr);
69 return new EmptyCast (expr, target_type);
72 // This code is kind of mirrored inside ImplicitStandardConversionExists
73 // with the small distinction that we only probe there
75 // Always ensure that the code here and there is in sync
77 // from the null type to any reference-type.
78 if (expr is NullLiteral && !target_type.IsValueType)
79 return new NullCast (expr, target_type);
82 // from any class-type S to any interface-type T.
83 if (target_type.IsInterface) {
84 if (TypeManager.ImplementsInterface (expr_type, target_type)){
85 if (expr_type.IsClass)
86 return new EmptyCast (expr, target_type);
87 else if (expr_type.IsValueType)
88 return new BoxedCast (expr, target_type);
92 // from any interface type S to interface-type T.
93 if (expr_type.IsInterface && target_type.IsInterface) {
94 if (TypeManager.ImplementsInterface (expr_type, target_type))
95 return new EmptyCast (expr, target_type);
100 // from an array-type S to an array-type of type T
101 if (expr_type.IsArray && target_type.IsArray) {
102 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
104 Type expr_element_type = expr_type.GetElementType ();
106 if (MyEmptyExpr == null)
107 MyEmptyExpr = new EmptyExpression ();
109 MyEmptyExpr.SetType (expr_element_type);
110 Type target_element_type = target_type.GetElementType ();
112 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
113 if (ImplicitStandardConversionExists (MyEmptyExpr,
114 target_element_type))
115 return new EmptyCast (expr, target_type);
120 // from an array-type to System.Array
121 if (expr_type.IsArray && target_type == TypeManager.array_type)
122 return new EmptyCast (expr, target_type);
124 // from any delegate type to System.Delegate
125 if ((expr_type == TypeManager.delegate_type ||
126 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
127 target_type == TypeManager.delegate_type)
128 return new EmptyCast (expr, target_type);
130 // from any array-type or delegate type into System.ICloneable.
131 if (expr_type.IsArray ||
132 expr_type == TypeManager.delegate_type ||
133 expr_type.IsSubclassOf (TypeManager.delegate_type))
134 if (target_type == TypeManager.icloneable_type)
135 return new EmptyCast (expr, target_type);
145 // Tests whether an implicit reference conversion exists between expr_type
148 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
150 Type expr_type = expr.Type;
153 // This is the boxed case.
155 if (target_type == TypeManager.object_type) {
156 if (expr_type.IsClass || expr_type.IsValueType ||
157 expr_type.IsInterface || expr_type == TypeManager.enum_type)
159 } else if (expr_type.IsSubclassOf (target_type)) {
162 // Please remember that all code below actually comes
163 // from ImplicitReferenceConversion so make sure code remains in sync
165 // from any class-type S to any interface-type T.
166 if (target_type.IsInterface) {
167 if (TypeManager.ImplementsInterface (expr_type, target_type))
171 // from any interface type S to interface-type T.
172 if (expr_type.IsInterface && target_type.IsInterface)
173 if (TypeManager.ImplementsInterface (expr_type, target_type))
176 // from an array-type S to an array-type of type T
177 if (expr_type.IsArray && target_type.IsArray) {
178 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
180 Type expr_element_type = expr_type.GetElementType ();
182 if (MyEmptyExpr == null)
183 MyEmptyExpr = new EmptyExpression ();
185 MyEmptyExpr.SetType (expr_element_type);
186 Type target_element_type = target_type.GetElementType ();
188 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
189 if (ImplicitStandardConversionExists (MyEmptyExpr,
190 target_element_type))
195 // from an array-type to System.Array
196 if (expr_type.IsArray && (target_type == TypeManager.array_type))
199 // from any delegate type to System.Delegate
200 if ((expr_type == TypeManager.delegate_type ||
201 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
202 target_type == TypeManager.delegate_type)
203 if (target_type.IsAssignableFrom (expr_type))
206 // from any array-type or delegate type into System.ICloneable.
207 if (expr_type.IsArray ||
208 expr_type == TypeManager.delegate_type ||
209 expr_type.IsSubclassOf (TypeManager.delegate_type))
210 if (target_type == TypeManager.icloneable_type)
213 // from the null type to any reference-type.
214 if (expr is NullLiteral && !target_type.IsValueType &&
215 !TypeManager.IsEnumType (target_type))
224 /// Implicit Numeric Conversions.
226 /// expr is the expression to convert, returns a new expression of type
227 /// target_type or null if an implicit conversion is not possible.
229 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
230 Type target_type, Location loc)
232 Type expr_type = expr.Type;
235 // Attempt to do the implicit constant expression conversions
237 if (expr is Constant){
239 if (expr is IntConstant){
242 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
246 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
248 // Try the implicit constant expression conversion
249 // from long to ulong, instead of a nice routine,
252 long v = ((LongConstant) expr).Value;
254 return new ULongConstant ((ulong) v);
258 Type real_target_type = target_type;
260 if (expr_type == TypeManager.sbyte_type){
262 // From sbyte to short, int, long, float, double.
264 if (real_target_type == TypeManager.int32_type)
265 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
266 if (real_target_type == TypeManager.int64_type)
267 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
268 if (real_target_type == TypeManager.double_type)
269 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
270 if (real_target_type == TypeManager.float_type)
271 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
272 if (real_target_type == TypeManager.short_type)
273 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
274 } else if (expr_type == TypeManager.byte_type){
276 // From byte to short, ushort, int, uint, long, ulong, float, double
278 if ((real_target_type == TypeManager.short_type) ||
279 (real_target_type == TypeManager.ushort_type) ||
280 (real_target_type == TypeManager.int32_type) ||
281 (real_target_type == TypeManager.uint32_type))
282 return new EmptyCast (expr, target_type);
284 if (real_target_type == TypeManager.uint64_type)
285 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
286 if (real_target_type == TypeManager.int64_type)
287 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
288 if (real_target_type == TypeManager.float_type)
289 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
290 if (real_target_type == TypeManager.double_type)
291 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
292 } else if (expr_type == TypeManager.short_type){
294 // From short to int, long, float, double
296 if (real_target_type == TypeManager.int32_type)
297 return new EmptyCast (expr, target_type);
298 if (real_target_type == TypeManager.int64_type)
299 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
300 if (real_target_type == TypeManager.double_type)
301 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
302 if (real_target_type == TypeManager.float_type)
303 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
304 } else if (expr_type == TypeManager.ushort_type){
306 // From ushort to int, uint, long, ulong, float, double
308 if (real_target_type == TypeManager.uint32_type)
309 return new EmptyCast (expr, target_type);
311 if (real_target_type == TypeManager.uint64_type)
312 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
313 if (real_target_type == TypeManager.int32_type)
314 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
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.int32_type){
323 // From int to long, float, double
325 if (real_target_type == TypeManager.int64_type)
326 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
327 if (real_target_type == TypeManager.double_type)
328 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
329 if (real_target_type == TypeManager.float_type)
330 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
331 } else if (expr_type == TypeManager.uint32_type){
333 // From uint to long, ulong, float, double
335 if (real_target_type == TypeManager.int64_type)
336 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
337 if (real_target_type == TypeManager.uint64_type)
338 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
339 if (real_target_type == TypeManager.double_type)
340 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
342 if (real_target_type == TypeManager.float_type)
343 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
345 } else if (expr_type == TypeManager.int64_type){
347 // From long/ulong to float, double
349 if (real_target_type == TypeManager.double_type)
350 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
351 if (real_target_type == TypeManager.float_type)
352 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
353 } else if (expr_type == TypeManager.uint64_type){
355 // From ulong to float, double
357 if (real_target_type == TypeManager.double_type)
358 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
360 if (real_target_type == TypeManager.float_type)
361 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
363 } else if (expr_type == TypeManager.char_type){
365 // From char to ushort, int, uint, long, ulong, float, double
367 if ((real_target_type == TypeManager.ushort_type) ||
368 (real_target_type == TypeManager.int32_type) ||
369 (real_target_type == TypeManager.uint32_type))
370 return new EmptyCast (expr, target_type);
371 if (real_target_type == TypeManager.uint64_type)
372 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
373 if (real_target_type == TypeManager.int64_type)
374 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
375 if (real_target_type == TypeManager.float_type)
376 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
377 if (real_target_type == TypeManager.double_type)
378 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
379 } else if (expr_type == TypeManager.float_type){
383 if (real_target_type == TypeManager.double_type)
384 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
392 /// Same as ImplicitStandardConversionExists except that it also looks at
393 /// implicit user defined conversions - needed for overload resolution
395 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
397 if (ImplicitStandardConversionExists (expr, target_type) == true)
400 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
408 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
410 Expression dummy = ImplicitUserConversion (
411 ec, new EmptyExpression (source), target, Location.Null);
412 return dummy != null;
416 /// Determines if a standard implicit conversion exists from
417 /// expr_type to target_type
419 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
421 Type expr_type = expr.Type;
423 if (expr_type == TypeManager.void_type)
426 if (expr_type == target_type)
429 // First numeric conversions
431 if (expr_type == TypeManager.sbyte_type){
433 // From sbyte to short, int, long, float, double.
435 if ((target_type == TypeManager.int32_type) ||
436 (target_type == TypeManager.int64_type) ||
437 (target_type == TypeManager.double_type) ||
438 (target_type == TypeManager.float_type) ||
439 (target_type == TypeManager.short_type) ||
440 (target_type == TypeManager.decimal_type))
443 } else if (expr_type == TypeManager.byte_type){
445 // From byte to short, ushort, int, uint, long, ulong, float, double
447 if ((target_type == TypeManager.short_type) ||
448 (target_type == TypeManager.ushort_type) ||
449 (target_type == TypeManager.int32_type) ||
450 (target_type == TypeManager.uint32_type) ||
451 (target_type == TypeManager.uint64_type) ||
452 (target_type == TypeManager.int64_type) ||
453 (target_type == TypeManager.float_type) ||
454 (target_type == TypeManager.double_type) ||
455 (target_type == TypeManager.decimal_type))
458 } else if (expr_type == TypeManager.short_type){
460 // From short to int, long, float, double
462 if ((target_type == TypeManager.int32_type) ||
463 (target_type == TypeManager.int64_type) ||
464 (target_type == TypeManager.double_type) ||
465 (target_type == TypeManager.float_type) ||
466 (target_type == TypeManager.decimal_type))
469 } else if (expr_type == TypeManager.ushort_type){
471 // From ushort to int, uint, long, ulong, float, double
473 if ((target_type == TypeManager.uint32_type) ||
474 (target_type == TypeManager.uint64_type) ||
475 (target_type == TypeManager.int32_type) ||
476 (target_type == TypeManager.int64_type) ||
477 (target_type == TypeManager.double_type) ||
478 (target_type == TypeManager.float_type) ||
479 (target_type == TypeManager.decimal_type))
482 } else if (expr_type == TypeManager.int32_type){
484 // From int to long, float, double
486 if ((target_type == TypeManager.int64_type) ||
487 (target_type == TypeManager.double_type) ||
488 (target_type == TypeManager.float_type) ||
489 (target_type == TypeManager.decimal_type))
492 } else if (expr_type == TypeManager.uint32_type){
494 // From uint to long, ulong, float, double
496 if ((target_type == TypeManager.int64_type) ||
497 (target_type == TypeManager.uint64_type) ||
498 (target_type == TypeManager.double_type) ||
499 (target_type == TypeManager.float_type) ||
500 (target_type == TypeManager.decimal_type))
503 } else if ((expr_type == TypeManager.uint64_type) ||
504 (expr_type == TypeManager.int64_type)) {
506 // From long/ulong to float, double
508 if ((target_type == TypeManager.double_type) ||
509 (target_type == TypeManager.float_type) ||
510 (target_type == TypeManager.decimal_type))
513 } else if (expr_type == TypeManager.char_type){
515 // From char to ushort, int, uint, long, ulong, float, double
517 if ((target_type == TypeManager.ushort_type) ||
518 (target_type == TypeManager.int32_type) ||
519 (target_type == TypeManager.uint32_type) ||
520 (target_type == TypeManager.uint64_type) ||
521 (target_type == TypeManager.int64_type) ||
522 (target_type == TypeManager.float_type) ||
523 (target_type == TypeManager.double_type) ||
524 (target_type == TypeManager.decimal_type))
527 } else if (expr_type == TypeManager.float_type){
531 if (target_type == TypeManager.double_type)
535 if (ImplicitReferenceConversionExists (expr, target_type))
538 if (expr is IntConstant){
539 int value = ((IntConstant) expr).Value;
541 if (target_type == TypeManager.sbyte_type){
542 if (value >= SByte.MinValue && value <= SByte.MaxValue)
544 } else if (target_type == TypeManager.byte_type){
545 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
547 } else if (target_type == TypeManager.short_type){
548 if (value >= Int16.MinValue && value <= Int16.MaxValue)
550 } else if (target_type == TypeManager.ushort_type){
551 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
553 } else if (target_type == TypeManager.uint32_type){
556 } else if (target_type == TypeManager.uint64_type){
558 // we can optimize this case: a positive int32
559 // always fits on a uint64. But we need an opcode
566 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
570 if (expr is LongConstant && target_type == TypeManager.uint64_type){
572 // Try the implicit constant expression conversion
573 // from long to ulong, instead of a nice routine,
576 long v = ((LongConstant) expr).Value;
581 if ((target_type == TypeManager.enum_type ||
582 target_type.IsSubclassOf (TypeManager.enum_type)) &&
584 IntLiteral i = (IntLiteral) expr;
590 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
597 // Used internally by FindMostEncompassedType, this is used
598 // to avoid creating lots of objects in the tight loop inside
599 // FindMostEncompassedType
601 static EmptyExpression priv_fmet_param;
604 /// Finds "most encompassed type" according to the spec (13.4.2)
605 /// amongst the methods in the MethodGroupExpr
607 static Type FindMostEncompassedType (ArrayList types)
611 if (priv_fmet_param == null)
612 priv_fmet_param = new EmptyExpression ();
614 foreach (Type t in types){
615 priv_fmet_param.SetType (t);
622 if (ImplicitStandardConversionExists (priv_fmet_param, best))
630 // Used internally by FindMostEncompassingType, this is used
631 // to avoid creating lots of objects in the tight loop inside
632 // FindMostEncompassingType
634 static EmptyExpression priv_fmee_ret;
637 /// Finds "most encompassing type" according to the spec (13.4.2)
638 /// amongst the types in the given set
640 static Type FindMostEncompassingType (ArrayList types)
644 if (priv_fmee_ret == null)
645 priv_fmee_ret = new EmptyExpression ();
647 foreach (Type t in types){
648 priv_fmee_ret.SetType (best);
655 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
663 // Used to avoid creating too many objects
665 static EmptyExpression priv_fms_expr;
668 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
669 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
670 /// for explicit and implicit conversion operators.
672 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
673 bool apply_explicit_conv_rules,
676 ArrayList src_types_set = new ArrayList ();
678 if (priv_fms_expr == null)
679 priv_fms_expr = new EmptyExpression ();
682 // If any operator converts from S then Sx = S
684 Type source_type = source.Type;
685 foreach (MethodBase mb in me.Methods){
686 ParameterData pd = Invocation.GetParameterData (mb);
687 Type param_type = pd.ParameterType (0);
689 if (param_type == source_type)
692 if (apply_explicit_conv_rules) {
695 // Find the set of applicable user-defined conversion operators, U. This set
697 // user-defined implicit or explicit conversion operators declared by
698 // the classes or structs in D that convert from a type encompassing
699 // or encompassed by S to a type encompassing or encompassed by T
701 priv_fms_expr.SetType (param_type);
702 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
703 src_types_set.Add (param_type);
705 if (ImplicitStandardConversionExists (source, param_type))
706 src_types_set.Add (param_type);
710 // Only if S is encompassed by param_type
712 if (ImplicitStandardConversionExists (source, param_type))
713 src_types_set.Add (param_type);
718 // Explicit Conv rules
720 if (apply_explicit_conv_rules) {
721 ArrayList candidate_set = new ArrayList ();
723 foreach (Type param_type in src_types_set){
724 if (ImplicitStandardConversionExists (source, param_type))
725 candidate_set.Add (param_type);
728 if (candidate_set.Count != 0)
729 return FindMostEncompassedType (candidate_set);
735 if (apply_explicit_conv_rules)
736 return FindMostEncompassingType (src_types_set);
738 return FindMostEncompassedType (src_types_set);
742 // Useful in avoiding proliferation of objects
744 static EmptyExpression priv_fmt_expr;
747 /// Finds the most specific target Tx according to section 13.4.4
749 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
750 bool apply_explicit_conv_rules,
753 ArrayList tgt_types_set = new ArrayList ();
755 if (priv_fmt_expr == null)
756 priv_fmt_expr = new EmptyExpression ();
759 // If any operator converts to T then Tx = T
761 foreach (MethodInfo mi in me.Methods){
762 Type ret_type = mi.ReturnType;
764 if (ret_type == target)
767 if (apply_explicit_conv_rules) {
770 // Find the set of applicable user-defined conversion operators, U.
772 // This set consists of the
773 // user-defined implicit or explicit conversion operators declared by
774 // the classes or structs in D that convert from a type encompassing
775 // or encompassed by S to a type encompassing or encompassed by T
777 priv_fms_expr.SetType (ret_type);
778 if (ImplicitStandardConversionExists (priv_fms_expr, target))
779 tgt_types_set.Add (ret_type);
781 priv_fms_expr.SetType (target);
782 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
783 tgt_types_set.Add (ret_type);
787 // Only if T is encompassed by param_type
789 priv_fms_expr.SetType (ret_type);
790 if (ImplicitStandardConversionExists (priv_fms_expr, target))
791 tgt_types_set.Add (ret_type);
796 // Explicit conv rules
798 if (apply_explicit_conv_rules) {
799 ArrayList candidate_set = new ArrayList ();
801 foreach (Type ret_type in tgt_types_set){
802 priv_fmt_expr.SetType (ret_type);
804 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
805 candidate_set.Add (ret_type);
808 if (candidate_set.Count != 0)
809 return FindMostEncompassingType (candidate_set);
813 // Okay, final case !
815 if (apply_explicit_conv_rules)
816 return FindMostEncompassedType (tgt_types_set);
818 return FindMostEncompassingType (tgt_types_set);
822 /// User-defined Implicit conversions
824 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
825 Type target, Location loc)
827 return UserDefinedConversion (ec, source, target, loc, false);
831 /// User-defined Explicit conversions
833 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
834 Type target, Location loc)
836 return UserDefinedConversion (ec, source, target, loc, true);
840 /// Computes the MethodGroup for the user-defined conversion
841 /// operators from source_type to target_type. `look_for_explicit'
842 /// controls whether we should also include the list of explicit
845 static MethodGroupExpr GetConversionOperators (EmitContext ec,
846 Type source_type, Type target_type,
847 Location loc, bool look_for_explicit)
849 Expression mg1 = null, mg2 = null;
850 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
853 op_name = "op_Implicit";
855 MethodGroupExpr union3;
857 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
858 if (source_type.BaseType != null)
859 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
862 union3 = (MethodGroupExpr) mg2;
863 else if (mg2 == null)
864 union3 = (MethodGroupExpr) mg1;
866 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
868 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
871 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
873 union3 = (MethodGroupExpr) mg1;
876 if (target_type.BaseType != null)
877 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
881 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
883 union3 = (MethodGroupExpr) mg1;
886 MethodGroupExpr union4 = null;
888 if (look_for_explicit) {
889 op_name = "op_Explicit";
891 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
892 if (source_type.BaseType != null)
893 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
895 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
896 if (target_type.BaseType != null)
897 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
899 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
900 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
902 union4 = Invocation.MakeUnionSet (union5, union6, loc);
905 return Invocation.MakeUnionSet (union3, union4, loc);
909 /// User-defined conversions
911 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
912 Type target, Location loc,
913 bool look_for_explicit)
915 MethodGroupExpr union;
916 Type source_type = source.Type;
917 MethodBase method = null;
919 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
923 Type most_specific_source, most_specific_target;
925 foreach (MethodBase m in union.Methods){
926 Console.WriteLine ("Name: " + m.Name);
927 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
931 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
932 if (most_specific_source == null)
935 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
936 if (most_specific_target == null)
942 foreach (MethodBase mb in union.Methods){
943 ParameterData pd = Invocation.GetParameterData (mb);
944 MethodInfo mi = (MethodInfo) mb;
946 if (pd.ParameterType (0) == most_specific_source &&
947 mi.ReturnType == most_specific_target) {
953 if (method == null || count > 1)
958 // This will do the conversion to the best match that we
959 // found. Now we need to perform an implict standard conversion
960 // if the best match was not the type that we were requested
963 if (look_for_explicit)
964 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
966 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
972 e = new UserCast ((MethodInfo) method, source, loc);
973 if (e.Type != target){
974 if (!look_for_explicit)
975 e = ImplicitConversionStandard (ec, e, target, loc);
977 e = ExplicitConversionStandard (ec, e, target, loc);
984 /// Converts implicitly the resolved expression `expr' into the
985 /// `target_type'. It returns a new expression that can be used
986 /// in a context that expects a `target_type'.
988 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
989 Type target_type, Location loc)
991 Type expr_type = expr.Type;
994 if (target_type == null)
995 throw new Exception ("Target type is null");
997 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1001 e = ImplicitUserConversion (ec, expr, target_type, loc);
1010 /// Attempts to apply the `Standard Implicit
1011 /// Conversion' rules to the expression `expr' into
1012 /// the `target_type'. It returns a new expression
1013 /// that can be used in a context that expects a
1016 /// This is different from `ImplicitConversion' in that the
1017 /// user defined implicit conversions are excluded.
1019 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1020 Type target_type, Location loc)
1022 Type expr_type = expr.Type;
1025 if (expr_type == target_type && !(expr is NullLiteral))
1028 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1032 e = ImplicitReferenceConversion (expr, target_type);
1036 if ((target_type == TypeManager.enum_type ||
1037 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1038 expr is IntLiteral){
1039 IntLiteral i = (IntLiteral) expr;
1042 return new EnumConstant ((Constant) expr, target_type);
1046 if (expr_type.IsPointer){
1047 if (target_type == TypeManager.void_ptr_type)
1048 return new EmptyCast (expr, target_type);
1051 // yep, comparing pointer types cant be done with
1052 // t1 == t2, we have to compare their element types.
1054 if (target_type.IsPointer){
1055 if (target_type.GetElementType() == expr_type.GetElementType())
1060 if (target_type.IsPointer) {
1061 if (expr is NullLiteral)
1062 return new EmptyCast (expr, target_type);
1064 if (expr_type == TypeManager.void_ptr_type)
1065 return new EmptyCast (expr, target_type);
1073 /// Attemps to perform an implict constant conversion of the IntConstant
1074 /// into a different data type using casts (See Implicit Constant
1075 /// Expression Conversions)
1077 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1079 int value = ic.Value;
1081 if (target_type == TypeManager.sbyte_type){
1082 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1083 return new SByteConstant ((sbyte) value);
1084 } else if (target_type == TypeManager.byte_type){
1085 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1086 return new ByteConstant ((byte) value);
1087 } else if (target_type == TypeManager.short_type){
1088 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1089 return new ShortConstant ((short) value);
1090 } else if (target_type == TypeManager.ushort_type){
1091 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1092 return new UShortConstant ((ushort) value);
1093 } else if (target_type == TypeManager.uint32_type){
1095 return new UIntConstant ((uint) value);
1096 } else if (target_type == TypeManager.uint64_type){
1098 // we can optimize this case: a positive int32
1099 // always fits on a uint64. But we need an opcode
1103 return new ULongConstant ((ulong) value);
1104 } else if (target_type == TypeManager.double_type)
1105 return new DoubleConstant ((double) value);
1106 else if (target_type == TypeManager.float_type)
1107 return new FloatConstant ((float) value);
1109 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1110 Type underlying = TypeManager.EnumToUnderlying (target_type);
1111 Constant e = (Constant) ic;
1114 // Possibly, we need to create a different 0 literal before passing
1117 if (underlying == TypeManager.int64_type)
1118 e = new LongLiteral (0);
1119 else if (underlying == TypeManager.uint64_type)
1120 e = new ULongLiteral (0);
1122 return new EnumConstant (e, target_type);
1127 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1129 string msg = "Cannot convert implicitly from `"+
1130 TypeManager.CSharpName (source) + "' to `" +
1131 TypeManager.CSharpName (target) + "'";
1133 Report.Error (29, loc, msg);
1137 /// Attemptes to implicityly convert `target' into `type', using
1138 /// ImplicitConversion. If there is no implicit conversion, then
1139 /// an error is signaled
1141 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1142 Type target_type, Location loc)
1146 e = ImplicitConversion (ec, source, target_type, loc);
1150 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1151 Report.Error (664, loc,
1152 "Double literal cannot be implicitly converted to " +
1153 "float type, use F suffix to create a float literal");
1156 Error_CannotImplicitConversion (loc, source.Type, target_type);
1162 /// Performs the explicit numeric conversions
1164 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1166 Type expr_type = expr.Type;
1169 // If we have an enumeration, extract the underlying type,
1170 // use this during the comparison, but wrap around the original
1173 Type real_target_type = target_type;
1175 if (TypeManager.IsEnumType (real_target_type))
1176 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1178 if (ImplicitStandardConversionExists (expr, real_target_type)){
1179 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1181 if (real_target_type != target_type)
1182 return new EmptyCast (ce, target_type);
1186 if (expr_type == TypeManager.sbyte_type){
1188 // From sbyte to byte, ushort, uint, ulong, char
1190 if (real_target_type == TypeManager.byte_type)
1191 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1192 if (real_target_type == TypeManager.ushort_type)
1193 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1194 if (real_target_type == TypeManager.uint32_type)
1195 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1196 if (real_target_type == TypeManager.uint64_type)
1197 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1198 if (real_target_type == TypeManager.char_type)
1199 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1200 } else if (expr_type == TypeManager.byte_type){
1202 // From byte to sbyte and char
1204 if (real_target_type == TypeManager.sbyte_type)
1205 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1206 if (real_target_type == TypeManager.char_type)
1207 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1208 } else if (expr_type == TypeManager.short_type){
1210 // From short to sbyte, byte, ushort, uint, ulong, char
1212 if (real_target_type == TypeManager.sbyte_type)
1213 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1214 if (real_target_type == TypeManager.byte_type)
1215 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1216 if (real_target_type == TypeManager.ushort_type)
1217 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1218 if (real_target_type == TypeManager.uint32_type)
1219 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1220 if (real_target_type == TypeManager.uint64_type)
1221 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1222 if (real_target_type == TypeManager.char_type)
1223 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1224 } else if (expr_type == TypeManager.ushort_type){
1226 // From ushort to sbyte, byte, short, char
1228 if (real_target_type == TypeManager.sbyte_type)
1229 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1230 if (real_target_type == TypeManager.byte_type)
1231 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1232 if (real_target_type == TypeManager.short_type)
1233 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1234 if (real_target_type == TypeManager.char_type)
1235 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1236 } else if (expr_type == TypeManager.int32_type){
1238 // From int to sbyte, byte, short, ushort, uint, ulong, char
1240 if (real_target_type == TypeManager.sbyte_type)
1241 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1242 if (real_target_type == TypeManager.byte_type)
1243 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1244 if (real_target_type == TypeManager.short_type)
1245 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1246 if (real_target_type == TypeManager.ushort_type)
1247 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1248 if (real_target_type == TypeManager.uint32_type)
1249 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1250 if (real_target_type == TypeManager.uint64_type)
1251 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1252 if (real_target_type == TypeManager.char_type)
1253 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1254 } else if (expr_type == TypeManager.uint32_type){
1256 // From uint to sbyte, byte, short, ushort, int, char
1258 if (real_target_type == TypeManager.sbyte_type)
1259 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1260 if (real_target_type == TypeManager.byte_type)
1261 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1262 if (real_target_type == TypeManager.short_type)
1263 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1264 if (real_target_type == TypeManager.ushort_type)
1265 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1266 if (real_target_type == TypeManager.int32_type)
1267 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1268 if (real_target_type == TypeManager.char_type)
1269 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1270 } else if (expr_type == TypeManager.int64_type){
1272 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1274 if (real_target_type == TypeManager.sbyte_type)
1275 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1276 if (real_target_type == TypeManager.byte_type)
1277 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1278 if (real_target_type == TypeManager.short_type)
1279 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1280 if (real_target_type == TypeManager.ushort_type)
1281 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1282 if (real_target_type == TypeManager.int32_type)
1283 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1284 if (real_target_type == TypeManager.uint32_type)
1285 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1286 if (real_target_type == TypeManager.uint64_type)
1287 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1288 if (real_target_type == TypeManager.char_type)
1289 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1290 } else if (expr_type == TypeManager.uint64_type){
1292 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1294 if (real_target_type == TypeManager.sbyte_type)
1295 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1296 if (real_target_type == TypeManager.byte_type)
1297 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1298 if (real_target_type == TypeManager.short_type)
1299 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1300 if (real_target_type == TypeManager.ushort_type)
1301 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1302 if (real_target_type == TypeManager.int32_type)
1303 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1304 if (real_target_type == TypeManager.uint32_type)
1305 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1306 if (real_target_type == TypeManager.int64_type)
1307 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1308 if (real_target_type == TypeManager.char_type)
1309 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1310 } else if (expr_type == TypeManager.char_type){
1312 // From char to sbyte, byte, short
1314 if (real_target_type == TypeManager.sbyte_type)
1315 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1316 if (real_target_type == TypeManager.byte_type)
1317 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1318 if (real_target_type == TypeManager.short_type)
1319 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1320 } else if (expr_type == TypeManager.float_type){
1322 // From float to sbyte, byte, short,
1323 // ushort, int, uint, long, ulong, char
1326 if (real_target_type == TypeManager.sbyte_type)
1327 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1328 if (real_target_type == TypeManager.byte_type)
1329 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1330 if (real_target_type == TypeManager.short_type)
1331 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1332 if (real_target_type == TypeManager.ushort_type)
1333 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1334 if (real_target_type == TypeManager.int32_type)
1335 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1336 if (real_target_type == TypeManager.uint32_type)
1337 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1338 if (real_target_type == TypeManager.int64_type)
1339 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1340 if (real_target_type == TypeManager.uint64_type)
1341 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1342 if (real_target_type == TypeManager.char_type)
1343 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1344 } else if (expr_type == TypeManager.double_type){
1346 // From double to byte, byte, short,
1347 // ushort, int, uint, long, ulong,
1348 // char, float or decimal
1350 if (real_target_type == TypeManager.sbyte_type)
1351 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1352 if (real_target_type == TypeManager.byte_type)
1353 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1354 if (real_target_type == TypeManager.short_type)
1355 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1356 if (real_target_type == TypeManager.ushort_type)
1357 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1358 if (real_target_type == TypeManager.int32_type)
1359 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1360 if (real_target_type == TypeManager.uint32_type)
1361 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1362 if (real_target_type == TypeManager.int64_type)
1363 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1364 if (real_target_type == TypeManager.uint64_type)
1365 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1366 if (real_target_type == TypeManager.char_type)
1367 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1368 if (real_target_type == TypeManager.float_type)
1369 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1372 // decimal is taken care of by the op_Explicit methods.
1378 /// Returns whether an explicit reference conversion can be performed
1379 /// from source_type to target_type
1381 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1383 bool target_is_value_type = target_type.IsValueType;
1385 if (source_type == target_type)
1389 // From object to any reference type
1391 if (source_type == TypeManager.object_type && !target_is_value_type)
1395 // From any class S to any class-type T, provided S is a base class of T
1397 if (target_type.IsSubclassOf (source_type))
1401 // From any interface type S to any interface T provided S is not derived from T
1403 if (source_type.IsInterface && target_type.IsInterface){
1404 if (!target_type.IsSubclassOf (source_type))
1409 // From any class type S to any interface T, provided S is not sealed
1410 // and provided S does not implement T.
1412 if (target_type.IsInterface && !source_type.IsSealed &&
1413 !TypeManager.ImplementsInterface (source_type, target_type))
1417 // From any interface-type S to to any class type T, provided T is not
1418 // sealed, or provided T implements S.
1420 if (source_type.IsInterface &&
1421 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1425 // From an array type S with an element type Se to an array type T with an
1426 // element type Te provided all the following are true:
1427 // * S and T differe only in element type, in other words, S and T
1428 // have the same number of dimensions.
1429 // * Both Se and Te are reference types
1430 // * An explicit referenc conversions exist from Se to Te
1432 if (source_type.IsArray && target_type.IsArray) {
1433 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1435 Type source_element_type = source_type.GetElementType ();
1436 Type target_element_type = target_type.GetElementType ();
1438 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1439 if (ExplicitReferenceConversionExists (source_element_type,
1440 target_element_type))
1446 // From System.Array to any array-type
1447 if (source_type == TypeManager.array_type &&
1448 target_type.IsArray){
1453 // From System delegate to any delegate-type
1455 if (source_type == TypeManager.delegate_type &&
1456 target_type.IsSubclassOf (TypeManager.delegate_type))
1460 // From ICloneable to Array or Delegate types
1462 if (source_type == TypeManager.icloneable_type &&
1463 (target_type == TypeManager.array_type ||
1464 target_type == TypeManager.delegate_type))
1471 /// Implements Explicit Reference conversions
1473 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1475 Type source_type = source.Type;
1476 bool target_is_value_type = target_type.IsValueType;
1479 // From object to any reference type
1481 if (source_type == TypeManager.object_type && !target_is_value_type)
1482 return new ClassCast (source, target_type);
1486 // From any class S to any class-type T, provided S is a base class of T
1488 if (target_type.IsSubclassOf (source_type))
1489 return new ClassCast (source, target_type);
1492 // From any interface type S to any interface T provided S is not derived from T
1494 if (source_type.IsInterface && target_type.IsInterface){
1495 if (TypeManager.ImplementsInterface (source_type, target_type))
1498 return new ClassCast (source, target_type);
1502 // From any class type S to any interface T, provides S is not sealed
1503 // and provided S does not implement T.
1505 if (target_type.IsInterface && !source_type.IsSealed) {
1506 if (TypeManager.ImplementsInterface (source_type, target_type))
1509 return new ClassCast (source, target_type);
1514 // From any interface-type S to to any class type T, provided T is not
1515 // sealed, or provided T implements S.
1517 if (source_type.IsInterface) {
1518 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
1519 return new ClassCast (source, target_type);
1524 // From an array type S with an element type Se to an array type T with an
1525 // element type Te provided all the following are true:
1526 // * S and T differe only in element type, in other words, S and T
1527 // have the same number of dimensions.
1528 // * Both Se and Te are reference types
1529 // * An explicit referenc conversions exist from Se to Te
1531 if (source_type.IsArray && target_type.IsArray) {
1532 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1534 Type source_element_type = source_type.GetElementType ();
1535 Type target_element_type = target_type.GetElementType ();
1537 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1538 if (ExplicitReferenceConversionExists (source_element_type,
1539 target_element_type))
1540 return new ClassCast (source, target_type);
1545 // From System.Array to any array-type
1546 if (source_type == TypeManager.array_type &&
1547 target_type.IsArray) {
1548 return new ClassCast (source, target_type);
1552 // From System delegate to any delegate-type
1554 if (source_type == TypeManager.delegate_type &&
1555 target_type.IsSubclassOf (TypeManager.delegate_type))
1556 return new ClassCast (source, target_type);
1559 // From ICloneable to Array or Delegate types
1561 if (source_type == TypeManager.icloneable_type &&
1562 (target_type == TypeManager.array_type ||
1563 target_type == TypeManager.delegate_type))
1564 return new ClassCast (source, target_type);
1570 /// Performs an explicit conversion of the expression `expr' whose
1571 /// type is expr.Type to `target_type'.
1573 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1574 Type target_type, Location loc)
1576 Type expr_type = expr.Type;
1577 Type original_expr_type = expr_type;
1579 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1580 if (target_type == TypeManager.enum_type ||
1581 target_type == TypeManager.object_type) {
1582 if (expr is EnumConstant)
1583 expr = ((EnumConstant) expr).Child;
1584 // We really need all these casts here .... :-(
1585 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1586 return new EmptyCast (expr, target_type);
1587 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1588 target_type.IsSubclassOf (TypeManager.enum_type))
1589 return new UnboxCast (expr, target_type);
1592 // Notice that we have kept the expr_type unmodified, which is only
1594 if (expr is EnumConstant)
1595 expr = ((EnumConstant) expr).Child;
1597 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1598 expr_type = expr.Type;
1601 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1606 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1611 // Unboxing conversion.
1613 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1614 if (expr is NullLiteral){
1615 Report.Error (37, "Cannot convert null to value type `" +
1616 TypeManager.CSharpName (expr_type) + "'");
1619 return new UnboxCast (expr, target_type);
1623 ne = ExplicitReferenceConversion (expr, target_type);
1628 if (target_type.IsPointer){
1629 if (expr_type.IsPointer)
1630 return new EmptyCast (expr, target_type);
1632 if (expr_type == TypeManager.sbyte_type ||
1633 expr_type == TypeManager.byte_type ||
1634 expr_type == TypeManager.short_type ||
1635 expr_type == TypeManager.ushort_type ||
1636 expr_type == TypeManager.int32_type ||
1637 expr_type == TypeManager.uint32_type ||
1638 expr_type == TypeManager.uint64_type ||
1639 expr_type == TypeManager.int64_type)
1640 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1642 if (expr_type.IsPointer){
1643 if (target_type == TypeManager.sbyte_type ||
1644 target_type == TypeManager.byte_type ||
1645 target_type == TypeManager.short_type ||
1646 target_type == TypeManager.ushort_type ||
1647 target_type == TypeManager.int32_type ||
1648 target_type == TypeManager.uint32_type ||
1649 target_type == TypeManager.uint64_type ||
1650 target_type == TypeManager.int64_type){
1651 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1654 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1659 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1663 // We should always be able to go from an uint32
1664 // implicitly or explicitly to the other integral
1667 throw new Exception ("Internal compiler error");
1672 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1676 Error_CannotConvertType (loc, original_expr_type, target_type);
1681 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1683 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1684 Type target_type, Location l)
1686 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1691 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1695 ne = ExplicitReferenceConversion (expr, target_type);
1699 Error_CannotConvertType (l, expr.Type, target_type);