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 ((Constant)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) {
97 return new NullCast ((Constant)expr, target_type);
99 // I found only one case when it happens -- Foo () ? null : null;
100 Report.Warning (-100, 1, expr.Location, "The result of the expression is always `null'");
101 return new NullCast (new NullLiteral (expr.Location), target_type);
105 // from any class-type S to any interface-type T.
106 if (target_type.IsInterface) {
107 if (target_type != TypeManager.iconvertible_type &&
108 expr_type.IsValueType && (expr is Constant) &&
109 !(expr is IntLiteral || expr is BoolLiteral ||
110 expr is FloatLiteral || expr is DoubleLiteral ||
111 expr is LongLiteral || expr is CharLiteral ||
112 expr is StringLiteral || expr is DecimalLiteral ||
113 expr is UIntLiteral || expr is ULongLiteral)) {
117 if (TypeManager.ImplementsInterface (expr_type, target_type)){
118 if (expr_type.IsClass)
119 return new EmptyCast (expr, target_type);
120 else if (expr_type.IsValueType)
121 return new BoxedCast (expr, target_type);
123 return new EmptyCast (expr, target_type);
127 // from any interface type S to interface-type T.
128 if (expr_type.IsInterface && target_type.IsInterface) {
129 if (TypeManager.ImplementsInterface (expr_type, target_type))
130 return new EmptyCast (expr, target_type);
135 // from an array-type S to an array-type of type T
136 if (expr_type.IsArray && target_type.IsArray) {
137 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
139 Type expr_element_type = TypeManager.GetElementType (expr_type);
141 if (MyEmptyExpr == null)
142 MyEmptyExpr = new EmptyExpression ();
144 MyEmptyExpr.SetType (expr_element_type);
145 Type target_element_type = TypeManager.GetElementType (target_type);
147 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
148 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
149 target_element_type))
150 return new EmptyCast (expr, target_type);
154 // from an array-type to System.Array
155 if (expr_type.IsArray && target_type == TypeManager.array_type)
156 return new EmptyCast (expr, target_type);
158 // from any delegate type to System.Delegate
159 if ((expr_type == TypeManager.delegate_type ||
160 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
161 target_type == TypeManager.delegate_type)
162 return new EmptyCast (expr, target_type);
164 // from any array-type or delegate type into System.ICloneable.
165 if (expr_type.IsArray ||
166 expr_type == TypeManager.delegate_type ||
167 expr_type.IsSubclassOf (TypeManager.delegate_type))
168 if (target_type == TypeManager.icloneable_type)
169 return new EmptyCast (expr, target_type);
175 // Tests whether an implicit reference conversion exists between expr_type
178 static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
180 if (target_type.IsValueType)
183 Type expr_type = expr.Type;
186 // This is the boxed case.
188 if (target_type == TypeManager.object_type) {
189 if (expr_type.IsClass || expr_type.IsValueType ||
190 expr_type.IsInterface || expr_type == TypeManager.enum_type)
191 if (target_type != TypeManager.anonymous_method_type)
195 } else if (expr_type.IsSubclassOf (target_type))
198 // Please remember that all code below actually comes
199 // from ImplicitReferenceConversion so make sure code remains in sync
201 // from any class-type S to any interface-type T.
202 if (target_type.IsInterface) {
203 if (target_type != TypeManager.iconvertible_type &&
204 expr_type.IsValueType && (expr is Constant) &&
205 !(expr is IntLiteral || expr is BoolLiteral ||
206 expr is FloatLiteral || expr is DoubleLiteral ||
207 expr is LongLiteral || expr is CharLiteral ||
208 expr is StringLiteral || expr is DecimalLiteral ||
209 expr is UIntLiteral || expr is ULongLiteral)) {
213 if (TypeManager.ImplementsInterface (expr_type, target_type))
217 // from any interface type S to interface-type T.
218 if (expr_type.IsInterface && target_type.IsInterface)
219 if (TypeManager.ImplementsInterface (expr_type, target_type))
222 // from an array-type S to an array-type of type T
223 if (expr_type.IsArray && target_type.IsArray) {
224 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
226 Type expr_element_type = expr_type.GetElementType ();
228 if (MyEmptyExpr == null)
229 MyEmptyExpr = new EmptyExpression ();
231 MyEmptyExpr.SetType (expr_element_type);
232 Type target_element_type = TypeManager.GetElementType (target_type);
234 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
235 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
236 target_element_type))
241 // from an array-type to System.Array
242 if (expr_type.IsArray && (target_type == TypeManager.array_type))
245 // from any delegate type to System.Delegate
246 if ((expr_type == TypeManager.delegate_type ||
247 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
248 target_type == TypeManager.delegate_type)
249 if (target_type.IsAssignableFrom (expr_type))
252 // from any array-type or delegate type into System.ICloneable.
253 if (expr_type.IsArray ||
254 expr_type == TypeManager.delegate_type ||
255 expr_type.IsSubclassOf (TypeManager.delegate_type))
256 if (target_type == TypeManager.icloneable_type)
259 // from the null type to any reference-type.
260 if (expr_type == TypeManager.null_type){
261 if (target_type.IsPointer)
264 if (!target_type.IsValueType)
271 /// Implicit Numeric Conversions.
273 /// expr is the expression to convert, returns a new expression of type
274 /// target_type or null if an implicit conversion is not possible.
276 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
279 Type expr_type = expr.Type;
282 // Attempt to do the implicit constant expression conversions
284 if (expr is Constant){
285 if (expr is IntConstant){
288 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
292 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
294 // Try the implicit constant expression conversion
295 // from long to ulong, instead of a nice routine,
298 long v = ((LongConstant) expr).Value;
300 return new ULongConstant ((ulong) v, expr.Location);
304 Type real_target_type = target_type;
306 if (expr_type == TypeManager.sbyte_type){
308 // From sbyte to short, int, long, float, double, decimal
310 if (real_target_type == TypeManager.int32_type)
311 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
312 if (real_target_type == TypeManager.int64_type)
313 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
314 if (real_target_type == TypeManager.double_type)
315 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
316 if (real_target_type == TypeManager.float_type)
317 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
318 if (real_target_type == TypeManager.short_type)
319 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
320 if (real_target_type == TypeManager.decimal_type)
321 return new CastToDecimal (expr);
322 } else if (expr_type == TypeManager.byte_type){
324 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
326 if ((real_target_type == TypeManager.short_type) ||
327 (real_target_type == TypeManager.ushort_type) ||
328 (real_target_type == TypeManager.int32_type) ||
329 (real_target_type == TypeManager.uint32_type))
330 return new EmptyCast (expr, target_type);
332 if (real_target_type == TypeManager.uint64_type)
333 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
334 if (real_target_type == TypeManager.int64_type)
335 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
336 if (real_target_type == TypeManager.float_type)
337 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
338 if (real_target_type == TypeManager.double_type)
339 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
340 if (real_target_type == TypeManager.decimal_type)
341 return new CastToDecimal (expr);
343 } else if (expr_type == TypeManager.short_type){
345 // From short to int, long, float, double, decimal
347 if (real_target_type == TypeManager.int32_type)
348 return new EmptyCast (expr, target_type);
349 if (real_target_type == TypeManager.int64_type)
350 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
351 if (real_target_type == TypeManager.double_type)
352 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
353 if (real_target_type == TypeManager.float_type)
354 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
355 if (real_target_type == TypeManager.decimal_type)
356 return new CastToDecimal (expr);
358 } else if (expr_type == TypeManager.ushort_type){
360 // From ushort to int, uint, long, ulong, float, double, decimal
362 if (real_target_type == TypeManager.uint32_type)
363 return new EmptyCast (expr, target_type);
365 if (real_target_type == TypeManager.uint64_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
367 if (real_target_type == TypeManager.int32_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
369 if (real_target_type == TypeManager.int64_type)
370 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
371 if (real_target_type == TypeManager.double_type)
372 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
373 if (real_target_type == TypeManager.float_type)
374 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
375 if (real_target_type == TypeManager.decimal_type)
376 return new CastToDecimal (expr);
377 } else if (expr_type == TypeManager.int32_type){
379 // From int to long, float, double, decimal
381 if (real_target_type == TypeManager.int64_type)
382 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
383 if (real_target_type == TypeManager.double_type)
384 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
385 if (real_target_type == TypeManager.float_type)
386 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
387 if (real_target_type == TypeManager.decimal_type)
388 return new CastToDecimal (expr);
389 } else if (expr_type == TypeManager.uint32_type){
391 // From uint to long, ulong, float, double, decimal
393 if (real_target_type == TypeManager.int64_type)
394 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
395 if (real_target_type == TypeManager.uint64_type)
396 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
397 if (real_target_type == TypeManager.double_type)
398 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
400 if (real_target_type == TypeManager.float_type)
401 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
403 if (real_target_type == TypeManager.decimal_type)
404 return new CastToDecimal (expr);
405 } else if (expr_type == TypeManager.int64_type){
407 // From long/ulong to float, double
409 if (real_target_type == TypeManager.double_type)
410 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
411 if (real_target_type == TypeManager.float_type)
412 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
413 if (real_target_type == TypeManager.decimal_type)
414 return new CastToDecimal (expr);
415 } else if (expr_type == TypeManager.uint64_type){
417 // From ulong to float, double
419 if (real_target_type == TypeManager.double_type)
420 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
422 if (real_target_type == TypeManager.float_type)
423 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
425 if (real_target_type == TypeManager.decimal_type)
426 return new CastToDecimal (expr);
427 } else if (expr_type == TypeManager.char_type){
429 // From char to ushort, int, uint, long, ulong, float, double, decimal
431 if ((real_target_type == TypeManager.ushort_type) ||
432 (real_target_type == TypeManager.int32_type) ||
433 (real_target_type == TypeManager.uint32_type))
434 return new EmptyCast (expr, target_type);
435 if (real_target_type == TypeManager.uint64_type)
436 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
437 if (real_target_type == TypeManager.int64_type)
438 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
439 if (real_target_type == TypeManager.float_type)
440 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
441 if (real_target_type == TypeManager.double_type)
442 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
443 if (real_target_type == TypeManager.decimal_type)
444 return new CastToDecimal (expr);
445 } else if (expr_type == TypeManager.float_type){
449 if (real_target_type == TypeManager.double_type)
450 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
457 /// Same as ImplicitStandardConversionExists except that it also looks at
458 /// implicit user defined conversions - needed for overload resolution
460 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
462 if (ImplicitStandardConversionExists (ec, expr, target_type))
465 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
473 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
475 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
479 /// Determines if a standard implicit conversion exists from
480 /// expr_type to target_type
482 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
484 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
486 Type expr_type = expr.Type;
488 if (expr_type == TypeManager.void_type)
491 if (expr_type == target_type)
495 // First numeric conversions
497 if (expr_type == TypeManager.sbyte_type){
499 // From sbyte to short, int, long, float, double, decimal
501 if ((target_type == TypeManager.int32_type) ||
502 (target_type == TypeManager.int64_type) ||
503 (target_type == TypeManager.double_type) ||
504 (target_type == TypeManager.float_type) ||
505 (target_type == TypeManager.short_type) ||
506 (target_type == TypeManager.decimal_type))
509 } else if (expr_type == TypeManager.byte_type){
511 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
513 if ((target_type == TypeManager.short_type) ||
514 (target_type == TypeManager.ushort_type) ||
515 (target_type == TypeManager.int32_type) ||
516 (target_type == TypeManager.uint32_type) ||
517 (target_type == TypeManager.uint64_type) ||
518 (target_type == TypeManager.int64_type) ||
519 (target_type == TypeManager.float_type) ||
520 (target_type == TypeManager.double_type) ||
521 (target_type == TypeManager.decimal_type))
524 } else if (expr_type == TypeManager.short_type){
526 // From short to int, long, double, float, decimal
528 if ((target_type == TypeManager.int32_type) ||
529 (target_type == TypeManager.int64_type) ||
530 (target_type == TypeManager.double_type) ||
531 (target_type == TypeManager.float_type) ||
532 (target_type == TypeManager.decimal_type))
535 } else if (expr_type == TypeManager.ushort_type){
537 // From ushort to int, uint, long, ulong, double, float, decimal
539 if ((target_type == TypeManager.uint32_type) ||
540 (target_type == TypeManager.uint64_type) ||
541 (target_type == TypeManager.int32_type) ||
542 (target_type == TypeManager.int64_type) ||
543 (target_type == TypeManager.double_type) ||
544 (target_type == TypeManager.float_type) ||
545 (target_type == TypeManager.decimal_type))
548 } else if (expr_type == TypeManager.int32_type){
550 // From int to long, double, float, decimal
552 if ((target_type == TypeManager.int64_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.uint32_type){
560 // From uint to long, ulong, double, float, decimal
562 if ((target_type == TypeManager.int64_type) ||
563 (target_type == TypeManager.uint64_type) ||
564 (target_type == TypeManager.double_type) ||
565 (target_type == TypeManager.float_type) ||
566 (target_type == TypeManager.decimal_type))
569 } else if ((expr_type == TypeManager.uint64_type) ||
570 (expr_type == TypeManager.int64_type)) {
572 // From long/ulong to double, float, decimal
574 if ((target_type == TypeManager.double_type) ||
575 (target_type == TypeManager.float_type) ||
576 (target_type == TypeManager.decimal_type))
579 } else if (expr_type == TypeManager.char_type){
581 // From char to ushort, int, uint, ulong, long, float, double, decimal
583 if ((target_type == TypeManager.ushort_type) ||
584 (target_type == TypeManager.int32_type) ||
585 (target_type == TypeManager.uint32_type) ||
586 (target_type == TypeManager.uint64_type) ||
587 (target_type == TypeManager.int64_type) ||
588 (target_type == TypeManager.float_type) ||
589 (target_type == TypeManager.double_type) ||
590 (target_type == TypeManager.decimal_type))
593 } else if (expr_type == TypeManager.float_type){
597 if (target_type == TypeManager.double_type)
601 if (expr.eclass == ExprClass.MethodGroup){
602 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
603 MethodGroupExpr mg = expr as MethodGroupExpr;
606 // This should not happen frequently, so we can create an object
607 // to test compatibility
609 Expression c = ImplicitDelegateCreation.Create (
610 ec, mg, target_type, true, Location.Null);
616 if (ImplicitReferenceConversionExists (expr, target_type))
620 // Implicit Constant Expression Conversions
622 if (expr is IntConstant){
623 int value = ((IntConstant) expr).Value;
625 if (target_type == TypeManager.sbyte_type){
626 if (value >= SByte.MinValue && value <= SByte.MaxValue)
628 } else if (target_type == TypeManager.byte_type){
629 if (value >= 0 && value <= Byte.MaxValue)
631 } else if (target_type == TypeManager.short_type){
632 if (value >= Int16.MinValue && value <= Int16.MaxValue)
634 } else if (target_type == TypeManager.ushort_type){
635 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
637 } else if (target_type == TypeManager.uint32_type){
640 } else if (target_type == TypeManager.uint64_type){
642 // we can optimize this case: a positive int32
643 // always fits on a uint64. But we need an opcode
650 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
654 if (expr is LongConstant && target_type == TypeManager.uint64_type){
656 // Try the implicit constant expression conversion
657 // from long to ulong, instead of a nice routine,
660 long v = ((LongConstant) expr).Value;
665 if ((target_type == TypeManager.enum_type ||
666 target_type.IsSubclassOf (TypeManager.enum_type)) &&
668 IntLiteral i = (IntLiteral) expr;
675 // If `expr_type' implements `target_type' (which is an iface)
676 // see TryImplicitIntConversion
678 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
681 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
684 if (expr_type == TypeManager.anonymous_method_type){
685 if (!TypeManager.IsDelegateType (target_type))
688 AnonymousMethod am = (AnonymousMethod) expr;
690 Expression conv = am.Compatible (ec, target_type, true);
699 /// Finds "most encompassed type" according to the spec (13.4.2)
700 /// amongst the methods in the MethodGroupExpr
702 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
706 if (types.Count == 0)
709 if (types.Count == 1)
710 return (Type) types [0];
712 EmptyExpression expr = EmptyExpression.Grab ();
714 foreach (Type t in types) {
721 if (ImplicitStandardConversionExists (ec, expr, best))
726 foreach (Type t in types) {
729 if (!ImplicitStandardConversionExists (ec, expr, t)) {
735 EmptyExpression.Release (expr);
741 /// Finds "most encompassing type" according to the spec (13.4.2)
742 /// amongst the types in the given set
744 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
748 if (types.Count == 0)
751 if (types.Count == 1)
752 return (Type) types [0];
754 EmptyExpression expr = EmptyExpression.Grab ();
756 foreach (Type t in types) {
763 if (ImplicitStandardConversionExists (ec, expr, t))
767 foreach (Type t in types) {
771 if (!ImplicitStandardConversionExists (ec, expr, best)) {
777 EmptyExpression.Release (expr);
783 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
784 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
785 /// for explicit and implicit conversion operators.
787 static public Type FindMostSpecificSource (EmitContext ec, IList list,
788 Expression source, bool apply_explicit_conv_rules)
790 ArrayList src_types_set = new ArrayList ();
793 // If any operator converts from S then Sx = S
795 Type source_type = source.Type;
796 foreach (MethodBase mb in list){
797 ParameterData pd = TypeManager.GetParameterData (mb);
798 Type param_type = pd.ParameterType (0);
800 if (param_type == source_type)
803 src_types_set.Add (param_type);
807 // Explicit Conv rules
809 if (apply_explicit_conv_rules) {
810 ArrayList candidate_set = new ArrayList ();
812 foreach (Type param_type in src_types_set){
813 if (ImplicitStandardConversionExists (ec, source, param_type))
814 candidate_set.Add (param_type);
817 if (candidate_set.Count != 0)
818 return FindMostEncompassedType (ec, candidate_set);
824 if (apply_explicit_conv_rules)
825 return FindMostEncompassingType (ec, src_types_set);
827 return FindMostEncompassedType (ec, src_types_set);
831 /// Finds the most specific target Tx according to section 13.4.4
833 static public Type FindMostSpecificTarget (EmitContext ec, IList list,
834 Type target, bool apply_explicit_conv_rules)
836 ArrayList tgt_types_set = new ArrayList ();
839 // If any operator converts to T then Tx = T
841 foreach (MethodInfo mi in list){
842 Type ret_type = mi.ReturnType;
843 if (ret_type == target)
846 tgt_types_set.Add (ret_type);
850 // Explicit conv rules
852 if (apply_explicit_conv_rules) {
853 ArrayList candidate_set = new ArrayList ();
855 EmptyExpression expr = EmptyExpression.Grab ();
857 foreach (Type ret_type in tgt_types_set){
858 expr.SetType (ret_type);
860 if (ImplicitStandardConversionExists (ec, expr, target))
861 candidate_set.Add (ret_type);
864 EmptyExpression.Release (expr);
866 if (candidate_set.Count != 0)
867 return FindMostEncompassingType (ec, candidate_set);
871 // Okay, final case !
873 if (apply_explicit_conv_rules)
874 return FindMostEncompassedType (ec, tgt_types_set);
876 return FindMostEncompassingType (ec, 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 void AddConversionOperators (EmitContext ec, ArrayList list,
898 Expression source, Type target_type,
899 bool look_for_explicit,
905 Type source_type = source.Type;
906 EmptyExpression expr = EmptyExpression.Grab ();
907 foreach (MethodInfo m in mg.Methods) {
908 ParameterData pd = TypeManager.GetParameterData (m);
909 Type return_type = m.ReturnType;
910 Type arg_type = pd.ParameterType (0);
912 if (source_type != arg_type) {
913 if (!ImplicitStandardConversionExists (ec, source, arg_type)) {
914 if (!look_for_explicit)
916 expr.SetType (arg_type);
917 if (!ImplicitStandardConversionExists (ec, expr, source_type))
922 if (target_type != return_type) {
923 expr.SetType (return_type);
924 if (!ImplicitStandardConversionExists (ec, expr, target_type)) {
925 if (!look_for_explicit)
927 expr.SetType (target_type);
928 if (!ImplicitStandardConversionExists (ec, expr, return_type))
936 EmptyExpression.Release (expr);
940 /// Compute the user-defined conversion operator from source_type to target_type.
941 /// `look_for_explicit' controls whether we should also include the list of explicit operators
943 static MethodInfo GetConversionOperator (EmitContext ec, Expression source, Type target_type, bool look_for_explicit)
945 ArrayList ops = new ArrayList (4);
947 Type source_type = source.Type;
949 if (source_type != TypeManager.decimal_type) {
950 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
951 Expression.MethodLookup (ec, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
952 if (look_for_explicit) {
953 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
954 Expression.MethodLookup (
955 ec, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
959 if (target_type != TypeManager.decimal_type) {
960 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
961 Expression.MethodLookup (ec, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
962 if (look_for_explicit) {
963 AddConversionOperators (ec, ops, source, target_type, look_for_explicit,
964 Expression.MethodLookup (
965 ec, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
972 Type most_specific_source = FindMostSpecificSource (ec, ops, source, look_for_explicit);
973 if (most_specific_source == null)
976 Type most_specific_target = FindMostSpecificTarget (ec, ops, target_type, look_for_explicit);
977 if (most_specific_target == null)
980 MethodInfo method = null;
982 foreach (MethodInfo m in ops) {
983 if (m.ReturnType != most_specific_target)
985 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
987 // Ambiguous: more than one conversion operator satisfies the signature.
996 static DoubleHash explicit_conv = new DoubleHash (100);
997 static DoubleHash implicit_conv = new DoubleHash (100);
1000 /// User-defined conversions
1002 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1003 Type target, Location loc,
1004 bool look_for_explicit)
1006 Type source_type = source.Type;
1007 MethodInfo method = null;
1010 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1012 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1013 method = (MethodInfo) o;
1015 method = GetConversionOperator (ec, source, target, look_for_explicit);
1016 if (!(source is Constant))
1017 hash.Insert (source_type, target, method);
1023 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1026 // This will do the conversion to the best match that we
1027 // found. Now we need to perform an implict standard conversion
1028 // if the best match was not the type that we were requested
1031 if (look_for_explicit)
1032 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1034 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1040 e = new UserCast (method, source, loc);
1041 if (e.Type != target){
1042 if (!look_for_explicit)
1043 e = ImplicitConversionStandard (ec, e, target, loc);
1045 e = ExplicitConversionStandard (ec, e, target, loc);
1052 /// Converts implicitly the resolved expression `expr' into the
1053 /// `target_type'. It returns a new expression that can be used
1054 /// in a context that expects a `target_type'.
1056 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1057 Type target_type, Location loc)
1061 if (target_type == null)
1062 throw new Exception ("Target type is null");
1064 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1068 e = ImplicitUserConversion (ec, expr, target_type, loc);
1077 /// Attempts to apply the `Standard Implicit
1078 /// Conversion' rules to the expression `expr' into
1079 /// the `target_type'. It returns a new expression
1080 /// that can be used in a context that expects a
1083 /// This is different from `ImplicitConversion' in that the
1084 /// user defined implicit conversions are excluded.
1086 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1087 Type target_type, Location loc)
1089 Type expr_type = expr.Type;
1092 if (expr.eclass == ExprClass.MethodGroup){
1093 if (!TypeManager.IsDelegateType (target_type)){
1098 // Only allow anonymous method conversions on post ISO_1
1100 if (RootContext.Version != LanguageVersion.ISO_1){
1101 MethodGroupExpr mg = expr as MethodGroupExpr;
1103 return ImplicitDelegateCreation.Create (
1104 ec, mg, target_type, false, loc);
1108 if (expr_type == target_type && expr_type != TypeManager.null_type)
1111 e = ImplicitNumericConversion (ec, expr, target_type);
1115 e = ImplicitReferenceConversion (expr, target_type);
1119 if ((target_type == TypeManager.enum_type ||
1120 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1121 expr is IntLiteral){
1122 IntLiteral i = (IntLiteral) expr;
1125 return new EnumConstant ((Constant) expr, target_type);
1129 if (expr_type.IsPointer){
1130 if (target_type == TypeManager.void_ptr_type)
1131 return new EmptyCast (expr, target_type);
1134 // yep, comparing pointer types cant be done with
1135 // t1 == t2, we have to compare their element types.
1137 if (target_type.IsPointer){
1138 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1145 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1146 return new EmptyCast (NullPointer.Null, target_type);
1149 if (expr_type == TypeManager.anonymous_method_type){
1150 if (!TypeManager.IsDelegateType (target_type)){
1151 Report.Error (1660, loc,
1152 "Cannot convert anonymous method block to type `{0}' because it is not a delegate type",
1153 TypeManager.CSharpName (target_type));
1157 AnonymousMethod am = (AnonymousMethod) expr;
1158 int errors = Report.Errors;
1160 Expression conv = am.Compatible (ec, target_type, false);
1165 // We return something instead of null, to avoid
1166 // the duplicate error, since am.Compatible would have
1167 // reported that already
1169 if (errors != Report.Errors)
1170 return new EmptyCast (expr, target_type);
1177 /// Attempts to perform an implicit constant conversion of the IntConstant
1178 /// into a different data type using casts (See Implicit Constant
1179 /// Expression Conversions)
1181 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1183 int value = ic.Value;
1185 if (target_type == TypeManager.sbyte_type){
1186 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1187 return new SByteConstant ((sbyte) value, ic.Location);
1188 } else if (target_type == TypeManager.byte_type){
1189 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1190 return new ByteConstant ((byte) value, ic.Location);
1191 } else if (target_type == TypeManager.short_type){
1192 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1193 return new ShortConstant ((short) value, ic.Location);
1194 } else if (target_type == TypeManager.ushort_type){
1195 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1196 return new UShortConstant ((ushort) value, ic.Location);
1197 } else if (target_type == TypeManager.uint32_type){
1199 return new UIntConstant ((uint) value, ic.Location);
1200 } else if (target_type == TypeManager.uint64_type){
1202 // we can optimize this case: a positive int32
1203 // always fits on a uint64. But we need an opcode
1207 return new ULongConstant ((ulong) value, ic.Location);
1208 } else if (target_type == TypeManager.double_type)
1209 return new DoubleConstant ((double) value, ic.Location);
1210 else if (target_type == TypeManager.float_type)
1211 return new FloatConstant ((float) value, ic.Location);
1213 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1214 Type underlying = TypeManager.EnumToUnderlying (target_type);
1215 Constant e = (Constant) ic;
1218 // Possibly, we need to create a different 0 literal before passing
1221 if (underlying == TypeManager.int64_type)
1222 e = new LongLiteral (0, ic.Location);
1223 else if (underlying == TypeManager.uint64_type)
1224 e = new ULongLiteral (0, ic.Location);
1226 return new EnumConstant (e, target_type);
1230 // If `target_type' is an interface and the type of `ic' implements the interface
1231 // e.g. target_type is IComparable, IConvertible, IFormattable
1233 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1234 return new BoxedCast (ic, target_type);
1240 /// Attempts to implicitly convert `source' into `target_type', using
1241 /// ImplicitConversion. If there is no implicit conversion, then
1242 /// an error is signaled
1244 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1245 Type target_type, Location loc)
1249 e = ImplicitConversion (ec, source, target_type, loc);
1253 if (source is DoubleLiteral) {
1254 if (target_type == TypeManager.float_type) {
1255 Error_664 (loc, "float", "f");
1258 if (target_type == TypeManager.decimal_type) {
1259 Error_664 (loc, "decimal", "m");
1264 source.Error_ValueCannotBeConverted (loc, target_type, false);
1268 static void Error_664 (Location loc, string type, string suffix) {
1269 Report.Error (664, loc,
1270 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
1275 /// Performs the explicit numeric conversions
1277 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1279 Type expr_type = expr.Type;
1280 Type real_target_type = target_type;
1282 if (expr_type == TypeManager.sbyte_type){
1284 // From sbyte to byte, ushort, uint, ulong, char
1286 if (real_target_type == TypeManager.byte_type)
1287 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1288 if (real_target_type == TypeManager.ushort_type)
1289 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1290 if (real_target_type == TypeManager.uint32_type)
1291 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1292 if (real_target_type == TypeManager.uint64_type)
1293 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1294 if (real_target_type == TypeManager.char_type)
1295 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1296 } else if (expr_type == TypeManager.byte_type){
1298 // From byte to sbyte and char
1300 if (real_target_type == TypeManager.sbyte_type)
1301 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1302 if (real_target_type == TypeManager.char_type)
1303 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1304 } else if (expr_type == TypeManager.short_type){
1306 // From short to sbyte, byte, ushort, uint, ulong, char
1308 if (real_target_type == TypeManager.sbyte_type)
1309 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1310 if (real_target_type == TypeManager.byte_type)
1311 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1312 if (real_target_type == TypeManager.ushort_type)
1313 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1314 if (real_target_type == TypeManager.uint32_type)
1315 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1316 if (real_target_type == TypeManager.uint64_type)
1317 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1318 if (real_target_type == TypeManager.char_type)
1319 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1320 } else if (expr_type == TypeManager.ushort_type){
1322 // From ushort to sbyte, byte, short, char
1324 if (real_target_type == TypeManager.sbyte_type)
1325 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1326 if (real_target_type == TypeManager.byte_type)
1327 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1328 if (real_target_type == TypeManager.short_type)
1329 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1330 if (real_target_type == TypeManager.char_type)
1331 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1332 } else if (expr_type == TypeManager.int32_type){
1334 // From int to sbyte, byte, short, ushort, uint, ulong, char
1336 if (real_target_type == TypeManager.sbyte_type)
1337 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1338 if (real_target_type == TypeManager.byte_type)
1339 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1340 if (real_target_type == TypeManager.short_type)
1341 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1342 if (real_target_type == TypeManager.ushort_type)
1343 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1344 if (real_target_type == TypeManager.uint32_type)
1345 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1346 if (real_target_type == TypeManager.uint64_type)
1347 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1348 if (real_target_type == TypeManager.char_type)
1349 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1350 } else if (expr_type == TypeManager.uint32_type){
1352 // From uint to sbyte, byte, short, ushort, int, char
1354 if (real_target_type == TypeManager.sbyte_type)
1355 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1356 if (real_target_type == TypeManager.byte_type)
1357 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1358 if (real_target_type == TypeManager.short_type)
1359 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1360 if (real_target_type == TypeManager.ushort_type)
1361 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1362 if (real_target_type == TypeManager.int32_type)
1363 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1364 if (real_target_type == TypeManager.char_type)
1365 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1366 } else if (expr_type == TypeManager.int64_type){
1368 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1370 if (real_target_type == TypeManager.sbyte_type)
1371 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1372 if (real_target_type == TypeManager.byte_type)
1373 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1374 if (real_target_type == TypeManager.short_type)
1375 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1376 if (real_target_type == TypeManager.ushort_type)
1377 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1378 if (real_target_type == TypeManager.int32_type)
1379 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1380 if (real_target_type == TypeManager.uint32_type)
1381 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1382 if (real_target_type == TypeManager.uint64_type)
1383 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1384 if (real_target_type == TypeManager.char_type)
1385 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1386 } else if (expr_type == TypeManager.uint64_type){
1388 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1390 if (real_target_type == TypeManager.sbyte_type)
1391 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1392 if (real_target_type == TypeManager.byte_type)
1393 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1394 if (real_target_type == TypeManager.short_type)
1395 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1396 if (real_target_type == TypeManager.ushort_type)
1397 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1398 if (real_target_type == TypeManager.int32_type)
1399 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1400 if (real_target_type == TypeManager.uint32_type)
1401 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1402 if (real_target_type == TypeManager.int64_type)
1403 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1404 if (real_target_type == TypeManager.char_type)
1405 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1406 } else if (expr_type == TypeManager.char_type){
1408 // From char to sbyte, byte, short
1410 if (real_target_type == TypeManager.sbyte_type)
1411 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1412 if (real_target_type == TypeManager.byte_type)
1413 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1414 if (real_target_type == TypeManager.short_type)
1415 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1416 } else if (expr_type == TypeManager.float_type){
1418 // From float to sbyte, byte, short,
1419 // ushort, int, uint, long, ulong, char
1422 if (real_target_type == TypeManager.sbyte_type)
1423 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1424 if (real_target_type == TypeManager.byte_type)
1425 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1426 if (real_target_type == TypeManager.short_type)
1427 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1428 if (real_target_type == TypeManager.ushort_type)
1429 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1430 if (real_target_type == TypeManager.int32_type)
1431 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1432 if (real_target_type == TypeManager.uint32_type)
1433 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1434 if (real_target_type == TypeManager.int64_type)
1435 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1436 if (real_target_type == TypeManager.uint64_type)
1437 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1438 if (real_target_type == TypeManager.char_type)
1439 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1440 if (real_target_type == TypeManager.decimal_type)
1441 return new CastToDecimal (expr, true);
1442 } else if (expr_type == TypeManager.double_type){
1444 // From double to sbyte, byte, short,
1445 // ushort, int, uint, long, ulong,
1446 // char, float or decimal
1448 if (real_target_type == TypeManager.sbyte_type)
1449 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1450 if (real_target_type == TypeManager.byte_type)
1451 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1452 if (real_target_type == TypeManager.short_type)
1453 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1454 if (real_target_type == TypeManager.ushort_type)
1455 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1456 if (real_target_type == TypeManager.int32_type)
1457 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1458 if (real_target_type == TypeManager.uint32_type)
1459 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1460 if (real_target_type == TypeManager.int64_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1462 if (real_target_type == TypeManager.uint64_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1464 if (real_target_type == TypeManager.char_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1466 if (real_target_type == TypeManager.float_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1468 if (real_target_type == TypeManager.decimal_type)
1469 return new CastToDecimal (expr, true);
1470 } else if (expr_type == TypeManager.decimal_type) {
1471 return new CastFromDecimal (expr, target_type).Resolve ();
1477 /// Returns whether an explicit reference conversion can be performed
1478 /// from source_type to target_type
1480 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1482 bool target_is_value_type = target_type.IsValueType;
1484 if (source_type == target_type)
1488 // From object to any reference type
1490 if (source_type == TypeManager.object_type && !target_is_value_type)
1494 // From any class S to any class-type T, provided S is a base class of T
1496 if (target_type.IsSubclassOf (source_type))
1500 // From any interface type S to any interface T provided S is not derived from T
1502 if (source_type.IsInterface && target_type.IsInterface){
1503 if (!target_type.IsSubclassOf (source_type))
1508 // From any class type S to any interface T, provided S is not sealed
1509 // and provided S does not implement T.
1511 if (target_type.IsInterface && !source_type.IsSealed &&
1512 !TypeManager.ImplementsInterface (source_type, target_type))
1516 // From any interface-type S to to any class type T, provided T is not
1517 // sealed, or provided T implements S.
1519 if (source_type.IsInterface &&
1520 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1524 // From an array type S with an element type Se to an array type T with an
1525 // element type Te provided all the following are true:
1526 // * S and T differe only in element type, in other words, S and T
1527 // have the same number of dimensions.
1528 // * Both Se and Te are reference types
1529 // * An explicit referenc conversions exist from Se to Te
1531 if (source_type.IsArray && target_type.IsArray) {
1532 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1534 Type source_element_type = TypeManager.GetElementType (source_type);
1535 Type target_element_type = TypeManager.GetElementType (target_type);
1537 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1538 if (ExplicitReferenceConversionExists (source_element_type,
1539 target_element_type))
1545 // From System.Array to any array-type
1546 if (source_type == TypeManager.array_type &&
1547 target_type.IsArray){
1552 // From System delegate to any delegate-type
1554 if (source_type == TypeManager.delegate_type &&
1555 target_type.IsSubclassOf (TypeManager.delegate_type))
1559 // From ICloneable to Array or Delegate types
1561 if (source_type == TypeManager.icloneable_type &&
1562 (target_type == TypeManager.array_type ||
1563 target_type == TypeManager.delegate_type))
1570 /// Implements Explicit Reference conversions
1572 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1574 Type source_type = source.Type;
1575 bool target_is_value_type = target_type.IsValueType;
1578 // From object to any reference type
1580 if (source_type == TypeManager.object_type && !target_is_value_type)
1581 return new ClassCast (source, target_type);
1584 // Unboxing conversion.
1586 if (((source_type == TypeManager.enum_type &&
1587 !(source is EmptyCast)) ||
1588 source_type == TypeManager.value_type) && target_is_value_type)
1589 return new UnboxCast (source, target_type);
1592 // From any class S to any class-type T, provided S is a base class of T
1594 if (target_type.IsSubclassOf (source_type))
1595 return new ClassCast (source, target_type);
1598 // From any interface type S to any interface T provided S is not derived from T
1600 if (source_type.IsInterface && target_type.IsInterface){
1601 if (TypeManager.ImplementsInterface (source_type, target_type))
1604 return new ClassCast (source, target_type);
1608 // From any class type S to any interface T, provides S is not sealed
1609 // and provided S does not implement T.
1611 if (target_type.IsInterface && !source_type.IsSealed) {
1612 if (TypeManager.ImplementsInterface (source_type, target_type))
1615 return new ClassCast (source, target_type);
1620 // From any interface-type S to to any class type T, provided T is not
1621 // sealed, or provided T implements S.
1623 if (source_type.IsInterface) {
1624 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1625 if (target_type.IsClass)
1626 return new ClassCast (source, target_type);
1628 return new UnboxCast (source, target_type);
1634 // From an array type S with an element type Se to an array type T with an
1635 // element type Te provided all the following are true:
1636 // * S and T differe only in element type, in other words, S and T
1637 // have the same number of dimensions.
1638 // * Both Se and Te are reference types
1639 // * An explicit referenc conversions exist from Se to Te
1641 if (source_type.IsArray && target_type.IsArray) {
1642 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1644 Type source_element_type = TypeManager.GetElementType (source_type);
1645 Type target_element_type = TypeManager.GetElementType (target_type);
1647 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1648 if (ExplicitReferenceConversionExists (source_element_type,
1649 target_element_type))
1650 return new ClassCast (source, target_type);
1655 // From System.Array to any array-type
1656 if (source_type == TypeManager.array_type &&
1657 target_type.IsArray) {
1658 return new ClassCast (source, target_type);
1662 // From System delegate to any delegate-type
1664 if (source_type == TypeManager.delegate_type &&
1665 target_type.IsSubclassOf (TypeManager.delegate_type))
1666 return new ClassCast (source, target_type);
1669 // From ICloneable to Array or Delegate types
1671 if (source_type == TypeManager.icloneable_type &&
1672 (target_type == TypeManager.array_type ||
1673 target_type == TypeManager.delegate_type))
1674 return new ClassCast (source, target_type);
1680 /// Performs an explicit conversion of the expression `expr' whose
1681 /// type is expr.Type to `target_type'.
1683 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1684 Type target_type, Location loc)
1686 Type expr_type = expr.Type;
1688 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1689 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1694 // Unboxing conversions; only object types can be convertible to enum
1696 if (expr_type == TypeManager.object_type && target_type.IsValueType || expr_type == TypeManager.enum_type)
1697 return new UnboxCast (expr, target_type);
1699 if (TypeManager.IsEnumType (expr_type)) {
1700 if (expr is EnumConstant)
1701 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1703 return ExplicitConversionCore (ec, new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1706 if (TypeManager.IsEnumType (target_type))
1707 return new EmptyCast (ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc), target_type);
1709 ne = ExplicitNumericConversion (expr, target_type);
1714 // Skip the ExplicitReferenceConversion because we can not convert
1715 // from Null to a ValueType, and ExplicitReference wont check against
1716 // null literal explicitly
1718 if (expr_type != TypeManager.null_type){
1719 ne = ExplicitReferenceConversion (expr, target_type);
1725 ne = ExplicitUnsafe (expr, target_type);
1730 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1737 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1739 Type expr_type = expr.Type;
1741 if (target_type.IsPointer){
1742 if (expr_type.IsPointer)
1743 return new EmptyCast (expr, target_type);
1745 if (expr_type == TypeManager.sbyte_type ||
1746 expr_type == TypeManager.short_type ||
1747 expr_type == TypeManager.int32_type ||
1748 expr_type == TypeManager.int64_type)
1749 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1751 if (expr_type == TypeManager.ushort_type ||
1752 expr_type == TypeManager.uint32_type ||
1753 expr_type == TypeManager.uint64_type ||
1754 expr_type == TypeManager.byte_type)
1755 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1758 if (expr_type.IsPointer){
1759 if (target_type == TypeManager.sbyte_type)
1760 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1761 else if (target_type == TypeManager.byte_type)
1762 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1763 else if (target_type == TypeManager.short_type)
1764 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1765 else if (target_type == TypeManager.ushort_type)
1766 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1767 else if (target_type == TypeManager.int32_type)
1768 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1769 else if (target_type == TypeManager.uint32_type)
1770 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1771 else if (target_type == TypeManager.uint64_type)
1772 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1773 else if (target_type == TypeManager.int64_type){
1774 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1781 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1783 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1784 Type target_type, Location l)
1786 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1790 ne = ExplicitNumericConversion (expr, target_type);
1794 ne = ExplicitReferenceConversion (expr, target_type);
1798 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1799 return new EmptyCast (expr, target_type);
1801 expr.Error_ValueCannotBeConverted (l, target_type, true);
1806 /// Performs an explicit conversion of the expression `expr' whose
1807 /// type is expr.Type to `target_type'.
1809 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1810 Type target_type, Location loc)
1812 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1816 expr.Error_ValueCannotBeConverted (loc, target_type, true);