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