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, target_type);
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, target_type);
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, target_type);
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 static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
181 if (target_type.IsValueType)
184 Type expr_type = expr.Type;
187 // This is the boxed case.
189 if (target_type == TypeManager.object_type) {
190 if (expr_type.IsClass || expr_type.IsValueType ||
191 expr_type.IsInterface || expr_type == TypeManager.enum_type)
192 if (target_type != TypeManager.anonymous_method_type)
196 } else if (expr_type.IsSubclassOf (target_type))
199 // Please remember that all code below actually comes
200 // from ImplicitReferenceConversion so make sure code remains in sync
202 // from any class-type S to any interface-type T.
203 if (target_type.IsInterface) {
204 if (target_type != TypeManager.iconvertible_type &&
205 expr_type.IsValueType && (expr is Constant) &&
206 !(expr is IntLiteral || expr is BoolLiteral ||
207 expr is FloatLiteral || expr is DoubleLiteral ||
208 expr is LongLiteral || expr is CharLiteral ||
209 expr is StringLiteral || expr is DecimalLiteral ||
210 expr is UIntLiteral || expr is ULongLiteral)) {
214 if (TypeManager.ImplementsInterface (expr_type, target_type))
218 // from any interface type S to interface-type T.
219 if (expr_type.IsInterface && target_type.IsInterface)
220 if (TypeManager.ImplementsInterface (expr_type, target_type))
223 // from an array-type S to an array-type of type T
224 if (expr_type.IsArray && target_type.IsArray) {
225 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
227 Type expr_element_type = expr_type.GetElementType ();
229 if (MyEmptyExpr == null)
230 MyEmptyExpr = new EmptyExpression ();
232 MyEmptyExpr.SetType (expr_element_type);
233 Type target_element_type = TypeManager.GetElementType (target_type);
235 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
236 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
237 target_element_type))
242 // from an array-type to System.Array
243 if (expr_type.IsArray && (target_type == TypeManager.array_type))
246 // from any delegate type to System.Delegate
247 if ((expr_type == TypeManager.delegate_type ||
248 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
249 target_type == TypeManager.delegate_type)
250 if (target_type.IsAssignableFrom (expr_type))
253 // from any array-type or delegate type into System.ICloneable.
254 if (expr_type.IsArray ||
255 expr_type == TypeManager.delegate_type ||
256 expr_type.IsSubclassOf (TypeManager.delegate_type))
257 if (target_type == TypeManager.icloneable_type)
260 // from the null type to any reference-type.
261 if (expr_type == TypeManager.null_type){
262 if (target_type.IsPointer)
265 if (!target_type.IsValueType)
272 /// Implicit Numeric Conversions.
274 /// expr is the expression to convert, returns a new expression of type
275 /// target_type or null if an implicit conversion is not possible.
277 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
278 Type target_type, Location loc)
280 Type expr_type = expr.Type;
283 // Attempt to do the implicit constant expression conversions
285 if (expr is Constant){
286 if (expr is IntConstant){
289 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
293 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
295 // Try the implicit constant expression conversion
296 // from long to ulong, instead of a nice routine,
299 long v = ((LongConstant) expr).Value;
301 return new ULongConstant ((ulong) v, expr.Location);
305 Type real_target_type = target_type;
307 if (expr_type == TypeManager.sbyte_type){
309 // From sbyte to short, int, long, float, double, decimal
311 if (real_target_type == TypeManager.int32_type)
312 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
313 if (real_target_type == TypeManager.int64_type)
314 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
315 if (real_target_type == TypeManager.double_type)
316 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
317 if (real_target_type == TypeManager.float_type)
318 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
319 if (real_target_type == TypeManager.short_type)
320 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
321 if (real_target_type == TypeManager.decimal_type)
322 return new CastToDecimal (ec, expr);
323 } else if (expr_type == TypeManager.byte_type){
325 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
327 if ((real_target_type == TypeManager.short_type) ||
328 (real_target_type == TypeManager.ushort_type) ||
329 (real_target_type == TypeManager.int32_type) ||
330 (real_target_type == TypeManager.uint32_type))
331 return new EmptyCast (expr, target_type);
333 if (real_target_type == TypeManager.uint64_type)
334 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
335 if (real_target_type == TypeManager.int64_type)
336 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
337 if (real_target_type == TypeManager.float_type)
338 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
339 if (real_target_type == TypeManager.double_type)
340 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
341 if (real_target_type == TypeManager.decimal_type)
342 return new CastToDecimal (ec, expr);
344 } else if (expr_type == TypeManager.short_type){
346 // From short to int, long, float, double, decimal
348 if (real_target_type == TypeManager.int32_type)
349 return new EmptyCast (expr, target_type);
350 if (real_target_type == TypeManager.int64_type)
351 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
352 if (real_target_type == TypeManager.double_type)
353 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
354 if (real_target_type == TypeManager.float_type)
355 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
356 if (real_target_type == TypeManager.decimal_type)
357 return new CastToDecimal (ec, expr);
359 } else if (expr_type == TypeManager.ushort_type){
361 // From ushort to int, uint, long, ulong, float, double, decimal
363 if (real_target_type == TypeManager.uint32_type)
364 return new EmptyCast (expr, target_type);
366 if (real_target_type == TypeManager.uint64_type)
367 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
368 if (real_target_type == TypeManager.int32_type)
369 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
370 if (real_target_type == TypeManager.int64_type)
371 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
372 if (real_target_type == TypeManager.double_type)
373 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
374 if (real_target_type == TypeManager.float_type)
375 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
376 if (real_target_type == TypeManager.decimal_type)
377 return new CastToDecimal (ec, expr);
378 } else if (expr_type == TypeManager.int32_type){
380 // From int to long, float, double, decimal
382 if (real_target_type == TypeManager.int64_type)
383 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
384 if (real_target_type == TypeManager.double_type)
385 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
386 if (real_target_type == TypeManager.float_type)
387 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
388 if (real_target_type == TypeManager.decimal_type)
389 return new CastToDecimal (ec, expr);
390 } else if (expr_type == TypeManager.uint32_type){
392 // From uint to long, ulong, float, double, decimal
394 if (real_target_type == TypeManager.int64_type)
395 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
396 if (real_target_type == TypeManager.uint64_type)
397 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
398 if (real_target_type == TypeManager.double_type)
399 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
401 if (real_target_type == TypeManager.float_type)
402 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
404 if (real_target_type == TypeManager.decimal_type)
405 return new CastToDecimal (ec, expr);
406 } else if (expr_type == TypeManager.int64_type){
408 // From long/ulong to float, double
410 if (real_target_type == TypeManager.double_type)
411 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
412 if (real_target_type == TypeManager.float_type)
413 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
414 if (real_target_type == TypeManager.decimal_type)
415 return new CastToDecimal (ec, expr);
416 } else if (expr_type == TypeManager.uint64_type){
418 // From ulong to float, double
420 if (real_target_type == TypeManager.double_type)
421 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
423 if (real_target_type == TypeManager.float_type)
424 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
426 if (real_target_type == TypeManager.decimal_type)
427 return new CastToDecimal (ec, expr);
428 } else if (expr_type == TypeManager.char_type){
430 // From char to ushort, int, uint, long, ulong, float, double, decimal
432 if ((real_target_type == TypeManager.ushort_type) ||
433 (real_target_type == TypeManager.int32_type) ||
434 (real_target_type == TypeManager.uint32_type))
435 return new EmptyCast (expr, target_type);
436 if (real_target_type == TypeManager.uint64_type)
437 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
438 if (real_target_type == TypeManager.int64_type)
439 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
440 if (real_target_type == TypeManager.float_type)
441 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
442 if (real_target_type == TypeManager.double_type)
443 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
444 if (real_target_type == TypeManager.decimal_type)
445 return new CastToDecimal (ec, expr);
446 } else if (expr_type == TypeManager.float_type){
450 if (real_target_type == TypeManager.double_type)
451 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
459 /// Same as ImplicitStandardConversionExists except that it also looks at
460 /// implicit user defined conversions - needed for overload resolution
462 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
464 if (ImplicitStandardConversionExists (ec, expr, target_type))
467 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
475 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
477 Expression dummy = ImplicitUserConversion (
478 ec, new EmptyExpression (source), target, Location.Null);
479 return dummy != null;
483 /// Determines if a standard implicit conversion exists from
484 /// expr_type to target_type
486 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
488 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
490 Type expr_type = expr.Type;
492 if (expr_type == TypeManager.void_type)
495 if (expr_type == target_type)
499 // First numeric conversions
501 if (expr_type == TypeManager.sbyte_type){
503 // From sbyte to short, int, long, float, double, decimal
505 if ((target_type == TypeManager.int32_type) ||
506 (target_type == TypeManager.int64_type) ||
507 (target_type == TypeManager.double_type) ||
508 (target_type == TypeManager.float_type) ||
509 (target_type == TypeManager.short_type) ||
510 (target_type == TypeManager.decimal_type))
513 } else if (expr_type == TypeManager.byte_type){
515 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
517 if ((target_type == TypeManager.short_type) ||
518 (target_type == TypeManager.ushort_type) ||
519 (target_type == TypeManager.int32_type) ||
520 (target_type == TypeManager.uint32_type) ||
521 (target_type == TypeManager.uint64_type) ||
522 (target_type == TypeManager.int64_type) ||
523 (target_type == TypeManager.float_type) ||
524 (target_type == TypeManager.double_type) ||
525 (target_type == TypeManager.decimal_type))
528 } else if (expr_type == TypeManager.short_type){
530 // From short to int, long, double, float, decimal
532 if ((target_type == TypeManager.int32_type) ||
533 (target_type == TypeManager.int64_type) ||
534 (target_type == TypeManager.double_type) ||
535 (target_type == TypeManager.float_type) ||
536 (target_type == TypeManager.decimal_type))
539 } else if (expr_type == TypeManager.ushort_type){
541 // From ushort to int, uint, long, ulong, double, float, decimal
543 if ((target_type == TypeManager.uint32_type) ||
544 (target_type == TypeManager.uint64_type) ||
545 (target_type == TypeManager.int32_type) ||
546 (target_type == TypeManager.int64_type) ||
547 (target_type == TypeManager.double_type) ||
548 (target_type == TypeManager.float_type) ||
549 (target_type == TypeManager.decimal_type))
552 } else if (expr_type == TypeManager.int32_type){
554 // From int to long, double, float, decimal
556 if ((target_type == TypeManager.int64_type) ||
557 (target_type == TypeManager.double_type) ||
558 (target_type == TypeManager.float_type) ||
559 (target_type == TypeManager.decimal_type))
562 } else if (expr_type == TypeManager.uint32_type){
564 // From uint to long, ulong, double, float, decimal
566 if ((target_type == TypeManager.int64_type) ||
567 (target_type == TypeManager.uint64_type) ||
568 (target_type == TypeManager.double_type) ||
569 (target_type == TypeManager.float_type) ||
570 (target_type == TypeManager.decimal_type))
573 } else if ((expr_type == TypeManager.uint64_type) ||
574 (expr_type == TypeManager.int64_type)) {
576 // From long/ulong to double, float, decimal
578 if ((target_type == TypeManager.double_type) ||
579 (target_type == TypeManager.float_type) ||
580 (target_type == TypeManager.decimal_type))
583 } else if (expr_type == TypeManager.char_type){
585 // From char to ushort, int, uint, ulong, long, float, double, decimal
587 if ((target_type == TypeManager.ushort_type) ||
588 (target_type == TypeManager.int32_type) ||
589 (target_type == TypeManager.uint32_type) ||
590 (target_type == TypeManager.uint64_type) ||
591 (target_type == TypeManager.int64_type) ||
592 (target_type == TypeManager.float_type) ||
593 (target_type == TypeManager.double_type) ||
594 (target_type == TypeManager.decimal_type))
597 } else if (expr_type == TypeManager.float_type){
601 if (target_type == TypeManager.double_type)
605 if (expr.eclass == ExprClass.MethodGroup){
606 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
607 MethodGroupExpr mg = expr as MethodGroupExpr;
610 // This should not happen frequently, so we can create an object
611 // to test compatibility
613 Expression c = ImplicitDelegateCreation.Create (
614 ec, mg, target_type, true, Location.Null);
620 if (ImplicitReferenceConversionExists (expr, target_type))
624 // Implicit Constant Expression Conversions
626 if (expr is IntConstant){
627 int value = ((IntConstant) expr).Value;
629 if (target_type == TypeManager.sbyte_type){
630 if (value >= SByte.MinValue && value <= SByte.MaxValue)
632 } else if (target_type == TypeManager.byte_type){
633 if (value >= 0 && value <= Byte.MaxValue)
635 } else if (target_type == TypeManager.short_type){
636 if (value >= Int16.MinValue && value <= Int16.MaxValue)
638 } else if (target_type == TypeManager.ushort_type){
639 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
641 } else if (target_type == TypeManager.uint32_type){
644 } else if (target_type == TypeManager.uint64_type){
646 // we can optimize this case: a positive int32
647 // always fits on a uint64. But we need an opcode
654 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
658 if (expr is LongConstant && target_type == TypeManager.uint64_type){
660 // Try the implicit constant expression conversion
661 // from long to ulong, instead of a nice routine,
664 long v = ((LongConstant) expr).Value;
669 if ((target_type == TypeManager.enum_type ||
670 target_type.IsSubclassOf (TypeManager.enum_type)) &&
672 IntLiteral i = (IntLiteral) expr;
679 // If `expr_type' implements `target_type' (which is an iface)
680 // see TryImplicitIntConversion
682 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
685 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
688 if (expr_type == TypeManager.anonymous_method_type){
689 if (!TypeManager.IsDelegateType (target_type))
692 AnonymousMethod am = (AnonymousMethod) expr;
694 Expression conv = am.Compatible (ec, target_type, true);
703 /// Finds "most encompassed type" according to the spec (13.4.2)
704 /// amongst the methods in the MethodGroupExpr
706 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
710 if (types.Count == 0)
713 if (types.Count == 1)
714 return (Type) types [0];
716 EmptyExpression expr = EmptyExpression.Grab ();
718 foreach (Type t in types) {
725 if (ImplicitStandardConversionExists (ec, expr, best))
730 foreach (Type t in types) {
733 if (!ImplicitStandardConversionExists (ec, expr, t)) {
739 EmptyExpression.Release (expr);
745 /// Finds "most encompassing type" according to the spec (13.4.2)
746 /// amongst the types in the given set
748 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
752 if (types.Count == 0)
755 if (types.Count == 1)
756 return (Type) types [0];
758 EmptyExpression expr = EmptyExpression.Grab ();
760 foreach (Type t in types) {
767 if (ImplicitStandardConversionExists (ec, expr, t))
771 foreach (Type t in types) {
775 if (!ImplicitStandardConversionExists (ec, expr, best)) {
781 EmptyExpression.Release (expr);
787 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
788 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
789 /// for explicit and implicit conversion operators.
791 static public Type FindMostSpecificSource (EmitContext ec, IList list,
792 Expression source, bool apply_explicit_conv_rules,
795 ArrayList src_types_set = new ArrayList ();
798 // If any operator converts from S then Sx = S
800 Type source_type = source.Type;
801 foreach (MethodBase mb in list){
802 ParameterData pd = TypeManager.GetParameterData (mb);
803 Type param_type = pd.ParameterType (0);
805 if (param_type == source_type)
808 src_types_set.Add (param_type);
812 // Explicit Conv rules
814 if (apply_explicit_conv_rules) {
815 ArrayList candidate_set = new ArrayList ();
817 foreach (Type param_type in src_types_set){
818 if (ImplicitStandardConversionExists (ec, source, param_type))
819 candidate_set.Add (param_type);
822 if (candidate_set.Count != 0)
823 return FindMostEncompassedType (ec, candidate_set);
829 if (apply_explicit_conv_rules)
830 return FindMostEncompassingType (ec, src_types_set);
832 return FindMostEncompassedType (ec, src_types_set);
836 /// Finds the most specific target Tx according to section 13.4.4
838 static public Type FindMostSpecificTarget (EmitContext ec, IList list,
839 Type target, bool apply_explicit_conv_rules,
842 ArrayList tgt_types_set = new ArrayList ();
845 // If any operator converts to T then Tx = T
847 foreach (MethodInfo mi in list){
848 Type ret_type = mi.ReturnType;
849 if (ret_type == target)
852 tgt_types_set.Add (ret_type);
856 // Explicit conv rules
858 if (apply_explicit_conv_rules) {
859 ArrayList candidate_set = new ArrayList ();
861 EmptyExpression expr = EmptyExpression.Grab ();
863 foreach (Type ret_type in tgt_types_set){
864 expr.SetType (ret_type);
866 if (ImplicitStandardConversionExists (ec, expr, target))
867 candidate_set.Add (ret_type);
870 EmptyExpression.Release (expr);
872 if (candidate_set.Count != 0)
873 return FindMostEncompassingType (ec, candidate_set);
877 // Okay, final case !
879 if (apply_explicit_conv_rules)
880 return FindMostEncompassedType (ec, tgt_types_set);
882 return FindMostEncompassingType (ec, tgt_types_set);
886 /// User-defined Implicit conversions
888 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
889 Type target, Location loc)
891 return UserDefinedConversion (ec, source, target, loc, false);
895 /// User-defined Explicit conversions
897 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
898 Type target, Location loc)
900 return UserDefinedConversion (ec, source, target, loc, true);
903 static void AddConversionOperators (EmitContext ec, ArrayList list,
904 Expression source, Type target_type,
905 bool look_for_explicit,
911 Type source_type = source.Type;
912 EmptyExpression expr = EmptyExpression.Grab ();
913 foreach (MethodInfo m in mg.Methods) {
914 ParameterData pd = TypeManager.GetParameterData (m);
915 Type return_type = m.ReturnType;
916 Type arg_type = pd.ParameterType (0);
918 if (source_type != arg_type) {
919 if (!ImplicitStandardConversionExists (ec, source, arg_type)) {
920 if (!look_for_explicit)
922 expr.SetType (arg_type);
923 if (!ImplicitStandardConversionExists (ec, expr, source_type))
928 if (target_type != return_type) {
929 expr.SetType (return_type);
930 if (!ImplicitStandardConversionExists (ec, expr, target_type)) {
931 if (!look_for_explicit)
933 expr.SetType (target_type);
934 if (!ImplicitStandardConversionExists (ec, expr, return_type))
942 EmptyExpression.Release (expr);
946 /// Computes the list of the user-defined conversion
947 /// operators from source_type to target_type. `look_for_explicit'
948 /// controls whether we should also include the list of explicit
951 static IList GetConversionOperators (EmitContext ec,
952 Expression source, Type target_type,
953 Location loc, bool look_for_explicit)
955 ArrayList ret = new ArrayList (4);
957 Type source_type = source.Type;
959 if (source_type != TypeManager.decimal_type) {
960 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
961 Expression.MethodLookup (
962 ec, source_type, "op_Implicit", loc) as MethodGroupExpr);
963 if (look_for_explicit) {
964 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
965 Expression.MethodLookup (
966 ec, source_type, "op_Explicit", loc) as MethodGroupExpr);
970 if (target_type != TypeManager.decimal_type) {
971 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
972 Expression.MethodLookup (
973 ec, target_type, "op_Implicit", loc) as MethodGroupExpr);
974 if (look_for_explicit) {
975 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
976 Expression.MethodLookup (
977 ec, target_type, "op_Explicit", loc) as MethodGroupExpr);
984 static DoubleHash explicit_conv = new DoubleHash (100);
985 static DoubleHash implicit_conv = new DoubleHash (100);
988 /// User-defined conversions
990 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
991 Type target, Location loc,
992 bool look_for_explicit)
994 Type source_type = source.Type;
995 MethodInfo method = null;
996 Type most_specific_source = null;
997 Type most_specific_target = null;
1000 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1002 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1003 method = (MethodInfo) o;
1004 if (method != null) {
1005 ParameterData pd = TypeManager.GetParameterData (method);
1006 most_specific_source = pd.ParameterType (0);
1007 most_specific_target = method.ReturnType;
1010 IList ops = GetConversionOperators (ec, source, target, loc, look_for_explicit);
1011 if (ops == null || ops.Count == 0) {
1016 most_specific_source = FindMostSpecificSource (ec, ops, source, look_for_explicit, loc);
1017 if (most_specific_source == null) {
1022 most_specific_target = FindMostSpecificTarget (ec, ops, target, look_for_explicit, loc);
1023 if (most_specific_target == null) {
1030 foreach (MethodInfo m in ops) {
1031 ParameterData pd = TypeManager.GetParameterData (m);
1033 if (pd.ParameterType (0) == most_specific_source &&
1034 m.ReturnType == most_specific_target) {
1043 if (!(source is Constant))
1044 hash.Insert (source_type, target, method);
1051 // This will do the conversion to the best match that we
1052 // found. Now we need to perform an implict standard conversion
1053 // if the best match was not the type that we were requested
1056 if (look_for_explicit)
1057 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1059 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1065 e = new UserCast (method, source, loc);
1066 if (e.Type != target){
1067 if (!look_for_explicit)
1068 e = ImplicitConversionStandard (ec, e, target, loc);
1070 e = ExplicitConversionStandard (ec, e, target, loc);
1077 /// Converts implicitly the resolved expression `expr' into the
1078 /// `target_type'. It returns a new expression that can be used
1079 /// in a context that expects a `target_type'.
1081 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1082 Type target_type, Location loc)
1086 if (target_type == null)
1087 throw new Exception ("Target type is null");
1089 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1093 e = ImplicitUserConversion (ec, expr, target_type, loc);
1102 /// Attempts to apply the `Standard Implicit
1103 /// Conversion' rules to the expression `expr' into
1104 /// the `target_type'. It returns a new expression
1105 /// that can be used in a context that expects a
1108 /// This is different from `ImplicitConversion' in that the
1109 /// user defined implicit conversions are excluded.
1111 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1112 Type target_type, Location loc)
1114 Type expr_type = expr.Type;
1117 if (expr.eclass == ExprClass.MethodGroup){
1118 if (!TypeManager.IsDelegateType (target_type)){
1123 // Only allow anonymous method conversions on post ISO_1
1125 if (RootContext.Version != LanguageVersion.ISO_1){
1126 MethodGroupExpr mg = expr as MethodGroupExpr;
1128 return ImplicitDelegateCreation.Create (
1129 ec, mg, target_type, false, loc);
1133 if (expr_type == target_type && expr_type != TypeManager.null_type)
1136 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1140 e = ImplicitReferenceConversion (expr, target_type);
1144 if ((target_type == TypeManager.enum_type ||
1145 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1146 expr is IntLiteral){
1147 IntLiteral i = (IntLiteral) expr;
1150 return new EnumConstant ((Constant) expr, target_type);
1154 if (expr_type.IsPointer){
1155 if (target_type == TypeManager.void_ptr_type)
1156 return new EmptyCast (expr, target_type);
1159 // yep, comparing pointer types cant be done with
1160 // t1 == t2, we have to compare their element types.
1162 if (target_type.IsPointer){
1163 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1168 if (target_type.IsPointer) {
1169 if (expr_type == TypeManager.null_type)
1170 return new EmptyCast (NullPointer.Null, target_type);
1172 if (expr_type == TypeManager.void_ptr_type)
1173 return new EmptyCast (expr, target_type);
1177 if (expr_type == TypeManager.anonymous_method_type){
1178 if (!TypeManager.IsDelegateType (target_type)){
1179 Report.Error (1660, loc,
1180 "Cannot convert anonymous method block to type `{0}' because it is not a delegate type",
1181 TypeManager.CSharpName (target_type));
1185 AnonymousMethod am = (AnonymousMethod) expr;
1186 int errors = Report.Errors;
1188 Expression conv = am.Compatible (ec, target_type, false);
1193 // We return something instead of null, to avoid
1194 // the duplicate error, since am.Compatible would have
1195 // reported that already
1197 if (errors != Report.Errors)
1198 return new EmptyCast (expr, target_type);
1205 /// Attempts to perform an implicit constant conversion of the IntConstant
1206 /// into a different data type using casts (See Implicit Constant
1207 /// Expression Conversions)
1209 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1211 int value = ic.Value;
1213 if (target_type == TypeManager.sbyte_type){
1214 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1215 return new SByteConstant ((sbyte) value, ic.Location);
1216 } else if (target_type == TypeManager.byte_type){
1217 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1218 return new ByteConstant ((byte) value, ic.Location);
1219 } else if (target_type == TypeManager.short_type){
1220 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1221 return new ShortConstant ((short) value, ic.Location);
1222 } else if (target_type == TypeManager.ushort_type){
1223 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1224 return new UShortConstant ((ushort) value, ic.Location);
1225 } else if (target_type == TypeManager.uint32_type){
1227 return new UIntConstant ((uint) value, ic.Location);
1228 } else if (target_type == TypeManager.uint64_type){
1230 // we can optimize this case: a positive int32
1231 // always fits on a uint64. But we need an opcode
1235 return new ULongConstant ((ulong) value, ic.Location);
1236 } else if (target_type == TypeManager.double_type)
1237 return new DoubleConstant ((double) value, ic.Location);
1238 else if (target_type == TypeManager.float_type)
1239 return new FloatConstant ((float) value, ic.Location);
1241 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1242 Type underlying = TypeManager.EnumToUnderlying (target_type);
1243 Constant e = (Constant) ic;
1246 // Possibly, we need to create a different 0 literal before passing
1249 if (underlying == TypeManager.int64_type)
1250 e = new LongLiteral (0, ic.Location);
1251 else if (underlying == TypeManager.uint64_type)
1252 e = new ULongLiteral (0, ic.Location);
1254 return new EnumConstant (e, target_type);
1258 // If `target_type' is an interface and the type of `ic' implements the interface
1259 // e.g. target_type is IComparable, IConvertible, IFormattable
1261 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1262 return new BoxedCast (ic, target_type);
1267 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1269 if (source.Name == target.Name){
1270 Report.ExtraInformation (loc,
1272 "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
1273 source.Name, source.Assembly.FullName, target.Assembly.FullName));
1277 // TODO: Missing explicit numeric check
1278 //ExplicitNumericConversion
1280 if (ExplicitReferenceConversionExists (source, target)) {
1281 Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
1282 TypeManager.CSharpName (source), TypeManager.CSharpName (target));
1286 Report.Error (29, loc, "Cannot implicitly convert type {0} to `{1}'",
1287 source == TypeManager.anonymous_method_type ?
1288 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1289 TypeManager.CSharpName (target));
1293 /// Attempts to implicitly convert `source' into `target_type', using
1294 /// ImplicitConversion. If there is no implicit conversion, then
1295 /// an error is signaled
1297 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1298 Type target_type, Location loc)
1302 e = ImplicitConversion (ec, source, target_type, loc);
1306 if (source is DoubleLiteral) {
1307 if (target_type == TypeManager.float_type) {
1308 Error_664 (loc, "float", "f");
1311 if (target_type == TypeManager.decimal_type) {
1312 Error_664 (loc, "decimal", "m");
1317 source.Error_ValueCannotBeConverted (loc, target_type);
1321 static void Error_664 (Location loc, string type, string suffix) {
1322 Report.Error (664, loc,
1323 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
1328 /// Performs the explicit numeric conversions
1330 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1332 Type expr_type = expr.Type;
1335 // If we have an enumeration, extract the underlying type,
1336 // use this during the comparison, but wrap around the original
1339 Type real_target_type = target_type;
1341 if (TypeManager.IsEnumType (real_target_type))
1342 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1344 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1345 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1347 if (real_target_type != target_type)
1348 return new EmptyCast (ce, target_type);
1352 if (expr_type == TypeManager.sbyte_type){
1354 // From sbyte to byte, ushort, uint, ulong, char
1356 if (real_target_type == TypeManager.byte_type)
1357 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1358 if (real_target_type == TypeManager.ushort_type)
1359 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1360 if (real_target_type == TypeManager.uint32_type)
1361 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1362 if (real_target_type == TypeManager.uint64_type)
1363 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1364 if (real_target_type == TypeManager.char_type)
1365 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1366 } else if (expr_type == TypeManager.byte_type){
1368 // From byte to sbyte and char
1370 if (real_target_type == TypeManager.sbyte_type)
1371 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1372 if (real_target_type == TypeManager.char_type)
1373 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1374 } else if (expr_type == TypeManager.short_type){
1376 // From short to sbyte, byte, ushort, uint, ulong, char
1378 if (real_target_type == TypeManager.sbyte_type)
1379 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1380 if (real_target_type == TypeManager.byte_type)
1381 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1382 if (real_target_type == TypeManager.ushort_type)
1383 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1384 if (real_target_type == TypeManager.uint32_type)
1385 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1386 if (real_target_type == TypeManager.uint64_type)
1387 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1388 if (real_target_type == TypeManager.char_type)
1389 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1390 } else if (expr_type == TypeManager.ushort_type){
1392 // From ushort to sbyte, byte, short, char
1394 if (real_target_type == TypeManager.sbyte_type)
1395 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1396 if (real_target_type == TypeManager.byte_type)
1397 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1398 if (real_target_type == TypeManager.short_type)
1399 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1400 if (real_target_type == TypeManager.char_type)
1401 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1402 } else if (expr_type == TypeManager.int32_type){
1404 // From int to sbyte, byte, short, ushort, uint, ulong, char
1406 if (real_target_type == TypeManager.sbyte_type)
1407 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1408 if (real_target_type == TypeManager.byte_type)
1409 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1410 if (real_target_type == TypeManager.short_type)
1411 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1412 if (real_target_type == TypeManager.ushort_type)
1413 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1414 if (real_target_type == TypeManager.uint32_type)
1415 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1416 if (real_target_type == TypeManager.uint64_type)
1417 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1418 if (real_target_type == TypeManager.char_type)
1419 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1420 } else if (expr_type == TypeManager.uint32_type){
1422 // From uint to sbyte, byte, short, ushort, int, char
1424 if (real_target_type == TypeManager.sbyte_type)
1425 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1426 if (real_target_type == TypeManager.byte_type)
1427 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1428 if (real_target_type == TypeManager.short_type)
1429 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1430 if (real_target_type == TypeManager.ushort_type)
1431 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1432 if (real_target_type == TypeManager.int32_type)
1433 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1434 if (real_target_type == TypeManager.char_type)
1435 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1436 } else if (expr_type == TypeManager.int64_type){
1438 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1440 if (real_target_type == TypeManager.sbyte_type)
1441 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1442 if (real_target_type == TypeManager.byte_type)
1443 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1444 if (real_target_type == TypeManager.short_type)
1445 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1446 if (real_target_type == TypeManager.ushort_type)
1447 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1448 if (real_target_type == TypeManager.int32_type)
1449 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1450 if (real_target_type == TypeManager.uint32_type)
1451 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1452 if (real_target_type == TypeManager.uint64_type)
1453 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1454 if (real_target_type == TypeManager.char_type)
1455 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1456 } else if (expr_type == TypeManager.uint64_type){
1458 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1460 if (real_target_type == TypeManager.sbyte_type)
1461 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1462 if (real_target_type == TypeManager.byte_type)
1463 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1464 if (real_target_type == TypeManager.short_type)
1465 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1466 if (real_target_type == TypeManager.ushort_type)
1467 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1468 if (real_target_type == TypeManager.int32_type)
1469 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1470 if (real_target_type == TypeManager.uint32_type)
1471 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1472 if (real_target_type == TypeManager.int64_type)
1473 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1474 if (real_target_type == TypeManager.char_type)
1475 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1476 } else if (expr_type == TypeManager.char_type){
1478 // From char to sbyte, byte, short
1480 if (real_target_type == TypeManager.sbyte_type)
1481 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1482 if (real_target_type == TypeManager.byte_type)
1483 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1484 if (real_target_type == TypeManager.short_type)
1485 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1486 } else if (expr_type == TypeManager.float_type){
1488 // From float to sbyte, byte, short,
1489 // ushort, int, uint, long, ulong, char
1492 if (real_target_type == TypeManager.sbyte_type)
1493 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1494 if (real_target_type == TypeManager.byte_type)
1495 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1496 if (real_target_type == TypeManager.short_type)
1497 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1498 if (real_target_type == TypeManager.ushort_type)
1499 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1500 if (real_target_type == TypeManager.int32_type)
1501 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1502 if (real_target_type == TypeManager.uint32_type)
1503 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1504 if (real_target_type == TypeManager.int64_type)
1505 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1506 if (real_target_type == TypeManager.uint64_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1508 if (real_target_type == TypeManager.char_type)
1509 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1510 if (real_target_type == TypeManager.decimal_type)
1511 return new CastToDecimal (ec, expr, true);
1512 } else if (expr_type == TypeManager.double_type){
1514 // From double to sbyte, byte, short,
1515 // ushort, int, uint, long, ulong,
1516 // char, float or decimal
1518 if (real_target_type == TypeManager.sbyte_type)
1519 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1520 if (real_target_type == TypeManager.byte_type)
1521 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1522 if (real_target_type == TypeManager.short_type)
1523 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1524 if (real_target_type == TypeManager.ushort_type)
1525 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1526 if (real_target_type == TypeManager.int32_type)
1527 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1528 if (real_target_type == TypeManager.uint32_type)
1529 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1530 if (real_target_type == TypeManager.int64_type)
1531 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1532 if (real_target_type == TypeManager.uint64_type)
1533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1534 if (real_target_type == TypeManager.char_type)
1535 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1536 if (real_target_type == TypeManager.float_type)
1537 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1538 if (real_target_type == TypeManager.decimal_type)
1539 return new CastToDecimal (ec, expr, true);
1540 } else if (expr_type == TypeManager.decimal_type) {
1542 // From decimal to sbyte, byte, short, ushort, int, uint,
1543 // long, ulong, char, double or float
1545 if (real_target_type == TypeManager.sbyte_type ||
1546 real_target_type == TypeManager.byte_type ||
1547 real_target_type == TypeManager.short_type ||
1548 real_target_type == TypeManager.ushort_type ||
1549 real_target_type == TypeManager.int32_type ||
1550 real_target_type == TypeManager.uint32_type ||
1551 real_target_type == TypeManager.int64_type ||
1552 real_target_type == TypeManager.uint64_type ||
1553 real_target_type == TypeManager.char_type ||
1554 real_target_type == TypeManager.double_type ||
1555 real_target_type == TypeManager.float_type)
1556 return new CastFromDecimal (ec, expr, target_type);
1562 /// Returns whether an explicit reference conversion can be performed
1563 /// from source_type to target_type
1565 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1567 bool target_is_value_type = target_type.IsValueType;
1569 if (source_type == target_type)
1573 // From object to any reference type
1575 if (source_type == TypeManager.object_type && !target_is_value_type)
1579 // From any class S to any class-type T, provided S is a base class of T
1581 if (target_type.IsSubclassOf (source_type))
1585 // From any interface type S to any interface T provided S is not derived from T
1587 if (source_type.IsInterface && target_type.IsInterface){
1588 if (!target_type.IsSubclassOf (source_type))
1593 // From any class type S to any interface T, provided S is not sealed
1594 // and provided S does not implement T.
1596 if (target_type.IsInterface && !source_type.IsSealed &&
1597 !TypeManager.ImplementsInterface (source_type, target_type))
1601 // From any interface-type S to to any class type T, provided T is not
1602 // sealed, or provided T implements S.
1604 if (source_type.IsInterface &&
1605 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1609 // From an array type S with an element type Se to an array type T with an
1610 // element type Te provided all the following are true:
1611 // * S and T differe only in element type, in other words, S and T
1612 // have the same number of dimensions.
1613 // * Both Se and Te are reference types
1614 // * An explicit referenc conversions exist from Se to Te
1616 if (source_type.IsArray && target_type.IsArray) {
1617 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1619 Type source_element_type = TypeManager.GetElementType (source_type);
1620 Type target_element_type = TypeManager.GetElementType (target_type);
1622 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1623 if (ExplicitReferenceConversionExists (source_element_type,
1624 target_element_type))
1630 // From System.Array to any array-type
1631 if (source_type == TypeManager.array_type &&
1632 target_type.IsArray){
1637 // From System delegate to any delegate-type
1639 if (source_type == TypeManager.delegate_type &&
1640 target_type.IsSubclassOf (TypeManager.delegate_type))
1644 // From ICloneable to Array or Delegate types
1646 if (source_type == TypeManager.icloneable_type &&
1647 (target_type == TypeManager.array_type ||
1648 target_type == TypeManager.delegate_type))
1655 /// Implements Explicit Reference conversions
1657 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1659 Type source_type = source.Type;
1660 bool target_is_value_type = target_type.IsValueType;
1663 // From object to any reference type
1665 if (source_type == TypeManager.object_type && !target_is_value_type)
1666 return new ClassCast (source, target_type);
1669 // Unboxing conversion.
1671 if (((source_type == TypeManager.enum_type &&
1672 !(source is EmptyCast)) ||
1673 source_type == TypeManager.value_type) && target_is_value_type)
1674 return new UnboxCast (source, target_type);
1677 // From any class S to any class-type T, provided S is a base class of T
1679 if (target_type.IsSubclassOf (source_type))
1680 return new ClassCast (source, target_type);
1683 // From any interface type S to any interface T provided S is not derived from T
1685 if (source_type.IsInterface && target_type.IsInterface){
1686 if (TypeManager.ImplementsInterface (source_type, target_type))
1689 return new ClassCast (source, target_type);
1693 // From any class type S to any interface T, provides S is not sealed
1694 // and provided S does not implement T.
1696 if (target_type.IsInterface && !source_type.IsSealed) {
1697 if (TypeManager.ImplementsInterface (source_type, target_type))
1700 return new ClassCast (source, target_type);
1705 // From any interface-type S to to any class type T, provided T is not
1706 // sealed, or provided T implements S.
1708 if (source_type.IsInterface) {
1709 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1710 if (target_type.IsClass)
1711 return new ClassCast (source, target_type);
1713 return new UnboxCast (source, target_type);
1719 // From an array type S with an element type Se to an array type T with an
1720 // element type Te provided all the following are true:
1721 // * S and T differe only in element type, in other words, S and T
1722 // have the same number of dimensions.
1723 // * Both Se and Te are reference types
1724 // * An explicit referenc conversions exist from Se to Te
1726 if (source_type.IsArray && target_type.IsArray) {
1727 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1729 Type source_element_type = TypeManager.GetElementType (source_type);
1730 Type target_element_type = TypeManager.GetElementType (target_type);
1732 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1733 if (ExplicitReferenceConversionExists (source_element_type,
1734 target_element_type))
1735 return new ClassCast (source, target_type);
1740 // From System.Array to any array-type
1741 if (source_type == TypeManager.array_type &&
1742 target_type.IsArray) {
1743 return new ClassCast (source, target_type);
1747 // From System delegate to any delegate-type
1749 if (source_type == TypeManager.delegate_type &&
1750 target_type.IsSubclassOf (TypeManager.delegate_type))
1751 return new ClassCast (source, target_type);
1754 // From ICloneable to Array or Delegate types
1756 if (source_type == TypeManager.icloneable_type &&
1757 (target_type == TypeManager.array_type ||
1758 target_type == TypeManager.delegate_type))
1759 return new ClassCast (source, target_type);
1765 /// Performs an explicit conversion of the expression `expr' whose
1766 /// type is expr.Type to `target_type'.
1768 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1769 Type target_type, Location loc)
1771 Type expr_type = expr.Type;
1772 Type original_expr_type = expr_type;
1774 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1779 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1780 if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1781 target_type.IsSubclassOf (TypeManager.enum_type))
1782 return new UnboxCast (expr, target_type);
1785 // Notice that we have kept the expr_type unmodified, which is only
1787 if (expr is EnumConstant)
1788 expr = ((EnumConstant) expr).Child;
1790 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1791 expr_type = expr.Type;
1794 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1799 // Unboxing conversion.
1801 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1802 return new UnboxCast (expr, target_type);
1805 // Skip the ExplicitReferenceConversion because we can not convert
1806 // from Null to a ValueType, and ExplicitReference wont check against
1807 // null literal explicitly
1809 if (expr_type != TypeManager.null_type){
1810 ne = ExplicitReferenceConversion (expr, target_type);
1816 if (target_type.IsPointer){
1817 if (expr_type.IsPointer)
1818 return new EmptyCast (expr, target_type);
1820 if (expr_type == TypeManager.sbyte_type ||
1821 expr_type == TypeManager.short_type ||
1822 expr_type == TypeManager.int32_type ||
1823 expr_type == TypeManager.int64_type)
1824 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1826 if (expr_type == TypeManager.ushort_type ||
1827 expr_type == TypeManager.uint32_type ||
1828 expr_type == TypeManager.uint64_type ||
1829 expr_type == TypeManager.byte_type)
1830 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1832 if (expr_type.IsPointer){
1833 Expression e = null;
1835 if (target_type == TypeManager.sbyte_type)
1836 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1837 else if (target_type == TypeManager.byte_type)
1838 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1839 else if (target_type == TypeManager.short_type)
1840 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1841 else if (target_type == TypeManager.ushort_type)
1842 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1843 else if (target_type == TypeManager.int32_type)
1844 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1845 else if (target_type == TypeManager.uint32_type)
1846 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1847 else if (target_type == TypeManager.uint64_type)
1848 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1849 else if (target_type == TypeManager.int64_type){
1850 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1856 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1861 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1865 // We should always be able to go from an uint32
1866 // implicitly or explicitly to the other integral
1869 throw new Exception ("Internal compiler error");
1874 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1878 if (expr is Constant)
1879 expr.Error_ValueCannotBeConverted (loc, target_type);
1881 Error_CannotConvertType (loc, original_expr_type, target_type);
1886 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1888 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1889 Type target_type, Location l)
1891 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1896 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1900 ne = ExplicitReferenceConversion (expr, target_type);
1904 Error_CannotConvertType (l, expr.Type, target_type);