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 EmptyExpression expr = EmptyExpression.Grab ();
709 foreach (Type t in types) {
716 if (ImplicitStandardConversionExists (ec, expr, best))
721 foreach (Type t in types) {
724 if (!ImplicitStandardConversionExists (ec, expr, t)) {
730 EmptyExpression.Release (expr);
736 /// Finds "most encompassing type" according to the spec (13.4.2)
737 /// amongst the types in the given set
739 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
743 EmptyExpression expr = EmptyExpression.Grab ();
745 foreach (Type t in types) {
752 if (ImplicitStandardConversionExists (ec, expr, t))
756 foreach (Type t in types) {
760 if (!ImplicitStandardConversionExists (ec, expr, best)) {
766 EmptyExpression.Release (expr);
772 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
773 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
774 /// for explicit and implicit conversion operators.
776 static public Type FindMostSpecificSource (EmitContext ec, IList list,
777 Expression source, bool apply_explicit_conv_rules,
780 ArrayList src_types_set = new ArrayList ();
783 // If any operator converts from S then Sx = S
785 Type source_type = source.Type;
786 foreach (MethodBase mb in list){
787 ParameterData pd = TypeManager.GetParameterData (mb);
788 Type param_type = pd.ParameterType (0);
790 if (param_type == source_type)
793 src_types_set.Add (param_type);
797 // Explicit Conv rules
799 if (apply_explicit_conv_rules) {
800 ArrayList candidate_set = new ArrayList ();
802 foreach (Type param_type in src_types_set){
803 if (ImplicitStandardConversionExists (ec, source, param_type))
804 candidate_set.Add (param_type);
807 if (candidate_set.Count != 0)
808 return FindMostEncompassedType (ec, candidate_set);
814 if (apply_explicit_conv_rules)
815 return FindMostEncompassingType (ec, src_types_set);
817 return FindMostEncompassedType (ec, src_types_set);
821 /// Finds the most specific target Tx according to section 13.4.4
823 static public Type FindMostSpecificTarget (EmitContext ec, IList list,
824 Type target, bool apply_explicit_conv_rules,
827 ArrayList tgt_types_set = new ArrayList ();
830 // If any operator converts to T then Tx = T
832 foreach (MethodInfo mi in list){
833 Type ret_type = mi.ReturnType;
834 if (ret_type == target)
837 tgt_types_set.Add (ret_type);
841 // Explicit conv rules
843 if (apply_explicit_conv_rules) {
844 ArrayList candidate_set = new ArrayList ();
846 EmptyExpression expr = EmptyExpression.Grab ();
848 foreach (Type ret_type in tgt_types_set){
849 expr.SetType (ret_type);
851 if (ImplicitStandardConversionExists (ec, expr, target))
852 candidate_set.Add (ret_type);
855 EmptyExpression.Release (expr);
857 if (candidate_set.Count != 0)
858 return FindMostEncompassingType (ec, candidate_set);
862 // Okay, final case !
864 if (apply_explicit_conv_rules)
865 return FindMostEncompassedType (ec, tgt_types_set);
867 return FindMostEncompassingType (ec, tgt_types_set);
871 /// User-defined Implicit conversions
873 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
874 Type target, Location loc)
876 return UserDefinedConversion (ec, source, target, loc, false);
880 /// User-defined Explicit conversions
882 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
883 Type target, Location loc)
885 return UserDefinedConversion (ec, source, target, loc, true);
888 static void AddConversionOperators (EmitContext ec, ArrayList list,
889 Expression source, Type target_type,
890 bool look_for_explicit,
896 Type source_type = source.Type;
897 EmptyExpression expr = EmptyExpression.Grab ();
898 foreach (MethodInfo m in mg.Methods) {
899 ParameterData pd = TypeManager.GetParameterData (m);
900 Type return_type = m.ReturnType;
901 Type arg_type = pd.ParameterType (0);
903 if (source_type != arg_type) {
904 if (!ImplicitStandardConversionExists (ec, source, arg_type)) {
905 if (!look_for_explicit)
907 expr.SetType (arg_type);
908 if (!ImplicitStandardConversionExists (ec, expr, source_type))
913 if (target_type != return_type) {
914 expr.SetType (return_type);
915 if (!ImplicitStandardConversionExists (ec, expr, target_type)) {
916 if (!look_for_explicit)
918 expr.SetType (target_type);
919 if (!ImplicitStandardConversionExists (ec, expr, return_type))
927 EmptyExpression.Release (expr);
931 /// Computes the list of the user-defined conversion
932 /// operators from source_type to target_type. `look_for_explicit'
933 /// controls whether we should also include the list of explicit
936 static IList GetConversionOperators (EmitContext ec,
937 Expression source, Type target_type,
938 Location loc, bool look_for_explicit)
940 ArrayList ret = new ArrayList (4);
942 Type source_type = source.Type;
944 if (source_type != TypeManager.decimal_type) {
945 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
946 Expression.MethodLookup (
947 ec, source_type, "op_Implicit", loc) as MethodGroupExpr);
948 if (look_for_explicit) {
949 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
950 Expression.MethodLookup (
951 ec, source_type, "op_Explicit", loc) as MethodGroupExpr);
955 if (target_type != TypeManager.decimal_type) {
956 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
957 Expression.MethodLookup (
958 ec, target_type, "op_Implicit", loc) as MethodGroupExpr);
959 if (look_for_explicit) {
960 AddConversionOperators (ec, ret, source, target_type, look_for_explicit,
961 Expression.MethodLookup (
962 ec, target_type, "op_Explicit", loc) as MethodGroupExpr);
969 static DoubleHash explicit_conv = new DoubleHash (100);
970 static DoubleHash implicit_conv = new DoubleHash (100);
973 /// User-defined conversions
975 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
976 Type target, Location loc,
977 bool look_for_explicit)
979 Type source_type = source.Type;
980 MethodInfo method = null;
981 Type most_specific_source = null;
982 Type most_specific_target = null;
985 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
987 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
988 method = (MethodInfo) o;
989 if (method != null) {
990 ParameterData pd = TypeManager.GetParameterData (method);
991 most_specific_source = pd.ParameterType (0);
992 most_specific_target = method.ReturnType;
995 IList ops = GetConversionOperators (ec, source, target, loc, look_for_explicit);
996 if (ops == null || ops.Count == 0) {
1001 most_specific_source = FindMostSpecificSource (ec, ops, source, look_for_explicit, loc);
1002 if (most_specific_source == null) {
1007 most_specific_target = FindMostSpecificTarget (ec, ops, target, look_for_explicit, loc);
1008 if (most_specific_target == null) {
1015 foreach (MethodInfo m in ops) {
1016 ParameterData pd = TypeManager.GetParameterData (m);
1018 if (pd.ParameterType (0) == most_specific_source &&
1019 m.ReturnType == most_specific_target) {
1028 if (!(source is Constant))
1029 hash.Insert (source_type, target, method);
1036 // This will do the conversion to the best match that we
1037 // found. Now we need to perform an implict standard conversion
1038 // if the best match was not the type that we were requested
1041 if (look_for_explicit)
1042 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1044 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1050 e = new UserCast (method, source, loc);
1051 if (e.Type != target){
1052 if (!look_for_explicit)
1053 e = ImplicitConversionStandard (ec, e, target, loc);
1055 e = ExplicitConversionStandard (ec, e, target, loc);
1062 /// Converts implicitly the resolved expression `expr' into the
1063 /// `target_type'. It returns a new expression that can be used
1064 /// in a context that expects a `target_type'.
1066 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1067 Type target_type, Location loc)
1071 if (target_type == null)
1072 throw new Exception ("Target type is null");
1074 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1078 e = ImplicitUserConversion (ec, expr, target_type, loc);
1087 /// Attempts to apply the `Standard Implicit
1088 /// Conversion' rules to the expression `expr' into
1089 /// the `target_type'. It returns a new expression
1090 /// that can be used in a context that expects a
1093 /// This is different from `ImplicitConversion' in that the
1094 /// user defined implicit conversions are excluded.
1096 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1097 Type target_type, Location loc)
1099 Type expr_type = expr.Type;
1102 if (expr.eclass == ExprClass.MethodGroup){
1103 if (!TypeManager.IsDelegateType (target_type)){
1108 // Only allow anonymous method conversions on post ISO_1
1110 if (RootContext.Version != LanguageVersion.ISO_1){
1111 MethodGroupExpr mg = expr as MethodGroupExpr;
1113 return ImplicitDelegateCreation.Create (
1114 ec, mg, target_type, false, loc);
1118 if (expr_type == target_type && expr_type != TypeManager.null_type)
1121 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1125 e = ImplicitReferenceConversion (expr, target_type);
1129 if ((target_type == TypeManager.enum_type ||
1130 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1131 expr is IntLiteral){
1132 IntLiteral i = (IntLiteral) expr;
1135 return new EnumConstant ((Constant) expr, target_type);
1139 if (expr_type.IsPointer){
1140 if (target_type == TypeManager.void_ptr_type)
1141 return new EmptyCast (expr, target_type);
1144 // yep, comparing pointer types cant be done with
1145 // t1 == t2, we have to compare their element types.
1147 if (target_type.IsPointer){
1148 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1153 if (target_type.IsPointer) {
1154 if (expr_type == TypeManager.null_type)
1155 return new EmptyCast (NullPointer.Null, target_type);
1157 if (expr_type == TypeManager.void_ptr_type)
1158 return new EmptyCast (expr, target_type);
1162 if (expr_type == TypeManager.anonymous_method_type){
1163 if (!TypeManager.IsDelegateType (target_type)){
1164 Report.Error (1660, loc,
1165 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1166 TypeManager.CSharpName (target_type));
1170 AnonymousMethod am = (AnonymousMethod) expr;
1171 int errors = Report.Errors;
1173 Expression conv = am.Compatible (ec, target_type, false);
1178 // We return something instead of null, to avoid
1179 // the duplicate error, since am.Compatible would have
1180 // reported that already
1182 if (errors != Report.Errors)
1183 return new EmptyCast (expr, target_type);
1190 /// Attempts to perform an implicit constant conversion of the IntConstant
1191 /// into a different data type using casts (See Implicit Constant
1192 /// Expression Conversions)
1194 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1196 int value = ic.Value;
1198 if (target_type == TypeManager.sbyte_type){
1199 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1200 return new SByteConstant ((sbyte) value);
1201 } else if (target_type == TypeManager.byte_type){
1202 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1203 return new ByteConstant ((byte) value);
1204 } else if (target_type == TypeManager.short_type){
1205 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1206 return new ShortConstant ((short) value);
1207 } else if (target_type == TypeManager.ushort_type){
1208 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1209 return new UShortConstant ((ushort) value);
1210 } else if (target_type == TypeManager.uint32_type){
1212 return new UIntConstant ((uint) value);
1213 } else if (target_type == TypeManager.uint64_type){
1215 // we can optimize this case: a positive int32
1216 // always fits on a uint64. But we need an opcode
1220 return new ULongConstant ((ulong) value);
1221 } else if (target_type == TypeManager.double_type)
1222 return new DoubleConstant ((double) value);
1223 else if (target_type == TypeManager.float_type)
1224 return new FloatConstant ((float) value);
1226 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1227 Type underlying = TypeManager.EnumToUnderlying (target_type);
1228 Constant e = (Constant) ic;
1231 // Possibly, we need to create a different 0 literal before passing
1234 if (underlying == TypeManager.int64_type)
1235 e = new LongLiteral (0);
1236 else if (underlying == TypeManager.uint64_type)
1237 e = new ULongLiteral (0);
1239 return new EnumConstant (e, target_type);
1243 // If `target_type' is an interface and the type of `ic' implements the interface
1244 // e.g. target_type is IComparable, IConvertible, IFormattable
1246 if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1247 return new BoxedCast (ic);
1252 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1254 if (source.Name == target.Name){
1255 Report.ExtraInformation (loc,
1257 "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
1258 source.Name, source.Assembly.FullName, target.Assembly.FullName));
1261 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1262 source == TypeManager.anonymous_method_type ?
1263 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1264 TypeManager.CSharpName (target));
1268 /// Attempts to implicitly convert `source' into `target_type', using
1269 /// ImplicitConversion. If there is no implicit conversion, then
1270 /// an error is signaled
1272 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1273 Type target_type, Location loc)
1277 e = ImplicitConversion (ec, source, target_type, loc);
1281 if (source is DoubleLiteral) {
1282 if (target_type == TypeManager.float_type) {
1283 Error_664 (loc, "float", "f");
1286 if (target_type == TypeManager.decimal_type) {
1287 Error_664 (loc, "decimal", "m");
1292 if (source is Constant){
1293 Constant c = (Constant) source;
1295 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1299 Error_CannotImplicitConversion (loc, source.Type, target_type);
1304 static void Error_664 (Location loc, string type, string suffix) {
1305 Report.Error (664, loc,
1306 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1311 /// Performs the explicit numeric conversions
1313 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1315 Type expr_type = expr.Type;
1318 // If we have an enumeration, extract the underlying type,
1319 // use this during the comparison, but wrap around the original
1322 Type real_target_type = target_type;
1324 if (TypeManager.IsEnumType (real_target_type))
1325 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1327 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1328 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1330 if (real_target_type != target_type)
1331 return new EmptyCast (ce, target_type);
1335 if (expr_type == TypeManager.sbyte_type){
1337 // From sbyte to byte, ushort, uint, ulong, char
1339 if (real_target_type == TypeManager.byte_type)
1340 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1341 if (real_target_type == TypeManager.ushort_type)
1342 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1343 if (real_target_type == TypeManager.uint32_type)
1344 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1345 if (real_target_type == TypeManager.uint64_type)
1346 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1347 if (real_target_type == TypeManager.char_type)
1348 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1349 } else if (expr_type == TypeManager.byte_type){
1351 // From byte to sbyte and char
1353 if (real_target_type == TypeManager.sbyte_type)
1354 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1355 if (real_target_type == TypeManager.char_type)
1356 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1357 } else if (expr_type == TypeManager.short_type){
1359 // From short to sbyte, byte, ushort, uint, ulong, char
1361 if (real_target_type == TypeManager.sbyte_type)
1362 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1363 if (real_target_type == TypeManager.byte_type)
1364 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1365 if (real_target_type == TypeManager.ushort_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1367 if (real_target_type == TypeManager.uint32_type)
1368 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1369 if (real_target_type == TypeManager.uint64_type)
1370 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1371 if (real_target_type == TypeManager.char_type)
1372 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1373 } else if (expr_type == TypeManager.ushort_type){
1375 // From ushort to sbyte, byte, short, char
1377 if (real_target_type == TypeManager.sbyte_type)
1378 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1379 if (real_target_type == TypeManager.byte_type)
1380 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1381 if (real_target_type == TypeManager.short_type)
1382 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1383 if (real_target_type == TypeManager.char_type)
1384 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1385 } else if (expr_type == TypeManager.int32_type){
1387 // From int to sbyte, byte, short, ushort, uint, ulong, char
1389 if (real_target_type == TypeManager.sbyte_type)
1390 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1391 if (real_target_type == TypeManager.byte_type)
1392 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1393 if (real_target_type == TypeManager.short_type)
1394 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1395 if (real_target_type == TypeManager.ushort_type)
1396 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1397 if (real_target_type == TypeManager.uint32_type)
1398 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1399 if (real_target_type == TypeManager.uint64_type)
1400 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1401 if (real_target_type == TypeManager.char_type)
1402 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1403 } else if (expr_type == TypeManager.uint32_type){
1405 // From uint to sbyte, byte, short, ushort, int, char
1407 if (real_target_type == TypeManager.sbyte_type)
1408 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1409 if (real_target_type == TypeManager.byte_type)
1410 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1411 if (real_target_type == TypeManager.short_type)
1412 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1413 if (real_target_type == TypeManager.ushort_type)
1414 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1415 if (real_target_type == TypeManager.int32_type)
1416 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1417 if (real_target_type == TypeManager.char_type)
1418 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1419 } else if (expr_type == TypeManager.int64_type){
1421 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1423 if (real_target_type == TypeManager.sbyte_type)
1424 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1425 if (real_target_type == TypeManager.byte_type)
1426 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1427 if (real_target_type == TypeManager.short_type)
1428 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1429 if (real_target_type == TypeManager.ushort_type)
1430 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1431 if (real_target_type == TypeManager.int32_type)
1432 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1433 if (real_target_type == TypeManager.uint32_type)
1434 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1435 if (real_target_type == TypeManager.uint64_type)
1436 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1437 if (real_target_type == TypeManager.char_type)
1438 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1439 } else if (expr_type == TypeManager.uint64_type){
1441 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1443 if (real_target_type == TypeManager.sbyte_type)
1444 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1445 if (real_target_type == TypeManager.byte_type)
1446 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1447 if (real_target_type == TypeManager.short_type)
1448 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1449 if (real_target_type == TypeManager.ushort_type)
1450 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1451 if (real_target_type == TypeManager.int32_type)
1452 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1453 if (real_target_type == TypeManager.uint32_type)
1454 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1455 if (real_target_type == TypeManager.int64_type)
1456 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1457 if (real_target_type == TypeManager.char_type)
1458 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1459 } else if (expr_type == TypeManager.char_type){
1461 // From char to sbyte, byte, short
1463 if (real_target_type == TypeManager.sbyte_type)
1464 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1465 if (real_target_type == TypeManager.byte_type)
1466 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1467 if (real_target_type == TypeManager.short_type)
1468 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1469 } else if (expr_type == TypeManager.float_type){
1471 // From float to sbyte, byte, short,
1472 // ushort, int, uint, long, ulong, char
1475 if (real_target_type == TypeManager.sbyte_type)
1476 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1477 if (real_target_type == TypeManager.byte_type)
1478 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1479 if (real_target_type == TypeManager.short_type)
1480 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1481 if (real_target_type == TypeManager.ushort_type)
1482 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1483 if (real_target_type == TypeManager.int32_type)
1484 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1485 if (real_target_type == TypeManager.uint32_type)
1486 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1487 if (real_target_type == TypeManager.int64_type)
1488 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1489 if (real_target_type == TypeManager.uint64_type)
1490 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1491 if (real_target_type == TypeManager.char_type)
1492 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1493 if (real_target_type == TypeManager.decimal_type)
1494 return new CastToDecimal (ec, expr, true);
1495 } else if (expr_type == TypeManager.double_type){
1497 // From double to sbyte, byte, short,
1498 // ushort, int, uint, long, ulong,
1499 // char, float or decimal
1501 if (real_target_type == TypeManager.sbyte_type)
1502 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1503 if (real_target_type == TypeManager.byte_type)
1504 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1505 if (real_target_type == TypeManager.short_type)
1506 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1507 if (real_target_type == TypeManager.ushort_type)
1508 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1509 if (real_target_type == TypeManager.int32_type)
1510 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1511 if (real_target_type == TypeManager.uint32_type)
1512 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1513 if (real_target_type == TypeManager.int64_type)
1514 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1515 if (real_target_type == TypeManager.uint64_type)
1516 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1517 if (real_target_type == TypeManager.char_type)
1518 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1519 if (real_target_type == TypeManager.float_type)
1520 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1521 if (real_target_type == TypeManager.decimal_type)
1522 return new CastToDecimal (ec, expr, true);
1523 } else if (expr_type == TypeManager.decimal_type) {
1525 // From decimal to sbyte, byte, short, ushort, int, uint,
1526 // long, ulong, char, double or float
1528 if (real_target_type == TypeManager.sbyte_type ||
1529 real_target_type == TypeManager.byte_type ||
1530 real_target_type == TypeManager.short_type ||
1531 real_target_type == TypeManager.ushort_type ||
1532 real_target_type == TypeManager.int32_type ||
1533 real_target_type == TypeManager.uint32_type ||
1534 real_target_type == TypeManager.int64_type ||
1535 real_target_type == TypeManager.uint64_type ||
1536 real_target_type == TypeManager.char_type ||
1537 real_target_type == TypeManager.double_type ||
1538 real_target_type == TypeManager.float_type)
1539 return new CastFromDecimal (ec, expr, target_type);
1545 /// Returns whether an explicit reference conversion can be performed
1546 /// from source_type to target_type
1548 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1550 bool target_is_value_type = target_type.IsValueType;
1552 if (source_type == target_type)
1556 // From object to any reference type
1558 if (source_type == TypeManager.object_type && !target_is_value_type)
1562 // From any class S to any class-type T, provided S is a base class of T
1564 if (target_type.IsSubclassOf (source_type))
1568 // From any interface type S to any interface T provided S is not derived from T
1570 if (source_type.IsInterface && target_type.IsInterface){
1571 if (!target_type.IsSubclassOf (source_type))
1576 // From any class type S to any interface T, provided S is not sealed
1577 // and provided S does not implement T.
1579 if (target_type.IsInterface && !source_type.IsSealed &&
1580 !TypeManager.ImplementsInterface (source_type, target_type))
1584 // From any interface-type S to to any class type T, provided T is not
1585 // sealed, or provided T implements S.
1587 if (source_type.IsInterface &&
1588 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1592 // From an array type S with an element type Se to an array type T with an
1593 // element type Te provided all the following are true:
1594 // * S and T differe only in element type, in other words, S and T
1595 // have the same number of dimensions.
1596 // * Both Se and Te are reference types
1597 // * An explicit referenc conversions exist from Se to Te
1599 if (source_type.IsArray && target_type.IsArray) {
1600 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1602 Type source_element_type = TypeManager.GetElementType (source_type);
1603 Type target_element_type = TypeManager.GetElementType (target_type);
1605 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1606 if (ExplicitReferenceConversionExists (source_element_type,
1607 target_element_type))
1613 // From System.Array to any array-type
1614 if (source_type == TypeManager.array_type &&
1615 target_type.IsArray){
1620 // From System delegate to any delegate-type
1622 if (source_type == TypeManager.delegate_type &&
1623 target_type.IsSubclassOf (TypeManager.delegate_type))
1627 // From ICloneable to Array or Delegate types
1629 if (source_type == TypeManager.icloneable_type &&
1630 (target_type == TypeManager.array_type ||
1631 target_type == TypeManager.delegate_type))
1638 /// Implements Explicit Reference conversions
1640 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1642 Type source_type = source.Type;
1643 bool target_is_value_type = target_type.IsValueType;
1646 // From object to any reference type
1648 if (source_type == TypeManager.object_type && !target_is_value_type)
1649 return new ClassCast (source, target_type);
1652 // Unboxing conversion.
1654 if (((source_type == TypeManager.enum_type &&
1655 !(source is EmptyCast)) ||
1656 source_type == TypeManager.value_type) && target_is_value_type)
1657 return new UnboxCast (source, target_type);
1660 // From any class S to any class-type T, provided S is a base class of T
1662 if (target_type.IsSubclassOf (source_type))
1663 return new ClassCast (source, target_type);
1666 // From any interface type S to any interface T provided S is not derived from T
1668 if (source_type.IsInterface && target_type.IsInterface){
1669 if (TypeManager.ImplementsInterface (source_type, target_type))
1672 return new ClassCast (source, target_type);
1676 // From any class type S to any interface T, provides S is not sealed
1677 // and provided S does not implement T.
1679 if (target_type.IsInterface && !source_type.IsSealed) {
1680 if (TypeManager.ImplementsInterface (source_type, target_type))
1683 return new ClassCast (source, target_type);
1688 // From any interface-type S to to any class type T, provided T is not
1689 // sealed, or provided T implements S.
1691 if (source_type.IsInterface) {
1692 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1693 if (target_type.IsClass)
1694 return new ClassCast (source, target_type);
1696 return new UnboxCast (source, target_type);
1702 // From an array type S with an element type Se to an array type T with an
1703 // element type Te provided all the following are true:
1704 // * S and T differe only in element type, in other words, S and T
1705 // have the same number of dimensions.
1706 // * Both Se and Te are reference types
1707 // * An explicit referenc conversions exist from Se to Te
1709 if (source_type.IsArray && target_type.IsArray) {
1710 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1712 Type source_element_type = TypeManager.GetElementType (source_type);
1713 Type target_element_type = TypeManager.GetElementType (target_type);
1715 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1716 if (ExplicitReferenceConversionExists (source_element_type,
1717 target_element_type))
1718 return new ClassCast (source, target_type);
1723 // From System.Array to any array-type
1724 if (source_type == TypeManager.array_type &&
1725 target_type.IsArray) {
1726 return new ClassCast (source, target_type);
1730 // From System delegate to any delegate-type
1732 if (source_type == TypeManager.delegate_type &&
1733 target_type.IsSubclassOf (TypeManager.delegate_type))
1734 return new ClassCast (source, target_type);
1737 // From ICloneable to Array or Delegate types
1739 if (source_type == TypeManager.icloneable_type &&
1740 (target_type == TypeManager.array_type ||
1741 target_type == TypeManager.delegate_type))
1742 return new ClassCast (source, target_type);
1748 /// Performs an explicit conversion of the expression `expr' whose
1749 /// type is expr.Type to `target_type'.
1751 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1752 Type target_type, Location loc)
1754 Type expr_type = expr.Type;
1755 Type original_expr_type = expr_type;
1757 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1758 if (target_type == TypeManager.enum_type ||
1759 target_type == TypeManager.object_type) {
1760 if (expr is EnumConstant)
1761 expr = ((EnumConstant) expr).Child;
1762 // We really need all these casts here .... :-(
1763 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1764 return new EmptyCast (expr, target_type);
1765 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1766 target_type.IsSubclassOf (TypeManager.enum_type))
1767 return new UnboxCast (expr, target_type);
1770 // Notice that we have kept the expr_type unmodified, which is only
1772 if (expr is EnumConstant)
1773 expr = ((EnumConstant) expr).Child;
1775 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1776 expr_type = expr.Type;
1779 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1784 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1789 // Unboxing conversion.
1791 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1792 return new UnboxCast (expr, target_type);
1795 // Skip the ExplicitReferenceConversion because we can not convert
1796 // from Null to a ValueType, and ExplicitReference wont check against
1797 // null literal explicitly
1799 if (expr_type != TypeManager.null_type){
1800 ne = ExplicitReferenceConversion (expr, target_type);
1806 if (target_type.IsPointer){
1807 if (expr_type.IsPointer)
1808 return new EmptyCast (expr, target_type);
1810 if (expr_type == TypeManager.sbyte_type ||
1811 expr_type == TypeManager.short_type ||
1812 expr_type == TypeManager.int32_type ||
1813 expr_type == TypeManager.int64_type)
1814 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1816 if (expr_type == TypeManager.ushort_type ||
1817 expr_type == TypeManager.uint32_type ||
1818 expr_type == TypeManager.uint64_type ||
1819 expr_type == TypeManager.byte_type)
1820 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1822 if (expr_type.IsPointer){
1823 Expression e = null;
1825 if (target_type == TypeManager.sbyte_type)
1826 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1827 else if (target_type == TypeManager.byte_type)
1828 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1829 else if (target_type == TypeManager.short_type)
1830 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1831 else if (target_type == TypeManager.ushort_type)
1832 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1833 else if (target_type == TypeManager.int32_type)
1834 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1835 else if (target_type == TypeManager.uint32_type)
1836 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1837 else if (target_type == TypeManager.uint64_type)
1838 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1839 else if (target_type == TypeManager.int64_type){
1840 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1846 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1851 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1855 // We should always be able to go from an uint32
1856 // implicitly or explicitly to the other integral
1859 throw new Exception ("Internal compiler error");
1864 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1868 if (expr is NullLiteral){
1869 Report.Error (37, loc, "Cannot convert null to value type `" +
1870 TypeManager.CSharpName (target_type) + "'");
1874 Error_CannotConvertType (loc, original_expr_type, target_type);
1879 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1881 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1882 Type target_type, Location l)
1884 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1889 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1893 ne = ExplicitReferenceConversion (expr, target_type);
1897 Error_CannotConvertType (l, expr.Type, target_type);