2009-07-20 Jb Evain <jbevain@novell.com>
[mcs.git] / mcs / convert.cs
blob366dccf9613441cd3fb70ffd1652bcb91025fc3b
1 //
2 // conversion.cs: various routines for implementing conversions.
3 //
4 // Authors:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Ravi Pratap (ravi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
8 //
9 // Copyright 2001, 2002, 2003 Ximian, Inc.
10 // Copyright 2003-2008 Novell, Inc.
13 namespace Mono.CSharp {
14 using System;
15 using System.Collections;
16 using System.Diagnostics;
17 using System.Reflection;
18 using System.Reflection.Emit;
21 // A container class for all the conversion operations
23 static class Convert {
25 static EmptyExpression MyEmptyExpr;
26 static DoubleHash explicit_conv;
27 static DoubleHash implicit_conv;
29 static Convert ()
31 Reset ();
34 public static void Reset ()
36 MyEmptyExpr = null;
37 explicit_conv = new DoubleHash (100);
38 implicit_conv = new DoubleHash (100);
41 static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
43 ArrayList list = new ArrayList ();
44 list.Add (gc.EffectiveBaseClass);
45 foreach (Type t in gc.InterfaceConstraints) {
46 if (!TypeManager.IsGenericParameter (t))
47 continue;
49 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
50 if (new_gc != null)
51 list.Add (TypeParam_EffectiveBaseType (new_gc));
53 return FindMostEncompassedType (list);
57 // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
58 // interfaces of this interface, provided there is an implicit reference conversion
59 // from S to T.
61 static bool Array_To_IList (Type array, Type list, bool isExplicit)
63 if ((array.GetArrayRank () != 1) || !TypeManager.IsGenericType (list))
64 return false;
66 Type gt = TypeManager.DropGenericTypeArguments (list);
67 if ((gt != TypeManager.generic_ilist_type) &&
68 (gt != TypeManager.generic_icollection_type) &&
69 (gt != TypeManager.generic_ienumerable_type))
70 return false;
72 Type element_type = TypeManager.GetElementType (array);
73 Type arg_type = TypeManager.GetTypeArguments (list) [0];
75 if (element_type == arg_type)
76 return true;
78 if (isExplicit)
79 return ExplicitReferenceConversionExists (element_type, arg_type);
81 Type t = TypeManager.GetElementType (array);
82 if (MyEmptyExpr == null)
83 MyEmptyExpr = new EmptyExpression (t);
84 else
85 MyEmptyExpr.SetType (t);
87 return ImplicitReferenceConversionExists (MyEmptyExpr, arg_type);
90 static bool IList_To_Array(Type list, Type array)
92 if (!TypeManager.IsGenericType (list) || !array.IsArray || array.GetArrayRank() != 1)
93 return false;
95 Type gt = TypeManager.DropGenericTypeArguments (list);
96 if (gt != TypeManager.generic_ilist_type &&
97 gt != TypeManager.generic_icollection_type &&
98 gt != TypeManager.generic_ienumerable_type)
99 return false;
101 Type arg_type = TypeManager.GetTypeArguments(list)[0];
102 Type element_type = TypeManager.GetElementType(array);
104 if (element_type == arg_type)
105 return true;
107 if (MyEmptyExpr == null)
108 MyEmptyExpr = new EmptyExpression(element_type);
109 else
110 MyEmptyExpr.SetType(element_type);
112 return ImplicitReferenceConversionExists(MyEmptyExpr, arg_type) || ExplicitReferenceConversionExists(element_type, arg_type);
115 static Expression ImplicitTypeParameterConversion (Expression expr,
116 Type target_type)
118 Type expr_type = expr.Type;
120 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
122 if (gc == null) {
123 if (target_type == TypeManager.object_type)
124 return new BoxedCast (expr, target_type);
126 return null;
129 // We're converting from a type parameter which is known to be a reference type.
130 Type base_type = TypeParam_EffectiveBaseType (gc);
132 if (TypeManager.IsSubclassOf (base_type, target_type))
133 return new ClassCast (expr, target_type);
135 if (target_type.IsInterface) {
136 if (TypeManager.ImplementsInterface (base_type, target_type))
137 return new ClassCast (expr, target_type);
139 foreach (Type t in gc.InterfaceConstraints) {
140 if (TypeManager.IsSubclassOf (t, target_type))
141 return new ClassCast (expr, target_type);
142 if (TypeManager.ImplementsInterface (t, target_type))
143 return new ClassCast (expr, target_type);
147 foreach (Type t in gc.InterfaceConstraints) {
148 if (!TypeManager.IsGenericParameter (t))
149 continue;
150 if (TypeManager.IsSubclassOf (t, target_type))
151 return new ClassCast (expr, target_type);
152 if (TypeManager.ImplementsInterface (t, target_type))
153 return new ClassCast (expr, target_type);
156 return null;
159 static bool ImplicitTypeParameterBoxingConversion (Type expr_type, Type target_type,
160 out bool use_class_cast)
162 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
164 if (gc == null) {
165 use_class_cast = false;
166 return target_type == TypeManager.object_type;
169 use_class_cast = true;
171 if (!gc.HasReferenceTypeConstraint)
172 return false;
174 // We're converting from a type parameter which is known to be a reference type.
175 Type base_type = TypeParam_EffectiveBaseType (gc);
177 if (TypeManager.IsSubclassOf (base_type, target_type))
178 return true;
180 if (target_type.IsInterface) {
181 if (TypeManager.ImplementsInterface (base_type, target_type))
182 return true;
184 foreach (Type t in gc.InterfaceConstraints) {
185 if (TypeManager.IsSubclassOf (t, target_type))
186 return true;
187 if (TypeManager.ImplementsInterface (t, target_type))
188 return true;
192 foreach (Type t in gc.InterfaceConstraints) {
193 if (!TypeManager.IsGenericParameter (t))
194 continue;
195 if (TypeManager.IsSubclassOf (t, target_type))
196 return true;
197 if (TypeManager.ImplementsInterface (t, target_type))
198 return true;
201 use_class_cast = false;
202 return false;
205 static Expression ExplicitTypeParameterConversion (Expression source, Type source_type, Type target_type)
207 if (TypeManager.IsGenericParameter (target_type)) {
208 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
209 if (gc == null)
210 return null;
212 foreach (Type iface in gc.InterfaceConstraints) {
213 if (!TypeManager.IsGenericParameter (iface))
214 continue;
216 if (TypeManager.IsSubclassOf (source_type, iface))
217 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
221 if (target_type.IsInterface)
222 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
224 return null;
227 static Expression ImplicitReferenceConversion (Expression expr, Type target_type, bool explicit_cast)
229 Type expr_type = expr.Type;
231 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
232 // if we are a method group, emit a warning
234 expr.Emit (null);
237 if (expr_type == TypeManager.void_type)
238 return null;
240 if (TypeManager.IsGenericParameter (expr_type))
241 return ImplicitTypeParameterConversion (expr, target_type);
244 // from the null type to any reference-type.
246 NullLiteral nl = expr as NullLiteral;
247 if (nl != null) {
248 return nl.ConvertImplicitly(target_type);
251 if (ImplicitReferenceConversionExists (expr, target_type)) {
253 // Avoid wrapping implicitly convertible reference type
255 if (!explicit_cast)
256 return expr;
258 return EmptyCast.Create (expr, target_type);
261 bool use_class_cast;
262 if (ImplicitBoxingConversionExists (expr, target_type, out use_class_cast)) {
263 if (use_class_cast)
264 return new ClassCast (expr, target_type);
265 else
266 return new BoxedCast (expr, target_type);
269 return null;
273 // 6.1.6 Implicit reference conversions
275 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
277 if (TypeManager.IsStruct (target_type))
278 return false;
280 Type expr_type = expr.Type;
282 // from the null type to any reference-type.
283 if (expr_type == TypeManager.null_type)
284 return target_type != InternalType.AnonymousMethod;
286 if (TypeManager.IsGenericParameter (expr_type))
287 return ImplicitTypeParameterConversion (expr, target_type) != null;
290 // notice that it is possible to write "ValueType v = 1", the ValueType here
291 // is an abstract class, and not really a value type, so we apply the same rules.
293 if (target_type == TypeManager.object_type || target_type == InternalType.Dynamic) {
295 // A pointer type cannot be converted to object
297 if (expr_type.IsPointer)
298 return false;
300 if (TypeManager.IsValueType (expr_type))
301 return false;
303 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
304 // No mcs internal types are convertible
305 return expr_type.Module != typeof (Convert).Module;
308 return false;
309 } else if (target_type == TypeManager.value_type) {
310 return expr_type == TypeManager.enum_type;
311 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
313 // Special case: enumeration to System.Enum.
314 // System.Enum is not a value type, it is a class, so we need
315 // a boxing conversion
317 if (target_type == TypeManager.enum_type || TypeManager.IsGenericParameter (expr_type))
318 return false;
320 return true;
323 // This code is kind of mirrored inside ImplicitStandardConversionExists
324 // with the small distinction that we only probe there
326 // Always ensure that the code here and there is in sync
328 // from any class-type S to any interface-type T.
329 if (target_type.IsInterface) {
330 if (TypeManager.ImplementsInterface (expr_type, target_type)){
331 return !TypeManager.IsGenericParameter (expr_type) &&
332 !TypeManager.IsValueType (expr_type);
336 if (expr_type.IsArray) {
337 // from an array-type S to an array-type of type T
338 if (target_type.IsArray && expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
341 // Both SE and TE are reference-types
343 Type expr_element_type = TypeManager.GetElementType (expr_type);
344 if (!TypeManager.IsReferenceType (expr_element_type))
345 return false;
347 Type target_element_type = TypeManager.GetElementType (target_type);
348 if (!TypeManager.IsReferenceType (target_element_type))
349 return false;
351 if (MyEmptyExpr == null)
352 MyEmptyExpr = new EmptyExpression (expr_element_type);
353 else
354 MyEmptyExpr.SetType (expr_element_type);
356 return ImplicitStandardConversionExists (MyEmptyExpr, target_element_type);
359 // from an array-type to System.Array
360 if (target_type == TypeManager.array_type)
361 return true;
363 // from an array-type of type T to IList<T>
364 if (Array_To_IList (expr_type, target_type, false))
365 return true;
367 return false;
370 if (TypeManager.IsVariantOf (expr_type, target_type))
371 return true;
373 // from any interface type S to interface-type T.
374 if (expr_type.IsInterface && target_type.IsInterface) {
375 return TypeManager.ImplementsInterface (expr_type, target_type);
378 // from any delegate type to System.Delegate
379 if (target_type == TypeManager.delegate_type &&
380 (expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)))
381 return true;
383 if (TypeManager.IsEqual (expr_type, target_type))
384 return true;
386 return false;
389 public static bool ImplicitBoxingConversionExists (Expression expr, Type target_type,
390 out bool use_class_cast)
392 Type expr_type = expr.Type;
393 use_class_cast = false;
396 // From any value-type to the type object.
398 if (target_type == TypeManager.object_type || target_type == InternalType.Dynamic) {
400 // A pointer type cannot be converted to object
402 if (expr_type.IsPointer)
403 return false;
405 return TypeManager.IsValueType (expr_type);
409 // From any value-type to the type System.ValueType.
411 if (target_type == TypeManager.value_type)
412 return TypeManager.IsValueType (expr_type);
414 if (target_type == TypeManager.enum_type) {
416 // From any enum-type to the type System.Enum.
418 if (TypeManager.IsEnumType (expr_type))
419 return true;
421 // From any nullable-type with an underlying enum-type to the type System.Enum
423 if (TypeManager.IsNullableType (expr_type))
424 return TypeManager.IsEnumType (TypeManager.GetTypeArguments (expr_type) [0]);
427 if (TypeManager.IsSubclassOf (expr_type, target_type)) {
429 // Don't box same type arguments
431 if (TypeManager.IsGenericParameter (expr_type) && expr_type != target_type)
432 return true;
434 return false;
437 // This code is kind of mirrored inside ImplicitStandardConversionExists
438 // with the small distinction that we only probe there
440 // Always ensure that the code here and there is in sync
442 // from any class-type S to any interface-type T.
443 if (target_type.IsInterface) {
444 if (TypeManager.ImplementsInterface (expr_type, target_type))
445 return TypeManager.IsGenericParameter (expr_type) ||
446 TypeManager.IsValueType (expr_type);
449 if (TypeManager.IsGenericParameter (expr_type))
450 return ImplicitTypeParameterBoxingConversion (
451 expr_type, target_type, out use_class_cast);
453 return false;
456 public static Expression ImplicitNulableConversion (EmitContext ec, Expression expr, Type target_type)
458 Type expr_type = expr.Type;
461 // From null to any nullable type
463 if (expr_type == TypeManager.null_type)
464 return ec == null ? EmptyExpression.Null : Nullable.LiftedNull.Create (target_type, expr.Location);
466 Type target = TypeManager.GetTypeArguments (target_type)[0];
467 Expression e;
469 // S? -> T?
470 if (TypeManager.IsNullableType (expr_type)) {
471 Type etype = TypeManager.GetTypeArguments (expr_type)[0];
473 if (ec == null)
474 return ImplicitConversionExists (ec, new EmptyExpression (etype), target) ? EmptyExpression.Null : null;
476 Expression unwrap = Nullable.Unwrap.Create (expr);
477 e = ImplicitConversion (ec, unwrap, target, expr.Location);
478 if (e == null)
479 return null;
481 return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
484 // S -> T?
485 if (ec == null)
486 return ImplicitConversionExists (ec, expr, target) ? EmptyExpression.Null : null;
488 e = ImplicitConversion (ec, expr, target, expr.Location);
489 if (e != null)
490 return Nullable.Wrap.Create (e, target_type);
492 return null;
495 /// <summary>
496 /// Implicit Numeric Conversions.
498 /// expr is the expression to convert, returns a new expression of type
499 /// target_type or null if an implicit conversion is not possible.
500 /// </summary>
501 public static Expression ImplicitNumericConversion (Expression expr, Type target_type)
503 return ImplicitNumericConversion (expr, expr.Type, target_type);
506 static Expression ImplicitNumericConversion (Expression expr, Type expr_type, Type target_type)
508 if (expr_type == TypeManager.sbyte_type){
510 // From sbyte to short, int, long, float, double, decimal
512 if (target_type == TypeManager.int32_type)
513 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
514 if (target_type == TypeManager.int64_type)
515 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
516 if (target_type == TypeManager.double_type)
517 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
518 if (target_type == TypeManager.float_type)
519 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
520 if (target_type == TypeManager.short_type)
521 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
522 if (target_type == TypeManager.decimal_type)
523 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
524 } else if (expr_type == TypeManager.byte_type){
526 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
528 if (target_type == TypeManager.int32_type || target_type == TypeManager.uint32_type ||
529 target_type == TypeManager.short_type || target_type == TypeManager.ushort_type)
530 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
532 if (target_type == TypeManager.uint64_type)
533 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
534 if (target_type == TypeManager.int64_type)
535 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
536 if (target_type == TypeManager.float_type)
537 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
538 if (target_type == TypeManager.double_type)
539 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
540 if (target_type == TypeManager.decimal_type)
541 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
543 } else if (expr_type == TypeManager.short_type){
545 // From short to int, long, float, double, decimal
547 if (target_type == TypeManager.int32_type)
548 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
549 if (target_type == TypeManager.int64_type)
550 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
551 if (target_type == TypeManager.double_type)
552 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
553 if (target_type == TypeManager.float_type)
554 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
555 if (target_type == TypeManager.decimal_type)
556 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
558 } else if (expr_type == TypeManager.ushort_type){
560 // From ushort to int, uint, long, ulong, float, double, decimal
562 if (target_type == TypeManager.int32_type || target_type == TypeManager.uint32_type)
563 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
565 if (target_type == TypeManager.uint64_type)
566 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
567 if (target_type == TypeManager.int64_type)
568 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
569 if (target_type == TypeManager.double_type)
570 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
571 if (target_type == TypeManager.float_type)
572 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
573 if (target_type == TypeManager.decimal_type)
574 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
575 } else if (expr_type == TypeManager.int32_type){
577 // From int to long, float, double, decimal
579 if (target_type == TypeManager.int64_type)
580 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
581 if (target_type == TypeManager.double_type)
582 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
583 if (target_type == TypeManager.float_type)
584 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
585 if (target_type == TypeManager.decimal_type)
586 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
587 } else if (expr_type == TypeManager.uint32_type){
589 // From uint to long, ulong, float, double, decimal
591 if (target_type == TypeManager.int64_type)
592 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
593 if (target_type == TypeManager.uint64_type)
594 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
595 if (target_type == TypeManager.double_type)
596 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
597 if (target_type == TypeManager.float_type)
598 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
599 if (target_type == TypeManager.decimal_type)
600 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
601 } else if (expr_type == TypeManager.int64_type){
603 // From long/ulong to float, double
605 if (target_type == TypeManager.double_type)
606 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
607 if (target_type == TypeManager.float_type)
608 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
609 if (target_type == TypeManager.decimal_type)
610 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
611 } else if (expr_type == TypeManager.uint64_type){
613 // From ulong to float, double
615 if (target_type == TypeManager.double_type)
616 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R8);
617 if (target_type == TypeManager.float_type)
618 return expr == null ? EmptyExpression.Null : new OpcodeCast (new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un), target_type, OpCodes.Conv_R4);
619 if (target_type == TypeManager.decimal_type)
620 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
621 } else if (expr_type == TypeManager.char_type){
623 // From char to ushort, int, uint, long, ulong, float, double, decimal
625 if ((target_type == TypeManager.ushort_type) ||
626 (target_type == TypeManager.int32_type) ||
627 (target_type == TypeManager.uint32_type))
628 return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
629 if (target_type == TypeManager.uint64_type)
630 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
631 if (target_type == TypeManager.int64_type)
632 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
633 if (target_type == TypeManager.float_type)
634 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
635 if (target_type == TypeManager.double_type)
636 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
637 if (target_type == TypeManager.decimal_type)
638 return expr == null ? EmptyExpression.Null : new CastToDecimal (expr);
639 } else if (expr_type == TypeManager.float_type){
641 // float to double
643 if (target_type == TypeManager.double_type)
644 return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
647 return null;
650 /// <summary>
651 /// Same as ImplicitStandardConversionExists except that it also looks at
652 /// implicit user defined conversions - needed for overload resolution
653 /// </summary>
654 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
656 if (ImplicitStandardConversionExists (expr, target_type))
657 return true;
659 if (expr.Type == InternalType.AnonymousMethod) {
660 if (!TypeManager.IsDelegateType (target_type) &&
661 TypeManager.DropGenericTypeArguments (target_type) != TypeManager.expression_type)
662 return false;
664 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
665 return ame.ImplicitStandardConversionExists (ec, target_type);
668 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
671 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
673 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
676 /// <summary>
677 /// Determines if a standard implicit conversion exists from
678 /// expr_type to target_type
680 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
681 /// </summary>
682 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
684 Type expr_type = expr.Type;
686 if (expr_type == TypeManager.null_type) {
687 NullLiteral nl = expr as NullLiteral;
688 if (nl != null)
689 return nl.ConvertImplicitly (target_type) != null;
692 if (expr_type == TypeManager.void_type)
693 return false;
695 if (expr.eclass == ExprClass.MethodGroup) {
696 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1) {
697 MethodGroupExpr mg = expr as MethodGroupExpr;
698 if (mg != null) {
699 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
703 return false;
706 //Console.WriteLine ("Expr is {0}", expr);
707 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
708 if (TypeManager.IsEqual (expr_type, target_type))
709 return true;
711 if (TypeManager.IsNullableType (target_type))
712 return ImplicitNulableConversion (null, expr, target_type) != null;
714 // First numeric conversions
715 if (ImplicitNumericConversion (null, expr_type, target_type) != null)
716 return true;
718 if (ImplicitReferenceConversionExists (expr, target_type))
719 return true;
721 bool use_class_cast;
722 if (ImplicitBoxingConversionExists (expr, target_type, out use_class_cast))
723 return true;
726 // Implicit Constant Expression Conversions
728 if (expr is IntConstant){
729 int value = ((IntConstant) expr).Value;
731 if (target_type == TypeManager.sbyte_type){
732 if (value >= SByte.MinValue && value <= SByte.MaxValue)
733 return true;
734 } else if (target_type == TypeManager.byte_type){
735 if (value >= 0 && value <= Byte.MaxValue)
736 return true;
737 } else if (target_type == TypeManager.short_type){
738 if (value >= Int16.MinValue && value <= Int16.MaxValue)
739 return true;
740 } else if (target_type == TypeManager.ushort_type){
741 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
742 return true;
743 } else if (target_type == TypeManager.uint32_type){
744 if (value >= 0)
745 return true;
746 } else if (target_type == TypeManager.uint64_type){
748 // we can optimize this case: a positive int32
749 // always fits on a uint64. But we need an opcode
750 // to do it.
752 if (value >= 0)
753 return true;
756 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
757 return true;
760 if (expr is LongConstant && target_type == TypeManager.uint64_type){
762 // Try the implicit constant expression conversion
763 // from long to ulong, instead of a nice routine,
764 // we just inline it
766 long v = ((LongConstant) expr).Value;
767 if (v >= 0)
768 return true;
772 // If `expr_type' implements `target_type' (which is an iface)
773 // see TryImplicitIntConversion
775 if (target_type.IsInterface && TypeManager.ImplementsInterface (expr_type, target_type))
776 return true;
778 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
779 return true;
781 // Conversion from __arglist to System.ArgIterator
782 if (expr_type == InternalType.Arglist)
783 return target_type == TypeManager.arg_iterator_type;
785 return false;
788 /// <summary>
789 /// Finds "most encompassed type" according to the spec (13.4.2)
790 /// amongst the methods in the MethodGroupExpr
791 /// </summary>
792 static Type FindMostEncompassedType (ArrayList types)
794 Type best = null;
796 if (types.Count == 0)
797 return null;
799 if (types.Count == 1)
800 return (Type) types [0];
802 EmptyExpression expr = EmptyExpression.Grab ();
804 foreach (Type t in types) {
805 if (best == null) {
806 best = t;
807 continue;
810 expr.SetType (t);
811 if (ImplicitStandardConversionExists (expr, best))
812 best = t;
815 expr.SetType (best);
816 foreach (Type t in types) {
817 if (best == t)
818 continue;
819 if (!ImplicitStandardConversionExists (expr, t)) {
820 best = null;
821 break;
825 EmptyExpression.Release (expr);
827 return best;
830 /// <summary>
831 /// Finds "most encompassing type" according to the spec (13.4.2)
832 /// amongst the types in the given set
833 /// </summary>
834 static Type FindMostEncompassingType (ArrayList types)
836 Type best = null;
838 if (types.Count == 0)
839 return null;
841 if (types.Count == 1)
842 return (Type) types [0];
844 EmptyExpression expr = EmptyExpression.Grab ();
846 foreach (Type t in types) {
847 if (best == null) {
848 best = t;
849 continue;
852 expr.SetType (best);
853 if (ImplicitStandardConversionExists (expr, t))
854 best = t;
857 foreach (Type t in types) {
858 if (best == t)
859 continue;
860 expr.SetType (t);
861 if (!ImplicitStandardConversionExists (expr, best)) {
862 best = null;
863 break;
867 EmptyExpression.Release (expr);
869 return best;
872 /// <summary>
873 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
874 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
875 /// for explicit and implicit conversion operators.
876 /// </summary>
877 static public Type FindMostSpecificSource (IList list,
878 Expression source, bool apply_explicit_conv_rules)
880 ArrayList src_types_set = new ArrayList ();
883 // If any operator converts from S then Sx = S
885 Type source_type = source.Type;
886 foreach (MethodBase mb in list){
887 AParametersCollection pd = TypeManager.GetParameterData (mb);
888 Type param_type = pd.Types [0];
890 if (param_type == source_type)
891 return param_type;
893 src_types_set.Add (param_type);
897 // Explicit Conv rules
899 if (apply_explicit_conv_rules) {
900 ArrayList candidate_set = new ArrayList ();
902 foreach (Type param_type in src_types_set){
903 if (ImplicitStandardConversionExists (source, param_type))
904 candidate_set.Add (param_type);
907 if (candidate_set.Count != 0)
908 return FindMostEncompassedType (candidate_set);
912 // Final case
914 if (apply_explicit_conv_rules)
915 return FindMostEncompassingType (src_types_set);
916 else
917 return FindMostEncompassedType (src_types_set);
920 /// <summary>
921 /// Finds the most specific target Tx according to section 13.4.4
922 /// </summary>
923 static public Type FindMostSpecificTarget (IList list,
924 Type target, bool apply_explicit_conv_rules)
926 ArrayList tgt_types_set = new ArrayList ();
929 // If any operator converts to T then Tx = T
931 foreach (MethodInfo mi in list){
932 Type ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
933 if (ret_type == target)
934 return ret_type;
936 tgt_types_set.Add (ret_type);
940 // Explicit conv rules
942 if (apply_explicit_conv_rules) {
943 ArrayList candidate_set = new ArrayList ();
945 EmptyExpression expr = EmptyExpression.Grab ();
947 foreach (Type ret_type in tgt_types_set){
948 expr.SetType (ret_type);
950 if (ImplicitStandardConversionExists (expr, target))
951 candidate_set.Add (ret_type);
954 EmptyExpression.Release (expr);
956 if (candidate_set.Count != 0)
957 return FindMostEncompassingType (candidate_set);
961 // Okay, final case !
963 if (apply_explicit_conv_rules)
964 return FindMostEncompassedType (tgt_types_set);
965 else
966 return FindMostEncompassingType (tgt_types_set);
969 /// <summary>
970 /// User-defined Implicit conversions
971 /// </summary>
972 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
973 Type target, Location loc)
975 Expression expr = UserDefinedConversion (ec, source, target, loc, false);
976 if (expr != null && !TypeManager.IsEqual (expr.Type, target))
977 expr = ImplicitConversionStandard (ec, expr, target, loc);
979 return expr;
982 /// <summary>
983 /// User-defined Explicit conversions
984 /// </summary>
985 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
986 Type target, Location loc)
988 Expression expr = UserDefinedConversion (ec, source, target, loc, true);
989 if (expr != null && !TypeManager.IsEqual (expr.Type, target))
990 expr = ExplicitConversionStandard (ec, expr, target, loc);
992 return expr;
995 static void AddConversionOperators (ArrayList list,
996 Expression source, Type target_type,
997 bool look_for_explicit,
998 MethodGroupExpr mg)
1000 if (mg == null)
1001 return;
1003 Type source_type = source.Type;
1004 EmptyExpression expr = EmptyExpression.Grab ();
1007 // LAMESPEC: Undocumented IntPtr/UIntPtr conversions
1008 // IntPtr -> uint uses int
1009 // UIntPtr -> long uses ulong
1011 if (source_type == TypeManager.intptr_type) {
1012 if (target_type == TypeManager.uint32_type)
1013 target_type = TypeManager.int32_type;
1014 } else if (source_type == TypeManager.uintptr_type) {
1015 if (target_type == TypeManager.int64_type)
1016 target_type = TypeManager.uint64_type;
1019 foreach (MethodInfo m in mg.Methods) {
1020 AParametersCollection pd = TypeManager.GetParameterData (m);
1021 Type return_type = TypeManager.TypeToCoreType (m.ReturnType);
1022 Type arg_type = pd.Types [0];
1024 if (source_type != arg_type) {
1025 if (!ImplicitStandardConversionExists (source, arg_type)) {
1026 if (!look_for_explicit)
1027 continue;
1028 expr.SetType (arg_type);
1029 if (!ImplicitStandardConversionExists (expr, source_type))
1030 continue;
1034 if (target_type != return_type) {
1035 expr.SetType (return_type);
1036 if (!ImplicitStandardConversionExists (expr, target_type)) {
1037 if (!look_for_explicit)
1038 continue;
1039 expr.SetType (target_type);
1040 if (!ImplicitStandardConversionExists (expr, return_type))
1041 continue;
1045 // See LAMESPEC: Exclude IntPtr -> int conversion
1046 if (source_type == TypeManager.uintptr_type && return_type == TypeManager.uint32_type)
1047 continue;
1049 list.Add (m);
1052 EmptyExpression.Release (expr);
1055 /// <summary>
1056 /// Compute the user-defined conversion operator from source_type to target_type.
1057 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1058 /// </summary>
1059 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1061 ArrayList ops = new ArrayList (4);
1063 Type source_type = source.Type;
1065 if (source_type != TypeManager.decimal_type) {
1066 AddConversionOperators (ops, source, target_type, look_for_explicit,
1067 Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1068 if (look_for_explicit) {
1069 AddConversionOperators (ops, source, target_type, look_for_explicit,
1070 Expression.MethodLookup (
1071 container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1075 if (target_type != TypeManager.decimal_type) {
1076 AddConversionOperators (ops, source, target_type, look_for_explicit,
1077 Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1078 if (look_for_explicit) {
1079 AddConversionOperators (ops, source, target_type, look_for_explicit,
1080 Expression.MethodLookup (
1081 container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1085 if (ops.Count == 0)
1086 return null;
1088 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1089 if (most_specific_source == null)
1090 return null;
1092 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1093 if (most_specific_target == null)
1094 return null;
1096 MethodInfo method = null;
1098 foreach (MethodInfo m in ops) {
1099 if (TypeManager.TypeToCoreType (m.ReturnType) != most_specific_target)
1100 continue;
1101 if (TypeManager.GetParameterData (m).Types [0] != most_specific_source)
1102 continue;
1103 // Ambiguous: more than one conversion operator satisfies the signature.
1104 if (method != null)
1105 return null;
1106 method = m;
1109 return method;
1112 /// <summary>
1113 /// User-defined conversions
1114 /// </summary>
1115 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1116 Type target, Location loc,
1117 bool look_for_explicit)
1119 Type source_type = source.Type;
1120 MethodInfo method = null;
1122 object o;
1123 DoubleHash hash;
1124 if (look_for_explicit) {
1125 hash = explicit_conv;
1126 } else {
1127 // Implicit user operators cannot convert to interfaces
1128 if (target.IsInterface)
1129 return null;
1131 hash = implicit_conv;
1134 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1135 method = (MethodInfo) o;
1136 } else {
1137 method = GetConversionOperator (null, source, target, look_for_explicit);
1138 if (!(source is Constant))
1139 hash.Insert (source_type, target, method);
1142 if (method == null)
1143 return null;
1145 Type most_specific_source = TypeManager.GetParameterData (method).Types [0];
1148 // This will do the conversion to the best match that we
1149 // found. Now we need to perform an implict standard conversion
1150 // if the best match was not the type that we were requested
1151 // by target.
1153 if (look_for_explicit)
1154 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1155 else
1156 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1158 if (source == null)
1159 return null;
1161 return new UserCast (method, source, loc).DoResolve (ec);
1164 /// <summary>
1165 /// Converts implicitly the resolved expression `expr' into the
1166 /// `target_type'. It returns a new expression that can be used
1167 /// in a context that expects a `target_type'.
1168 /// </summary>
1169 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1170 Type target_type, Location loc)
1172 Expression e;
1174 if (target_type == null)
1175 throw new Exception ("Target type is null");
1177 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1178 if (e != null)
1179 return e;
1181 e = ImplicitUserConversion (ec, expr, target_type, loc);
1182 if (e != null)
1183 return e;
1185 return null;
1189 /// <summary>
1190 /// Attempts to apply the `Standard Implicit
1191 /// Conversion' rules to the expression `expr' into
1192 /// the `target_type'. It returns a new expression
1193 /// that can be used in a context that expects a
1194 /// `target_type'.
1196 /// This is different from `ImplicitConversion' in that the
1197 /// user defined implicit conversions are excluded.
1198 /// </summary>
1199 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1200 Type target_type, Location loc)
1202 return ImplicitConversionStandard (ec, expr, target_type, loc, false);
1205 static Expression ImplicitConversionStandard (EmitContext ec, Expression expr, Type target_type, Location loc, bool explicit_cast)
1207 if (expr.eclass == ExprClass.MethodGroup){
1208 if (!TypeManager.IsDelegateType (target_type)){
1209 return null;
1213 // Only allow anonymous method conversions on post ISO_1
1215 if (RootContext.Version != LanguageVersion.ISO_1){
1216 MethodGroupExpr mg = expr as MethodGroupExpr;
1217 if (mg != null)
1218 return ImplicitDelegateCreation.Create (
1219 ec, mg, target_type, loc);
1223 Type expr_type = expr.Type;
1224 Expression e;
1226 if (expr_type.Equals (target_type)) {
1227 if (expr_type != TypeManager.null_type && expr_type != InternalType.AnonymousMethod)
1228 return expr;
1229 return null;
1232 if (TypeManager.IsVariantOf (expr_type, target_type)) {
1233 return expr;
1236 if (TypeManager.IsNullableType (target_type))
1237 return ImplicitNulableConversion (ec, expr, target_type);
1240 // Attempt to do the implicit constant expression conversions
1242 Constant c = expr as Constant;
1243 if (c != null) {
1244 try {
1245 c = c.ConvertImplicitly (target_type);
1246 } catch {
1247 Console.WriteLine ("Conversion error happened in line {0}", loc);
1248 throw;
1250 if (c != null)
1251 return c;
1254 e = ImplicitNumericConversion (expr, expr_type, target_type);
1255 if (e != null)
1256 return e;
1258 e = ImplicitReferenceConversion (expr, target_type, explicit_cast);
1259 if (e != null)
1260 return e;
1262 if (expr is IntConstant && TypeManager.IsEnumType (target_type)){
1263 Constant i = (Constant) expr;
1265 // LAMESPEC: Conversion from any 0 constant is allowed
1267 // An implicit enumeration conversion permits the decimal-integer-literal 0
1268 // to be converted to any enum-type and to any nullable-type whose underlying
1269 // type is an enum-type
1271 if (i.IsDefaultValue)
1272 return new EnumConstant (i, target_type);
1275 if (ec.InUnsafe) {
1276 if (expr_type.IsPointer){
1277 if (target_type == TypeManager.void_ptr_type)
1278 return EmptyCast.Create (expr, target_type);
1281 // yep, comparing pointer types cant be done with
1282 // t1 == t2, we have to compare their element types.
1284 if (target_type.IsPointer){
1285 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1286 return expr;
1288 //return null;
1292 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1293 return EmptyCast.Create (new NullPointer (loc), target_type);
1296 if (expr_type == InternalType.AnonymousMethod){
1297 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1298 Expression am = ame.Compatible (ec, target_type);
1299 if (am != null)
1300 return am.DoResolve (ec);
1303 if (expr_type == InternalType.Arglist && target_type == TypeManager.arg_iterator_type)
1304 return expr;
1306 return null;
1309 /// <summary>
1310 /// Attempts to implicitly convert `source' into `target_type', using
1311 /// ImplicitConversion. If there is no implicit conversion, then
1312 /// an error is signaled
1313 /// </summary>
1314 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1315 Type target_type, Location loc)
1317 Expression e = ImplicitConversion (ec, source, target_type, loc);
1318 if (e != null)
1319 return e;
1321 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1322 return null;
1325 /// <summary>
1326 /// Performs the explicit numeric conversions
1328 /// There are a few conversions that are not part of the C# standard,
1329 /// they were interim hacks in the C# compiler that were supposed to
1330 /// become explicit operators in the UIntPtr class and IntPtr class,
1331 /// but for historical reasons it did not happen, so the C# compiler
1332 /// ended up with these special hacks.
1334 /// See bug 59800 for details.
1336 /// The conversion are:
1337 /// UIntPtr->SByte
1338 /// UIntPtr->Int16
1339 /// UIntPtr->Int32
1340 /// IntPtr->UInt64
1341 /// UInt64->IntPtr
1342 /// SByte->UIntPtr
1343 /// Int16->UIntPtr
1345 /// </summary>
1346 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1348 Type expr_type = expr.Type;
1349 Type real_target_type = target_type;
1351 if (expr_type == TypeManager.sbyte_type){
1353 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1355 if (real_target_type == TypeManager.byte_type)
1356 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1357 if (real_target_type == TypeManager.ushort_type)
1358 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1359 if (real_target_type == TypeManager.uint32_type)
1360 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1361 if (real_target_type == TypeManager.uint64_type)
1362 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1363 if (real_target_type == TypeManager.char_type)
1364 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1366 // One of the built-in conversions that belonged in the class library
1367 if (real_target_type == TypeManager.uintptr_type){
1368 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1370 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1372 } else if (expr_type == TypeManager.byte_type){
1374 // From byte to sbyte and char
1376 if (real_target_type == TypeManager.sbyte_type)
1377 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1378 if (real_target_type == TypeManager.char_type)
1379 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1380 } else if (expr_type == TypeManager.short_type){
1382 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1384 if (real_target_type == TypeManager.sbyte_type)
1385 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1386 if (real_target_type == TypeManager.byte_type)
1387 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1388 if (real_target_type == TypeManager.ushort_type)
1389 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1390 if (real_target_type == TypeManager.uint32_type)
1391 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1392 if (real_target_type == TypeManager.uint64_type)
1393 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1394 if (real_target_type == TypeManager.char_type)
1395 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1397 // One of the built-in conversions that belonged in the class library
1398 if (real_target_type == TypeManager.uintptr_type){
1399 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1401 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1403 } else if (expr_type == TypeManager.ushort_type){
1405 // From ushort to sbyte, byte, short, char
1407 if (real_target_type == TypeManager.sbyte_type)
1408 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1409 if (real_target_type == TypeManager.byte_type)
1410 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1411 if (real_target_type == TypeManager.short_type)
1412 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1413 if (real_target_type == TypeManager.char_type)
1414 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1415 } else if (expr_type == TypeManager.int32_type){
1417 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1419 if (real_target_type == TypeManager.sbyte_type)
1420 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1421 if (real_target_type == TypeManager.byte_type)
1422 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1423 if (real_target_type == TypeManager.short_type)
1424 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1425 if (real_target_type == TypeManager.ushort_type)
1426 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1427 if (real_target_type == TypeManager.uint32_type)
1428 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1429 if (real_target_type == TypeManager.uint64_type)
1430 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1431 if (real_target_type == TypeManager.char_type)
1432 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1434 // One of the built-in conversions that belonged in the class library
1435 if (real_target_type == TypeManager.uintptr_type){
1436 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1438 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1440 } else if (expr_type == TypeManager.uint32_type){
1442 // From uint to sbyte, byte, short, ushort, int, char
1444 if (real_target_type == TypeManager.sbyte_type)
1445 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1446 if (real_target_type == TypeManager.byte_type)
1447 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1448 if (real_target_type == TypeManager.short_type)
1449 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1450 if (real_target_type == TypeManager.ushort_type)
1451 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1452 if (real_target_type == TypeManager.int32_type)
1453 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1454 if (real_target_type == TypeManager.char_type)
1455 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1456 } else if (expr_type == TypeManager.int64_type){
1458 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1460 if (real_target_type == TypeManager.sbyte_type)
1461 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1462 if (real_target_type == TypeManager.byte_type)
1463 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1464 if (real_target_type == TypeManager.short_type)
1465 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1466 if (real_target_type == TypeManager.ushort_type)
1467 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1468 if (real_target_type == TypeManager.int32_type)
1469 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1470 if (real_target_type == TypeManager.uint32_type)
1471 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1472 if (real_target_type == TypeManager.uint64_type)
1473 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1474 if (real_target_type == TypeManager.char_type)
1475 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1476 } else if (expr_type == TypeManager.uint64_type){
1478 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1480 if (real_target_type == TypeManager.sbyte_type)
1481 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1482 if (real_target_type == TypeManager.byte_type)
1483 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1484 if (real_target_type == TypeManager.short_type)
1485 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1486 if (real_target_type == TypeManager.ushort_type)
1487 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1488 if (real_target_type == TypeManager.int32_type)
1489 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1490 if (real_target_type == TypeManager.uint32_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1492 if (real_target_type == TypeManager.int64_type)
1493 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1494 if (real_target_type == TypeManager.char_type)
1495 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1497 // One of the built-in conversions that belonged in the class library
1498 if (real_target_type == TypeManager.intptr_type){
1499 return new OperatorCast (EmptyCast.Create (expr, TypeManager.int64_type),
1500 TypeManager.intptr_type, true);
1502 } else if (expr_type == TypeManager.char_type){
1504 // From char to sbyte, byte, short
1506 if (real_target_type == TypeManager.sbyte_type)
1507 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1508 if (real_target_type == TypeManager.byte_type)
1509 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1510 if (real_target_type == TypeManager.short_type)
1511 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1512 } else if (expr_type == TypeManager.float_type){
1514 // From float to sbyte, byte, short,
1515 // ushort, int, uint, long, ulong, char
1516 // or decimal
1518 if (real_target_type == TypeManager.sbyte_type)
1519 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1520 if (real_target_type == TypeManager.byte_type)
1521 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1522 if (real_target_type == TypeManager.short_type)
1523 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1524 if (real_target_type == TypeManager.ushort_type)
1525 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1526 if (real_target_type == TypeManager.int32_type)
1527 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1528 if (real_target_type == TypeManager.uint32_type)
1529 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1530 if (real_target_type == TypeManager.int64_type)
1531 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1532 if (real_target_type == TypeManager.uint64_type)
1533 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1534 if (real_target_type == TypeManager.char_type)
1535 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1536 if (real_target_type == TypeManager.decimal_type)
1537 return new CastToDecimal (expr, true);
1538 } else if (expr_type == TypeManager.double_type){
1540 // From double to sbyte, byte, short,
1541 // ushort, int, uint, long, ulong,
1542 // char, float or decimal
1544 if (real_target_type == TypeManager.sbyte_type)
1545 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1546 if (real_target_type == TypeManager.byte_type)
1547 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1548 if (real_target_type == TypeManager.short_type)
1549 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1550 if (real_target_type == TypeManager.ushort_type)
1551 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1552 if (real_target_type == TypeManager.int32_type)
1553 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1554 if (real_target_type == TypeManager.uint32_type)
1555 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1556 if (real_target_type == TypeManager.int64_type)
1557 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1558 if (real_target_type == TypeManager.uint64_type)
1559 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1560 if (real_target_type == TypeManager.char_type)
1561 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1562 if (real_target_type == TypeManager.float_type)
1563 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1564 if (real_target_type == TypeManager.decimal_type)
1565 return new CastToDecimal (expr, true);
1566 } else if (expr_type == TypeManager.uintptr_type){
1568 // Various built-in conversions that belonged in the class library
1570 // from uintptr to sbyte, short, int32
1572 if (real_target_type == TypeManager.sbyte_type){
1573 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1574 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1576 if (real_target_type == TypeManager.short_type){
1577 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1578 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1580 if (real_target_type == TypeManager.int32_type){
1581 return EmptyCast.Create (new OperatorCast (expr, TypeManager.uint32_type, true),
1582 TypeManager.int32_type);
1584 } else if (expr_type == TypeManager.intptr_type){
1585 if (real_target_type == TypeManager.uint64_type){
1586 return EmptyCast.Create (new OperatorCast (expr, TypeManager.int64_type, true),
1587 TypeManager.uint64_type);
1589 } else if (expr_type == TypeManager.decimal_type) {
1590 return new CastFromDecimal (expr, target_type).Resolve ();
1592 return null;
1595 /// <summary>
1596 /// Returns whether an explicit reference conversion can be performed
1597 /// from source_type to target_type
1598 /// </summary>
1599 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1601 Expression e = ExplicitReferenceConversion (null, source_type, target_type);
1602 if (e == null)
1603 return false;
1605 if (e == EmptyExpression.Null)
1606 return true;
1608 throw new InternalErrorException ("Invalid probing conversion result");
1611 /// <summary>
1612 /// Implements Explicit Reference conversions
1613 /// </summary>
1614 static Expression ExplicitReferenceConversion (Expression source, Type source_type, Type target_type)
1616 bool target_is_value_type = TypeManager.IsStruct (target_type);
1619 // From object to a generic parameter
1621 if (source_type == TypeManager.object_type && TypeManager.IsGenericParameter (target_type))
1622 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1625 // Explicit type parameter conversion.
1627 if (TypeManager.IsGenericParameter (source_type))
1628 return ExplicitTypeParameterConversion (source, source_type, target_type);
1631 // From object to any reference type or value type (unboxing)
1633 if (source_type == TypeManager.object_type)
1634 return source == null ? EmptyExpression.Null :
1635 target_is_value_type ? (Expression) new UnboxCast (source, target_type) : new ClassCast (source, target_type);
1638 // Unboxing conversion from the types object and System.ValueType to any non-nullable-value-type
1640 if (source_type == TypeManager.value_type && target_is_value_type)
1641 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1644 // From any class S to any class-type T, provided S is a base class of T
1646 if (TypeManager.IsSubclassOf (target_type, source_type))
1647 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1650 // From any class type S to any interface T, provides S is not sealed
1651 // and provided S does not implement T.
1653 if (target_type.IsInterface && !source_type.IsSealed &&
1654 !TypeManager.ImplementsInterface (source_type, target_type)) {
1655 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1659 // From any interface-type S to to any class type T, provided T is not
1660 // sealed, or provided T implements S.
1662 if (source_type.IsInterface) {
1663 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1664 if (target_type.IsClass)
1665 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1668 // Unboxing conversion from any interface-type to any non-nullable-value-type that
1669 // implements the interface-type
1671 return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
1675 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1676 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1678 if (IList_To_Array (source_type, target_type))
1679 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1681 return null;
1684 if (source_type.IsArray) {
1685 if (target_type.IsArray) {
1687 // From System.Array to any array-type
1689 if (source_type == TypeManager.array_type)
1690 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1693 // From an array type S with an element type Se to an array type T with an
1694 // element type Te provided all the following are true:
1695 // * S and T differe only in element type, in other words, S and T
1696 // have the same number of dimensions.
1697 // * Both Se and Te are reference types
1698 // * An explicit reference conversions exist from Se to Te
1700 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1702 source_type = TypeManager.GetElementType (source_type);
1703 if (!TypeManager.IsReferenceType (source_type))
1704 return null;
1706 Type target_type_element = TypeManager.GetElementType (target_type);
1707 if (!TypeManager.IsReferenceType (target_type_element))
1708 return null;
1710 if (ExplicitReferenceConversionExists (source_type, target_type_element))
1711 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1713 return null;
1718 // From a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces,
1719 // provided that there is an explicit reference conversion from S to T
1721 if (Array_To_IList (source_type, target_type, true))
1722 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1724 return null;
1728 // From System delegate to any delegate-type
1730 if (source_type == TypeManager.delegate_type && TypeManager.IsDelegateType (target_type))
1731 return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
1733 return null;
1736 /// <summary>
1737 /// Performs an explicit conversion of the expression `expr' whose
1738 /// type is expr.Type to `target_type'.
1739 /// </summary>
1740 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1741 Type target_type, Location loc)
1743 Type expr_type = expr.Type;
1745 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1746 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc, true);
1747 if (ne != null)
1748 return ne;
1750 if (TypeManager.IsEnumType (expr_type)) {
1751 Expression underlying = EmptyCast.Create (expr, TypeManager.GetEnumUnderlyingType (expr_type));
1752 expr = ExplicitConversionCore (ec, underlying, target_type, loc);
1753 if (expr != null)
1754 return expr;
1756 return ExplicitUserConversion (ec, underlying, target_type, loc);
1759 if (TypeManager.IsEnumType (target_type)){
1761 // Type System.Enum can be unboxed to any enum-type
1763 if (expr_type == TypeManager.enum_type)
1764 return new UnboxCast (expr, target_type);
1766 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.GetEnumUnderlyingType (target_type), loc);
1767 if (ce != null)
1768 return EmptyCast.Create (ce, target_type);
1771 // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
1773 if (expr_type == TypeManager.intptr_type || expr_type == TypeManager.uintptr_type) {
1774 ne = ExplicitUserConversion (ec, expr, TypeManager.GetEnumUnderlyingType (target_type), loc);
1775 if (ne != null)
1776 return ExplicitConversionCore (ec, ne, target_type, loc);
1779 return null;
1782 ne = ExplicitNumericConversion (expr, target_type);
1783 if (ne != null)
1784 return ne;
1787 // Skip the ExplicitReferenceConversion because we can not convert
1788 // from Null to a ValueType, and ExplicitReference wont check against
1789 // null literal explicitly
1791 if (expr_type != TypeManager.null_type){
1792 ne = ExplicitReferenceConversion (expr, expr_type, target_type);
1793 if (ne != null)
1794 return ne;
1797 if (ec.InUnsafe){
1798 ne = ExplicitUnsafe (expr, target_type);
1799 if (ne != null)
1800 return ne;
1803 return null;
1806 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1808 Type expr_type = expr.Type;
1810 if (target_type.IsPointer){
1811 if (expr_type.IsPointer)
1812 return EmptyCast.Create (expr, target_type);
1814 if (expr_type == TypeManager.sbyte_type ||
1815 expr_type == TypeManager.short_type ||
1816 expr_type == TypeManager.int32_type)
1817 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1819 if (expr_type == TypeManager.ushort_type ||
1820 expr_type == TypeManager.uint32_type ||
1821 expr_type == TypeManager.uint64_type || expr_type == TypeManager.int64_type ||
1822 expr_type == TypeManager.byte_type)
1823 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1826 if (expr_type.IsPointer){
1827 if (target_type == TypeManager.sbyte_type)
1828 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1829 else if (target_type == TypeManager.byte_type)
1830 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1831 else if (target_type == TypeManager.short_type)
1832 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1833 else if (target_type == TypeManager.ushort_type)
1834 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1835 else if (target_type == TypeManager.int32_type)
1836 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1837 else if (target_type == TypeManager.uint32_type)
1838 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1839 else if (target_type == TypeManager.uint64_type || target_type == TypeManager.int64_type)
1840 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1842 return null;
1845 /// <summary>
1846 /// Same as ExplicitConversion, only it doesn't include user defined conversions
1847 /// </summary>
1848 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1849 Type target_type, Location l)
1851 int errors = Report.Errors;
1852 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1853 if (Report.Errors > errors)
1854 return null;
1856 if (ne != null)
1857 return ne;
1859 ne = ExplicitNumericConversion (expr, target_type);
1860 if (ne != null)
1861 return ne;
1863 ne = ExplicitReferenceConversion (expr, expr.Type, target_type);
1864 if (ne != null)
1865 return ne;
1867 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1868 return EmptyCast.Create (expr, target_type);
1870 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
1871 return null;
1874 /// <summary>
1875 /// Performs an explicit conversion of the expression `expr' whose
1876 /// type is expr.Type to `target_type'.
1877 /// </summary>
1878 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1879 Type target_type, Location loc)
1881 Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1882 if (e != null)
1883 return e;
1885 Type expr_type = expr.Type;
1886 if (TypeManager.IsNullableType (target_type)) {
1887 if (TypeManager.IsNullableType (expr_type)) {
1888 Type target = TypeManager.GetTypeArguments (target_type)[0];
1889 Expression unwrap = Nullable.Unwrap.Create (expr);
1890 e = ExplicitConversion (ec, unwrap, target, expr.Location);
1891 if (e == null)
1892 return null;
1894 return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
1895 } else if (expr_type == TypeManager.object_type) {
1896 return new UnboxCast (expr, target_type);
1897 } else {
1898 Type target = TypeManager.GetTypeArguments (target_type) [0];
1900 e = ExplicitConversionCore (ec, expr, target, loc);
1901 if (e != null)
1902 return Nullable.Wrap.Create (e, target_type);
1904 } else if (TypeManager.IsNullableType (expr_type)) {
1905 e = Nullable.Unwrap.Create (expr, false);
1907 bool use_class_cast;
1908 if (ImplicitBoxingConversionExists (e, target_type, out use_class_cast))
1909 return new BoxedCast (expr, target_type);
1911 e = ExplicitConversion (ec, e, target_type, loc);
1912 if (e != null)
1913 e = EmptyCast.Create (e, target_type);
1914 return e;
1917 e = ExplicitUserConversion (ec, expr, target_type, loc);
1918 if (e != null)
1919 return e;
1921 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);
1922 return null;