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);
60 } else if (target_type == TypeManager.value_type) {
61 if (expr_type.IsValueType)
62 return new BoxedCast (expr);
63 if (expr is NullLiteral)
64 return new NullCast (expr, target_type);
67 } else if (expr_type.IsSubclassOf (target_type)) {
69 // Special case: enumeration to System.Enum.
70 // System.Enum is not a value type, it is a class, so we need
71 // a boxing conversion
74 return new BoxedCast (expr);
76 return new EmptyCast (expr, target_type);
79 // This code is kind of mirrored inside ImplicitStandardConversionExists
80 // with the small distinction that we only probe there
82 // Always ensure that the code here and there is in sync
84 // from the null type to any reference-type.
85 if (expr is NullLiteral){
86 if (target_type.IsPointer)
87 return NullPointer.Null;
89 if (!target_type.IsValueType)
90 return new NullCast (expr, target_type);
93 // from any class-type S to any interface-type T.
94 if (target_type.IsInterface) {
95 if (target_type != TypeManager.iconvertible_type &&
96 expr_type.IsValueType && (expr is Constant) &&
97 !(expr is IntLiteral || expr is BoolLiteral ||
98 expr is FloatLiteral || expr is DoubleLiteral ||
99 expr is LongLiteral || expr is CharLiteral ||
100 expr is StringLiteral || expr is DecimalLiteral ||
101 expr is UIntLiteral || expr is ULongLiteral)) {
105 if (TypeManager.ImplementsInterface (expr_type, target_type)){
106 if (expr_type.IsClass)
107 return new EmptyCast (expr, target_type);
108 else if (expr_type.IsValueType)
109 return new BoxedCast (expr, target_type);
111 return new EmptyCast (expr, target_type);
115 // from any interface type S to interface-type T.
116 if (expr_type.IsInterface && target_type.IsInterface) {
117 if (TypeManager.ImplementsInterface (expr_type, target_type))
118 return new EmptyCast (expr, target_type);
123 // from an array-type S to an array-type of type T
124 if (expr_type.IsArray && target_type.IsArray) {
125 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
127 Type expr_element_type = TypeManager.GetElementType (expr_type);
129 if (MyEmptyExpr == null)
130 MyEmptyExpr = new EmptyExpression ();
132 MyEmptyExpr.SetType (expr_element_type);
133 Type target_element_type = TypeManager.GetElementType (target_type);
135 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
136 if (ImplicitStandardConversionExists (MyEmptyExpr,
137 target_element_type))
138 return new EmptyCast (expr, target_type);
142 // from an array-type to System.Array
143 if (expr_type.IsArray && target_type == TypeManager.array_type)
144 return new EmptyCast (expr, target_type);
146 // from any delegate type to System.Delegate
147 if ((expr_type == TypeManager.delegate_type ||
148 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
149 target_type == TypeManager.delegate_type)
150 return new EmptyCast (expr, target_type);
152 // from any array-type or delegate type into System.ICloneable.
153 if (expr_type.IsArray ||
154 expr_type == TypeManager.delegate_type ||
155 expr_type.IsSubclassOf (TypeManager.delegate_type))
156 if (target_type == TypeManager.icloneable_type)
157 return new EmptyCast (expr, target_type);
163 // Tests whether an implicit reference conversion exists between expr_type
166 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
168 Type expr_type = expr.Type;
171 // This is the boxed case.
173 if (target_type == TypeManager.object_type) {
174 if (expr_type.IsClass || expr_type.IsValueType ||
175 expr_type.IsInterface || expr_type == TypeManager.enum_type)
179 } else if (expr_type.IsSubclassOf (target_type))
182 // Please remember that all code below actually comes
183 // from ImplicitReferenceConversion so make sure code remains in sync
185 // from any class-type S to any interface-type T.
186 if (target_type.IsInterface) {
187 if (TypeManager.ImplementsInterface (expr_type, target_type))
191 // from any interface type S to interface-type T.
192 if (expr_type.IsInterface && target_type.IsInterface)
193 if (TypeManager.ImplementsInterface (expr_type, target_type))
196 // from an array-type S to an array-type of type T
197 if (expr_type.IsArray && target_type.IsArray) {
198 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
200 Type expr_element_type = expr_type.GetElementType ();
202 if (MyEmptyExpr == null)
203 MyEmptyExpr = new EmptyExpression ();
205 MyEmptyExpr.SetType (expr_element_type);
206 Type target_element_type = TypeManager.GetElementType (target_type);
208 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
209 if (ImplicitStandardConversionExists (MyEmptyExpr,
210 target_element_type))
215 // from an array-type to System.Array
216 if (expr_type.IsArray && (target_type == TypeManager.array_type))
219 // from any delegate type to System.Delegate
220 if ((expr_type == TypeManager.delegate_type ||
221 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
222 target_type == TypeManager.delegate_type)
223 if (target_type.IsAssignableFrom (expr_type))
226 // from any array-type or delegate type into System.ICloneable.
227 if (expr_type.IsArray ||
228 expr_type == TypeManager.delegate_type ||
229 expr_type.IsSubclassOf (TypeManager.delegate_type))
230 if (target_type == TypeManager.icloneable_type)
233 // from the null type to any reference-type.
234 if (expr is NullLiteral && !target_type.IsValueType && !TypeManager.IsEnumType (target_type))
241 /// Implicit Numeric Conversions.
243 /// expr is the expression to convert, returns a new expression of type
244 /// target_type or null if an implicit conversion is not possible.
246 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
247 Type target_type, Location loc)
249 Type expr_type = expr.Type;
252 // Attempt to do the implicit constant expression conversions
254 if (expr is Constant){
255 if (expr is IntConstant){
258 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
262 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
264 // Try the implicit constant expression conversion
265 // from long to ulong, instead of a nice routine,
268 long v = ((LongConstant) expr).Value;
270 return new ULongConstant ((ulong) v);
274 Type real_target_type = target_type;
276 if (expr_type == TypeManager.sbyte_type){
278 // From sbyte to short, int, long, float, double.
280 if (real_target_type == TypeManager.int32_type)
281 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
282 if (real_target_type == TypeManager.int64_type)
283 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
284 if (real_target_type == TypeManager.double_type)
285 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
286 if (real_target_type == TypeManager.float_type)
287 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
288 if (real_target_type == TypeManager.short_type)
289 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
290 } else if (expr_type == TypeManager.byte_type){
292 // From byte to short, ushort, int, uint, long, ulong, float, double
294 if ((real_target_type == TypeManager.short_type) ||
295 (real_target_type == TypeManager.ushort_type) ||
296 (real_target_type == TypeManager.int32_type) ||
297 (real_target_type == TypeManager.uint32_type))
298 return new EmptyCast (expr, target_type);
300 if (real_target_type == TypeManager.uint64_type)
301 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
302 if (real_target_type == TypeManager.int64_type)
303 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
304 if (real_target_type == TypeManager.float_type)
305 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
306 if (real_target_type == TypeManager.double_type)
307 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
308 } else if (expr_type == TypeManager.short_type){
310 // From short to int, long, float, double
312 if (real_target_type == TypeManager.int32_type)
313 return new EmptyCast (expr, target_type);
314 if (real_target_type == TypeManager.int64_type)
315 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
316 if (real_target_type == TypeManager.double_type)
317 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
318 if (real_target_type == TypeManager.float_type)
319 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
320 } else if (expr_type == TypeManager.ushort_type){
322 // From ushort to int, uint, long, ulong, float, double
324 if (real_target_type == TypeManager.uint32_type)
325 return new EmptyCast (expr, target_type);
327 if (real_target_type == TypeManager.uint64_type)
328 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
329 if (real_target_type == TypeManager.int32_type)
330 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
331 if (real_target_type == TypeManager.int64_type)
332 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
333 if (real_target_type == TypeManager.double_type)
334 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
335 if (real_target_type == TypeManager.float_type)
336 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
337 } else if (expr_type == TypeManager.int32_type){
339 // From int to long, float, double
341 if (real_target_type == TypeManager.int64_type)
342 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
343 if (real_target_type == TypeManager.double_type)
344 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
345 if (real_target_type == TypeManager.float_type)
346 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
347 } else if (expr_type == TypeManager.uint32_type){
349 // From uint to long, ulong, float, double
351 if (real_target_type == TypeManager.int64_type)
352 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
353 if (real_target_type == TypeManager.uint64_type)
354 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
355 if (real_target_type == TypeManager.double_type)
356 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
358 if (real_target_type == TypeManager.float_type)
359 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
361 } else if (expr_type == TypeManager.int64_type){
363 // From long/ulong to float, double
365 if (real_target_type == TypeManager.double_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
367 if (real_target_type == TypeManager.float_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
369 } else if (expr_type == TypeManager.uint64_type){
371 // From ulong to float, double
373 if (real_target_type == TypeManager.double_type)
374 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
376 if (real_target_type == TypeManager.float_type)
377 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
379 } else if (expr_type == TypeManager.char_type){
381 // From char to ushort, int, uint, long, ulong, float, double
383 if ((real_target_type == TypeManager.ushort_type) ||
384 (real_target_type == TypeManager.int32_type) ||
385 (real_target_type == TypeManager.uint32_type))
386 return new EmptyCast (expr, target_type);
387 if (real_target_type == TypeManager.uint64_type)
388 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
389 if (real_target_type == TypeManager.int64_type)
390 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
391 if (real_target_type == TypeManager.float_type)
392 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
393 if (real_target_type == TypeManager.double_type)
394 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
395 } else if (expr_type == TypeManager.float_type){
399 if (real_target_type == TypeManager.double_type)
400 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
408 /// Same as ImplicitStandardConversionExists except that it also looks at
409 /// implicit user defined conversions - needed for overload resolution
411 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
413 if (ImplicitStandardConversionExists (expr, target_type))
416 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
424 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
426 Expression dummy = ImplicitUserConversion (
427 ec, new EmptyExpression (source), target, Location.Null);
428 return dummy != null;
432 /// Determines if a standard implicit conversion exists from
433 /// expr_type to target_type
435 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
437 Type expr_type = expr.Type;
439 if (expr_type == TypeManager.void_type)
442 if (expr_type == target_type)
445 // First numeric conversions
447 if (expr_type == TypeManager.sbyte_type){
449 // From sbyte to short, int, long, float, double.
451 if ((target_type == TypeManager.int32_type) ||
452 (target_type == TypeManager.int64_type) ||
453 (target_type == TypeManager.double_type) ||
454 (target_type == TypeManager.float_type) ||
455 (target_type == TypeManager.short_type) ||
456 (target_type == TypeManager.decimal_type))
459 } else if (expr_type == TypeManager.byte_type){
461 // From byte to short, ushort, int, uint, long, ulong, float, double
463 if ((target_type == TypeManager.short_type) ||
464 (target_type == TypeManager.ushort_type) ||
465 (target_type == TypeManager.int32_type) ||
466 (target_type == TypeManager.uint32_type) ||
467 (target_type == TypeManager.uint64_type) ||
468 (target_type == TypeManager.int64_type) ||
469 (target_type == TypeManager.float_type) ||
470 (target_type == TypeManager.double_type) ||
471 (target_type == TypeManager.decimal_type))
474 } else if (expr_type == TypeManager.short_type){
476 // From short to int, long, float, double
478 if ((target_type == TypeManager.int32_type) ||
479 (target_type == TypeManager.int64_type) ||
480 (target_type == TypeManager.double_type) ||
481 (target_type == TypeManager.float_type) ||
482 (target_type == TypeManager.decimal_type))
485 } else if (expr_type == TypeManager.ushort_type){
487 // From ushort to int, uint, long, ulong, float, double
489 if ((target_type == TypeManager.uint32_type) ||
490 (target_type == TypeManager.uint64_type) ||
491 (target_type == TypeManager.int32_type) ||
492 (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.int32_type){
500 // From int to long, float, double
502 if ((target_type == TypeManager.int64_type) ||
503 (target_type == TypeManager.double_type) ||
504 (target_type == TypeManager.float_type) ||
505 (target_type == TypeManager.decimal_type))
508 } else if (expr_type == TypeManager.uint32_type){
510 // From uint to long, ulong, float, double
512 if ((target_type == TypeManager.int64_type) ||
513 (target_type == TypeManager.uint64_type) ||
514 (target_type == TypeManager.double_type) ||
515 (target_type == TypeManager.float_type) ||
516 (target_type == TypeManager.decimal_type))
519 } else if ((expr_type == TypeManager.uint64_type) ||
520 (expr_type == TypeManager.int64_type)) {
522 // From long/ulong to float, double
524 if ((target_type == TypeManager.double_type) ||
525 (target_type == TypeManager.float_type) ||
526 (target_type == TypeManager.decimal_type))
529 } else if (expr_type == TypeManager.char_type){
531 // From char to ushort, int, uint, long, ulong, float, double
533 if ((target_type == TypeManager.ushort_type) ||
534 (target_type == TypeManager.int32_type) ||
535 (target_type == TypeManager.uint32_type) ||
536 (target_type == TypeManager.uint64_type) ||
537 (target_type == TypeManager.int64_type) ||
538 (target_type == TypeManager.float_type) ||
539 (target_type == TypeManager.double_type) ||
540 (target_type == TypeManager.decimal_type))
543 } else if (expr_type == TypeManager.float_type){
547 if (target_type == TypeManager.double_type)
551 if (ImplicitReferenceConversionExists (expr, target_type))
555 // Implicit Constant Expression Conversions
557 if (expr is IntConstant){
558 int value = ((IntConstant) expr).Value;
560 if (target_type == TypeManager.sbyte_type){
561 if (value >= SByte.MinValue && value <= SByte.MaxValue)
563 } else if (target_type == TypeManager.byte_type){
564 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
566 } else if (target_type == TypeManager.short_type){
567 if (value >= Int16.MinValue && value <= Int16.MaxValue)
569 } else if (target_type == TypeManager.ushort_type){
570 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
572 } else if (target_type == TypeManager.uint32_type){
575 } else if (target_type == TypeManager.uint64_type){
577 // we can optimize this case: a positive int32
578 // always fits on a uint64. But we need an opcode
585 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
589 if (expr is LongConstant && target_type == TypeManager.uint64_type){
591 // Try the implicit constant expression conversion
592 // from long to ulong, instead of a nice routine,
595 long v = ((LongConstant) expr).Value;
600 if ((target_type == TypeManager.enum_type ||
601 target_type.IsSubclassOf (TypeManager.enum_type)) &&
603 IntLiteral i = (IntLiteral) expr;
609 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
616 // Used internally by FindMostEncompassedType, this is used
617 // to avoid creating lots of objects in the tight loop inside
618 // FindMostEncompassedType
620 static EmptyExpression priv_fmet_param;
623 /// Finds "most encompassed type" according to the spec (13.4.2)
624 /// amongst the methods in the MethodGroupExpr
626 static Type FindMostEncompassedType (ArrayList types)
630 if (priv_fmet_param == null)
631 priv_fmet_param = new EmptyExpression ();
633 foreach (Type t in types){
634 priv_fmet_param.SetType (t);
641 if (ImplicitStandardConversionExists (priv_fmet_param, best))
649 // Used internally by FindMostEncompassingType, this is used
650 // to avoid creating lots of objects in the tight loop inside
651 // FindMostEncompassingType
653 static EmptyExpression priv_fmee_ret;
656 /// Finds "most encompassing type" according to the spec (13.4.2)
657 /// amongst the types in the given set
659 static Type FindMostEncompassingType (ArrayList types)
663 if (priv_fmee_ret == null)
664 priv_fmee_ret = new EmptyExpression ();
666 foreach (Type t in types){
667 priv_fmee_ret.SetType (best);
674 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
682 // Used to avoid creating too many objects
684 static EmptyExpression priv_fms_expr;
687 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
688 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
689 /// for explicit and implicit conversion operators.
691 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
692 bool apply_explicit_conv_rules,
695 ArrayList src_types_set = new ArrayList ();
697 if (priv_fms_expr == null)
698 priv_fms_expr = new EmptyExpression ();
701 // If any operator converts from S then Sx = S
703 Type source_type = source.Type;
704 foreach (MethodBase mb in me.Methods){
705 ParameterData pd = Invocation.GetParameterData (mb);
706 Type param_type = pd.ParameterType (0);
708 if (param_type == source_type)
711 if (apply_explicit_conv_rules) {
714 // Find the set of applicable user-defined conversion operators, U. This set
716 // user-defined implicit or explicit conversion operators declared by
717 // the classes or structs in D that convert from a type encompassing
718 // or encompassed by S to a type encompassing or encompassed by T
720 priv_fms_expr.SetType (param_type);
721 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
722 src_types_set.Add (param_type);
724 if (ImplicitStandardConversionExists (source, param_type))
725 src_types_set.Add (param_type);
729 // Only if S is encompassed by param_type
731 if (ImplicitStandardConversionExists (source, param_type))
732 src_types_set.Add (param_type);
737 // Explicit Conv rules
739 if (apply_explicit_conv_rules) {
740 ArrayList candidate_set = new ArrayList ();
742 foreach (Type param_type in src_types_set){
743 if (ImplicitStandardConversionExists (source, param_type))
744 candidate_set.Add (param_type);
747 if (candidate_set.Count != 0)
748 return FindMostEncompassedType (candidate_set);
754 if (apply_explicit_conv_rules)
755 return FindMostEncompassingType (src_types_set);
757 return FindMostEncompassedType (src_types_set);
761 // Useful in avoiding proliferation of objects
763 static EmptyExpression priv_fmt_expr;
766 /// Finds the most specific target Tx according to section 13.4.4
768 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
769 bool apply_explicit_conv_rules,
772 ArrayList tgt_types_set = new ArrayList ();
774 if (priv_fmt_expr == null)
775 priv_fmt_expr = new EmptyExpression ();
778 // If any operator converts to T then Tx = T
780 foreach (MethodInfo mi in me.Methods){
781 Type ret_type = mi.ReturnType;
783 if (ret_type == target)
786 if (apply_explicit_conv_rules) {
789 // Find the set of applicable user-defined conversion operators, U.
791 // This set consists of the
792 // user-defined implicit or explicit conversion operators declared by
793 // the classes or structs in D that convert from a type encompassing
794 // or encompassed by S to a type encompassing or encompassed by T
796 priv_fms_expr.SetType (ret_type);
797 if (ImplicitStandardConversionExists (priv_fms_expr, target))
798 tgt_types_set.Add (ret_type);
800 priv_fms_expr.SetType (target);
801 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
802 tgt_types_set.Add (ret_type);
806 // Only if T is encompassed by param_type
808 priv_fms_expr.SetType (ret_type);
809 if (ImplicitStandardConversionExists (priv_fms_expr, target))
810 tgt_types_set.Add (ret_type);
815 // Explicit conv rules
817 if (apply_explicit_conv_rules) {
818 ArrayList candidate_set = new ArrayList ();
820 foreach (Type ret_type in tgt_types_set){
821 priv_fmt_expr.SetType (ret_type);
823 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
824 candidate_set.Add (ret_type);
827 if (candidate_set.Count != 0)
828 return FindMostEncompassingType (candidate_set);
832 // Okay, final case !
834 if (apply_explicit_conv_rules)
835 return FindMostEncompassedType (tgt_types_set);
837 return FindMostEncompassingType (tgt_types_set);
841 /// User-defined Implicit conversions
843 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
844 Type target, Location loc)
846 return UserDefinedConversion (ec, source, target, loc, false);
850 /// User-defined Explicit conversions
852 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
853 Type target, Location loc)
855 return UserDefinedConversion (ec, source, target, loc, true);
858 static DoubleHash explicit_conv = new DoubleHash (100);
859 static DoubleHash implicit_conv = new DoubleHash (100);
861 /// Computes the MethodGroup for the user-defined conversion
862 /// operators from source_type to target_type. `look_for_explicit'
863 /// controls whether we should also include the list of explicit
866 static MethodGroupExpr GetConversionOperators (EmitContext ec,
867 Type source_type, Type target_type,
868 Location loc, bool look_for_explicit)
870 Expression mg1 = null, mg2 = null;
871 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
874 op_name = "op_Implicit";
876 MethodGroupExpr union3;
878 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
879 return (MethodGroupExpr) r;
881 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
882 if (source_type.BaseType != null)
883 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
886 union3 = (MethodGroupExpr) mg2;
887 else if (mg2 == null)
888 union3 = (MethodGroupExpr) mg1;
890 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
892 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
895 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
897 union3 = (MethodGroupExpr) mg1;
900 if (target_type.BaseType != null)
901 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
905 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
907 union3 = (MethodGroupExpr) mg1;
910 MethodGroupExpr union4 = null;
912 if (look_for_explicit) {
913 op_name = "op_Explicit";
915 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
916 if (source_type.BaseType != null)
917 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
919 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
920 if (target_type.BaseType != null)
921 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
923 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
924 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
926 union4 = Invocation.MakeUnionSet (union5, union6, loc);
929 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
930 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
935 /// User-defined conversions
937 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
938 Type target, Location loc,
939 bool look_for_explicit)
941 MethodGroupExpr union;
942 Type source_type = source.Type;
943 MethodBase method = null;
945 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
949 Type most_specific_source, most_specific_target;
951 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
952 if (most_specific_source == null)
955 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
956 if (most_specific_target == null)
962 foreach (MethodBase mb in union.Methods){
963 ParameterData pd = Invocation.GetParameterData (mb);
964 MethodInfo mi = (MethodInfo) mb;
966 if (pd.ParameterType (0) == most_specific_source &&
967 mi.ReturnType == most_specific_target) {
973 if (method == null || count > 1)
978 // This will do the conversion to the best match that we
979 // found. Now we need to perform an implict standard conversion
980 // if the best match was not the type that we were requested
983 if (look_for_explicit)
984 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
986 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
992 e = new UserCast ((MethodInfo) method, source, loc);
993 if (e.Type != target){
994 if (!look_for_explicit)
995 e = ImplicitConversionStandard (ec, e, target, loc);
997 e = ExplicitConversionStandard (ec, e, target, loc);
1004 /// Converts implicitly the resolved expression `expr' into the
1005 /// `target_type'. It returns a new expression that can be used
1006 /// in a context that expects a `target_type'.
1008 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1009 Type target_type, Location loc)
1013 if (target_type == null)
1014 throw new Exception ("Target type is null");
1016 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1020 e = ImplicitUserConversion (ec, expr, target_type, loc);
1029 /// Attempts to apply the `Standard Implicit
1030 /// Conversion' rules to the expression `expr' into
1031 /// the `target_type'. It returns a new expression
1032 /// that can be used in a context that expects a
1035 /// This is different from `ImplicitConversion' in that the
1036 /// user defined implicit conversions are excluded.
1038 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1039 Type target_type, Location loc)
1041 Type expr_type = expr.Type;
1044 if (expr.eclass == ExprClass.MethodGroup){
1045 if (!TypeManager.IsDelegateType (target_type)){
1046 Report.Error (428, loc,
1048 "Cannot convert method group to `{0}', since it is not a delegate",
1049 TypeManager.CSharpName (target_type)));
1053 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1056 if (expr_type == target_type && !(expr is NullLiteral))
1059 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1063 e = ImplicitReferenceConversion (expr, target_type);
1067 if ((target_type == TypeManager.enum_type ||
1068 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1069 expr is IntLiteral){
1070 IntLiteral i = (IntLiteral) expr;
1073 return new EnumConstant ((Constant) expr, target_type);
1077 if (expr_type.IsPointer){
1078 if (target_type == TypeManager.void_ptr_type)
1079 return new EmptyCast (expr, target_type);
1082 // yep, comparing pointer types cant be done with
1083 // t1 == t2, we have to compare their element types.
1085 if (target_type.IsPointer){
1086 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1091 if (target_type.IsPointer) {
1092 if (expr is NullLiteral)
1093 return new EmptyCast (expr, target_type);
1095 if (expr_type == TypeManager.void_ptr_type)
1096 return new EmptyCast (expr, target_type);
1104 /// Attemps to perform an implict constant conversion of the IntConstant
1105 /// into a different data type using casts (See Implicit Constant
1106 /// Expression Conversions)
1108 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1110 int value = ic.Value;
1112 if (target_type == TypeManager.sbyte_type){
1113 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1114 return new SByteConstant ((sbyte) value);
1115 } else if (target_type == TypeManager.byte_type){
1116 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1117 return new ByteConstant ((byte) value);
1118 } else if (target_type == TypeManager.short_type){
1119 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1120 return new ShortConstant ((short) value);
1121 } else if (target_type == TypeManager.ushort_type){
1122 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1123 return new UShortConstant ((ushort) value);
1124 } else if (target_type == TypeManager.uint32_type){
1126 return new UIntConstant ((uint) value);
1127 } else if (target_type == TypeManager.uint64_type){
1129 // we can optimize this case: a positive int32
1130 // always fits on a uint64. But we need an opcode
1134 return new ULongConstant ((ulong) value);
1135 } else if (target_type == TypeManager.double_type)
1136 return new DoubleConstant ((double) value);
1137 else if (target_type == TypeManager.float_type)
1138 return new FloatConstant ((float) value);
1140 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1141 Type underlying = TypeManager.EnumToUnderlying (target_type);
1142 Constant e = (Constant) ic;
1145 // Possibly, we need to create a different 0 literal before passing
1148 if (underlying == TypeManager.int64_type)
1149 e = new LongLiteral (0);
1150 else if (underlying == TypeManager.uint64_type)
1151 e = new ULongLiteral (0);
1153 return new EnumConstant (e, target_type);
1158 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1160 string msg = "Cannot convert implicitly from `"+
1161 TypeManager.CSharpName (source) + "' to `" +
1162 TypeManager.CSharpName (target) + "'";
1164 Report.Error (29, loc, msg);
1168 /// Attemptes to implicityly convert `target' into `type', using
1169 /// ImplicitConversion. If there is no implicit conversion, then
1170 /// an error is signaled
1172 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1173 Type target_type, Location loc)
1177 e = ImplicitConversion (ec, source, target_type, loc);
1181 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1182 Report.Error (664, loc,
1183 "Double literal cannot be implicitly converted to " +
1184 "float type, use F suffix to create a float literal");
1187 if (source is Constant){
1188 Constant c = (Constant) source;
1190 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1194 Error_CannotImplicitConversion (loc, source.Type, target_type);
1200 /// Performs the explicit numeric conversions
1202 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1204 Type expr_type = expr.Type;
1207 // If we have an enumeration, extract the underlying type,
1208 // use this during the comparison, but wrap around the original
1211 Type real_target_type = target_type;
1213 if (TypeManager.IsEnumType (real_target_type))
1214 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1216 if (ImplicitStandardConversionExists (expr, real_target_type)){
1217 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1219 if (real_target_type != target_type)
1220 return new EmptyCast (ce, target_type);
1224 if (expr_type == TypeManager.sbyte_type){
1226 // From sbyte to byte, ushort, uint, ulong, char
1228 if (real_target_type == TypeManager.byte_type)
1229 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1230 if (real_target_type == TypeManager.ushort_type)
1231 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1232 if (real_target_type == TypeManager.uint32_type)
1233 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1234 if (real_target_type == TypeManager.uint64_type)
1235 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1236 if (real_target_type == TypeManager.char_type)
1237 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1238 } else if (expr_type == TypeManager.byte_type){
1240 // From byte to sbyte and char
1242 if (real_target_type == TypeManager.sbyte_type)
1243 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1244 if (real_target_type == TypeManager.char_type)
1245 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1246 } else if (expr_type == TypeManager.short_type){
1248 // From short to sbyte, byte, ushort, uint, ulong, char
1250 if (real_target_type == TypeManager.sbyte_type)
1251 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1252 if (real_target_type == TypeManager.byte_type)
1253 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1254 if (real_target_type == TypeManager.ushort_type)
1255 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1256 if (real_target_type == TypeManager.uint32_type)
1257 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1258 if (real_target_type == TypeManager.uint64_type)
1259 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1260 if (real_target_type == TypeManager.char_type)
1261 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1262 } else if (expr_type == TypeManager.ushort_type){
1264 // From ushort to sbyte, byte, short, char
1266 if (real_target_type == TypeManager.sbyte_type)
1267 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1268 if (real_target_type == TypeManager.byte_type)
1269 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1270 if (real_target_type == TypeManager.short_type)
1271 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1272 if (real_target_type == TypeManager.char_type)
1273 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1274 } else if (expr_type == TypeManager.int32_type){
1276 // From int to sbyte, byte, short, ushort, uint, ulong, char
1278 if (real_target_type == TypeManager.sbyte_type)
1279 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1280 if (real_target_type == TypeManager.byte_type)
1281 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1282 if (real_target_type == TypeManager.short_type)
1283 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1284 if (real_target_type == TypeManager.ushort_type)
1285 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1286 if (real_target_type == TypeManager.uint32_type)
1287 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1288 if (real_target_type == TypeManager.uint64_type)
1289 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1290 if (real_target_type == TypeManager.char_type)
1291 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1292 } else if (expr_type == TypeManager.uint32_type){
1294 // From uint to sbyte, byte, short, ushort, int, char
1296 if (real_target_type == TypeManager.sbyte_type)
1297 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1298 if (real_target_type == TypeManager.byte_type)
1299 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1300 if (real_target_type == TypeManager.short_type)
1301 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1302 if (real_target_type == TypeManager.ushort_type)
1303 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1304 if (real_target_type == TypeManager.int32_type)
1305 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1306 if (real_target_type == TypeManager.char_type)
1307 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1308 } else if (expr_type == TypeManager.int64_type){
1310 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1312 if (real_target_type == TypeManager.sbyte_type)
1313 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1314 if (real_target_type == TypeManager.byte_type)
1315 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1316 if (real_target_type == TypeManager.short_type)
1317 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1318 if (real_target_type == TypeManager.ushort_type)
1319 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1320 if (real_target_type == TypeManager.int32_type)
1321 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1322 if (real_target_type == TypeManager.uint32_type)
1323 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1324 if (real_target_type == TypeManager.uint64_type)
1325 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1326 if (real_target_type == TypeManager.char_type)
1327 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1328 } else if (expr_type == TypeManager.uint64_type){
1330 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1332 if (real_target_type == TypeManager.sbyte_type)
1333 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1334 if (real_target_type == TypeManager.byte_type)
1335 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1336 if (real_target_type == TypeManager.short_type)
1337 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1338 if (real_target_type == TypeManager.ushort_type)
1339 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1340 if (real_target_type == TypeManager.int32_type)
1341 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1342 if (real_target_type == TypeManager.uint32_type)
1343 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1344 if (real_target_type == TypeManager.int64_type)
1345 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1346 if (real_target_type == TypeManager.char_type)
1347 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1348 } else if (expr_type == TypeManager.char_type){
1350 // From char to sbyte, byte, short
1352 if (real_target_type == TypeManager.sbyte_type)
1353 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1354 if (real_target_type == TypeManager.byte_type)
1355 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1356 if (real_target_type == TypeManager.short_type)
1357 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1358 } else if (expr_type == TypeManager.float_type){
1360 // From float to sbyte, byte, short,
1361 // ushort, int, uint, long, ulong, char
1364 if (real_target_type == TypeManager.sbyte_type)
1365 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1366 if (real_target_type == TypeManager.byte_type)
1367 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1368 if (real_target_type == TypeManager.short_type)
1369 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1370 if (real_target_type == TypeManager.ushort_type)
1371 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1372 if (real_target_type == TypeManager.int32_type)
1373 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1374 if (real_target_type == TypeManager.uint32_type)
1375 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1376 if (real_target_type == TypeManager.int64_type)
1377 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1378 if (real_target_type == TypeManager.uint64_type)
1379 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1380 if (real_target_type == TypeManager.char_type)
1381 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1382 } else if (expr_type == TypeManager.double_type){
1384 // From double to byte, byte, short,
1385 // ushort, int, uint, long, ulong,
1386 // char, float or decimal
1388 if (real_target_type == TypeManager.sbyte_type)
1389 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1390 if (real_target_type == TypeManager.byte_type)
1391 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1392 if (real_target_type == TypeManager.short_type)
1393 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1394 if (real_target_type == TypeManager.ushort_type)
1395 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1396 if (real_target_type == TypeManager.int32_type)
1397 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1398 if (real_target_type == TypeManager.uint32_type)
1399 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1400 if (real_target_type == TypeManager.int64_type)
1401 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1402 if (real_target_type == TypeManager.uint64_type)
1403 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1404 if (real_target_type == TypeManager.char_type)
1405 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1406 if (real_target_type == TypeManager.float_type)
1407 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1410 // decimal is taken care of by the op_Explicit methods.
1416 /// Returns whether an explicit reference conversion can be performed
1417 /// from source_type to target_type
1419 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1421 bool target_is_value_type = target_type.IsValueType;
1423 if (source_type == target_type)
1427 // From object to any reference type
1429 if (source_type == TypeManager.object_type && !target_is_value_type)
1433 // From any class S to any class-type T, provided S is a base class of T
1435 if (target_type.IsSubclassOf (source_type))
1439 // From any interface type S to any interface T provided S is not derived from T
1441 if (source_type.IsInterface && target_type.IsInterface){
1442 if (!target_type.IsSubclassOf (source_type))
1447 // From any class type S to any interface T, provided S is not sealed
1448 // and provided S does not implement T.
1450 if (target_type.IsInterface && !source_type.IsSealed &&
1451 !TypeManager.ImplementsInterface (source_type, target_type))
1455 // From any interface-type S to to any class type T, provided T is not
1456 // sealed, or provided T implements S.
1458 if (source_type.IsInterface &&
1459 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1463 // From an array type S with an element type Se to an array type T with an
1464 // element type Te provided all the following are true:
1465 // * S and T differe only in element type, in other words, S and T
1466 // have the same number of dimensions.
1467 // * Both Se and Te are reference types
1468 // * An explicit referenc conversions exist from Se to Te
1470 if (source_type.IsArray && target_type.IsArray) {
1471 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1473 Type source_element_type = TypeManager.GetElementType (source_type);
1474 Type target_element_type = TypeManager.GetElementType (target_type);
1476 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1477 if (ExplicitReferenceConversionExists (source_element_type,
1478 target_element_type))
1484 // From System.Array to any array-type
1485 if (source_type == TypeManager.array_type &&
1486 target_type.IsArray){
1491 // From System delegate to any delegate-type
1493 if (source_type == TypeManager.delegate_type &&
1494 target_type.IsSubclassOf (TypeManager.delegate_type))
1498 // From ICloneable to Array or Delegate types
1500 if (source_type == TypeManager.icloneable_type &&
1501 (target_type == TypeManager.array_type ||
1502 target_type == TypeManager.delegate_type))
1509 /// Implements Explicit Reference conversions
1511 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1513 Type source_type = source.Type;
1514 bool target_is_value_type = target_type.IsValueType;
1517 // From object to any reference type
1519 if (source_type == TypeManager.object_type && !target_is_value_type)
1520 return new ClassCast (source, target_type);
1523 // Unboxing conversion.
1525 if (((source_type == TypeManager.enum_type &&
1526 !(source is EmptyCast)) ||
1527 source_type == TypeManager.value_type) && target_is_value_type)
1528 return new UnboxCast (source, target_type);
1531 // From any class S to any class-type T, provided S is a base class of T
1533 if (target_type.IsSubclassOf (source_type))
1534 return new ClassCast (source, target_type);
1537 // From any interface type S to any interface T provided S is not derived from T
1539 if (source_type.IsInterface && target_type.IsInterface){
1540 if (TypeManager.ImplementsInterface (source_type, target_type))
1543 return new ClassCast (source, target_type);
1547 // From any class type S to any interface T, provides S is not sealed
1548 // and provided S does not implement T.
1550 if (target_type.IsInterface && !source_type.IsSealed) {
1551 if (TypeManager.ImplementsInterface (source_type, target_type))
1554 return new ClassCast (source, target_type);
1559 // From any interface-type S to to any class type T, provided T is not
1560 // sealed, or provided T implements S.
1562 if (source_type.IsInterface) {
1563 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1564 if (target_type.IsClass)
1565 return new ClassCast (source, target_type);
1567 return new UnboxCast (source, target_type);
1573 // From an array type S with an element type Se to an array type T with an
1574 // element type Te provided all the following are true:
1575 // * S and T differe only in element type, in other words, S and T
1576 // have the same number of dimensions.
1577 // * Both Se and Te are reference types
1578 // * An explicit referenc conversions exist from Se to Te
1580 if (source_type.IsArray && target_type.IsArray) {
1581 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1583 Type source_element_type = TypeManager.GetElementType (source_type);
1584 Type target_element_type = TypeManager.GetElementType (target_type);
1586 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1587 if (ExplicitReferenceConversionExists (source_element_type,
1588 target_element_type))
1589 return new ClassCast (source, target_type);
1594 // From System.Array to any array-type
1595 if (source_type == TypeManager.array_type &&
1596 target_type.IsArray) {
1597 return new ClassCast (source, target_type);
1601 // From System delegate to any delegate-type
1603 if (source_type == TypeManager.delegate_type &&
1604 target_type.IsSubclassOf (TypeManager.delegate_type))
1605 return new ClassCast (source, target_type);
1608 // From ICloneable to Array or Delegate types
1610 if (source_type == TypeManager.icloneable_type &&
1611 (target_type == TypeManager.array_type ||
1612 target_type == TypeManager.delegate_type))
1613 return new ClassCast (source, target_type);
1619 /// Performs an explicit conversion of the expression `expr' whose
1620 /// type is expr.Type to `target_type'.
1622 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1623 Type target_type, Location loc)
1625 Type expr_type = expr.Type;
1626 Type original_expr_type = expr_type;
1628 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1629 if (target_type == TypeManager.enum_type ||
1630 target_type == TypeManager.object_type) {
1631 if (expr is EnumConstant)
1632 expr = ((EnumConstant) expr).Child;
1633 // We really need all these casts here .... :-(
1634 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1635 return new EmptyCast (expr, target_type);
1636 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1637 target_type.IsSubclassOf (TypeManager.enum_type))
1638 return new UnboxCast (expr, target_type);
1641 // Notice that we have kept the expr_type unmodified, which is only
1643 if (expr is EnumConstant)
1644 expr = ((EnumConstant) expr).Child;
1646 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1647 expr_type = expr.Type;
1650 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1655 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1660 // Unboxing conversion.
1662 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1663 if (expr is NullLiteral){
1665 // Skip the ExplicitReferenceConversion because we can not convert
1666 // from Null to a ValueType, and ExplicitReference wont check against
1667 // null literal explicitly
1671 return new UnboxCast (expr, target_type);
1675 ne = ExplicitReferenceConversion (expr, target_type);
1681 if (target_type.IsPointer){
1682 if (expr_type.IsPointer)
1683 return new EmptyCast (expr, target_type);
1685 if (expr_type == TypeManager.sbyte_type ||
1686 expr_type == TypeManager.byte_type ||
1687 expr_type == TypeManager.short_type ||
1688 expr_type == TypeManager.ushort_type ||
1689 expr_type == TypeManager.int32_type ||
1690 expr_type == TypeManager.uint32_type ||
1691 expr_type == TypeManager.uint64_type ||
1692 expr_type == TypeManager.int64_type)
1693 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1695 if (expr_type.IsPointer){
1696 Expression e = null;
1698 if (target_type == TypeManager.sbyte_type)
1699 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1700 else if (target_type == TypeManager.byte_type)
1701 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1702 else if (target_type == TypeManager.short_type)
1703 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1704 else if (target_type == TypeManager.ushort_type)
1705 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1706 else if (target_type == TypeManager.int32_type)
1707 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1708 else if (target_type == TypeManager.uint32_type)
1709 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1710 else if (target_type == TypeManager.uint64_type)
1711 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1712 else if (target_type == TypeManager.int64_type){
1713 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1719 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1724 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1728 // We should always be able to go from an uint32
1729 // implicitly or explicitly to the other integral
1732 throw new Exception ("Internal compiler error");
1737 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1741 if (expr is NullLiteral){
1742 Report.Error (37, loc, "Cannot convert null to value type `" +
1743 TypeManager.CSharpName (target_type) + "'");
1747 Error_CannotConvertType (loc, original_expr_type, target_type);
1752 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1754 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1755 Type target_type, Location l)
1757 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1762 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1766 ne = ExplicitReferenceConversion (expr, target_type);
1770 Error_CannotConvertType (l, expr.Type, target_type);