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