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 {
22 static void Error_CannotConvertType (Location loc, Type source, Type target)
24 Report.Error (30, loc, "Cannot convert type '" +
25 TypeManager.CSharpName (source) + "' to '" +
26 TypeManager.CSharpName (target) + "'");
29 static Expression TypeParameter_to_Null (Expression expr, Type target_type,
32 if (!TypeParameter_to_Null (target_type)) {
33 Report.Error (403, loc, "Cannot convert null to the type " +
34 "parameter `{0}' becaues it could be a value " +
35 "type. Consider using `default ({0})' instead.",
40 return new NullCast (expr, target_type);
43 static bool TypeParameter_to_Null (Type target_type)
45 if ((target_type.BaseType == null) ||
46 (target_type.BaseType == TypeManager.value_type) ||
47 target_type.BaseType.IsValueType)
53 static EmptyExpression MyEmptyExpr;
54 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
56 Type expr_type = expr.Type;
58 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
59 // if we are a method group, emit a warning
64 if (expr_type == TypeManager.void_type)
68 // notice that it is possible to write "ValueType v = 1", the ValueType here
69 // is an abstract class, and not really a value type, so we apply the same rules.
71 if (target_type == TypeManager.object_type) {
73 // A pointer type cannot be converted to object
75 if (expr_type.IsPointer)
78 if (TypeManager.IsValueType (expr_type))
79 return new BoxedCast (expr);
80 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
81 return new EmptyCast (expr, target_type);
84 } else if (target_type == TypeManager.value_type) {
85 if (TypeManager.IsValueType (expr_type))
86 return new BoxedCast (expr);
87 if (expr is NullLiteral)
88 return new NullCast (expr, target_type);
91 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
93 // Special case: enumeration to System.Enum.
94 // System.Enum is not a value type, it is a class, so we need
95 // a boxing conversion
97 if (expr_type.IsEnum || expr_type.IsGenericParameter)
98 return new BoxedCast (expr);
100 return new EmptyCast (expr, target_type);
103 // This code is kind of mirrored inside ImplicitStandardConversionExists
104 // with the small distinction that we only probe there
106 // Always ensure that the code here and there is in sync
108 // from the null type to any reference-type.
109 if (expr is NullLiteral){
110 if (target_type.IsPointer)
111 return NullPointer.Null;
113 if (!target_type.IsValueType)
114 return new NullCast (expr, target_type);
117 // from any class-type S to any interface-type T.
118 if (target_type.IsInterface) {
119 if (target_type != TypeManager.iconvertible_type &&
120 expr_type.IsValueType && (expr is Constant) &&
121 !(expr is IntLiteral || expr is BoolLiteral ||
122 expr is FloatLiteral || expr is DoubleLiteral ||
123 expr is LongLiteral || expr is CharLiteral ||
124 expr is StringLiteral || expr is DecimalLiteral ||
125 expr is UIntLiteral || expr is ULongLiteral)) {
129 if (TypeManager.ImplementsInterface (expr_type, target_type)){
130 if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
131 return new BoxedCast (expr, target_type);
133 return new EmptyCast (expr, target_type);
137 // from any interface type S to interface-type T.
138 if (expr_type.IsInterface && target_type.IsInterface) {
139 if (TypeManager.ImplementsInterface (expr_type, target_type))
140 return new EmptyCast (expr, target_type);
145 // from an array-type S to an array-type of type T
146 if (expr_type.IsArray && target_type.IsArray) {
147 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
149 Type expr_element_type = TypeManager.GetElementType (expr_type);
151 if (MyEmptyExpr == null)
152 MyEmptyExpr = new EmptyExpression ();
154 MyEmptyExpr.SetType (expr_element_type);
155 Type target_element_type = TypeManager.GetElementType (target_type);
157 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
158 if (ImplicitStandardConversionExists (MyEmptyExpr,
159 target_element_type))
160 return new EmptyCast (expr, target_type);
164 // from an array-type to System.Array
165 if (expr_type.IsArray && target_type == TypeManager.array_type)
166 return new EmptyCast (expr, target_type);
168 // from any delegate type to System.Delegate
169 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
170 target_type == TypeManager.delegate_type)
171 return new EmptyCast (expr, target_type);
173 // from any array-type or delegate type into System.ICloneable.
174 if (expr_type.IsArray ||
175 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
176 if (target_type == TypeManager.icloneable_type)
177 return new EmptyCast (expr, target_type);
179 // from a generic type definition to a generic instance.
180 if (TypeManager.IsEqualGenericType (expr_type, target_type))
181 return new EmptyCast (expr, target_type);
183 if (expr_type.IsGenericParameter) {
184 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
186 // We're converting from a type parameter which is known to be a reference type.
187 if ((gc != null) && gc.IsReferenceType) {
188 if (gc.HasClassConstraint &&
189 TypeManager.IsSubclassOf (gc.ClassConstraint, target_type))
190 return new EmptyCast (expr, target_type);
192 foreach (Type t in gc.InterfaceConstraints) {
193 if (TypeManager.IsSubclassOf (t, target_type))
194 return new EmptyCast (expr, target_type);
203 // Tests whether an implicit reference conversion exists between expr_type
206 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
208 Type expr_type = expr.Type;
211 // This is the boxed case.
213 if (target_type == TypeManager.object_type) {
214 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
215 expr_type.IsInterface || expr_type == TypeManager.enum_type)
219 } else if (TypeManager.IsSubclassOf (expr_type, target_type))
222 // Please remember that all code below actually comes
223 // from ImplicitReferenceConversion so make sure code remains in sync
225 // from any class-type S to any interface-type T.
226 if (target_type.IsInterface) {
227 if (TypeManager.ImplementsInterface (expr_type, target_type))
231 // from any interface type S to interface-type T.
232 if (expr_type.IsInterface && target_type.IsInterface)
233 if (TypeManager.ImplementsInterface (expr_type, target_type))
236 // from an array-type S to an array-type of type T
237 if (expr_type.IsArray && target_type.IsArray) {
238 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
240 Type expr_element_type = expr_type.GetElementType ();
242 if (MyEmptyExpr == null)
243 MyEmptyExpr = new EmptyExpression ();
245 MyEmptyExpr.SetType (expr_element_type);
246 Type target_element_type = TypeManager.GetElementType (target_type);
248 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
249 if (ImplicitStandardConversionExists (MyEmptyExpr,
250 target_element_type))
255 // from an array-type to System.Array
256 if (expr_type.IsArray && (target_type == TypeManager.array_type))
259 // from any delegate type to System.Delegate
260 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
261 target_type == TypeManager.delegate_type)
262 if (target_type.IsAssignableFrom (expr_type))
265 // from any array-type or delegate type into System.ICloneable.
266 if (expr_type.IsArray ||
267 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
268 if (target_type == TypeManager.icloneable_type)
271 // from the null type to any reference-type.
272 if (expr is NullLiteral && !target_type.IsValueType && !TypeManager.IsEnumType (target_type))
275 // from a generic type definition to a generic instance.
276 if (TypeManager.IsEqualGenericType (expr_type, target_type))
279 if (expr_type.IsGenericParameter) {
280 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
282 // We're converting from a type parameter which is known to be a reference type.
283 if ((gc != null) && gc.IsReferenceType) {
284 if (gc.HasClassConstraint &&
285 TypeManager.IsSubclassOf (gc.ClassConstraint, target_type))
288 foreach (Type t in gc.InterfaceConstraints) {
289 if (TypeManager.IsSubclassOf (t, target_type))
299 /// Implicit Numeric Conversions.
301 /// expr is the expression to convert, returns a new expression of type
302 /// target_type or null if an implicit conversion is not possible.
304 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
305 Type target_type, Location loc)
307 Type expr_type = expr.Type;
310 // Attempt to do the implicit constant expression conversions
312 if (expr is Constant){
313 if (expr is IntConstant){
316 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
320 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
322 // Try the implicit constant expression conversion
323 // from long to ulong, instead of a nice routine,
326 long v = ((LongConstant) expr).Value;
328 return new ULongConstant ((ulong) v);
332 Type real_target_type = target_type;
334 if (expr_type == TypeManager.sbyte_type){
336 // From sbyte to short, int, long, float, double.
338 if (real_target_type == TypeManager.int32_type)
339 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
340 if (real_target_type == TypeManager.int64_type)
341 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
342 if (real_target_type == TypeManager.double_type)
343 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
344 if (real_target_type == TypeManager.float_type)
345 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
346 if (real_target_type == TypeManager.short_type)
347 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
348 } else if (expr_type == TypeManager.byte_type){
350 // From byte to short, ushort, int, uint, long, ulong, float, double
352 if ((real_target_type == TypeManager.short_type) ||
353 (real_target_type == TypeManager.ushort_type) ||
354 (real_target_type == TypeManager.int32_type) ||
355 (real_target_type == TypeManager.uint32_type))
356 return new EmptyCast (expr, target_type);
358 if (real_target_type == TypeManager.uint64_type)
359 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
360 if (real_target_type == TypeManager.int64_type)
361 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
362 if (real_target_type == TypeManager.float_type)
363 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
364 if (real_target_type == TypeManager.double_type)
365 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
366 } else if (expr_type == TypeManager.short_type){
368 // From short to int, long, float, double
370 if (real_target_type == TypeManager.int32_type)
371 return new EmptyCast (expr, target_type);
372 if (real_target_type == TypeManager.int64_type)
373 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
374 if (real_target_type == TypeManager.double_type)
375 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
376 if (real_target_type == TypeManager.float_type)
377 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
378 } else if (expr_type == TypeManager.ushort_type){
380 // From ushort to int, uint, long, ulong, float, double
382 if (real_target_type == TypeManager.uint32_type)
383 return new EmptyCast (expr, target_type);
385 if (real_target_type == TypeManager.uint64_type)
386 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
387 if (real_target_type == TypeManager.int32_type)
388 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
389 if (real_target_type == TypeManager.int64_type)
390 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
391 if (real_target_type == TypeManager.double_type)
392 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
393 if (real_target_type == TypeManager.float_type)
394 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
395 } else if (expr_type == TypeManager.int32_type){
397 // From int to long, float, double
399 if (real_target_type == TypeManager.int64_type)
400 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
401 if (real_target_type == TypeManager.double_type)
402 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
403 if (real_target_type == TypeManager.float_type)
404 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
405 } else if (expr_type == TypeManager.uint32_type){
407 // From uint to long, ulong, float, double
409 if (real_target_type == TypeManager.int64_type)
410 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
411 if (real_target_type == TypeManager.uint64_type)
412 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
413 if (real_target_type == TypeManager.double_type)
414 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
416 if (real_target_type == TypeManager.float_type)
417 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
419 } else if (expr_type == TypeManager.int64_type){
421 // From long/ulong to float, double
423 if (real_target_type == TypeManager.double_type)
424 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
425 if (real_target_type == TypeManager.float_type)
426 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
427 } else if (expr_type == TypeManager.uint64_type){
429 // From ulong to float, double
431 if (real_target_type == TypeManager.double_type)
432 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
434 if (real_target_type == TypeManager.float_type)
435 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
437 } else if (expr_type == TypeManager.char_type){
439 // From char to ushort, int, uint, long, ulong, float, double
441 if ((real_target_type == TypeManager.ushort_type) ||
442 (real_target_type == TypeManager.int32_type) ||
443 (real_target_type == TypeManager.uint32_type))
444 return new EmptyCast (expr, target_type);
445 if (real_target_type == TypeManager.uint64_type)
446 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
447 if (real_target_type == TypeManager.int64_type)
448 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
449 if (real_target_type == TypeManager.float_type)
450 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
451 if (real_target_type == TypeManager.double_type)
452 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
453 } else if (expr_type == TypeManager.float_type){
457 if (real_target_type == TypeManager.double_type)
458 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
466 /// Same as ImplicitStandardConversionExists except that it also looks at
467 /// implicit user defined conversions - needed for overload resolution
469 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
471 if ((expr is NullLiteral) && target_type.IsGenericParameter)
472 return TypeParameter_to_Null (target_type);
474 if (ImplicitStandardConversionExists (expr, target_type))
477 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
485 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
487 Expression dummy = ImplicitUserConversion (
488 ec, new EmptyExpression (source), target, Location.Null);
489 return dummy != null;
493 /// Determines if a standard implicit conversion exists from
494 /// expr_type to target_type
496 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
498 Type expr_type = expr.Type;
500 if (expr_type == TypeManager.void_type)
503 //Console.WriteLine ("Expr is {0}", expr);
504 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
505 if (expr_type.Equals (target_type))
508 // First numeric conversions
510 if (expr_type == TypeManager.sbyte_type){
512 // From sbyte to short, int, long, float, double.
514 if ((target_type == TypeManager.int32_type) ||
515 (target_type == TypeManager.int64_type) ||
516 (target_type == TypeManager.double_type) ||
517 (target_type == TypeManager.float_type) ||
518 (target_type == TypeManager.short_type) ||
519 (target_type == TypeManager.decimal_type))
522 } else if (expr_type == TypeManager.byte_type){
524 // From byte to short, ushort, int, uint, long, ulong, float, double
526 if ((target_type == TypeManager.short_type) ||
527 (target_type == TypeManager.ushort_type) ||
528 (target_type == TypeManager.int32_type) ||
529 (target_type == TypeManager.uint32_type) ||
530 (target_type == TypeManager.uint64_type) ||
531 (target_type == TypeManager.int64_type) ||
532 (target_type == TypeManager.float_type) ||
533 (target_type == TypeManager.double_type) ||
534 (target_type == TypeManager.decimal_type))
537 } else if (expr_type == TypeManager.short_type){
539 // From short to int, long, float, double
541 if ((target_type == TypeManager.int32_type) ||
542 (target_type == TypeManager.int64_type) ||
543 (target_type == TypeManager.double_type) ||
544 (target_type == TypeManager.float_type) ||
545 (target_type == TypeManager.decimal_type))
548 } else if (expr_type == TypeManager.ushort_type){
550 // From ushort to int, uint, long, ulong, float, double
552 if ((target_type == TypeManager.uint32_type) ||
553 (target_type == TypeManager.uint64_type) ||
554 (target_type == TypeManager.int32_type) ||
555 (target_type == TypeManager.int64_type) ||
556 (target_type == TypeManager.double_type) ||
557 (target_type == TypeManager.float_type) ||
558 (target_type == TypeManager.decimal_type))
561 } else if (expr_type == TypeManager.int32_type){
563 // From int to long, float, double
565 if ((target_type == TypeManager.int64_type) ||
566 (target_type == TypeManager.double_type) ||
567 (target_type == TypeManager.float_type) ||
568 (target_type == TypeManager.decimal_type))
571 } else if (expr_type == TypeManager.uint32_type){
573 // From uint to long, ulong, float, double
575 if ((target_type == TypeManager.int64_type) ||
576 (target_type == TypeManager.uint64_type) ||
577 (target_type == TypeManager.double_type) ||
578 (target_type == TypeManager.float_type) ||
579 (target_type == TypeManager.decimal_type))
582 } else if ((expr_type == TypeManager.uint64_type) ||
583 (expr_type == TypeManager.int64_type)) {
585 // From long/ulong to float, double
587 if ((target_type == TypeManager.double_type) ||
588 (target_type == TypeManager.float_type) ||
589 (target_type == TypeManager.decimal_type))
592 } else if (expr_type == TypeManager.char_type){
594 // From char to ushort, int, uint, long, ulong, float, double
596 if ((target_type == TypeManager.ushort_type) ||
597 (target_type == TypeManager.int32_type) ||
598 (target_type == TypeManager.uint32_type) ||
599 (target_type == TypeManager.uint64_type) ||
600 (target_type == TypeManager.int64_type) ||
601 (target_type == TypeManager.float_type) ||
602 (target_type == TypeManager.double_type) ||
603 (target_type == TypeManager.decimal_type))
606 } else if (expr_type == TypeManager.float_type){
610 if (target_type == TypeManager.double_type)
614 if (ImplicitReferenceConversionExists (expr, target_type))
618 // Implicit Constant Expression Conversions
620 if (expr is IntConstant){
621 int value = ((IntConstant) expr).Value;
623 if (target_type == TypeManager.sbyte_type){
624 if (value >= SByte.MinValue && value <= SByte.MaxValue)
626 } else if (target_type == TypeManager.byte_type){
627 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
629 } else if (target_type == TypeManager.short_type){
630 if (value >= Int16.MinValue && value <= Int16.MaxValue)
632 } else if (target_type == TypeManager.ushort_type){
633 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
635 } else if (target_type == TypeManager.uint32_type){
638 } else if (target_type == TypeManager.uint64_type){
640 // we can optimize this case: a positive int32
641 // always fits on a uint64. But we need an opcode
648 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
652 if (expr is LongConstant && target_type == TypeManager.uint64_type){
654 // Try the implicit constant expression conversion
655 // from long to ulong, instead of a nice routine,
658 long v = ((LongConstant) expr).Value;
663 if ((target_type == TypeManager.enum_type ||
664 target_type.IsSubclassOf (TypeManager.enum_type)) &&
666 IntLiteral i = (IntLiteral) expr;
672 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
679 // Used internally by FindMostEncompassedType, this is used
680 // to avoid creating lots of objects in the tight loop inside
681 // FindMostEncompassedType
683 static EmptyExpression priv_fmet_param;
686 /// Finds "most encompassed type" according to the spec (13.4.2)
687 /// amongst the methods in the MethodGroupExpr
689 static Type FindMostEncompassedType (ArrayList types)
693 if (priv_fmet_param == null)
694 priv_fmet_param = new EmptyExpression ();
696 foreach (Type t in types){
697 priv_fmet_param.SetType (t);
704 if (ImplicitStandardConversionExists (priv_fmet_param, best))
712 // Used internally by FindMostEncompassingType, this is used
713 // to avoid creating lots of objects in the tight loop inside
714 // FindMostEncompassingType
716 static EmptyExpression priv_fmee_ret;
719 /// Finds "most encompassing type" according to the spec (13.4.2)
720 /// amongst the types in the given set
722 static Type FindMostEncompassingType (ArrayList types)
726 if (priv_fmee_ret == null)
727 priv_fmee_ret = new EmptyExpression ();
729 foreach (Type t in types){
730 priv_fmee_ret.SetType (best);
737 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
745 // Used to avoid creating too many objects
747 static EmptyExpression priv_fms_expr;
750 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
751 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
752 /// for explicit and implicit conversion operators.
754 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
755 bool apply_explicit_conv_rules,
758 ArrayList src_types_set = new ArrayList ();
760 if (priv_fms_expr == null)
761 priv_fms_expr = new EmptyExpression ();
764 // If any operator converts from S then Sx = S
766 Type source_type = source.Type;
767 foreach (MethodBase mb in me.Methods){
768 ParameterData pd = Invocation.GetParameterData (mb);
769 Type param_type = pd.ParameterType (0);
771 if (param_type == source_type)
774 if (apply_explicit_conv_rules) {
777 // Find the set of applicable user-defined conversion operators, U. This set
779 // user-defined implicit or explicit conversion operators declared by
780 // the classes or structs in D that convert from a type encompassing
781 // or encompassed by S to a type encompassing or encompassed by T
783 priv_fms_expr.SetType (param_type);
784 if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
785 src_types_set.Add (param_type);
787 if (ImplicitStandardConversionExists (source, param_type))
788 src_types_set.Add (param_type);
792 // Only if S is encompassed by param_type
794 if (ImplicitStandardConversionExists (source, param_type))
795 src_types_set.Add (param_type);
800 // Explicit Conv rules
802 if (apply_explicit_conv_rules) {
803 ArrayList candidate_set = new ArrayList ();
805 foreach (Type param_type in src_types_set){
806 if (ImplicitStandardConversionExists (source, param_type))
807 candidate_set.Add (param_type);
810 if (candidate_set.Count != 0)
811 return FindMostEncompassedType (candidate_set);
817 if (apply_explicit_conv_rules)
818 return FindMostEncompassingType (src_types_set);
820 return FindMostEncompassedType (src_types_set);
824 // Useful in avoiding proliferation of objects
826 static EmptyExpression priv_fmt_expr;
829 /// Finds the most specific target Tx according to section 13.4.4
831 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
832 bool apply_explicit_conv_rules,
835 ArrayList tgt_types_set = new ArrayList ();
837 if (priv_fmt_expr == null)
838 priv_fmt_expr = new EmptyExpression ();
841 // If any operator converts to T then Tx = T
843 foreach (MethodInfo mi in me.Methods){
844 Type ret_type = mi.ReturnType;
846 if (ret_type == target)
849 if (apply_explicit_conv_rules) {
852 // Find the set of applicable user-defined conversion operators, U.
854 // This set consists of the
855 // user-defined implicit or explicit conversion operators declared by
856 // the classes or structs in D that convert from a type encompassing
857 // or encompassed by S to a type encompassing or encompassed by T
859 priv_fms_expr.SetType (ret_type);
860 if (ImplicitStandardConversionExists (priv_fms_expr, target))
861 tgt_types_set.Add (ret_type);
863 priv_fms_expr.SetType (target);
864 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
865 tgt_types_set.Add (ret_type);
869 // Only if T is encompassed by param_type
871 priv_fms_expr.SetType (ret_type);
872 if (ImplicitStandardConversionExists (priv_fms_expr, target))
873 tgt_types_set.Add (ret_type);
878 // Explicit conv rules
880 if (apply_explicit_conv_rules) {
881 ArrayList candidate_set = new ArrayList ();
883 foreach (Type ret_type in tgt_types_set){
884 priv_fmt_expr.SetType (ret_type);
886 if (ImplicitStandardConversionExists (priv_fmt_expr, target))
887 candidate_set.Add (ret_type);
890 if (candidate_set.Count != 0)
891 return FindMostEncompassingType (candidate_set);
895 // Okay, final case !
897 if (apply_explicit_conv_rules)
898 return FindMostEncompassedType (tgt_types_set);
900 return FindMostEncompassingType (tgt_types_set);
904 /// User-defined Implicit conversions
906 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
907 Type target, Location loc)
909 return UserDefinedConversion (ec, source, target, loc, false);
913 /// User-defined Explicit conversions
915 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
916 Type target, Location loc)
918 return UserDefinedConversion (ec, source, target, loc, true);
921 static DoubleHash explicit_conv = new DoubleHash (100);
922 static DoubleHash implicit_conv = new DoubleHash (100);
924 /// Computes the MethodGroup for the user-defined conversion
925 /// operators from source_type to target_type. `look_for_explicit'
926 /// controls whether we should also include the list of explicit
929 static MethodGroupExpr GetConversionOperators (EmitContext ec,
930 Type source_type, Type target_type,
931 Location loc, bool look_for_explicit)
933 Expression mg1 = null, mg2 = null;
934 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
937 op_name = "op_Implicit";
939 MethodGroupExpr union3;
941 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
942 return (MethodGroupExpr) r;
944 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
945 if (source_type.BaseType != null)
946 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
949 union3 = (MethodGroupExpr) mg2;
950 else if (mg2 == null)
951 union3 = (MethodGroupExpr) mg1;
953 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
955 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
958 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
960 union3 = (MethodGroupExpr) mg1;
963 if (target_type.BaseType != null)
964 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
968 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
970 union3 = (MethodGroupExpr) mg1;
973 MethodGroupExpr union4 = null;
975 if (look_for_explicit) {
976 op_name = "op_Explicit";
978 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
979 if (source_type.BaseType != null)
980 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
982 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
983 if (target_type.BaseType != null)
984 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
986 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
987 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
989 union4 = Invocation.MakeUnionSet (union5, union6, loc);
992 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
993 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
998 /// User-defined conversions
1000 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1001 Type target, Location loc,
1002 bool look_for_explicit)
1004 MethodGroupExpr union;
1005 Type source_type = source.Type;
1006 MethodBase method = null;
1008 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1012 Type most_specific_source, most_specific_target;
1014 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1015 if (most_specific_source == null)
1018 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1019 if (most_specific_target == null)
1025 foreach (MethodBase mb in union.Methods){
1026 ParameterData pd = Invocation.GetParameterData (mb);
1027 MethodInfo mi = (MethodInfo) mb;
1029 if (pd.ParameterType (0) == most_specific_source &&
1030 mi.ReturnType == most_specific_target) {
1036 if (method == null || count > 1)
1041 // This will do the conversion to the best match that we
1042 // found. Now we need to perform an implict standard conversion
1043 // if the best match was not the type that we were requested
1046 if (look_for_explicit)
1047 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1049 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1055 e = new UserCast ((MethodInfo) method, source, loc);
1056 if (e.Type != target){
1057 if (!look_for_explicit)
1058 e = ImplicitConversionStandard (ec, e, target, loc);
1060 e = ExplicitConversionStandard (ec, e, target, loc);
1067 /// Converts implicitly the resolved expression `expr' into the
1068 /// `target_type'. It returns a new expression that can be used
1069 /// in a context that expects a `target_type'.
1071 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1072 Type target_type, Location loc)
1076 if (target_type == null)
1077 throw new Exception ("Target type is null");
1079 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1083 e = ImplicitUserConversion (ec, expr, target_type, loc);
1092 /// Attempts to apply the `Standard Implicit
1093 /// Conversion' rules to the expression `expr' into
1094 /// the `target_type'. It returns a new expression
1095 /// that can be used in a context that expects a
1098 /// This is different from `ImplicitConversion' in that the
1099 /// user defined implicit conversions are excluded.
1101 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1102 Type target_type, Location loc)
1104 Type expr_type = expr.Type;
1107 if ((expr is NullLiteral) && target_type.IsGenericParameter)
1108 return TypeParameter_to_Null (expr, target_type, loc);
1110 if (expr.eclass == ExprClass.MethodGroup){
1111 if (!TypeManager.IsDelegateType (target_type)){
1112 Report.Error (428, loc,
1114 "Cannot convert method group to `{0}', since it is not a delegate",
1115 TypeManager.CSharpName (target_type)));
1119 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1122 if (expr_type.Equals (target_type) && !(expr is NullLiteral))
1125 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1129 e = ImplicitReferenceConversion (expr, target_type);
1133 if ((target_type == TypeManager.enum_type ||
1134 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1135 expr is IntLiteral){
1136 IntLiteral i = (IntLiteral) expr;
1139 return new EnumConstant ((Constant) expr, target_type);
1143 if (expr_type.IsPointer){
1144 if (target_type == TypeManager.void_ptr_type)
1145 return new EmptyCast (expr, target_type);
1148 // yep, comparing pointer types cant be done with
1149 // t1 == t2, we have to compare their element types.
1151 if (target_type.IsPointer){
1152 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1157 if (target_type.IsPointer) {
1158 if (expr is NullLiteral)
1159 return new EmptyCast (expr, target_type);
1161 if (expr_type == TypeManager.void_ptr_type)
1162 return new EmptyCast (expr, target_type);
1170 /// Attemps to perform an implict constant conversion of the IntConstant
1171 /// into a different data type using casts (See Implicit Constant
1172 /// Expression Conversions)
1174 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1176 int value = ic.Value;
1178 if (target_type == TypeManager.sbyte_type){
1179 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1180 return new SByteConstant ((sbyte) value);
1181 } else if (target_type == TypeManager.byte_type){
1182 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1183 return new ByteConstant ((byte) value);
1184 } else if (target_type == TypeManager.short_type){
1185 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1186 return new ShortConstant ((short) value);
1187 } else if (target_type == TypeManager.ushort_type){
1188 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1189 return new UShortConstant ((ushort) value);
1190 } else if (target_type == TypeManager.uint32_type){
1192 return new UIntConstant ((uint) value);
1193 } else if (target_type == TypeManager.uint64_type){
1195 // we can optimize this case: a positive int32
1196 // always fits on a uint64. But we need an opcode
1200 return new ULongConstant ((ulong) value);
1201 } else if (target_type == TypeManager.double_type)
1202 return new DoubleConstant ((double) value);
1203 else if (target_type == TypeManager.float_type)
1204 return new FloatConstant ((float) value);
1206 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1207 Type underlying = TypeManager.EnumToUnderlying (target_type);
1208 Constant e = (Constant) ic;
1211 // Possibly, we need to create a different 0 literal before passing
1214 if (underlying == TypeManager.int64_type)
1215 e = new LongLiteral (0);
1216 else if (underlying == TypeManager.uint64_type)
1217 e = new ULongLiteral (0);
1219 return new EnumConstant (e, target_type);
1224 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1226 string msg = "Cannot convert implicitly from `"+
1227 TypeManager.CSharpName (source) + "' to `" +
1228 TypeManager.CSharpName (target) + "'";
1230 Report.Error (29, loc, msg);
1234 /// Attemptes to implicityly convert `target' into `type', using
1235 /// ImplicitConversion. If there is no implicit conversion, then
1236 /// an error is signaled
1238 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1239 Type target_type, Location loc)
1243 int errors = Report.Errors;
1244 e = ImplicitConversion (ec, source, target_type, loc);
1245 if (Report.Errors > errors)
1250 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1251 Report.Error (664, loc,
1252 "Double literal cannot be implicitly converted to " +
1253 "float type, use F suffix to create a float literal");
1256 if (source is Constant){
1257 Constant c = (Constant) source;
1259 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1263 Error_CannotImplicitConversion (loc, source.Type, target_type);
1269 /// Performs the explicit numeric conversions
1271 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1273 Type expr_type = expr.Type;
1276 // If we have an enumeration, extract the underlying type,
1277 // use this during the comparison, but wrap around the original
1280 Type real_target_type = target_type;
1282 if (TypeManager.IsEnumType (real_target_type))
1283 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1285 if (ImplicitStandardConversionExists (expr, real_target_type)){
1286 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1288 if (real_target_type != target_type)
1289 return new EmptyCast (ce, target_type);
1293 if (expr_type == TypeManager.sbyte_type){
1295 // From sbyte to byte, ushort, uint, ulong, char
1297 if (real_target_type == TypeManager.byte_type)
1298 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1299 if (real_target_type == TypeManager.ushort_type)
1300 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1301 if (real_target_type == TypeManager.uint32_type)
1302 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1303 if (real_target_type == TypeManager.uint64_type)
1304 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1305 if (real_target_type == TypeManager.char_type)
1306 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1307 } else if (expr_type == TypeManager.byte_type){
1309 // From byte to sbyte and char
1311 if (real_target_type == TypeManager.sbyte_type)
1312 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1313 if (real_target_type == TypeManager.char_type)
1314 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1315 } else if (expr_type == TypeManager.short_type){
1317 // From short to sbyte, byte, ushort, uint, ulong, char
1319 if (real_target_type == TypeManager.sbyte_type)
1320 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1321 if (real_target_type == TypeManager.byte_type)
1322 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1323 if (real_target_type == TypeManager.ushort_type)
1324 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1325 if (real_target_type == TypeManager.uint32_type)
1326 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1327 if (real_target_type == TypeManager.uint64_type)
1328 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1329 if (real_target_type == TypeManager.char_type)
1330 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1331 } else if (expr_type == TypeManager.ushort_type){
1333 // From ushort to sbyte, byte, short, char
1335 if (real_target_type == TypeManager.sbyte_type)
1336 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1337 if (real_target_type == TypeManager.byte_type)
1338 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1339 if (real_target_type == TypeManager.short_type)
1340 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1341 if (real_target_type == TypeManager.char_type)
1342 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1343 } else if (expr_type == TypeManager.int32_type){
1345 // From int to sbyte, byte, short, ushort, uint, ulong, char
1347 if (real_target_type == TypeManager.sbyte_type)
1348 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1349 if (real_target_type == TypeManager.byte_type)
1350 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1351 if (real_target_type == TypeManager.short_type)
1352 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1353 if (real_target_type == TypeManager.ushort_type)
1354 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1355 if (real_target_type == TypeManager.uint32_type)
1356 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1357 if (real_target_type == TypeManager.uint64_type)
1358 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1359 if (real_target_type == TypeManager.char_type)
1360 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1361 } else if (expr_type == TypeManager.uint32_type){
1363 // From uint to sbyte, byte, short, ushort, int, char
1365 if (real_target_type == TypeManager.sbyte_type)
1366 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1367 if (real_target_type == TypeManager.byte_type)
1368 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1369 if (real_target_type == TypeManager.short_type)
1370 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1371 if (real_target_type == TypeManager.ushort_type)
1372 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1373 if (real_target_type == TypeManager.int32_type)
1374 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1375 if (real_target_type == TypeManager.char_type)
1376 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1377 } else if (expr_type == TypeManager.int64_type){
1379 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1381 if (real_target_type == TypeManager.sbyte_type)
1382 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1383 if (real_target_type == TypeManager.byte_type)
1384 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1385 if (real_target_type == TypeManager.short_type)
1386 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1387 if (real_target_type == TypeManager.ushort_type)
1388 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1389 if (real_target_type == TypeManager.int32_type)
1390 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1391 if (real_target_type == TypeManager.uint32_type)
1392 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1393 if (real_target_type == TypeManager.uint64_type)
1394 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1395 if (real_target_type == TypeManager.char_type)
1396 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1397 } else if (expr_type == TypeManager.uint64_type){
1399 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1401 if (real_target_type == TypeManager.sbyte_type)
1402 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1403 if (real_target_type == TypeManager.byte_type)
1404 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1405 if (real_target_type == TypeManager.short_type)
1406 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1407 if (real_target_type == TypeManager.ushort_type)
1408 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1409 if (real_target_type == TypeManager.int32_type)
1410 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1411 if (real_target_type == TypeManager.uint32_type)
1412 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1413 if (real_target_type == TypeManager.int64_type)
1414 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1415 if (real_target_type == TypeManager.char_type)
1416 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1417 } else if (expr_type == TypeManager.char_type){
1419 // From char to sbyte, byte, short
1421 if (real_target_type == TypeManager.sbyte_type)
1422 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1423 if (real_target_type == TypeManager.byte_type)
1424 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1425 if (real_target_type == TypeManager.short_type)
1426 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1427 } else if (expr_type == TypeManager.float_type){
1429 // From float to sbyte, byte, short,
1430 // ushort, int, uint, long, ulong, char
1433 if (real_target_type == TypeManager.sbyte_type)
1434 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1435 if (real_target_type == TypeManager.byte_type)
1436 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1437 if (real_target_type == TypeManager.short_type)
1438 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1439 if (real_target_type == TypeManager.ushort_type)
1440 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1441 if (real_target_type == TypeManager.int32_type)
1442 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1443 if (real_target_type == TypeManager.uint32_type)
1444 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1445 if (real_target_type == TypeManager.int64_type)
1446 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1447 if (real_target_type == TypeManager.uint64_type)
1448 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1449 if (real_target_type == TypeManager.char_type)
1450 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1451 } else if (expr_type == TypeManager.double_type){
1453 // From double to byte, byte, short,
1454 // ushort, int, uint, long, ulong,
1455 // char, float or decimal
1457 if (real_target_type == TypeManager.sbyte_type)
1458 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1459 if (real_target_type == TypeManager.byte_type)
1460 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1461 if (real_target_type == TypeManager.short_type)
1462 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1463 if (real_target_type == TypeManager.ushort_type)
1464 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1465 if (real_target_type == TypeManager.int32_type)
1466 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1467 if (real_target_type == TypeManager.uint32_type)
1468 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1469 if (real_target_type == TypeManager.int64_type)
1470 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1471 if (real_target_type == TypeManager.uint64_type)
1472 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1473 if (real_target_type == TypeManager.char_type)
1474 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1475 if (real_target_type == TypeManager.float_type)
1476 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1479 // decimal is taken care of by the op_Explicit methods.
1485 /// Returns whether an explicit reference conversion can be performed
1486 /// from source_type to target_type
1488 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1490 bool target_is_type_param = target_type.IsGenericParameter;
1491 bool target_is_value_type = target_type.IsValueType;
1493 if (source_type == target_type)
1497 // From object to a generic parameter
1499 if (source_type == TypeManager.object_type && target_is_type_param)
1503 // From object to any reference type
1505 if (source_type == TypeManager.object_type && !target_is_value_type)
1509 // From any class S to any class-type T, provided S is a base class of T
1511 if (TypeManager.IsSubclassOf (target_type, source_type))
1515 // From any interface type S to any interface T provided S is not derived from T
1517 if (source_type.IsInterface && target_type.IsInterface){
1518 if (!TypeManager.IsSubclassOf (target_type, source_type))
1523 // From any class type S to any interface T, provided S is not sealed
1524 // and provided S does not implement T.
1526 if (target_type.IsInterface && !source_type.IsSealed &&
1527 !TypeManager.ImplementsInterface (source_type, target_type))
1531 // From any interface-type S to to any class type T, provided T is not
1532 // sealed, or provided T implements S.
1534 if (source_type.IsInterface &&
1535 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1539 // From an array type S with an element type Se to an array type T with an
1540 // element type Te provided all the following are true:
1541 // * S and T differe only in element type, in other words, S and T
1542 // have the same number of dimensions.
1543 // * Both Se and Te are reference types
1544 // * An explicit referenc conversions exist from Se to Te
1546 if (source_type.IsArray && target_type.IsArray) {
1547 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1549 Type source_element_type = TypeManager.GetElementType (source_type);
1550 Type target_element_type = TypeManager.GetElementType (target_type);
1552 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1553 if (ExplicitReferenceConversionExists (source_element_type,
1554 target_element_type))
1560 // From System.Array to any array-type
1561 if (source_type == TypeManager.array_type &&
1562 target_type.IsArray){
1567 // From System delegate to any delegate-type
1569 if (source_type == TypeManager.delegate_type &&
1570 TypeManager.IsDelegateType (target_type))
1574 // From ICloneable to Array or Delegate types
1576 if (source_type == TypeManager.icloneable_type &&
1577 (target_type == TypeManager.array_type ||
1578 target_type == TypeManager.delegate_type))
1585 /// Implements Explicit Reference conversions
1587 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1589 Type source_type = source.Type;
1590 bool target_is_type_param = target_type.IsGenericParameter;
1591 bool target_is_value_type = target_type.IsValueType;
1594 // From object to a generic parameter
1596 if (source_type == TypeManager.object_type && target_is_type_param)
1597 return new UnboxCast (source, target_type);
1600 // From object to any reference type
1602 if (source_type == TypeManager.object_type && !target_is_value_type)
1603 return new ClassCast (source, target_type);
1606 // Unboxing conversion.
1608 if (((source_type == TypeManager.enum_type &&
1609 !(source is EmptyCast)) ||
1610 source_type == TypeManager.value_type) && target_is_value_type)
1611 return new UnboxCast (source, target_type);
1614 // From any class S to any class-type T, provided S is a base class of T
1616 if (TypeManager.IsSubclassOf (target_type, source_type))
1617 return new ClassCast (source, target_type);
1620 // From any interface type S to any interface T provided S is not derived from T
1622 if (source_type.IsInterface && target_type.IsInterface){
1623 if (TypeManager.ImplementsInterface (source_type, target_type))
1626 return new ClassCast (source, target_type);
1630 // From any class type S to any interface T, provides S is not sealed
1631 // and provided S does not implement T.
1633 if (target_type.IsInterface && !source_type.IsSealed) {
1634 if (TypeManager.ImplementsInterface (source_type, target_type))
1637 return new ClassCast (source, target_type);
1642 // From any interface-type S to to any class type T, provided T is not
1643 // sealed, or provided T implements S.
1645 if (source_type.IsInterface) {
1646 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1647 if (target_type.IsClass)
1648 return new ClassCast (source, target_type);
1650 return new UnboxCast (source, target_type);
1656 // From an array type S with an element type Se to an array type T with an
1657 // element type Te provided all the following are true:
1658 // * S and T differe only in element type, in other words, S and T
1659 // have the same number of dimensions.
1660 // * Both Se and Te are reference types
1661 // * An explicit referenc conversions exist from Se to Te
1663 if (source_type.IsArray && target_type.IsArray) {
1664 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1666 Type source_element_type = TypeManager.GetElementType (source_type);
1667 Type target_element_type = TypeManager.GetElementType (target_type);
1669 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1670 if (ExplicitReferenceConversionExists (source_element_type,
1671 target_element_type))
1672 return new ClassCast (source, target_type);
1677 // From System.Array to any array-type
1678 if (source_type == TypeManager.array_type &&
1679 target_type.IsArray) {
1680 return new ClassCast (source, target_type);
1684 // From System delegate to any delegate-type
1686 if (source_type == TypeManager.delegate_type &&
1687 TypeManager.IsDelegateType (target_type))
1688 return new ClassCast (source, target_type);
1691 // From ICloneable to Array or Delegate types
1693 if (source_type == TypeManager.icloneable_type &&
1694 (target_type == TypeManager.array_type ||
1695 target_type == TypeManager.delegate_type))
1696 return new ClassCast (source, target_type);
1702 /// Performs an explicit conversion of the expression `expr' whose
1703 /// type is expr.Type to `target_type'.
1705 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1706 Type target_type, Location loc)
1708 Type expr_type = expr.Type;
1709 Type original_expr_type = expr_type;
1711 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1712 if (target_type == TypeManager.enum_type ||
1713 target_type == TypeManager.object_type) {
1714 if (expr is EnumConstant)
1715 expr = ((EnumConstant) expr).Child;
1716 // We really need all these casts here .... :-(
1717 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1718 return new EmptyCast (expr, target_type);
1719 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1720 target_type.IsSubclassOf (TypeManager.enum_type))
1721 return new UnboxCast (expr, target_type);
1724 // Notice that we have kept the expr_type unmodified, which is only
1726 if (expr is EnumConstant)
1727 expr = ((EnumConstant) expr).Child;
1729 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1730 expr_type = expr.Type;
1733 int errors = Report.Errors;
1734 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1735 if (Report.Errors > errors)
1741 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1746 // Unboxing conversion.
1748 if (expr_type == TypeManager.object_type && target_type.IsValueType){
1749 if (expr is NullLiteral){
1751 // Skip the ExplicitReferenceConversion because we can not convert
1752 // from Null to a ValueType, and ExplicitReference wont check against
1753 // null literal explicitly
1757 return new UnboxCast (expr, target_type);
1761 ne = ExplicitReferenceConversion (expr, target_type);
1767 if (target_type.IsPointer){
1768 if (expr_type.IsPointer)
1769 return new EmptyCast (expr, target_type);
1771 if (expr_type == TypeManager.sbyte_type ||
1772 expr_type == TypeManager.byte_type ||
1773 expr_type == TypeManager.short_type ||
1774 expr_type == TypeManager.ushort_type ||
1775 expr_type == TypeManager.int32_type ||
1776 expr_type == TypeManager.uint32_type ||
1777 expr_type == TypeManager.uint64_type ||
1778 expr_type == TypeManager.int64_type)
1779 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1781 if (expr_type.IsPointer){
1782 Expression e = null;
1784 if (target_type == TypeManager.sbyte_type)
1785 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1786 else if (target_type == TypeManager.byte_type)
1787 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1788 else if (target_type == TypeManager.short_type)
1789 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1790 else if (target_type == TypeManager.ushort_type)
1791 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1792 else if (target_type == TypeManager.int32_type)
1793 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1794 else if (target_type == TypeManager.uint32_type)
1795 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1796 else if (target_type == TypeManager.uint64_type)
1797 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1798 else if (target_type == TypeManager.int64_type){
1799 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1805 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1810 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1814 // We should always be able to go from an uint32
1815 // implicitly or explicitly to the other integral
1818 throw new Exception ("Internal compiler error");
1823 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1827 if (expr is NullLiteral){
1828 Report.Error (37, loc, "Cannot convert null to value type `" +
1829 TypeManager.CSharpName (target_type) + "'");
1833 Error_CannotConvertType (loc, original_expr_type, target_type);
1838 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1840 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1841 Type target_type, Location l)
1843 int errors = Report.Errors;
1844 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1845 if (Report.Errors > errors)
1851 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1855 ne = ExplicitReferenceConversion (expr, target_type);
1859 Error_CannotConvertType (l, expr.Type, target_type);