* DataGrid.cs: remove the XXX'ed check at the top of
[mcs.git] / mcs / convert.cs
blobbd0df1c8071a194d61d970d48ed39e3a6b36a8ad
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 // (C) 2001, 2002, 2003 Ximian, Inc.
12 namespace Mono.CSharp {
13 using System;
14 using System.Collections;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Reflection.Emit;
20 // A container class for all the conversion operations
22 public class Convert {
23 #if GMCS_SOURCE
24 static bool TypeParameter_to_Null (Type target_type)
26 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
27 if (gc == null)
28 return false;
30 if (gc.HasReferenceTypeConstraint)
31 return true;
32 if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
33 return true;
35 return false;
38 static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
40 ArrayList list = new ArrayList ();
41 list.Add (gc.EffectiveBaseClass);
42 foreach (Type t in gc.InterfaceConstraints) {
43 if (!TypeManager.IsGenericParameter (t))
44 continue;
46 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
47 if (new_gc != null)
48 list.Add (TypeParam_EffectiveBaseType (new_gc));
50 return FindMostEncompassedType (list);
52 #endif
55 // From a one-dimensional array-type S[] to System.Collections.IList<S> and base
56 // interfaces of this interface.
58 // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
59 // interfaces of this interface, provided there is an implicit reference conversion
60 // from S to T.
62 static bool Array_To_IList (Type array, Type list)
64 #if GMCS_SOURCE
65 if (!array.IsArray || (array.GetArrayRank () != 1) || !list.IsGenericType)
66 return false;
68 Type gt = list.GetGenericTypeDefinition ();
69 if ((gt != TypeManager.generic_ilist_type) &&
70 (gt != TypeManager.generic_icollection_type) &&
71 (gt != TypeManager.generic_ienumerable_type))
72 return false;
74 Type element_type = TypeManager.GetElementType (array);
75 Type arg_type = TypeManager.GetTypeArguments (list) [0];
77 if (element_type == arg_type)
78 return true;
80 if (MyEmptyExpr == null)
81 MyEmptyExpr = new EmptyExpression ();
82 MyEmptyExpr.SetType (TypeManager.GetElementType (array));
84 return ImplicitReferenceConversionCore (MyEmptyExpr, arg_type);
85 #else
86 return false;
87 #endif
90 static bool IList_To_Array(Type list, Type array)
92 # if GMCS_SOURCE
93 if (!list.IsGenericType || !array.IsArray || array.GetArrayRank() != 1)
94 return false;
96 Type gt = list.GetGenericTypeDefinition();
97 if (gt != TypeManager.generic_ilist_type &&
98 gt != TypeManager.generic_icollection_type &&
99 gt != TypeManager.generic_ienumerable_type)
100 return false;
102 Type arg_type = TypeManager.GetTypeArguments(list)[0];
103 Type element_type = TypeManager.GetElementType(array);
105 if (element_type == arg_type)
106 return true;
108 if (MyEmptyExpr == null)
109 MyEmptyExpr = new EmptyExpression();
110 MyEmptyExpr.SetType(element_type);
111 return ImplicitReferenceConversionExists(MyEmptyExpr, arg_type) || ExplicitReferenceConversionExists(element_type, arg_type);
112 #else
113 return false;
114 #endif
117 static Expression ImplicitTypeParameterConversion (Expression expr,
118 Type target_type)
120 #if GMCS_SOURCE
121 Type expr_type = expr.Type;
123 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
125 if (gc == null) {
126 if (target_type == TypeManager.object_type)
127 return new BoxedCast (expr, target_type);
129 return null;
132 // We're converting from a type parameter which is known to be a reference type.
133 Type base_type = TypeParam_EffectiveBaseType (gc);
135 if (TypeManager.IsSubclassOf (base_type, target_type))
136 return new ClassCast (expr, target_type);
138 if (target_type.IsInterface) {
139 if (TypeManager.ImplementsInterface (base_type, target_type))
140 return new ClassCast (expr, target_type);
142 foreach (Type t in gc.InterfaceConstraints) {
143 if (TypeManager.IsSubclassOf (t, target_type))
144 return new ClassCast (expr, target_type);
145 if (TypeManager.ImplementsInterface (t, target_type))
146 return new ClassCast (expr, target_type);
150 foreach (Type t in gc.InterfaceConstraints) {
151 if (!TypeManager.IsGenericParameter (t))
152 continue;
153 if (TypeManager.IsSubclassOf (t, target_type))
154 return new ClassCast (expr, target_type);
155 if (TypeManager.ImplementsInterface (t, target_type))
156 return new ClassCast (expr, target_type);
158 #endif
159 return null;
162 static bool ImplicitTypeParameterBoxingConversion (Type expr_type, Type target_type,
163 out bool use_class_cast)
165 #if GMCS_SOURCE
166 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
168 if (gc == null) {
169 use_class_cast = false;
170 return target_type == TypeManager.object_type;
173 use_class_cast = true;
175 if (!gc.HasReferenceTypeConstraint)
176 return false;
178 // We're converting from a type parameter which is known to be a reference type.
179 Type base_type = TypeParam_EffectiveBaseType (gc);
181 if (TypeManager.IsSubclassOf (base_type, target_type))
182 return true;
184 if (target_type.IsInterface) {
185 if (TypeManager.ImplementsInterface (base_type, target_type))
186 return true;
188 foreach (Type t in gc.InterfaceConstraints) {
189 if (TypeManager.IsSubclassOf (t, target_type))
190 return true;
191 if (TypeManager.ImplementsInterface (t, target_type))
192 return true;
196 foreach (Type t in gc.InterfaceConstraints) {
197 if (!TypeManager.IsGenericParameter (t))
198 continue;
199 if (TypeManager.IsSubclassOf (t, target_type))
200 return true;
201 if (TypeManager.ImplementsInterface (t, target_type))
202 return true;
204 #endif
206 use_class_cast = false;
207 return false;
210 static bool ExplicitTypeParameterConversionExists (Type source_type, Type target_type)
212 #if GMCS_SOURCE
213 if (target_type.IsInterface)
214 return true;
216 if (TypeManager.IsGenericParameter (target_type)) {
217 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
218 if (gc == null)
219 return false;
221 foreach (Type iface in gc.InterfaceConstraints) {
222 if (!TypeManager.IsGenericParameter (iface))
223 continue;
225 if (TypeManager.IsSubclassOf (source_type, iface))
226 return true;
229 #endif
230 return false;
233 static Expression ExplicitTypeParameterConversion (Expression source, Type target_type)
235 #if GMCS_SOURCE
236 Type source_type = source.Type;
238 if (target_type.IsInterface)
239 return new ClassCast (source, target_type);
241 if (TypeManager.IsGenericParameter (target_type)) {
242 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
243 if (gc == null)
244 return null;
246 foreach (Type iface in gc.InterfaceConstraints) {
247 if (!TypeManager.IsGenericParameter (iface))
248 continue;
250 if (TypeManager.IsSubclassOf (source_type, iface))
251 return new ClassCast (source, target_type);
254 #endif
255 return null;
258 static EmptyExpression MyEmptyExpr;
259 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
261 Type expr_type = expr.Type;
263 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
264 // if we are a method group, emit a warning
266 expr.Emit (null);
269 if (expr_type == TypeManager.void_type)
270 return null;
272 if (TypeManager.IsGenericParameter (expr_type))
273 return ImplicitTypeParameterConversion (expr, target_type);
275 // from the null type to any reference-type.
276 if (expr_type == TypeManager.null_type) {
277 NullConstant nc = (NullConstant)expr;
278 return nc.ConvertImplicitly(target_type);
281 if (ImplicitReferenceConversionCore (expr, target_type))
282 return new EmptyCast (expr, target_type);
284 bool use_class_cast;
285 if (ImplicitBoxingConversionExists (expr, target_type, out use_class_cast)) {
286 if (use_class_cast)
287 return new ClassCast (expr, target_type);
288 else
289 return new BoxedCast (expr, target_type);
292 return null;
295 static public bool ImplicitReferenceConversionCore (Expression expr, Type target_type)
297 Type expr_type = expr.Type;
300 // notice that it is possible to write "ValueType v = 1", the ValueType here
301 // is an abstract class, and not really a value type, so we apply the same rules.
303 if (target_type == TypeManager.object_type) {
305 // A pointer type cannot be converted to object
307 if (expr_type.IsPointer)
308 return false;
310 if (TypeManager.IsValueType (expr_type))
311 return false;
312 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
313 return expr_type != TypeManager.anonymous_method_type;
316 return false;
317 } else if (target_type == TypeManager.value_type) {
318 return expr_type == TypeManager.enum_type;
319 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
321 // Special case: enumeration to System.Enum.
322 // System.Enum is not a value type, it is a class, so we need
323 // a boxing conversion
325 if (expr_type.IsEnum || TypeManager.IsGenericParameter (expr_type))
326 return false;
328 return true;
331 // This code is kind of mirrored inside ImplicitStandardConversionExists
332 // with the small distinction that we only probe there
334 // Always ensure that the code here and there is in sync
336 // from any class-type S to any interface-type T.
337 if (target_type.IsInterface) {
338 if (target_type != TypeManager.iconvertible_type &&
339 expr_type.IsValueType && (expr is Constant) &&
340 !(expr is IntLiteral || expr is BoolLiteral ||
341 expr is FloatLiteral || expr is DoubleLiteral ||
342 expr is LongLiteral || expr is CharLiteral ||
343 expr is StringLiteral || expr is DecimalLiteral ||
344 expr is UIntLiteral || expr is ULongLiteral)) {
345 return false;
348 if (TypeManager.ImplementsInterface (expr_type, target_type)){
349 return !TypeManager.IsGenericParameter (expr_type) &&
350 !TypeManager.IsValueType (expr_type);
354 // from any interface type S to interface-type T.
355 if (expr_type.IsInterface && target_type.IsInterface) {
356 return TypeManager.ImplementsInterface (expr_type, target_type);
359 // from an array-type S to an array-type of type T
360 if (expr_type.IsArray && target_type.IsArray) {
361 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
363 Type expr_element_type = TypeManager.GetElementType (expr_type);
365 if (MyEmptyExpr == null)
366 MyEmptyExpr = new EmptyExpression ();
368 MyEmptyExpr.SetType (expr_element_type);
369 Type target_element_type = TypeManager.GetElementType (target_type);
371 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
372 return ImplicitStandardConversionExists (
373 MyEmptyExpr, target_element_type);
377 // from an array-type to System.Array
378 if (expr_type.IsArray && target_type == TypeManager.array_type)
379 return true;
381 // from an array-type of type T to IList<T>
382 if (expr_type.IsArray && Array_To_IList (expr_type, target_type))
383 return true;
385 // from any delegate type to System.Delegate
386 if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
387 target_type == TypeManager.delegate_type)
388 return true;
390 // from any array-type or delegate type into System.ICloneable.
391 if (expr_type.IsArray ||
392 expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
393 if (target_type == TypeManager.icloneable_type)
394 return true;
396 // from a generic type definition to a generic instance.
397 if (TypeManager.IsEqual (expr_type, target_type))
398 return true;
400 return false;
403 static public bool ImplicitBoxingConversionExists (Expression expr, Type target_type,
404 out bool use_class_cast)
406 Type expr_type = expr.Type;
408 use_class_cast = false;
411 // notice that it is possible to write "ValueType v = 1", the ValueType here
412 // is an abstract class, and not really a value type, so we apply the same rules.
414 if (target_type == TypeManager.object_type) {
416 // A pointer type cannot be converted to object
418 if (expr_type.IsPointer)
419 return false;
421 return TypeManager.IsValueType (expr_type);
422 } else if (target_type == TypeManager.value_type) {
423 return TypeManager.IsValueType (expr_type);
424 } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
426 // Special case: enumeration to System.Enum.
427 // System.Enum is not a value type, it is a class, so we need
428 // a boxing conversion
430 if (expr_type.IsEnum || TypeManager.IsGenericParameter (expr_type))
431 return true;
433 return false;
436 // This code is kind of mirrored inside ImplicitStandardConversionExists
437 // with the small distinction that we only probe there
439 // Always ensure that the code here and there is in sync
441 // from any class-type S to any interface-type T.
442 if (target_type.IsInterface) {
443 if (target_type != TypeManager.iconvertible_type &&
444 expr_type.IsValueType && (expr is Constant) &&
445 !(expr is IntLiteral || expr is BoolLiteral ||
446 expr is FloatLiteral || expr is DoubleLiteral ||
447 expr is LongLiteral || expr is CharLiteral ||
448 expr is StringLiteral || expr is DecimalLiteral ||
449 expr is UIntLiteral || expr is ULongLiteral)) {
450 return false;
453 if (TypeManager.ImplementsInterface (expr_type, target_type))
454 return TypeManager.IsGenericParameter (expr_type) ||
455 TypeManager.IsValueType (expr_type);
458 if (TypeManager.IsGenericParameter (expr_type))
459 return ImplicitTypeParameterBoxingConversion (
460 expr_type, target_type, out use_class_cast);
462 return false;
466 // Tests whether an implicit reference conversion exists between expr_type
467 // and target_type
469 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
471 if (target_type.IsValueType)
472 return false;
474 Type expr_type = expr.Type;
476 // from the null type to any reference-type.
477 if (expr_type == TypeManager.null_type){
478 if (target_type.IsPointer)
479 return true;
481 if (!target_type.IsValueType)
482 return true;
485 if (TypeManager.IsGenericParameter (expr_type))
486 return ImplicitTypeParameterConversion (expr, target_type) != null;
488 bool use_class_cast;
489 if (ImplicitReferenceConversionCore (expr, target_type) ||
490 ImplicitBoxingConversionExists (expr, target_type, out use_class_cast))
491 return true;
493 return false;
496 /// <summary>
497 /// Implicit Numeric Conversions.
499 /// expr is the expression to convert, returns a new expression of type
500 /// target_type or null if an implicit conversion is not possible.
501 /// </summary>
502 static public Expression ImplicitNumericConversion (Expression expr,
503 Type target_type)
505 Type expr_type = expr.Type;
506 Type real_target_type = target_type;
508 if (expr_type == TypeManager.sbyte_type){
510 // From sbyte to short, int, long, float, double, decimal
512 if (real_target_type == TypeManager.int32_type)
513 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
514 if (real_target_type == TypeManager.int64_type)
515 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
516 if (real_target_type == TypeManager.double_type)
517 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
518 if (real_target_type == TypeManager.float_type)
519 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
520 if (real_target_type == TypeManager.short_type)
521 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
522 if (real_target_type == TypeManager.decimal_type)
523 return 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 ((real_target_type == TypeManager.short_type) ||
529 (real_target_type == TypeManager.ushort_type) ||
530 (real_target_type == TypeManager.int32_type) ||
531 (real_target_type == TypeManager.uint32_type))
532 return new EmptyCast (expr, target_type);
534 if (real_target_type == TypeManager.uint64_type)
535 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
536 if (real_target_type == TypeManager.int64_type)
537 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
538 if (real_target_type == TypeManager.float_type)
539 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
540 if (real_target_type == TypeManager.double_type)
541 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
542 if (real_target_type == TypeManager.decimal_type)
543 return new CastToDecimal (expr);
545 } else if (expr_type == TypeManager.short_type){
547 // From short to int, long, float, double, decimal
549 if (real_target_type == TypeManager.int32_type)
550 return new EmptyCast (expr, target_type);
551 if (real_target_type == TypeManager.int64_type)
552 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
553 if (real_target_type == TypeManager.double_type)
554 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
555 if (real_target_type == TypeManager.float_type)
556 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
557 if (real_target_type == TypeManager.decimal_type)
558 return new CastToDecimal (expr);
560 } else if (expr_type == TypeManager.ushort_type){
562 // From ushort to int, uint, long, ulong, float, double, decimal
564 if (real_target_type == TypeManager.uint32_type)
565 return new EmptyCast (expr, target_type);
567 if (real_target_type == TypeManager.uint64_type)
568 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
569 if (real_target_type == TypeManager.int32_type)
570 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
571 if (real_target_type == TypeManager.int64_type)
572 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
573 if (real_target_type == TypeManager.double_type)
574 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
575 if (real_target_type == TypeManager.float_type)
576 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
577 if (real_target_type == TypeManager.decimal_type)
578 return new CastToDecimal (expr);
579 } else if (expr_type == TypeManager.int32_type){
581 // From int to long, float, double, decimal
583 if (real_target_type == TypeManager.int64_type)
584 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
585 if (real_target_type == TypeManager.double_type)
586 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
587 if (real_target_type == TypeManager.float_type)
588 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
589 if (real_target_type == TypeManager.decimal_type)
590 return new CastToDecimal (expr);
591 } else if (expr_type == TypeManager.uint32_type){
593 // From uint to long, ulong, float, double, decimal
595 if (real_target_type == TypeManager.int64_type)
596 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
597 if (real_target_type == TypeManager.uint64_type)
598 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
599 if (real_target_type == TypeManager.double_type)
600 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
601 OpCodes.Conv_R8);
602 if (real_target_type == TypeManager.float_type)
603 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
604 OpCodes.Conv_R4);
605 if (real_target_type == TypeManager.decimal_type)
606 return new CastToDecimal (expr);
607 } else if (expr_type == TypeManager.int64_type){
609 // From long/ulong to float, double
611 if (real_target_type == TypeManager.double_type)
612 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
613 if (real_target_type == TypeManager.float_type)
614 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
615 if (real_target_type == TypeManager.decimal_type)
616 return new CastToDecimal (expr);
617 } else if (expr_type == TypeManager.uint64_type){
619 // From ulong to float, double
621 if (real_target_type == TypeManager.double_type)
622 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
623 OpCodes.Conv_R8);
624 if (real_target_type == TypeManager.float_type)
625 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
626 OpCodes.Conv_R4);
627 if (real_target_type == TypeManager.decimal_type)
628 return new CastToDecimal (expr);
629 } else if (expr_type == TypeManager.char_type){
631 // From char to ushort, int, uint, long, ulong, float, double, decimal
633 if ((real_target_type == TypeManager.ushort_type) ||
634 (real_target_type == TypeManager.int32_type) ||
635 (real_target_type == TypeManager.uint32_type))
636 return new EmptyCast (expr, target_type);
637 if (real_target_type == TypeManager.uint64_type)
638 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
639 if (real_target_type == TypeManager.int64_type)
640 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
641 if (real_target_type == TypeManager.float_type)
642 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
643 if (real_target_type == TypeManager.double_type)
644 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
645 if (real_target_type == TypeManager.decimal_type)
646 return new CastToDecimal (expr);
647 } else if (expr_type == TypeManager.float_type){
649 // float to double
651 if (real_target_type == TypeManager.double_type)
652 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
655 return null;
658 /// <summary>
659 /// Same as ImplicitStandardConversionExists except that it also looks at
660 /// implicit user defined conversions - needed for overload resolution
661 /// </summary>
662 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
664 #if GMCS_SOURCE
665 if (expr is NullLiteral) {
666 if (TypeManager.IsGenericParameter (target_type))
667 return TypeParameter_to_Null (target_type);
669 if (TypeManager.IsNullableType (target_type))
670 return true;
672 #endif
673 if (ImplicitStandardConversionExists (expr, target_type))
674 return true;
676 return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
679 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
681 return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
684 /// <summary>
685 /// Determines if a standard implicit conversion exists from
686 /// expr_type to target_type
688 /// ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
689 /// </summary>
690 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
692 Type expr_type = expr.Type;
693 #if GMCS_SOURCE
694 if (TypeManager.IsNullableType (target_type)) {
695 // if implicit standard conversion S -> T exists, S -> T? and S? -> T? also exists
696 target_type = TypeManager.GetTypeArguments (target_type) [0];
698 // S? -> T?
699 if (TypeManager.IsNullableType (expr_type)) {
700 EmptyExpression new_expr = EmptyExpression.Grab ();
701 new_expr.SetType (TypeManager.GetTypeArguments (expr_type) [0]);
702 bool retval = ImplicitStandardConversionExists (new_expr, target_type);
703 EmptyExpression.Release (new_expr);
704 return retval;
707 #endif
708 if (expr_type == TypeManager.void_type)
709 return false;
711 //Console.WriteLine ("Expr is {0}", expr);
712 //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
713 if (TypeManager.IsEqual (expr_type, target_type))
714 return true;
717 // First numeric conversions
719 if (expr_type == TypeManager.sbyte_type){
721 // From sbyte to short, int, long, float, double, decimal
723 if ((target_type == TypeManager.int32_type) ||
724 (target_type == TypeManager.int64_type) ||
725 (target_type == TypeManager.double_type) ||
726 (target_type == TypeManager.float_type) ||
727 (target_type == TypeManager.short_type) ||
728 (target_type == TypeManager.decimal_type))
729 return true;
731 } else if (expr_type == TypeManager.byte_type){
733 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
735 if ((target_type == TypeManager.short_type) ||
736 (target_type == TypeManager.ushort_type) ||
737 (target_type == TypeManager.int32_type) ||
738 (target_type == TypeManager.uint32_type) ||
739 (target_type == TypeManager.uint64_type) ||
740 (target_type == TypeManager.int64_type) ||
741 (target_type == TypeManager.float_type) ||
742 (target_type == TypeManager.double_type) ||
743 (target_type == TypeManager.decimal_type))
744 return true;
746 } else if (expr_type == TypeManager.short_type){
748 // From short to int, long, double, float, decimal
750 if ((target_type == TypeManager.int32_type) ||
751 (target_type == TypeManager.int64_type) ||
752 (target_type == TypeManager.double_type) ||
753 (target_type == TypeManager.float_type) ||
754 (target_type == TypeManager.decimal_type))
755 return true;
757 } else if (expr_type == TypeManager.ushort_type){
759 // From ushort to int, uint, long, ulong, double, float, decimal
761 if ((target_type == TypeManager.uint32_type) ||
762 (target_type == TypeManager.uint64_type) ||
763 (target_type == TypeManager.int32_type) ||
764 (target_type == TypeManager.int64_type) ||
765 (target_type == TypeManager.double_type) ||
766 (target_type == TypeManager.float_type) ||
767 (target_type == TypeManager.decimal_type))
768 return true;
770 } else if (expr_type == TypeManager.int32_type){
772 // From int to long, double, float, decimal
774 if ((target_type == TypeManager.int64_type) ||
775 (target_type == TypeManager.double_type) ||
776 (target_type == TypeManager.float_type) ||
777 (target_type == TypeManager.decimal_type))
778 return true;
780 } else if (expr_type == TypeManager.uint32_type){
782 // From uint to long, ulong, double, float, decimal
784 if ((target_type == TypeManager.int64_type) ||
785 (target_type == TypeManager.uint64_type) ||
786 (target_type == TypeManager.double_type) ||
787 (target_type == TypeManager.float_type) ||
788 (target_type == TypeManager.decimal_type))
789 return true;
791 } else if ((expr_type == TypeManager.uint64_type) ||
792 (expr_type == TypeManager.int64_type)) {
794 // From long/ulong to double, float, decimal
796 if ((target_type == TypeManager.double_type) ||
797 (target_type == TypeManager.float_type) ||
798 (target_type == TypeManager.decimal_type))
799 return true;
801 } else if (expr_type == TypeManager.char_type){
803 // From char to ushort, int, uint, ulong, long, float, double, decimal
805 if ((target_type == TypeManager.ushort_type) ||
806 (target_type == TypeManager.int32_type) ||
807 (target_type == TypeManager.uint32_type) ||
808 (target_type == TypeManager.uint64_type) ||
809 (target_type == TypeManager.int64_type) ||
810 (target_type == TypeManager.float_type) ||
811 (target_type == TypeManager.double_type) ||
812 (target_type == TypeManager.decimal_type))
813 return true;
815 } else if (expr_type == TypeManager.float_type){
817 // float to double
819 if (target_type == TypeManager.double_type)
820 return true;
823 if (expr.eclass == ExprClass.MethodGroup){
824 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
825 MethodGroupExpr mg = expr as MethodGroupExpr;
826 if (mg != null){
827 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
832 if (ImplicitReferenceConversionExists (expr, target_type))
833 return true;
836 // Implicit Constant Expression Conversions
838 if (expr is IntConstant){
839 int value = ((IntConstant) expr).Value;
841 if (target_type == TypeManager.sbyte_type){
842 if (value >= SByte.MinValue && value <= SByte.MaxValue)
843 return true;
844 } else if (target_type == TypeManager.byte_type){
845 if (value >= 0 && value <= Byte.MaxValue)
846 return true;
847 } else if (target_type == TypeManager.short_type){
848 if (value >= Int16.MinValue && value <= Int16.MaxValue)
849 return true;
850 } else if (target_type == TypeManager.ushort_type){
851 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
852 return true;
853 } else if (target_type == TypeManager.uint32_type){
854 if (value >= 0)
855 return true;
856 } else if (target_type == TypeManager.uint64_type){
858 // we can optimize this case: a positive int32
859 // always fits on a uint64. But we need an opcode
860 // to do it.
862 if (value >= 0)
863 return true;
866 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
867 return true;
870 if (expr is LongConstant && target_type == TypeManager.uint64_type){
872 // Try the implicit constant expression conversion
873 // from long to ulong, instead of a nice routine,
874 // we just inline it
876 long v = ((LongConstant) expr).Value;
877 if (v >= 0)
878 return true;
882 // If `expr_type' implements `target_type' (which is an iface)
883 // see TryImplicitIntConversion
885 if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
886 return true;
888 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
889 return true;
891 if (expr_type == TypeManager.anonymous_method_type){
892 if (!TypeManager.IsDelegateType (target_type))
893 return false;
895 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
896 return ame.ImplicitStandardConversionExists (target_type);
899 return false;
902 /// <summary>
903 /// Finds "most encompassed type" according to the spec (13.4.2)
904 /// amongst the methods in the MethodGroupExpr
905 /// </summary>
906 static Type FindMostEncompassedType (ArrayList types)
908 Type best = null;
910 if (types.Count == 0)
911 return null;
913 if (types.Count == 1)
914 return (Type) types [0];
916 EmptyExpression expr = EmptyExpression.Grab ();
918 foreach (Type t in types) {
919 if (best == null) {
920 best = t;
921 continue;
924 expr.SetType (t);
925 if (ImplicitStandardConversionExists (expr, best))
926 best = t;
929 expr.SetType (best);
930 foreach (Type t in types) {
931 if (best == t)
932 continue;
933 if (!ImplicitStandardConversionExists (expr, t)) {
934 best = null;
935 break;
939 EmptyExpression.Release (expr);
941 return best;
944 /// <summary>
945 /// Finds "most encompassing type" according to the spec (13.4.2)
946 /// amongst the types in the given set
947 /// </summary>
948 static Type FindMostEncompassingType (ArrayList types)
950 Type best = null;
952 if (types.Count == 0)
953 return null;
955 if (types.Count == 1)
956 return (Type) types [0];
958 EmptyExpression expr = EmptyExpression.Grab ();
960 foreach (Type t in types) {
961 if (best == null) {
962 best = t;
963 continue;
966 expr.SetType (best);
967 if (ImplicitStandardConversionExists (expr, t))
968 best = t;
971 foreach (Type t in types) {
972 if (best == t)
973 continue;
974 expr.SetType (t);
975 if (!ImplicitStandardConversionExists (expr, best)) {
976 best = null;
977 break;
981 EmptyExpression.Release (expr);
983 return best;
986 /// <summary>
987 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
988 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
989 /// for explicit and implicit conversion operators.
990 /// </summary>
991 static public Type FindMostSpecificSource (IList list,
992 Expression source, bool apply_explicit_conv_rules)
994 ArrayList src_types_set = new ArrayList ();
997 // If any operator converts from S then Sx = S
999 Type source_type = source.Type;
1000 foreach (MethodBase mb in list){
1001 ParameterData pd = TypeManager.GetParameterData (mb);
1002 Type param_type = pd.ParameterType (0);
1004 if (param_type == source_type)
1005 return param_type;
1007 src_types_set.Add (param_type);
1011 // Explicit Conv rules
1013 if (apply_explicit_conv_rules) {
1014 ArrayList candidate_set = new ArrayList ();
1016 foreach (Type param_type in src_types_set){
1017 if (ImplicitStandardConversionExists (source, param_type))
1018 candidate_set.Add (param_type);
1021 if (candidate_set.Count != 0)
1022 return FindMostEncompassedType (candidate_set);
1026 // Final case
1028 if (apply_explicit_conv_rules)
1029 return FindMostEncompassingType (src_types_set);
1030 else
1031 return FindMostEncompassedType (src_types_set);
1034 /// <summary>
1035 /// Finds the most specific target Tx according to section 13.4.4
1036 /// </summary>
1037 static public Type FindMostSpecificTarget (IList list,
1038 Type target, bool apply_explicit_conv_rules)
1040 ArrayList tgt_types_set = new ArrayList ();
1043 // If any operator converts to T then Tx = T
1045 foreach (MethodInfo mi in list){
1046 Type ret_type = mi.ReturnType;
1047 if (ret_type == target)
1048 return ret_type;
1050 tgt_types_set.Add (ret_type);
1054 // Explicit conv rules
1056 if (apply_explicit_conv_rules) {
1057 ArrayList candidate_set = new ArrayList ();
1059 EmptyExpression expr = EmptyExpression.Grab ();
1061 foreach (Type ret_type in tgt_types_set){
1062 expr.SetType (ret_type);
1064 if (ImplicitStandardConversionExists (expr, target))
1065 candidate_set.Add (ret_type);
1068 EmptyExpression.Release (expr);
1070 if (candidate_set.Count != 0)
1071 return FindMostEncompassingType (candidate_set);
1075 // Okay, final case !
1077 if (apply_explicit_conv_rules)
1078 return FindMostEncompassedType (tgt_types_set);
1079 else
1080 return FindMostEncompassingType (tgt_types_set);
1083 /// <summary>
1084 /// User-defined Implicit conversions
1085 /// </summary>
1086 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1087 Type target, Location loc)
1089 return UserDefinedConversion (ec, source, target, loc, false);
1092 /// <summary>
1093 /// User-defined Explicit conversions
1094 /// </summary>
1095 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1096 Type target, Location loc)
1098 return UserDefinedConversion (ec, source, target, loc, true);
1101 static void AddConversionOperators (ArrayList list,
1102 Expression source, Type target_type,
1103 bool look_for_explicit,
1104 MethodGroupExpr mg)
1106 if (mg == null)
1107 return;
1109 Type source_type = source.Type;
1110 EmptyExpression expr = EmptyExpression.Grab ();
1111 foreach (MethodInfo m in mg.Methods) {
1112 ParameterData pd = TypeManager.GetParameterData (m);
1113 Type return_type = m.ReturnType;
1114 Type arg_type = pd.ParameterType (0);
1116 if (source_type != arg_type) {
1117 if (!ImplicitStandardConversionExists (source, arg_type)) {
1118 if (!look_for_explicit)
1119 continue;
1120 expr.SetType (arg_type);
1121 if (!ImplicitStandardConversionExists (expr, source_type))
1122 continue;
1126 if (target_type != return_type) {
1127 expr.SetType (return_type);
1128 if (!ImplicitStandardConversionExists (expr, target_type)) {
1129 if (!look_for_explicit)
1130 continue;
1131 expr.SetType (target_type);
1132 if (!ImplicitStandardConversionExists (expr, return_type))
1133 continue;
1137 list.Add (m);
1140 EmptyExpression.Release (expr);
1143 /// <summary>
1144 /// Compute the user-defined conversion operator from source_type to target_type.
1145 /// `look_for_explicit' controls whether we should also include the list of explicit operators
1146 /// </summary>
1147 static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
1149 ArrayList ops = new ArrayList (4);
1151 Type source_type = source.Type;
1153 if (source_type != TypeManager.decimal_type) {
1154 AddConversionOperators (ops, source, target_type, look_for_explicit,
1155 Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1156 if (look_for_explicit) {
1157 AddConversionOperators (ops, source, target_type, look_for_explicit,
1158 Expression.MethodLookup (
1159 container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1163 if (target_type != TypeManager.decimal_type) {
1164 AddConversionOperators (ops, source, target_type, look_for_explicit,
1165 Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1166 if (look_for_explicit) {
1167 AddConversionOperators (ops, source, target_type, look_for_explicit,
1168 Expression.MethodLookup (
1169 container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1173 if (ops.Count == 0)
1174 return null;
1176 Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1177 if (most_specific_source == null)
1178 return null;
1180 Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1181 if (most_specific_target == null)
1182 return null;
1184 MethodInfo method = null;
1186 foreach (MethodInfo m in ops) {
1187 if (m.ReturnType != most_specific_target)
1188 continue;
1189 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1190 continue;
1191 // Ambiguous: more than one conversion operator satisfies the signature.
1192 if (method != null)
1193 return null;
1194 method = m;
1197 return method;
1200 static DoubleHash explicit_conv = new DoubleHash (100);
1201 static DoubleHash implicit_conv = new DoubleHash (100);
1203 /// <summary>
1204 /// User-defined conversions
1205 /// </summary>
1206 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1207 Type target, Location loc,
1208 bool look_for_explicit)
1210 Type source_type = source.Type;
1211 MethodInfo method = null;
1213 object o;
1214 DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1216 if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1217 method = (MethodInfo) o;
1218 } else {
1219 method = GetConversionOperator (null, source, target, look_for_explicit);
1220 if (!(source is Constant))
1221 hash.Insert (source_type, target, method);
1224 if (method == null)
1225 return null;
1227 Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1230 // This will do the conversion to the best match that we
1231 // found. Now we need to perform an implict standard conversion
1232 // if the best match was not the type that we were requested
1233 // by target.
1235 if (look_for_explicit)
1236 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1237 else
1238 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1240 if (source == null)
1241 return null;
1243 Expression e;
1244 e = new UserCast (method, source, loc);
1245 if (e.Type != target){
1246 if (!look_for_explicit)
1247 e = ImplicitConversionStandard (ec, e, target, loc);
1248 else
1249 e = ExplicitConversionStandard (ec, e, target, loc);
1252 return e;
1255 /// <summary>
1256 /// Converts implicitly the resolved expression `expr' into the
1257 /// `target_type'. It returns a new expression that can be used
1258 /// in a context that expects a `target_type'.
1259 /// </summary>
1260 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1261 Type target_type, Location loc)
1263 Expression e;
1265 if (target_type == null)
1266 throw new Exception ("Target type is null");
1268 e = ImplicitConversionStandard (ec, expr, target_type, loc);
1269 if (e != null)
1270 return e;
1272 e = ImplicitUserConversion (ec, expr, target_type, loc);
1273 if (e != null)
1274 return e;
1276 return null;
1280 /// <summary>
1281 /// Attempts to apply the `Standard Implicit
1282 /// Conversion' rules to the expression `expr' into
1283 /// the `target_type'. It returns a new expression
1284 /// that can be used in a context that expects a
1285 /// `target_type'.
1287 /// This is different from `ImplicitConversion' in that the
1288 /// user defined implicit conversions are excluded.
1289 /// </summary>
1290 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1291 Type target_type, Location loc)
1293 Type expr_type = expr.Type;
1294 Expression e;
1295 #if GMCS_SOURCE
1296 // TODO: refactor to be a part of constant infrastructure
1297 if (expr is NullLiteral) {
1298 if (TypeManager.IsNullableType (target_type))
1299 return new Nullable.NullableLiteral (target_type, loc);
1302 if (TypeManager.IsNullableType (target_type)) {
1303 Type target = TypeManager.GetTypeArguments (target_type) [0];
1305 if (TypeManager.IsNullableType (expr.Type)) {
1306 e = new Nullable.LiftedConversion (
1307 expr, target_type, false, false, loc).Resolve (ec);
1308 if (e != null)
1309 return e;
1310 } else {
1311 e = ImplicitConversion (ec, expr, target, loc);
1312 if (e != null)
1313 return Nullable.Wrap.Create (e, ec);
1316 #endif
1317 if (expr.eclass == ExprClass.MethodGroup){
1318 if (!TypeManager.IsDelegateType (target_type)){
1319 return null;
1323 // Only allow anonymous method conversions on post ISO_1
1325 if (RootContext.Version != LanguageVersion.ISO_1){
1326 MethodGroupExpr mg = expr as MethodGroupExpr;
1327 if (mg != null)
1328 return ImplicitDelegateCreation.Create (
1329 ec, mg, target_type, loc);
1333 if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1334 return expr;
1337 // Attempt to do the implicit constant expression conversions
1339 Constant c = expr as Constant;
1340 if (c != null) {
1342 // If `target_type' is an interface and the type of `ic' implements the interface
1343 // e.g. target_type is IComparable, IConvertible, IFormattable
1345 if (c.Type == TypeManager.int32_type && target_type.IsInterface && target_type.IsAssignableFrom (c.Type))
1346 return new BoxedCast (c, target_type);
1348 try {
1349 c = c.ConvertImplicitly (target_type);
1350 } catch {
1351 Console.WriteLine ("Conversion error happened in line {0}", loc);
1352 throw;
1354 if (c != null)
1355 return c;
1358 e = ImplicitNumericConversion (expr, target_type);
1359 if (e != null)
1360 return e;
1362 e = ImplicitReferenceConversion (expr, target_type);
1363 if (e != null)
1364 return e;
1366 if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1367 IntLiteral i = (IntLiteral) expr;
1369 if (i.Value == 0)
1370 return new EnumConstant ((Constant) expr, target_type);
1373 if (ec.InUnsafe) {
1374 if (expr_type.IsPointer){
1375 if (target_type == TypeManager.void_ptr_type)
1376 return new EmptyCast (expr, target_type);
1379 // yep, comparing pointer types cant be done with
1380 // t1 == t2, we have to compare their element types.
1382 if (target_type.IsPointer){
1383 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1384 return expr;
1386 //return null;
1390 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1391 return new EmptyCast (NullPointer.Null, target_type);
1394 if (expr_type == TypeManager.anonymous_method_type){
1395 AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
1397 int errors = Report.Errors;
1399 Expression conv = ame.Compatible (ec, target_type);
1400 if (conv != null)
1401 return conv;
1404 // We return something instead of null, to avoid
1405 // the duplicate error, since am.Compatible would have
1406 // reported that already
1408 if (errors != Report.Errors)
1409 return new EmptyCast (expr, target_type);
1412 return null;
1415 /// <summary>
1416 /// Attempts to implicitly convert `source' into `target_type', using
1417 /// ImplicitConversion. If there is no implicit conversion, then
1418 /// an error is signaled
1419 /// </summary>
1420 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1421 Type target_type, Location loc)
1423 Expression e = ImplicitConversion (ec, source, target_type, loc);
1424 if (e != null)
1425 return e;
1427 source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
1428 return null;
1431 /// <summary>
1432 /// Performs the explicit numeric conversions
1434 /// There are a few conversions that are not part of the C# standard,
1435 /// they were interim hacks in the C# compiler that were supposed to
1436 /// become explicit operators in the UIntPtr class and IntPtr class,
1437 /// but for historical reasons it did not happen, so the C# compiler
1438 /// ended up with these special hacks.
1440 /// See bug 59800 for details.
1442 /// The conversion are:
1443 /// UIntPtr->SByte
1444 /// UIntPtr->Int16
1445 /// UIntPtr->Int32
1446 /// IntPtr->UInt64
1447 /// UInt64->IntPtr
1448 /// SByte->UIntPtr
1449 /// Int16->UIntPtr
1451 /// </summary>
1452 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1454 Type expr_type = expr.Type;
1455 Type real_target_type = target_type;
1457 if (expr_type == TypeManager.sbyte_type){
1459 // From sbyte to byte, ushort, uint, ulong, char, uintptr
1461 if (real_target_type == TypeManager.byte_type)
1462 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1463 if (real_target_type == TypeManager.ushort_type)
1464 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1465 if (real_target_type == TypeManager.uint32_type)
1466 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1467 if (real_target_type == TypeManager.uint64_type)
1468 return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1469 if (real_target_type == TypeManager.char_type)
1470 return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1472 // One of the built-in conversions that belonged in the class library
1473 if (real_target_type == TypeManager.uintptr_type){
1474 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I1_U8);
1476 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1478 } else if (expr_type == TypeManager.byte_type){
1480 // From byte to sbyte and char
1482 if (real_target_type == TypeManager.sbyte_type)
1483 return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1484 if (real_target_type == TypeManager.char_type)
1485 return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1486 } else if (expr_type == TypeManager.short_type){
1488 // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
1490 if (real_target_type == TypeManager.sbyte_type)
1491 return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1492 if (real_target_type == TypeManager.byte_type)
1493 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1494 if (real_target_type == TypeManager.ushort_type)
1495 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1496 if (real_target_type == TypeManager.uint32_type)
1497 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1498 if (real_target_type == TypeManager.uint64_type)
1499 return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1500 if (real_target_type == TypeManager.char_type)
1501 return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1503 // One of the built-in conversions that belonged in the class library
1504 if (real_target_type == TypeManager.uintptr_type){
1505 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1507 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1509 } else if (expr_type == TypeManager.ushort_type){
1511 // From ushort to sbyte, byte, short, char
1513 if (real_target_type == TypeManager.sbyte_type)
1514 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1515 if (real_target_type == TypeManager.byte_type)
1516 return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1517 if (real_target_type == TypeManager.short_type)
1518 return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1519 if (real_target_type == TypeManager.char_type)
1520 return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1521 } else if (expr_type == TypeManager.int32_type){
1523 // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
1525 if (real_target_type == TypeManager.sbyte_type)
1526 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1527 if (real_target_type == TypeManager.byte_type)
1528 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1529 if (real_target_type == TypeManager.short_type)
1530 return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1531 if (real_target_type == TypeManager.ushort_type)
1532 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1533 if (real_target_type == TypeManager.uint32_type)
1534 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1535 if (real_target_type == TypeManager.uint64_type)
1536 return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1537 if (real_target_type == TypeManager.char_type)
1538 return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1540 // One of the built-in conversions that belonged in the class library
1541 if (real_target_type == TypeManager.uintptr_type){
1542 Expression u8e = new ConvCast (expr, TypeManager.uint64_type, ConvCast.Mode.I2_U8);
1544 return new OperatorCast (u8e, TypeManager.uintptr_type, true);
1546 } else if (expr_type == TypeManager.uint32_type){
1548 // From uint to sbyte, byte, short, ushort, int, char
1550 if (real_target_type == TypeManager.sbyte_type)
1551 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1552 if (real_target_type == TypeManager.byte_type)
1553 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1554 if (real_target_type == TypeManager.short_type)
1555 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1556 if (real_target_type == TypeManager.ushort_type)
1557 return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1558 if (real_target_type == TypeManager.int32_type)
1559 return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1560 if (real_target_type == TypeManager.char_type)
1561 return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1562 } else if (expr_type == TypeManager.int64_type){
1564 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1566 if (real_target_type == TypeManager.sbyte_type)
1567 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1568 if (real_target_type == TypeManager.byte_type)
1569 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1570 if (real_target_type == TypeManager.short_type)
1571 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1572 if (real_target_type == TypeManager.ushort_type)
1573 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1574 if (real_target_type == TypeManager.int32_type)
1575 return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1576 if (real_target_type == TypeManager.uint32_type)
1577 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1578 if (real_target_type == TypeManager.uint64_type)
1579 return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1580 if (real_target_type == TypeManager.char_type)
1581 return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1582 } else if (expr_type == TypeManager.uint64_type){
1584 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1586 if (real_target_type == TypeManager.sbyte_type)
1587 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1588 if (real_target_type == TypeManager.byte_type)
1589 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1590 if (real_target_type == TypeManager.short_type)
1591 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1592 if (real_target_type == TypeManager.ushort_type)
1593 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1594 if (real_target_type == TypeManager.int32_type)
1595 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1596 if (real_target_type == TypeManager.uint32_type)
1597 return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1598 if (real_target_type == TypeManager.int64_type)
1599 return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1600 if (real_target_type == TypeManager.char_type)
1601 return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1603 // One of the built-in conversions that belonged in the class library
1604 if (real_target_type == TypeManager.intptr_type){
1605 return new OperatorCast (new EmptyCast (expr, TypeManager.int64_type),
1606 TypeManager.intptr_type, true);
1608 } else if (expr_type == TypeManager.char_type){
1610 // From char to sbyte, byte, short
1612 if (real_target_type == TypeManager.sbyte_type)
1613 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1614 if (real_target_type == TypeManager.byte_type)
1615 return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1616 if (real_target_type == TypeManager.short_type)
1617 return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1618 } else if (expr_type == TypeManager.float_type){
1620 // From float to sbyte, byte, short,
1621 // ushort, int, uint, long, ulong, char
1622 // or decimal
1624 if (real_target_type == TypeManager.sbyte_type)
1625 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1626 if (real_target_type == TypeManager.byte_type)
1627 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1628 if (real_target_type == TypeManager.short_type)
1629 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1630 if (real_target_type == TypeManager.ushort_type)
1631 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1632 if (real_target_type == TypeManager.int32_type)
1633 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1634 if (real_target_type == TypeManager.uint32_type)
1635 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1636 if (real_target_type == TypeManager.int64_type)
1637 return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1638 if (real_target_type == TypeManager.uint64_type)
1639 return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1640 if (real_target_type == TypeManager.char_type)
1641 return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1642 if (real_target_type == TypeManager.decimal_type)
1643 return new CastToDecimal (expr, true);
1644 } else if (expr_type == TypeManager.double_type){
1646 // From double to sbyte, byte, short,
1647 // ushort, int, uint, long, ulong,
1648 // char, float or decimal
1650 if (real_target_type == TypeManager.sbyte_type)
1651 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1652 if (real_target_type == TypeManager.byte_type)
1653 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1654 if (real_target_type == TypeManager.short_type)
1655 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1656 if (real_target_type == TypeManager.ushort_type)
1657 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1658 if (real_target_type == TypeManager.int32_type)
1659 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1660 if (real_target_type == TypeManager.uint32_type)
1661 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1662 if (real_target_type == TypeManager.int64_type)
1663 return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1664 if (real_target_type == TypeManager.uint64_type)
1665 return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1666 if (real_target_type == TypeManager.char_type)
1667 return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1668 if (real_target_type == TypeManager.float_type)
1669 return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1670 if (real_target_type == TypeManager.decimal_type)
1671 return new CastToDecimal (expr, true);
1672 } else if (expr_type == TypeManager.uintptr_type){
1674 // Various built-in conversions that belonged in the class library
1676 // from uintptr to sbyte, short, int32
1678 if (real_target_type == TypeManager.sbyte_type){
1679 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1680 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I1);
1682 if (real_target_type == TypeManager.short_type){
1683 Expression uint32e = new OperatorCast (expr, TypeManager.uint32_type, true);
1684 return new ConvCast (uint32e, TypeManager.sbyte_type, ConvCast.Mode.U4_I2);
1686 if (real_target_type == TypeManager.int32_type){
1687 return new EmptyCast (new OperatorCast (expr, TypeManager.uint32_type, true),
1688 TypeManager.int32_type);
1690 } else if (expr_type == TypeManager.intptr_type){
1691 if (real_target_type == TypeManager.uint64_type){
1692 return new EmptyCast (new OperatorCast (expr, TypeManager.int64_type, true),
1693 TypeManager.uint64_type);
1695 } else if (expr_type == TypeManager.decimal_type) {
1696 return new CastFromDecimal (expr, target_type).Resolve ();
1698 return null;
1701 /// <summary>
1702 /// Returns whether an explicit reference conversion can be performed
1703 /// from source_type to target_type
1704 /// </summary>
1705 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1707 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1708 bool target_is_value_type = target_type.IsValueType;
1710 if (source_type == target_type)
1711 return true;
1714 // From generic parameter to any type
1716 if (TypeManager.IsGenericParameter (source_type))
1717 return ExplicitTypeParameterConversionExists (source_type, target_type);
1720 // From object to a generic parameter
1722 if (source_type == TypeManager.object_type && target_is_type_param)
1723 return true;
1726 // From object to any reference type
1728 if (source_type == TypeManager.object_type && !target_is_value_type)
1729 return true;
1732 // From any class S to any class-type T, provided S is a base class of T
1734 if (TypeManager.IsSubclassOf (target_type, source_type))
1735 return true;
1738 // From any interface type S to any interface T provided S is not derived from T
1740 if (source_type.IsInterface && target_type.IsInterface){
1741 if (!TypeManager.IsSubclassOf (target_type, source_type))
1742 return true;
1746 // From any class type S to any interface T, provided S is not sealed
1747 // and provided S does not implement T.
1749 if (target_type.IsInterface && !source_type.IsSealed &&
1750 !TypeManager.ImplementsInterface (source_type, target_type))
1751 return true;
1754 // From any interface-type S to to any class type T, provided T is not
1755 // sealed, or provided T implements S.
1757 if (source_type.IsInterface &&
1758 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1759 return true;
1762 if (source_type.IsInterface && IList_To_Array (source_type, target_type))
1763 return true;
1765 // From an array type S with an element type Se to an array type T with an
1766 // element type Te provided all the following are true:
1767 // * S and T differe only in element type, in other words, S and T
1768 // have the same number of dimensions.
1769 // * Both Se and Te are reference types
1770 // * An explicit referenc conversions exist from Se to Te
1772 if (source_type.IsArray && target_type.IsArray) {
1773 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1775 Type source_element_type = TypeManager.GetElementType (source_type);
1776 Type target_element_type = TypeManager.GetElementType (target_type);
1778 if (TypeManager.IsGenericParameter (source_element_type) ||
1779 (!source_element_type.IsValueType && !target_element_type.IsValueType))
1780 if (ExplicitReferenceConversionExists (source_element_type,
1781 target_element_type))
1782 return true;
1787 // From System.Array to any array-type
1788 if (source_type == TypeManager.array_type &&
1789 target_type.IsArray){
1790 return true;
1794 // From System delegate to any delegate-type
1796 if (source_type == TypeManager.delegate_type &&
1797 TypeManager.IsDelegateType (target_type))
1798 return true;
1801 // From ICloneable to Array or Delegate types
1803 if (source_type == TypeManager.icloneable_type &&
1804 (target_type == TypeManager.array_type ||
1805 target_type == TypeManager.delegate_type))
1806 return true;
1808 return false;
1811 /// <summary>
1812 /// Implements Explicit Reference conversions
1813 /// </summary>
1814 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1816 Type source_type = source.Type;
1817 bool target_is_type_param = TypeManager.IsGenericParameter (target_type);
1818 bool target_is_value_type = target_type.IsValueType;
1821 // From object to a generic parameter
1823 if (source_type == TypeManager.object_type && target_is_type_param)
1824 return new UnboxCast (source, target_type);
1827 // Explicit type parameter conversion.
1830 if (TypeManager.IsGenericParameter (source_type))
1831 return ExplicitTypeParameterConversion (source, target_type);
1834 // From object to any reference type
1836 if (source_type == TypeManager.object_type && !target_is_value_type)
1837 return new ClassCast (source, target_type);
1840 // Unboxing conversion.
1842 if (((source_type == TypeManager.enum_type &&
1843 !(source is EmptyCast)) ||
1844 source_type == TypeManager.value_type) && target_is_value_type)
1845 return new UnboxCast (source, target_type);
1848 // From any class S to any class-type T, provided S is a base class of T
1850 if (TypeManager.IsSubclassOf (target_type, source_type))
1851 return new ClassCast (source, target_type);
1854 // From any interface type S to any interface T provided S is not derived from T
1856 if (source_type.IsInterface && target_type.IsInterface){
1857 if (TypeManager.ImplementsInterface (source_type, target_type))
1858 return null;
1859 else
1860 return new ClassCast (source, target_type);
1864 // From any class type S to any interface T, provides S is not sealed
1865 // and provided S does not implement T.
1867 if (target_type.IsInterface && !source_type.IsSealed) {
1868 if (TypeManager.ImplementsInterface (source_type, target_type))
1869 return null;
1870 else
1871 return new ClassCast (source, target_type);
1876 // From any interface-type S to to any class type T, provided T is not
1877 // sealed, or provided T implements S.
1879 if (source_type.IsInterface) {
1880 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1881 if (target_type.IsClass)
1882 return new ClassCast (source, target_type);
1883 else
1884 return new UnboxCast (source, target_type);
1888 // From System.Collecitons.Generic.IList<T> and its base interfaces to a one-dimensional
1889 // array type S[], provided there is an implicit or explicit reference conversion from S to T.
1891 if (IList_To_Array (source_type, target_type))
1892 return new ClassCast (source, target_type);
1894 return null;
1897 // From an array type S with an element type Se to an array type T with an
1898 // element type Te provided all the following are true:
1899 // * S and T differe only in element type, in other words, S and T
1900 // have the same number of dimensions.
1901 // * Both Se and Te are reference types
1902 // * An explicit referenc conversions exist from Se to Te
1904 if (source_type.IsArray && target_type.IsArray) {
1905 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1907 Type source_element_type = TypeManager.GetElementType (source_type);
1908 Type target_element_type = TypeManager.GetElementType (target_type);
1910 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1911 if (ExplicitReferenceConversionExists (source_element_type,
1912 target_element_type))
1913 return new ClassCast (source, target_type);
1918 // From System.Array to any array-type
1919 if (source_type == TypeManager.array_type &&
1920 target_type.IsArray) {
1921 return new ClassCast (source, target_type);
1925 // From System delegate to any delegate-type
1927 if (source_type == TypeManager.delegate_type &&
1928 TypeManager.IsDelegateType (target_type))
1929 return new ClassCast (source, target_type);
1932 // From ICloneable to Array or Delegate types
1934 if (source_type == TypeManager.icloneable_type &&
1935 (target_type == TypeManager.array_type ||
1936 target_type == TypeManager.delegate_type))
1937 return new ClassCast (source, target_type);
1939 return null;
1942 /// <summary>
1943 /// Performs an explicit conversion of the expression `expr' whose
1944 /// type is expr.Type to `target_type'.
1945 /// </summary>
1946 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1947 Type target_type, Location loc)
1949 Type expr_type = expr.Type;
1951 // Explicit conversion includes implicit conversion and it used for enum underlying types too
1952 Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1953 if (ne != null)
1954 return ne;
1957 // Unboxing conversions; only object types can be convertible to enum
1959 if (expr_type == TypeManager.object_type && target_type.IsValueType)
1960 return new UnboxCast (expr, target_type);
1962 if (TypeManager.IsEnumType (expr_type)) {
1963 if (expr is EnumConstant)
1964 return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1966 return ExplicitConversionCore (ec, new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1969 if (TypeManager.IsEnumType (target_type)){
1970 if (expr_type == TypeManager.enum_type)
1971 return new UnboxCast (expr, target_type);
1973 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1974 if (ce != null)
1975 return new EmptyCast (ce, target_type);
1978 ne = ExplicitNumericConversion (expr, target_type);
1979 if (ne != null)
1980 return ne;
1983 // Skip the ExplicitReferenceConversion because we can not convert
1984 // from Null to a ValueType, and ExplicitReference wont check against
1985 // null literal explicitly
1987 if (expr_type != TypeManager.null_type){
1988 ne = ExplicitReferenceConversion (expr, target_type);
1989 if (ne != null)
1990 return ne;
1993 if (ec.InUnsafe){
1994 ne = ExplicitUnsafe (expr, target_type);
1995 if (ne != null)
1996 return ne;
1999 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2000 if (ne != null)
2001 return ne;
2003 return null;
2006 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
2008 Type expr_type = expr.Type;
2010 if (target_type.IsPointer){
2011 if (expr_type.IsPointer)
2012 return new EmptyCast (expr, target_type);
2014 if (expr_type == TypeManager.sbyte_type ||
2015 expr_type == TypeManager.short_type ||
2016 expr_type == TypeManager.int32_type ||
2017 expr_type == TypeManager.int64_type)
2018 return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
2020 if (expr_type == TypeManager.ushort_type ||
2021 expr_type == TypeManager.uint32_type ||
2022 expr_type == TypeManager.uint64_type ||
2023 expr_type == TypeManager.byte_type)
2024 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2027 if (expr_type.IsPointer){
2028 if (target_type == TypeManager.sbyte_type)
2029 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
2030 else if (target_type == TypeManager.byte_type)
2031 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
2032 else if (target_type == TypeManager.short_type)
2033 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
2034 else if (target_type == TypeManager.ushort_type)
2035 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
2036 else if (target_type == TypeManager.int32_type)
2037 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
2038 else if (target_type == TypeManager.uint32_type)
2039 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
2040 else if (target_type == TypeManager.uint64_type)
2041 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
2042 else if (target_type == TypeManager.int64_type){
2043 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2046 return null;
2049 /// <summary>
2050 /// Same as ExplicitConversion, only it doesn't include user defined conversions
2051 /// </summary>
2052 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
2053 Type target_type, Location l)
2055 int errors = Report.Errors;
2056 Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
2057 if (Report.Errors > errors)
2058 return null;
2060 if (ne != null)
2061 return ne;
2063 ne = ExplicitNumericConversion (expr, target_type);
2064 if (ne != null)
2065 return ne;
2067 ne = ExplicitReferenceConversion (expr, target_type);
2068 if (ne != null)
2069 return ne;
2071 if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
2072 return new EmptyCast (expr, target_type);
2074 expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
2075 return null;
2078 /// <summary>
2079 /// Performs an explicit conversion of the expression `expr' whose
2080 /// type is expr.Type to `target_type'.
2081 /// </summary>
2082 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
2083 Type target_type, Location loc)
2085 Expression e;
2086 #if GMCS_SOURCE
2087 Type expr_type = expr.Type;
2088 if (TypeManager.IsNullableType (target_type)) {
2089 if (TypeManager.IsNullableType (expr_type)) {
2090 e = new Nullable.LiftedConversion (
2091 expr, target_type, false, true, loc).Resolve (ec);
2092 if (e != null)
2093 return e;
2094 } else if (expr_type == TypeManager.object_type) {
2095 return new UnboxCast (expr, target_type);
2096 } else {
2097 Type target = TypeManager.GetTypeArguments (target_type) [0];
2099 e = ExplicitConversionCore (ec, expr, target, loc);
2100 if (e != null)
2101 return Nullable.Wrap.Create (e, ec);
2103 } else if (TypeManager.IsNullableType (expr_type)) {
2104 Expression source = Nullable.Unwrap.Create (expr, ec);
2105 if (source != null) {
2106 e = ExplicitConversionCore (ec, source, target_type, loc);
2107 if (e != null)
2108 return e;
2111 #endif
2112 e = ExplicitConversionCore (ec, expr, target_type, loc);
2113 if (e != null)
2114 return e;
2116 expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);
2117 return null;