2006-06-16 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / gmcs / convert.cs
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 //
8 // (C) 2001, 2002, 2003 Ximian, Inc.
9 //
10
11 namespace Mono.CSharp {
12         using System;
13         using System.Collections;
14         using System.Diagnostics;
15         using System.Reflection;
16         using System.Reflection.Emit;
17
18         //
19         // A container class for all the conversion operations
20         //
21         public class Convert {
22         
23                 static Expression TypeParameter_to_Null (Type target_type, Location loc)
24                 {
25                         if (!TypeParameter_to_Null (target_type)) {
26                                 Report.Error (403, loc, "Cannot convert null to the type " +
27                                               "parameter `{0}' becaues it could be a value " +
28                                               "type. Consider using `default ({0})' instead.",
29                                               target_type.ToString ());
30                                 return null;
31                         }
32
33                         Constant expr = new Nullable.NullableLiteral (target_type, loc);
34                         return new NullCast (expr, target_type);
35                 }
36
37                 static bool TypeParameter_to_Null (Type target_type)
38                 {
39                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
40                         if (gc == null)
41                                 return false;
42
43                         if (gc.HasReferenceTypeConstraint)
44                                 return true;
45                         if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
46                                 return true;
47
48                         return false;
49                 }
50
51                 static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
52                 {
53                         ArrayList list = new ArrayList ();
54                         list.Add (gc.EffectiveBaseClass);
55                         foreach (Type t in gc.InterfaceConstraints) {
56                                 if (!t.IsGenericParameter)
57                                         continue;
58
59                                 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
60                                 if (new_gc != null)
61                                         list.Add (TypeParam_EffectiveBaseType (new_gc));
62                         }
63                         return FindMostEncompassedType (list);
64                 }
65
66                 static Expression ImplicitTypeParameterConversion (Expression expr,
67                                                                    Type target_type)
68                 {
69                         Type expr_type = expr.Type;
70
71                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
72
73                         if (gc == null) {
74                                 if (target_type == TypeManager.object_type)
75                                         return new BoxedCast (expr, target_type);
76
77                                 return null;
78                         }
79
80                         // We're converting from a type parameter which is known to be a reference type.
81                         Type base_type = TypeParam_EffectiveBaseType (gc);
82
83                         if (TypeManager.IsSubclassOf (base_type, target_type))
84                                 return new ClassCast (expr, target_type);
85
86                         if (target_type.IsInterface) {
87                                 if (TypeManager.ImplementsInterface (base_type, target_type))
88                                         return new ClassCast (expr, target_type);
89
90                                 foreach (Type t in gc.InterfaceConstraints) {
91                                         if (TypeManager.IsSubclassOf (t, target_type))
92                                                 return new ClassCast (expr, target_type);
93                                         if (TypeManager.ImplementsInterface (t, target_type))
94                                                 return new ClassCast (expr, target_type);
95                                 }
96                         }
97
98                         foreach (Type t in gc.InterfaceConstraints) {
99                                 if (!t.IsGenericParameter)
100                                         continue;
101                                 if (TypeManager.IsSubclassOf (t, target_type))
102                                         return new ClassCast (expr, target_type);
103                                 if (TypeManager.ImplementsInterface (t, target_type))
104                                         return new ClassCast (expr, target_type);
105                         }
106
107                         return null;
108                 }
109
110                 static bool ExplicitTypeParameterConversionExists (Type source_type, Type target_type)
111                 {
112                         if (target_type.IsInterface)
113                                 return true;
114
115                         if (target_type.IsGenericParameter) {
116                                 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
117                                 if (gc == null)
118                                         return false;
119
120                                 foreach (Type iface in gc.InterfaceConstraints) {
121                                         if (!iface.IsGenericParameter)
122                                                 continue;
123
124                                         if (TypeManager.IsSubclassOf (source_type, iface))
125                                                 return true;
126                                 }
127                         }
128
129                         return false;
130                 }
131
132                 static Expression ExplicitTypeParameterConversion (Expression source, Type target_type)
133                 {
134                         Type source_type = source.Type;
135
136                         if (target_type.IsInterface)
137                                 return new ClassCast (source, target_type);
138
139                         if (target_type.IsGenericParameter) {
140                                 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
141                                 if (gc == null)
142                                         return null;
143
144                                 foreach (Type iface in gc.InterfaceConstraints) {
145                                         if (!iface.IsGenericParameter)
146                                                 continue;
147
148                                         if (TypeManager.IsSubclassOf (source_type, iface))
149                                                 return new ClassCast (source, target_type);
150                                 }
151                         }
152
153                         return null;
154                 }
155
156                 static EmptyExpression MyEmptyExpr;
157                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
158                 {
159                         Type expr_type = expr.Type;
160
161                         if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
162                                 // if we are a method group, emit a warning
163
164                                 expr.Emit (null);
165                         }
166
167                         if (expr_type == TypeManager.void_type)
168                                 return null;
169
170                         if (expr_type.IsGenericParameter)
171                                 return ImplicitTypeParameterConversion (expr, target_type);
172                                 
173                         //
174                         // notice that it is possible to write "ValueType v = 1", the ValueType here
175                         // is an abstract class, and not really a value type, so we apply the same rules.
176                         //
177                         if (target_type == TypeManager.object_type) {
178                                 //
179                                 // A pointer type cannot be converted to object
180                                 // 
181                                 if (expr_type.IsPointer)
182                                         return null;
183
184                                 if (TypeManager.IsValueType (expr_type))
185                                         return new BoxedCast (expr, target_type);
186                                 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
187                                         if (expr_type == TypeManager.anonymous_method_type)
188                                                 return null;
189                                         return new EmptyCast (expr, target_type);
190                                 }
191
192                                 return null;
193                         } else if (target_type == TypeManager.value_type) {
194                                 if (TypeManager.IsValueType (expr_type))
195                                         return new BoxedCast (expr, target_type);
196                                 if (expr_type == TypeManager.null_type)
197                                         return new NullCast ((Constant)expr, target_type);
198
199                                 return null;
200                         } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
201                                 //
202                                 // Special case: enumeration to System.Enum.
203                                 // System.Enum is not a value type, it is a class, so we need
204                                 // a boxing conversion
205                                 //
206                                 if (expr_type.IsEnum || expr_type.IsGenericParameter)
207                                         return new BoxedCast (expr, target_type);
208
209                                 return new EmptyCast (expr, target_type);
210                         }
211
212                         // This code is kind of mirrored inside ImplicitStandardConversionExists
213                         // with the small distinction that we only probe there
214                         //
215                         // Always ensure that the code here and there is in sync
216
217                         // from the null type to any reference-type.
218                         if (expr_type == TypeManager.null_type){
219                                 if (target_type.IsPointer)
220                                         return new EmptyCast (NullPointer.Null, target_type);
221                                         
222                                 if (!target_type.IsValueType) {
223                                         if (expr is Constant)
224                                                 return new NullCast ((Constant)expr, target_type);
225
226                                         // I found only one case when it happens -- Foo () ? null : null;
227                                         Report.Warning (-100, 1, expr.Location, "The result of the expression is always `null'");
228                                         return new NullCast (new NullLiteral (expr.Location), target_type);
229                                 }
230                         }
231
232                         // from any class-type S to any interface-type T.
233                         if (target_type.IsInterface) {
234                                 if (target_type != TypeManager.iconvertible_type &&
235                                     expr_type.IsValueType && (expr is Constant) &&
236                                     !(expr is IntLiteral || expr is BoolLiteral ||
237                                       expr is FloatLiteral || expr is DoubleLiteral ||
238                                       expr is LongLiteral || expr is CharLiteral ||
239                                       expr is StringLiteral || expr is DecimalLiteral ||
240                                       expr is UIntLiteral || expr is ULongLiteral)) {
241                                         return null;
242                                 }
243
244                                 if (TypeManager.ImplementsInterface (expr_type, target_type)){
245                                         if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
246                                                 return new BoxedCast (expr, target_type);
247                                         else
248                                                 return new EmptyCast (expr, target_type);
249                                 }
250                         }
251
252                         // from any interface type S to interface-type T.
253                         if (expr_type.IsInterface && target_type.IsInterface) {
254                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
255                                         return new EmptyCast (expr, target_type);
256                                 else
257                                         return null;
258                         }
259
260                         // from an array-type S to an array-type of type T
261                         if (expr_type.IsArray && target_type.IsArray) {
262                                 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
263
264                                         Type expr_element_type = TypeManager.GetElementType (expr_type);
265
266                                         if (MyEmptyExpr == null)
267                                                 MyEmptyExpr = new EmptyExpression ();
268                                                 
269                                         MyEmptyExpr.SetType (expr_element_type);
270                                         Type target_element_type = TypeManager.GetElementType (target_type);
271
272                                         if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
273                                                 if (ImplicitStandardConversionExists (MyEmptyExpr,
274                                                                                       target_element_type))
275                                                         return new EmptyCast (expr, target_type);
276                                 }
277                         }
278                                 
279                         // from an array-type to System.Array
280                         if (expr_type.IsArray && target_type == TypeManager.array_type)
281                                 return new EmptyCast (expr, target_type);
282
283                         // from an array-type of type T to IEnumerable<T>
284                         if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
285                                 return new EmptyCast (expr, target_type);
286
287                         // from any delegate type to System.Delegate
288                         if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
289                             target_type == TypeManager.delegate_type)
290                                 return new EmptyCast (expr, target_type);
291                                         
292                         // from any array-type or delegate type into System.ICloneable.
293                         if (expr_type.IsArray ||
294                             expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
295                                 if (target_type == TypeManager.icloneable_type)
296                                         return new EmptyCast (expr, target_type);
297
298                         // from a generic type definition to a generic instance.
299                         if (TypeManager.IsEqual (expr_type, target_type))
300                                 return new EmptyCast (expr, target_type);
301
302                         return null;
303                 }
304
305                 //
306                 // Tests whether an implicit reference conversion exists between expr_type
307                 // and target_type
308                 //
309                 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
310                 {
311                         if (target_type.IsValueType)
312                                 return false;
313
314                         Type expr_type = expr.Type;
315
316                         if (expr_type.IsGenericParameter)
317                                 return ImplicitTypeParameterConversion (expr, target_type) != null;
318
319                         //
320                         // This is the boxed case.
321                         //
322                         if (target_type == TypeManager.object_type) {
323                                 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
324                                     expr_type.IsInterface || expr_type == TypeManager.enum_type)
325                                         if (target_type != TypeManager.anonymous_method_type)
326                                         return true;
327
328                                 return false;
329                         } else if (TypeManager.IsSubclassOf (expr_type, target_type))
330                                 return true;
331
332                         // Please remember that all code below actually comes
333                         // from ImplicitReferenceConversion so make sure code remains in sync
334                                 
335                         // from any class-type S to any interface-type T.
336                         if (target_type.IsInterface) {
337                                 if (target_type != TypeManager.iconvertible_type &&
338                                     expr_type.IsValueType && (expr is Constant) &&
339                                     !(expr is IntLiteral || expr is BoolLiteral ||
340                                       expr is FloatLiteral || expr is DoubleLiteral ||
341                                       expr is LongLiteral || expr is CharLiteral ||
342                                       expr is StringLiteral || expr is DecimalLiteral ||
343                                       expr is UIntLiteral || expr is ULongLiteral)) {
344                                         return false;
345                                 }
346                                 
347                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
348                                         return true;
349                         }
350                                 
351                         // from any interface type S to interface-type T.
352                         if (expr_type.IsInterface && target_type.IsInterface)
353                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
354                                         return true;
355                                 
356                         // from an array-type S to an array-type of type T
357                         if (expr_type.IsArray && target_type.IsArray) {
358                                 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
359                                                 
360                                         Type expr_element_type = expr_type.GetElementType ();
361
362                                         if (MyEmptyExpr == null)
363                                                 MyEmptyExpr = new EmptyExpression ();
364                                                 
365                                         MyEmptyExpr.SetType (expr_element_type);
366                                         Type target_element_type = TypeManager.GetElementType (target_type);
367                                                 
368                                         if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
369                                                 if (ImplicitStandardConversionExists (MyEmptyExpr,
370                                                                                       target_element_type))
371                                                         return true;
372                                 }
373                         }
374                                 
375                         // from an array-type to System.Array
376                         if (expr_type.IsArray && (target_type == TypeManager.array_type))
377                                 return true;
378
379                         // from an array-type of type T to IEnumerable<T>
380                         if (expr_type.IsArray && TypeManager.IsIEnumerable (expr_type, target_type))
381                                 return true;
382
383                         // from any delegate type to System.Delegate
384                         if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
385                             target_type == TypeManager.delegate_type)
386                                 if (target_type.IsAssignableFrom (expr_type))
387                                         return true;
388                                         
389                         // from any array-type or delegate type into System.ICloneable.
390                         if (expr_type.IsArray ||
391                             expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
392                                 if (target_type == TypeManager.icloneable_type)
393                                         return true;
394                                 
395                         // from the null type to any reference-type.
396                         if (expr_type == TypeManager.null_type){
397                                 if (target_type.IsPointer)
398                                         return true;
399                         
400                                 if (!target_type.IsValueType)
401                                         return true;
402                         }
403
404                         // from a generic type definition to a generic instance.
405                         if (TypeManager.IsEqual (expr_type, target_type))
406                                 return true;
407
408                         return false;
409                 }
410
411                 /// <summary>
412                 ///   Implicit Numeric Conversions.
413                 ///
414                 ///   expr is the expression to convert, returns a new expression of type
415                 ///   target_type or null if an implicit conversion is not possible.
416                 /// </summary>
417                 static public Expression ImplicitNumericConversion (Expression expr,
418                                                                     Type target_type)
419                 {
420                         Type expr_type = expr.Type;
421
422                         //
423                         // Attempt to do the implicit constant expression conversions
424
425                         if (expr is Constant){
426                                 if (expr is IntConstant){
427                                         Expression e;
428                                         
429                                         e = TryImplicitIntConversion (target_type, (IntConstant) expr);
430                                         
431                                         if (e != null)
432                                                 return e;
433                                 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
434                                         //
435                                         // Try the implicit constant expression conversion
436                                         // from long to ulong, instead of a nice routine,
437                                         // we just inline it
438                                         //
439                                         long v = ((LongConstant) expr).Value;
440                                         if (v >= 0)
441                                                 return new ULongConstant ((ulong) v, expr.Location);
442                                 } 
443                         }
444                         
445                         Type real_target_type = target_type;
446
447                         if (expr_type == TypeManager.sbyte_type){
448                                 //
449                                 // From sbyte to short, int, long, float, double, decimal
450                                 //
451                                 if (real_target_type == TypeManager.int32_type)
452                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
453                                 if (real_target_type == TypeManager.int64_type)
454                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
455                                 if (real_target_type == TypeManager.double_type)
456                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
457                                 if (real_target_type == TypeManager.float_type)
458                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
459                                 if (real_target_type == TypeManager.short_type)
460                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
461                                 if (real_target_type == TypeManager.decimal_type)
462                                         return new CastToDecimal (expr);
463                         } else if (expr_type == TypeManager.byte_type){
464                                 //
465                                 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
466                                 // 
467                                 if ((real_target_type == TypeManager.short_type) ||
468                                     (real_target_type == TypeManager.ushort_type) ||
469                                     (real_target_type == TypeManager.int32_type) ||
470                                     (real_target_type == TypeManager.uint32_type))
471                                         return new EmptyCast (expr, target_type);
472
473                                 if (real_target_type == TypeManager.uint64_type)
474                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
475                                 if (real_target_type == TypeManager.int64_type)
476                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
477                                 if (real_target_type == TypeManager.float_type)
478                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
479                                 if (real_target_type == TypeManager.double_type)
480                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
481                                 if (real_target_type == TypeManager.decimal_type)
482                                         return new CastToDecimal (expr);
483                                 
484                         } else if (expr_type == TypeManager.short_type){
485                                 //
486                                 // From short to int, long, float, double, decimal
487                                 // 
488                                 if (real_target_type == TypeManager.int32_type)
489                                         return new EmptyCast (expr, target_type);
490                                 if (real_target_type == TypeManager.int64_type)
491                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
492                                 if (real_target_type == TypeManager.double_type)
493                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
494                                 if (real_target_type == TypeManager.float_type)
495                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
496                                 if (real_target_type == TypeManager.decimal_type)
497                                         return new CastToDecimal (expr);
498                                 
499                         } else if (expr_type == TypeManager.ushort_type){
500                                 //
501                                 // From ushort to int, uint, long, ulong, float, double, decimal
502                                 //
503                                 if (real_target_type == TypeManager.uint32_type)
504                                         return new EmptyCast (expr, target_type);
505
506                                 if (real_target_type == TypeManager.uint64_type)
507                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
508                                 if (real_target_type == TypeManager.int32_type)
509                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
510                                 if (real_target_type == TypeManager.int64_type)
511                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
512                                 if (real_target_type == TypeManager.double_type)
513                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
514                                 if (real_target_type == TypeManager.float_type)
515                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
516                                 if (real_target_type == TypeManager.decimal_type)
517                                         return new CastToDecimal (expr);
518                         } else if (expr_type == TypeManager.int32_type){
519                                 //
520                                 // From int to long, float, double, decimal
521                                 //
522                                 if (real_target_type == TypeManager.int64_type)
523                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
524                                 if (real_target_type == TypeManager.double_type)
525                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
526                                 if (real_target_type == TypeManager.float_type)
527                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
528                                 if (real_target_type == TypeManager.decimal_type)
529                                         return new CastToDecimal (expr);
530                         } else if (expr_type == TypeManager.uint32_type){
531                                 //
532                                 // From uint to long, ulong, float, double, decimal
533                                 //
534                                 if (real_target_type == TypeManager.int64_type)
535                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
536                                 if (real_target_type == TypeManager.uint64_type)
537                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
538                                 if (real_target_type == TypeManager.double_type)
539                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
540                                                                OpCodes.Conv_R8);
541                                 if (real_target_type == TypeManager.float_type)
542                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
543                                                                OpCodes.Conv_R4);
544                                 if (real_target_type == TypeManager.decimal_type)
545                                         return new CastToDecimal (expr);
546                         } else if (expr_type == TypeManager.int64_type){
547                                 //
548                                 // From long/ulong to float, double
549                                 //
550                                 if (real_target_type == TypeManager.double_type)
551                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
552                                 if (real_target_type == TypeManager.float_type)
553                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
554                                 if (real_target_type == TypeManager.decimal_type)
555                                         return new CastToDecimal (expr);
556                         } else if (expr_type == TypeManager.uint64_type){
557                                 //
558                                 // From ulong to float, double
559                                 //
560                                 if (real_target_type == TypeManager.double_type)
561                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
562                                                                OpCodes.Conv_R8);
563                                 if (real_target_type == TypeManager.float_type)
564                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
565                                                                OpCodes.Conv_R4);
566                                 if (real_target_type == TypeManager.decimal_type)
567                                         return new CastToDecimal (expr);
568                         } else if (expr_type == TypeManager.char_type){
569                                 //
570                                 // From char to ushort, int, uint, long, ulong, float, double, decimal
571                                 // 
572                                 if ((real_target_type == TypeManager.ushort_type) ||
573                                     (real_target_type == TypeManager.int32_type) ||
574                                     (real_target_type == TypeManager.uint32_type))
575                                         return new EmptyCast (expr, target_type);
576                                 if (real_target_type == TypeManager.uint64_type)
577                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
578                                 if (real_target_type == TypeManager.int64_type)
579                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
580                                 if (real_target_type == TypeManager.float_type)
581                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
582                                 if (real_target_type == TypeManager.double_type)
583                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
584                                 if (real_target_type == TypeManager.decimal_type)
585                                         return new CastToDecimal (expr);
586                         } else if (expr_type == TypeManager.float_type){
587                                 //
588                                 // float to double
589                                 //
590                                 if (real_target_type == TypeManager.double_type)
591                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
592                         }
593
594                         return null;
595                 }
596
597                 /// <summary>
598                 ///  Same as ImplicitStandardConversionExists except that it also looks at
599                 ///  implicit user defined conversions - needed for overload resolution
600                 /// </summary>
601                 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
602                 {
603                         if (expr is NullLiteral) {
604                                 if (target_type.IsGenericParameter)
605                                         return TypeParameter_to_Null (target_type);
606
607                                 if (TypeManager.IsNullableType (target_type))
608                                         return true;
609                         }
610
611                         if (ImplicitStandardConversionExists (expr, target_type))
612                                 return true;
613
614                         return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
615                 }
616
617                 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
618                 {
619                         return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
620                 }
621
622                 /// <summary>
623                 ///  Determines if a standard implicit conversion exists from
624                 ///  expr_type to target_type
625                 ///
626                 ///  ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
627                 /// </summary>
628                 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
629                 {
630                         Type expr_type = expr.Type;
631
632                         if (expr_type == TypeManager.void_type)
633                                 return false;
634
635                         //Console.WriteLine ("Expr is {0}", expr);
636                         //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
637                         if (expr_type.Equals (target_type))
638                                 return true;
639
640
641                         // First numeric conversions 
642
643                         if (expr_type == TypeManager.sbyte_type){
644                                 //
645                                 // From sbyte to short, int, long, float, double, decimal
646                                 //
647                                 if ((target_type == TypeManager.int32_type) || 
648                                     (target_type == TypeManager.int64_type) ||
649                                     (target_type == TypeManager.double_type) ||
650                                     (target_type == TypeManager.float_type)  ||
651                                     (target_type == TypeManager.short_type) ||
652                                     (target_type == TypeManager.decimal_type))
653                                         return true;
654                                 
655                         } else if (expr_type == TypeManager.byte_type){
656                                 //
657                                 // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
658                                 // 
659                                 if ((target_type == TypeManager.short_type) ||
660                                     (target_type == TypeManager.ushort_type) ||
661                                     (target_type == TypeManager.int32_type) ||
662                                     (target_type == TypeManager.uint32_type) ||
663                                     (target_type == TypeManager.uint64_type) ||
664                                     (target_type == TypeManager.int64_type) ||
665                                     (target_type == TypeManager.float_type) ||
666                                     (target_type == TypeManager.double_type) ||
667                                     (target_type == TypeManager.decimal_type))
668                                         return true;
669         
670                         } else if (expr_type == TypeManager.short_type){
671                                 //
672                                 // From short to int, long, double, float, decimal
673                                 // 
674                                 if ((target_type == TypeManager.int32_type) ||
675                                     (target_type == TypeManager.int64_type) ||
676                                     (target_type == TypeManager.double_type) ||
677                                     (target_type == TypeManager.float_type) ||
678                                     (target_type == TypeManager.decimal_type))
679                                         return true;
680                                         
681                         } else if (expr_type == TypeManager.ushort_type){
682                                 //
683                                 // From ushort to int, uint, long, ulong, double, float, decimal
684                                 //
685                                 if ((target_type == TypeManager.uint32_type) ||
686                                     (target_type == TypeManager.uint64_type) ||
687                                     (target_type == TypeManager.int32_type) ||
688                                     (target_type == TypeManager.int64_type) ||
689                                     (target_type == TypeManager.double_type) ||
690                                     (target_type == TypeManager.float_type) ||
691                                     (target_type == TypeManager.decimal_type))
692                                         return true;
693                                     
694                         } else if (expr_type == TypeManager.int32_type){
695                                 //
696                                 // From int to long, double, float, decimal
697                                 //
698                                 if ((target_type == TypeManager.int64_type) ||
699                                     (target_type == TypeManager.double_type) ||
700                                     (target_type == TypeManager.float_type) ||
701                                     (target_type == TypeManager.decimal_type))
702                                         return true;
703                                         
704                         } else if (expr_type == TypeManager.uint32_type){
705                                 //
706                                 // From uint to long, ulong, double, float, decimal
707                                 //
708                                 if ((target_type == TypeManager.int64_type) ||
709                                     (target_type == TypeManager.uint64_type) ||
710                                     (target_type == TypeManager.double_type) ||
711                                     (target_type == TypeManager.float_type) ||
712                                     (target_type == TypeManager.decimal_type))
713                                         return true;
714                                         
715                         } else if ((expr_type == TypeManager.uint64_type) ||
716                                    (expr_type == TypeManager.int64_type)) {
717                                 //
718                                 // From long/ulong to double, float, decimal
719                                 //
720                                 if ((target_type == TypeManager.double_type) ||
721                                     (target_type == TypeManager.float_type) ||
722                                     (target_type == TypeManager.decimal_type))
723                                         return true;
724                                     
725                         } else if (expr_type == TypeManager.char_type){
726                                 //
727                                 // From char to ushort, int, uint, ulong, long, float, double, decimal
728                                 // 
729                                 if ((target_type == TypeManager.ushort_type) ||
730                                     (target_type == TypeManager.int32_type) ||
731                                     (target_type == TypeManager.uint32_type) ||
732                                     (target_type == TypeManager.uint64_type) ||
733                                     (target_type == TypeManager.int64_type) ||
734                                     (target_type == TypeManager.float_type) ||
735                                     (target_type == TypeManager.double_type) ||
736                                     (target_type == TypeManager.decimal_type))
737                                         return true;
738
739                         } else if (expr_type == TypeManager.float_type){
740                                 //
741                                 // float to double
742                                 //
743                                 if (target_type == TypeManager.double_type)
744                                         return true;
745                         }       
746                         
747                         if (expr.eclass == ExprClass.MethodGroup){
748                                 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
749                                         MethodGroupExpr mg = expr as MethodGroupExpr;
750                                         if (mg != null){
751                                                 return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
752                                         }
753                                 }
754                         }
755
756                         if (ImplicitReferenceConversionExists (expr, target_type))
757                                 return true;
758
759                         //
760                         // Implicit Constant Expression Conversions
761                         //
762                         if (expr is IntConstant){
763                                 int value = ((IntConstant) expr).Value;
764
765                                 if (target_type == TypeManager.sbyte_type){
766                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
767                                                 return true;
768                                 } else if (target_type == TypeManager.byte_type){
769                                         if (value >= 0 && value <= Byte.MaxValue)
770                                                 return true;
771                                 } else if (target_type == TypeManager.short_type){
772                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
773                                                 return true;
774                                 } else if (target_type == TypeManager.ushort_type){
775                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
776                                                 return true;
777                                 } else if (target_type == TypeManager.uint32_type){
778                                         if (value >= 0)
779                                                 return true;
780                                 } else if (target_type == TypeManager.uint64_type){
781                                          //
782                                          // we can optimize this case: a positive int32
783                                          // always fits on a uint64.  But we need an opcode
784                                          // to do it.
785                                          //
786                                         if (value >= 0)
787                                                 return true;
788                                 }
789                                 
790                                 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
791                                         return true;
792                         }
793
794                         if (expr is LongConstant && target_type == TypeManager.uint64_type){
795                                 //
796                                 // Try the implicit constant expression conversion
797                                 // from long to ulong, instead of a nice routine,
798                                 // we just inline it
799                                 //
800                                 long v = ((LongConstant) expr).Value;
801                                 if (v >= 0)
802                                         return true;
803                         }
804                         
805                         if ((target_type == TypeManager.enum_type ||
806                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
807                              expr is IntLiteral){
808                                 IntLiteral i = (IntLiteral) expr;
809
810                                 if (i.Value == 0)
811                                         return true;
812                         }
813
814                         //
815                         // If `expr_type' implements `target_type' (which is an iface)
816                         // see TryImplicitIntConversion
817                         // 
818                         if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
819                                 return true;
820
821                         if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
822                                 return true;
823
824                         if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
825                                 return true;
826
827                         if (TypeManager.IsNullableTypeOf (target_type, expr_type))
828                                 return true;                    
829
830                         if (expr_type == TypeManager.anonymous_method_type){
831                                 if (!TypeManager.IsDelegateType (target_type))
832                                         return false;
833
834                                 AnonymousMethod am = (AnonymousMethod) expr;
835                                 return am.ImplicitStandardConversionExists (target_type);
836                         }
837
838                         return false;
839                 }
840
841                 /// <summary>
842                 ///  Finds "most encompassed type" according to the spec (13.4.2)
843                 ///  amongst the methods in the MethodGroupExpr
844                 /// </summary>
845                 static Type FindMostEncompassedType (ArrayList types)
846                 {
847                         Type best = null;
848
849                         if (types.Count == 0)
850                                 return null;
851
852                         if (types.Count == 1)
853                                 return (Type) types [0];
854
855                         EmptyExpression expr = EmptyExpression.Grab ();
856
857                         foreach (Type t in types) {
858                                 if (best == null) {
859                                         best = t;
860                                         continue;
861                                 }
862
863                                 expr.SetType (t);
864                                 if (ImplicitStandardConversionExists (expr, best))
865                                         best = t;
866                         }
867
868                         expr.SetType (best);
869                         foreach (Type t in types) {
870                                 if (best == t)
871                                         continue;
872                                 if (!ImplicitStandardConversionExists (expr, t)) {
873                                         best = null;
874                                         break;
875                                 }
876                         }
877
878                         EmptyExpression.Release (expr);
879
880                         return best;
881                 }
882         
883                 /// <summary>
884                 ///  Finds "most encompassing type" according to the spec (13.4.2)
885                 ///  amongst the types in the given set
886                 /// </summary>
887                 static Type FindMostEncompassingType (ArrayList types)
888                 {
889                         Type best = null;
890
891                         if (types.Count == 0)
892                                 return null;
893
894                         if (types.Count == 1)
895                                 return (Type) types [0];
896
897                         EmptyExpression expr = EmptyExpression.Grab ();
898
899                         foreach (Type t in types) {
900                                 if (best == null) {
901                                         best = t;
902                                         continue;
903                                 }
904
905                                 expr.SetType (best);
906                                 if (ImplicitStandardConversionExists (expr, t))
907                                         best = t;
908                         }
909
910                         foreach (Type t in types) {
911                                 if (best == t)
912                                         continue;
913                                 expr.SetType (t);
914                                 if (!ImplicitStandardConversionExists (expr, best)) {
915                                         best = null;
916                                         break;
917                                 }
918                         }
919
920                         EmptyExpression.Release (expr);
921
922                         return best;
923                 }
924
925                 /// <summary>
926                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
927                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
928                 ///   for explicit and implicit conversion operators.
929                 /// </summary>
930                 static public Type FindMostSpecificSource (IList list,
931                                                            Expression source, bool apply_explicit_conv_rules)
932                 {
933                         ArrayList src_types_set = new ArrayList ();
934                         
935                         //
936                         // If any operator converts from S then Sx = S
937                         //
938                         Type source_type = source.Type;
939                         foreach (MethodBase mb in list){
940                                 ParameterData pd = TypeManager.GetParameterData (mb);
941                                 Type param_type = pd.ParameterType (0);
942
943                                 if (param_type == source_type)
944                                         return param_type;
945
946                                 src_types_set.Add (param_type);
947                         }
948                         
949                         //
950                         // Explicit Conv rules
951                         //
952                         if (apply_explicit_conv_rules) {
953                                 ArrayList candidate_set = new ArrayList ();
954
955                                 foreach (Type param_type in src_types_set){
956                                         if (ImplicitStandardConversionExists (source, param_type))
957                                                 candidate_set.Add (param_type);
958                                 }
959
960                                 if (candidate_set.Count != 0)
961                                         return FindMostEncompassedType (candidate_set);
962                         }
963
964                         //
965                         // Final case
966                         //
967                         if (apply_explicit_conv_rules)
968                                 return FindMostEncompassingType (src_types_set);
969                         else
970                                 return FindMostEncompassedType (src_types_set);
971                 }
972
973                 /// <summary>
974                 ///  Finds the most specific target Tx according to section 13.4.4
975                 /// </summary>
976                 static public Type FindMostSpecificTarget (IList list,
977                                                            Type target, bool apply_explicit_conv_rules)
978                 {
979                         ArrayList tgt_types_set = new ArrayList ();
980                         
981                         //
982                         // If any operator converts to T then Tx = T
983                         //
984                         foreach (MethodInfo mi in list){
985                                 Type ret_type = mi.ReturnType;
986                                 if (ret_type == target)
987                                         return ret_type;
988
989                                 tgt_types_set.Add (ret_type);
990                         }
991
992                         //
993                         // Explicit conv rules
994                         //
995                         if (apply_explicit_conv_rules) {
996                                 ArrayList candidate_set = new ArrayList ();
997
998                                 EmptyExpression expr = EmptyExpression.Grab ();
999
1000                                 foreach (Type ret_type in tgt_types_set){
1001                                         expr.SetType (ret_type);
1002                                         
1003                                         if (ImplicitStandardConversionExists (expr, target))
1004                                                 candidate_set.Add (ret_type);
1005                                 }
1006
1007                                 EmptyExpression.Release (expr);
1008
1009                                 if (candidate_set.Count != 0)
1010                                         return FindMostEncompassingType (candidate_set);
1011                         }
1012                         
1013                         //
1014                         // Okay, final case !
1015                         //
1016                         if (apply_explicit_conv_rules)
1017                                 return FindMostEncompassedType (tgt_types_set);
1018                         else 
1019                                 return FindMostEncompassingType (tgt_types_set);
1020                 }
1021                 
1022                 /// <summary>
1023                 ///  User-defined Implicit conversions
1024                 /// </summary>
1025                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1026                                                                  Type target, Location loc)
1027                 {
1028                         return UserDefinedConversion (ec, source, target, loc, false);
1029                 }
1030
1031                 /// <summary>
1032                 ///  User-defined Explicit conversions
1033                 /// </summary>
1034                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1035                                                                  Type target, Location loc)
1036                 {
1037                         return UserDefinedConversion (ec, source, target, loc, true);
1038                 }
1039
1040                 static void AddConversionOperators (ArrayList list, 
1041                                                     Expression source, Type target_type, 
1042                                                     bool look_for_explicit,
1043                                                     MethodGroupExpr mg)
1044                 {
1045                         if (mg == null)
1046                                 return;
1047
1048                         Type source_type = source.Type;
1049                         EmptyExpression expr = EmptyExpression.Grab ();
1050                         foreach (MethodInfo m in mg.Methods) {
1051                                 ParameterData pd = TypeManager.GetParameterData (m);
1052                                 Type return_type = m.ReturnType;
1053                                 Type arg_type = pd.ParameterType (0);
1054
1055                                 if (source_type != arg_type) {
1056                                         if (!ImplicitStandardConversionExists (source, arg_type)) {
1057                                                 if (!look_for_explicit)
1058                                                         continue;
1059                                                 expr.SetType (arg_type);
1060                                                 if (!ImplicitStandardConversionExists (expr, source_type))
1061                                                         continue;
1062                                         }
1063                                 }
1064
1065                                 if (target_type != return_type) {
1066                                         expr.SetType (return_type);
1067                                         if (!ImplicitStandardConversionExists (expr, target_type)) {
1068                                                 if (!look_for_explicit)
1069                                                         continue;
1070                                                 expr.SetType (target_type);
1071                                                 if (!ImplicitStandardConversionExists (expr, return_type))
1072                                                         continue;
1073                                         }
1074                                 }
1075
1076                                 list.Add (m);
1077                         }
1078
1079                         EmptyExpression.Release (expr);
1080                 }
1081
1082                 /// <summary>
1083                 ///   Compute the user-defined conversion operator from source_type to target_type. 
1084                 ///   `look_for_explicit' controls whether we should also include the list of explicit operators
1085                 /// </summary>
1086                 static MethodInfo GetConversionOperator (EmitContext ec, Expression source, Type target_type, bool look_for_explicit)
1087                 {
1088                         ArrayList ops = new ArrayList (4);
1089
1090                         Type source_type = source.Type;
1091
1092                         if (source_type != TypeManager.decimal_type) {
1093                                 AddConversionOperators (ops, source, target_type, look_for_explicit,
1094                                         Expression.MethodLookup (ec, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1095                                 if (look_for_explicit) {
1096                                         AddConversionOperators (ops, source, target_type, look_for_explicit,
1097                                                 Expression.MethodLookup (
1098                                                         ec, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1099                                 }
1100                         }
1101
1102                         if (target_type != TypeManager.decimal_type) {
1103                                 AddConversionOperators (ops, source, target_type, look_for_explicit,
1104                                         Expression.MethodLookup (ec, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
1105                                 if (look_for_explicit) {
1106                                         AddConversionOperators (ops, source, target_type, look_for_explicit,
1107                                                 Expression.MethodLookup (
1108                                                         ec, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
1109                                 }
1110                         }
1111
1112                         if (ops.Count == 0)
1113                                 return null;
1114
1115                         Type most_specific_source = FindMostSpecificSource (ops, source, look_for_explicit);
1116                         if (most_specific_source == null)
1117                                 return null;
1118
1119                         Type most_specific_target = FindMostSpecificTarget (ops, target_type, look_for_explicit);
1120                         if (most_specific_target == null)
1121                                 return null;
1122
1123                         MethodInfo method = null;
1124
1125                         foreach (MethodInfo m in ops) {
1126                                 if (m.ReturnType != most_specific_target)
1127                                         continue;
1128                                 if (TypeManager.GetParameterData (m).ParameterType (0) != most_specific_source)
1129                                         continue;
1130                                 // Ambiguous: more than one conversion operator satisfies the signature.
1131                                 if (method != null)
1132                                         return null;
1133                                 method = m;
1134                         }
1135
1136                         return method;
1137                 }
1138
1139                 static DoubleHash explicit_conv = new DoubleHash (100);
1140                 static DoubleHash implicit_conv = new DoubleHash (100);
1141
1142                 /// <summary>
1143                 ///   User-defined conversions
1144                 /// </summary>
1145                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1146                                                                 Type target, Location loc,
1147                                                                 bool look_for_explicit)
1148                 {
1149                         Type source_type = source.Type;
1150                         MethodInfo method = null;
1151
1152                         if (TypeManager.IsNullableType (source_type) && TypeManager.IsNullableType (target))
1153                                 return new Nullable.LiftedConversion (
1154                                         source, target, true, look_for_explicit, loc).Resolve (ec);
1155
1156                         object o;
1157                         DoubleHash hash = look_for_explicit ? explicit_conv : implicit_conv;
1158
1159                         if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
1160                                 method = (MethodInfo) o;
1161                         } else {
1162                                 method = GetConversionOperator (ec, source, target, look_for_explicit);
1163                                 if (!(source is Constant))
1164                                         hash.Insert (source_type, target, method);
1165                         }
1166
1167                         if (method == null)
1168                                 return null;
1169
1170                         Type most_specific_source = TypeManager.GetParameterData (method).ParameterType (0);
1171
1172                         //
1173                         // This will do the conversion to the best match that we
1174                         // found.  Now we need to perform an implict standard conversion
1175                         // if the best match was not the type that we were requested
1176                         // by target.
1177                         //
1178                         if (look_for_explicit)
1179                                 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1180                         else
1181                                 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1182
1183                         if (source == null)
1184                                 return null;
1185
1186                         Expression e;
1187                         e =  new UserCast (method, source, loc);
1188                         if (e.Type != target){
1189                                 if (!look_for_explicit)
1190                                         e = ImplicitConversionStandard (ec, e, target, loc);
1191                                 else
1192                                         e = ExplicitConversionStandard (ec, e, target, loc);
1193                         }
1194
1195                         return e;
1196                 }
1197                 
1198                 /// <summary>
1199                 ///   Converts implicitly the resolved expression `expr' into the
1200                 ///   `target_type'.  It returns a new expression that can be used
1201                 ///   in a context that expects a `target_type'. 
1202                 /// </summary>
1203                 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1204                                                              Type target_type, Location loc)
1205                 {
1206                         Expression e;
1207
1208                         if (target_type == null)
1209                                 throw new Exception ("Target type is null");
1210
1211                         e = ImplicitConversionStandard (ec, expr, target_type, loc);
1212                         if (e != null)
1213                                 return e;
1214
1215                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1216                         if (e != null)
1217                                 return e;
1218
1219                         return null;
1220                 }
1221
1222                 
1223                 /// <summary>
1224                 ///   Attempts to apply the `Standard Implicit
1225                 ///   Conversion' rules to the expression `expr' into
1226                 ///   the `target_type'.  It returns a new expression
1227                 ///   that can be used in a context that expects a
1228                 ///   `target_type'.
1229                 ///
1230                 ///   This is different from `ImplicitConversion' in that the
1231                 ///   user defined implicit conversions are excluded. 
1232                 /// </summary>
1233                 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1234                                                                      Type target_type, Location loc)
1235                 {
1236                         Type expr_type = expr.Type;
1237                         Expression e;
1238
1239                         if (expr is NullLiteral) {
1240                                 if (target_type.IsGenericParameter)
1241                                         return TypeParameter_to_Null (target_type, loc);
1242
1243                                 if (TypeManager.IsNullableType (target_type))
1244                                         return new Nullable.NullableLiteral (target_type, loc);
1245                         }
1246
1247                         if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1248                                 return new Nullable.LiftedConversion (
1249                                         expr, target_type, false, false, loc).Resolve (ec);
1250
1251                         if (expr.eclass == ExprClass.MethodGroup){
1252                                 if (!TypeManager.IsDelegateType (target_type)){
1253                                         return null;
1254                                 }
1255
1256                                 //
1257                                 // Only allow anonymous method conversions on post ISO_1
1258                                 //
1259                                 if (RootContext.Version != LanguageVersion.ISO_1){
1260                                         MethodGroupExpr mg = expr as MethodGroupExpr;
1261                                         if (mg != null)
1262                                                 return ImplicitDelegateCreation.Create (
1263                                                         ec, mg, target_type, loc);
1264                                 }
1265                         }
1266
1267                         if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1268                                 return expr;
1269
1270                         e = ImplicitNumericConversion (expr, target_type);
1271                         if (e != null)
1272                                 return e;
1273
1274                         e = ImplicitReferenceConversion (expr, target_type);
1275                         if (e != null)
1276                                 return e;
1277                         
1278                         if (TypeManager.IsEnumType (target_type) && expr is IntLiteral){
1279                                 IntLiteral i = (IntLiteral) expr;
1280
1281                                 if (i.Value == 0)
1282                                         return new EnumConstant ((Constant) expr, target_type);
1283                         }
1284
1285                         if (ec.InUnsafe) {
1286                                 if (expr_type.IsPointer){
1287                                         if (target_type == TypeManager.void_ptr_type)
1288                                                 return new EmptyCast (expr, target_type);
1289
1290                                         //
1291                                         // yep, comparing pointer types cant be done with
1292                                         // t1 == t2, we have to compare their element types.
1293                                         //
1294                                         if (target_type.IsPointer){
1295                                                 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1296                                                         return expr;
1297
1298                                                 //return null;
1299                                         }
1300                                 }
1301                                 
1302                                 if (expr_type == TypeManager.null_type && target_type.IsPointer)
1303                                         return new EmptyCast (NullPointer.Null, target_type);
1304                         }
1305
1306                         if (TypeManager.IsNullableTypeOf (target_type, expr_type))
1307                                 return new Nullable.Wrap (expr, loc).Resolve (ec);
1308
1309                         if (expr_type == TypeManager.anonymous_method_type){
1310                                 if (!TypeManager.IsDelegateType (target_type)){
1311                                         Report.Error (1660, loc,
1312                                                 "Cannot convert anonymous method block to type `{0}' because it is not a delegate type",
1313                                                 TypeManager.CSharpName (target_type));
1314                                         return null;
1315                                 }
1316
1317                                 AnonymousMethod am = (AnonymousMethod) expr;
1318                                 int errors = Report.Errors;
1319
1320                                 Expression conv = am.Compatible (ec, target_type);
1321                                 if (conv != null)
1322                                         return conv;
1323                                 
1324                                 //
1325                                 // We return something instead of null, to avoid
1326                                 // the duplicate error, since am.Compatible would have
1327                                 // reported that already
1328                                 //
1329                                 if (errors != Report.Errors)
1330                                         return new EmptyCast (expr, target_type);
1331                         }
1332                         
1333                         return null;
1334                 }
1335
1336                 /// <summary>
1337                 ///   Attempts to perform an implicit constant conversion of the IntConstant
1338                 ///   into a different data type using casts (See Implicit Constant
1339                 ///   Expression Conversions)
1340                 /// </summary>
1341                 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1342                 {
1343                         int value = ic.Value;
1344
1345                         if (target_type == TypeManager.sbyte_type){
1346                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1347                                         return new SByteConstant ((sbyte) value, ic.Location);
1348                         } else if (target_type == TypeManager.byte_type){
1349                                 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1350                                         return new ByteConstant ((byte) value, ic.Location);
1351                         } else if (target_type == TypeManager.short_type){
1352                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1353                                         return new ShortConstant ((short) value, ic.Location);
1354                         } else if (target_type == TypeManager.ushort_type){
1355                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1356                                         return new UShortConstant ((ushort) value, ic.Location);
1357                         } else if (target_type == TypeManager.uint32_type){
1358                                 if (value >= 0)
1359                                         return new UIntConstant ((uint) value, ic.Location);
1360                         } else if (target_type == TypeManager.uint64_type){
1361                                 //
1362                                 // we can optimize this case: a positive int32
1363                                 // always fits on a uint64.  But we need an opcode
1364                                 // to do it.
1365                                 //
1366                                 if (value >= 0)
1367                                         return new ULongConstant ((ulong) value, ic.Location);
1368                         } else if (target_type == TypeManager.double_type)
1369                                 return new DoubleConstant ((double) value, ic.Location);
1370                         else if (target_type == TypeManager.float_type)
1371                                 return new FloatConstant ((float) value, ic.Location);
1372                         
1373                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1374                                 Type underlying = TypeManager.EnumToUnderlying (target_type);
1375                                 Constant e = (Constant) ic;
1376                                 
1377                                 //
1378                                 // Possibly, we need to create a different 0 literal before passing
1379                                 // to EnumConstant
1380                                 //
1381                                 if (underlying == TypeManager.int64_type)
1382                                         e = new LongLiteral (0, ic.Location);
1383                                 else if (underlying == TypeManager.uint64_type)
1384                                         e = new ULongLiteral (0, ic.Location);
1385
1386                                 return new EnumConstant (e, target_type);
1387                         }
1388
1389                         //
1390                         // If `target_type' is an interface and the type of `ic' implements the interface
1391                         // e.g. target_type is IComparable, IConvertible, IFormattable
1392                         //
1393                         if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1394                                 return new BoxedCast (ic, target_type);
1395
1396                         return null;
1397                 }
1398
1399                 /// <summary>
1400                 ///   Attempts to implicitly convert `source' into `target_type', using
1401                 ///   ImplicitConversion.  If there is no implicit conversion, then
1402                 ///   an error is signaled
1403                 /// </summary>
1404                 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1405                                                                      Type target_type, Location loc)
1406                 {
1407                         Expression e = ImplicitConversion (ec, source, target_type, loc);
1408                         if (e != null)
1409                                 return e;
1410
1411                         source.Error_ValueCannotBeConverted (loc, target_type, false);
1412                         return null;
1413                 }
1414
1415                 /// <summary>
1416                 ///   Performs the explicit numeric conversions
1417                 /// </summary>
1418                 public static Expression ExplicitNumericConversion (Expression expr, Type target_type)
1419                 {
1420                         Type expr_type = expr.Type;
1421                         Type real_target_type = target_type;
1422
1423                         if (expr_type == TypeManager.sbyte_type){
1424                                 //
1425                                 // From sbyte to byte, ushort, uint, ulong, char
1426                                 //
1427                                 if (real_target_type == TypeManager.byte_type)
1428                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
1429                                 if (real_target_type == TypeManager.ushort_type)
1430                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
1431                                 if (real_target_type == TypeManager.uint32_type)
1432                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
1433                                 if (real_target_type == TypeManager.uint64_type)
1434                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
1435                                 if (real_target_type == TypeManager.char_type)
1436                                         return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
1437                         } else if (expr_type == TypeManager.byte_type){
1438                                 //
1439                                 // From byte to sbyte and char
1440                                 //
1441                                 if (real_target_type == TypeManager.sbyte_type)
1442                                         return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
1443                                 if (real_target_type == TypeManager.char_type)
1444                                         return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
1445                         } else if (expr_type == TypeManager.short_type){
1446                                 //
1447                                 // From short to sbyte, byte, ushort, uint, ulong, char
1448                                 //
1449                                 if (real_target_type == TypeManager.sbyte_type)
1450                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
1451                                 if (real_target_type == TypeManager.byte_type)
1452                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
1453                                 if (real_target_type == TypeManager.ushort_type)
1454                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
1455                                 if (real_target_type == TypeManager.uint32_type)
1456                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
1457                                 if (real_target_type == TypeManager.uint64_type)
1458                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
1459                                 if (real_target_type == TypeManager.char_type)
1460                                         return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
1461                         } else if (expr_type == TypeManager.ushort_type){
1462                                 //
1463                                 // From ushort to sbyte, byte, short, char
1464                                 //
1465                                 if (real_target_type == TypeManager.sbyte_type)
1466                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
1467                                 if (real_target_type == TypeManager.byte_type)
1468                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
1469                                 if (real_target_type == TypeManager.short_type)
1470                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
1471                                 if (real_target_type == TypeManager.char_type)
1472                                         return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
1473                         } else if (expr_type == TypeManager.int32_type){
1474                                 //
1475                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1476                                 //
1477                                 if (real_target_type == TypeManager.sbyte_type)
1478                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
1479                                 if (real_target_type == TypeManager.byte_type)
1480                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
1481                                 if (real_target_type == TypeManager.short_type)
1482                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
1483                                 if (real_target_type == TypeManager.ushort_type)
1484                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
1485                                 if (real_target_type == TypeManager.uint32_type)
1486                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
1487                                 if (real_target_type == TypeManager.uint64_type)
1488                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
1489                                 if (real_target_type == TypeManager.char_type)
1490                                         return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
1491                         } else if (expr_type == TypeManager.uint32_type){
1492                                 //
1493                                 // From uint to sbyte, byte, short, ushort, int, char
1494                                 //
1495                                 if (real_target_type == TypeManager.sbyte_type)
1496                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
1497                                 if (real_target_type == TypeManager.byte_type)
1498                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
1499                                 if (real_target_type == TypeManager.short_type)
1500                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
1501                                 if (real_target_type == TypeManager.ushort_type)
1502                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
1503                                 if (real_target_type == TypeManager.int32_type)
1504                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
1505                                 if (real_target_type == TypeManager.char_type)
1506                                         return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
1507                         } else if (expr_type == TypeManager.int64_type){
1508                                 //
1509                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1510                                 //
1511                                 if (real_target_type == TypeManager.sbyte_type)
1512                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
1513                                 if (real_target_type == TypeManager.byte_type)
1514                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
1515                                 if (real_target_type == TypeManager.short_type)
1516                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
1517                                 if (real_target_type == TypeManager.ushort_type)
1518                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
1519                                 if (real_target_type == TypeManager.int32_type)
1520                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
1521                                 if (real_target_type == TypeManager.uint32_type)
1522                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
1523                                 if (real_target_type == TypeManager.uint64_type)
1524                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
1525                                 if (real_target_type == TypeManager.char_type)
1526                                         return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
1527                         } else if (expr_type == TypeManager.uint64_type){
1528                                 //
1529                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1530                                 //
1531                                 if (real_target_type == TypeManager.sbyte_type)
1532                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
1533                                 if (real_target_type == TypeManager.byte_type)
1534                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
1535                                 if (real_target_type == TypeManager.short_type)
1536                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
1537                                 if (real_target_type == TypeManager.ushort_type)
1538                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
1539                                 if (real_target_type == TypeManager.int32_type)
1540                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
1541                                 if (real_target_type == TypeManager.uint32_type)
1542                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
1543                                 if (real_target_type == TypeManager.int64_type)
1544                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
1545                                 if (real_target_type == TypeManager.char_type)
1546                                         return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
1547                         } else if (expr_type == TypeManager.char_type){
1548                                 //
1549                                 // From char to sbyte, byte, short
1550                                 //
1551                                 if (real_target_type == TypeManager.sbyte_type)
1552                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
1553                                 if (real_target_type == TypeManager.byte_type)
1554                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
1555                                 if (real_target_type == TypeManager.short_type)
1556                                         return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
1557                         } else if (expr_type == TypeManager.float_type){
1558                                 //
1559                                 // From float to sbyte, byte, short,
1560                                 // ushort, int, uint, long, ulong, char
1561                                 // or decimal
1562                                 //
1563                                 if (real_target_type == TypeManager.sbyte_type)
1564                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
1565                                 if (real_target_type == TypeManager.byte_type)
1566                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
1567                                 if (real_target_type == TypeManager.short_type)
1568                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
1569                                 if (real_target_type == TypeManager.ushort_type)
1570                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
1571                                 if (real_target_type == TypeManager.int32_type)
1572                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
1573                                 if (real_target_type == TypeManager.uint32_type)
1574                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
1575                                 if (real_target_type == TypeManager.int64_type)
1576                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
1577                                 if (real_target_type == TypeManager.uint64_type)
1578                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
1579                                 if (real_target_type == TypeManager.char_type)
1580                                         return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
1581                                 if (real_target_type == TypeManager.decimal_type)
1582                                         return new CastToDecimal (expr, true);
1583                         } else if (expr_type == TypeManager.double_type){
1584                                 //
1585                                 // From double to sbyte, byte, short,
1586                                 // ushort, int, uint, long, ulong,
1587                                 // char, float or decimal
1588                                 //
1589                                 if (real_target_type == TypeManager.sbyte_type)
1590                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
1591                                 if (real_target_type == TypeManager.byte_type)
1592                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
1593                                 if (real_target_type == TypeManager.short_type)
1594                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
1595                                 if (real_target_type == TypeManager.ushort_type)
1596                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
1597                                 if (real_target_type == TypeManager.int32_type)
1598                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
1599                                 if (real_target_type == TypeManager.uint32_type)
1600                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
1601                                 if (real_target_type == TypeManager.int64_type)
1602                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
1603                                 if (real_target_type == TypeManager.uint64_type)
1604                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
1605                                 if (real_target_type == TypeManager.char_type)
1606                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
1607                                 if (real_target_type == TypeManager.float_type)
1608                                         return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
1609                                 if (real_target_type == TypeManager.decimal_type)
1610                                         return new CastToDecimal (expr, true);
1611                         } else if (expr_type == TypeManager.decimal_type) {
1612                                 return new CastFromDecimal (expr, target_type).Resolve ();
1613                         }
1614                         return null;
1615                 }
1616
1617                 /// <summary>
1618                 ///  Returns whether an explicit reference conversion can be performed
1619                 ///  from source_type to target_type
1620                 /// </summary>
1621                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1622                 {
1623                         bool target_is_type_param = target_type.IsGenericParameter;
1624                         bool target_is_value_type = target_type.IsValueType;
1625                         
1626                         if (source_type == target_type)
1627                                 return true;
1628                         
1629                         //
1630                         // From generic parameter to any type
1631                         //
1632                         if (source_type.IsGenericParameter)
1633                                 return ExplicitTypeParameterConversionExists (source_type, target_type);
1634
1635                         //
1636                         // From object to a generic parameter
1637                         //
1638                         if (source_type == TypeManager.object_type && target_is_type_param)
1639                                 return true;
1640
1641                         //
1642                         // From object to any reference type
1643                         //
1644                         if (source_type == TypeManager.object_type && !target_is_value_type)
1645                                 return true;
1646                                         
1647                         //
1648                         // From any class S to any class-type T, provided S is a base class of T
1649                         //
1650                         if (TypeManager.IsSubclassOf (target_type, source_type))
1651                                 return true;
1652
1653                         //
1654                         // From any interface type S to any interface T provided S is not derived from T
1655                         //
1656                         if (source_type.IsInterface && target_type.IsInterface){
1657                                 if (!TypeManager.IsSubclassOf (target_type, source_type))
1658                                         return true;
1659                         }
1660                             
1661                         //
1662                         // From any class type S to any interface T, provided S is not sealed
1663                         // and provided S does not implement T.
1664                         //
1665                         if (target_type.IsInterface && !source_type.IsSealed &&
1666                             !TypeManager.ImplementsInterface (source_type, target_type))
1667                                 return true;
1668
1669                         //
1670                         // From any interface-type S to to any class type T, provided T is not
1671                         // sealed, or provided T implements S.
1672                         //
1673                         if (source_type.IsInterface &&
1674                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1675                                 return true;
1676                         
1677                         
1678                         // From an array type S with an element type Se to an array type T with an 
1679                         // element type Te provided all the following are true:
1680                         //     * S and T differe only in element type, in other words, S and T
1681                         //       have the same number of dimensions.
1682                         //     * Both Se and Te are reference types
1683                         //     * An explicit referenc conversions exist from Se to Te
1684                         //
1685                         if (source_type.IsArray && target_type.IsArray) {
1686                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1687                                         
1688                                         Type source_element_type = TypeManager.GetElementType (source_type);
1689                                         Type target_element_type = TypeManager.GetElementType (target_type);
1690
1691                                         if (source_element_type.IsGenericParameter ||
1692                                             (!source_element_type.IsValueType && !target_element_type.IsValueType))
1693                                                 if (ExplicitReferenceConversionExists (source_element_type,
1694                                                                                        target_element_type))
1695                                                         return true;
1696                                 }
1697                         }
1698                         
1699
1700                         // From System.Array to any array-type
1701                         if (source_type == TypeManager.array_type &&
1702                             target_type.IsArray){
1703                                 return true;
1704                         }
1705
1706                         //
1707                         // From System delegate to any delegate-type
1708                         //
1709                         if (source_type == TypeManager.delegate_type &&
1710                             TypeManager.IsDelegateType (target_type))
1711                                 return true;
1712
1713                         //
1714                         // From ICloneable to Array or Delegate types
1715                         //
1716                         if (source_type == TypeManager.icloneable_type &&
1717                             (target_type == TypeManager.array_type ||
1718                              target_type == TypeManager.delegate_type))
1719                                 return true;
1720                         
1721                         return false;
1722                 }
1723
1724                 /// <summary>
1725                 ///   Implements Explicit Reference conversions
1726                 /// </summary>
1727                 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1728                 {
1729                         Type source_type = source.Type;
1730                         bool target_is_type_param = target_type.IsGenericParameter;
1731                         bool target_is_value_type = target_type.IsValueType;
1732
1733                         //
1734                         // From object to a generic parameter
1735                         //
1736                         if (source_type == TypeManager.object_type && target_is_type_param)
1737                                 return new UnboxCast (source, target_type);
1738
1739                         //
1740                         // Explicit type parameter conversion.
1741                         //
1742
1743                         if (source_type.IsGenericParameter)
1744                                 return ExplicitTypeParameterConversion (source, target_type);
1745
1746                         //
1747                         // From object to any reference type
1748                         //
1749                         if (source_type == TypeManager.object_type && !target_is_value_type)
1750                                 return new ClassCast (source, target_type);
1751
1752                         //
1753                         // Unboxing conversion.
1754                         //
1755                         if (((source_type == TypeManager.enum_type &&
1756                                 !(source is EmptyCast)) ||
1757                                 source_type == TypeManager.value_type) && target_is_value_type)
1758                                 return new UnboxCast (source, target_type);
1759
1760                         //
1761                         // From any class S to any class-type T, provided S is a base class of T
1762                         //
1763                         if (TypeManager.IsSubclassOf (target_type, source_type))
1764                                 return new ClassCast (source, target_type);
1765
1766                         //
1767                         // From any interface type S to any interface T provided S is not derived from T
1768                         //
1769                         if (source_type.IsInterface && target_type.IsInterface){
1770                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1771                                         return null;
1772                                 else
1773                                         return new ClassCast (source, target_type);
1774                         }
1775
1776                         //
1777                         // From any class type S to any interface T, provides S is not sealed
1778                         // and provided S does not implement T.
1779                         //
1780                         if (target_type.IsInterface && !source_type.IsSealed) {
1781                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1782                                         return null;
1783                                 else
1784                                         return new ClassCast (source, target_type);
1785                                 
1786                         }
1787
1788                         //
1789                         // From any interface-type S to to any class type T, provided T is not
1790                         // sealed, or provided T implements S.
1791                         //
1792                         if (source_type.IsInterface) {
1793                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1794                                         if (target_type.IsClass)
1795                                                 return new ClassCast (source, target_type);
1796                                         else
1797                                                 return new UnboxCast (source, target_type);
1798                                 }
1799
1800                                 return null;
1801                         }
1802                         
1803                         // From an array type S with an element type Se to an array type T with an 
1804                         // element type Te provided all the following are true:
1805                         //     * S and T differe only in element type, in other words, S and T
1806                         //       have the same number of dimensions.
1807                         //     * Both Se and Te are reference types
1808                         //     * An explicit referenc conversions exist from Se to Te
1809                         //
1810                         if (source_type.IsArray && target_type.IsArray) {
1811                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1812                                         
1813                                         Type source_element_type = TypeManager.GetElementType (source_type);
1814                                         Type target_element_type = TypeManager.GetElementType (target_type);
1815                                         
1816                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1817                                                 if (ExplicitReferenceConversionExists (source_element_type,
1818                                                                                        target_element_type))
1819                                                         return new ClassCast (source, target_type);
1820                                 }
1821                         }
1822                         
1823
1824                         // From System.Array to any array-type
1825                         if (source_type == TypeManager.array_type &&
1826                             target_type.IsArray) {
1827                                 return new ClassCast (source, target_type);
1828                         }
1829
1830                         //
1831                         // From System delegate to any delegate-type
1832                         //
1833                         if (source_type == TypeManager.delegate_type &&
1834                             TypeManager.IsDelegateType (target_type))
1835                                 return new ClassCast (source, target_type);
1836
1837                         //
1838                         // From ICloneable to Array or Delegate types
1839                         //
1840                         if (source_type == TypeManager.icloneable_type &&
1841                             (target_type == TypeManager.array_type ||
1842                              target_type == TypeManager.delegate_type))
1843                                 return new ClassCast (source, target_type);
1844                         
1845                         return null;
1846                 }
1847                 
1848                 /// <summary>
1849                 ///   Performs an explicit conversion of the expression `expr' whose
1850                 ///   type is expr.Type to `target_type'.
1851                 /// </summary>
1852                 static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
1853                                                                  Type target_type, Location loc)
1854                 {
1855                         Type expr_type = expr.Type;
1856
1857                         // Explicit conversion includes implicit conversion and it used for enum underlying types too
1858                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1859                         if (ne != null)
1860                                 return ne;
1861
1862                         //
1863                         // Unboxing conversions; only object types can be convertible to enum
1864                         //
1865                         if (expr_type == TypeManager.object_type && target_type.IsValueType || expr_type == TypeManager.enum_type)
1866                                 return new UnboxCast (expr, target_type);
1867
1868                         if (TypeManager.IsEnumType (expr_type)) {
1869                                 if (expr is EnumConstant)
1870                                         return ExplicitConversionCore (ec, ((EnumConstant) expr).Child, target_type, loc);
1871
1872                                 return ExplicitConversionCore (ec, new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type)), target_type, loc);
1873                         }
1874
1875                         if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1876                                 return new Nullable.LiftedConversion (
1877                                         expr, target_type, false, true, loc).Resolve (ec);
1878
1879                         if (TypeManager.IsEnumType (target_type)){
1880                                 Expression ce = ExplicitConversionCore (ec, expr, TypeManager.EnumToUnderlying (target_type), loc);
1881                                 if (ce != null)
1882                                         return new EmptyCast (ce, target_type);
1883                         }
1884
1885                         ne = ExplicitNumericConversion (expr, target_type);
1886                         if (ne != null)
1887                                 return ne;
1888
1889                         //
1890                         // Skip the ExplicitReferenceConversion because we can not convert
1891                         // from Null to a ValueType, and ExplicitReference wont check against
1892                         // null literal explicitly
1893                         //
1894                         if (expr_type != TypeManager.null_type){
1895                                 ne = ExplicitReferenceConversion (expr, target_type);
1896                                 if (ne != null)
1897                                         return ne;
1898                         }
1899
1900                         if (ec.InUnsafe){
1901                                 ne = ExplicitUnsafe (expr, target_type);
1902                                 if (ne != null)
1903                                         return ne;
1904                         }
1905
1906                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1907                         if (ne != null)
1908                                 return ne;
1909
1910                         return null;
1911                 }
1912
1913                 public static Expression ExplicitUnsafe (Expression expr, Type target_type)
1914                 {
1915                         Type expr_type = expr.Type;
1916
1917                         if (target_type.IsPointer){
1918                                 if (expr_type.IsPointer)
1919                                         return new EmptyCast (expr, target_type);
1920                                         
1921                                 if (expr_type == TypeManager.sbyte_type ||
1922                                         expr_type == TypeManager.short_type ||
1923                                         expr_type == TypeManager.int32_type ||
1924                                         expr_type == TypeManager.int64_type)
1925                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
1926
1927                                 if (expr_type == TypeManager.ushort_type ||
1928                                         expr_type == TypeManager.uint32_type ||
1929                                         expr_type == TypeManager.uint64_type ||
1930                                         expr_type == TypeManager.byte_type)
1931                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1932                         }
1933                         
1934                         if (expr_type.IsPointer){
1935                                 if (target_type == TypeManager.sbyte_type)
1936                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1937                                 else if (target_type == TypeManager.byte_type)
1938                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1939                                 else if (target_type == TypeManager.short_type)
1940                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1941                                 else if (target_type == TypeManager.ushort_type)
1942                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1943                                 else if (target_type == TypeManager.int32_type)
1944                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1945                                 else if (target_type == TypeManager.uint32_type)
1946                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1947                                 else if (target_type == TypeManager.uint64_type)
1948                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1949                                 else if (target_type == TypeManager.int64_type){
1950                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1951                                 }
1952                         }
1953                         return null;
1954                 }
1955
1956                 /// <summary>
1957                 ///   Same as ExplicitConversion, only it doesn't include user defined conversions
1958                 /// </summary>
1959                 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1960                                                                      Type target_type, Location l)
1961                 {
1962                         int errors = Report.Errors;
1963                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1964                         if (Report.Errors > errors)
1965                                 return null;
1966
1967                         if (ne != null)
1968                                 return ne;
1969
1970                         if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
1971                                 return new Nullable.LiftedConversion (
1972                                         expr, target_type, false, true, l).Resolve (ec);
1973
1974                         ne = ExplicitNumericConversion (expr, target_type);
1975                         if (ne != null)
1976                                 return ne;
1977
1978                         ne = ExplicitReferenceConversion (expr, target_type);
1979                         if (ne != null)
1980                                 return ne;
1981
1982                         if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
1983                                 return new EmptyCast (expr, target_type);
1984
1985                         expr.Error_ValueCannotBeConverted (l, target_type, true);
1986                         return null;
1987                 }
1988
1989                 /// <summary>
1990                 ///   Performs an explicit conversion of the expression `expr' whose
1991                 ///   type is expr.Type to `target_type'.
1992                 /// </summary>
1993                 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1994                         Type target_type, Location loc)
1995                 {
1996                         Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
1997                         if (e != null)
1998                                 return e;
1999
2000                         expr.Error_ValueCannotBeConverted (loc, target_type, true);
2001                         return null;
2002                 }
2003         }
2004 }