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 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 Expression TypeParameter_to_Null (Expression expr, Type target_type,
42 if (!TypeParameter_to_Null (target_type)) {
43 Report.Error (403, loc, "Cannot convert null to the type " +
44 "parameter `{0}' becaues it could be a value " +
45 "type. Consider using `default ({0})' instead.",
50 return new NullCast (expr, target_type);
53 static bool TypeParameter_to_Null (Type target_type)
55 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
59 if (gc.HasReferenceTypeConstraint)
61 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
67 static Type TypeParam_EffectiveBaseType (EmitContext ec, Type t)
69 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
71 return TypeManager.object_type;
73 return TypeParam_EffectiveBaseType (ec, gc);
76 static Type TypeParam_EffectiveBaseType (EmitContext ec, GenericConstraints gc)
78 ArrayList list = new ArrayList ();
79 list.Add (gc.EffectiveBaseClass);
80 foreach (Type t in gc.InterfaceConstraints) {
81 if (!t.IsGenericParameter)
84 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
86 list.Add (TypeParam_EffectiveBaseType (ec, new_gc));
88 return FindMostEncompassedType (ec, list);
91 static Expression TypeParameterConversion (Expression expr, bool is_reference, Type target_type)
94 return new EmptyCast (expr, target_type);
96 return new BoxedCast (expr, target_type);
99 static Expression ImplicitTypeParameterConversion (EmitContext ec, Expression expr,
102 Type expr_type = expr.Type;
104 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
107 if (target_type == TypeManager.object_type)
108 return new BoxedCast (expr);
113 // We're converting from a type parameter which is known to be a reference type.
114 bool is_reference = gc.IsReferenceType;
115 Type base_type = TypeParam_EffectiveBaseType (ec, gc);
117 if (TypeManager.IsSubclassOf (base_type, target_type))
118 return TypeParameterConversion (expr, is_reference, target_type);
120 if (target_type.IsInterface) {
121 if (TypeManager.ImplementsInterface (base_type, target_type))
122 return TypeParameterConversion (expr, is_reference, target_type);
124 foreach (Type t in gc.InterfaceConstraints) {
125 if (TypeManager.IsSubclassOf (t, target_type))
126 return TypeParameterConversion (expr, is_reference, target_type);
130 foreach (Type t in gc.InterfaceConstraints) {
131 if (!t.IsGenericParameter)
133 if (TypeManager.IsSubclassOf (t, target_type))
134 return TypeParameterConversion (expr, is_reference, target_type);
140 static EmptyExpression MyEmptyExpr;
141 static public Expression ImplicitReferenceConversion (EmitContext ec, Expression expr, Type target_type)
143 Type expr_type = expr.Type;
145 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
146 // if we are a method group, emit a warning
151 if (expr_type == TypeManager.void_type)
154 if (expr_type.IsGenericParameter)
155 return ImplicitTypeParameterConversion (ec, expr, target_type);
158 // notice that it is possible to write "ValueType v = 1", the ValueType here
159 // is an abstract class, and not really a value type, so we apply the same rules.
161 if (target_type == TypeManager.object_type) {
163 // A pointer type cannot be converted to object
165 if (expr_type.IsPointer)
168 if (TypeManager.IsValueType (expr_type))
169 return new BoxedCast (expr);
170 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
171 if (expr_type == TypeManager.anonymous_method_type)
173 return new EmptyCast (expr, target_type);
177 } else if (target_type == TypeManager.value_type) {
178 if (TypeManager.IsValueType (expr_type))
179 return new BoxedCast (expr);
180 if (expr_type == TypeManager.null_type)
181 return new NullCast (expr, target_type);
184 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
186 // Special case: enumeration to System.Enum.
187 // System.Enum is not a value type, it is a class, so we need
188 // a boxing conversion
190 if (expr_type.IsEnum || expr_type.IsGenericParameter)
191 return new BoxedCast (expr);
193 return new EmptyCast (expr, target_type);
196 // This code is kind of mirrored inside ImplicitStandardConversionExists
197 // with the small distinction that we only probe there
199 // Always ensure that the code here and there is in sync
201 // from the null type to any reference-type.
202 if (expr_type == TypeManager.null_type){
203 if (target_type.IsPointer)
204 return new EmptyCast (expr, target_type);
206 if (!target_type.IsValueType)
207 return new NullCast (expr, target_type);
210 // from any class-type S to any interface-type T.
211 if (target_type.IsInterface) {
212 if (target_type != TypeManager.iconvertible_type &&
213 expr_type.IsValueType && (expr is Constant) &&
214 !(expr is IntLiteral || expr is BoolLiteral ||
215 expr is FloatLiteral || expr is DoubleLiteral ||
216 expr is LongLiteral || expr is CharLiteral ||
217 expr is StringLiteral || expr is DecimalLiteral ||
218 expr is UIntLiteral || expr is ULongLiteral)) {
222 if (TypeManager.ImplementsInterface (expr_type, target_type)){
223 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
224 return new BoxedCast (expr, target_type);
226 return new EmptyCast (expr, target_type);
230 // from any interface type S to interface-type T.
231 if (expr_type.IsInterface && target_type.IsInterface) {
232 if (TypeManager.ImplementsInterface (expr_type, target_type))
233 return new EmptyCast (expr, target_type);
238 // from an array-type S to an array-type of type T
239 if (expr_type.IsArray && target_type.IsArray) {
240 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
242 Type expr_element_type = TypeManager.GetElementType (expr_type);
244 if (MyEmptyExpr == null)
245 MyEmptyExpr = new EmptyExpression ();
247 MyEmptyExpr.SetType (expr_element_type);
248 Type target_element_type = TypeManager.GetElementType (target_type);
250 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
251 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
252 target_element_type))
253 return new EmptyCast (expr, target_type);
257 // from an array-type to System.Array
258 if (expr_type.IsArray && target_type == TypeManager.array_type)
259 return new EmptyCast (expr, target_type);
261 // from any delegate type to System.Delegate
262 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
263 target_type == TypeManager.delegate_type)
264 return new EmptyCast (expr, target_type);
266 // from any array-type or delegate type into System.ICloneable.
267 if (expr_type.IsArray ||
268 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
269 if (target_type == TypeManager.icloneable_type)
270 return new EmptyCast (expr, target_type);
272 // from a generic type definition to a generic instance.
273 if (TypeManager.IsEqual (expr_type, target_type))
274 return new EmptyCast (expr, target_type);
280 // Tests whether an implicit reference conversion exists between expr_type
283 public static bool ImplicitReferenceConversionExists (EmitContext ec, Expression expr, Type target_type)
285 Type expr_type = expr.Type;
287 if (expr_type.IsGenericParameter)
288 return ImplicitTypeParameterConversion (ec, expr, target_type) != null;
291 // This is the boxed case.
293 if (target_type == TypeManager.object_type) {
294 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
295 expr_type.IsInterface || expr_type == TypeManager.enum_type)
296 if (target_type != TypeManager.anonymous_method_type)
300 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
303 // Please remember that all code below actually comes
304 // from ImplicitReferenceConversion so make sure code remains in sync
306 // from any class-type S to any interface-type T.
307 if (target_type.IsInterface) {
308 if (target_type != TypeManager.iconvertible_type &&
309 expr_type.IsValueType && (expr is Constant) &&
310 !(expr is IntLiteral || expr is BoolLiteral ||
311 expr is FloatLiteral || expr is DoubleLiteral ||
312 expr is LongLiteral || expr is CharLiteral ||
313 expr is StringLiteral || expr is DecimalLiteral ||
314 expr is UIntLiteral || expr is ULongLiteral)) {
318 if (TypeManager.ImplementsInterface (expr_type, target_type))
322 // from any interface type S to interface-type T.
323 if (expr_type.IsInterface && target_type.IsInterface)
324 if (TypeManager.ImplementsInterface (expr_type, target_type))
327 // from an array-type S to an array-type of type T
328 if (expr_type.IsArray && target_type.IsArray) {
329 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
331 Type expr_element_type = expr_type.GetElementType ();
333 if (MyEmptyExpr == null)
334 MyEmptyExpr = new EmptyExpression ();
336 MyEmptyExpr.SetType (expr_element_type);
337 Type target_element_type = TypeManager.GetElementType (target_type);
339 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
340 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
341 target_element_type))
346 // from an array-type to System.Array
347 if (expr_type.IsArray && (target_type == TypeManager.array_type))
350 // from any delegate type to System.Delegate
351 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
352 target_type == TypeManager.delegate_type)
353 if (target_type.IsAssignableFrom (expr_type))
356 // from any array-type or delegate type into System.ICloneable.
357 if (expr_type.IsArray ||
358 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
359 if (target_type == TypeManager.icloneable_type)
362 // from the null type to any reference-type.
363 if (expr_type == TypeManager.null_type){
364 if (target_type.IsPointer)
367 if (!target_type.IsValueType)
371 // from a generic type definition to a generic instance.
372 if (TypeManager.IsEqual (expr_type, target_type))
379 /// Implicit Numeric Conversions.
381 /// expr is the expression to convert, returns a new expression of type
382 /// target_type or null if an implicit conversion is not possible.
384 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
385 Type target_type, Location loc)
387 Type expr_type = expr.Type;
390 // Attempt to do the implicit constant expression conversions
392 if (expr is Constant){
393 if (expr is IntConstant){
396 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
400 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
402 // Try the implicit constant expression conversion
403 // from long to ulong, instead of a nice routine,
406 long v = ((LongConstant) expr).Value;
408 return new ULongConstant ((ulong) v);
412 Type real_target_type = target_type;
414 if (expr_type == TypeManager.sbyte_type){
416 // From sbyte to short, int, long, float, double.
418 if (real_target_type == TypeManager.int32_type)
419 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
420 if (real_target_type == TypeManager.int64_type)
421 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
422 if (real_target_type == TypeManager.double_type)
423 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
424 if (real_target_type == TypeManager.float_type)
425 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
426 if (real_target_type == TypeManager.short_type)
427 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
428 } else if (expr_type == TypeManager.byte_type){
430 // From byte to short, ushort, int, uint, long, ulong, float, double
432 if ((real_target_type == TypeManager.short_type) ||
433 (real_target_type == TypeManager.ushort_type) ||
434 (real_target_type == TypeManager.int32_type) ||
435 (real_target_type == TypeManager.uint32_type))
436 return new EmptyCast (expr, target_type);
438 if (real_target_type == TypeManager.uint64_type)
439 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
440 if (real_target_type == TypeManager.int64_type)
441 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
442 if (real_target_type == TypeManager.float_type)
443 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
444 if (real_target_type == TypeManager.double_type)
445 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
446 } else if (expr_type == TypeManager.short_type){
448 // From short to int, long, float, double
450 if (real_target_type == TypeManager.int32_type)
451 return new EmptyCast (expr, target_type);
452 if (real_target_type == TypeManager.int64_type)
453 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
454 if (real_target_type == TypeManager.double_type)
455 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
456 if (real_target_type == TypeManager.float_type)
457 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
458 } else if (expr_type == TypeManager.ushort_type){
460 // From ushort to int, uint, long, ulong, float, double
462 if (real_target_type == TypeManager.uint32_type)
463 return new EmptyCast (expr, target_type);
465 if (real_target_type == TypeManager.uint64_type)
466 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
467 if (real_target_type == TypeManager.int32_type)
468 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
469 if (real_target_type == TypeManager.int64_type)
470 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
471 if (real_target_type == TypeManager.double_type)
472 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
473 if (real_target_type == TypeManager.float_type)
474 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
475 } else if (expr_type == TypeManager.int32_type){
477 // From int to long, float, double
479 if (real_target_type == TypeManager.int64_type)
480 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
481 if (real_target_type == TypeManager.double_type)
482 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
483 if (real_target_type == TypeManager.float_type)
484 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
485 } else if (expr_type == TypeManager.uint32_type){
487 // From uint to long, ulong, float, double
489 if (real_target_type == TypeManager.int64_type)
490 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
491 if (real_target_type == TypeManager.uint64_type)
492 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
493 if (real_target_type == TypeManager.double_type)
494 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
496 if (real_target_type == TypeManager.float_type)
497 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
499 } else if (expr_type == TypeManager.int64_type){
501 // From long/ulong to float, double
503 if (real_target_type == TypeManager.double_type)
504 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
505 if (real_target_type == TypeManager.float_type)
506 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
507 } else if (expr_type == TypeManager.uint64_type){
509 // From ulong to float, double
511 if (real_target_type == TypeManager.double_type)
512 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
514 if (real_target_type == TypeManager.float_type)
515 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
517 } else if (expr_type == TypeManager.char_type){
519 // From char to ushort, int, uint, long, ulong, float, double
521 if ((real_target_type == TypeManager.ushort_type) ||
522 (real_target_type == TypeManager.int32_type) ||
523 (real_target_type == TypeManager.uint32_type))
524 return new EmptyCast (expr, target_type);
525 if (real_target_type == TypeManager.uint64_type)
526 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
527 if (real_target_type == TypeManager.int64_type)
528 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
529 if (real_target_type == TypeManager.float_type)
530 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
531 if (real_target_type == TypeManager.double_type)
532 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
533 } else if (expr_type == TypeManager.float_type){
537 if (real_target_type == TypeManager.double_type)
538 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
546 /// Same as ImplicitStandardConversionExists except that it also looks at
547 /// implicit user defined conversions - needed for overload resolution
549 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
551 if ((expr is NullLiteral) && target_type.IsGenericParameter)
552 return TypeParameter_to_Null (target_type);
554 if (ImplicitStandardConversionExists (ec, expr, target_type))
557 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
565 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
567 Expression dummy = ImplicitUserConversion (
568 ec, new EmptyExpression (source), target, Location.Null);
569 return dummy != null;
573 /// Determines if a standard implicit conversion exists from
574 /// expr_type to target_type
576 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
578 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
580 Type expr_type = expr.Type;
582 if (expr_type == TypeManager.void_type)
585 //Console.WriteLine ("Expr is {0}", expr);
586 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
587 if (expr_type.Equals (target_type))
590 // First numeric conversions
592 if (expr_type == TypeManager.sbyte_type){
594 // From sbyte to short, int, long, float, double.
596 if ((target_type == TypeManager.int32_type) ||
597 (target_type == TypeManager.int64_type) ||
598 (target_type == TypeManager.double_type) ||
599 (target_type == TypeManager.float_type) ||
600 (target_type == TypeManager.short_type) ||
601 (target_type == TypeManager.decimal_type))
604 } else if (expr_type == TypeManager.byte_type){
606 // From byte to short, ushort, int, uint, long, ulong, float, double
608 if ((target_type == TypeManager.short_type) ||
609 (target_type == TypeManager.ushort_type) ||
610 (target_type == TypeManager.int32_type) ||
611 (target_type == TypeManager.uint32_type) ||
612 (target_type == TypeManager.uint64_type) ||
613 (target_type == TypeManager.int64_type) ||
614 (target_type == TypeManager.float_type) ||
615 (target_type == TypeManager.double_type) ||
616 (target_type == TypeManager.decimal_type))
619 } else if (expr_type == TypeManager.short_type){
621 // From short to int, long, float, double
623 if ((target_type == TypeManager.int32_type) ||
624 (target_type == TypeManager.int64_type) ||
625 (target_type == TypeManager.double_type) ||
626 (target_type == TypeManager.float_type) ||
627 (target_type == TypeManager.decimal_type))
630 } else if (expr_type == TypeManager.ushort_type){
632 // From ushort to int, uint, long, ulong, float, double
634 if ((target_type == TypeManager.uint32_type) ||
635 (target_type == TypeManager.uint64_type) ||
636 (target_type == TypeManager.int32_type) ||
637 (target_type == TypeManager.int64_type) ||
638 (target_type == TypeManager.double_type) ||
639 (target_type == TypeManager.float_type) ||
640 (target_type == TypeManager.decimal_type))
643 } else if (expr_type == TypeManager.int32_type){
645 // From int to long, float, double
647 if ((target_type == TypeManager.int64_type) ||
648 (target_type == TypeManager.double_type) ||
649 (target_type == TypeManager.float_type) ||
650 (target_type == TypeManager.decimal_type))
653 } else if (expr_type == TypeManager.uint32_type){
655 // From uint to long, ulong, float, double
657 if ((target_type == TypeManager.int64_type) ||
658 (target_type == TypeManager.uint64_type) ||
659 (target_type == TypeManager.double_type) ||
660 (target_type == TypeManager.float_type) ||
661 (target_type == TypeManager.decimal_type))
664 } else if ((expr_type == TypeManager.uint64_type) ||
665 (expr_type == TypeManager.int64_type)) {
667 // From long/ulong to float, double
669 if ((target_type == TypeManager.double_type) ||
670 (target_type == TypeManager.float_type) ||
671 (target_type == TypeManager.decimal_type))
674 } else if (expr_type == TypeManager.char_type){
676 // From char to ushort, int, uint, long, ulong, float, double
678 if ((target_type == TypeManager.ushort_type) ||
679 (target_type == TypeManager.int32_type) ||
680 (target_type == TypeManager.uint32_type) ||
681 (target_type == TypeManager.uint64_type) ||
682 (target_type == TypeManager.int64_type) ||
683 (target_type == TypeManager.float_type) ||
684 (target_type == TypeManager.double_type) ||
685 (target_type == TypeManager.decimal_type))
688 } else if (expr_type == TypeManager.float_type){
692 if (target_type == TypeManager.double_type)
696 if (ImplicitReferenceConversionExists (ec, expr, target_type))
700 // Implicit Constant Expression Conversions
702 if (expr is IntConstant){
703 int value = ((IntConstant) expr).Value;
705 if (target_type == TypeManager.sbyte_type){
706 if (value >= SByte.MinValue && value <= SByte.MaxValue)
708 } else if (target_type == TypeManager.byte_type){
709 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
711 } else if (target_type == TypeManager.short_type){
712 if (value >= Int16.MinValue && value <= Int16.MaxValue)
714 } else if (target_type == TypeManager.ushort_type){
715 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
717 } else if (target_type == TypeManager.uint32_type){
720 } else if (target_type == TypeManager.uint64_type){
722 // we can optimize this case: a positive int32
723 // always fits on a uint64. But we need an opcode
730 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
734 if (expr is LongConstant && target_type == TypeManager.uint64_type){
736 // Try the implicit constant expression conversion
737 // from long to ulong, instead of a nice routine,
740 long v = ((LongConstant) expr).Value;
745 if ((target_type == TypeManager.enum_type ||
746 target_type.IsSubclassOf (TypeManager.enum_type)) &&
748 IntLiteral i = (IntLiteral) expr;
754 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
757 if (expr_type == TypeManager.anonymous_method_type){
758 if (!TypeManager.IsDelegateType (target_type))
761 AnonymousMethod am = (AnonymousMethod) expr;
762 int errors = Report.Errors;
764 Expression conv = am.Compatible (ec, target_type, true);
773 // Used internally by FindMostEncompassedType, this is used
774 // to avoid creating lots of objects in the tight loop inside
775 // FindMostEncompassedType
777 static EmptyExpression priv_fmet_param;
780 /// Finds "most encompassed type" according to the spec (13.4.2)
781 /// amongst the methods in the MethodGroupExpr
783 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
787 if (priv_fmet_param == null)
788 priv_fmet_param = new EmptyExpression ();
790 foreach (Type t in types){
791 priv_fmet_param.SetType (t);
798 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
806 // Used internally by FindMostEncompassingType, this is used
807 // to avoid creating lots of objects in the tight loop inside
808 // FindMostEncompassingType
810 static EmptyExpression priv_fmee_ret;
813 /// Finds "most encompassing type" according to the spec (13.4.2)
814 /// amongst the types in the given set
816 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
820 if (priv_fmee_ret == null)
821 priv_fmee_ret = new EmptyExpression ();
823 foreach (Type t in types){
824 priv_fmee_ret.SetType (best);
831 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
839 // Used to avoid creating too many objects
841 static EmptyExpression priv_fms_expr;
844 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
845 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
846 /// for explicit and implicit conversion operators.
848 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
849 Expression source, bool apply_explicit_conv_rules,
852 ArrayList src_types_set = new ArrayList ();
854 if (priv_fms_expr == null)
855 priv_fms_expr = new EmptyExpression ();
858 // If any operator converts from S then Sx = S
860 Type source_type = source.Type;
861 foreach (MethodBase mb in me.Methods){
862 ParameterData pd = Invocation.GetParameterData (mb);
863 Type param_type = pd.ParameterType (0);
865 if (param_type == source_type)
868 if (apply_explicit_conv_rules) {
871 // Find the set of applicable user-defined conversion operators, U. This set
873 // user-defined implicit or explicit conversion operators declared by
874 // the classes or structs in D that convert from a type encompassing
875 // or encompassed by S to a type encompassing or encompassed by T
877 priv_fms_expr.SetType (param_type);
878 if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
879 src_types_set.Add (param_type);
881 if (ImplicitStandardConversionExists (ec, source, param_type))
882 src_types_set.Add (param_type);
886 // Only if S is encompassed by param_type
888 if (ImplicitStandardConversionExists (ec, source, param_type))
889 src_types_set.Add (param_type);
894 // Explicit Conv rules
896 if (apply_explicit_conv_rules) {
897 ArrayList candidate_set = new ArrayList ();
899 foreach (Type param_type in src_types_set){
900 if (ImplicitStandardConversionExists (ec, source, param_type))
901 candidate_set.Add (param_type);
904 if (candidate_set.Count != 0)
905 return FindMostEncompassedType (ec, candidate_set);
911 if (apply_explicit_conv_rules)
912 return FindMostEncompassingType (ec, src_types_set);
914 return FindMostEncompassedType (ec, src_types_set);
918 // Useful in avoiding proliferation of objects
920 static EmptyExpression priv_fmt_expr;
923 /// Finds the most specific target Tx according to section 13.4.4
925 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
926 Type target, bool apply_explicit_conv_rules,
929 ArrayList tgt_types_set = new ArrayList ();
931 if (priv_fmt_expr == null)
932 priv_fmt_expr = new EmptyExpression ();
935 // If any operator converts to T then Tx = T
937 foreach (MethodInfo mi in me.Methods){
938 Type ret_type = mi.ReturnType;
940 if (ret_type == target)
943 if (apply_explicit_conv_rules) {
946 // Find the set of applicable user-defined conversion operators, U.
948 // This set consists of the
949 // user-defined implicit or explicit conversion operators declared by
950 // the classes or structs in D that convert from a type encompassing
951 // or encompassed by S to a type encompassing or encompassed by T
953 priv_fms_expr.SetType (ret_type);
954 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
955 tgt_types_set.Add (ret_type);
957 priv_fms_expr.SetType (target);
958 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
959 tgt_types_set.Add (ret_type);
963 // Only if T is encompassed by param_type
965 priv_fms_expr.SetType (ret_type);
966 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
967 tgt_types_set.Add (ret_type);
972 // Explicit conv rules
974 if (apply_explicit_conv_rules) {
975 ArrayList candidate_set = new ArrayList ();
977 foreach (Type ret_type in tgt_types_set){
978 priv_fmt_expr.SetType (ret_type);
980 if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
981 candidate_set.Add (ret_type);
984 if (candidate_set.Count != 0)
985 return FindMostEncompassingType (ec, candidate_set);
989 // Okay, final case !
991 if (apply_explicit_conv_rules)
992 return FindMostEncompassedType (ec, tgt_types_set);
994 return FindMostEncompassingType (ec, tgt_types_set);
998 /// User-defined Implicit conversions
1000 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1001 Type target, Location loc)
1003 return UserDefinedConversion (ec, source, target, loc, false);
1007 /// User-defined Explicit conversions
1009 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1010 Type target, Location loc)
1012 return UserDefinedConversion (ec, source, target, loc, true);
1015 static DoubleHash explicit_conv = new DoubleHash (100);
1016 static DoubleHash implicit_conv = new DoubleHash (100);
1018 /// Computes the MethodGroup for the user-defined conversion
1019 /// operators from source_type to target_type. `look_for_explicit'
1020 /// controls whether we should also include the list of explicit
1023 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1024 Type source_type, Type target_type,
1025 Location loc, bool look_for_explicit)
1027 Expression mg1 = null, mg2 = null;
1028 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1031 op_name = "op_Implicit";
1033 MethodGroupExpr union3;
1035 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1036 return (MethodGroupExpr) r;
1038 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1039 if (source_type.BaseType != null)
1040 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1043 union3 = (MethodGroupExpr) mg2;
1044 else if (mg2 == null)
1045 union3 = (MethodGroupExpr) mg1;
1047 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1049 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1052 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1054 union3 = (MethodGroupExpr) mg1;
1057 if (target_type.BaseType != null)
1058 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1062 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1064 union3 = (MethodGroupExpr) mg1;
1067 MethodGroupExpr union4 = null;
1069 if (look_for_explicit) {
1070 op_name = "op_Explicit";
1072 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1073 if (source_type.BaseType != null)
1074 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1076 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1077 if (target_type.BaseType != null)
1078 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1080 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1081 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1083 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1086 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1087 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1092 /// User-defined conversions
1094 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1095 Type target, Location loc,
1096 bool look_for_explicit)
1098 MethodGroupExpr union;
1099 Type source_type = source.Type;
1100 MethodBase method = null;
1102 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1106 Type most_specific_source, most_specific_target;
1108 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1109 if (most_specific_source == null)
1112 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1113 if (most_specific_target == null)
1119 foreach (MethodBase mb in union.Methods){
1120 ParameterData pd = Invocation.GetParameterData (mb);
1121 MethodInfo mi = (MethodInfo) mb;
1123 if (pd.ParameterType (0) == most_specific_source &&
1124 mi.ReturnType == most_specific_target) {
1130 if (method == null || count > 1)
1135 // This will do the conversion to the best match that we
1136 // found. Now we need to perform an implict standard conversion
1137 // if the best match was not the type that we were requested
1140 if (look_for_explicit)
1141 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1143 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1149 e = new UserCast ((MethodInfo) method, source, loc);
1150 if (e.Type != target){
1151 if (!look_for_explicit)
1152 e = ImplicitConversionStandard (ec, e, target, loc);
1154 e = ExplicitConversionStandard (ec, e, target, loc);
1161 /// Converts implicitly the resolved expression `expr' into the
1162 /// `target_type'. It returns a new expression that can be used
1163 /// in a context that expects a `target_type'.
1165 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1166 Type target_type, Location loc)
1170 if (target_type == null)
1171 throw new Exception ("Target type is null");
1173 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1177 e = ImplicitUserConversion (ec, expr, target_type, loc);
1186 /// Attempts to apply the `Standard Implicit
1187 /// Conversion' rules to the expression `expr' into
1188 /// the `target_type'. It returns a new expression
1189 /// that can be used in a context that expects a
1192 /// This is different from `ImplicitConversion' in that the
1193 /// user defined implicit conversions are excluded.
1195 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1196 Type target_type, Location loc)
1198 Type expr_type = expr.Type;
1201 if ((expr is NullLiteral) && target_type.IsGenericParameter)
1202 return TypeParameter_to_Null (expr, target_type, loc);
1204 if (expr.eclass == ExprClass.MethodGroup){
1205 if (!TypeManager.IsDelegateType (target_type)){
1210 // Only allow anonymous method conversions on post ISO_1
1212 if (RootContext.Version != LanguageVersion.ISO_1){
1213 MethodGroupExpr mg = expr as MethodGroupExpr;
1215 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1219 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1222 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1226 e = ImplicitReferenceConversion (ec, expr, target_type);
1230 if ((target_type == TypeManager.enum_type ||
1231 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1232 expr is IntLiteral){
1233 IntLiteral i = (IntLiteral) expr;
1236 return new EnumConstant ((Constant) expr, target_type);
1240 if (expr_type.IsPointer){
1241 if (target_type == TypeManager.void_ptr_type)
1242 return new EmptyCast (expr, target_type);
1245 // yep, comparing pointer types cant be done with
1246 // t1 == t2, we have to compare their element types.
1248 if (target_type.IsPointer){
1249 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1254 if (target_type.IsPointer) {
1255 if (expr_type == TypeManager.null_type)
1256 return new EmptyCast (expr, target_type);
1258 if (expr_type == TypeManager.void_ptr_type)
1259 return new EmptyCast (expr, target_type);
1263 if (expr_type == TypeManager.anonymous_method_type){
1264 if (!TypeManager.IsDelegateType (target_type)){
1265 Report.Error (1660, loc,
1266 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1267 TypeManager.CSharpName (target_type));
1271 AnonymousMethod am = (AnonymousMethod) expr;
1272 int errors = Report.Errors;
1274 Expression conv = am.Compatible (ec, target_type, false);
1279 // We return something instead of null, to avoid
1280 // the duplicate error, since am.Compatible would have
1281 // reported that already
1283 if (errors != Report.Errors)
1284 return new EmptyCast (expr, target_type);
1291 /// Attemps to perform an implict constant conversion of the IntConstant
1292 /// into a different data type using casts (See Implicit Constant
1293 /// Expression Conversions)
1295 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1297 int value = ic.Value;
1299 if (target_type == TypeManager.sbyte_type){
1300 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1301 return new SByteConstant ((sbyte) value);
1302 } else if (target_type == TypeManager.byte_type){
1303 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1304 return new ByteConstant ((byte) value);
1305 } else if (target_type == TypeManager.short_type){
1306 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1307 return new ShortConstant ((short) value);
1308 } else if (target_type == TypeManager.ushort_type){
1309 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1310 return new UShortConstant ((ushort) value);
1311 } else if (target_type == TypeManager.uint32_type){
1313 return new UIntConstant ((uint) value);
1314 } else if (target_type == TypeManager.uint64_type){
1316 // we can optimize this case: a positive int32
1317 // always fits on a uint64. But we need an opcode
1321 return new ULongConstant ((ulong) value);
1322 } else if (target_type == TypeManager.double_type)
1323 return new DoubleConstant ((double) value);
1324 else if (target_type == TypeManager.float_type)
1325 return new FloatConstant ((float) value);
1327 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1328 Type underlying = TypeManager.EnumToUnderlying (target_type);
1329 Constant e = (Constant) ic;
1332 // Possibly, we need to create a different 0 literal before passing
1335 if (underlying == TypeManager.int64_type)
1336 e = new LongLiteral (0);
1337 else if (underlying == TypeManager.uint64_type)
1338 e = new ULongLiteral (0);
1340 return new EnumConstant (e, target_type);
1345 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1347 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1348 source == TypeManager.anonymous_method_type ?
1349 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1350 TypeManager.CSharpName (target));
1354 /// Attemptes to implicityly convert `target' into `type', using
1355 /// ImplicitConversion. If there is no implicit conversion, then
1356 /// an error is signaled
1358 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1359 Type target_type, Location loc)
1363 int errors = Report.Errors;
1364 e = ImplicitConversion (ec, source, target_type, loc);
1365 if (Report.Errors > errors)
1370 if (source is DoubleLiteral) {
1371 if (target_type == TypeManager.float_type) {
1372 Error_664 (loc, "float", "f");
1375 if (target_type == TypeManager.decimal_type) {
1376 Error_664 (loc, "decimal", "m");
1381 if (source is Constant){
1382 Constant c = (Constant) source;
1384 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1388 Error_CannotImplicitConversion (loc, source.Type, target_type);
1393 static void Error_664 (Location loc, string type, string suffix) {
1394 Report.Error (664, loc,
1395 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1400 /// Performs the explicit numeric conversions
1402 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1404 Type expr_type = expr.Type;
1407 // If we have an enumeration, extract the underlying type,
1408 // use this during the comparison, but wrap around the original
1411 Type real_target_type = target_type;
1413 if (TypeManager.IsEnumType (real_target_type))
1414 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1416 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1417 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1419 if (real_target_type != target_type)
1420 return new EmptyCast (ce, target_type);
1424 if (expr_type == TypeManager.sbyte_type){
1426 // From sbyte to byte, ushort, uint, ulong, char
1428 if (real_target_type == TypeManager.byte_type)
1429 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1430 if (real_target_type == TypeManager.ushort_type)
1431 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1432 if (real_target_type == TypeManager.uint32_type)
1433 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1434 if (real_target_type == TypeManager.uint64_type)
1435 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1436 if (real_target_type == TypeManager.char_type)
1437 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1438 } else if (expr_type == TypeManager.byte_type){
1440 // From byte to sbyte and char
1442 if (real_target_type == TypeManager.sbyte_type)
1443 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1444 if (real_target_type == TypeManager.char_type)
1445 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1446 } else if (expr_type == TypeManager.short_type){
1448 // From short to sbyte, byte, ushort, uint, ulong, char
1450 if (real_target_type == TypeManager.sbyte_type)
1451 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1452 if (real_target_type == TypeManager.byte_type)
1453 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1454 if (real_target_type == TypeManager.ushort_type)
1455 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1456 if (real_target_type == TypeManager.uint32_type)
1457 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1458 if (real_target_type == TypeManager.uint64_type)
1459 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1460 if (real_target_type == TypeManager.char_type)
1461 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1462 } else if (expr_type == TypeManager.ushort_type){
1464 // From ushort to sbyte, byte, short, char
1466 if (real_target_type == TypeManager.sbyte_type)
1467 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1468 if (real_target_type == TypeManager.byte_type)
1469 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1470 if (real_target_type == TypeManager.short_type)
1471 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1472 if (real_target_type == TypeManager.char_type)
1473 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1474 } else if (expr_type == TypeManager.int32_type){
1476 // From int to sbyte, byte, short, ushort, uint, ulong, char
1478 if (real_target_type == TypeManager.sbyte_type)
1479 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1480 if (real_target_type == TypeManager.byte_type)
1481 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1482 if (real_target_type == TypeManager.short_type)
1483 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1484 if (real_target_type == TypeManager.ushort_type)
1485 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1486 if (real_target_type == TypeManager.uint32_type)
1487 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1488 if (real_target_type == TypeManager.uint64_type)
1489 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1490 if (real_target_type == TypeManager.char_type)
1491 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1492 } else if (expr_type == TypeManager.uint32_type){
1494 // From uint to sbyte, byte, short, ushort, int, char
1496 if (real_target_type == TypeManager.sbyte_type)
1497 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1498 if (real_target_type == TypeManager.byte_type)
1499 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1500 if (real_target_type == TypeManager.short_type)
1501 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1502 if (real_target_type == TypeManager.ushort_type)
1503 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1504 if (real_target_type == TypeManager.int32_type)
1505 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1506 if (real_target_type == TypeManager.char_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1508 } else if (expr_type == TypeManager.int64_type){
1510 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1512 if (real_target_type == TypeManager.sbyte_type)
1513 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1514 if (real_target_type == TypeManager.byte_type)
1515 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1516 if (real_target_type == TypeManager.short_type)
1517 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1518 if (real_target_type == TypeManager.ushort_type)
1519 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1520 if (real_target_type == TypeManager.int32_type)
1521 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1522 if (real_target_type == TypeManager.uint32_type)
1523 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1524 if (real_target_type == TypeManager.uint64_type)
1525 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1526 if (real_target_type == TypeManager.char_type)
1527 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1528 } else if (expr_type == TypeManager.uint64_type){
1530 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1532 if (real_target_type == TypeManager.sbyte_type)
1533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1534 if (real_target_type == TypeManager.byte_type)
1535 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1536 if (real_target_type == TypeManager.short_type)
1537 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1538 if (real_target_type == TypeManager.ushort_type)
1539 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1540 if (real_target_type == TypeManager.int32_type)
1541 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1542 if (real_target_type == TypeManager.uint32_type)
1543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1544 if (real_target_type == TypeManager.int64_type)
1545 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1546 if (real_target_type == TypeManager.char_type)
1547 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1548 } else if (expr_type == TypeManager.char_type){
1550 // From char to sbyte, byte, short
1552 if (real_target_type == TypeManager.sbyte_type)
1553 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1554 if (real_target_type == TypeManager.byte_type)
1555 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1556 if (real_target_type == TypeManager.short_type)
1557 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1558 } else if (expr_type == TypeManager.float_type){
1560 // From float to sbyte, byte, short,
1561 // ushort, int, uint, long, ulong, char
1564 if (real_target_type == TypeManager.sbyte_type)
1565 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1566 if (real_target_type == TypeManager.byte_type)
1567 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1568 if (real_target_type == TypeManager.short_type)
1569 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1570 if (real_target_type == TypeManager.ushort_type)
1571 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1572 if (real_target_type == TypeManager.int32_type)
1573 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1574 if (real_target_type == TypeManager.uint32_type)
1575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1576 if (real_target_type == TypeManager.int64_type)
1577 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1578 if (real_target_type == TypeManager.uint64_type)
1579 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1580 if (real_target_type == TypeManager.char_type)
1581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1582 } else if (expr_type == TypeManager.double_type){
1584 // From double to byte, byte, short,
1585 // ushort, int, uint, long, ulong,
1586 // char, float or decimal
1588 if (real_target_type == TypeManager.sbyte_type)
1589 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1590 if (real_target_type == TypeManager.byte_type)
1591 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1592 if (real_target_type == TypeManager.short_type)
1593 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1594 if (real_target_type == TypeManager.ushort_type)
1595 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1596 if (real_target_type == TypeManager.int32_type)
1597 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1598 if (real_target_type == TypeManager.uint32_type)
1599 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1600 if (real_target_type == TypeManager.int64_type)
1601 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1602 if (real_target_type == TypeManager.uint64_type)
1603 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1604 if (real_target_type == TypeManager.char_type)
1605 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1606 if (real_target_type == TypeManager.float_type)
1607 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1610 // decimal is taken care of by the op_Explicit methods.
1616 /// Returns whether an explicit reference conversion can be performed
1617 /// from source_type to target_type
1619 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1621 bool target_is_type_param = target_type.IsGenericParameter;
1622 bool target_is_value_type = target_type.IsValueType;
1624 if (source_type == target_type)
1628 // From object to a generic parameter
1630 if (source_type == TypeManager.object_type && target_is_type_param)
1634 // From object to any reference type
1636 if (source_type == TypeManager.object_type && !target_is_value_type)
1640 // From any class S to any class-type T, provided S is a base class of T
1642 if (TypeManager.IsSubclassOf (target_type, source_type))
1646 // From any interface type S to any interface T provided S is not derived from T
1648 if (source_type.IsInterface && target_type.IsInterface){
1649 if (!TypeManager.IsSubclassOf (target_type, source_type))
1654 // From any class type S to any interface T, provided S is not sealed
1655 // and provided S does not implement T.
1657 if (target_type.IsInterface && !source_type.IsSealed &&
1658 !TypeManager.ImplementsInterface (source_type, target_type))
1662 // From any interface-type S to to any class type T, provided T is not
1663 // sealed, or provided T implements S.
1665 if (source_type.IsInterface &&
1666 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1670 // From an array type S with an element type Se to an array type T with an
1671 // element type Te provided all the following are true:
1672 // * S and T differe only in element type, in other words, S and T
1673 // have the same number of dimensions.
1674 // * Both Se and Te are reference types
1675 // * An explicit referenc conversions exist from Se to Te
1677 if (source_type.IsArray && target_type.IsArray) {
1678 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1680 Type source_element_type = TypeManager.GetElementType (source_type);
1681 Type target_element_type = TypeManager.GetElementType (target_type);
1683 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1684 if (ExplicitReferenceConversionExists (source_element_type,
1685 target_element_type))
1691 // From System.Array to any array-type
1692 if (source_type == TypeManager.array_type &&
1693 target_type.IsArray){
1698 // From System delegate to any delegate-type
1700 if (source_type == TypeManager.delegate_type &&
1701 TypeManager.IsDelegateType (target_type))
1705 // From ICloneable to Array or Delegate types
1707 if (source_type == TypeManager.icloneable_type &&
1708 (target_type == TypeManager.array_type ||
1709 target_type == TypeManager.delegate_type))
1716 /// Implements Explicit Reference conversions
1718 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1720 Type source_type = source.Type;
1721 bool target_is_type_param = target_type.IsGenericParameter;
1722 bool target_is_value_type = target_type.IsValueType;
1725 // From object to a generic parameter
1727 if (source_type == TypeManager.object_type && target_is_type_param)
1728 return new UnboxCast (source, target_type);
1731 // From object to any reference type
1733 if (source_type == TypeManager.object_type && !target_is_value_type)
1734 return new ClassCast (source, target_type);
1737 // Unboxing conversion.
1739 if (((source_type == TypeManager.enum_type &&
1740 !(source is EmptyCast)) ||
1741 source_type == TypeManager.value_type) && target_is_value_type)
1742 return new UnboxCast (source, target_type);
1745 // From any class S to any class-type T, provided S is a base class of T
1747 if (TypeManager.IsSubclassOf (target_type, source_type))
1748 return new ClassCast (source, target_type);
1751 // From any interface type S to any interface T provided S is not derived from T
1753 if (source_type.IsInterface && target_type.IsInterface){
1754 if (TypeManager.ImplementsInterface (source_type, target_type))
1757 return new ClassCast (source, target_type);
1761 // From any class type S to any interface T, provides S is not sealed
1762 // and provided S does not implement T.
1764 if (target_type.IsInterface && !source_type.IsSealed) {
1765 if (TypeManager.ImplementsInterface (source_type, target_type))
1768 return new ClassCast (source, target_type);
1773 // From any interface-type S to to any class type T, provided T is not
1774 // sealed, or provided T implements S.
1776 if (source_type.IsInterface) {
1777 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1778 if (target_type.IsClass)
1779 return new ClassCast (source, target_type);
1781 return new UnboxCast (source, target_type);
1787 // From an array type S with an element type Se to an array type T with an
1788 // element type Te provided all the following are true:
1789 // * S and T differe only in element type, in other words, S and T
1790 // have the same number of dimensions.
1791 // * Both Se and Te are reference types
1792 // * An explicit referenc conversions exist from Se to Te
1794 if (source_type.IsArray && target_type.IsArray) {
1795 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1797 Type source_element_type = TypeManager.GetElementType (source_type);
1798 Type target_element_type = TypeManager.GetElementType (target_type);
1800 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1801 if (ExplicitReferenceConversionExists (source_element_type,
1802 target_element_type))
1803 return new ClassCast (source, target_type);
1808 // From System.Array to any array-type
1809 if (source_type == TypeManager.array_type &&
1810 target_type.IsArray) {
1811 return new ClassCast (source, target_type);
1815 // From System delegate to any delegate-type
1817 if (source_type == TypeManager.delegate_type &&
1818 TypeManager.IsDelegateType (target_type))
1819 return new ClassCast (source, target_type);
1822 // From ICloneable to Array or Delegate types
1824 if (source_type == TypeManager.icloneable_type &&
1825 (target_type == TypeManager.array_type ||
1826 target_type == TypeManager.delegate_type))
1827 return new ClassCast (source, target_type);
1833 /// Performs an explicit conversion of the expression `expr' whose
1834 /// type is expr.Type to `target_type'.
1836 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1837 Type target_type, Location loc)
1839 Type expr_type = expr.Type;
1840 Type original_expr_type = expr_type;
1842 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1843 if (target_type == TypeManager.enum_type ||
1844 target_type == TypeManager.object_type) {
1845 if (expr is EnumConstant)
1846 expr = ((EnumConstant) expr).Child;
1847 // We really need all these casts here .... :-(
1848 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1849 return new EmptyCast (expr, target_type);
1850 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1851 target_type.IsSubclassOf (TypeManager.enum_type))
1852 return new UnboxCast (expr, target_type);
1855 // Notice that we have kept the expr_type unmodified, which is only
1857 if (expr is EnumConstant)
1858 expr = ((EnumConstant) expr).Child;
1860 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1861 expr_type = expr.Type;
1864 int errors = Report.Errors;
1865 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1866 if (Report.Errors > errors)
1872 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1877 // Unboxing conversion.
1879 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1880 return new UnboxCast (expr, target_type);
1883 // Skip the ExplicitReferenceConversion because we can not convert
1884 // from Null to a ValueType, and ExplicitReference wont check against
1885 // null literal explicitly
1887 if (expr_type != TypeManager.null_type){
1888 ne = ExplicitReferenceConversion (expr, target_type);
1895 if (target_type.IsPointer){
1896 if (expr_type.IsPointer)
1897 return new EmptyCast (expr, target_type);
1899 if (expr_type == TypeManager.sbyte_type ||
1900 expr_type == TypeManager.byte_type ||
1901 expr_type == TypeManager.short_type ||
1902 expr_type == TypeManager.ushort_type ||
1903 expr_type == TypeManager.int32_type ||
1904 expr_type == TypeManager.uint32_type ||
1905 expr_type == TypeManager.uint64_type ||
1906 expr_type == TypeManager.int64_type)
1907 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1909 if (expr_type.IsPointer){
1910 Expression e = null;
1912 if (target_type == TypeManager.sbyte_type)
1913 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1914 else if (target_type == TypeManager.byte_type)
1915 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1916 else if (target_type == TypeManager.short_type)
1917 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1918 else if (target_type == TypeManager.ushort_type)
1919 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1920 else if (target_type == TypeManager.int32_type)
1921 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1922 else if (target_type == TypeManager.uint32_type)
1923 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1924 else if (target_type == TypeManager.uint64_type)
1925 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1926 else if (target_type == TypeManager.int64_type){
1927 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1933 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1938 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1942 // We should always be able to go from an uint32
1943 // implicitly or explicitly to the other integral
1946 throw new Exception ("Internal compiler error");
1951 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1955 if (expr is NullLiteral){
1956 Report.Error (37, loc, "Cannot convert null to value type `" +
1957 TypeManager.CSharpName (target_type) + "'");
1961 Error_CannotConvertType (loc, original_expr_type, target_type);
1966 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1968 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1969 Type target_type, Location l)
1971 int errors = Report.Errors;
1972 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1973 if (Report.Errors > errors)
1979 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1983 ne = ExplicitReferenceConversion (expr, target_type);
1987 Error_CannotConvertType (l, expr.Type, target_type);