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 {
23 // This is used to prettify the code: a null argument is allowed
24 // for ImplicitStandardConversion as long as it is known that
25 // no anonymous method will play a role.
27 // FIXME: renamed from `const' to `static' to allow bootstraping from older
28 // versions of the compiler that could not cope with this construct.
30 public static EmitContext ConstantEC = null;
32 static EmptyExpression MyEmptyExpr;
33 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
35 Type expr_type = expr.Type;
37 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
38 // if we are a method group, emit a warning
43 if (expr_type == TypeManager.void_type)
47 // notice that it is possible to write "ValueType v = 1", the ValueType here
48 // is an abstract class, and not really a value type, so we apply the same rules.
50 if (target_type == TypeManager.object_type) {
52 // A pointer type cannot be converted to object
54 if (expr_type.IsPointer)
57 if (expr_type.IsValueType)
58 return new BoxedCast (expr, target_type);
59 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
60 if (expr_type == TypeManager.anonymous_method_type)
62 return new EmptyCast (expr, target_type);
66 } else if (target_type == TypeManager.value_type) {
67 if (expr_type.IsValueType)
68 return new BoxedCast (expr, target_type);
69 if (expr_type == TypeManager.null_type)
70 return new NullCast (expr, target_type);
73 } else if (expr_type.IsSubclassOf (target_type)) {
75 // Special case: enumeration to System.Enum.
76 // System.Enum is not a value type, it is a class, so we need
77 // a boxing conversion
80 return new BoxedCast (expr, target_type);
82 return new EmptyCast (expr, target_type);
85 // This code is kind of mirrored inside ImplicitStandardConversionExists
86 // with the small distinction that we only probe there
88 // Always ensure that the code here and there is in sync
90 // from the null type to any reference-type.
91 if (expr_type == TypeManager.null_type){
92 if (target_type.IsPointer)
93 return new EmptyCast (NullPointer.Null, target_type);
95 if (!target_type.IsValueType)
96 return new NullCast (expr, target_type);
99 // from any class-type S to any interface-type T.
100 if (target_type.IsInterface) {
101 if (target_type != TypeManager.iconvertible_type &&
102 expr_type.IsValueType && (expr is Constant) &&
103 !(expr is IntLiteral || expr is BoolLiteral ||
104 expr is FloatLiteral || expr is DoubleLiteral ||
105 expr is LongLiteral || expr is CharLiteral ||
106 expr is StringLiteral || expr is DecimalLiteral ||
107 expr is UIntLiteral || expr is ULongLiteral)) {
111 if (TypeManager.ImplementsInterface (expr_type, target_type)){
112 if (expr_type.IsClass)
113 return new EmptyCast (expr, target_type);
114 else if (expr_type.IsValueType)
115 return new BoxedCast (expr, target_type);
117 return new EmptyCast (expr, target_type);
121 // from any interface type S to interface-type T.
122 if (expr_type.IsInterface && target_type.IsInterface) {
123 if (TypeManager.ImplementsInterface (expr_type, target_type))
124 return new EmptyCast (expr, target_type);
129 // from an array-type S to an array-type of type T
130 if (expr_type.IsArray && target_type.IsArray) {
131 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
133 Type expr_element_type = TypeManager.GetElementType (expr_type);
135 if (MyEmptyExpr == null)
136 MyEmptyExpr = new EmptyExpression ();
138 MyEmptyExpr.SetType (expr_element_type);
139 Type target_element_type = TypeManager.GetElementType (target_type);
141 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
142 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
143 target_element_type))
144 return new EmptyCast (expr, target_type);
148 // from an array-type to System.Array
149 if (expr_type.IsArray && target_type == TypeManager.array_type)
150 return new EmptyCast (expr, target_type);
152 // from any delegate type to System.Delegate
153 if ((expr_type == TypeManager.delegate_type ||
154 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
155 target_type == TypeManager.delegate_type)
156 return new EmptyCast (expr, target_type);
158 // from any array-type or delegate type into System.ICloneable.
159 if (expr_type.IsArray ||
160 expr_type == TypeManager.delegate_type ||
161 expr_type.IsSubclassOf (TypeManager.delegate_type))
162 if (target_type == TypeManager.icloneable_type)
163 return new EmptyCast (expr, target_type);
169 // Tests whether an implicit reference conversion exists between expr_type
172 static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
174 if (target_type.IsValueType)
177 Type expr_type = expr.Type;
180 // This is the boxed case.
182 if (target_type == TypeManager.object_type) {
183 if (expr_type.IsClass || expr_type.IsValueType ||
184 expr_type.IsInterface || expr_type == TypeManager.enum_type)
185 if (target_type != TypeManager.anonymous_method_type)
189 } else if (expr_type.IsSubclassOf (target_type))
192 // Please remember that all code below actually comes
193 // from ImplicitReferenceConversion so make sure code remains in sync
195 // from any class-type S to any interface-type T.
196 if (target_type.IsInterface) {
197 if (target_type != TypeManager.iconvertible_type &&
198 expr_type.IsValueType && (expr is Constant) &&
199 !(expr is IntLiteral || expr is BoolLiteral ||
200 expr is FloatLiteral || expr is DoubleLiteral ||
201 expr is LongLiteral || expr is CharLiteral ||
202 expr is StringLiteral || expr is DecimalLiteral ||
203 expr is UIntLiteral || expr is ULongLiteral)) {
207 if (TypeManager.ImplementsInterface (expr_type, target_type))
211 // from any interface type S to interface-type T.
212 if (expr_type.IsInterface && target_type.IsInterface)
213 if (TypeManager.ImplementsInterface (expr_type, target_type))
216 // from an array-type S to an array-type of type T
217 if (expr_type.IsArray && target_type.IsArray) {
218 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
220 Type expr_element_type = expr_type.GetElementType ();
222 if (MyEmptyExpr == null)
223 MyEmptyExpr = new EmptyExpression ();
225 MyEmptyExpr.SetType (expr_element_type);
226 Type target_element_type = TypeManager.GetElementType (target_type);
228 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
229 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
230 target_element_type))
235 // from an array-type to System.Array
236 if (expr_type.IsArray && (target_type == TypeManager.array_type))
239 // from any delegate type to System.Delegate
240 if ((expr_type == TypeManager.delegate_type ||
241 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
242 target_type == TypeManager.delegate_type)
243 if (target_type.IsAssignableFrom (expr_type))
246 // from any array-type or delegate type into System.ICloneable.
247 if (expr_type.IsArray ||
248 expr_type == TypeManager.delegate_type ||
249 expr_type.IsSubclassOf (TypeManager.delegate_type))
250 if (target_type == TypeManager.icloneable_type)
253 // from the null type to any reference-type.
254 if (expr_type == TypeManager.null_type){
255 if (target_type.IsPointer)
258 if (!target_type.IsValueType)
265 /// Implicit Numeric Conversions.
267 /// expr is the expression to convert, returns a new expression of type
268 /// target_type or null if an implicit conversion is not possible.
270 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
273 Type expr_type = expr.Type;
276 // Attempt to do the implicit constant expression conversions
278 if (expr is Constant){
279 if (expr is IntConstant){
282 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
286 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
288 // Try the implicit constant expression conversion
289 // from long to ulong, instead of a nice routine,
292 long v = ((LongConstant) expr).Value;
294 return new ULongConstant ((ulong) v, expr.Location);
298 Type real_target_type = target_type;
300 if (expr_type == TypeManager.sbyte_type){
302 // From sbyte to short, int, long, float, double, decimal
304 if (real_target_type == TypeManager.int32_type)
305 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
306 if (real_target_type == TypeManager.int64_type)
307 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
308 if (real_target_type == TypeManager.double_type)
309 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
310 if (real_target_type == TypeManager.float_type)
311 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
312 if (real_target_type == TypeManager.short_type)
313 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
314 if (real_target_type == TypeManager.decimal_type)
315 return new CastToDecimal (expr);
316 } else if (expr_type == TypeManager.byte_type){
318 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
320 if ((real_target_type == TypeManager.short_type) ||
321 (real_target_type == TypeManager.ushort_type) ||
322 (real_target_type == TypeManager.int32_type) ||
323 (real_target_type == TypeManager.uint32_type))
324 return new EmptyCast (expr, target_type);
326 if (real_target_type == TypeManager.uint64_type)
327 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
328 if (real_target_type == TypeManager.int64_type)
329 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
330 if (real_target_type == TypeManager.float_type)
331 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
332 if (real_target_type == TypeManager.double_type)
333 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
334 if (real_target_type == TypeManager.decimal_type)
335 return new CastToDecimal (expr);
337 } else if (expr_type == TypeManager.short_type){
339 // From short to int, long, float, double, decimal
341 if (real_target_type == TypeManager.int32_type)
342 return new EmptyCast (expr, target_type);
343 if (real_target_type == TypeManager.int64_type)
344 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
345 if (real_target_type == TypeManager.double_type)
346 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
347 if (real_target_type == TypeManager.float_type)
348 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
349 if (real_target_type == TypeManager.decimal_type)
350 return new CastToDecimal (expr);
352 } else if (expr_type == TypeManager.ushort_type){
354 // From ushort to int, uint, long, ulong, float, double, decimal
356 if (real_target_type == TypeManager.uint32_type)
357 return new EmptyCast (expr, target_type);
359 if (real_target_type == TypeManager.uint64_type)
360 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
361 if (real_target_type == TypeManager.int32_type)
362 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
363 if (real_target_type == TypeManager.int64_type)
364 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
365 if (real_target_type == TypeManager.double_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
367 if (real_target_type == TypeManager.float_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
369 if (real_target_type == TypeManager.decimal_type)
370 return new CastToDecimal (expr);
371 } else if (expr_type == TypeManager.int32_type){
373 // From int to long, float, double, decimal
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 if (real_target_type == TypeManager.decimal_type)
382 return new CastToDecimal (expr);
383 } else if (expr_type == TypeManager.uint32_type){
385 // From uint to long, ulong, float, double, decimal
387 if (real_target_type == TypeManager.int64_type)
388 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
389 if (real_target_type == TypeManager.uint64_type)
390 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
391 if (real_target_type == TypeManager.double_type)
392 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
394 if (real_target_type == TypeManager.float_type)
395 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
397 if (real_target_type == TypeManager.decimal_type)
398 return new CastToDecimal (expr);
399 } else if (expr_type == TypeManager.int64_type){
401 // From long/ulong to float, double
403 if (real_target_type == TypeManager.double_type)
404 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
405 if (real_target_type == TypeManager.float_type)
406 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
407 if (real_target_type == TypeManager.decimal_type)
408 return new CastToDecimal (expr);
409 } else if (expr_type == TypeManager.uint64_type){
411 // From ulong to float, double
413 if (real_target_type == TypeManager.double_type)
414 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
416 if (real_target_type == TypeManager.float_type)
417 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
419 if (real_target_type == TypeManager.decimal_type)
420 return new CastToDecimal (expr);
421 } else if (expr_type == TypeManager.char_type){
423 // From char to ushort, int, uint, long, ulong, float, double, decimal
425 if ((real_target_type == TypeManager.ushort_type) ||
426 (real_target_type == TypeManager.int32_type) ||
427 (real_target_type == TypeManager.uint32_type))
428 return new EmptyCast (expr, target_type);
429 if (real_target_type == TypeManager.uint64_type)
430 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
431 if (real_target_type == TypeManager.int64_type)
432 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
433 if (real_target_type == TypeManager.float_type)
434 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
435 if (real_target_type == TypeManager.double_type)
436 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
437 if (real_target_type == TypeManager.decimal_type)
438 return new CastToDecimal (expr);
439 } else if (expr_type == TypeManager.float_type){
443 if (real_target_type == TypeManager.double_type)
444 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
451 /// Same as ImplicitStandardConversionExists except that it also looks at
452 /// implicit user defined conversions - needed for overload resolution
454 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
456 if (ImplicitStandardConversionExists (ec, expr, target_type))
459 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
467 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
469 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
473 /// Determines if a standard implicit conversion exists from
474 /// expr_type to target_type
476 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
478 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
480 Type expr_type = expr.Type;
482 if (expr_type == TypeManager.void_type)
485 if (expr_type == target_type)
489 // First numeric conversions
491 if (expr_type == TypeManager.sbyte_type){
493 // From sbyte to short, int, long, float, double, decimal
495 if ((target_type == TypeManager.int32_type) ||
496 (target_type == TypeManager.int64_type) ||
497 (target_type == TypeManager.double_type) ||
498 (target_type == TypeManager.float_type) ||
499 (target_type == TypeManager.short_type) ||
500 (target_type == TypeManager.decimal_type))
503 } else if (expr_type == TypeManager.byte_type){
505 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
507 if ((target_type == TypeManager.short_type) ||
508 (target_type == TypeManager.ushort_type) ||
509 (target_type == TypeManager.int32_type) ||
510 (target_type == TypeManager.uint32_type) ||
511 (target_type == TypeManager.uint64_type) ||
512 (target_type == TypeManager.int64_type) ||
513 (target_type == TypeManager.float_type) ||
514 (target_type == TypeManager.double_type) ||
515 (target_type == TypeManager.decimal_type))
518 } else if (expr_type == TypeManager.short_type){
520 // From short to int, long, double, float, decimal
522 if ((target_type == TypeManager.int32_type) ||
523 (target_type == TypeManager.int64_type) ||
524 (target_type == TypeManager.double_type) ||
525 (target_type == TypeManager.float_type) ||
526 (target_type == TypeManager.decimal_type))
529 } else if (expr_type == TypeManager.ushort_type){
531 // From ushort to int, uint, long, ulong, double, float, decimal
533 if ((target_type == TypeManager.uint32_type) ||
534 (target_type == TypeManager.uint64_type) ||
535 (target_type == TypeManager.int32_type) ||
536 (target_type == TypeManager.int64_type) ||
537 (target_type == TypeManager.double_type) ||
538 (target_type == TypeManager.float_type) ||
539 (target_type == TypeManager.decimal_type))
542 } else if (expr_type == TypeManager.int32_type){
544 // From int to long, double, float, decimal
546 if ((target_type == TypeManager.int64_type) ||
547 (target_type == TypeManager.double_type) ||
548 (target_type == TypeManager.float_type) ||
549 (target_type == TypeManager.decimal_type))
552 } else if (expr_type == TypeManager.uint32_type){
554 // From uint to long, ulong, double, float, decimal
556 if ((target_type == TypeManager.int64_type) ||
557 (target_type == TypeManager.uint64_type) ||
558 (target_type == TypeManager.double_type) ||
559 (target_type == TypeManager.float_type) ||
560 (target_type == TypeManager.decimal_type))
563 } else if ((expr_type == TypeManager.uint64_type) ||
564 (expr_type == TypeManager.int64_type)) {
566 // From long/ulong to double, float, decimal
568 if ((target_type == TypeManager.double_type) ||
569 (target_type == TypeManager.float_type) ||
570 (target_type == TypeManager.decimal_type))
573 } else if (expr_type == TypeManager.char_type){
575 // From char to ushort, int, uint, ulong, long, float, double, decimal
577 if ((target_type == TypeManager.ushort_type) ||
578 (target_type == TypeManager.int32_type) ||
579 (target_type == TypeManager.uint32_type) ||
580 (target_type == TypeManager.uint64_type) ||
581 (target_type == TypeManager.int64_type) ||
582 (target_type == TypeManager.float_type) ||
583 (target_type == TypeManager.double_type) ||
584 (target_type == TypeManager.decimal_type))
587 } else if (expr_type == TypeManager.float_type){
591 if (target_type == TypeManager.double_type)
595 if (expr.eclass == ExprClass.MethodGroup){
596 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
597 MethodGroupExpr mg = expr as MethodGroupExpr;
600 // This should not happen frequently, so we can create an object
601 // to test compatibility
603 Expression c = ImplicitDelegateCreation.Create (
604 ec, mg, target_type, true, Location.Null);
610 if (ImplicitReferenceConversionExists (expr, target_type))
614 // Implicit Constant Expression Conversions
616 if (expr is IntConstant){
617 int value = ((IntConstant) expr).Value;
619 if (target_type == TypeManager.sbyte_type){
620 if (value >= SByte.MinValue && value <= SByte.MaxValue)
622 } else if (target_type == TypeManager.byte_type){
623 if (value >= 0 && value <= Byte.MaxValue)
625 } else if (target_type == TypeManager.short_type){
626 if (value >= Int16.MinValue && value <= Int16.MaxValue)
628 } else if (target_type == TypeManager.ushort_type){
629 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
631 } else if (target_type == TypeManager.uint32_type){
634 } else if (target_type == TypeManager.uint64_type){
636 // we can optimize this case: a positive int32
637 // always fits on a uint64. But we need an opcode
644 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
648 if (expr is LongConstant && target_type == TypeManager.uint64_type){
650 // Try the implicit constant expression conversion
651 // from long to ulong, instead of a nice routine,
654 long v = ((LongConstant) expr).Value;
659 if ((target_type == TypeManager.enum_type ||
660 target_type.IsSubclassOf (TypeManager.enum_type)) &&
662 IntLiteral i = (IntLiteral) expr;
669 // If `expr_type' implements `target_type' (which is an iface)
670 // see TryImplicitIntConversion
672 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
675 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
678 if (expr_type == TypeManager.anonymous_method_type){
679 if (!TypeManager.IsDelegateType (target_type))
682 AnonymousMethod am = (AnonymousMethod) expr;
684 Expression conv = am.Compatible (ec, target_type, true);
693 /// Finds "most encompassed type" according to the spec (13.4.2)
694 /// amongst the methods in the MethodGroupExpr
696 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
700 if (types.Count == 0)
703 if (types.Count == 1)
704 return (Type) types [0];
706 EmptyExpression expr = EmptyExpression.Grab ();
708 foreach (Type t in types) {
715 if (ImplicitStandardConversionExists (ec, expr, best))
720 foreach (Type t in types) {
723 if (!ImplicitStandardConversionExists (ec, expr, t)) {
729 EmptyExpression.Release (expr);
735 /// Finds "most encompassing type" according to the spec (13.4.2)
736 /// amongst the types in the given set
738 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
742 if (types.Count == 0)
745 if (types.Count == 1)
746 return (Type) types [0];
748 EmptyExpression expr = EmptyExpression.Grab ();
750 foreach (Type t in types) {
757 if (ImplicitStandardConversionExists (ec, expr, t))
761 foreach (Type t in types) {
765 if (!ImplicitStandardConversionExists (ec, expr, best)) {
771 EmptyExpression.Release (expr);
777 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
778 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
779 /// for explicit and implicit conversion operators.
781 static public Type FindMostSpecificSource (EmitContext ec, IList list,
782 Expression source, bool apply_explicit_conv_rules)
784 ArrayList src_types_set = new ArrayList ();
787 // If any operator converts from S then Sx = S
789 Type source_type = source.Type;
790 foreach (MethodBase mb in list){
791 ParameterData pd = TypeManager.GetParameterData (mb);
792 Type param_type = pd.ParameterType (0);
794 if (param_type == source_type)
797 src_types_set.Add (param_type);
801 // Explicit Conv rules
803 if (apply_explicit_conv_rules) {
804 ArrayList candidate_set = new ArrayList ();
806 foreach (Type param_type in src_types_set){
807 if (ImplicitStandardConversionExists (ec, source, param_type))
808 candidate_set.Add (param_type);
811 if (candidate_set.Count != 0)
812 return FindMostEncompassedType (ec, candidate_set);
818 if (apply_explicit_conv_rules)
819 return FindMostEncompassingType (ec, src_types_set);
821 return FindMostEncompassedType (ec, src_types_set);
825 /// Finds the most specific target Tx according to section 13.4.4
827 static public Type FindMostSpecificTarget (EmitContext ec, IList list,
828 Type target, bool apply_explicit_conv_rules)
830 ArrayList tgt_types_set = new ArrayList ();
833 // If any operator converts to T then Tx = T
835 foreach (MethodInfo mi in list){
836 Type ret_type = mi.ReturnType;
837 if (ret_type == target)
840 tgt_types_set.Add (ret_type);
844 // Explicit conv rules
846 if (apply_explicit_conv_rules) {
847 ArrayList candidate_set = new ArrayList ();
849 EmptyExpression expr = EmptyExpression.Grab ();
851 foreach (Type ret_type in tgt_types_set){
852 expr.SetType (ret_type);
854 if (ImplicitStandardConversionExists (ec, expr, target))
855 candidate_set.Add (ret_type);
858 EmptyExpression.Release (expr);
860 if (candidate_set.Count != 0)
861 return FindMostEncompassingType (ec, candidate_set);
865 // Okay, final case !
867 if (apply_explicit_conv_rules)
868 return FindMostEncompassedType (ec, tgt_types_set);
870 return FindMostEncompassingType (ec, tgt_types_set);
874 /// User-defined Implicit conversions
876 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
877 Type target, Location loc)
879 return UserDefinedConversion (ec, source, target, loc, false);
883 /// User-defined Explicit conversions
885 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
886 Type target, Location loc)
888 return UserDefinedConversion (ec, source, target, loc, true);
891 static void AddConversionOperators (EmitContext ec, ArrayList list,
892 Expression source, Type target_type,
893 bool look_for_explicit,
899 Type source_type = source.Type;
900 EmptyExpression expr = EmptyExpression.Grab ();
901 foreach (MethodInfo m in mg.Methods) {
902 ParameterData pd = TypeManager.GetParameterData (m);
903 Type return_type = m.ReturnType;
904 Type arg_type = pd.ParameterType (0);
906 if (source_type != arg_type) {
907 if (!ImplicitStandardConversionExists (ec, source, arg_type)) {
908 if (!look_for_explicit)
910 expr.SetType (arg_type);
911 if (!ImplicitStandardConversionExists (ec, expr, source_type))
916 if (target_type != return_type) {
917 expr.SetType (return_type);
918 if (!ImplicitStandardConversionExists (ec, expr, target_type)) {
919 if (!look_for_explicit)
921 expr.SetType (target_type);
922 if (!ImplicitStandardConversionExists (ec, expr, return_type))
930 EmptyExpression.Release (expr);
934 /// Compute the user-defined conversion operator from source_type to target_type.
935 /// `look_for_explicit' controls whether we should also include the list of explicit operators
937 static MethodInfo GetConversionOperator (EmitContext ec, Expression source, Type target_type, bool look_for_explicit)
939 ArrayList ops = new ArrayList (4);
941 Type source_type = source.Type;
943 if (source_type != TypeManager.decimal_type) {
944 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
945 Expression.MethodLookup (ec, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
946 if (look_for_explicit) {
947 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
948 Expression.MethodLookup (
949 ec, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
953 if (target_type != TypeManager.decimal_type) {
954 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
955 Expression.MethodLookup (ec, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
956 if (look_for_explicit) {
957 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
958 Expression.MethodLookup (
959 ec, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
966 Type most_specific_source = FindMostSpecificSource (ec, ops, source, look_for_explicit);
967 if (most_specific_source == null)
970 Type most_specific_target = FindMostSpecificTarget (ec, ops, target_type, look_for_explicit);
971 if (most_specific_target == null)
974 MethodInfo method = null;
976 foreach (MethodInfo m in ops) {
977 if (m.ReturnType != most_specific_target)
979 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
981 // Ambiguous: more than one conversion operator satisfies the signature.
990 static DoubleHash explicit_conv = new DoubleHash (100);
991 static DoubleHash implicit_conv = new DoubleHash (100);
994 /// User-defined conversions
996 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
997 Type target, Location loc,
998 bool look_for_explicit)
1000 Type source_type = source.Type;
1001 MethodInfo method = null;
1004 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1006 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1007 method = (MethodInfo) o;
1009 method = GetConversionOperator (ec, source, target, look_for_explicit);
1010 if (!(source is Constant))
1011 hash.Insert (source_type, target, method);
1017 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1020 // This will do the conversion to the best match that we
1021 // found. Now we need to perform an implict standard conversion
1022 // if the best match was not the type that we were requested
1025 if (look_for_explicit)
1026 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1028 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1034 e = new UserCast (method, source, loc);
1035 if (e.Type != target){
1036 if (!look_for_explicit)
1037 e = ImplicitConversionStandard (ec, e, target, loc);
1039 e = ExplicitConversionStandard (ec, e, target, loc);
1046 /// Converts implicitly the resolved expression `expr' into the
1047 /// `target_type'. It returns a new expression that can be used
1048 /// in a context that expects a `target_type'.
1050 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1051 Type target_type, Location loc)
1055 if (target_type == null)
1056 throw new Exception ("Target type is null");
1058 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1062 e = ImplicitUserConversion (ec, expr, target_type, loc);
1071 /// Attempts to apply the `Standard Implicit
1072 /// Conversion' rules to the expression `expr' into
1073 /// the `target_type'. It returns a new expression
1074 /// that can be used in a context that expects a
1077 /// This is different from `ImplicitConversion' in that the
1078 /// user defined implicit conversions are excluded.
1080 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1081 Type target_type, Location loc)
1083 Type expr_type = expr.Type;
1086 if (expr.eclass == ExprClass.MethodGroup){
1087 if (!TypeManager.IsDelegateType (target_type)){
1092 // Only allow anonymous method conversions on post ISO_1
1094 if (RootContext.Version != LanguageVersion.ISO_1){
1095 MethodGroupExpr mg = expr as MethodGroupExpr;
1097 return ImplicitDelegateCreation.Create (
1098 ec, mg, target_type, false, loc);
1102 if (expr_type == target_type && expr_type != TypeManager.null_type)
1105 e = ImplicitNumericConversion (ec, expr, target_type);
1109 e = ImplicitReferenceConversion (expr, target_type);
1113 if ((target_type == TypeManager.enum_type ||
1114 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1115 expr is IntLiteral){
1116 IntLiteral i = (IntLiteral) expr;
1119 return new EnumConstant ((Constant) expr, target_type);
1123 if (expr_type.IsPointer){
1124 if (target_type == TypeManager.void_ptr_type)
1125 return new EmptyCast (expr, target_type);
1128 // yep, comparing pointer types cant be done with
1129 // t1 == t2, we have to compare their element types.
1131 if (target_type.IsPointer){
1132 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1139 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1140 return new EmptyCast (NullPointer.Null, target_type);
1143 if (expr_type == TypeManager.anonymous_method_type){
1144 if (!TypeManager.IsDelegateType (target_type)){
1145 Report.Error (1660, loc,
1146 "Cannot convert anonymous method block to type `{0}' because it is not a delegate type",
1147 TypeManager.CSharpName (target_type));
1151 AnonymousMethod am = (AnonymousMethod) expr;
1152 int errors = Report.Errors;
1154 Expression conv = am.Compatible (ec, target_type, false);
1159 // We return something instead of null, to avoid
1160 // the duplicate error, since am.Compatible would have
1161 // reported that already
1163 if (errors != Report.Errors)
1164 return new EmptyCast (expr, target_type);
1171 /// Attempts to perform an implicit constant conversion of the IntConstant
1172 /// into a different data type using casts (See Implicit Constant
1173 /// Expression Conversions)
1175 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1177 int value = ic.Value;
1179 if (target_type == TypeManager.sbyte_type){
1180 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1181 return new SByteConstant ((sbyte) value, ic.Location);
1182 } else if (target_type == TypeManager.byte_type){
1183 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1184 return new ByteConstant ((byte) value, ic.Location);
1185 } else if (target_type == TypeManager.short_type){
1186 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1187 return new ShortConstant ((short) value, ic.Location);
1188 } else if (target_type == TypeManager.ushort_type){
1189 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1190 return new UShortConstant ((ushort) value, ic.Location);
1191 } else if (target_type == TypeManager.uint32_type){
1193 return new UIntConstant ((uint) value, ic.Location);
1194 } else if (target_type == TypeManager.uint64_type){
1196 // we can optimize this case: a positive int32
1197 // always fits on a uint64. But we need an opcode
1201 return new ULongConstant ((ulong) value, ic.Location);
1202 } else if (target_type == TypeManager.double_type)
1203 return new DoubleConstant ((double) value, ic.Location);
1204 else if (target_type == TypeManager.float_type)
1205 return new FloatConstant ((float) value, ic.Location);
1207 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1208 Type underlying = TypeManager.EnumToUnderlying (target_type);
1209 Constant e = (Constant) ic;
1212 // Possibly, we need to create a different 0 literal before passing
1215 if (underlying == TypeManager.int64_type)
1216 e = new LongLiteral (0, ic.Location);
1217 else if (underlying == TypeManager.uint64_type)
1218 e = new ULongLiteral (0, ic.Location);
1220 return new EnumConstant (e, target_type);
1224 // If `target_type' is an interface and the type of `ic' implements the interface
1225 // e.g. target_type is IComparable, IConvertible, IFormattable
1227 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1228 return new BoxedCast (ic, target_type);
1234 /// Attempts to implicitly convert `source' into `target_type', using
1235 /// ImplicitConversion. If there is no implicit conversion, then
1236 /// an error is signaled
1238 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1239 Type target_type, Location loc)
1243 e = ImplicitConversion (ec, source, target_type, loc);
1247 if (source is DoubleLiteral) {
1248 if (target_type == TypeManager.float_type) {
1249 Error_664 (loc, "float", "f");
1252 if (target_type == TypeManager.decimal_type) {
1253 Error_664 (loc, "decimal", "m");
1258 source.Error_ValueCannotBeConverted (loc, target_type, false);
1262 static void Error_664 (Location loc, string type, string suffix) {
1263 Report.Error (664, loc,
1264 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
1269 /// Performs the explicit numeric conversions
1271 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1273 Type expr_type = expr.Type;
1274 Type real_target_type = target_type;
1276 if (expr_type == TypeManager.sbyte_type){
1278 // From sbyte to byte, ushort, uint, ulong, char
1280 if (real_target_type == TypeManager.byte_type)
1281 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1282 if (real_target_type == TypeManager.ushort_type)
1283 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1284 if (real_target_type == TypeManager.uint32_type)
1285 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1286 if (real_target_type == TypeManager.uint64_type)
1287 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1288 if (real_target_type == TypeManager.char_type)
1289 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1290 } else if (expr_type == TypeManager.byte_type){
1292 // From byte to sbyte and char
1294 if (real_target_type == TypeManager.sbyte_type)
1295 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1296 if (real_target_type == TypeManager.char_type)
1297 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1298 } else if (expr_type == TypeManager.short_type){
1300 // From short to sbyte, byte, ushort, uint, ulong, char
1302 if (real_target_type == TypeManager.sbyte_type)
1303 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1304 if (real_target_type == TypeManager.byte_type)
1305 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1306 if (real_target_type == TypeManager.ushort_type)
1307 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1308 if (real_target_type == TypeManager.uint32_type)
1309 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1310 if (real_target_type == TypeManager.uint64_type)
1311 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1312 if (real_target_type == TypeManager.char_type)
1313 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1314 } else if (expr_type == TypeManager.ushort_type){
1316 // From ushort to sbyte, byte, short, char
1318 if (real_target_type == TypeManager.sbyte_type)
1319 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1320 if (real_target_type == TypeManager.byte_type)
1321 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1322 if (real_target_type == TypeManager.short_type)
1323 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1324 if (real_target_type == TypeManager.char_type)
1325 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1326 } else if (expr_type == TypeManager.int32_type){
1328 // From int to sbyte, byte, short, ushort, uint, ulong, char
1330 if (real_target_type == TypeManager.sbyte_type)
1331 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1332 if (real_target_type == TypeManager.byte_type)
1333 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1334 if (real_target_type == TypeManager.short_type)
1335 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1336 if (real_target_type == TypeManager.ushort_type)
1337 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1338 if (real_target_type == TypeManager.uint32_type)
1339 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1340 if (real_target_type == TypeManager.uint64_type)
1341 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1342 if (real_target_type == TypeManager.char_type)
1343 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1344 } else if (expr_type == TypeManager.uint32_type){
1346 // From uint to sbyte, byte, short, ushort, int, char
1348 if (real_target_type == TypeManager.sbyte_type)
1349 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1350 if (real_target_type == TypeManager.byte_type)
1351 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1352 if (real_target_type == TypeManager.short_type)
1353 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1354 if (real_target_type == TypeManager.ushort_type)
1355 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1356 if (real_target_type == TypeManager.int32_type)
1357 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1358 if (real_target_type == TypeManager.char_type)
1359 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1360 } else if (expr_type == TypeManager.int64_type){
1362 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1364 if (real_target_type == TypeManager.sbyte_type)
1365 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1366 if (real_target_type == TypeManager.byte_type)
1367 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1368 if (real_target_type == TypeManager.short_type)
1369 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1370 if (real_target_type == TypeManager.ushort_type)
1371 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1372 if (real_target_type == TypeManager.int32_type)
1373 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1374 if (real_target_type == TypeManager.uint32_type)
1375 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1376 if (real_target_type == TypeManager.uint64_type)
1377 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1378 if (real_target_type == TypeManager.char_type)
1379 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1380 } else if (expr_type == TypeManager.uint64_type){
1382 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1384 if (real_target_type == TypeManager.sbyte_type)
1385 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1386 if (real_target_type == TypeManager.byte_type)
1387 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1388 if (real_target_type == TypeManager.short_type)
1389 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1390 if (real_target_type == TypeManager.ushort_type)
1391 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1392 if (real_target_type == TypeManager.int32_type)
1393 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1394 if (real_target_type == TypeManager.uint32_type)
1395 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1396 if (real_target_type == TypeManager.int64_type)
1397 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1398 if (real_target_type == TypeManager.char_type)
1399 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1400 } else if (expr_type == TypeManager.char_type){
1402 // From char to sbyte, byte, short
1404 if (real_target_type == TypeManager.sbyte_type)
1405 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1406 if (real_target_type == TypeManager.byte_type)
1407 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1408 if (real_target_type == TypeManager.short_type)
1409 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1410 } else if (expr_type == TypeManager.float_type){
1412 // From float to sbyte, byte, short,
1413 // ushort, int, uint, long, ulong, char
1416 if (real_target_type == TypeManager.sbyte_type)
1417 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1418 if (real_target_type == TypeManager.byte_type)
1419 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1420 if (real_target_type == TypeManager.short_type)
1421 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1422 if (real_target_type == TypeManager.ushort_type)
1423 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1424 if (real_target_type == TypeManager.int32_type)
1425 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1426 if (real_target_type == TypeManager.uint32_type)
1427 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1428 if (real_target_type == TypeManager.int64_type)
1429 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1430 if (real_target_type == TypeManager.uint64_type)
1431 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1432 if (real_target_type == TypeManager.char_type)
1433 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1434 if (real_target_type == TypeManager.decimal_type)
1435 return new CastToDecimal (expr, true);
1436 } else if (expr_type == TypeManager.double_type){
1438 // From double to sbyte, byte, short,
1439 // ushort, int, uint, long, ulong,
1440 // char, float or decimal
1442 if (real_target_type == TypeManager.sbyte_type)
1443 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1444 if (real_target_type == TypeManager.byte_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1446 if (real_target_type == TypeManager.short_type)
1447 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1448 if (real_target_type == TypeManager.ushort_type)
1449 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1450 if (real_target_type == TypeManager.int32_type)
1451 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1452 if (real_target_type == TypeManager.uint32_type)
1453 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1454 if (real_target_type == TypeManager.int64_type)
1455 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1456 if (real_target_type == TypeManager.uint64_type)
1457 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1458 if (real_target_type == TypeManager.char_type)
1459 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1460 if (real_target_type == TypeManager.float_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1462 if (real_target_type == TypeManager.decimal_type)
1463 return new CastToDecimal (expr, true);
1464 } else if (expr_type == TypeManager.decimal_type) {
1465 return new CastFromDecimal (expr, target_type).Resolve ();
1471 /// Returns whether an explicit reference conversion can be performed
1472 /// from source_type to target_type
1474 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1476 bool target_is_value_type = target_type.IsValueType;
1478 if (source_type == target_type)
1482 // From object to any reference type
1484 if (source_type == TypeManager.object_type && !target_is_value_type)
1488 // From any class S to any class-type T, provided S is a base class of T
1490 if (target_type.IsSubclassOf (source_type))
1494 // From any interface type S to any interface T provided S is not derived from T
1496 if (source_type.IsInterface && target_type.IsInterface){
1497 if (!target_type.IsSubclassOf (source_type))
1502 // From any class type S to any interface T, provided S is not sealed
1503 // and provided S does not implement T.
1505 if (target_type.IsInterface && !source_type.IsSealed &&
1506 !TypeManager.ImplementsInterface (source_type, target_type))
1510 // From any interface-type S to to any class type T, provided T is not
1511 // sealed, or provided T implements S.
1513 if (source_type.IsInterface &&
1514 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1518 // From an array type S with an element type Se to an array type T with an
1519 // element type Te provided all the following are true:
1520 // * S and T differe only in element type, in other words, S and T
1521 // have the same number of dimensions.
1522 // * Both Se and Te are reference types
1523 // * An explicit referenc conversions exist from Se to Te
1525 if (source_type.IsArray && target_type.IsArray) {
1526 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1528 Type source_element_type = TypeManager.GetElementType (source_type);
1529 Type target_element_type = TypeManager.GetElementType (target_type);
1531 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1532 if (ExplicitReferenceConversionExists (source_element_type,
1533 target_element_type))
1539 // From System.Array to any array-type
1540 if (source_type == TypeManager.array_type &&
1541 target_type.IsArray){
1546 // From System delegate to any delegate-type
1548 if (source_type == TypeManager.delegate_type &&
1549 target_type.IsSubclassOf (TypeManager.delegate_type))
1553 // From ICloneable to Array or Delegate types
1555 if (source_type == TypeManager.icloneable_type &&
1556 (target_type == TypeManager.array_type ||
1557 target_type == TypeManager.delegate_type))
1564 /// Implements Explicit Reference conversions
1566 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1568 Type source_type = source.Type;
1569 bool target_is_value_type = target_type.IsValueType;
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 (target_type.IsSubclassOf (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 target_type.IsSubclassOf (TypeManager.delegate_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 ExplicitConversionCore (EmitContext ec, Expression expr,
1678 Type target_type, Location loc)
1680 Type expr_type = expr.Type;
1682 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1683 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1688 // Unboxing conversions; only object types can be convertible to enum
1690 if (expr_type == TypeManager.object_type && target_type.IsValueType || expr_type == TypeManager.enum_type)
1691 return new UnboxCast (expr, target_type);
1693 if (TypeManager.IsEnumType (expr_type)) {
1694 if (expr is EnumConstant)
1695 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1697 return ExplicitConversionCore (ec, new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1700 if (TypeManager.IsEnumType (target_type))
1701 return new EmptyCast (ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc), target_type);
1703 ne = ExplicitNumericConversion (expr, target_type);
1708 // Skip the ExplicitReferenceConversion because we can not convert
1709 // from Null to a ValueType, and ExplicitReference wont check against
1710 // null literal explicitly
1712 if (expr_type != TypeManager.null_type){
1713 ne = ExplicitReferenceConversion (expr, target_type);
1719 ne = ExplicitUnsafe (expr, target_type);
1724 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1731 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1733 Type expr_type = expr.Type;
1735 if (target_type.IsPointer){
1736 if (expr_type.IsPointer)
1737 return new EmptyCast (expr, target_type);
1739 if (expr_type == TypeManager.sbyte_type ||
1740 expr_type == TypeManager.short_type ||
1741 expr_type == TypeManager.int32_type ||
1742 expr_type == TypeManager.int64_type)
1743 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1745 if (expr_type == TypeManager.ushort_type ||
1746 expr_type == TypeManager.uint32_type ||
1747 expr_type == TypeManager.uint64_type ||
1748 expr_type == TypeManager.byte_type)
1749 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1752 if (expr_type.IsPointer){
1753 if (target_type == TypeManager.sbyte_type)
1754 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1755 else if (target_type == TypeManager.byte_type)
1756 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1757 else if (target_type == TypeManager.short_type)
1758 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1759 else if (target_type == TypeManager.ushort_type)
1760 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1761 else if (target_type == TypeManager.int32_type)
1762 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1763 else if (target_type == TypeManager.uint32_type)
1764 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1765 else if (target_type == TypeManager.uint64_type)
1766 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1767 else if (target_type == TypeManager.int64_type){
1768 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1775 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1777 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1778 Type target_type, Location l)
1780 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1784 ne = ExplicitNumericConversion (expr, target_type);
1788 ne = ExplicitReferenceConversion (expr, target_type);
1792 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1793 return new EmptyCast (expr, target_type);
1795 expr.Error_ValueCannotBeConverted (l, target_type, true);
1800 /// Performs an explicit conversion of the expression `expr' whose
1801 /// type is expr.Type to `target_type'.
1803 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1804 Type target_type, Location loc)
1806 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1810 expr.Error_ValueCannotBeConverted (loc, target_type, true);