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)
127 return new BoxedCast (expr, target_type);
128 else if (expr_type.IsClass)
129 return new EmptyCast (expr, target_type);
130 else if (TypeManager.IsValueType (expr_type))
131 return new BoxedCast (expr, target_type);
133 return new EmptyCast (expr, target_type);
137 // from any interface type S to interface-type T.
138 if (expr_type.IsInterface && target_type.IsInterface) {
139 if (TypeManager.ImplementsInterface (expr_type, target_type))
140 return new EmptyCast (expr, target_type);
145 // from an array-type S to an array-type of type T
146 if (expr_type.IsArray && target_type.IsArray) {
147 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
149 Type expr_element_type = TypeManager.GetElementType (expr_type);
151 if (MyEmptyExpr == null)
152 MyEmptyExpr = new EmptyExpression ();
154 MyEmptyExpr.SetType (expr_element_type);
155 Type target_element_type = TypeManager.GetElementType (target_type);
157 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
158 if (ImplicitStandardConversionExists (MyEmptyExpr,
159 target_element_type))
160 return new EmptyCast (expr, target_type);
165 // from an array-type to System.Array
166 if (expr_type.IsArray && target_type == TypeManager.array_type)
167 return new EmptyCast (expr, target_type);
169 // from any delegate type to System.Delegate
170 if ((expr_type == TypeManager.delegate_type ||
171 TypeManager.IsDelegateType (expr_type)) &&
172 target_type == TypeManager.delegate_type)
173 return new EmptyCast (expr, target_type);
175 // from any array-type or delegate type into System.ICloneable.
176 if (expr_type.IsArray ||
177 expr_type == TypeManager.delegate_type ||
178 TypeManager.IsDelegateType (expr_type))
179 if (target_type == TypeManager.icloneable_type)
180 return new EmptyCast (expr, target_type);
182 // from a generic type definition to a generic instance.
183 if (TypeManager.IsEqualGenericType (expr_type, target_type))
184 return new EmptyCast (expr, target_type);
194 // Tests whether an implicit reference conversion exists between expr_type
197 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
199 Type expr_type = expr.Type;
202 // This is the boxed case.
204 if (target_type == TypeManager.object_type) {
205 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
206 expr_type.IsInterface || expr_type == TypeManager.enum_type)
208 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
211 // Please remember that all code below actually comes
212 // from ImplicitReferenceConversion so make sure code remains in sync
214 // from any class-type S to any interface-type T.
215 if (target_type.IsInterface) {
216 if (TypeManager.ImplementsInterface (expr_type, target_type))
220 // from any interface type S to interface-type T.
221 if (expr_type.IsInterface && target_type.IsInterface)
222 if (TypeManager.ImplementsInterface (expr_type, target_type))
225 // from an array-type S to an array-type of type T
226 if (expr_type.IsArray && target_type.IsArray) {
227 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
229 Type expr_element_type = expr_type.GetElementType ();
231 if (MyEmptyExpr == null)
232 MyEmptyExpr = new EmptyExpression ();
234 MyEmptyExpr.SetType (expr_element_type);
235 Type target_element_type = TypeManager.GetElementType (target_type);
237 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
238 if (ImplicitStandardConversionExists (MyEmptyExpr,
239 target_element_type))
244 // from an array-type to System.Array
245 if (expr_type.IsArray && (target_type == TypeManager.array_type))
248 // from any delegate type to System.Delegate
249 if ((expr_type == TypeManager.delegate_type ||
250 TypeManager.IsDelegateType (expr_type)) &&
251 target_type == TypeManager.delegate_type)
252 if (target_type.IsAssignableFrom (expr_type))
255 // from any array-type or delegate type into System.ICloneable.
256 if (expr_type.IsArray ||
257 expr_type == TypeManager.delegate_type ||
258 TypeManager.IsDelegateType (expr_type))
259 if (target_type == TypeManager.icloneable_type)
262 // from the null type to any reference-type.
263 if (expr is NullLiteral && !target_type.IsValueType &&
264 !TypeManager.IsEnumType (target_type))
267 // from a generic type definition to a generic instance.
268 if (TypeManager.IsEqualGenericType (expr_type, target_type))
275 /// Implicit Numeric Conversions.
277 /// expr is the expression to convert, returns a new expression of type
278 /// target_type or null if an implicit conversion is not possible.
280 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
281 Type target_type, Location loc)
283 Type expr_type = expr.Type;
286 // Attempt to do the implicit constant expression conversions
288 if (expr is Constant){
289 if (expr is IntConstant){
292 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
296 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
298 // Try the implicit constant expression conversion
299 // from long to ulong, instead of a nice routine,
302 long v = ((LongConstant) expr).Value;
304 return new ULongConstant ((ulong) v);
308 Type real_target_type = target_type;
310 if (expr_type == TypeManager.sbyte_type){
312 // From sbyte to short, int, long, float, double.
314 if (real_target_type == TypeManager.int32_type)
315 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
316 if (real_target_type == TypeManager.int64_type)
317 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
318 if (real_target_type == TypeManager.double_type)
319 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
320 if (real_target_type == TypeManager.float_type)
321 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
322 if (real_target_type == TypeManager.short_type)
323 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
324 } else if (expr_type == TypeManager.byte_type){
326 // From byte to short, ushort, int, uint, long, ulong, float, double
328 if ((real_target_type == TypeManager.short_type) ||
329 (real_target_type == TypeManager.ushort_type) ||
330 (real_target_type == TypeManager.int32_type) ||
331 (real_target_type == TypeManager.uint32_type))
332 return new EmptyCast (expr, target_type);
334 if (real_target_type == TypeManager.uint64_type)
335 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
336 if (real_target_type == TypeManager.int64_type)
337 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
338 if (real_target_type == TypeManager.float_type)
339 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
340 if (real_target_type == TypeManager.double_type)
341 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
342 } else if (expr_type == TypeManager.short_type){
344 // From short to int, long, float, double
346 if (real_target_type == TypeManager.int32_type)
347 return new EmptyCast (expr, target_type);
348 if (real_target_type == TypeManager.int64_type)
349 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
350 if (real_target_type == TypeManager.double_type)
351 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
352 if (real_target_type == TypeManager.float_type)
353 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
354 } else if (expr_type == TypeManager.ushort_type){
356 // From ushort to int, uint, long, ulong, float, double
358 if (real_target_type == TypeManager.uint32_type)
359 return new EmptyCast (expr, target_type);
361 if (real_target_type == TypeManager.uint64_type)
362 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
363 if (real_target_type == TypeManager.int32_type)
364 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
365 if (real_target_type == TypeManager.int64_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
367 if (real_target_type == TypeManager.double_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
369 if (real_target_type == TypeManager.float_type)
370 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
371 } else if (expr_type == TypeManager.int32_type){
373 // From int to long, float, double
375 if (real_target_type == TypeManager.int64_type)
376 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
377 if (real_target_type == TypeManager.double_type)
378 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
379 if (real_target_type == TypeManager.float_type)
380 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
381 } else if (expr_type == TypeManager.uint32_type){
383 // From uint to long, ulong, float, double
385 if (real_target_type == TypeManager.int64_type)
386 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
387 if (real_target_type == TypeManager.uint64_type)
388 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
389 if (real_target_type == TypeManager.double_type)
390 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
392 if (real_target_type == TypeManager.float_type)
393 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
395 } else if (expr_type == TypeManager.int64_type){
397 // From long/ulong to float, double
399 if (real_target_type == TypeManager.double_type)
400 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
401 if (real_target_type == TypeManager.float_type)
402 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
403 } else if (expr_type == TypeManager.uint64_type){
405 // From ulong to float, double
407 if (real_target_type == TypeManager.double_type)
408 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
410 if (real_target_type == TypeManager.float_type)
411 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
413 } else if (expr_type == TypeManager.char_type){
415 // From char to ushort, int, uint, long, ulong, float, double
417 if ((real_target_type == TypeManager.ushort_type) ||
418 (real_target_type == TypeManager.int32_type) ||
419 (real_target_type == TypeManager.uint32_type))
420 return new EmptyCast (expr, target_type);
421 if (real_target_type == TypeManager.uint64_type)
422 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
423 if (real_target_type == TypeManager.int64_type)
424 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
425 if (real_target_type == TypeManager.float_type)
426 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
427 if (real_target_type == TypeManager.double_type)
428 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
429 } else if (expr_type == TypeManager.float_type){
433 if (real_target_type == TypeManager.double_type)
434 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
442 /// Same as ImplicitStandardConversionExists except that it also looks at
443 /// implicit user defined conversions - needed for overload resolution
445 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
447 if ((expr is NullLiteral) && target_type.IsGenericParameter)
448 return TypeParameter_to_Null (target_type);
450 if (ImplicitStandardConversionExists (expr, target_type))
453 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
461 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
463 Expression dummy = ImplicitUserConversion (
464 ec, new EmptyExpression (source), target, Location.Null);
465 return dummy != null;
469 /// Determines if a standard implicit conversion exists from
470 /// expr_type to target_type
472 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
474 Type expr_type = expr.Type;
476 if (expr_type == TypeManager.void_type)
479 //Console.WriteLine ("Expr is {0}", expr);
480 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
481 if (expr_type.Equals (target_type))
484 // First numeric conversions
486 if (expr_type == TypeManager.sbyte_type){
488 // From sbyte to short, int, long, float, double.
490 if ((target_type == TypeManager.int32_type) ||
491 (target_type == TypeManager.int64_type) ||
492 (target_type == TypeManager.double_type) ||
493 (target_type == TypeManager.float_type) ||
494 (target_type == TypeManager.short_type) ||
495 (target_type == TypeManager.decimal_type))
498 } else if (expr_type == TypeManager.byte_type){
500 // From byte to short, ushort, int, uint, long, ulong, float, double
502 if ((target_type == TypeManager.short_type) ||
503 (target_type == TypeManager.ushort_type) ||
504 (target_type == TypeManager.int32_type) ||
505 (target_type == TypeManager.uint32_type) ||
506 (target_type == TypeManager.uint64_type) ||
507 (target_type == TypeManager.int64_type) ||
508 (target_type == TypeManager.float_type) ||
509 (target_type == TypeManager.double_type) ||
510 (target_type == TypeManager.decimal_type))
513 } else if (expr_type == TypeManager.short_type){
515 // From short to int, long, float, double
517 if ((target_type == TypeManager.int32_type) ||
518 (target_type == TypeManager.int64_type) ||
519 (target_type == TypeManager.double_type) ||
520 (target_type == TypeManager.float_type) ||
521 (target_type == TypeManager.decimal_type))
524 } else if (expr_type == TypeManager.ushort_type){
526 // From ushort to int, uint, long, ulong, float, double
528 if ((target_type == TypeManager.uint32_type) ||
529 (target_type == TypeManager.uint64_type) ||
530 (target_type == TypeManager.int32_type) ||
531 (target_type == TypeManager.int64_type) ||
532 (target_type == TypeManager.double_type) ||
533 (target_type == TypeManager.float_type) ||
534 (target_type == TypeManager.decimal_type))
537 } else if (expr_type == TypeManager.int32_type){
539 // From int to long, float, double
541 if ((target_type == TypeManager.int64_type) ||
542 (target_type == TypeManager.double_type) ||
543 (target_type == TypeManager.float_type) ||
544 (target_type == TypeManager.decimal_type))
547 } else if (expr_type == TypeManager.uint32_type){
549 // From uint to long, ulong, float, double
551 if ((target_type == TypeManager.int64_type) ||
552 (target_type == TypeManager.uint64_type) ||
553 (target_type == TypeManager.double_type) ||
554 (target_type == TypeManager.float_type) ||
555 (target_type == TypeManager.decimal_type))
558 } else if ((expr_type == TypeManager.uint64_type) ||
559 (expr_type == TypeManager.int64_type)) {
561 // From long/ulong to float, double
563 if ((target_type == TypeManager.double_type) ||
564 (target_type == TypeManager.float_type) ||
565 (target_type == TypeManager.decimal_type))
568 } else if (expr_type == TypeManager.char_type){
570 // From char to ushort, int, uint, long, ulong, float, double
572 if ((target_type == TypeManager.ushort_type) ||
573 (target_type == TypeManager.int32_type) ||
574 (target_type == TypeManager.uint32_type) ||
575 (target_type == TypeManager.uint64_type) ||
576 (target_type == TypeManager.int64_type) ||
577 (target_type == TypeManager.float_type) ||
578 (target_type == TypeManager.double_type) ||
579 (target_type == TypeManager.decimal_type))
582 } else if (expr_type == TypeManager.float_type){
586 if (target_type == TypeManager.double_type)
590 if (ImplicitReferenceConversionExists (expr, target_type))
594 // Implicit Constant Expression Conversions
596 if (expr is IntConstant){
597 int value = ((IntConstant) expr).Value;
599 if (target_type == TypeManager.sbyte_type){
600 if (value >= SByte.MinValue && value <= SByte.MaxValue)
602 } else if (target_type == TypeManager.byte_type){
603 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
605 } else if (target_type == TypeManager.short_type){
606 if (value >= Int16.MinValue && value <= Int16.MaxValue)
608 } else if (target_type == TypeManager.ushort_type){
609 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
611 } else if (target_type == TypeManager.uint32_type){
614 } else if (target_type == TypeManager.uint64_type){
616 // we can optimize this case: a positive int32
617 // always fits on a uint64. But we need an opcode
624 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
628 if (expr is LongConstant && target_type == TypeManager.uint64_type){
630 // Try the implicit constant expression conversion
631 // from long to ulong, instead of a nice routine,
634 long v = ((LongConstant) expr).Value;
639 if ((target_type == TypeManager.enum_type ||
640 target_type.IsSubclassOf (TypeManager.enum_type)) &&
642 IntLiteral i = (IntLiteral) expr;
648 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
655 // Used internally by FindMostEncompassedType, this is used
656 // to avoid creating lots of objects in the tight loop inside
657 // FindMostEncompassedType
659 static EmptyExpression priv_fmet_param;
662 /// Finds "most encompassed type" according to the spec (13.4.2)
663 /// amongst the methods in the MethodGroupExpr
665 static Type FindMostEncompassedType (ArrayList types)
669 if (priv_fmet_param == null)
670 priv_fmet_param = new EmptyExpression ();
672 foreach (Type t in types){
673 priv_fmet_param.SetType (t);
680 if (ImplicitStandardConversionExists (priv_fmet_param, best))
688 // Used internally by FindMostEncompassingType, this is used
689 // to avoid creating lots of objects in the tight loop inside
690 // FindMostEncompassingType
692 static EmptyExpression priv_fmee_ret;
695 /// Finds "most encompassing type" according to the spec (13.4.2)
696 /// amongst the types in the given set
698 static Type FindMostEncompassingType (ArrayList types)
702 if (priv_fmee_ret == null)
703 priv_fmee_ret = new EmptyExpression ();
705 foreach (Type t in types){
706 priv_fmee_ret.SetType (best);
713 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
721 // Used to avoid creating too many objects
723 static EmptyExpression priv_fms_expr;
726 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
727 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
728 /// for explicit and implicit conversion operators.
730 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
731 bool apply_explicit_conv_rules,
734 ArrayList src_types_set = new ArrayList ();
736 if (priv_fms_expr == null)
737 priv_fms_expr = new EmptyExpression ();
740 // If any operator converts from S then Sx = S
742 Type source_type = source.Type;
743 foreach (MethodBase mb in me.Methods){
744 ParameterData pd = Invocation.GetParameterData (mb);
745 Type param_type = pd.ParameterType (0);
747 if (param_type == source_type)
750 if (apply_explicit_conv_rules) {
753 // Find the set of applicable user-defined conversion operators, U. This set
755 // user-defined implicit or explicit conversion operators declared by
756 // the classes or structs in D that convert from a type encompassing
757 // or encompassed by S to a type encompassing or encompassed by T
759 priv_fms_expr.SetType (param_type);
760 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
761 src_types_set.Add (param_type);
763 if (ImplicitStandardConversionExists (source, param_type))
764 src_types_set.Add (param_type);
768 // Only if S is encompassed by param_type
770 if (ImplicitStandardConversionExists (source, param_type))
771 src_types_set.Add (param_type);
776 // Explicit Conv rules
778 if (apply_explicit_conv_rules) {
779 ArrayList candidate_set = new ArrayList ();
781 foreach (Type param_type in src_types_set){
782 if (ImplicitStandardConversionExists (source, param_type))
783 candidate_set.Add (param_type);
786 if (candidate_set.Count != 0)
787 return FindMostEncompassedType (candidate_set);
793 if (apply_explicit_conv_rules)
794 return FindMostEncompassingType (src_types_set);
796 return FindMostEncompassedType (src_types_set);
800 // Useful in avoiding proliferation of objects
802 static EmptyExpression priv_fmt_expr;
805 /// Finds the most specific target Tx according to section 13.4.4
807 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
808 bool apply_explicit_conv_rules,
811 ArrayList tgt_types_set = new ArrayList ();
813 if (priv_fmt_expr == null)
814 priv_fmt_expr = new EmptyExpression ();
817 // If any operator converts to T then Tx = T
819 foreach (MethodInfo mi in me.Methods){
820 Type ret_type = mi.ReturnType;
822 if (ret_type == target)
825 if (apply_explicit_conv_rules) {
828 // Find the set of applicable user-defined conversion operators, U.
830 // This set consists of the
831 // user-defined implicit or explicit conversion operators declared by
832 // the classes or structs in D that convert from a type encompassing
833 // or encompassed by S to a type encompassing or encompassed by T
835 priv_fms_expr.SetType (ret_type);
836 if (ImplicitStandardConversionExists (priv_fms_expr, target))
837 tgt_types_set.Add (ret_type);
839 priv_fms_expr.SetType (target);
840 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
841 tgt_types_set.Add (ret_type);
845 // Only if T is encompassed by param_type
847 priv_fms_expr.SetType (ret_type);
848 if (ImplicitStandardConversionExists (priv_fms_expr, target))
849 tgt_types_set.Add (ret_type);
854 // Explicit conv rules
856 if (apply_explicit_conv_rules) {
857 ArrayList candidate_set = new ArrayList ();
859 foreach (Type ret_type in tgt_types_set){
860 priv_fmt_expr.SetType (ret_type);
862 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
863 candidate_set.Add (ret_type);
866 if (candidate_set.Count != 0)
867 return FindMostEncompassingType (candidate_set);
871 // Okay, final case !
873 if (apply_explicit_conv_rules)
874 return FindMostEncompassedType (tgt_types_set);
876 return FindMostEncompassingType (tgt_types_set);
880 /// User-defined Implicit conversions
882 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
883 Type target, Location loc)
885 return UserDefinedConversion (ec, source, target, loc, false);
889 /// User-defined Explicit conversions
891 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
892 Type target, Location loc)
894 return UserDefinedConversion (ec, source, target, loc, true);
897 static DoubleHash explicit_conv = new DoubleHash (100);
898 static DoubleHash implicit_conv = new DoubleHash (100);
900 /// Computes the MethodGroup for the user-defined conversion
901 /// operators from source_type to target_type. `look_for_explicit'
902 /// controls whether we should also include the list of explicit
905 static MethodGroupExpr GetConversionOperators (EmitContext ec,
906 Type source_type, Type target_type,
907 Location loc, bool look_for_explicit)
909 Expression mg1 = null, mg2 = null;
910 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
913 op_name = "op_Implicit";
915 MethodGroupExpr union3;
917 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
918 return (MethodGroupExpr) r;
920 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
921 if (source_type.BaseType != null)
922 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
925 union3 = (MethodGroupExpr) mg2;
926 else if (mg2 == null)
927 union3 = (MethodGroupExpr) mg1;
929 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
931 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
934 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
936 union3 = (MethodGroupExpr) mg1;
939 if (target_type.BaseType != null)
940 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
944 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
946 union3 = (MethodGroupExpr) mg1;
949 MethodGroupExpr union4 = null;
951 if (look_for_explicit) {
952 op_name = "op_Explicit";
954 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
955 if (source_type.BaseType != null)
956 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
958 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
959 if (target_type.BaseType != null)
960 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
962 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
963 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
965 union4 = Invocation.MakeUnionSet (union5, union6, loc);
968 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
969 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
974 /// User-defined conversions
976 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
977 Type target, Location loc,
978 bool look_for_explicit)
980 MethodGroupExpr union;
981 Type source_type = source.Type;
982 MethodBase method = null;
984 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
988 Type most_specific_source, most_specific_target;
990 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
991 if (most_specific_source == null)
994 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
995 if (most_specific_target == null)
1001 foreach (MethodBase mb in union.Methods){
1002 ParameterData pd = Invocation.GetParameterData (mb);
1003 MethodInfo mi = (MethodInfo) mb;
1005 if (pd.ParameterType (0) == most_specific_source &&
1006 mi.ReturnType == most_specific_target) {
1012 if (method == null || count > 1)
1017 // This will do the conversion to the best match that we
1018 // found. Now we need to perform an implict standard conversion
1019 // if the best match was not the type that we were requested
1022 if (look_for_explicit)
1023 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1025 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1031 e = new UserCast ((MethodInfo) method, source, loc);
1032 if (e.Type != target){
1033 if (!look_for_explicit)
1034 e = ImplicitConversionStandard (ec, e, target, loc);
1036 e = ExplicitConversionStandard (ec, e, target, loc);
1043 /// Converts implicitly the resolved expression `expr' into the
1044 /// `target_type'. It returns a new expression that can be used
1045 /// in a context that expects a `target_type'.
1047 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1048 Type target_type, Location loc)
1052 if (target_type == null)
1053 throw new Exception ("Target type is null");
1055 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1059 e = ImplicitUserConversion (ec, expr, target_type, loc);
1068 /// Attempts to apply the `Standard Implicit
1069 /// Conversion' rules to the expression `expr' into
1070 /// the `target_type'. It returns a new expression
1071 /// that can be used in a context that expects a
1074 /// This is different from `ImplicitConversion' in that the
1075 /// user defined implicit conversions are excluded.
1077 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1078 Type target_type, Location loc)
1080 Type expr_type = expr.Type;
1083 if ((expr is NullLiteral) && target_type.IsGenericParameter)
1084 return TypeParameter_to_Null (expr, target_type, loc);
1086 if (expr.eclass == ExprClass.MethodGroup){
1087 if (!TypeManager.IsDelegateType (target_type)){
1088 Report.Error (428, loc,
1090 "Cannot convert method group to `{0}', since it is not a delegate",
1091 TypeManager.CSharpName (target_type)));
1095 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1098 if (expr_type.Equals (target_type) && !(expr is NullLiteral))
1101 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1105 e = ImplicitReferenceConversion (expr, target_type);
1109 if ((target_type == TypeManager.enum_type ||
1110 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1111 expr is IntLiteral){
1112 IntLiteral i = (IntLiteral) expr;
1115 return new EnumConstant ((Constant) expr, target_type);
1119 if (expr_type.IsPointer){
1120 if (target_type == TypeManager.void_ptr_type)
1121 return new EmptyCast (expr, target_type);
1124 // yep, comparing pointer types cant be done with
1125 // t1 == t2, we have to compare their element types.
1127 if (target_type.IsPointer){
1128 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1133 if (target_type.IsPointer) {
1134 if (expr is NullLiteral)
1135 return new EmptyCast (expr, target_type);
1137 if (expr_type == TypeManager.void_ptr_type)
1138 return new EmptyCast (expr, target_type);
1146 /// Attemps to perform an implict constant conversion of the IntConstant
1147 /// into a different data type using casts (See Implicit Constant
1148 /// Expression Conversions)
1150 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1152 int value = ic.Value;
1154 if (target_type == TypeManager.sbyte_type){
1155 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1156 return new SByteConstant ((sbyte) value);
1157 } else if (target_type == TypeManager.byte_type){
1158 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1159 return new ByteConstant ((byte) value);
1160 } else if (target_type == TypeManager.short_type){
1161 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1162 return new ShortConstant ((short) value);
1163 } else if (target_type == TypeManager.ushort_type){
1164 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1165 return new UShortConstant ((ushort) value);
1166 } else if (target_type == TypeManager.uint32_type){
1168 return new UIntConstant ((uint) value);
1169 } else if (target_type == TypeManager.uint64_type){
1171 // we can optimize this case: a positive int32
1172 // always fits on a uint64. But we need an opcode
1176 return new ULongConstant ((ulong) value);
1177 } else if (target_type == TypeManager.double_type)
1178 return new DoubleConstant ((double) value);
1179 else if (target_type == TypeManager.float_type)
1180 return new FloatConstant ((float) value);
1182 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1183 Type underlying = TypeManager.EnumToUnderlying (target_type);
1184 Constant e = (Constant) ic;
1187 // Possibly, we need to create a different 0 literal before passing
1190 if (underlying == TypeManager.int64_type)
1191 e = new LongLiteral (0);
1192 else if (underlying == TypeManager.uint64_type)
1193 e = new ULongLiteral (0);
1195 return new EnumConstant (e, target_type);
1200 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1202 string msg = "Cannot convert implicitly from `"+
1203 TypeManager.CSharpName (source) + "' to `" +
1204 TypeManager.CSharpName (target) + "'";
1206 Report.Error (29, loc, msg);
1210 /// Attemptes to implicityly convert `target' into `type', using
1211 /// ImplicitConversion. If there is no implicit conversion, then
1212 /// an error is signaled
1214 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1215 Type target_type, Location loc)
1219 int errors = Report.Errors;
1220 e = ImplicitConversion (ec, source, target_type, loc);
1221 if (Report.Errors > errors)
1226 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1227 Report.Error (664, loc,
1228 "Double literal cannot be implicitly converted to " +
1229 "float type, use F suffix to create a float literal");
1232 if (source is Constant){
1233 Constant c = (Constant) source;
1235 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1239 Error_CannotImplicitConversion (loc, source.Type, target_type);
1245 /// Performs the explicit numeric conversions
1247 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1249 Type expr_type = expr.Type;
1252 // If we have an enumeration, extract the underlying type,
1253 // use this during the comparison, but wrap around the original
1256 Type real_target_type = target_type;
1258 if (TypeManager.IsEnumType (real_target_type))
1259 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1261 if (ImplicitStandardConversionExists (expr, real_target_type)){
1262 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1264 if (real_target_type != target_type)
1265 return new EmptyCast (ce, target_type);
1269 if (expr_type == TypeManager.sbyte_type){
1271 // From sbyte to byte, ushort, uint, ulong, char
1273 if (real_target_type == TypeManager.byte_type)
1274 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1275 if (real_target_type == TypeManager.ushort_type)
1276 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1277 if (real_target_type == TypeManager.uint32_type)
1278 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1279 if (real_target_type == TypeManager.uint64_type)
1280 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1281 if (real_target_type == TypeManager.char_type)
1282 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1283 } else if (expr_type == TypeManager.byte_type){
1285 // From byte to sbyte and char
1287 if (real_target_type == TypeManager.sbyte_type)
1288 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1289 if (real_target_type == TypeManager.char_type)
1290 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1291 } else if (expr_type == TypeManager.short_type){
1293 // From short to sbyte, byte, ushort, uint, ulong, char
1295 if (real_target_type == TypeManager.sbyte_type)
1296 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1297 if (real_target_type == TypeManager.byte_type)
1298 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1299 if (real_target_type == TypeManager.ushort_type)
1300 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1301 if (real_target_type == TypeManager.uint32_type)
1302 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1303 if (real_target_type == TypeManager.uint64_type)
1304 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1305 if (real_target_type == TypeManager.char_type)
1306 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1307 } else if (expr_type == TypeManager.ushort_type){
1309 // From ushort to sbyte, byte, short, char
1311 if (real_target_type == TypeManager.sbyte_type)
1312 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1313 if (real_target_type == TypeManager.byte_type)
1314 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1315 if (real_target_type == TypeManager.short_type)
1316 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1317 if (real_target_type == TypeManager.char_type)
1318 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1319 } else if (expr_type == TypeManager.int32_type){
1321 // From int to sbyte, byte, short, ushort, uint, ulong, char
1323 if (real_target_type == TypeManager.sbyte_type)
1324 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1325 if (real_target_type == TypeManager.byte_type)
1326 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1327 if (real_target_type == TypeManager.short_type)
1328 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1329 if (real_target_type == TypeManager.ushort_type)
1330 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1331 if (real_target_type == TypeManager.uint32_type)
1332 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1333 if (real_target_type == TypeManager.uint64_type)
1334 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1335 if (real_target_type == TypeManager.char_type)
1336 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1337 } else if (expr_type == TypeManager.uint32_type){
1339 // From uint to sbyte, byte, short, ushort, int, char
1341 if (real_target_type == TypeManager.sbyte_type)
1342 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1343 if (real_target_type == TypeManager.byte_type)
1344 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1345 if (real_target_type == TypeManager.short_type)
1346 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1347 if (real_target_type == TypeManager.ushort_type)
1348 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1349 if (real_target_type == TypeManager.int32_type)
1350 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1351 if (real_target_type == TypeManager.char_type)
1352 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1353 } else if (expr_type == TypeManager.int64_type){
1355 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1357 if (real_target_type == TypeManager.sbyte_type)
1358 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1359 if (real_target_type == TypeManager.byte_type)
1360 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1361 if (real_target_type == TypeManager.short_type)
1362 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1363 if (real_target_type == TypeManager.ushort_type)
1364 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1365 if (real_target_type == TypeManager.int32_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1367 if (real_target_type == TypeManager.uint32_type)
1368 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1369 if (real_target_type == TypeManager.uint64_type)
1370 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1371 if (real_target_type == TypeManager.char_type)
1372 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1373 } else if (expr_type == TypeManager.uint64_type){
1375 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1377 if (real_target_type == TypeManager.sbyte_type)
1378 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1379 if (real_target_type == TypeManager.byte_type)
1380 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1381 if (real_target_type == TypeManager.short_type)
1382 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1383 if (real_target_type == TypeManager.ushort_type)
1384 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1385 if (real_target_type == TypeManager.int32_type)
1386 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1387 if (real_target_type == TypeManager.uint32_type)
1388 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1389 if (real_target_type == TypeManager.int64_type)
1390 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1391 if (real_target_type == TypeManager.char_type)
1392 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1393 } else if (expr_type == TypeManager.char_type){
1395 // From char to sbyte, byte, short
1397 if (real_target_type == TypeManager.sbyte_type)
1398 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1399 if (real_target_type == TypeManager.byte_type)
1400 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1401 if (real_target_type == TypeManager.short_type)
1402 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1403 } else if (expr_type == TypeManager.float_type){
1405 // From float to sbyte, byte, short,
1406 // ushort, int, uint, long, ulong, char
1409 if (real_target_type == TypeManager.sbyte_type)
1410 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1411 if (real_target_type == TypeManager.byte_type)
1412 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1413 if (real_target_type == TypeManager.short_type)
1414 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1415 if (real_target_type == TypeManager.ushort_type)
1416 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1417 if (real_target_type == TypeManager.int32_type)
1418 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1419 if (real_target_type == TypeManager.uint32_type)
1420 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1421 if (real_target_type == TypeManager.int64_type)
1422 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1423 if (real_target_type == TypeManager.uint64_type)
1424 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1425 if (real_target_type == TypeManager.char_type)
1426 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1427 } else if (expr_type == TypeManager.double_type){
1429 // From double to byte, byte, short,
1430 // ushort, int, uint, long, ulong,
1431 // char, float or decimal
1433 if (real_target_type == TypeManager.sbyte_type)
1434 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1435 if (real_target_type == TypeManager.byte_type)
1436 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1437 if (real_target_type == TypeManager.short_type)
1438 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1439 if (real_target_type == TypeManager.ushort_type)
1440 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1441 if (real_target_type == TypeManager.int32_type)
1442 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1443 if (real_target_type == TypeManager.uint32_type)
1444 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1445 if (real_target_type == TypeManager.int64_type)
1446 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1447 if (real_target_type == TypeManager.uint64_type)
1448 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1449 if (real_target_type == TypeManager.char_type)
1450 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1451 if (real_target_type == TypeManager.float_type)
1452 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1455 // decimal is taken care of by the op_Explicit methods.
1461 /// Returns whether an explicit reference conversion can be performed
1462 /// from source_type to target_type
1464 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1466 bool target_is_type_param = target_type.IsGenericParameter;
1467 bool target_is_value_type = target_type.IsValueType;
1469 if (source_type == target_type)
1473 // From object to a generic parameter
1475 if (source_type == TypeManager.object_type && target_is_type_param)
1479 // From object to any reference type
1481 if (source_type == TypeManager.object_type && !target_is_value_type)
1485 // From any class S to any class-type T, provided S is a base class of T
1487 if (TypeManager.IsSubclassOf (target_type, source_type))
1491 // From any interface type S to any interface T provided S is not derived from T
1493 if (source_type.IsInterface && target_type.IsInterface){
1494 if (!TypeManager.IsSubclassOf (target_type, source_type))
1499 // From any class type S to any interface T, provided S is not sealed
1500 // and provided S does not implement T.
1502 if (target_type.IsInterface && !source_type.IsSealed &&
1503 !TypeManager.ImplementsInterface (source_type, target_type))
1507 // From any interface-type S to to any class type T, provided T is not
1508 // sealed, or provided T implements S.
1510 if (source_type.IsInterface &&
1511 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1515 // From an array type S with an element type Se to an array type T with an
1516 // element type Te provided all the following are true:
1517 // * S and T differe only in element type, in other words, S and T
1518 // have the same number of dimensions.
1519 // * Both Se and Te are reference types
1520 // * An explicit referenc conversions exist from Se to Te
1522 if (source_type.IsArray && target_type.IsArray) {
1523 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1525 Type source_element_type = TypeManager.GetElementType (source_type);
1526 Type target_element_type = TypeManager.GetElementType (target_type);
1528 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1529 if (ExplicitReferenceConversionExists (source_element_type,
1530 target_element_type))
1536 // From System.Array to any array-type
1537 if (source_type == TypeManager.array_type &&
1538 target_type.IsArray){
1543 // From System delegate to any delegate-type
1545 if (source_type == TypeManager.delegate_type &&
1546 TypeManager.IsDelegateType (target_type))
1550 // From ICloneable to Array or Delegate types
1552 if (source_type == TypeManager.icloneable_type &&
1553 (target_type == TypeManager.array_type ||
1554 target_type == TypeManager.delegate_type))
1561 /// Implements Explicit Reference conversions
1563 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1565 Type source_type = source.Type;
1566 bool target_is_type_param = target_type.IsGenericParameter;
1567 bool target_is_value_type = target_type.IsValueType;
1570 // From object to a generic parameter
1572 if (source_type == TypeManager.object_type && target_is_type_param)
1573 return new UnboxCast (source, target_type);
1576 // From object to any reference type
1578 if (source_type == TypeManager.object_type && !target_is_value_type)
1579 return new ClassCast (source, target_type);
1582 // Unboxing conversion.
1584 if (((source_type == TypeManager.enum_type &&
1585 !(source is EmptyCast)) ||
1586 source_type == TypeManager.value_type) && target_is_value_type)
1587 return new UnboxCast (source, target_type);
1590 // From any class S to any class-type T, provided S is a base class of T
1592 if (TypeManager.IsSubclassOf (target_type, source_type))
1593 return new ClassCast (source, target_type);
1596 // From any interface type S to any interface T provided S is not derived from T
1598 if (source_type.IsInterface && target_type.IsInterface){
1599 if (TypeManager.ImplementsInterface (source_type, target_type))
1602 return new ClassCast (source, target_type);
1606 // From any class type S to any interface T, provides S is not sealed
1607 // and provided S does not implement T.
1609 if (target_type.IsInterface && !source_type.IsSealed) {
1610 if (TypeManager.ImplementsInterface (source_type, target_type))
1613 return new ClassCast (source, target_type);
1618 // From any interface-type S to to any class type T, provided T is not
1619 // sealed, or provided T implements S.
1621 if (source_type.IsInterface) {
1622 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1623 if (target_type.IsClass)
1624 return new ClassCast (source, target_type);
1626 return new UnboxCast (source, target_type);
1632 // From an array type S with an element type Se to an array type T with an
1633 // element type Te provided all the following are true:
1634 // * S and T differe only in element type, in other words, S and T
1635 // have the same number of dimensions.
1636 // * Both Se and Te are reference types
1637 // * An explicit referenc conversions exist from Se to Te
1639 if (source_type.IsArray && target_type.IsArray) {
1640 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1642 Type source_element_type = TypeManager.GetElementType (source_type);
1643 Type target_element_type = TypeManager.GetElementType (target_type);
1645 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1646 if (ExplicitReferenceConversionExists (source_element_type,
1647 target_element_type))
1648 return new ClassCast (source, target_type);
1653 // From System.Array to any array-type
1654 if (source_type == TypeManager.array_type &&
1655 target_type.IsArray) {
1656 return new ClassCast (source, target_type);
1660 // From System delegate to any delegate-type
1662 if (source_type == TypeManager.delegate_type &&
1663 TypeManager.IsDelegateType (target_type))
1664 return new ClassCast (source, target_type);
1667 // From ICloneable to Array or Delegate types
1669 if (source_type == TypeManager.icloneable_type &&
1670 (target_type == TypeManager.array_type ||
1671 target_type == TypeManager.delegate_type))
1672 return new ClassCast (source, target_type);
1678 /// Performs an explicit conversion of the expression `expr' whose
1679 /// type is expr.Type to `target_type'.
1681 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1682 Type target_type, Location loc)
1684 Type expr_type = expr.Type;
1685 Type original_expr_type = expr_type;
1687 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1688 if (target_type == TypeManager.enum_type ||
1689 target_type == TypeManager.object_type) {
1690 if (expr is EnumConstant)
1691 expr = ((EnumConstant) expr).Child;
1692 // We really need all these casts here .... :-(
1693 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1694 return new EmptyCast (expr, target_type);
1695 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1696 target_type.IsSubclassOf (TypeManager.enum_type))
1697 return new UnboxCast (expr, target_type);
1700 // Notice that we have kept the expr_type unmodified, which is only
1702 if (expr is EnumConstant)
1703 expr = ((EnumConstant) expr).Child;
1705 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1706 expr_type = expr.Type;
1709 int errors = Report.Errors;
1710 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1711 if (Report.Errors > errors)
1717 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1722 // Unboxing conversion.
1724 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1725 if (expr is NullLiteral){
1727 // Skip the ExplicitReferenceConversion because we can not convert
1728 // from Null to a ValueType, and ExplicitReference wont check against
1729 // null literal explicitly
1733 return new UnboxCast (expr, target_type);
1737 ne = ExplicitReferenceConversion (expr, target_type);
1743 if (target_type.IsPointer){
1744 if (expr_type.IsPointer)
1745 return new EmptyCast (expr, target_type);
1747 if (expr_type == TypeManager.sbyte_type ||
1748 expr_type == TypeManager.byte_type ||
1749 expr_type == TypeManager.short_type ||
1750 expr_type == TypeManager.ushort_type ||
1751 expr_type == TypeManager.int32_type ||
1752 expr_type == TypeManager.uint32_type ||
1753 expr_type == TypeManager.uint64_type ||
1754 expr_type == TypeManager.int64_type)
1755 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1757 if (expr_type.IsPointer){
1758 Expression e = null;
1760 if (target_type == TypeManager.sbyte_type)
1761 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1762 else if (target_type == TypeManager.byte_type)
1763 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1764 else if (target_type == TypeManager.short_type)
1765 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1766 else if (target_type == TypeManager.ushort_type)
1767 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1768 else if (target_type == TypeManager.int32_type)
1769 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1770 else if (target_type == TypeManager.uint32_type)
1771 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1772 else if (target_type == TypeManager.uint64_type)
1773 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1774 else if (target_type == TypeManager.int64_type){
1775 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1781 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1786 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1790 // We should always be able to go from an uint32
1791 // implicitly or explicitly to the other integral
1794 throw new Exception ("Internal compiler error");
1799 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1803 if (expr is NullLiteral){
1804 Report.Error (37, loc, "Cannot convert null to value type `" +
1805 TypeManager.CSharpName (target_type) + "'");
1809 Error_CannotConvertType (loc, original_expr_type, target_type);
1814 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1816 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1817 Type target_type, Location l)
1819 int errors = Report.Errors;
1820 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1821 if (Report.Errors > errors)
1827 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1831 ne = ExplicitReferenceConversion (expr, target_type);
1835 Error_CannotConvertType (l, expr.Type, target_type);