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 public void Error_CannotConvertType (Location loc, Type source, Type target)
34 Report.Error (30, loc, "Cannot convert type '" +
35 TypeManager.CSharpName (source) + "' to '" +
36 TypeManager.CSharpName (target) + "'");
39 static EmptyExpression MyEmptyExpr;
40 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
42 Type expr_type = expr.Type;
44 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
45 // if we are a method group, emit a warning
50 if (expr_type == TypeManager.void_type)
54 // notice that it is possible to write "ValueType v = 1", the ValueType here
55 // is an abstract class, and not really a value type, so we apply the same rules.
57 if (target_type == TypeManager.object_type) {
59 // A pointer type cannot be converted to object
61 if (expr_type.IsPointer)
64 if (expr_type.IsValueType)
65 return new BoxedCast (expr);
66 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
67 if (expr_type == TypeManager.anonymous_method_type)
69 return new EmptyCast (expr, target_type);
73 } else if (target_type == TypeManager.value_type) {
74 if (expr_type.IsValueType)
75 return new BoxedCast (expr);
76 if (expr_type == TypeManager.null_type)
77 return new NullCast (expr, target_type);
80 } else if (expr_type.IsSubclassOf (target_type)) {
82 // Special case: enumeration to System.Enum.
83 // System.Enum is not a value type, it is a class, so we need
84 // a boxing conversion
87 return new BoxedCast (expr);
89 return new EmptyCast (expr, target_type);
92 // This code is kind of mirrored inside ImplicitStandardConversionExists
93 // with the small distinction that we only probe there
95 // Always ensure that the code here and there is in sync
97 // from the null type to any reference-type.
98 if (expr_type == TypeManager.null_type){
99 if (target_type.IsPointer)
100 return new EmptyCast (NullPointer.Null, target_type);
102 if (!target_type.IsValueType)
103 return new NullCast (expr, target_type);
106 // from any class-type S to any interface-type T.
107 if (target_type.IsInterface) {
108 if (target_type != TypeManager.iconvertible_type &&
109 expr_type.IsValueType && (expr is Constant) &&
110 !(expr is IntLiteral || expr is BoolLiteral ||
111 expr is FloatLiteral || expr is DoubleLiteral ||
112 expr is LongLiteral || expr is CharLiteral ||
113 expr is StringLiteral || expr is DecimalLiteral ||
114 expr is UIntLiteral || expr is ULongLiteral)) {
118 if (TypeManager.ImplementsInterface (expr_type, target_type)){
119 if (expr_type.IsClass)
120 return new EmptyCast (expr, target_type);
121 else if (expr_type.IsValueType)
122 return new BoxedCast (expr, target_type);
124 return new EmptyCast (expr, target_type);
128 // from any interface type S to interface-type T.
129 if (expr_type.IsInterface && target_type.IsInterface) {
130 if (TypeManager.ImplementsInterface (expr_type, target_type))
131 return new EmptyCast (expr, target_type);
136 // from an array-type S to an array-type of type T
137 if (expr_type.IsArray && target_type.IsArray) {
138 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
140 Type expr_element_type = TypeManager.GetElementType (expr_type);
142 if (MyEmptyExpr == null)
143 MyEmptyExpr = new EmptyExpression ();
145 MyEmptyExpr.SetType (expr_element_type);
146 Type target_element_type = TypeManager.GetElementType (target_type);
148 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
149 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
150 target_element_type))
151 return new EmptyCast (expr, target_type);
155 // from an array-type to System.Array
156 if (expr_type.IsArray && target_type == TypeManager.array_type)
157 return new EmptyCast (expr, target_type);
159 // from any delegate type to System.Delegate
160 if ((expr_type == TypeManager.delegate_type ||
161 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
162 target_type == TypeManager.delegate_type)
163 return new EmptyCast (expr, target_type);
165 // from any array-type or delegate type into System.ICloneable.
166 if (expr_type.IsArray ||
167 expr_type == TypeManager.delegate_type ||
168 expr_type.IsSubclassOf (TypeManager.delegate_type))
169 if (target_type == TypeManager.icloneable_type)
170 return new EmptyCast (expr, target_type);
176 // Tests whether an implicit reference conversion exists between expr_type
179 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
181 Type expr_type = expr.Type;
184 // This is the boxed case.
186 if (target_type == TypeManager.object_type) {
187 if (expr_type.IsClass || expr_type.IsValueType ||
188 expr_type.IsInterface || expr_type == TypeManager.enum_type)
189 if (target_type != TypeManager.anonymous_method_type)
193 } else if (expr_type.IsSubclassOf (target_type))
196 // Please remember that all code below actually comes
197 // from ImplicitReferenceConversion so make sure code remains in sync
199 // from any class-type S to any interface-type T.
200 if (target_type.IsInterface) {
201 if (target_type != TypeManager.iconvertible_type &&
202 expr_type.IsValueType && (expr is Constant) &&
203 !(expr is IntLiteral || expr is BoolLiteral ||
204 expr is FloatLiteral || expr is DoubleLiteral ||
205 expr is LongLiteral || expr is CharLiteral ||
206 expr is StringLiteral || expr is DecimalLiteral ||
207 expr is UIntLiteral || expr is ULongLiteral)) {
211 if (TypeManager.ImplementsInterface (expr_type, target_type))
215 // from any interface type S to interface-type T.
216 if (expr_type.IsInterface && target_type.IsInterface)
217 if (TypeManager.ImplementsInterface (expr_type, target_type))
220 // from an array-type S to an array-type of type T
221 if (expr_type.IsArray && target_type.IsArray) {
222 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
224 Type expr_element_type = expr_type.GetElementType ();
226 if (MyEmptyExpr == null)
227 MyEmptyExpr = new EmptyExpression ();
229 MyEmptyExpr.SetType (expr_element_type);
230 Type target_element_type = TypeManager.GetElementType (target_type);
232 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
233 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
234 target_element_type))
239 // from an array-type to System.Array
240 if (expr_type.IsArray && (target_type == TypeManager.array_type))
243 // from any delegate type to System.Delegate
244 if ((expr_type == TypeManager.delegate_type ||
245 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
246 target_type == TypeManager.delegate_type)
247 if (target_type.IsAssignableFrom (expr_type))
250 // from any array-type or delegate type into System.ICloneable.
251 if (expr_type.IsArray ||
252 expr_type == TypeManager.delegate_type ||
253 expr_type.IsSubclassOf (TypeManager.delegate_type))
254 if (target_type == TypeManager.icloneable_type)
257 // from the null type to any reference-type.
258 if (expr_type == TypeManager.null_type){
259 if (target_type.IsPointer)
262 if (!target_type.IsValueType)
269 /// Implicit Numeric Conversions.
271 /// expr is the expression to convert, returns a new expression of type
272 /// target_type or null if an implicit conversion is not possible.
274 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
275 Type target_type, Location loc)
277 Type expr_type = expr.Type;
280 // Attempt to do the implicit constant expression conversions
282 if (expr is Constant){
283 if (expr is IntConstant){
286 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
290 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
292 // Try the implicit constant expression conversion
293 // from long to ulong, instead of a nice routine,
296 long v = ((LongConstant) expr).Value;
298 return new ULongConstant ((ulong) v);
302 Type real_target_type = target_type;
304 if (expr_type == TypeManager.sbyte_type){
306 // From sbyte to short, int, long, float, double, decimal
308 if (real_target_type == TypeManager.int32_type)
309 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
310 if (real_target_type == TypeManager.int64_type)
311 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
312 if (real_target_type == TypeManager.double_type)
313 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
314 if (real_target_type == TypeManager.float_type)
315 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
316 if (real_target_type == TypeManager.short_type)
317 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
318 if (real_target_type == TypeManager.decimal_type)
319 return new CastToDecimal (ec, expr);
320 } else if (expr_type == TypeManager.byte_type){
322 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
324 if ((real_target_type == TypeManager.short_type) ||
325 (real_target_type == TypeManager.ushort_type) ||
326 (real_target_type == TypeManager.int32_type) ||
327 (real_target_type == TypeManager.uint32_type))
328 return new EmptyCast (expr, target_type);
330 if (real_target_type == TypeManager.uint64_type)
331 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
332 if (real_target_type == TypeManager.int64_type)
333 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
334 if (real_target_type == TypeManager.float_type)
335 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
336 if (real_target_type == TypeManager.double_type)
337 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
338 if (real_target_type == TypeManager.decimal_type)
339 return new CastToDecimal (ec, expr);
341 } else if (expr_type == TypeManager.short_type){
343 // From short to int, long, float, double, decimal
345 if (real_target_type == TypeManager.int32_type)
346 return new EmptyCast (expr, target_type);
347 if (real_target_type == TypeManager.int64_type)
348 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
349 if (real_target_type == TypeManager.double_type)
350 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
351 if (real_target_type == TypeManager.float_type)
352 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
353 if (real_target_type == TypeManager.decimal_type)
354 return new CastToDecimal (ec, expr);
356 } else if (expr_type == TypeManager.ushort_type){
358 // From ushort to int, uint, long, ulong, float, double, decimal
360 if (real_target_type == TypeManager.uint32_type)
361 return new EmptyCast (expr, target_type);
363 if (real_target_type == TypeManager.uint64_type)
364 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
365 if (real_target_type == TypeManager.int32_type)
366 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
367 if (real_target_type == TypeManager.int64_type)
368 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
369 if (real_target_type == TypeManager.double_type)
370 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
371 if (real_target_type == TypeManager.float_type)
372 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
373 if (real_target_type == TypeManager.decimal_type)
374 return new CastToDecimal (ec, expr);
375 } else if (expr_type == TypeManager.int32_type){
377 // From int to long, float, double, decimal
379 if (real_target_type == TypeManager.int64_type)
380 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
381 if (real_target_type == TypeManager.double_type)
382 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
383 if (real_target_type == TypeManager.float_type)
384 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
385 if (real_target_type == TypeManager.decimal_type)
386 return new CastToDecimal (ec, expr);
387 } else if (expr_type == TypeManager.uint32_type){
389 // From uint to long, ulong, float, double, decimal
391 if (real_target_type == TypeManager.int64_type)
392 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
393 if (real_target_type == TypeManager.uint64_type)
394 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
395 if (real_target_type == TypeManager.double_type)
396 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
398 if (real_target_type == TypeManager.float_type)
399 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
401 if (real_target_type == TypeManager.decimal_type)
402 return new CastToDecimal (ec, expr);
403 } else if (expr_type == TypeManager.int64_type){
405 // From long/ulong to float, double
407 if (real_target_type == TypeManager.double_type)
408 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
409 if (real_target_type == TypeManager.float_type)
410 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
411 if (real_target_type == TypeManager.decimal_type)
412 return new CastToDecimal (ec, expr);
413 } else if (expr_type == TypeManager.uint64_type){
415 // From ulong to float, double
417 if (real_target_type == TypeManager.double_type)
418 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
420 if (real_target_type == TypeManager.float_type)
421 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
423 if (real_target_type == TypeManager.decimal_type)
424 return new CastToDecimal (ec, expr);
425 } else if (expr_type == TypeManager.char_type){
427 // From char to ushort, int, uint, long, ulong, float, double, decimal
429 if ((real_target_type == TypeManager.ushort_type) ||
430 (real_target_type == TypeManager.int32_type) ||
431 (real_target_type == TypeManager.uint32_type))
432 return new EmptyCast (expr, target_type);
433 if (real_target_type == TypeManager.uint64_type)
434 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
435 if (real_target_type == TypeManager.int64_type)
436 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
437 if (real_target_type == TypeManager.float_type)
438 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
439 if (real_target_type == TypeManager.double_type)
440 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
441 if (real_target_type == TypeManager.decimal_type)
442 return new CastToDecimal (ec, expr);
443 } else if (expr_type == TypeManager.float_type){
447 if (real_target_type == TypeManager.double_type)
448 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
456 /// Same as ImplicitStandardConversionExists except that it also looks at
457 /// implicit user defined conversions - needed for overload resolution
459 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
461 if (ImplicitStandardConversionExists (ec, expr, target_type))
464 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
472 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
474 Expression dummy = ImplicitUserConversion (
475 ec, new EmptyExpression (source), target, Location.Null);
476 return dummy != null;
480 /// Determines if a standard implicit conversion exists from
481 /// expr_type to target_type
483 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
485 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
487 Type expr_type = expr.Type;
489 if (expr_type == TypeManager.void_type)
492 if (expr_type == target_type)
496 // First numeric conversions
498 if (expr_type == TypeManager.sbyte_type){
500 // From sbyte to short, int, long, float, double, decimal
502 if ((target_type == TypeManager.int32_type) ||
503 (target_type == TypeManager.int64_type) ||
504 (target_type == TypeManager.double_type) ||
505 (target_type == TypeManager.float_type) ||
506 (target_type == TypeManager.short_type) ||
507 (target_type == TypeManager.decimal_type))
510 } else if (expr_type == TypeManager.byte_type){
512 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
514 if ((target_type == TypeManager.short_type) ||
515 (target_type == TypeManager.ushort_type) ||
516 (target_type == TypeManager.int32_type) ||
517 (target_type == TypeManager.uint32_type) ||
518 (target_type == TypeManager.uint64_type) ||
519 (target_type == TypeManager.int64_type) ||
520 (target_type == TypeManager.float_type) ||
521 (target_type == TypeManager.double_type) ||
522 (target_type == TypeManager.decimal_type))
525 } else if (expr_type == TypeManager.short_type){
527 // From short to int, long, double, float, decimal
529 if ((target_type == TypeManager.int32_type) ||
530 (target_type == TypeManager.int64_type) ||
531 (target_type == TypeManager.double_type) ||
532 (target_type == TypeManager.float_type) ||
533 (target_type == TypeManager.decimal_type))
536 } else if (expr_type == TypeManager.ushort_type){
538 // From ushort to int, uint, long, ulong, double, float, decimal
540 if ((target_type == TypeManager.uint32_type) ||
541 (target_type == TypeManager.uint64_type) ||
542 (target_type == TypeManager.int32_type) ||
543 (target_type == TypeManager.int64_type) ||
544 (target_type == TypeManager.double_type) ||
545 (target_type == TypeManager.float_type) ||
546 (target_type == TypeManager.decimal_type))
549 } else if (expr_type == TypeManager.int32_type){
551 // From int to long, double, float, decimal
553 if ((target_type == TypeManager.int64_type) ||
554 (target_type == TypeManager.double_type) ||
555 (target_type == TypeManager.float_type) ||
556 (target_type == TypeManager.decimal_type))
559 } else if (expr_type == TypeManager.uint32_type){
561 // From uint to long, ulong, double, float, decimal
563 if ((target_type == TypeManager.int64_type) ||
564 (target_type == TypeManager.uint64_type) ||
565 (target_type == TypeManager.double_type) ||
566 (target_type == TypeManager.float_type) ||
567 (target_type == TypeManager.decimal_type))
570 } else if ((expr_type == TypeManager.uint64_type) ||
571 (expr_type == TypeManager.int64_type)) {
573 // From long/ulong to double, float, decimal
575 if ((target_type == TypeManager.double_type) ||
576 (target_type == TypeManager.float_type) ||
577 (target_type == TypeManager.decimal_type))
580 } else if (expr_type == TypeManager.char_type){
582 // From char to ushort, int, uint, ulong, long, float, double, decimal
584 if ((target_type == TypeManager.ushort_type) ||
585 (target_type == TypeManager.int32_type) ||
586 (target_type == TypeManager.uint32_type) ||
587 (target_type == TypeManager.uint64_type) ||
588 (target_type == TypeManager.int64_type) ||
589 (target_type == TypeManager.float_type) ||
590 (target_type == TypeManager.double_type) ||
591 (target_type == TypeManager.decimal_type))
594 } else if (expr_type == TypeManager.float_type){
598 if (target_type == TypeManager.double_type)
602 if (expr.eclass == ExprClass.MethodGroup){
603 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
604 MethodGroupExpr mg = expr as MethodGroupExpr;
607 // This should not happen frequently, so we can create an object
608 // to test compatibility
610 Expression c = ImplicitDelegateCreation.Create (
611 ec, mg, target_type, true, Location.Null);
617 if (ImplicitReferenceConversionExists (expr, target_type))
621 // Implicit Constant Expression Conversions
623 if (expr is IntConstant){
624 int value = ((IntConstant) expr).Value;
626 if (target_type == TypeManager.sbyte_type){
627 if (value >= SByte.MinValue && value <= SByte.MaxValue)
629 } else if (target_type == TypeManager.byte_type){
630 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
632 } else if (target_type == TypeManager.short_type){
633 if (value >= Int16.MinValue && value <= Int16.MaxValue)
635 } else if (target_type == TypeManager.ushort_type){
636 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
638 } else if (target_type == TypeManager.uint32_type){
641 } else if (target_type == TypeManager.uint64_type){
643 // we can optimize this case: a positive int32
644 // always fits on a uint64. But we need an opcode
651 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
655 if (expr is LongConstant && target_type == TypeManager.uint64_type){
657 // Try the implicit constant expression conversion
658 // from long to ulong, instead of a nice routine,
661 long v = ((LongConstant) expr).Value;
666 if ((target_type == TypeManager.enum_type ||
667 target_type.IsSubclassOf (TypeManager.enum_type)) &&
669 IntLiteral i = (IntLiteral) expr;
676 // If `expr_type' implements `target_type' (which is an iface)
677 // see TryImplicitIntConversion
679 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
682 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
685 if (expr_type == TypeManager.anonymous_method_type){
686 if (!TypeManager.IsDelegateType (target_type))
689 AnonymousMethod am = (AnonymousMethod) expr;
691 Expression conv = am.Compatible (ec, target_type, true);
700 /// Finds "most encompassed type" according to the spec (13.4.2)
701 /// amongst the methods in the MethodGroupExpr
703 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
707 if (types.Count == 0)
710 if (types.Count == 1)
711 return (Type) types [0];
713 EmptyExpression expr = EmptyExpression.Grab ();
715 foreach (Type t in types) {
722 if (ImplicitStandardConversionExists (ec, expr, best))
727 foreach (Type t in types) {
730 if (!ImplicitStandardConversionExists (ec, expr, t)) {
736 EmptyExpression.Release (expr);
742 /// Finds "most encompassing type" according to the spec (13.4.2)
743 /// amongst the types in the given set
745 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
749 if (types.Count == 0)
752 if (types.Count == 1)
753 return (Type) types [0];
755 EmptyExpression expr = EmptyExpression.Grab ();
757 foreach (Type t in types) {
764 if (ImplicitStandardConversionExists (ec, expr, t))
768 foreach (Type t in types) {
772 if (!ImplicitStandardConversionExists (ec, expr, best)) {
778 EmptyExpression.Release (expr);
784 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
785 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
786 /// for explicit and implicit conversion operators.
788 static public Type FindMostSpecificSource (EmitContext ec, IList list,
789 Expression source, bool apply_explicit_conv_rules,
792 ArrayList src_types_set = new ArrayList ();
795 // If any operator converts from S then Sx = S
797 Type source_type = source.Type;
798 foreach (MethodBase mb in list){
799 ParameterData pd = TypeManager.GetParameterData (mb);
800 Type param_type = pd.ParameterType (0);
802 if (param_type == source_type)
805 src_types_set.Add (param_type);
809 // Explicit Conv rules
811 if (apply_explicit_conv_rules) {
812 ArrayList candidate_set = new ArrayList ();
814 foreach (Type param_type in src_types_set){
815 if (ImplicitStandardConversionExists (ec, source, param_type))
816 candidate_set.Add (param_type);
819 if (candidate_set.Count != 0)
820 return FindMostEncompassedType (ec, candidate_set);
826 if (apply_explicit_conv_rules)
827 return FindMostEncompassingType (ec, src_types_set);
829 return FindMostEncompassedType (ec, src_types_set);
833 /// Finds the most specific target Tx according to section 13.4.4
835 static public Type FindMostSpecificTarget (EmitContext ec, IList list,
836 Type target, bool apply_explicit_conv_rules,
839 ArrayList tgt_types_set = new ArrayList ();
842 // If any operator converts to T then Tx = T
844 foreach (MethodInfo mi in list){
845 Type ret_type = mi.ReturnType;
846 if (ret_type == target)
849 tgt_types_set.Add (ret_type);
853 // Explicit conv rules
855 if (apply_explicit_conv_rules) {
856 ArrayList candidate_set = new ArrayList ();
858 EmptyExpression expr = EmptyExpression.Grab ();
860 foreach (Type ret_type in tgt_types_set){
861 expr.SetType (ret_type);
863 if (ImplicitStandardConversionExists (ec, expr, target))
864 candidate_set.Add (ret_type);
867 EmptyExpression.Release (expr);
869 if (candidate_set.Count != 0)
870 return FindMostEncompassingType (ec, candidate_set);
874 // Okay, final case !
876 if (apply_explicit_conv_rules)
877 return FindMostEncompassedType (ec, tgt_types_set);
879 return FindMostEncompassingType (ec, tgt_types_set);
883 /// User-defined Implicit conversions
885 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
886 Type target, Location loc)
888 return UserDefinedConversion (ec, source, target, loc, false);
892 /// User-defined Explicit conversions
894 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
895 Type target, Location loc)
897 return UserDefinedConversion (ec, source, target, loc, true);
900 static void AddConversionOperators (EmitContext ec, ArrayList list,
901 Expression source, Type target_type,
902 bool look_for_explicit,
908 Type source_type = source.Type;
909 EmptyExpression expr = EmptyExpression.Grab ();
910 foreach (MethodInfo m in mg.Methods) {
911 ParameterData pd = TypeManager.GetParameterData (m);
912 Type return_type = m.ReturnType;
913 Type arg_type = pd.ParameterType (0);
915 if (source_type != arg_type) {
916 if (!ImplicitStandardConversionExists (ec, source, arg_type)) {
917 if (!look_for_explicit)
919 expr.SetType (arg_type);
920 if (!ImplicitStandardConversionExists (ec, expr, source_type))
925 if (target_type != return_type) {
926 expr.SetType (return_type);
927 if (!ImplicitStandardConversionExists (ec, expr, target_type)) {
928 if (!look_for_explicit)
930 expr.SetType (target_type);
931 if (!ImplicitStandardConversionExists (ec, expr, return_type))
939 EmptyExpression.Release (expr);
943 /// Computes the list of the user-defined conversion
944 /// operators from source_type to target_type. `look_for_explicit'
945 /// controls whether we should also include the list of explicit
948 static IList GetConversionOperators (EmitContext ec,
949 Expression source, Type target_type,
950 Location loc, bool look_for_explicit)
952 ArrayList ret = new ArrayList (4);
954 Type source_type = source.Type;
956 if (source_type != TypeManager.decimal_type) {
957 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
958 Expression.MethodLookup (
959 ec, source_type, "op_Implicit", loc) as MethodGroupExpr);
960 if (look_for_explicit) {
961 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
962 Expression.MethodLookup (
963 ec, source_type, "op_Explicit", loc) as MethodGroupExpr);
967 if (target_type != TypeManager.decimal_type) {
968 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
969 Expression.MethodLookup (
970 ec, target_type, "op_Implicit", loc) as MethodGroupExpr);
971 if (look_for_explicit) {
972 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
973 Expression.MethodLookup (
974 ec, target_type, "op_Explicit", loc) as MethodGroupExpr);
981 static DoubleHash explicit_conv = new DoubleHash (100);
982 static DoubleHash implicit_conv = new DoubleHash (100);
985 /// User-defined conversions
987 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
988 Type target, Location loc,
989 bool look_for_explicit)
991 Type source_type = source.Type;
992 MethodInfo method = null;
993 Type most_specific_source = null;
994 Type most_specific_target = null;
997 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
999 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1000 method = (MethodInfo) o;
1001 if (method != null) {
1002 ParameterData pd = TypeManager.GetParameterData (method);
1003 most_specific_source = pd.ParameterType (0);
1004 most_specific_target = method.ReturnType;
1007 IList ops = GetConversionOperators (ec, source, target, loc, look_for_explicit);
1008 if (ops == null || ops.Count == 0) {
1013 most_specific_source = FindMostSpecificSource (ec, ops, source, look_for_explicit, loc);
1014 if (most_specific_source == null) {
1019 most_specific_target = FindMostSpecificTarget (ec, ops, target, look_for_explicit, loc);
1020 if (most_specific_target == null) {
1027 foreach (MethodInfo m in ops) {
1028 ParameterData pd = TypeManager.GetParameterData (m);
1030 if (pd.ParameterType (0) == most_specific_source &&
1031 m.ReturnType == most_specific_target) {
1040 if (!(source is Constant))
1041 hash.Insert (source_type, target, method);
1048 // This will do the conversion to the best match that we
1049 // found. Now we need to perform an implict standard conversion
1050 // if the best match was not the type that we were requested
1053 if (look_for_explicit)
1054 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1056 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1062 e = new UserCast (method, source, loc);
1063 if (e.Type != target){
1064 if (!look_for_explicit)
1065 e = ImplicitConversionStandard (ec, e, target, loc);
1067 e = ExplicitConversionStandard (ec, e, target, loc);
1074 /// Converts implicitly the resolved expression `expr' into the
1075 /// `target_type'. It returns a new expression that can be used
1076 /// in a context that expects a `target_type'.
1078 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1079 Type target_type, Location loc)
1083 if (target_type == null)
1084 throw new Exception ("Target type is null");
1086 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1090 e = ImplicitUserConversion (ec, expr, target_type, loc);
1099 /// Attempts to apply the `Standard Implicit
1100 /// Conversion' rules to the expression `expr' into
1101 /// the `target_type'. It returns a new expression
1102 /// that can be used in a context that expects a
1105 /// This is different from `ImplicitConversion' in that the
1106 /// user defined implicit conversions are excluded.
1108 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1109 Type target_type, Location loc)
1111 Type expr_type = expr.Type;
1114 if (expr.eclass == ExprClass.MethodGroup){
1115 if (!TypeManager.IsDelegateType (target_type)){
1120 // Only allow anonymous method conversions on post ISO_1
1122 if (RootContext.Version != LanguageVersion.ISO_1){
1123 MethodGroupExpr mg = expr as MethodGroupExpr;
1125 return ImplicitDelegateCreation.Create (
1126 ec, mg, target_type, false, loc);
1130 if (expr_type == target_type && expr_type != TypeManager.null_type)
1133 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1137 e = ImplicitReferenceConversion (expr, target_type);
1141 if ((target_type == TypeManager.enum_type ||
1142 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1143 expr is IntLiteral){
1144 IntLiteral i = (IntLiteral) expr;
1147 return new EnumConstant ((Constant) expr, target_type);
1151 if (expr_type.IsPointer){
1152 if (target_type == TypeManager.void_ptr_type)
1153 return new EmptyCast (expr, target_type);
1156 // yep, comparing pointer types cant be done with
1157 // t1 == t2, we have to compare their element types.
1159 if (target_type.IsPointer){
1160 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1165 if (target_type.IsPointer) {
1166 if (expr_type == TypeManager.null_type)
1167 return new EmptyCast (NullPointer.Null, target_type);
1169 if (expr_type == TypeManager.void_ptr_type)
1170 return new EmptyCast (expr, target_type);
1174 if (expr_type == TypeManager.anonymous_method_type){
1175 if (!TypeManager.IsDelegateType (target_type)){
1176 Report.Error (1660, loc,
1177 "Cannot convert anonymous method block to type `{0}' because it is not a delegate type",
1178 TypeManager.CSharpName (target_type));
1182 AnonymousMethod am = (AnonymousMethod) expr;
1183 int errors = Report.Errors;
1185 Expression conv = am.Compatible (ec, target_type, false);
1190 // We return something instead of null, to avoid
1191 // the duplicate error, since am.Compatible would have
1192 // reported that already
1194 if (errors != Report.Errors)
1195 return new EmptyCast (expr, target_type);
1202 /// Attempts to perform an implicit constant conversion of the IntConstant
1203 /// into a different data type using casts (See Implicit Constant
1204 /// Expression Conversions)
1206 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1208 int value = ic.Value;
1210 if (target_type == TypeManager.sbyte_type){
1211 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1212 return new SByteConstant ((sbyte) value);
1213 } else if (target_type == TypeManager.byte_type){
1214 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1215 return new ByteConstant ((byte) value);
1216 } else if (target_type == TypeManager.short_type){
1217 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1218 return new ShortConstant ((short) value);
1219 } else if (target_type == TypeManager.ushort_type){
1220 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1221 return new UShortConstant ((ushort) value);
1222 } else if (target_type == TypeManager.uint32_type){
1224 return new UIntConstant ((uint) value);
1225 } else if (target_type == TypeManager.uint64_type){
1227 // we can optimize this case: a positive int32
1228 // always fits on a uint64. But we need an opcode
1232 return new ULongConstant ((ulong) value);
1233 } else if (target_type == TypeManager.double_type)
1234 return new DoubleConstant ((double) value);
1235 else if (target_type == TypeManager.float_type)
1236 return new FloatConstant ((float) value);
1238 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1239 Type underlying = TypeManager.EnumToUnderlying (target_type);
1240 Constant e = (Constant) ic;
1243 // Possibly, we need to create a different 0 literal before passing
1246 if (underlying == TypeManager.int64_type)
1247 e = new LongLiteral (0);
1248 else if (underlying == TypeManager.uint64_type)
1249 e = new ULongLiteral (0);
1251 return new EnumConstant (e, target_type);
1255 // If `target_type' is an interface and the type of `ic' implements the interface
1256 // e.g. target_type is IComparable, IConvertible, IFormattable
1258 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1259 return new BoxedCast (ic);
1264 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1266 if (source.Name == target.Name){
1267 Report.ExtraInformation (loc,
1269 "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
1270 source.Name, source.Assembly.FullName, target.Assembly.FullName));
1273 Report.Error (29, loc, "Cannot implicitly convert type {0} to `{1}'",
1274 source == TypeManager.anonymous_method_type ?
1275 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1276 TypeManager.CSharpName (target));
1280 /// Attempts to implicitly convert `source' into `target_type', using
1281 /// ImplicitConversion. If there is no implicit conversion, then
1282 /// an error is signaled
1284 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1285 Type target_type, Location loc)
1289 e = ImplicitConversion (ec, source, target_type, loc);
1293 if (source is DoubleLiteral) {
1294 if (target_type == TypeManager.float_type) {
1295 Error_664 (loc, "float", "f");
1298 if (target_type == TypeManager.decimal_type) {
1299 Error_664 (loc, "decimal", "m");
1304 if (source is Constant){
1305 Constant c = (Constant) source;
1307 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1311 Error_CannotImplicitConversion (loc, source.Type, target_type);
1316 static void Error_664 (Location loc, string type, string suffix) {
1317 Report.Error (664, loc,
1318 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
1323 /// Performs the explicit numeric conversions
1325 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1327 Type expr_type = expr.Type;
1330 // If we have an enumeration, extract the underlying type,
1331 // use this during the comparison, but wrap around the original
1334 Type real_target_type = target_type;
1336 if (TypeManager.IsEnumType (real_target_type))
1337 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1339 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1340 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1342 if (real_target_type != target_type)
1343 return new EmptyCast (ce, target_type);
1347 if (expr_type == TypeManager.sbyte_type){
1349 // From sbyte to byte, ushort, uint, ulong, char
1351 if (real_target_type == TypeManager.byte_type)
1352 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1353 if (real_target_type == TypeManager.ushort_type)
1354 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1355 if (real_target_type == TypeManager.uint32_type)
1356 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1357 if (real_target_type == TypeManager.uint64_type)
1358 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1359 if (real_target_type == TypeManager.char_type)
1360 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1361 } else if (expr_type == TypeManager.byte_type){
1363 // From byte to sbyte and char
1365 if (real_target_type == TypeManager.sbyte_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1367 if (real_target_type == TypeManager.char_type)
1368 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1369 } else if (expr_type == TypeManager.short_type){
1371 // From short to sbyte, byte, ushort, uint, ulong, char
1373 if (real_target_type == TypeManager.sbyte_type)
1374 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1375 if (real_target_type == TypeManager.byte_type)
1376 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1377 if (real_target_type == TypeManager.ushort_type)
1378 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1379 if (real_target_type == TypeManager.uint32_type)
1380 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1381 if (real_target_type == TypeManager.uint64_type)
1382 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1383 if (real_target_type == TypeManager.char_type)
1384 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1385 } else if (expr_type == TypeManager.ushort_type){
1387 // From ushort to sbyte, byte, short, char
1389 if (real_target_type == TypeManager.sbyte_type)
1390 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1391 if (real_target_type == TypeManager.byte_type)
1392 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1393 if (real_target_type == TypeManager.short_type)
1394 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1395 if (real_target_type == TypeManager.char_type)
1396 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1397 } else if (expr_type == TypeManager.int32_type){
1399 // From int to sbyte, byte, short, ushort, uint, ulong, char
1401 if (real_target_type == TypeManager.sbyte_type)
1402 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1403 if (real_target_type == TypeManager.byte_type)
1404 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1405 if (real_target_type == TypeManager.short_type)
1406 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1407 if (real_target_type == TypeManager.ushort_type)
1408 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1409 if (real_target_type == TypeManager.uint32_type)
1410 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1411 if (real_target_type == TypeManager.uint64_type)
1412 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1413 if (real_target_type == TypeManager.char_type)
1414 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1415 } else if (expr_type == TypeManager.uint32_type){
1417 // From uint to sbyte, byte, short, ushort, int, char
1419 if (real_target_type == TypeManager.sbyte_type)
1420 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1421 if (real_target_type == TypeManager.byte_type)
1422 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1423 if (real_target_type == TypeManager.short_type)
1424 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1425 if (real_target_type == TypeManager.ushort_type)
1426 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1427 if (real_target_type == TypeManager.int32_type)
1428 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1429 if (real_target_type == TypeManager.char_type)
1430 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1431 } else if (expr_type == TypeManager.int64_type){
1433 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1435 if (real_target_type == TypeManager.sbyte_type)
1436 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1437 if (real_target_type == TypeManager.byte_type)
1438 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1439 if (real_target_type == TypeManager.short_type)
1440 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1441 if (real_target_type == TypeManager.ushort_type)
1442 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1443 if (real_target_type == TypeManager.int32_type)
1444 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1445 if (real_target_type == TypeManager.uint32_type)
1446 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1447 if (real_target_type == TypeManager.uint64_type)
1448 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1449 if (real_target_type == TypeManager.char_type)
1450 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1451 } else if (expr_type == TypeManager.uint64_type){
1453 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1455 if (real_target_type == TypeManager.sbyte_type)
1456 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1457 if (real_target_type == TypeManager.byte_type)
1458 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1459 if (real_target_type == TypeManager.short_type)
1460 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1461 if (real_target_type == TypeManager.ushort_type)
1462 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1463 if (real_target_type == TypeManager.int32_type)
1464 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1465 if (real_target_type == TypeManager.uint32_type)
1466 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1467 if (real_target_type == TypeManager.int64_type)
1468 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1469 if (real_target_type == TypeManager.char_type)
1470 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1471 } else if (expr_type == TypeManager.char_type){
1473 // From char to sbyte, byte, short
1475 if (real_target_type == TypeManager.sbyte_type)
1476 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1477 if (real_target_type == TypeManager.byte_type)
1478 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1479 if (real_target_type == TypeManager.short_type)
1480 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1481 } else if (expr_type == TypeManager.float_type){
1483 // From float to sbyte, byte, short,
1484 // ushort, int, uint, long, ulong, char
1487 if (real_target_type == TypeManager.sbyte_type)
1488 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1489 if (real_target_type == TypeManager.byte_type)
1490 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1491 if (real_target_type == TypeManager.short_type)
1492 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1493 if (real_target_type == TypeManager.ushort_type)
1494 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1495 if (real_target_type == TypeManager.int32_type)
1496 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1497 if (real_target_type == TypeManager.uint32_type)
1498 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1499 if (real_target_type == TypeManager.int64_type)
1500 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1501 if (real_target_type == TypeManager.uint64_type)
1502 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1503 if (real_target_type == TypeManager.char_type)
1504 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1505 if (real_target_type == TypeManager.decimal_type)
1506 return new CastToDecimal (ec, expr, true);
1507 } else if (expr_type == TypeManager.double_type){
1509 // From double to sbyte, byte, short,
1510 // ushort, int, uint, long, ulong,
1511 // char, float or decimal
1513 if (real_target_type == TypeManager.sbyte_type)
1514 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1515 if (real_target_type == TypeManager.byte_type)
1516 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1517 if (real_target_type == TypeManager.short_type)
1518 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1519 if (real_target_type == TypeManager.ushort_type)
1520 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1521 if (real_target_type == TypeManager.int32_type)
1522 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1523 if (real_target_type == TypeManager.uint32_type)
1524 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1525 if (real_target_type == TypeManager.int64_type)
1526 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1527 if (real_target_type == TypeManager.uint64_type)
1528 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1529 if (real_target_type == TypeManager.char_type)
1530 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1531 if (real_target_type == TypeManager.float_type)
1532 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1533 if (real_target_type == TypeManager.decimal_type)
1534 return new CastToDecimal (ec, expr, true);
1535 } else if (expr_type == TypeManager.decimal_type) {
1537 // From decimal to sbyte, byte, short, ushort, int, uint,
1538 // long, ulong, char, double or float
1540 if (real_target_type == TypeManager.sbyte_type ||
1541 real_target_type == TypeManager.byte_type ||
1542 real_target_type == TypeManager.short_type ||
1543 real_target_type == TypeManager.ushort_type ||
1544 real_target_type == TypeManager.int32_type ||
1545 real_target_type == TypeManager.uint32_type ||
1546 real_target_type == TypeManager.int64_type ||
1547 real_target_type == TypeManager.uint64_type ||
1548 real_target_type == TypeManager.char_type ||
1549 real_target_type == TypeManager.double_type ||
1550 real_target_type == TypeManager.float_type)
1551 return new CastFromDecimal (ec, expr, target_type);
1557 /// Returns whether an explicit reference conversion can be performed
1558 /// from source_type to target_type
1560 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1562 bool target_is_value_type = target_type.IsValueType;
1564 if (source_type == target_type)
1568 // From object to any reference type
1570 if (source_type == TypeManager.object_type && !target_is_value_type)
1574 // From any class S to any class-type T, provided S is a base class of T
1576 if (target_type.IsSubclassOf (source_type))
1580 // From any interface type S to any interface T provided S is not derived from T
1582 if (source_type.IsInterface && target_type.IsInterface){
1583 if (!target_type.IsSubclassOf (source_type))
1588 // From any class type S to any interface T, provided S is not sealed
1589 // and provided S does not implement T.
1591 if (target_type.IsInterface && !source_type.IsSealed &&
1592 !TypeManager.ImplementsInterface (source_type, target_type))
1596 // From any interface-type S to to any class type T, provided T is not
1597 // sealed, or provided T implements S.
1599 if (source_type.IsInterface &&
1600 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1604 // From an array type S with an element type Se to an array type T with an
1605 // element type Te provided all the following are true:
1606 // * S and T differe only in element type, in other words, S and T
1607 // have the same number of dimensions.
1608 // * Both Se and Te are reference types
1609 // * An explicit referenc conversions exist from Se to Te
1611 if (source_type.IsArray && target_type.IsArray) {
1612 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1614 Type source_element_type = TypeManager.GetElementType (source_type);
1615 Type target_element_type = TypeManager.GetElementType (target_type);
1617 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1618 if (ExplicitReferenceConversionExists (source_element_type,
1619 target_element_type))
1625 // From System.Array to any array-type
1626 if (source_type == TypeManager.array_type &&
1627 target_type.IsArray){
1632 // From System delegate to any delegate-type
1634 if (source_type == TypeManager.delegate_type &&
1635 target_type.IsSubclassOf (TypeManager.delegate_type))
1639 // From ICloneable to Array or Delegate types
1641 if (source_type == TypeManager.icloneable_type &&
1642 (target_type == TypeManager.array_type ||
1643 target_type == TypeManager.delegate_type))
1650 /// Implements Explicit Reference conversions
1652 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1654 Type source_type = source.Type;
1655 bool target_is_value_type = target_type.IsValueType;
1658 // From object to any reference type
1660 if (source_type == TypeManager.object_type && !target_is_value_type)
1661 return new ClassCast (source, target_type);
1664 // Unboxing conversion.
1666 if (((source_type == TypeManager.enum_type &&
1667 !(source is EmptyCast)) ||
1668 source_type == TypeManager.value_type) && target_is_value_type)
1669 return new UnboxCast (source, target_type);
1672 // From any class S to any class-type T, provided S is a base class of T
1674 if (target_type.IsSubclassOf (source_type))
1675 return new ClassCast (source, target_type);
1678 // From any interface type S to any interface T provided S is not derived from T
1680 if (source_type.IsInterface && target_type.IsInterface){
1681 if (TypeManager.ImplementsInterface (source_type, target_type))
1684 return new ClassCast (source, target_type);
1688 // From any class type S to any interface T, provides S is not sealed
1689 // and provided S does not implement T.
1691 if (target_type.IsInterface && !source_type.IsSealed) {
1692 if (TypeManager.ImplementsInterface (source_type, target_type))
1695 return new ClassCast (source, target_type);
1700 // From any interface-type S to to any class type T, provided T is not
1701 // sealed, or provided T implements S.
1703 if (source_type.IsInterface) {
1704 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1705 if (target_type.IsClass)
1706 return new ClassCast (source, target_type);
1708 return new UnboxCast (source, target_type);
1714 // From an array type S with an element type Se to an array type T with an
1715 // element type Te provided all the following are true:
1716 // * S and T differe only in element type, in other words, S and T
1717 // have the same number of dimensions.
1718 // * Both Se and Te are reference types
1719 // * An explicit referenc conversions exist from Se to Te
1721 if (source_type.IsArray && target_type.IsArray) {
1722 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1724 Type source_element_type = TypeManager.GetElementType (source_type);
1725 Type target_element_type = TypeManager.GetElementType (target_type);
1727 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1728 if (ExplicitReferenceConversionExists (source_element_type,
1729 target_element_type))
1730 return new ClassCast (source, target_type);
1735 // From System.Array to any array-type
1736 if (source_type == TypeManager.array_type &&
1737 target_type.IsArray) {
1738 return new ClassCast (source, target_type);
1742 // From System delegate to any delegate-type
1744 if (source_type == TypeManager.delegate_type &&
1745 target_type.IsSubclassOf (TypeManager.delegate_type))
1746 return new ClassCast (source, target_type);
1749 // From ICloneable to Array or Delegate types
1751 if (source_type == TypeManager.icloneable_type &&
1752 (target_type == TypeManager.array_type ||
1753 target_type == TypeManager.delegate_type))
1754 return new ClassCast (source, target_type);
1760 /// Performs an explicit conversion of the expression `expr' whose
1761 /// type is expr.Type to `target_type'.
1763 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1764 Type target_type, Location loc)
1766 Type expr_type = expr.Type;
1767 Type original_expr_type = expr_type;
1769 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1770 if (target_type == TypeManager.enum_type ||
1771 target_type == TypeManager.object_type) {
1772 if (expr is EnumConstant)
1773 expr = ((EnumConstant) expr).Child;
1774 // We really need all these casts here .... :-(
1775 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1776 return new EmptyCast (expr, target_type);
1777 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1778 target_type.IsSubclassOf (TypeManager.enum_type))
1779 return new UnboxCast (expr, target_type);
1782 // Notice that we have kept the expr_type unmodified, which is only
1784 if (expr is EnumConstant)
1785 expr = ((EnumConstant) expr).Child;
1787 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1788 expr_type = expr.Type;
1791 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1796 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1801 // Unboxing conversion.
1803 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1804 return new UnboxCast (expr, target_type);
1807 // Skip the ExplicitReferenceConversion because we can not convert
1808 // from Null to a ValueType, and ExplicitReference wont check against
1809 // null literal explicitly
1811 if (expr_type != TypeManager.null_type){
1812 ne = ExplicitReferenceConversion (expr, target_type);
1818 if (target_type.IsPointer){
1819 if (expr_type.IsPointer)
1820 return new EmptyCast (expr, target_type);
1822 if (expr_type == TypeManager.sbyte_type ||
1823 expr_type == TypeManager.short_type ||
1824 expr_type == TypeManager.int32_type ||
1825 expr_type == TypeManager.int64_type)
1826 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1828 if (expr_type == TypeManager.ushort_type ||
1829 expr_type == TypeManager.uint32_type ||
1830 expr_type == TypeManager.uint64_type ||
1831 expr_type == TypeManager.byte_type)
1832 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1834 if (expr_type.IsPointer){
1835 Expression e = null;
1837 if (target_type == TypeManager.sbyte_type)
1838 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1839 else if (target_type == TypeManager.byte_type)
1840 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1841 else if (target_type == TypeManager.short_type)
1842 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1843 else if (target_type == TypeManager.ushort_type)
1844 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1845 else if (target_type == TypeManager.int32_type)
1846 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1847 else if (target_type == TypeManager.uint32_type)
1848 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1849 else if (target_type == TypeManager.uint64_type)
1850 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1851 else if (target_type == TypeManager.int64_type){
1852 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1858 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1863 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1867 // We should always be able to go from an uint32
1868 // implicitly or explicitly to the other integral
1871 throw new Exception ("Internal compiler error");
1876 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1880 if (expr is NullLiteral){
1881 Report.Error (37, loc, "Cannot convert null to `{0}' because it is a value type",
1882 TypeManager.CSharpName (target_type));
1886 Error_CannotConvertType (loc, original_expr_type, target_type);
1891 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1893 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1894 Type target_type, Location l)
1896 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1901 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1905 ne = ExplicitReferenceConversion (expr, target_type);
1909 Error_CannotConvertType (l, expr.Type, target_type);