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 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 Expression TypeParameter_to_Null (Expression expr, Type target_type,
32 if (!TypeParameter_to_Null (target_type)) {
33 Report.Error (403, loc, "Cannot convert null to the type " +
34 "parameter `{0}' becaues it could be a value " +
35 "type. Consider using `default ({0})' instead.",
40 return new NullCast (expr, target_type);
43 static bool TypeParameter_to_Null (Type target_type)
45 if ((target_type.BaseType == null) ||
46 (target_type.BaseType == TypeManager.value_type) ||
47 target_type.BaseType.IsValueType)
53 static EmptyExpression MyEmptyExpr;
54 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
56 Type expr_type = expr.Type;
58 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
59 // if we are a method group, emit a warning
64 if (expr_type == TypeManager.void_type)
68 // notice that it is possible to write "ValueType v = 1", the ValueType here
69 // is an abstract class, and not really a value type, so we apply the same rules.
71 if (target_type == TypeManager.object_type) {
73 // A pointer type cannot be converted to object
75 if (expr_type.IsPointer)
78 if (TypeManager.IsValueType (expr_type))
79 return new BoxedCast (expr);
80 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
81 return new EmptyCast (expr, target_type);
82 } else if (target_type == TypeManager.value_type) {
83 if (TypeManager.IsValueType (expr_type))
84 return new BoxedCast (expr);
85 if (expr is NullLiteral)
86 return new NullCast (expr, target_type);
87 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
89 // Special case: enumeration to System.Enum.
90 // System.Enum is not a value type, it is a class, so we need
91 // a boxing conversion
93 if (expr_type.IsEnum || expr_type.IsGenericParameter)
94 return new BoxedCast (expr);
96 return new EmptyCast (expr, target_type);
99 // This code is kind of mirrored inside ImplicitStandardConversionExists
100 // with the small distinction that we only probe there
102 // Always ensure that the code here and there is in sync
104 // from the null type to any reference-type.
105 if (expr is NullLiteral){
106 if (target_type.IsPointer)
107 return NullPointer.Null;
109 if (!target_type.IsValueType)
110 return new NullCast (expr, target_type);
113 // from any class-type S to any interface-type T.
114 if (target_type.IsInterface) {
115 if (target_type != TypeManager.iconvertible_type &&
116 expr_type.IsValueType && (expr is Constant) &&
117 !(expr is IntLiteral || expr is BoolLiteral ||
118 expr is FloatLiteral || expr is DoubleLiteral ||
119 expr is LongLiteral || expr is CharLiteral ||
120 expr is StringLiteral || expr is DecimalLiteral ||
121 expr is UIntLiteral || expr is ULongLiteral)) {
125 if (TypeManager.ImplementsInterface (expr_type, target_type)){
126 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
127 return new BoxedCast (expr, target_type);
129 return new EmptyCast (expr, target_type);
133 // from any interface type S to interface-type T.
134 if (expr_type.IsInterface && target_type.IsInterface) {
135 if (TypeManager.ImplementsInterface (expr_type, target_type))
136 return new EmptyCast (expr, target_type);
141 // from an array-type S to an array-type of type T
142 if (expr_type.IsArray && target_type.IsArray) {
143 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
145 Type expr_element_type = TypeManager.GetElementType (expr_type);
147 if (MyEmptyExpr == null)
148 MyEmptyExpr = new EmptyExpression ();
150 MyEmptyExpr.SetType (expr_element_type);
151 Type target_element_type = TypeManager.GetElementType (target_type);
153 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
154 if (ImplicitStandardConversionExists (MyEmptyExpr,
155 target_element_type))
156 return new EmptyCast (expr, target_type);
161 // from an array-type to System.Array
162 if (expr_type.IsArray && target_type == TypeManager.array_type)
163 return new EmptyCast (expr, target_type);
165 // from any delegate type to System.Delegate
166 if ((expr_type == TypeManager.delegate_type ||
167 TypeManager.IsDelegateType (expr_type)) &&
168 target_type == TypeManager.delegate_type)
169 return new EmptyCast (expr, target_type);
171 // from any array-type or delegate type into System.ICloneable.
172 if (expr_type.IsArray ||
173 expr_type == TypeManager.delegate_type ||
174 TypeManager.IsDelegateType (expr_type))
175 if (target_type == TypeManager.icloneable_type)
176 return new EmptyCast (expr, target_type);
178 // from a generic type definition to a generic instance.
179 if (TypeManager.IsEqualGenericType (expr_type, target_type))
180 return new EmptyCast (expr, target_type);
190 // Tests whether an implicit reference conversion exists between expr_type
193 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
195 Type expr_type = expr.Type;
198 // This is the boxed case.
200 if (target_type == TypeManager.object_type) {
201 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
202 expr_type.IsInterface || expr_type == TypeManager.enum_type)
204 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
207 // Please remember that all code below actually comes
208 // from ImplicitReferenceConversion so make sure code remains in sync
210 // from any class-type S to any interface-type T.
211 if (target_type.IsInterface) {
212 if (TypeManager.ImplementsInterface (expr_type, target_type))
216 // from any interface type S to interface-type T.
217 if (expr_type.IsInterface && target_type.IsInterface)
218 if (TypeManager.ImplementsInterface (expr_type, target_type))
221 // from an array-type S to an array-type of type T
222 if (expr_type.IsArray && target_type.IsArray) {
223 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
225 Type expr_element_type = expr_type.GetElementType ();
227 if (MyEmptyExpr == null)
228 MyEmptyExpr = new EmptyExpression ();
230 MyEmptyExpr.SetType (expr_element_type);
231 Type target_element_type = TypeManager.GetElementType (target_type);
233 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
234 if (ImplicitStandardConversionExists (MyEmptyExpr,
235 target_element_type))
240 // from an array-type to System.Array
241 if (expr_type.IsArray && (target_type == TypeManager.array_type))
244 // from any delegate type to System.Delegate
245 if ((expr_type == TypeManager.delegate_type ||
246 TypeManager.IsDelegateType (expr_type)) &&
247 target_type == TypeManager.delegate_type)
248 if (target_type.IsAssignableFrom (expr_type))
251 // from any array-type or delegate type into System.ICloneable.
252 if (expr_type.IsArray ||
253 expr_type == TypeManager.delegate_type ||
254 TypeManager.IsDelegateType (expr_type))
255 if (target_type == TypeManager.icloneable_type)
258 // from the null type to any reference-type.
259 if (expr is NullLiteral && !target_type.IsValueType &&
260 !TypeManager.IsEnumType (target_type))
263 // from a generic type definition to a generic instance.
264 if (TypeManager.IsEqualGenericType (expr_type, target_type))
271 /// Implicit Numeric Conversions.
273 /// expr is the expression to convert, returns a new expression of type
274 /// target_type or null if an implicit conversion is not possible.
276 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
277 Type target_type, Location loc)
279 Type expr_type = expr.Type;
282 // Attempt to do the implicit constant expression conversions
284 if (expr is Constant){
285 if (expr is IntConstant){
288 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
292 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
294 // Try the implicit constant expression conversion
295 // from long to ulong, instead of a nice routine,
298 long v = ((LongConstant) expr).Value;
300 return new ULongConstant ((ulong) v);
304 Type real_target_type = target_type;
306 if (expr_type == TypeManager.sbyte_type){
308 // From sbyte to short, int, long, float, double.
310 if (real_target_type == TypeManager.int32_type)
311 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
312 if (real_target_type == TypeManager.int64_type)
313 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
314 if (real_target_type == TypeManager.double_type)
315 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
316 if (real_target_type == TypeManager.float_type)
317 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
318 if (real_target_type == TypeManager.short_type)
319 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
320 } else if (expr_type == TypeManager.byte_type){
322 // From byte to short, ushort, int, uint, long, ulong, float, double
324 if ((real_target_type == TypeManager.short_type) ||
325 (real_target_type == TypeManager.ushort_type) ||
326 (real_target_type == TypeManager.int32_type) ||
327 (real_target_type == TypeManager.uint32_type))
328 return new EmptyCast (expr, target_type);
330 if (real_target_type == TypeManager.uint64_type)
331 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
332 if (real_target_type == TypeManager.int64_type)
333 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
334 if (real_target_type == TypeManager.float_type)
335 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
336 if (real_target_type == TypeManager.double_type)
337 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
338 } else if (expr_type == TypeManager.short_type){
340 // From short to int, long, float, double
342 if (real_target_type == TypeManager.int32_type)
343 return new EmptyCast (expr, target_type);
344 if (real_target_type == TypeManager.int64_type)
345 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
346 if (real_target_type == TypeManager.double_type)
347 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
348 if (real_target_type == TypeManager.float_type)
349 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
350 } else if (expr_type == TypeManager.ushort_type){
352 // From ushort to int, uint, long, ulong, float, double
354 if (real_target_type == TypeManager.uint32_type)
355 return new EmptyCast (expr, target_type);
357 if (real_target_type == TypeManager.uint64_type)
358 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
359 if (real_target_type == TypeManager.int32_type)
360 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
361 if (real_target_type == TypeManager.int64_type)
362 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
363 if (real_target_type == TypeManager.double_type)
364 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
365 if (real_target_type == TypeManager.float_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
367 } else if (expr_type == TypeManager.int32_type){
369 // From int to long, float, double
371 if (real_target_type == TypeManager.int64_type)
372 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
373 if (real_target_type == TypeManager.double_type)
374 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
375 if (real_target_type == TypeManager.float_type)
376 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
377 } else if (expr_type == TypeManager.uint32_type){
379 // From uint to long, ulong, float, double
381 if (real_target_type == TypeManager.int64_type)
382 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
383 if (real_target_type == TypeManager.uint64_type)
384 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
385 if (real_target_type == TypeManager.double_type)
386 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
388 if (real_target_type == TypeManager.float_type)
389 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
391 } else if (expr_type == TypeManager.int64_type){
393 // From long/ulong to float, double
395 if (real_target_type == TypeManager.double_type)
396 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
397 if (real_target_type == TypeManager.float_type)
398 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
399 } else if (expr_type == TypeManager.uint64_type){
401 // From ulong to float, double
403 if (real_target_type == TypeManager.double_type)
404 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
406 if (real_target_type == TypeManager.float_type)
407 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
409 } else if (expr_type == TypeManager.char_type){
411 // From char to ushort, int, uint, long, ulong, float, double
413 if ((real_target_type == TypeManager.ushort_type) ||
414 (real_target_type == TypeManager.int32_type) ||
415 (real_target_type == TypeManager.uint32_type))
416 return new EmptyCast (expr, target_type);
417 if (real_target_type == TypeManager.uint64_type)
418 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
419 if (real_target_type == TypeManager.int64_type)
420 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
421 if (real_target_type == TypeManager.float_type)
422 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
423 if (real_target_type == TypeManager.double_type)
424 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
425 } else if (expr_type == TypeManager.float_type){
429 if (real_target_type == TypeManager.double_type)
430 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
438 /// Same as ImplicitStandardConversionExists except that it also looks at
439 /// implicit user defined conversions - needed for overload resolution
441 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
443 if ((expr is NullLiteral) && target_type.IsGenericParameter)
444 return TypeParameter_to_Null (target_type);
446 if (ImplicitStandardConversionExists (expr, target_type))
449 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
457 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
459 Expression dummy = ImplicitUserConversion (
460 ec, new EmptyExpression (source), target, Location.Null);
461 return dummy != null;
465 /// Determines if a standard implicit conversion exists from
466 /// expr_type to target_type
468 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
470 Type expr_type = expr.Type;
472 if (expr_type == TypeManager.void_type)
475 //Console.WriteLine ("Expr is {0}", expr);
476 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
477 if (expr_type.Equals (target_type))
480 // First numeric conversions
482 if (expr_type == TypeManager.sbyte_type){
484 // From sbyte to short, int, long, float, double.
486 if ((target_type == TypeManager.int32_type) ||
487 (target_type == TypeManager.int64_type) ||
488 (target_type == TypeManager.double_type) ||
489 (target_type == TypeManager.float_type) ||
490 (target_type == TypeManager.short_type) ||
491 (target_type == TypeManager.decimal_type))
494 } else if (expr_type == TypeManager.byte_type){
496 // From byte to short, ushort, int, uint, long, ulong, float, double
498 if ((target_type == TypeManager.short_type) ||
499 (target_type == TypeManager.ushort_type) ||
500 (target_type == TypeManager.int32_type) ||
501 (target_type == TypeManager.uint32_type) ||
502 (target_type == TypeManager.uint64_type) ||
503 (target_type == TypeManager.int64_type) ||
504 (target_type == TypeManager.float_type) ||
505 (target_type == TypeManager.double_type) ||
506 (target_type == TypeManager.decimal_type))
509 } else if (expr_type == TypeManager.short_type){
511 // From short to int, long, float, double
513 if ((target_type == TypeManager.int32_type) ||
514 (target_type == TypeManager.int64_type) ||
515 (target_type == TypeManager.double_type) ||
516 (target_type == TypeManager.float_type) ||
517 (target_type == TypeManager.decimal_type))
520 } else if (expr_type == TypeManager.ushort_type){
522 // From ushort to int, uint, long, ulong, float, double
524 if ((target_type == TypeManager.uint32_type) ||
525 (target_type == TypeManager.uint64_type) ||
526 (target_type == TypeManager.int32_type) ||
527 (target_type == TypeManager.int64_type) ||
528 (target_type == TypeManager.double_type) ||
529 (target_type == TypeManager.float_type) ||
530 (target_type == TypeManager.decimal_type))
533 } else if (expr_type == TypeManager.int32_type){
535 // From int to long, float, double
537 if ((target_type == TypeManager.int64_type) ||
538 (target_type == TypeManager.double_type) ||
539 (target_type == TypeManager.float_type) ||
540 (target_type == TypeManager.decimal_type))
543 } else if (expr_type == TypeManager.uint32_type){
545 // From uint to long, ulong, float, double
547 if ((target_type == TypeManager.int64_type) ||
548 (target_type == TypeManager.uint64_type) ||
549 (target_type == TypeManager.double_type) ||
550 (target_type == TypeManager.float_type) ||
551 (target_type == TypeManager.decimal_type))
554 } else if ((expr_type == TypeManager.uint64_type) ||
555 (expr_type == TypeManager.int64_type)) {
557 // From long/ulong to float, double
559 if ((target_type == TypeManager.double_type) ||
560 (target_type == TypeManager.float_type) ||
561 (target_type == TypeManager.decimal_type))
564 } else if (expr_type == TypeManager.char_type){
566 // From char to ushort, int, uint, long, ulong, float, double
568 if ((target_type == TypeManager.ushort_type) ||
569 (target_type == TypeManager.int32_type) ||
570 (target_type == TypeManager.uint32_type) ||
571 (target_type == TypeManager.uint64_type) ||
572 (target_type == TypeManager.int64_type) ||
573 (target_type == TypeManager.float_type) ||
574 (target_type == TypeManager.double_type) ||
575 (target_type == TypeManager.decimal_type))
578 } else if (expr_type == TypeManager.float_type){
582 if (target_type == TypeManager.double_type)
586 if (ImplicitReferenceConversionExists (expr, target_type))
590 // Implicit Constant Expression Conversions
592 if (expr is IntConstant){
593 int value = ((IntConstant) expr).Value;
595 if (target_type == TypeManager.sbyte_type){
596 if (value >= SByte.MinValue && value <= SByte.MaxValue)
598 } else if (target_type == TypeManager.byte_type){
599 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
601 } else if (target_type == TypeManager.short_type){
602 if (value >= Int16.MinValue && value <= Int16.MaxValue)
604 } else if (target_type == TypeManager.ushort_type){
605 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
607 } else if (target_type == TypeManager.uint32_type){
610 } else if (target_type == TypeManager.uint64_type){
612 // we can optimize this case: a positive int32
613 // always fits on a uint64. But we need an opcode
620 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
624 if (expr is LongConstant && target_type == TypeManager.uint64_type){
626 // Try the implicit constant expression conversion
627 // from long to ulong, instead of a nice routine,
630 long v = ((LongConstant) expr).Value;
635 if ((target_type == TypeManager.enum_type ||
636 target_type.IsSubclassOf (TypeManager.enum_type)) &&
638 IntLiteral i = (IntLiteral) expr;
644 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
651 // Used internally by FindMostEncompassedType, this is used
652 // to avoid creating lots of objects in the tight loop inside
653 // FindMostEncompassedType
655 static EmptyExpression priv_fmet_param;
658 /// Finds "most encompassed type" according to the spec (13.4.2)
659 /// amongst the methods in the MethodGroupExpr
661 static Type FindMostEncompassedType (ArrayList types)
665 if (priv_fmet_param == null)
666 priv_fmet_param = new EmptyExpression ();
668 foreach (Type t in types){
669 priv_fmet_param.SetType (t);
676 if (ImplicitStandardConversionExists (priv_fmet_param, best))
684 // Used internally by FindMostEncompassingType, this is used
685 // to avoid creating lots of objects in the tight loop inside
686 // FindMostEncompassingType
688 static EmptyExpression priv_fmee_ret;
691 /// Finds "most encompassing type" according to the spec (13.4.2)
692 /// amongst the types in the given set
694 static Type FindMostEncompassingType (ArrayList types)
698 if (priv_fmee_ret == null)
699 priv_fmee_ret = new EmptyExpression ();
701 foreach (Type t in types){
702 priv_fmee_ret.SetType (best);
709 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
717 // Used to avoid creating too many objects
719 static EmptyExpression priv_fms_expr;
722 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
723 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
724 /// for explicit and implicit conversion operators.
726 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
727 bool apply_explicit_conv_rules,
730 ArrayList src_types_set = new ArrayList ();
732 if (priv_fms_expr == null)
733 priv_fms_expr = new EmptyExpression ();
736 // If any operator converts from S then Sx = S
738 Type source_type = source.Type;
739 foreach (MethodBase mb in me.Methods){
740 ParameterData pd = Invocation.GetParameterData (mb);
741 Type param_type = pd.ParameterType (0);
743 if (param_type == source_type)
746 if (apply_explicit_conv_rules) {
749 // Find the set of applicable user-defined conversion operators, U. This set
751 // user-defined implicit or explicit conversion operators declared by
752 // the classes or structs in D that convert from a type encompassing
753 // or encompassed by S to a type encompassing or encompassed by T
755 priv_fms_expr.SetType (param_type);
756 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
757 src_types_set.Add (param_type);
759 if (ImplicitStandardConversionExists (source, param_type))
760 src_types_set.Add (param_type);
764 // Only if S is encompassed by param_type
766 if (ImplicitStandardConversionExists (source, param_type))
767 src_types_set.Add (param_type);
772 // Explicit Conv rules
774 if (apply_explicit_conv_rules) {
775 ArrayList candidate_set = new ArrayList ();
777 foreach (Type param_type in src_types_set){
778 if (ImplicitStandardConversionExists (source, param_type))
779 candidate_set.Add (param_type);
782 if (candidate_set.Count != 0)
783 return FindMostEncompassedType (candidate_set);
789 if (apply_explicit_conv_rules)
790 return FindMostEncompassingType (src_types_set);
792 return FindMostEncompassedType (src_types_set);
796 // Useful in avoiding proliferation of objects
798 static EmptyExpression priv_fmt_expr;
801 /// Finds the most specific target Tx according to section 13.4.4
803 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
804 bool apply_explicit_conv_rules,
807 ArrayList tgt_types_set = new ArrayList ();
809 if (priv_fmt_expr == null)
810 priv_fmt_expr = new EmptyExpression ();
813 // If any operator converts to T then Tx = T
815 foreach (MethodInfo mi in me.Methods){
816 Type ret_type = mi.ReturnType;
818 if (ret_type == target)
821 if (apply_explicit_conv_rules) {
824 // Find the set of applicable user-defined conversion operators, U.
826 // This set consists of the
827 // user-defined implicit or explicit conversion operators declared by
828 // the classes or structs in D that convert from a type encompassing
829 // or encompassed by S to a type encompassing or encompassed by T
831 priv_fms_expr.SetType (ret_type);
832 if (ImplicitStandardConversionExists (priv_fms_expr, target))
833 tgt_types_set.Add (ret_type);
835 priv_fms_expr.SetType (target);
836 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
837 tgt_types_set.Add (ret_type);
841 // Only if T is encompassed by param_type
843 priv_fms_expr.SetType (ret_type);
844 if (ImplicitStandardConversionExists (priv_fms_expr, target))
845 tgt_types_set.Add (ret_type);
850 // Explicit conv rules
852 if (apply_explicit_conv_rules) {
853 ArrayList candidate_set = new ArrayList ();
855 foreach (Type ret_type in tgt_types_set){
856 priv_fmt_expr.SetType (ret_type);
858 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
859 candidate_set.Add (ret_type);
862 if (candidate_set.Count != 0)
863 return FindMostEncompassingType (candidate_set);
867 // Okay, final case !
869 if (apply_explicit_conv_rules)
870 return FindMostEncompassedType (tgt_types_set);
872 return FindMostEncompassingType (tgt_types_set);
876 /// User-defined Implicit conversions
878 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
879 Type target, Location loc)
881 return UserDefinedConversion (ec, source, target, loc, false);
885 /// User-defined Explicit conversions
887 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
888 Type target, Location loc)
890 return UserDefinedConversion (ec, source, target, loc, true);
893 static DoubleHash explicit_conv = new DoubleHash (100);
894 static DoubleHash implicit_conv = new DoubleHash (100);
896 /// Computes the MethodGroup for the user-defined conversion
897 /// operators from source_type to target_type. `look_for_explicit'
898 /// controls whether we should also include the list of explicit
901 static MethodGroupExpr GetConversionOperators (EmitContext ec,
902 Type source_type, Type target_type,
903 Location loc, bool look_for_explicit)
905 Expression mg1 = null, mg2 = null;
906 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
909 op_name = "op_Implicit";
911 MethodGroupExpr union3;
913 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
914 return (MethodGroupExpr) r;
916 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
917 if (source_type.BaseType != null)
918 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
921 union3 = (MethodGroupExpr) mg2;
922 else if (mg2 == null)
923 union3 = (MethodGroupExpr) mg1;
925 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
927 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
930 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
932 union3 = (MethodGroupExpr) mg1;
935 if (target_type.BaseType != null)
936 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
940 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
942 union3 = (MethodGroupExpr) mg1;
945 MethodGroupExpr union4 = null;
947 if (look_for_explicit) {
948 op_name = "op_Explicit";
950 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
951 if (source_type.BaseType != null)
952 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
954 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
955 if (target_type.BaseType != null)
956 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
958 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
959 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
961 union4 = Invocation.MakeUnionSet (union5, union6, loc);
964 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
965 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
970 /// User-defined conversions
972 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
973 Type target, Location loc,
974 bool look_for_explicit)
976 MethodGroupExpr union;
977 Type source_type = source.Type;
978 MethodBase method = null;
980 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
984 Type most_specific_source, most_specific_target;
986 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
987 if (most_specific_source == null)
990 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
991 if (most_specific_target == null)
997 foreach (MethodBase mb in union.Methods){
998 ParameterData pd = Invocation.GetParameterData (mb);
999 MethodInfo mi = (MethodInfo) mb;
1001 if (pd.ParameterType (0) == most_specific_source &&
1002 mi.ReturnType == most_specific_target) {
1008 if (method == null || count > 1)
1013 // This will do the conversion to the best match that we
1014 // found. Now we need to perform an implict standard conversion
1015 // if the best match was not the type that we were requested
1018 if (look_for_explicit)
1019 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1021 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1027 e = new UserCast ((MethodInfo) method, source, loc);
1028 if (e.Type != target){
1029 if (!look_for_explicit)
1030 e = ImplicitConversionStandard (ec, e, target, loc);
1032 e = ExplicitConversionStandard (ec, e, target, loc);
1039 /// Converts implicitly the resolved expression `expr' into the
1040 /// `target_type'. It returns a new expression that can be used
1041 /// in a context that expects a `target_type'.
1043 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1044 Type target_type, Location loc)
1048 if (target_type == null)
1049 throw new Exception ("Target type is null");
1051 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1055 e = ImplicitUserConversion (ec, expr, target_type, loc);
1064 /// Attempts to apply the `Standard Implicit
1065 /// Conversion' rules to the expression `expr' into
1066 /// the `target_type'. It returns a new expression
1067 /// that can be used in a context that expects a
1070 /// This is different from `ImplicitConversion' in that the
1071 /// user defined implicit conversions are excluded.
1073 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1074 Type target_type, Location loc)
1076 Type expr_type = expr.Type;
1079 if ((expr is NullLiteral) && target_type.IsGenericParameter)
1080 return TypeParameter_to_Null (expr, target_type, loc);
1082 if (expr.eclass == ExprClass.MethodGroup){
1083 if (!TypeManager.IsDelegateType (target_type)){
1084 Report.Error (428, loc,
1086 "Cannot convert method group to `{0}', since it is not a delegate",
1087 TypeManager.CSharpName (target_type)));
1091 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1094 if (expr_type.Equals (target_type) && !(expr is NullLiteral))
1097 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1101 e = ImplicitReferenceConversion (expr, target_type);
1105 if ((target_type == TypeManager.enum_type ||
1106 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1107 expr is IntLiteral){
1108 IntLiteral i = (IntLiteral) expr;
1111 return new EnumConstant ((Constant) expr, target_type);
1115 if (expr_type.IsPointer){
1116 if (target_type == TypeManager.void_ptr_type)
1117 return new EmptyCast (expr, target_type);
1120 // yep, comparing pointer types cant be done with
1121 // t1 == t2, we have to compare their element types.
1123 if (target_type.IsPointer){
1124 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1129 if (target_type.IsPointer) {
1130 if (expr is NullLiteral)
1131 return new EmptyCast (expr, target_type);
1133 if (expr_type == TypeManager.void_ptr_type)
1134 return new EmptyCast (expr, target_type);
1142 /// Attemps to perform an implict constant conversion of the IntConstant
1143 /// into a different data type using casts (See Implicit Constant
1144 /// Expression Conversions)
1146 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1148 int value = ic.Value;
1150 if (target_type == TypeManager.sbyte_type){
1151 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1152 return new SByteConstant ((sbyte) value);
1153 } else if (target_type == TypeManager.byte_type){
1154 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1155 return new ByteConstant ((byte) value);
1156 } else if (target_type == TypeManager.short_type){
1157 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1158 return new ShortConstant ((short) value);
1159 } else if (target_type == TypeManager.ushort_type){
1160 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1161 return new UShortConstant ((ushort) value);
1162 } else if (target_type == TypeManager.uint32_type){
1164 return new UIntConstant ((uint) value);
1165 } else if (target_type == TypeManager.uint64_type){
1167 // we can optimize this case: a positive int32
1168 // always fits on a uint64. But we need an opcode
1172 return new ULongConstant ((ulong) value);
1173 } else if (target_type == TypeManager.double_type)
1174 return new DoubleConstant ((double) value);
1175 else if (target_type == TypeManager.float_type)
1176 return new FloatConstant ((float) value);
1178 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1179 Type underlying = TypeManager.EnumToUnderlying (target_type);
1180 Constant e = (Constant) ic;
1183 // Possibly, we need to create a different 0 literal before passing
1186 if (underlying == TypeManager.int64_type)
1187 e = new LongLiteral (0);
1188 else if (underlying == TypeManager.uint64_type)
1189 e = new ULongLiteral (0);
1191 return new EnumConstant (e, target_type);
1196 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1198 string msg = "Cannot convert implicitly from `"+
1199 TypeManager.CSharpName (source) + "' to `" +
1200 TypeManager.CSharpName (target) + "'";
1202 Report.Error (29, loc, msg);
1206 /// Attemptes to implicityly convert `target' into `type', using
1207 /// ImplicitConversion. If there is no implicit conversion, then
1208 /// an error is signaled
1210 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1211 Type target_type, Location loc)
1215 int errors = Report.Errors;
1216 e = ImplicitConversion (ec, source, target_type, loc);
1217 if (Report.Errors > errors)
1222 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1223 Report.Error (664, loc,
1224 "Double literal cannot be implicitly converted to " +
1225 "float type, use F suffix to create a float literal");
1228 if (source is Constant){
1229 Constant c = (Constant) source;
1231 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1235 Error_CannotImplicitConversion (loc, source.Type, target_type);
1241 /// Performs the explicit numeric conversions
1243 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1245 Type expr_type = expr.Type;
1248 // If we have an enumeration, extract the underlying type,
1249 // use this during the comparison, but wrap around the original
1252 Type real_target_type = target_type;
1254 if (TypeManager.IsEnumType (real_target_type))
1255 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1257 if (ImplicitStandardConversionExists (expr, real_target_type)){
1258 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1260 if (real_target_type != target_type)
1261 return new EmptyCast (ce, target_type);
1265 if (expr_type == TypeManager.sbyte_type){
1267 // From sbyte to byte, ushort, uint, ulong, char
1269 if (real_target_type == TypeManager.byte_type)
1270 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1271 if (real_target_type == TypeManager.ushort_type)
1272 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1273 if (real_target_type == TypeManager.uint32_type)
1274 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1275 if (real_target_type == TypeManager.uint64_type)
1276 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1277 if (real_target_type == TypeManager.char_type)
1278 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1279 } else if (expr_type == TypeManager.byte_type){
1281 // From byte to sbyte and char
1283 if (real_target_type == TypeManager.sbyte_type)
1284 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1285 if (real_target_type == TypeManager.char_type)
1286 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1287 } else if (expr_type == TypeManager.short_type){
1289 // From short to sbyte, byte, ushort, uint, ulong, char
1291 if (real_target_type == TypeManager.sbyte_type)
1292 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1293 if (real_target_type == TypeManager.byte_type)
1294 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1295 if (real_target_type == TypeManager.ushort_type)
1296 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1297 if (real_target_type == TypeManager.uint32_type)
1298 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1299 if (real_target_type == TypeManager.uint64_type)
1300 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1301 if (real_target_type == TypeManager.char_type)
1302 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1303 } else if (expr_type == TypeManager.ushort_type){
1305 // From ushort to sbyte, byte, short, char
1307 if (real_target_type == TypeManager.sbyte_type)
1308 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1309 if (real_target_type == TypeManager.byte_type)
1310 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1311 if (real_target_type == TypeManager.short_type)
1312 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1313 if (real_target_type == TypeManager.char_type)
1314 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1315 } else if (expr_type == TypeManager.int32_type){
1317 // From int to sbyte, byte, short, ushort, uint, ulong, char
1319 if (real_target_type == TypeManager.sbyte_type)
1320 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1321 if (real_target_type == TypeManager.byte_type)
1322 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1323 if (real_target_type == TypeManager.short_type)
1324 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1325 if (real_target_type == TypeManager.ushort_type)
1326 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1327 if (real_target_type == TypeManager.uint32_type)
1328 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1329 if (real_target_type == TypeManager.uint64_type)
1330 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1331 if (real_target_type == TypeManager.char_type)
1332 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1333 } else if (expr_type == TypeManager.uint32_type){
1335 // From uint to sbyte, byte, short, ushort, int, char
1337 if (real_target_type == TypeManager.sbyte_type)
1338 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1339 if (real_target_type == TypeManager.byte_type)
1340 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1341 if (real_target_type == TypeManager.short_type)
1342 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1343 if (real_target_type == TypeManager.ushort_type)
1344 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1345 if (real_target_type == TypeManager.int32_type)
1346 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1347 if (real_target_type == TypeManager.char_type)
1348 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1349 } else if (expr_type == TypeManager.int64_type){
1351 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1353 if (real_target_type == TypeManager.sbyte_type)
1354 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1355 if (real_target_type == TypeManager.byte_type)
1356 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1357 if (real_target_type == TypeManager.short_type)
1358 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1359 if (real_target_type == TypeManager.ushort_type)
1360 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1361 if (real_target_type == TypeManager.int32_type)
1362 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1363 if (real_target_type == TypeManager.uint32_type)
1364 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1365 if (real_target_type == TypeManager.uint64_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1367 if (real_target_type == TypeManager.char_type)
1368 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1369 } else if (expr_type == TypeManager.uint64_type){
1371 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1373 if (real_target_type == TypeManager.sbyte_type)
1374 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1375 if (real_target_type == TypeManager.byte_type)
1376 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1377 if (real_target_type == TypeManager.short_type)
1378 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1379 if (real_target_type == TypeManager.ushort_type)
1380 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1381 if (real_target_type == TypeManager.int32_type)
1382 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1383 if (real_target_type == TypeManager.uint32_type)
1384 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1385 if (real_target_type == TypeManager.int64_type)
1386 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1387 if (real_target_type == TypeManager.char_type)
1388 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1389 } else if (expr_type == TypeManager.char_type){
1391 // From char to sbyte, byte, short
1393 if (real_target_type == TypeManager.sbyte_type)
1394 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1395 if (real_target_type == TypeManager.byte_type)
1396 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1397 if (real_target_type == TypeManager.short_type)
1398 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1399 } else if (expr_type == TypeManager.float_type){
1401 // From float to sbyte, byte, short,
1402 // ushort, int, uint, long, ulong, char
1405 if (real_target_type == TypeManager.sbyte_type)
1406 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1407 if (real_target_type == TypeManager.byte_type)
1408 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1409 if (real_target_type == TypeManager.short_type)
1410 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1411 if (real_target_type == TypeManager.ushort_type)
1412 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1413 if (real_target_type == TypeManager.int32_type)
1414 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1415 if (real_target_type == TypeManager.uint32_type)
1416 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1417 if (real_target_type == TypeManager.int64_type)
1418 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1419 if (real_target_type == TypeManager.uint64_type)
1420 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1421 if (real_target_type == TypeManager.char_type)
1422 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1423 } else if (expr_type == TypeManager.double_type){
1425 // From double to byte, byte, short,
1426 // ushort, int, uint, long, ulong,
1427 // char, float or decimal
1429 if (real_target_type == TypeManager.sbyte_type)
1430 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1431 if (real_target_type == TypeManager.byte_type)
1432 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1433 if (real_target_type == TypeManager.short_type)
1434 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1435 if (real_target_type == TypeManager.ushort_type)
1436 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1437 if (real_target_type == TypeManager.int32_type)
1438 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1439 if (real_target_type == TypeManager.uint32_type)
1440 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1441 if (real_target_type == TypeManager.int64_type)
1442 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1443 if (real_target_type == TypeManager.uint64_type)
1444 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1445 if (real_target_type == TypeManager.char_type)
1446 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1447 if (real_target_type == TypeManager.float_type)
1448 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1451 // decimal is taken care of by the op_Explicit methods.
1457 /// Returns whether an explicit reference conversion can be performed
1458 /// from source_type to target_type
1460 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1462 bool target_is_type_param = target_type.IsGenericParameter;
1463 bool target_is_value_type = target_type.IsValueType;
1465 if (source_type == target_type)
1469 // From object to a generic parameter
1471 if (source_type == TypeManager.object_type && target_is_type_param)
1475 // From object to any reference type
1477 if (source_type == TypeManager.object_type && !target_is_value_type)
1481 // From any class S to any class-type T, provided S is a base class of T
1483 if (TypeManager.IsSubclassOf (target_type, source_type))
1487 // From any interface type S to any interface T provided S is not derived from T
1489 if (source_type.IsInterface && target_type.IsInterface){
1490 if (!TypeManager.IsSubclassOf (target_type, source_type))
1495 // From any class type S to any interface T, provided S is not sealed
1496 // and provided S does not implement T.
1498 if (target_type.IsInterface && !source_type.IsSealed &&
1499 !TypeManager.ImplementsInterface (source_type, target_type))
1503 // From any interface-type S to to any class type T, provided T is not
1504 // sealed, or provided T implements S.
1506 if (source_type.IsInterface &&
1507 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1511 // From an array type S with an element type Se to an array type T with an
1512 // element type Te provided all the following are true:
1513 // * S and T differe only in element type, in other words, S and T
1514 // have the same number of dimensions.
1515 // * Both Se and Te are reference types
1516 // * An explicit referenc conversions exist from Se to Te
1518 if (source_type.IsArray && target_type.IsArray) {
1519 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1521 Type source_element_type = TypeManager.GetElementType (source_type);
1522 Type target_element_type = TypeManager.GetElementType (target_type);
1524 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1525 if (ExplicitReferenceConversionExists (source_element_type,
1526 target_element_type))
1532 // From System.Array to any array-type
1533 if (source_type == TypeManager.array_type &&
1534 target_type.IsArray){
1539 // From System delegate to any delegate-type
1541 if (source_type == TypeManager.delegate_type &&
1542 TypeManager.IsDelegateType (target_type))
1546 // From ICloneable to Array or Delegate types
1548 if (source_type == TypeManager.icloneable_type &&
1549 (target_type == TypeManager.array_type ||
1550 target_type == TypeManager.delegate_type))
1557 /// Implements Explicit Reference conversions
1559 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1561 Type source_type = source.Type;
1562 bool target_is_type_param = target_type.IsGenericParameter;
1563 bool target_is_value_type = target_type.IsValueType;
1566 // From object to a generic parameter
1568 if (source_type == TypeManager.object_type && target_is_type_param)
1569 return new UnboxCast (source, target_type);
1572 // From object to any reference type
1574 if (source_type == TypeManager.object_type && !target_is_value_type)
1575 return new ClassCast (source, target_type);
1578 // Unboxing conversion.
1580 if (((source_type == TypeManager.enum_type &&
1581 !(source is EmptyCast)) ||
1582 source_type == TypeManager.value_type) && target_is_value_type)
1583 return new UnboxCast (source, target_type);
1586 // From any class S to any class-type T, provided S is a base class of T
1588 if (TypeManager.IsSubclassOf (target_type, source_type))
1589 return new ClassCast (source, target_type);
1592 // From any interface type S to any interface T provided S is not derived from T
1594 if (source_type.IsInterface && target_type.IsInterface){
1595 if (TypeManager.ImplementsInterface (source_type, target_type))
1598 return new ClassCast (source, target_type);
1602 // From any class type S to any interface T, provides S is not sealed
1603 // and provided S does not implement T.
1605 if (target_type.IsInterface && !source_type.IsSealed) {
1606 if (TypeManager.ImplementsInterface (source_type, target_type))
1609 return new ClassCast (source, target_type);
1614 // From any interface-type S to to any class type T, provided T is not
1615 // sealed, or provided T implements S.
1617 if (source_type.IsInterface) {
1618 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1619 if (target_type.IsClass)
1620 return new ClassCast (source, target_type);
1622 return new UnboxCast (source, target_type);
1628 // From an array type S with an element type Se to an array type T with an
1629 // element type Te provided all the following are true:
1630 // * S and T differe only in element type, in other words, S and T
1631 // have the same number of dimensions.
1632 // * Both Se and Te are reference types
1633 // * An explicit referenc conversions exist from Se to Te
1635 if (source_type.IsArray && target_type.IsArray) {
1636 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1638 Type source_element_type = TypeManager.GetElementType (source_type);
1639 Type target_element_type = TypeManager.GetElementType (target_type);
1641 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1642 if (ExplicitReferenceConversionExists (source_element_type,
1643 target_element_type))
1644 return new ClassCast (source, target_type);
1649 // From System.Array to any array-type
1650 if (source_type == TypeManager.array_type &&
1651 target_type.IsArray) {
1652 return new ClassCast (source, target_type);
1656 // From System delegate to any delegate-type
1658 if (source_type == TypeManager.delegate_type &&
1659 TypeManager.IsDelegateType (target_type))
1660 return new ClassCast (source, target_type);
1663 // From ICloneable to Array or Delegate types
1665 if (source_type == TypeManager.icloneable_type &&
1666 (target_type == TypeManager.array_type ||
1667 target_type == TypeManager.delegate_type))
1668 return new ClassCast (source, target_type);
1674 /// Performs an explicit conversion of the expression `expr' whose
1675 /// type is expr.Type to `target_type'.
1677 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1678 Type target_type, Location loc)
1680 Type expr_type = expr.Type;
1681 Type original_expr_type = expr_type;
1683 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1684 if (target_type == TypeManager.enum_type ||
1685 target_type == TypeManager.object_type) {
1686 if (expr is EnumConstant)
1687 expr = ((EnumConstant) expr).Child;
1688 // We really need all these casts here .... :-(
1689 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1690 return new EmptyCast (expr, target_type);
1691 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1692 target_type.IsSubclassOf (TypeManager.enum_type))
1693 return new UnboxCast (expr, target_type);
1696 // Notice that we have kept the expr_type unmodified, which is only
1698 if (expr is EnumConstant)
1699 expr = ((EnumConstant) expr).Child;
1701 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1702 expr_type = expr.Type;
1705 int errors = Report.Errors;
1706 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1707 if (Report.Errors > errors)
1713 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1718 // Unboxing conversion.
1720 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1721 if (expr is NullLiteral){
1723 // Skip the ExplicitReferenceConversion because we can not convert
1724 // from Null to a ValueType, and ExplicitReference wont check against
1725 // null literal explicitly
1729 return new UnboxCast (expr, target_type);
1733 ne = ExplicitReferenceConversion (expr, target_type);
1739 if (target_type.IsPointer){
1740 if (expr_type.IsPointer)
1741 return new EmptyCast (expr, target_type);
1743 if (expr_type == TypeManager.sbyte_type ||
1744 expr_type == TypeManager.byte_type ||
1745 expr_type == TypeManager.short_type ||
1746 expr_type == TypeManager.ushort_type ||
1747 expr_type == TypeManager.int32_type ||
1748 expr_type == TypeManager.uint32_type ||
1749 expr_type == TypeManager.uint64_type ||
1750 expr_type == TypeManager.int64_type)
1751 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1753 if (expr_type.IsPointer){
1754 Expression e = null;
1756 if (target_type == TypeManager.sbyte_type)
1757 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1758 else if (target_type == TypeManager.byte_type)
1759 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1760 else if (target_type == TypeManager.short_type)
1761 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1762 else if (target_type == TypeManager.ushort_type)
1763 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1764 else if (target_type == TypeManager.int32_type)
1765 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1766 else if (target_type == TypeManager.uint32_type)
1767 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1768 else if (target_type == TypeManager.uint64_type)
1769 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1770 else if (target_type == TypeManager.int64_type){
1771 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1777 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1782 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1786 // We should always be able to go from an uint32
1787 // implicitly or explicitly to the other integral
1790 throw new Exception ("Internal compiler error");
1795 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1799 if (expr is NullLiteral){
1800 Report.Error (37, loc, "Cannot convert null to value type `" +
1801 TypeManager.CSharpName (target_type) + "'");
1805 Error_CannotConvertType (loc, original_expr_type, target_type);
1810 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1812 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1813 Type target_type, Location l)
1815 int errors = Report.Errors;
1816 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1817 if (Report.Errors > errors)
1823 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1827 ne = ExplicitReferenceConversion (expr, target_type);
1831 Error_CannotConvertType (l, expr.Type, target_type);