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