2 // conversion.cs: various routines for implementing conversions.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
19 // A container class for all the conversion operations
21 public class Convert {
23 // This is used to prettify the code: a null argument is allowed
24 // for ImplicitStandardConversion as long as it is known that
25 // no anonymous method will play a role.
27 // FIXME: renamed from `const' to `static' to allow bootstraping from older
28 // versions of the compiler that could not cope with this construct.
30 public static EmitContext ConstantEC = null;
32 static public void Error_CannotConvertType (Location loc, Type source, Type target)
34 Report.Error (30, loc, "Cannot convert type '" +
35 TypeManager.CSharpName (source) + "' to '" +
36 TypeManager.CSharpName (target) + "'");
39 static EmptyExpression MyEmptyExpr;
40 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
42 Type expr_type = expr.Type;
44 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
45 // if we are a method group, emit a warning
50 if (expr_type == TypeManager.void_type)
54 // notice that it is possible to write "ValueType v = 1", the ValueType here
55 // is an abstract class, and not really a value type, so we apply the same rules.
57 if (target_type == TypeManager.object_type) {
59 // A pointer type cannot be converted to object
61 if (expr_type.IsPointer)
64 if (expr_type.IsValueType)
65 return new BoxedCast (expr);
66 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
67 if (expr_type == TypeManager.anonymous_method_type)
69 return new EmptyCast (expr, target_type);
73 } else if (target_type == TypeManager.value_type) {
74 if (expr_type.IsValueType)
75 return new BoxedCast (expr);
76 if (expr_type == TypeManager.null_type)
77 return new NullCast (expr, target_type);
80 } else if (expr_type.IsSubclassOf (target_type)) {
82 // Special case: enumeration to System.Enum.
83 // System.Enum is not a value type, it is a class, so we need
84 // a boxing conversion
87 return new BoxedCast (expr);
89 return new EmptyCast (expr, target_type);
92 // This code is kind of mirrored inside ImplicitStandardConversionExists
93 // with the small distinction that we only probe there
95 // Always ensure that the code here and there is in sync
97 // from the null type to any reference-type.
98 if (expr_type == TypeManager.null_type){
99 if (target_type.IsPointer)
100 return new EmptyCast (expr, target_type);
102 if (!target_type.IsValueType)
103 return new NullCast (expr, target_type);
106 // from any class-type S to any interface-type T.
107 if (target_type.IsInterface) {
108 if (target_type != TypeManager.iconvertible_type &&
109 expr_type.IsValueType && (expr is Constant) &&
110 !(expr is IntLiteral || expr is BoolLiteral ||
111 expr is FloatLiteral || expr is DoubleLiteral ||
112 expr is LongLiteral || expr is CharLiteral ||
113 expr is StringLiteral || expr is DecimalLiteral ||
114 expr is UIntLiteral || expr is ULongLiteral)) {
118 if (TypeManager.ImplementsInterface (expr_type, target_type)){
119 if (expr_type.IsClass)
120 return new EmptyCast (expr, target_type);
121 else if (expr_type.IsValueType)
122 return new BoxedCast (expr, target_type);
124 return new EmptyCast (expr, target_type);
128 // from any interface type S to interface-type T.
129 if (expr_type.IsInterface && target_type.IsInterface) {
130 if (TypeManager.ImplementsInterface (expr_type, target_type))
131 return new EmptyCast (expr, target_type);
136 // from an array-type S to an array-type of type T
137 if (expr_type.IsArray && target_type.IsArray) {
138 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
140 Type expr_element_type = TypeManager.GetElementType (expr_type);
142 if (MyEmptyExpr == null)
143 MyEmptyExpr = new EmptyExpression ();
145 MyEmptyExpr.SetType (expr_element_type);
146 Type target_element_type = TypeManager.GetElementType (target_type);
148 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
149 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
150 target_element_type))
151 return new EmptyCast (expr, target_type);
155 // from an array-type to System.Array
156 if (expr_type.IsArray && target_type == TypeManager.array_type)
157 return new EmptyCast (expr, target_type);
159 // from any delegate type to System.Delegate
160 if ((expr_type == TypeManager.delegate_type ||
161 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
162 target_type == TypeManager.delegate_type)
163 return new EmptyCast (expr, target_type);
165 // from any array-type or delegate type into System.ICloneable.
166 if (expr_type.IsArray ||
167 expr_type == TypeManager.delegate_type ||
168 expr_type.IsSubclassOf (TypeManager.delegate_type))
169 if (target_type == TypeManager.icloneable_type)
170 return new EmptyCast (expr, target_type);
176 // Tests whether an implicit reference conversion exists between expr_type
179 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
181 Type expr_type = expr.Type;
184 // This is the boxed case.
186 if (target_type == TypeManager.object_type) {
187 if (expr_type.IsClass || expr_type.IsValueType ||
188 expr_type.IsInterface || expr_type == TypeManager.enum_type)
189 if (target_type != TypeManager.anonymous_method_type)
193 } else if (expr_type.IsSubclassOf (target_type))
196 // Please remember that all code below actually comes
197 // from ImplicitReferenceConversion so make sure code remains in sync
199 // from any class-type S to any interface-type T.
200 if (target_type.IsInterface) {
201 if (target_type != TypeManager.iconvertible_type &&
202 expr_type.IsValueType && (expr is Constant) &&
203 !(expr is IntLiteral || expr is BoolLiteral ||
204 expr is FloatLiteral || expr is DoubleLiteral ||
205 expr is LongLiteral || expr is CharLiteral ||
206 expr is StringLiteral || expr is DecimalLiteral ||
207 expr is UIntLiteral || expr is ULongLiteral)) {
211 if (TypeManager.ImplementsInterface (expr_type, target_type))
215 // from any interface type S to interface-type T.
216 if (expr_type.IsInterface && target_type.IsInterface)
217 if (TypeManager.ImplementsInterface (expr_type, target_type))
220 // from an array-type S to an array-type of type T
221 if (expr_type.IsArray && target_type.IsArray) {
222 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
224 Type expr_element_type = expr_type.GetElementType ();
226 if (MyEmptyExpr == null)
227 MyEmptyExpr = new EmptyExpression ();
229 MyEmptyExpr.SetType (expr_element_type);
230 Type target_element_type = TypeManager.GetElementType (target_type);
232 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
233 if (ImplicitStandardConversionExists (ConstantEC, MyEmptyExpr,
234 target_element_type))
239 // from an array-type to System.Array
240 if (expr_type.IsArray && (target_type == TypeManager.array_type))
243 // from any delegate type to System.Delegate
244 if ((expr_type == TypeManager.delegate_type ||
245 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
246 target_type == TypeManager.delegate_type)
247 if (target_type.IsAssignableFrom (expr_type))
250 // from any array-type or delegate type into System.ICloneable.
251 if (expr_type.IsArray ||
252 expr_type == TypeManager.delegate_type ||
253 expr_type.IsSubclassOf (TypeManager.delegate_type))
254 if (target_type == TypeManager.icloneable_type)
257 // from the null type to any reference-type.
258 if (expr_type == TypeManager.null_type){
259 if (target_type.IsPointer)
262 if (!target_type.IsValueType)
269 /// Implicit Numeric Conversions.
271 /// expr is the expression to convert, returns a new expression of type
272 /// target_type or null if an implicit conversion is not possible.
274 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
275 Type target_type, Location loc)
277 Type expr_type = expr.Type;
280 // Attempt to do the implicit constant expression conversions
282 if (expr is Constant){
283 if (expr is IntConstant){
286 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
290 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
292 // Try the implicit constant expression conversion
293 // from long to ulong, instead of a nice routine,
296 long v = ((LongConstant) expr).Value;
298 return new ULongConstant ((ulong) v);
302 Type real_target_type = target_type;
304 if (expr_type == TypeManager.sbyte_type){
306 // From sbyte to short, int, long, float, double.
308 if (real_target_type == TypeManager.int32_type)
309 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
310 if (real_target_type == TypeManager.int64_type)
311 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
312 if (real_target_type == TypeManager.double_type)
313 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
314 if (real_target_type == TypeManager.float_type)
315 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
316 if (real_target_type == TypeManager.short_type)
317 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
318 } else if (expr_type == TypeManager.byte_type){
320 // From byte to short, ushort, int, uint, long, ulong, float, double
322 if ((real_target_type == TypeManager.short_type) ||
323 (real_target_type == TypeManager.ushort_type) ||
324 (real_target_type == TypeManager.int32_type) ||
325 (real_target_type == TypeManager.uint32_type))
326 return new EmptyCast (expr, target_type);
328 if (real_target_type == TypeManager.uint64_type)
329 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
330 if (real_target_type == TypeManager.int64_type)
331 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
332 if (real_target_type == TypeManager.float_type)
333 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
334 if (real_target_type == TypeManager.double_type)
335 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
336 } else if (expr_type == TypeManager.short_type){
338 // From short to int, long, float, double
340 if (real_target_type == TypeManager.int32_type)
341 return new EmptyCast (expr, target_type);
342 if (real_target_type == TypeManager.int64_type)
343 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
344 if (real_target_type == TypeManager.double_type)
345 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
346 if (real_target_type == TypeManager.float_type)
347 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
348 } else if (expr_type == TypeManager.ushort_type){
350 // From ushort to int, uint, long, ulong, float, double
352 if (real_target_type == TypeManager.uint32_type)
353 return new EmptyCast (expr, target_type);
355 if (real_target_type == TypeManager.uint64_type)
356 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
357 if (real_target_type == TypeManager.int32_type)
358 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
359 if (real_target_type == TypeManager.int64_type)
360 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
361 if (real_target_type == TypeManager.double_type)
362 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
363 if (real_target_type == TypeManager.float_type)
364 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
365 } else if (expr_type == TypeManager.int32_type){
367 // From int to long, float, double
369 if (real_target_type == TypeManager.int64_type)
370 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
371 if (real_target_type == TypeManager.double_type)
372 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
373 if (real_target_type == TypeManager.float_type)
374 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
375 } else if (expr_type == TypeManager.uint32_type){
377 // From uint to long, ulong, float, double
379 if (real_target_type == TypeManager.int64_type)
380 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
381 if (real_target_type == TypeManager.uint64_type)
382 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
383 if (real_target_type == TypeManager.double_type)
384 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
386 if (real_target_type == TypeManager.float_type)
387 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
389 } else if (expr_type == TypeManager.int64_type){
391 // From long/ulong to float, double
393 if (real_target_type == TypeManager.double_type)
394 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
395 if (real_target_type == TypeManager.float_type)
396 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
397 } else if (expr_type == TypeManager.uint64_type){
399 // From ulong to float, double
401 if (real_target_type == TypeManager.double_type)
402 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
404 if (real_target_type == TypeManager.float_type)
405 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
407 } else if (expr_type == TypeManager.char_type){
409 // From char to ushort, int, uint, long, ulong, float, double
411 if ((real_target_type == TypeManager.ushort_type) ||
412 (real_target_type == TypeManager.int32_type) ||
413 (real_target_type == TypeManager.uint32_type))
414 return new EmptyCast (expr, target_type);
415 if (real_target_type == TypeManager.uint64_type)
416 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
417 if (real_target_type == TypeManager.int64_type)
418 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
419 if (real_target_type == TypeManager.float_type)
420 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
421 if (real_target_type == TypeManager.double_type)
422 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
423 } else if (expr_type == TypeManager.float_type){
427 if (real_target_type == TypeManager.double_type)
428 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
436 /// Same as ImplicitStandardConversionExists except that it also looks at
437 /// implicit user defined conversions - needed for overload resolution
439 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
441 if (ImplicitStandardConversionExists (ec, expr, target_type))
444 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
452 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
454 Expression dummy = ImplicitUserConversion (
455 ec, new EmptyExpression (source), target, Location.Null);
456 return dummy != null;
460 /// Determines if a standard implicit conversion exists from
461 /// expr_type to target_type
463 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
465 public static bool ImplicitStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
467 Type expr_type = expr.Type;
469 if (expr_type == TypeManager.void_type)
472 if (expr_type == target_type)
475 // First numeric conversions
477 if (expr_type == TypeManager.sbyte_type){
479 // From sbyte to short, int, long, float, double.
481 if ((target_type == TypeManager.int32_type) ||
482 (target_type == TypeManager.int64_type) ||
483 (target_type == TypeManager.double_type) ||
484 (target_type == TypeManager.float_type) ||
485 (target_type == TypeManager.short_type) ||
486 (target_type == TypeManager.decimal_type))
489 } else if (expr_type == TypeManager.byte_type){
491 // From byte to short, ushort, int, uint, long, ulong, float, double
493 if ((target_type == TypeManager.short_type) ||
494 (target_type == TypeManager.ushort_type) ||
495 (target_type == TypeManager.int32_type) ||
496 (target_type == TypeManager.uint32_type) ||
497 (target_type == TypeManager.uint64_type) ||
498 (target_type == TypeManager.int64_type) ||
499 (target_type == TypeManager.float_type) ||
500 (target_type == TypeManager.double_type) ||
501 (target_type == TypeManager.decimal_type))
504 } else if (expr_type == TypeManager.short_type){
506 // From short to int, long, float, double
508 if ((target_type == TypeManager.int32_type) ||
509 (target_type == TypeManager.int64_type) ||
510 (target_type == TypeManager.double_type) ||
511 (target_type == TypeManager.float_type) ||
512 (target_type == TypeManager.decimal_type))
515 } else if (expr_type == TypeManager.ushort_type){
517 // From ushort to int, uint, long, ulong, float, double
519 if ((target_type == TypeManager.uint32_type) ||
520 (target_type == TypeManager.uint64_type) ||
521 (target_type == TypeManager.int32_type) ||
522 (target_type == TypeManager.int64_type) ||
523 (target_type == TypeManager.double_type) ||
524 (target_type == TypeManager.float_type) ||
525 (target_type == TypeManager.decimal_type))
528 } else if (expr_type == TypeManager.int32_type){
530 // From int to long, float, double
532 if ((target_type == TypeManager.int64_type) ||
533 (target_type == TypeManager.double_type) ||
534 (target_type == TypeManager.float_type) ||
535 (target_type == TypeManager.decimal_type))
538 } else if (expr_type == TypeManager.uint32_type){
540 // From uint to long, ulong, float, double
542 if ((target_type == TypeManager.int64_type) ||
543 (target_type == TypeManager.uint64_type) ||
544 (target_type == TypeManager.double_type) ||
545 (target_type == TypeManager.float_type) ||
546 (target_type == TypeManager.decimal_type))
549 } else if ((expr_type == TypeManager.uint64_type) ||
550 (expr_type == TypeManager.int64_type)) {
552 // From long/ulong to float, double
554 if ((target_type == TypeManager.double_type) ||
555 (target_type == TypeManager.float_type) ||
556 (target_type == TypeManager.decimal_type))
559 } else if (expr_type == TypeManager.char_type){
561 // From char to ushort, int, uint, long, ulong, float, double
563 if ((target_type == TypeManager.ushort_type) ||
564 (target_type == TypeManager.int32_type) ||
565 (target_type == TypeManager.uint32_type) ||
566 (target_type == TypeManager.uint64_type) ||
567 (target_type == TypeManager.int64_type) ||
568 (target_type == TypeManager.float_type) ||
569 (target_type == TypeManager.double_type) ||
570 (target_type == TypeManager.decimal_type))
573 } else if (expr_type == TypeManager.float_type){
577 if (target_type == TypeManager.double_type)
581 if (ImplicitReferenceConversionExists (expr, target_type))
585 // Implicit Constant Expression Conversions
587 if (expr is IntConstant){
588 int value = ((IntConstant) expr).Value;
590 if (target_type == TypeManager.sbyte_type){
591 if (value >= SByte.MinValue && value <= SByte.MaxValue)
593 } else if (target_type == TypeManager.byte_type){
594 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
596 } else if (target_type == TypeManager.short_type){
597 if (value >= Int16.MinValue && value <= Int16.MaxValue)
599 } else if (target_type == TypeManager.ushort_type){
600 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
602 } else if (target_type == TypeManager.uint32_type){
605 } else if (target_type == TypeManager.uint64_type){
607 // we can optimize this case: a positive int32
608 // always fits on a uint64. But we need an opcode
615 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
619 if (expr is LongConstant && target_type == TypeManager.uint64_type){
621 // Try the implicit constant expression conversion
622 // from long to ulong, instead of a nice routine,
625 long v = ((LongConstant) expr).Value;
630 if ((target_type == TypeManager.enum_type ||
631 target_type.IsSubclassOf (TypeManager.enum_type)) &&
633 IntLiteral i = (IntLiteral) expr;
639 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
642 if (expr_type == TypeManager.anonymous_method_type){
643 if (!TypeManager.IsDelegateType (target_type))
646 AnonymousMethod am = (AnonymousMethod) expr;
647 int errors = Report.Errors;
649 Expression conv = am.Compatible (ec, target_type, true);
658 // Used internally by FindMostEncompassedType, this is used
659 // to avoid creating lots of objects in the tight loop inside
660 // FindMostEncompassedType
662 static EmptyExpression priv_fmet_param;
665 /// Finds "most encompassed type" according to the spec (13.4.2)
666 /// amongst the methods in the MethodGroupExpr
668 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
672 if (priv_fmet_param == null)
673 priv_fmet_param = new EmptyExpression ();
675 foreach (Type t in types){
676 priv_fmet_param.SetType (t);
683 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
691 // Used internally by FindMostEncompassingType, this is used
692 // to avoid creating lots of objects in the tight loop inside
693 // FindMostEncompassingType
695 static EmptyExpression priv_fmee_ret;
698 /// Finds "most encompassing type" according to the spec (13.4.2)
699 /// amongst the types in the given set
701 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
705 if (priv_fmee_ret == null)
706 priv_fmee_ret = new EmptyExpression ();
708 foreach (Type t in types){
709 priv_fmee_ret.SetType (best);
716 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
724 // Used to avoid creating too many objects
726 static EmptyExpression priv_fms_expr;
729 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
730 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
731 /// for explicit and implicit conversion operators.
733 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
734 Expression source, bool apply_explicit_conv_rules,
737 ArrayList src_types_set = new ArrayList ();
739 if (priv_fms_expr == null)
740 priv_fms_expr = new EmptyExpression ();
743 // If any operator converts from S then Sx = S
745 Type source_type = source.Type;
746 foreach (MethodBase mb in me.Methods){
747 ParameterData pd = Invocation.GetParameterData (mb);
748 Type param_type = pd.ParameterType (0);
750 if (param_type == source_type)
753 if (apply_explicit_conv_rules) {
756 // Find the set of applicable user-defined conversion operators, U. This set
758 // user-defined implicit or explicit conversion operators declared by
759 // the classes or structs in D that convert from a type encompassing
760 // or encompassed by S to a type encompassing or encompassed by T
762 priv_fms_expr.SetType (param_type);
763 if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
764 src_types_set.Add (param_type);
766 if (ImplicitStandardConversionExists (ec, source, param_type))
767 src_types_set.Add (param_type);
771 // Only if S is encompassed by param_type
773 if (ImplicitStandardConversionExists (ec, source, param_type))
774 src_types_set.Add (param_type);
779 // Explicit Conv rules
781 if (apply_explicit_conv_rules) {
782 ArrayList candidate_set = new ArrayList ();
784 foreach (Type param_type in src_types_set){
785 if (ImplicitStandardConversionExists (ec, source, param_type))
786 candidate_set.Add (param_type);
789 if (candidate_set.Count != 0)
790 return FindMostEncompassedType (ec, candidate_set);
796 if (apply_explicit_conv_rules)
797 return FindMostEncompassingType (ec, src_types_set);
799 return FindMostEncompassedType (ec, src_types_set);
803 // Useful in avoiding proliferation of objects
805 static EmptyExpression priv_fmt_expr;
808 /// Finds the most specific target Tx according to section 13.4.4
810 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
811 Type target, bool apply_explicit_conv_rules,
814 ArrayList tgt_types_set = new ArrayList ();
816 if (priv_fmt_expr == null)
817 priv_fmt_expr = new EmptyExpression ();
820 // If any operator converts to T then Tx = T
822 foreach (MethodInfo mi in me.Methods){
823 Type ret_type = mi.ReturnType;
825 if (ret_type == target)
828 if (apply_explicit_conv_rules) {
831 // Find the set of applicable user-defined conversion operators, U.
833 // This set consists of the
834 // user-defined implicit or explicit conversion operators declared by
835 // the classes or structs in D that convert from a type encompassing
836 // or encompassed by S to a type encompassing or encompassed by T
838 priv_fms_expr.SetType (ret_type);
839 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
840 tgt_types_set.Add (ret_type);
842 priv_fms_expr.SetType (target);
843 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
844 tgt_types_set.Add (ret_type);
848 // Only if T is encompassed by param_type
850 priv_fms_expr.SetType (ret_type);
851 if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
852 tgt_types_set.Add (ret_type);
857 // Explicit conv rules
859 if (apply_explicit_conv_rules) {
860 ArrayList candidate_set = new ArrayList ();
862 foreach (Type ret_type in tgt_types_set){
863 priv_fmt_expr.SetType (ret_type);
865 if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
866 candidate_set.Add (ret_type);
869 if (candidate_set.Count != 0)
870 return FindMostEncompassingType (ec, candidate_set);
874 // Okay, final case !
876 if (apply_explicit_conv_rules)
877 return FindMostEncompassedType (ec, tgt_types_set);
879 return FindMostEncompassingType (ec, tgt_types_set);
883 /// User-defined Implicit conversions
885 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
886 Type target, Location loc)
888 return UserDefinedConversion (ec, source, target, loc, false);
892 /// User-defined Explicit conversions
894 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
895 Type target, Location loc)
897 return UserDefinedConversion (ec, source, target, loc, true);
900 static DoubleHash explicit_conv = new DoubleHash (100);
901 static DoubleHash implicit_conv = new DoubleHash (100);
903 /// Computes the MethodGroup for the user-defined conversion
904 /// operators from source_type to target_type. `look_for_explicit'
905 /// controls whether we should also include the list of explicit
908 static MethodGroupExpr GetConversionOperators (EmitContext ec,
909 Type source_type, Type target_type,
910 Location loc, bool look_for_explicit)
912 Expression mg1 = null, mg2 = null;
913 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
916 op_name = "op_Implicit";
918 MethodGroupExpr union3;
920 if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
921 return (MethodGroupExpr) r;
923 mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
924 if (source_type.BaseType != null)
925 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
928 union3 = (MethodGroupExpr) mg2;
929 else if (mg2 == null)
930 union3 = (MethodGroupExpr) mg1;
932 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
934 mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
937 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
939 union3 = (MethodGroupExpr) mg1;
942 if (target_type.BaseType != null)
943 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
947 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
949 union3 = (MethodGroupExpr) mg1;
952 MethodGroupExpr union4 = null;
954 if (look_for_explicit) {
955 op_name = "op_Explicit";
957 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
958 if (source_type.BaseType != null)
959 mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
961 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
962 if (target_type.BaseType != null)
963 mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
965 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
966 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
968 union4 = Invocation.MakeUnionSet (union5, union6, loc);
971 MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
972 (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
977 /// User-defined conversions
979 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
980 Type target, Location loc,
981 bool look_for_explicit)
983 MethodGroupExpr union;
984 Type source_type = source.Type;
985 MethodBase method = null;
987 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
991 Type most_specific_source, most_specific_target;
993 most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
994 if (most_specific_source == null)
997 most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
998 if (most_specific_target == null)
1004 foreach (MethodBase mb in union.Methods){
1005 ParameterData pd = Invocation.GetParameterData (mb);
1006 MethodInfo mi = (MethodInfo) mb;
1008 if (pd.ParameterType (0) == most_specific_source &&
1009 mi.ReturnType == most_specific_target) {
1015 if (method == null || count > 1)
1020 // This will do the conversion to the best match that we
1021 // found. Now we need to perform an implict standard conversion
1022 // if the best match was not the type that we were requested
1025 if (look_for_explicit)
1026 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1028 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1034 e = new UserCast ((MethodInfo) method, source, loc);
1035 if (e.Type != target){
1036 if (!look_for_explicit)
1037 e = ImplicitConversionStandard (ec, e, target, loc);
1039 e = ExplicitConversionStandard (ec, e, target, loc);
1046 /// Converts implicitly the resolved expression `expr' into the
1047 /// `target_type'. It returns a new expression that can be used
1048 /// in a context that expects a `target_type'.
1050 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1051 Type target_type, Location loc)
1055 if (target_type == null)
1056 throw new Exception ("Target type is null");
1058 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1062 e = ImplicitUserConversion (ec, expr, target_type, loc);
1071 /// Attempts to apply the `Standard Implicit
1072 /// Conversion' rules to the expression `expr' into
1073 /// the `target_type'. It returns a new expression
1074 /// that can be used in a context that expects a
1077 /// This is different from `ImplicitConversion' in that the
1078 /// user defined implicit conversions are excluded.
1080 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1081 Type target_type, Location loc)
1083 Type expr_type = expr.Type;
1086 if (expr.eclass == ExprClass.MethodGroup){
1087 if (!TypeManager.IsDelegateType (target_type)){
1092 // Only allow anonymous method conversions on post ISO_1
1094 if (RootContext.Version != LanguageVersion.ISO_1){
1095 MethodGroupExpr mg = expr as MethodGroupExpr;
1097 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1101 if (expr_type == target_type && expr_type != TypeManager.null_type)
1104 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1108 e = ImplicitReferenceConversion (expr, target_type);
1112 if ((target_type == TypeManager.enum_type ||
1113 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1114 expr is IntLiteral){
1115 IntLiteral i = (IntLiteral) expr;
1118 return new EnumConstant ((Constant) expr, target_type);
1122 if (expr_type.IsPointer){
1123 if (target_type == TypeManager.void_ptr_type)
1124 return new EmptyCast (expr, target_type);
1127 // yep, comparing pointer types cant be done with
1128 // t1 == t2, we have to compare their element types.
1130 if (target_type.IsPointer){
1131 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1136 if (target_type.IsPointer) {
1137 if (expr_type == TypeManager.null_type)
1138 return new EmptyCast (expr, target_type);
1140 if (expr_type == TypeManager.void_ptr_type)
1141 return new EmptyCast (expr, target_type);
1145 if (expr_type == TypeManager.anonymous_method_type){
1146 if (!TypeManager.IsDelegateType (target_type)){
1147 Report.Error (1660, loc,
1148 "Cannot convert anonymous method to `{0}', since it is not a delegate",
1149 TypeManager.CSharpName (target_type));
1153 AnonymousMethod am = (AnonymousMethod) expr;
1154 int errors = Report.Errors;
1156 Expression conv = am.Compatible (ec, target_type, false);
1161 // We return something instead of null, to avoid
1162 // the duplicate error, since am.Compatible would have
1163 // reported that already
1165 if (errors != Report.Errors)
1166 return new EmptyCast (expr, target_type);
1173 /// Attemps to perform an implict constant conversion of the IntConstant
1174 /// into a different data type using casts (See Implicit Constant
1175 /// Expression Conversions)
1177 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1179 int value = ic.Value;
1181 if (target_type == TypeManager.sbyte_type){
1182 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1183 return new SByteConstant ((sbyte) value);
1184 } else if (target_type == TypeManager.byte_type){
1185 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1186 return new ByteConstant ((byte) value);
1187 } else if (target_type == TypeManager.short_type){
1188 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1189 return new ShortConstant ((short) value);
1190 } else if (target_type == TypeManager.ushort_type){
1191 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1192 return new UShortConstant ((ushort) value);
1193 } else if (target_type == TypeManager.uint32_type){
1195 return new UIntConstant ((uint) value);
1196 } else if (target_type == TypeManager.uint64_type){
1198 // we can optimize this case: a positive int32
1199 // always fits on a uint64. But we need an opcode
1203 return new ULongConstant ((ulong) value);
1204 } else if (target_type == TypeManager.double_type)
1205 return new DoubleConstant ((double) value);
1206 else if (target_type == TypeManager.float_type)
1207 return new FloatConstant ((float) value);
1209 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1210 Type underlying = TypeManager.EnumToUnderlying (target_type);
1211 Constant e = (Constant) ic;
1214 // Possibly, we need to create a different 0 literal before passing
1217 if (underlying == TypeManager.int64_type)
1218 e = new LongLiteral (0);
1219 else if (underlying == TypeManager.uint64_type)
1220 e = new ULongLiteral (0);
1222 return new EnumConstant (e, target_type);
1227 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1229 if (source.Name == target.Name){
1230 Report.ExtraInformation (loc,
1232 "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
1233 source.Assembly.FullName, target.Assembly.FullName));
1236 Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1237 source == TypeManager.anonymous_method_type ?
1238 "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1239 TypeManager.CSharpName (target));
1243 /// Attemptes to implicityly convert `target' into `type', using
1244 /// ImplicitConversion. If there is no implicit conversion, then
1245 /// an error is signaled
1247 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1248 Type target_type, Location loc)
1252 e = ImplicitConversion (ec, source, target_type, loc);
1256 if (source is DoubleLiteral) {
1257 if (target_type == TypeManager.float_type) {
1258 Error_664 (loc, "float", "f");
1261 if (target_type == TypeManager.decimal_type) {
1262 Error_664 (loc, "decimal", "m");
1267 if (source is Constant){
1268 Constant c = (Constant) source;
1270 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1274 Error_CannotImplicitConversion (loc, source.Type, target_type);
1279 static void Error_664 (Location loc, string type, string suffix) {
1280 Report.Error (664, loc,
1281 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1286 /// Performs the explicit numeric conversions
1288 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1290 Type expr_type = expr.Type;
1293 // If we have an enumeration, extract the underlying type,
1294 // use this during the comparison, but wrap around the original
1297 Type real_target_type = target_type;
1299 if (TypeManager.IsEnumType (real_target_type))
1300 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1302 if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1303 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1305 if (real_target_type != target_type)
1306 return new EmptyCast (ce, target_type);
1310 if (expr_type == TypeManager.sbyte_type){
1312 // From sbyte to byte, ushort, uint, ulong, char
1314 if (real_target_type == TypeManager.byte_type)
1315 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1316 if (real_target_type == TypeManager.ushort_type)
1317 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1318 if (real_target_type == TypeManager.uint32_type)
1319 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1320 if (real_target_type == TypeManager.uint64_type)
1321 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1322 if (real_target_type == TypeManager.char_type)
1323 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1324 } else if (expr_type == TypeManager.byte_type){
1326 // From byte to sbyte and char
1328 if (real_target_type == TypeManager.sbyte_type)
1329 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1330 if (real_target_type == TypeManager.char_type)
1331 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1332 } else if (expr_type == TypeManager.short_type){
1334 // From short to sbyte, byte, ushort, uint, ulong, char
1336 if (real_target_type == TypeManager.sbyte_type)
1337 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1338 if (real_target_type == TypeManager.byte_type)
1339 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1340 if (real_target_type == TypeManager.ushort_type)
1341 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1342 if (real_target_type == TypeManager.uint32_type)
1343 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1344 if (real_target_type == TypeManager.uint64_type)
1345 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1346 if (real_target_type == TypeManager.char_type)
1347 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1348 } else if (expr_type == TypeManager.ushort_type){
1350 // From ushort to sbyte, byte, short, char
1352 if (real_target_type == TypeManager.sbyte_type)
1353 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1354 if (real_target_type == TypeManager.byte_type)
1355 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1356 if (real_target_type == TypeManager.short_type)
1357 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1358 if (real_target_type == TypeManager.char_type)
1359 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1360 } else if (expr_type == TypeManager.int32_type){
1362 // From int to sbyte, byte, short, ushort, uint, ulong, char
1364 if (real_target_type == TypeManager.sbyte_type)
1365 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1366 if (real_target_type == TypeManager.byte_type)
1367 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1368 if (real_target_type == TypeManager.short_type)
1369 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1370 if (real_target_type == TypeManager.ushort_type)
1371 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1372 if (real_target_type == TypeManager.uint32_type)
1373 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1374 if (real_target_type == TypeManager.uint64_type)
1375 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1376 if (real_target_type == TypeManager.char_type)
1377 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1378 } else if (expr_type == TypeManager.uint32_type){
1380 // From uint to sbyte, byte, short, ushort, int, char
1382 if (real_target_type == TypeManager.sbyte_type)
1383 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1384 if (real_target_type == TypeManager.byte_type)
1385 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1386 if (real_target_type == TypeManager.short_type)
1387 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1388 if (real_target_type == TypeManager.ushort_type)
1389 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1390 if (real_target_type == TypeManager.int32_type)
1391 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1392 if (real_target_type == TypeManager.char_type)
1393 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1394 } else if (expr_type == TypeManager.int64_type){
1396 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1398 if (real_target_type == TypeManager.sbyte_type)
1399 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1400 if (real_target_type == TypeManager.byte_type)
1401 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1402 if (real_target_type == TypeManager.short_type)
1403 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1404 if (real_target_type == TypeManager.ushort_type)
1405 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1406 if (real_target_type == TypeManager.int32_type)
1407 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1408 if (real_target_type == TypeManager.uint32_type)
1409 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1410 if (real_target_type == TypeManager.uint64_type)
1411 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1412 if (real_target_type == TypeManager.char_type)
1413 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1414 } else if (expr_type == TypeManager.uint64_type){
1416 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1418 if (real_target_type == TypeManager.sbyte_type)
1419 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1420 if (real_target_type == TypeManager.byte_type)
1421 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1422 if (real_target_type == TypeManager.short_type)
1423 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1424 if (real_target_type == TypeManager.ushort_type)
1425 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1426 if (real_target_type == TypeManager.int32_type)
1427 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1428 if (real_target_type == TypeManager.uint32_type)
1429 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1430 if (real_target_type == TypeManager.int64_type)
1431 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1432 if (real_target_type == TypeManager.char_type)
1433 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1434 } else if (expr_type == TypeManager.char_type){
1436 // From char to sbyte, byte, short
1438 if (real_target_type == TypeManager.sbyte_type)
1439 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1440 if (real_target_type == TypeManager.byte_type)
1441 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1442 if (real_target_type == TypeManager.short_type)
1443 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1444 } else if (expr_type == TypeManager.float_type){
1446 // From float to sbyte, byte, short,
1447 // ushort, int, uint, long, ulong, char
1450 if (real_target_type == TypeManager.sbyte_type)
1451 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1452 if (real_target_type == TypeManager.byte_type)
1453 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1454 if (real_target_type == TypeManager.short_type)
1455 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1456 if (real_target_type == TypeManager.ushort_type)
1457 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1458 if (real_target_type == TypeManager.int32_type)
1459 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1460 if (real_target_type == TypeManager.uint32_type)
1461 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1462 if (real_target_type == TypeManager.int64_type)
1463 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1464 if (real_target_type == TypeManager.uint64_type)
1465 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1466 if (real_target_type == TypeManager.char_type)
1467 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1468 } else if (expr_type == TypeManager.double_type){
1470 // From double to byte, byte, short,
1471 // ushort, int, uint, long, ulong,
1472 // char, float or decimal
1474 if (real_target_type == TypeManager.sbyte_type)
1475 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1476 if (real_target_type == TypeManager.byte_type)
1477 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1478 if (real_target_type == TypeManager.short_type)
1479 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1480 if (real_target_type == TypeManager.ushort_type)
1481 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1482 if (real_target_type == TypeManager.int32_type)
1483 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1484 if (real_target_type == TypeManager.uint32_type)
1485 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1486 if (real_target_type == TypeManager.int64_type)
1487 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1488 if (real_target_type == TypeManager.uint64_type)
1489 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1490 if (real_target_type == TypeManager.char_type)
1491 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1492 if (real_target_type == TypeManager.float_type)
1493 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1496 // decimal is taken care of by the op_Explicit methods.
1502 /// Returns whether an explicit reference conversion can be performed
1503 /// from source_type to target_type
1505 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1507 bool target_is_value_type = target_type.IsValueType;
1509 if (source_type == target_type)
1513 // From object to any reference type
1515 if (source_type == TypeManager.object_type && !target_is_value_type)
1519 // From any class S to any class-type T, provided S is a base class of T
1521 if (target_type.IsSubclassOf (source_type))
1525 // From any interface type S to any interface T provided S is not derived from T
1527 if (source_type.IsInterface && target_type.IsInterface){
1528 if (!target_type.IsSubclassOf (source_type))
1533 // From any class type S to any interface T, provided S is not sealed
1534 // and provided S does not implement T.
1536 if (target_type.IsInterface && !source_type.IsSealed &&
1537 !TypeManager.ImplementsInterface (source_type, target_type))
1541 // From any interface-type S to to any class type T, provided T is not
1542 // sealed, or provided T implements S.
1544 if (source_type.IsInterface &&
1545 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1549 // From an array type S with an element type Se to an array type T with an
1550 // element type Te provided all the following are true:
1551 // * S and T differe only in element type, in other words, S and T
1552 // have the same number of dimensions.
1553 // * Both Se and Te are reference types
1554 // * An explicit referenc conversions exist from Se to Te
1556 if (source_type.IsArray && target_type.IsArray) {
1557 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1559 Type source_element_type = TypeManager.GetElementType (source_type);
1560 Type target_element_type = TypeManager.GetElementType (target_type);
1562 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1563 if (ExplicitReferenceConversionExists (source_element_type,
1564 target_element_type))
1570 // From System.Array to any array-type
1571 if (source_type == TypeManager.array_type &&
1572 target_type.IsArray){
1577 // From System delegate to any delegate-type
1579 if (source_type == TypeManager.delegate_type &&
1580 target_type.IsSubclassOf (TypeManager.delegate_type))
1584 // From ICloneable to Array or Delegate types
1586 if (source_type == TypeManager.icloneable_type &&
1587 (target_type == TypeManager.array_type ||
1588 target_type == TypeManager.delegate_type))
1595 /// Implements Explicit Reference conversions
1597 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1599 Type source_type = source.Type;
1600 bool target_is_value_type = target_type.IsValueType;
1603 // From object to any reference type
1605 if (source_type == TypeManager.object_type && !target_is_value_type)
1606 return new ClassCast (source, target_type);
1609 // Unboxing conversion.
1611 if (((source_type == TypeManager.enum_type &&
1612 !(source is EmptyCast)) ||
1613 source_type == TypeManager.value_type) && target_is_value_type)
1614 return new UnboxCast (source, target_type);
1617 // From any class S to any class-type T, provided S is a base class of T
1619 if (target_type.IsSubclassOf (source_type))
1620 return new ClassCast (source, target_type);
1623 // From any interface type S to any interface T provided S is not derived from T
1625 if (source_type.IsInterface && target_type.IsInterface){
1626 if (TypeManager.ImplementsInterface (source_type, target_type))
1629 return new ClassCast (source, target_type);
1633 // From any class type S to any interface T, provides S is not sealed
1634 // and provided S does not implement T.
1636 if (target_type.IsInterface && !source_type.IsSealed) {
1637 if (TypeManager.ImplementsInterface (source_type, target_type))
1640 return new ClassCast (source, target_type);
1645 // From any interface-type S to to any class type T, provided T is not
1646 // sealed, or provided T implements S.
1648 if (source_type.IsInterface) {
1649 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1650 if (target_type.IsClass)
1651 return new ClassCast (source, target_type);
1653 return new UnboxCast (source, target_type);
1659 // From an array type S with an element type Se to an array type T with an
1660 // element type Te provided all the following are true:
1661 // * S and T differe only in element type, in other words, S and T
1662 // have the same number of dimensions.
1663 // * Both Se and Te are reference types
1664 // * An explicit referenc conversions exist from Se to Te
1666 if (source_type.IsArray && target_type.IsArray) {
1667 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1669 Type source_element_type = TypeManager.GetElementType (source_type);
1670 Type target_element_type = TypeManager.GetElementType (target_type);
1672 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1673 if (ExplicitReferenceConversionExists (source_element_type,
1674 target_element_type))
1675 return new ClassCast (source, target_type);
1680 // From System.Array to any array-type
1681 if (source_type == TypeManager.array_type &&
1682 target_type.IsArray) {
1683 return new ClassCast (source, target_type);
1687 // From System delegate to any delegate-type
1689 if (source_type == TypeManager.delegate_type &&
1690 target_type.IsSubclassOf (TypeManager.delegate_type))
1691 return new ClassCast (source, target_type);
1694 // From ICloneable to Array or Delegate types
1696 if (source_type == TypeManager.icloneable_type &&
1697 (target_type == TypeManager.array_type ||
1698 target_type == TypeManager.delegate_type))
1699 return new ClassCast (source, target_type);
1705 /// Performs an explicit conversion of the expression `expr' whose
1706 /// type is expr.Type to `target_type'.
1708 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1709 Type target_type, Location loc)
1711 Type expr_type = expr.Type;
1712 Type original_expr_type = expr_type;
1714 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1715 if (target_type == TypeManager.enum_type ||
1716 target_type == TypeManager.object_type) {
1717 if (expr is EnumConstant)
1718 expr = ((EnumConstant) expr).Child;
1719 // We really need all these casts here .... :-(
1720 expr = new BoxedCast (new EmptyCast (expr, expr_type));
1721 return new EmptyCast (expr, target_type);
1722 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1723 target_type.IsSubclassOf (TypeManager.enum_type))
1724 return new UnboxCast (expr, target_type);
1727 // Notice that we have kept the expr_type unmodified, which is only
1729 if (expr is EnumConstant)
1730 expr = ((EnumConstant) expr).Child;
1732 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1733 expr_type = expr.Type;
1736 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1741 ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1746 // Unboxing conversion.
1748 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1749 return new UnboxCast (expr, target_type);
1752 // Skip the ExplicitReferenceConversion because we can not convert
1753 // from Null to a ValueType, and ExplicitReference wont check against
1754 // null literal explicitly
1756 if (expr_type != TypeManager.null_type){
1757 ne = ExplicitReferenceConversion (expr, target_type);
1764 if (target_type.IsPointer){
1765 if (expr_type.IsPointer)
1766 return new EmptyCast (expr, target_type);
1768 if (expr_type == TypeManager.sbyte_type ||
1769 expr_type == TypeManager.byte_type ||
1770 expr_type == TypeManager.short_type ||
1771 expr_type == TypeManager.ushort_type ||
1772 expr_type == TypeManager.int32_type ||
1773 expr_type == TypeManager.uint32_type ||
1774 expr_type == TypeManager.uint64_type ||
1775 expr_type == TypeManager.int64_type)
1776 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1778 if (expr_type.IsPointer){
1779 Expression e = null;
1781 if (target_type == TypeManager.sbyte_type)
1782 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1783 else if (target_type == TypeManager.byte_type)
1784 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1785 else if (target_type == TypeManager.short_type)
1786 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1787 else if (target_type == TypeManager.ushort_type)
1788 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1789 else if (target_type == TypeManager.int32_type)
1790 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1791 else if (target_type == TypeManager.uint32_type)
1792 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1793 else if (target_type == TypeManager.uint64_type)
1794 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1795 else if (target_type == TypeManager.int64_type){
1796 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1802 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1807 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1811 // We should always be able to go from an uint32
1812 // implicitly or explicitly to the other integral
1815 throw new Exception ("Internal compiler error");
1820 ne = ExplicitUserConversion (ec, expr, target_type, loc);
1824 if (expr is NullLiteral){
1825 Report.Error (37, loc, "Cannot convert null to value type `" +
1826 TypeManager.CSharpName (target_type) + "'");
1830 Error_CannotConvertType (loc, original_expr_type, target_type);
1835 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1837 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1838 Type target_type, Location l)
1840 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1845 ne = ExplicitNumericConversion (ec, expr, target_type, l);
1849 ne = ExplicitReferenceConversion (expr, target_type);
1853 Error_CannotConvertType (l, expr.Type, target_type);