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 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;
763 Expression conv = am.Compatible (ec, target_type, true);
772 // Used internally by FindMostEncompassedType, this is used
773 // to avoid creating lots of objects in the tight loop inside
774 // FindMostEncompassedType
776 static EmptyExpression priv_fmet_param;
779 /// Finds "most encompassed type" according to the spec (13.4.2)
780 /// amongst the methods in the MethodGroupExpr
782 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
786 if (priv_fmet_param == null)
787 priv_fmet_param = new EmptyExpression ();
789 foreach (Type t in types){
790 priv_fmet_param.SetType (t);
797 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
805 // Used internally by FindMostEncompassingType, this is used
806 // to avoid creating lots of objects in the tight loop inside
807 // FindMostEncompassingType
809 static EmptyExpression priv_fmee_ret;
812 /// Finds "most encompassing type" according to the spec (13.4.2)
813 /// amongst the types in the given set
815 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
819 if (priv_fmee_ret == null)
820 priv_fmee_ret = new EmptyExpression ();
822 foreach (Type t in types){
823 priv_fmee_ret.SetType (best);
830 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
838 // Used to avoid creating too many objects
840 static EmptyExpression priv_fms_expr;
843 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
844 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
845 /// for explicit and implicit conversion operators.
847 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
848 Expression source, bool apply_explicit_conv_rules,
851 ArrayList src_types_set = new ArrayList ();
853 if (priv_fms_expr == null)
854 priv_fms_expr = new EmptyExpression ();
857 // If any operator converts from S then Sx = S
859 Type source_type = source.Type;
860 foreach (MethodBase mb in me.Methods){
861 ParameterData pd = Invocation.GetParameterData (mb);
862 Type param_type = pd.ParameterType (0);
864 if (param_type == source_type)
867 if (apply_explicit_conv_rules) {
870 // Find the set of applicable user-defined conversion operators, U. This set
872 // user-defined implicit or explicit conversion operators declared by
873 // the classes or structs in D that convert from a type encompassing
874 // or encompassed by S to a type encompassing or encompassed by T
876 priv_fms_expr.SetType (param_type);
877 if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
878 src_types_set.Add (param_type);
880 if (ImplicitStandardConversionExists (ec, source, param_type))
881 src_types_set.Add (param_type);
885 // Only if S is encompassed by param_type
887 if (ImplicitStandardConversionExists (ec, source, param_type))
888 src_types_set.Add (param_type);
893 // Explicit Conv rules
895 if (apply_explicit_conv_rules) {
896 ArrayList candidate_set = new ArrayList ();
898 foreach (Type param_type in src_types_set){
899 if (ImplicitStandardConversionExists (ec, source, param_type))
900 candidate_set.Add (param_type);
903 if (candidate_set.Count != 0)
904 return FindMostEncompassedType (ec, candidate_set);
910 if (apply_explicit_conv_rules)
911 return FindMostEncompassingType (ec, src_types_set);
913 return FindMostEncompassedType (ec, src_types_set);
917 // Useful in avoiding proliferation of objects
919 static EmptyExpression priv_fmt_expr;
922 /// Finds the most specific target Tx according to section 13.4.4
924 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
925 Type target, bool apply_explicit_conv_rules,
928 ArrayList tgt_types_set = new ArrayList ();
930 if (priv_fmt_expr == null)
931 priv_fmt_expr = new EmptyExpression ();
934 // If any operator converts to T then Tx = T
936 foreach (MethodInfo mi in me.Methods){
937 Type ret_type = mi.ReturnType;
939 if (ret_type == target)
942 if (apply_explicit_conv_rules) {
945 // Find the set of applicable user-defined conversion operators, U.
947 // This set consists of the
948 // user-defined implicit or explicit conversion operators declared by
949 // the classes or structs in D that convert from a type encompassing
950 // or encompassed by S to a type encompassing or encompassed by T
952 priv_fms_expr.SetType (ret_type);
953 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
954 tgt_types_set.Add (ret_type);
956 priv_fms_expr.SetType (target);
957 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
958 tgt_types_set.Add (ret_type);
962 // Only if T is encompassed by param_type
964 priv_fms_expr.SetType (ret_type);
965 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
966 tgt_types_set.Add (ret_type);
971 // Explicit conv rules
973 if (apply_explicit_conv_rules) {
974 ArrayList candidate_set = new ArrayList ();
976 foreach (Type ret_type in tgt_types_set){
977 priv_fmt_expr.SetType (ret_type);
979 if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
980 candidate_set.Add (ret_type);
983 if (candidate_set.Count != 0)
984 return FindMostEncompassingType (ec, candidate_set);
988 // Okay, final case !
990 if (apply_explicit_conv_rules)
991 return FindMostEncompassedType (ec, tgt_types_set);
993 return FindMostEncompassingType (ec, tgt_types_set);
997 /// User-defined Implicit conversions
999 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1000 Type target, Location loc)
1002 return UserDefinedConversion (ec, source, target, loc, false);
1006 /// User-defined Explicit conversions
1008 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1009 Type target, Location loc)
1011 return UserDefinedConversion (ec, source, target, loc, true);
1014 static DoubleHash explicit_conv = new DoubleHash (100);
1015 static DoubleHash implicit_conv = new DoubleHash (100);
1017 /// Computes the MethodGroup for the user-defined conversion
1018 /// operators from source_type to target_type. `look_for_explicit'
1019 /// controls whether we should also include the list of explicit
1022 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1023 Type source_type, Type target_type,
1024 Location loc, bool look_for_explicit)
1026 Expression mg1 = null, mg2 = null;
1027 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1030 op_name = "op_Implicit";
1032 MethodGroupExpr union3;
1034 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1035 return (MethodGroupExpr) r;
1037 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1038 if (source_type.BaseType != null)
1039 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1042 union3 = (MethodGroupExpr) mg2;
1043 else if (mg2 == null)
1044 union3 = (MethodGroupExpr) mg1;
1046 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1048 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1051 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1053 union3 = (MethodGroupExpr) mg1;
1056 if (target_type.BaseType != null)
1057 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1061 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1063 union3 = (MethodGroupExpr) mg1;
1066 MethodGroupExpr union4 = null;
1068 if (look_for_explicit) {
1069 op_name = "op_Explicit";
1071 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1072 if (source_type.BaseType != null)
1073 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1075 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1076 if (target_type.BaseType != null)
1077 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1079 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1080 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1082 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1085 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1086 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1091 /// User-defined conversions
1093 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1094 Type target, Location loc,
1095 bool look_for_explicit)
1097 MethodGroupExpr union;
1098 Type source_type = source.Type;
1099 MethodBase method = null;
1101 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1105 Type most_specific_source, most_specific_target;
1107 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1108 if (most_specific_source == null)
1111 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1112 if (most_specific_target == null)
1118 foreach (MethodBase mb in union.Methods){
1119 ParameterData pd = Invocation.GetParameterData (mb);
1120 MethodInfo mi = (MethodInfo) mb;
1122 if (pd.ParameterType (0) == most_specific_source &&
1123 mi.ReturnType == most_specific_target) {
1129 if (method == null || count > 1)
1134 // This will do the conversion to the best match that we
1135 // found. Now we need to perform an implict standard conversion
1136 // if the best match was not the type that we were requested
1139 if (look_for_explicit)
1140 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1142 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1148 e = new UserCast ((MethodInfo) method, source, loc);
1149 if (e.Type != target){
1150 if (!look_for_explicit)
1151 e = ImplicitConversionStandard (ec, e, target, loc);
1153 e = ExplicitConversionStandard (ec, e, target, loc);
1160 /// Converts implicitly the resolved expression `expr' into the
1161 /// `target_type'. It returns a new expression that can be used
1162 /// in a context that expects a `target_type'.
1164 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1165 Type target_type, Location loc)
1169 if (target_type == null)
1170 throw new Exception ("Target type is null");
1172 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1176 e = ImplicitUserConversion (ec, expr, target_type, loc);
1185 /// Attempts to apply the `Standard Implicit
1186 /// Conversion' rules to the expression `expr' into
1187 /// the `target_type'. It returns a new expression
1188 /// that can be used in a context that expects a
1191 /// This is different from `ImplicitConversion' in that the
1192 /// user defined implicit conversions are excluded.
1194 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1195 Type target_type, Location loc)
1197 Type expr_type = expr.Type;
1200 if ((expr is NullLiteral) && target_type.IsGenericParameter)
1201 return TypeParameter_to_Null (expr, target_type, loc);
1203 if (expr.eclass == ExprClass.MethodGroup){
1204 if (!TypeManager.IsDelegateType (target_type)){
1209 // Only allow anonymous method conversions on post ISO_1
1211 if (RootContext.Version != LanguageVersion.ISO_1){
1212 MethodGroupExpr mg = expr as MethodGroupExpr;
1214 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1218 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1221 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1225 e = ImplicitReferenceConversion (ec, expr, target_type);
1229 if ((target_type == TypeManager.enum_type ||
1230 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1231 expr is IntLiteral){
1232 IntLiteral i = (IntLiteral) expr;
1235 return new EnumConstant ((Constant) expr, target_type);
1239 if (expr_type.IsPointer){
1240 if (target_type == TypeManager.void_ptr_type)
1241 return new EmptyCast (expr, target_type);
1244 // yep, comparing pointer types cant be done with
1245 // t1 == t2, we have to compare their element types.
1247 if (target_type.IsPointer){
1248 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1253 if (target_type.IsPointer) {
1254 if (expr_type == TypeManager.null_type)
1255 return new EmptyCast (expr, target_type);
1257 if (expr_type == TypeManager.void_ptr_type)
1258 return new EmptyCast (expr, target_type);
1262 if (expr_type == TypeManager.anonymous_method_type){
1263 if (!TypeManager.IsDelegateType (target_type)){
1264 Report.Error (1660, loc,
1265 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1266 TypeManager.CSharpName (target_type));
1270 AnonymousMethod am = (AnonymousMethod) expr;
1271 int errors = Report.Errors;
1273 Expression conv = am.Compatible (ec, target_type, false);
1278 // We return something instead of null, to avoid
1279 // the duplicate error, since am.Compatible would have
1280 // reported that already
1282 if (errors != Report.Errors)
1283 return new EmptyCast (expr, target_type);
1290 /// Attemps to perform an implict constant conversion of the IntConstant
1291 /// into a different data type using casts (See Implicit Constant
1292 /// Expression Conversions)
1294 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1296 int value = ic.Value;
1298 if (target_type == TypeManager.sbyte_type){
1299 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1300 return new SByteConstant ((sbyte) value);
1301 } else if (target_type == TypeManager.byte_type){
1302 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1303 return new ByteConstant ((byte) value);
1304 } else if (target_type == TypeManager.short_type){
1305 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1306 return new ShortConstant ((short) value);
1307 } else if (target_type == TypeManager.ushort_type){
1308 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1309 return new UShortConstant ((ushort) value);
1310 } else if (target_type == TypeManager.uint32_type){
1312 return new UIntConstant ((uint) value);
1313 } else if (target_type == TypeManager.uint64_type){
1315 // we can optimize this case: a positive int32
1316 // always fits on a uint64. But we need an opcode
1320 return new ULongConstant ((ulong) value);
1321 } else if (target_type == TypeManager.double_type)
1322 return new DoubleConstant ((double) value);
1323 else if (target_type == TypeManager.float_type)
1324 return new FloatConstant ((float) value);
1326 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1327 Type underlying = TypeManager.EnumToUnderlying (target_type);
1328 Constant e = (Constant) ic;
1331 // Possibly, we need to create a different 0 literal before passing
1334 if (underlying == TypeManager.int64_type)
1335 e = new LongLiteral (0);
1336 else if (underlying == TypeManager.uint64_type)
1337 e = new ULongLiteral (0);
1339 return new EnumConstant (e, target_type);
1344 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1346 if (source.Name == target.Name){
1347 Report.ExtraInformation (loc,
1349 "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
1350 source.Assembly.FullName, target.Assembly.FullName));
1353 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1354 source == TypeManager.anonymous_method_type ?
1355 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1356 TypeManager.CSharpName (target));
1360 /// Attemptes to implicityly convert `target' into `type', using
1361 /// ImplicitConversion. If there is no implicit conversion, then
1362 /// an error is signaled
1364 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1365 Type target_type, Location loc)
1369 int errors = Report.Errors;
1370 e = ImplicitConversion (ec, source, target_type, loc);
1371 if (Report.Errors > errors)
1376 if (source is DoubleLiteral) {
1377 if (target_type == TypeManager.float_type) {
1378 Error_664 (loc, "float", "f");
1381 if (target_type == TypeManager.decimal_type) {
1382 Error_664 (loc, "decimal", "m");
1387 if (source is Constant){
1388 Constant c = (Constant) source;
1390 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1394 Error_CannotImplicitConversion (loc, source.Type, target_type);
1399 static void Error_664 (Location loc, string type, string suffix) {
1400 Report.Error (664, loc,
1401 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1406 /// Performs the explicit numeric conversions
1408 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1410 Type expr_type = expr.Type;
1413 // If we have an enumeration, extract the underlying type,
1414 // use this during the comparison, but wrap around the original
1417 Type real_target_type = target_type;
1419 if (TypeManager.IsEnumType (real_target_type))
1420 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1422 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1423 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1425 if (real_target_type != target_type)
1426 return new EmptyCast (ce, target_type);
1430 if (expr_type == TypeManager.sbyte_type){
1432 // From sbyte to byte, ushort, uint, ulong, char
1434 if (real_target_type == TypeManager.byte_type)
1435 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1436 if (real_target_type == TypeManager.ushort_type)
1437 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1438 if (real_target_type == TypeManager.uint32_type)
1439 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1440 if (real_target_type == TypeManager.uint64_type)
1441 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1442 if (real_target_type == TypeManager.char_type)
1443 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1444 } else if (expr_type == TypeManager.byte_type){
1446 // From byte to sbyte and char
1448 if (real_target_type == TypeManager.sbyte_type)
1449 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1450 if (real_target_type == TypeManager.char_type)
1451 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1452 } else if (expr_type == TypeManager.short_type){
1454 // From short to sbyte, byte, ushort, uint, ulong, char
1456 if (real_target_type == TypeManager.sbyte_type)
1457 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1458 if (real_target_type == TypeManager.byte_type)
1459 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1460 if (real_target_type == TypeManager.ushort_type)
1461 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1462 if (real_target_type == TypeManager.uint32_type)
1463 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1464 if (real_target_type == TypeManager.uint64_type)
1465 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1466 if (real_target_type == TypeManager.char_type)
1467 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1468 } else if (expr_type == TypeManager.ushort_type){
1470 // From ushort to sbyte, byte, short, char
1472 if (real_target_type == TypeManager.sbyte_type)
1473 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1474 if (real_target_type == TypeManager.byte_type)
1475 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1476 if (real_target_type == TypeManager.short_type)
1477 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1478 if (real_target_type == TypeManager.char_type)
1479 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1480 } else if (expr_type == TypeManager.int32_type){
1482 // From int to sbyte, byte, short, ushort, uint, ulong, char
1484 if (real_target_type == TypeManager.sbyte_type)
1485 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1486 if (real_target_type == TypeManager.byte_type)
1487 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1488 if (real_target_type == TypeManager.short_type)
1489 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1490 if (real_target_type == TypeManager.ushort_type)
1491 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1492 if (real_target_type == TypeManager.uint32_type)
1493 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1494 if (real_target_type == TypeManager.uint64_type)
1495 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1496 if (real_target_type == TypeManager.char_type)
1497 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1498 } else if (expr_type == TypeManager.uint32_type){
1500 // From uint to sbyte, byte, short, ushort, int, char
1502 if (real_target_type == TypeManager.sbyte_type)
1503 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1504 if (real_target_type == TypeManager.byte_type)
1505 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1506 if (real_target_type == TypeManager.short_type)
1507 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1508 if (real_target_type == TypeManager.ushort_type)
1509 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1510 if (real_target_type == TypeManager.int32_type)
1511 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1512 if (real_target_type == TypeManager.char_type)
1513 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1514 } else if (expr_type == TypeManager.int64_type){
1516 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1518 if (real_target_type == TypeManager.sbyte_type)
1519 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1520 if (real_target_type == TypeManager.byte_type)
1521 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1522 if (real_target_type == TypeManager.short_type)
1523 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1524 if (real_target_type == TypeManager.ushort_type)
1525 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1526 if (real_target_type == TypeManager.int32_type)
1527 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1528 if (real_target_type == TypeManager.uint32_type)
1529 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1530 if (real_target_type == TypeManager.uint64_type)
1531 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1532 if (real_target_type == TypeManager.char_type)
1533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1534 } else if (expr_type == TypeManager.uint64_type){
1536 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1538 if (real_target_type == TypeManager.sbyte_type)
1539 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1540 if (real_target_type == TypeManager.byte_type)
1541 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1542 if (real_target_type == TypeManager.short_type)
1543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1544 if (real_target_type == TypeManager.ushort_type)
1545 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1546 if (real_target_type == TypeManager.int32_type)
1547 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1548 if (real_target_type == TypeManager.uint32_type)
1549 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1550 if (real_target_type == TypeManager.int64_type)
1551 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1552 if (real_target_type == TypeManager.char_type)
1553 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1554 } else if (expr_type == TypeManager.char_type){
1556 // From char to sbyte, byte, short
1558 if (real_target_type == TypeManager.sbyte_type)
1559 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1560 if (real_target_type == TypeManager.byte_type)
1561 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1562 if (real_target_type == TypeManager.short_type)
1563 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1564 } else if (expr_type == TypeManager.float_type){
1566 // From float to sbyte, byte, short,
1567 // ushort, int, uint, long, ulong, char
1570 if (real_target_type == TypeManager.sbyte_type)
1571 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1572 if (real_target_type == TypeManager.byte_type)
1573 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1574 if (real_target_type == TypeManager.short_type)
1575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1576 if (real_target_type == TypeManager.ushort_type)
1577 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1578 if (real_target_type == TypeManager.int32_type)
1579 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1580 if (real_target_type == TypeManager.uint32_type)
1581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1582 if (real_target_type == TypeManager.int64_type)
1583 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1584 if (real_target_type == TypeManager.uint64_type)
1585 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1586 if (real_target_type == TypeManager.char_type)
1587 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1588 } else if (expr_type == TypeManager.double_type){
1590 // From double to byte, byte, short,
1591 // ushort, int, uint, long, ulong,
1592 // char, float or decimal
1594 if (real_target_type == TypeManager.sbyte_type)
1595 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1596 if (real_target_type == TypeManager.byte_type)
1597 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1598 if (real_target_type == TypeManager.short_type)
1599 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1600 if (real_target_type == TypeManager.ushort_type)
1601 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1602 if (real_target_type == TypeManager.int32_type)
1603 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1604 if (real_target_type == TypeManager.uint32_type)
1605 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1606 if (real_target_type == TypeManager.int64_type)
1607 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1608 if (real_target_type == TypeManager.uint64_type)
1609 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1610 if (real_target_type == TypeManager.char_type)
1611 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1612 if (real_target_type == TypeManager.float_type)
1613 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1616 // decimal is taken care of by the op_Explicit methods.
1622 /// Returns whether an explicit reference conversion can be performed
1623 /// from source_type to target_type
1625 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1627 bool target_is_type_param = target_type.IsGenericParameter;
1628 bool target_is_value_type = target_type.IsValueType;
1630 if (source_type == target_type)
1634 // From object to a generic parameter
1636 if (source_type == TypeManager.object_type && target_is_type_param)
1640 // From object to any reference type
1642 if (source_type == TypeManager.object_type && !target_is_value_type)
1646 // From any class S to any class-type T, provided S is a base class of T
1648 if (TypeManager.IsSubclassOf (target_type, source_type))
1652 // From any interface type S to any interface T provided S is not derived from T
1654 if (source_type.IsInterface && target_type.IsInterface){
1655 if (!TypeManager.IsSubclassOf (target_type, source_type))
1660 // From any class type S to any interface T, provided S is not sealed
1661 // and provided S does not implement T.
1663 if (target_type.IsInterface && !source_type.IsSealed &&
1664 !TypeManager.ImplementsInterface (source_type, target_type))
1668 // From any interface-type S to to any class type T, provided T is not
1669 // sealed, or provided T implements S.
1671 if (source_type.IsInterface &&
1672 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1676 // From an array type S with an element type Se to an array type T with an
1677 // element type Te provided all the following are true:
1678 // * S and T differe only in element type, in other words, S and T
1679 // have the same number of dimensions.
1680 // * Both Se and Te are reference types
1681 // * An explicit referenc conversions exist from Se to Te
1683 if (source_type.IsArray && target_type.IsArray) {
1684 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1686 Type source_element_type = TypeManager.GetElementType (source_type);
1687 Type target_element_type = TypeManager.GetElementType (target_type);
1689 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1690 if (ExplicitReferenceConversionExists (source_element_type,
1691 target_element_type))
1697 // From System.Array to any array-type
1698 if (source_type == TypeManager.array_type &&
1699 target_type.IsArray){
1704 // From System delegate to any delegate-type
1706 if (source_type == TypeManager.delegate_type &&
1707 TypeManager.IsDelegateType (target_type))
1711 // From ICloneable to Array or Delegate types
1713 if (source_type == TypeManager.icloneable_type &&
1714 (target_type == TypeManager.array_type ||
1715 target_type == TypeManager.delegate_type))
1722 /// Implements Explicit Reference conversions
1724 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1726 Type source_type = source.Type;
1727 bool target_is_type_param = target_type.IsGenericParameter;
1728 bool target_is_value_type = target_type.IsValueType;
1731 // From object to a generic parameter
1733 if (source_type == TypeManager.object_type && target_is_type_param)
1734 return new UnboxCast (source, target_type);
1737 // From object to any reference type
1739 if (source_type == TypeManager.object_type && !target_is_value_type)
1740 return new ClassCast (source, target_type);
1743 // Unboxing conversion.
1745 if (((source_type == TypeManager.enum_type &&
1746 !(source is EmptyCast)) ||
1747 source_type == TypeManager.value_type) && target_is_value_type)
1748 return new UnboxCast (source, target_type);
1751 // From any class S to any class-type T, provided S is a base class of T
1753 if (TypeManager.IsSubclassOf (target_type, source_type))
1754 return new ClassCast (source, target_type);
1757 // From any interface type S to any interface T provided S is not derived from T
1759 if (source_type.IsInterface && target_type.IsInterface){
1760 if (TypeManager.ImplementsInterface (source_type, target_type))
1763 return new ClassCast (source, target_type);
1767 // From any class type S to any interface T, provides S is not sealed
1768 // and provided S does not implement T.
1770 if (target_type.IsInterface && !source_type.IsSealed) {
1771 if (TypeManager.ImplementsInterface (source_type, target_type))
1774 return new ClassCast (source, target_type);
1779 // From any interface-type S to to any class type T, provided T is not
1780 // sealed, or provided T implements S.
1782 if (source_type.IsInterface) {
1783 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1784 if (target_type.IsClass)
1785 return new ClassCast (source, target_type);
1787 return new UnboxCast (source, target_type);
1793 // From an array type S with an element type Se to an array type T with an
1794 // element type Te provided all the following are true:
1795 // * S and T differe only in element type, in other words, S and T
1796 // have the same number of dimensions.
1797 // * Both Se and Te are reference types
1798 // * An explicit referenc conversions exist from Se to Te
1800 if (source_type.IsArray && target_type.IsArray) {
1801 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1803 Type source_element_type = TypeManager.GetElementType (source_type);
1804 Type target_element_type = TypeManager.GetElementType (target_type);
1806 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1807 if (ExplicitReferenceConversionExists (source_element_type,
1808 target_element_type))
1809 return new ClassCast (source, target_type);
1814 // From System.Array to any array-type
1815 if (source_type == TypeManager.array_type &&
1816 target_type.IsArray) {
1817 return new ClassCast (source, target_type);
1821 // From System delegate to any delegate-type
1823 if (source_type == TypeManager.delegate_type &&
1824 TypeManager.IsDelegateType (target_type))
1825 return new ClassCast (source, target_type);
1828 // From ICloneable to Array or Delegate types
1830 if (source_type == TypeManager.icloneable_type &&
1831 (target_type == TypeManager.array_type ||
1832 target_type == TypeManager.delegate_type))
1833 return new ClassCast (source, target_type);
1839 /// Performs an explicit conversion of the expression `expr' whose
1840 /// type is expr.Type to `target_type'.
1842 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1843 Type target_type, Location loc)
1845 Type expr_type = expr.Type;
1846 Type original_expr_type = expr_type;
1848 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1849 if (target_type == TypeManager.enum_type ||
1850 target_type == TypeManager.object_type) {
1851 if (expr is EnumConstant)
1852 expr = ((EnumConstant) expr).Child;
1853 // We really need all these casts here .... :-(
1854 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1855 return new EmptyCast (expr, target_type);
1856 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1857 target_type.IsSubclassOf (TypeManager.enum_type))
1858 return new UnboxCast (expr, target_type);
1861 // Notice that we have kept the expr_type unmodified, which is only
1863 if (expr is EnumConstant)
1864 expr = ((EnumConstant) expr).Child;
1866 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1867 expr_type = expr.Type;
1870 int errors = Report.Errors;
1871 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1872 if (Report.Errors > errors)
1878 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1883 // Unboxing conversion.
1885 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1886 return new UnboxCast (expr, target_type);
1889 // Skip the ExplicitReferenceConversion because we can not convert
1890 // from Null to a ValueType, and ExplicitReference wont check against
1891 // null literal explicitly
1893 if (expr_type != TypeManager.null_type){
1894 ne = ExplicitReferenceConversion (expr, target_type);
1901 if (target_type.IsPointer){
1902 if (expr_type.IsPointer)
1903 return new EmptyCast (expr, target_type);
1905 if (expr_type == TypeManager.sbyte_type ||
1906 expr_type == TypeManager.byte_type ||
1907 expr_type == TypeManager.short_type ||
1908 expr_type == TypeManager.ushort_type ||
1909 expr_type == TypeManager.int32_type ||
1910 expr_type == TypeManager.uint32_type ||
1911 expr_type == TypeManager.uint64_type ||
1912 expr_type == TypeManager.int64_type)
1913 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1915 if (expr_type.IsPointer){
1916 Expression e = null;
1918 if (target_type == TypeManager.sbyte_type)
1919 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1920 else if (target_type == TypeManager.byte_type)
1921 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1922 else if (target_type == TypeManager.short_type)
1923 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1924 else if (target_type == TypeManager.ushort_type)
1925 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1926 else if (target_type == TypeManager.int32_type)
1927 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1928 else if (target_type == TypeManager.uint32_type)
1929 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1930 else if (target_type == TypeManager.uint64_type)
1931 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1932 else if (target_type == TypeManager.int64_type){
1933 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1939 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1944 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1948 // We should always be able to go from an uint32
1949 // implicitly or explicitly to the other integral
1952 throw new Exception ("Internal compiler error");
1957 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1961 if (expr is NullLiteral){
1962 Report.Error (37, loc, "Cannot convert null to value type `" +
1963 TypeManager.CSharpName (target_type) + "'");
1967 Error_CannotConvertType (loc, original_expr_type, target_type);
1972 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1974 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1975 Type target_type, Location l)
1977 int errors = Report.Errors;
1978 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1979 if (Report.Errors > errors)
1985 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1989 ne = ExplicitReferenceConversion (expr, target_type);
1993 Error_CannotConvertType (l, expr.Type, target_type);