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