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