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