Copied remotely
[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 (expr, 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                         // First numeric conversions 
476
477                         if (expr_type == TypeManager.sbyte_type){
478                                 //
479                                 // From sbyte to short, int, long, float, double.
480                                 //
481                                 if ((target_type == TypeManager.int32_type) || 
482                                     (target_type == TypeManager.int64_type) ||
483                                     (target_type == TypeManager.double_type) ||
484                                     (target_type == TypeManager.float_type)  ||
485                                     (target_type == TypeManager.short_type) ||
486                                     (target_type == TypeManager.decimal_type))
487                                         return true;
488                                 
489                         } else if (expr_type == TypeManager.byte_type){
490                                 //
491                                 // From byte to short, ushort, int, uint, long, ulong, float, double
492                                 // 
493                                 if ((target_type == TypeManager.short_type) ||
494                                     (target_type == TypeManager.ushort_type) ||
495                                     (target_type == TypeManager.int32_type) ||
496                                     (target_type == TypeManager.uint32_type) ||
497                                     (target_type == TypeManager.uint64_type) ||
498                                     (target_type == TypeManager.int64_type) ||
499                                     (target_type == TypeManager.float_type) ||
500                                     (target_type == TypeManager.double_type) ||
501                                     (target_type == TypeManager.decimal_type))
502                                         return true;
503         
504                         } else if (expr_type == TypeManager.short_type){
505                                 //
506                                 // From short to int, long, float, double
507                                 // 
508                                 if ((target_type == TypeManager.int32_type) ||
509                                     (target_type == TypeManager.int64_type) ||
510                                     (target_type == TypeManager.double_type) ||
511                                     (target_type == TypeManager.float_type) ||
512                                     (target_type == TypeManager.decimal_type))
513                                         return true;
514                                         
515                         } else if (expr_type == TypeManager.ushort_type){
516                                 //
517                                 // From ushort to int, uint, long, ulong, float, double
518                                 //
519                                 if ((target_type == TypeManager.uint32_type) ||
520                                     (target_type == TypeManager.uint64_type) ||
521                                     (target_type == TypeManager.int32_type) ||
522                                     (target_type == TypeManager.int64_type) ||
523                                     (target_type == TypeManager.double_type) ||
524                                     (target_type == TypeManager.float_type) ||
525                                     (target_type == TypeManager.decimal_type))
526                                         return true;
527                                     
528                         } else if (expr_type == TypeManager.int32_type){
529                                 //
530                                 // From int to long, float, double
531                                 //
532                                 if ((target_type == TypeManager.int64_type) ||
533                                     (target_type == TypeManager.double_type) ||
534                                     (target_type == TypeManager.float_type) ||
535                                     (target_type == TypeManager.decimal_type))
536                                         return true;
537                                         
538                         } else if (expr_type == TypeManager.uint32_type){
539                                 //
540                                 // From uint to long, ulong, float, double
541                                 //
542                                 if ((target_type == TypeManager.int64_type) ||
543                                     (target_type == TypeManager.uint64_type) ||
544                                     (target_type == TypeManager.double_type) ||
545                                     (target_type == TypeManager.float_type) ||
546                                     (target_type == TypeManager.decimal_type))
547                                         return true;
548                                         
549                         } else if ((expr_type == TypeManager.uint64_type) ||
550                                    (expr_type == TypeManager.int64_type)) {
551                                 //
552                                 // From long/ulong to float, double
553                                 //
554                                 if ((target_type == TypeManager.double_type) ||
555                                     (target_type == TypeManager.float_type) ||
556                                     (target_type == TypeManager.decimal_type))
557                                         return true;
558                                     
559                         } else if (expr_type == TypeManager.char_type){
560                                 //
561                                 // From char to ushort, int, uint, long, ulong, float, double
562                                 // 
563                                 if ((target_type == TypeManager.ushort_type) ||
564                                     (target_type == TypeManager.int32_type) ||
565                                     (target_type == TypeManager.uint32_type) ||
566                                     (target_type == TypeManager.uint64_type) ||
567                                     (target_type == TypeManager.int64_type) ||
568                                     (target_type == TypeManager.float_type) ||
569                                     (target_type == TypeManager.double_type) ||
570                                     (target_type == TypeManager.decimal_type))
571                                         return true;
572
573                         } else if (expr_type == TypeManager.float_type){
574                                 //
575                                 // float to double
576                                 //
577                                 if (target_type == TypeManager.double_type)
578                                         return true;
579                         }       
580                         
581                         if (ImplicitReferenceConversionExists (expr, target_type))
582                                 return true;
583
584                         //
585                         // Implicit Constant Expression Conversions
586                         //
587                         if (expr is IntConstant){
588                                 int value = ((IntConstant) expr).Value;
589
590                                 if (target_type == TypeManager.sbyte_type){
591                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
592                                                 return true;
593                                 } else if (target_type == TypeManager.byte_type){
594                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
595                                                 return true;
596                                 } else if (target_type == TypeManager.short_type){
597                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
598                                                 return true;
599                                 } else if (target_type == TypeManager.ushort_type){
600                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
601                                                 return true;
602                                 } else if (target_type == TypeManager.uint32_type){
603                                         if (value >= 0)
604                                                 return true;
605                                 } else if (target_type == TypeManager.uint64_type){
606                                          //
607                                          // we can optimize this case: a positive int32
608                                          // always fits on a uint64.  But we need an opcode
609                                          // to do it.
610                                          //
611                                         if (value >= 0)
612                                                 return true;
613                                 }
614                                 
615                                 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
616                                         return true;
617                         }
618
619                         if (expr is LongConstant && target_type == TypeManager.uint64_type){
620                                 //
621                                 // Try the implicit constant expression conversion
622                                 // from long to ulong, instead of a nice routine,
623                                 // we just inline it
624                                 //
625                                 long v = ((LongConstant) expr).Value;
626                                 if (v > 0)
627                                         return true;
628                         }
629                         
630                         if ((target_type == TypeManager.enum_type ||
631                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
632                              expr is IntLiteral){
633                                 IntLiteral i = (IntLiteral) expr;
634
635                                 if (i.Value == 0)
636                                         return true;
637                         }
638
639                         if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
640                                 return true;
641
642                         if (expr_type == TypeManager.anonymous_method_type){
643                                 if (!TypeManager.IsDelegateType (target_type))
644                                         return false;
645
646                                 AnonymousMethod am = (AnonymousMethod) expr;
647
648                                 Expression conv = am.Compatible (ec, target_type, true);
649                                 if (conv != null)
650                                         return true;
651                         }
652                         
653                         return false;
654                 }
655
656                 //
657                 // Used internally by FindMostEncompassedType, this is used
658                 // to avoid creating lots of objects in the tight loop inside
659                 // FindMostEncompassedType
660                 //
661                 static EmptyExpression priv_fmet_param;
662                 
663                 /// <summary>
664                 ///  Finds "most encompassed type" according to the spec (13.4.2)
665                 ///  amongst the methods in the MethodGroupExpr
666                 /// </summary>
667                 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
668                 {
669                         Type best = null;
670
671                         if (priv_fmet_param == null)
672                                 priv_fmet_param = new EmptyExpression ();
673
674                         foreach (Type t in types){
675                                 priv_fmet_param.SetType (t);
676                                 
677                                 if (best == null) {
678                                         best = t;
679                                         continue;
680                                 }
681                                 
682                                 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
683                                         best = t;
684                         }
685
686                         return best;
687                 }
688
689                 //
690                 // Used internally by FindMostEncompassingType, this is used
691                 // to avoid creating lots of objects in the tight loop inside
692                 // FindMostEncompassingType
693                 //
694                 static EmptyExpression priv_fmee_ret;
695                 
696                 /// <summary>
697                 ///  Finds "most encompassing type" according to the spec (13.4.2)
698                 ///  amongst the types in the given set
699                 /// </summary>
700                 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
701                 {
702                         Type best = null;
703
704                         if (priv_fmee_ret == null)
705                                 priv_fmee_ret = new EmptyExpression ();
706
707                         foreach (Type t in types){
708                                 priv_fmee_ret.SetType (best);
709
710                                 if (best == null) {
711                                         best = t;
712                                         continue;
713                                 }
714
715                                 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
716                                         best = t;
717                         }
718                         
719                         return best;
720                 }
721
722                 //
723                 // Used to avoid creating too many objects
724                 //
725                 static EmptyExpression priv_fms_expr;
726                 
727                 /// <summary>
728                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
729                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
730                 ///   for explicit and implicit conversion operators.
731                 /// </summary>
732                 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
733                                                            Expression source, bool apply_explicit_conv_rules,
734                                                            Location loc)
735                 {
736                         ArrayList src_types_set = new ArrayList ();
737                         
738                         if (priv_fms_expr == null)
739                                 priv_fms_expr = new EmptyExpression ();
740
741                         //
742                         // If any operator converts from S then Sx = S
743                         //
744                         Type source_type = source.Type;
745                         foreach (MethodBase mb in me.Methods){
746                                 ParameterData pd = Invocation.GetParameterData (mb);
747                                 Type param_type = pd.ParameterType (0);
748
749                                 if (param_type == source_type)
750                                         return param_type;
751
752                                 if (apply_explicit_conv_rules) {
753                                         //
754                                         // From the spec :
755                                         // Find the set of applicable user-defined conversion operators, U.  This set
756                                         // consists of the
757                                         // user-defined implicit or explicit conversion operators declared by
758                                         // the classes or structs in D that convert from a type encompassing
759                                         // or encompassed by S to a type encompassing or encompassed by T
760                                         //
761                                         priv_fms_expr.SetType (param_type);
762                                         if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
763                                                 src_types_set.Add (param_type);
764                                         else {
765                                                 if (ImplicitStandardConversionExists (ec, source, param_type))
766                                                         src_types_set.Add (param_type);
767                                         }
768                                 } else {
769                                         //
770                                         // Only if S is encompassed by param_type
771                                         //
772                                         if (ImplicitStandardConversionExists (ec, source, param_type))
773                                                 src_types_set.Add (param_type);
774                                 }
775                         }
776                         
777                         //
778                         // Explicit Conv rules
779                         //
780                         if (apply_explicit_conv_rules) {
781                                 ArrayList candidate_set = new ArrayList ();
782
783                                 foreach (Type param_type in src_types_set){
784                                         if (ImplicitStandardConversionExists (ec, source, param_type))
785                                                 candidate_set.Add (param_type);
786                                 }
787
788                                 if (candidate_set.Count != 0)
789                                         return FindMostEncompassedType (ec, candidate_set);
790                         }
791
792                         //
793                         // Final case
794                         //
795                         if (apply_explicit_conv_rules)
796                                 return FindMostEncompassingType (ec, src_types_set);
797                         else
798                                 return FindMostEncompassedType (ec, src_types_set);
799                 }
800
801                 //
802                 // Useful in avoiding proliferation of objects
803                 //
804                 static EmptyExpression priv_fmt_expr;
805                 
806                 /// <summary>
807                 ///  Finds the most specific target Tx according to section 13.4.4
808                 /// </summary>
809                 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
810                                                            Type target, bool apply_explicit_conv_rules,
811                                                            Location loc)
812                 {
813                         ArrayList tgt_types_set = new ArrayList ();
814                         
815                         if (priv_fmt_expr == null)
816                                 priv_fmt_expr = new EmptyExpression ();
817                         
818                         //
819                         // If any operator converts to T then Tx = T
820                         //
821                         foreach (MethodInfo mi in me.Methods){
822                                 Type ret_type = mi.ReturnType;
823
824                                 if (ret_type == target)
825                                         return ret_type;
826
827                                 if (apply_explicit_conv_rules) {
828                                         //
829                                         // From the spec :
830                                         // Find the set of applicable user-defined conversion operators, U.
831                                         //
832                                         // This set consists of the
833                                         // user-defined implicit or explicit conversion operators declared by
834                                         // the classes or structs in D that convert from a type encompassing
835                                         // or encompassed by S to a type encompassing or encompassed by T
836                                         //
837                                         priv_fms_expr.SetType (ret_type);
838                                         if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
839                                                 tgt_types_set.Add (ret_type);
840                                         else {
841                                                 priv_fms_expr.SetType (target);
842                                                 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
843                                                         tgt_types_set.Add (ret_type);
844                                         }
845                                 } else {
846                                         //
847                                         // Only if T is encompassed by param_type
848                                         //
849                                         priv_fms_expr.SetType (ret_type);
850                                         if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
851                                                 tgt_types_set.Add (ret_type);
852                                 }
853                         }
854
855                         //
856                         // Explicit conv rules
857                         //
858                         if (apply_explicit_conv_rules) {
859                                 ArrayList candidate_set = new ArrayList ();
860
861                                 foreach (Type ret_type in tgt_types_set){
862                                         priv_fmt_expr.SetType (ret_type);
863                                         
864                                         if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
865                                                 candidate_set.Add (ret_type);
866                                 }
867
868                                 if (candidate_set.Count != 0)
869                                         return FindMostEncompassingType (ec, candidate_set);
870                         }
871                         
872                         //
873                         // Okay, final case !
874                         //
875                         if (apply_explicit_conv_rules)
876                                 return FindMostEncompassedType (ec, tgt_types_set);
877                         else 
878                                 return FindMostEncompassingType (ec, tgt_types_set);
879                 }
880                 
881                 /// <summary>
882                 ///  User-defined Implicit conversions
883                 /// </summary>
884                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
885                                                                  Type target, Location loc)
886                 {
887                         return UserDefinedConversion (ec, source, target, loc, false);
888                 }
889
890                 /// <summary>
891                 ///  User-defined Explicit conversions
892                 /// </summary>
893                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
894                                                                  Type target, Location loc)
895                 {
896                         return UserDefinedConversion (ec, source, target, loc, true);
897                 }
898
899                 static DoubleHash explicit_conv = new DoubleHash (100);
900                 static DoubleHash implicit_conv = new DoubleHash (100);
901                 /// <summary>
902                 ///   Computes the MethodGroup for the user-defined conversion
903                 ///   operators from source_type to target_type.  `look_for_explicit'
904                 ///   controls whether we should also include the list of explicit
905                 ///   operators
906                 /// </summary>
907                 static MethodGroupExpr GetConversionOperators (EmitContext ec,
908                                                                Type source_type, Type target_type,
909                                                                Location loc, bool look_for_explicit)
910                 {
911                         Expression mg1 = null, mg2 = null;
912                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
913                         string op_name;
914
915                         op_name = "op_Implicit";
916
917                         MethodGroupExpr union3;
918                         object r;
919                         if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
920                                 return (MethodGroupExpr) r;
921                         
922                         mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
923                         if (source_type.BaseType != null)
924                                 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
925
926                         if (mg1 == null)
927                                 union3 = (MethodGroupExpr) mg2;
928                         else if (mg2 == null)
929                                 union3 = (MethodGroupExpr) mg1;
930                         else
931                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
932
933                         mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
934                         if (mg1 != null){
935                                 if (union3 != null)
936                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
937                                 else
938                                         union3 = (MethodGroupExpr) mg1;
939                         }
940
941                         if (target_type.BaseType != null)
942                                 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
943                         
944                         if (mg1 != null){
945                                 if (union3 != null)
946                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
947                                 else
948                                         union3 = (MethodGroupExpr) mg1;
949                         }
950
951                         MethodGroupExpr union4 = null;
952
953                         if (look_for_explicit) {
954                                 op_name = "op_Explicit";
955
956                                 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
957                                 if (source_type.BaseType != null)
958                                         mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
959                                 
960                                 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
961                                 if (target_type.BaseType != null)
962                                         mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
963                                 
964                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
965                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
966
967                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
968                         }
969                         
970                         MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
971                         (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
972                         return ret;
973                 }
974                 
975                 /// <summary>
976                 ///   User-defined conversions
977                 /// </summary>
978                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
979                                                                 Type target, Location loc,
980                                                                 bool look_for_explicit)
981                 {
982                         MethodGroupExpr union;
983                         Type source_type = source.Type;
984                         MethodBase method = null;
985
986                         union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
987                         if (union == null)
988                                 return null;
989                         
990                         Type most_specific_source, most_specific_target;
991
992                         most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
993                         if (most_specific_source == null)
994                                 return null;
995
996                         most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
997                         if (most_specific_target == null) 
998                                 return null;
999
1000                         int count = 0;
1001
1002                         
1003                         foreach (MethodBase mb in union.Methods){
1004                                 ParameterData pd = Invocation.GetParameterData (mb);
1005                                 MethodInfo mi = (MethodInfo) mb;
1006                                 
1007                                 if (pd.ParameterType (0) == most_specific_source &&
1008                                     mi.ReturnType == most_specific_target) {
1009                                         method = mb;
1010                                         count++;
1011                                 }
1012                         }
1013                         
1014                         if (method == null || count > 1)
1015                                 return null;
1016                         
1017                         
1018                         //
1019                         // This will do the conversion to the best match that we
1020                         // found.  Now we need to perform an implict standard conversion
1021                         // if the best match was not the type that we were requested
1022                         // by target.
1023                         //
1024                         if (look_for_explicit)
1025                                 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1026                         else
1027                                 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1028
1029                         if (source == null)
1030                                 return null;
1031
1032                         Expression e;
1033                         e =  new UserCast ((MethodInfo) method, source, loc);
1034                         if (e.Type != target){
1035                                 if (!look_for_explicit)
1036                                         e = ImplicitConversionStandard (ec, e, target, loc);
1037                                 else
1038                                         e = ExplicitConversionStandard (ec, e, target, loc);
1039                         }
1040
1041                         return e;
1042                 }
1043                 
1044                 /// <summary>
1045                 ///   Converts implicitly the resolved expression `expr' into the
1046                 ///   `target_type'.  It returns a new expression that can be used
1047                 ///   in a context that expects a `target_type'. 
1048                 /// </summary>
1049                 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1050                                                              Type target_type, Location loc)
1051                 {
1052                         Expression e;
1053
1054                         if (target_type == null)
1055                                 throw new Exception ("Target type is null");
1056
1057                         e = ImplicitConversionStandard (ec, expr, target_type, loc);
1058                         if (e != null)
1059                                 return e;
1060
1061                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1062                         if (e != null)
1063                                 return e;
1064
1065                         return null;
1066                 }
1067
1068                 
1069                 /// <summary>
1070                 ///   Attempts to apply the `Standard Implicit
1071                 ///   Conversion' rules to the expression `expr' into
1072                 ///   the `target_type'.  It returns a new expression
1073                 ///   that can be used in a context that expects a
1074                 ///   `target_type'.
1075                 ///
1076                 ///   This is different from `ImplicitConversion' in that the
1077                 ///   user defined implicit conversions are excluded. 
1078                 /// </summary>
1079                 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1080                                                                      Type target_type, Location loc)
1081                 {
1082                         Type expr_type = expr.Type;
1083                         Expression e;
1084
1085                         if (expr.eclass == ExprClass.MethodGroup){
1086                                 if (!TypeManager.IsDelegateType (target_type)){
1087                                         return null;
1088                                 }
1089                                 
1090                                 //
1091                                 // Only allow anonymous method conversions on post ISO_1
1092                                 //
1093                                 if (RootContext.Version != LanguageVersion.ISO_1){
1094                                         MethodGroupExpr mg = expr as MethodGroupExpr;
1095                                         if (mg != null)
1096                                                 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1097                                 }
1098                         }
1099
1100                         if (expr_type == target_type && expr_type != TypeManager.null_type)
1101                                 return expr;
1102
1103                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1104                         if (e != null)
1105                                 return e;
1106
1107                         e = ImplicitReferenceConversion (expr, target_type);
1108                         if (e != null)
1109                                 return e;
1110                         
1111                         if ((target_type == TypeManager.enum_type ||
1112                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
1113                             expr is IntLiteral){
1114                                 IntLiteral i = (IntLiteral) expr;
1115                                 
1116                                 if (i.Value == 0)
1117                                         return new EnumConstant ((Constant) expr, target_type);
1118                         }
1119
1120                         if (ec.InUnsafe) {
1121                                 if (expr_type.IsPointer){
1122                                         if (target_type == TypeManager.void_ptr_type)
1123                                                 return new EmptyCast (expr, target_type);
1124
1125                                         //
1126                                         // yep, comparing pointer types cant be done with
1127                                         // t1 == t2, we have to compare their element types.
1128                                         //
1129                                         if (target_type.IsPointer){
1130                                                 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1131                                                         return expr;
1132                                         }
1133                                 }
1134                                 
1135                                 if (target_type.IsPointer) {
1136                                         if (expr_type == TypeManager.null_type)
1137                                                 return new EmptyCast (expr, target_type);
1138
1139                                         if (expr_type == TypeManager.void_ptr_type)
1140                                                 return new EmptyCast (expr, target_type);
1141                                 }
1142                         }
1143
1144                         if (expr_type == TypeManager.anonymous_method_type){
1145                                 if (!TypeManager.IsDelegateType (target_type)){
1146                                         Report.Error (1660, loc,
1147                                                               "Cannot convert anonymous method to `{0}', since it is not a delegate",
1148                                                               TypeManager.CSharpName (target_type));
1149                                         return null;
1150                                 }
1151
1152                                 AnonymousMethod am = (AnonymousMethod) expr;
1153                                 int errors = Report.Errors;
1154
1155                                 Expression conv = am.Compatible (ec, target_type, false);
1156                                 if (conv != null)
1157                                         return conv;
1158                                 
1159                                 //
1160                                 // We return something instead of null, to avoid
1161                                 // the duplicate error, since am.Compatible would have
1162                                 // reported that already
1163                                 //
1164                                 if (errors != Report.Errors)
1165                                         return new EmptyCast (expr, target_type);
1166                         }
1167                         
1168                         return null;
1169                 }
1170
1171                 /// <summary>
1172                 ///   Attemps to perform an implict constant conversion of the IntConstant
1173                 ///   into a different data type using casts (See Implicit Constant
1174                 ///   Expression Conversions)
1175                 /// </summary>
1176                 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1177                 {
1178                         int value = ic.Value;
1179
1180                         if (target_type == TypeManager.sbyte_type){
1181                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1182                                         return new SByteConstant ((sbyte) value);
1183                         } else if (target_type == TypeManager.byte_type){
1184                                 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1185                                         return new ByteConstant ((byte) value);
1186                         } else if (target_type == TypeManager.short_type){
1187                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1188                                         return new ShortConstant ((short) value);
1189                         } else if (target_type == TypeManager.ushort_type){
1190                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1191                                         return new UShortConstant ((ushort) value);
1192                         } else if (target_type == TypeManager.uint32_type){
1193                                 if (value >= 0)
1194                                         return new UIntConstant ((uint) value);
1195                         } else if (target_type == TypeManager.uint64_type){
1196                                 //
1197                                 // we can optimize this case: a positive int32
1198                                 // always fits on a uint64.  But we need an opcode
1199                                 // to do it.
1200                                 //
1201                                 if (value >= 0)
1202                                         return new ULongConstant ((ulong) value);
1203                         } else if (target_type == TypeManager.double_type)
1204                                 return new DoubleConstant ((double) value);
1205                         else if (target_type == TypeManager.float_type)
1206                                 return new FloatConstant ((float) value);
1207                         
1208                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1209                                 Type underlying = TypeManager.EnumToUnderlying (target_type);
1210                                 Constant e = (Constant) ic;
1211                                 
1212                                 //
1213                                 // Possibly, we need to create a different 0 literal before passing
1214                                 // to EnumConstant
1215                                 //
1216                                 if (underlying == TypeManager.int64_type)
1217                                         e = new LongLiteral (0);
1218                                 else if (underlying == TypeManager.uint64_type)
1219                                         e = new ULongLiteral (0);
1220
1221                                 return new EnumConstant (e, target_type);
1222                         }
1223                         return null;
1224                 }
1225
1226                 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1227                 {
1228                         if (source.Name == target.Name){
1229                                 Report.ExtraInformation (loc,
1230                                          String.Format (
1231                                                 "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
1232                                                 source.Assembly.FullName, target.Assembly.FullName));
1233                                                          
1234                         }
1235                         Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1236                                       source == TypeManager.anonymous_method_type ?
1237                                       "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1238                                       TypeManager.CSharpName (target));
1239                 }
1240
1241                 /// <summary>
1242                 ///   Attemptes to implicityly convert `target' into `type', using
1243                 ///   ImplicitConversion.  If there is no implicit conversion, then
1244                 ///   an error is signaled
1245                 /// </summary>
1246                 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1247                                                                      Type target_type, Location loc)
1248                 {
1249                         Expression e;
1250                         
1251                         e = ImplicitConversion (ec, source, target_type, loc);
1252                         if (e != null)
1253                                 return e;
1254
1255                         if (source is DoubleLiteral) {
1256                                 if (target_type == TypeManager.float_type) {
1257                                         Error_664 (loc, "float", "f");
1258                                         return null;
1259                                 }
1260                                 if (target_type == TypeManager.decimal_type) {
1261                                         Error_664 (loc, "decimal", "m");
1262                                         return null;
1263                                 }
1264                         }
1265
1266                         if (source is Constant){
1267                                 Constant c = (Constant) source;
1268
1269                                 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1270                                 return null;
1271                         }
1272                         
1273                         Error_CannotImplicitConversion (loc, source.Type, target_type);
1274
1275                         return null;
1276                 }
1277
1278                 static void Error_664 (Location loc, string type, string suffix) {
1279                         Report.Error (664, loc,
1280                                 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1281                                 type, suffix);
1282                 }
1283
1284                 /// <summary>
1285                 ///   Performs the explicit numeric conversions
1286                 /// </summary>
1287                 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1288                 {
1289                         Type expr_type = expr.Type;
1290
1291                         //
1292                         // If we have an enumeration, extract the underlying type,
1293                         // use this during the comparison, but wrap around the original
1294                         // target_type
1295                         //
1296                         Type real_target_type = target_type;
1297
1298                         if (TypeManager.IsEnumType (real_target_type))
1299                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1300
1301                         if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1302                                 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1303
1304                                 if (real_target_type != target_type)
1305                                         return new EmptyCast (ce, target_type);
1306                                 return ce;
1307                         }
1308                         
1309                         if (expr_type == TypeManager.sbyte_type){
1310                                 //
1311                                 // From sbyte to byte, ushort, uint, ulong, char
1312                                 //
1313                                 if (real_target_type == TypeManager.byte_type)
1314                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1315                                 if (real_target_type == TypeManager.ushort_type)
1316                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1317                                 if (real_target_type == TypeManager.uint32_type)
1318                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1319                                 if (real_target_type == TypeManager.uint64_type)
1320                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1321                                 if (real_target_type == TypeManager.char_type)
1322                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1323                         } else if (expr_type == TypeManager.byte_type){
1324                                 //
1325                                 // From byte to sbyte and char
1326                                 //
1327                                 if (real_target_type == TypeManager.sbyte_type)
1328                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1329                                 if (real_target_type == TypeManager.char_type)
1330                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1331                         } else if (expr_type == TypeManager.short_type){
1332                                 //
1333                                 // From short to sbyte, byte, ushort, uint, ulong, char
1334                                 //
1335                                 if (real_target_type == TypeManager.sbyte_type)
1336                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1337                                 if (real_target_type == TypeManager.byte_type)
1338                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1339                                 if (real_target_type == TypeManager.ushort_type)
1340                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1341                                 if (real_target_type == TypeManager.uint32_type)
1342                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1343                                 if (real_target_type == TypeManager.uint64_type)
1344                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1345                                 if (real_target_type == TypeManager.char_type)
1346                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1347                         } else if (expr_type == TypeManager.ushort_type){
1348                                 //
1349                                 // From ushort to sbyte, byte, short, char
1350                                 //
1351                                 if (real_target_type == TypeManager.sbyte_type)
1352                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1353                                 if (real_target_type == TypeManager.byte_type)
1354                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1355                                 if (real_target_type == TypeManager.short_type)
1356                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1357                                 if (real_target_type == TypeManager.char_type)
1358                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1359                         } else if (expr_type == TypeManager.int32_type){
1360                                 //
1361                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1362                                 //
1363                                 if (real_target_type == TypeManager.sbyte_type)
1364                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1365                                 if (real_target_type == TypeManager.byte_type)
1366                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1367                                 if (real_target_type == TypeManager.short_type)
1368                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1369                                 if (real_target_type == TypeManager.ushort_type)
1370                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1371                                 if (real_target_type == TypeManager.uint32_type)
1372                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1373                                 if (real_target_type == TypeManager.uint64_type)
1374                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1375                                 if (real_target_type == TypeManager.char_type)
1376                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1377                         } else if (expr_type == TypeManager.uint32_type){
1378                                 //
1379                                 // From uint to sbyte, byte, short, ushort, int, char
1380                                 //
1381                                 if (real_target_type == TypeManager.sbyte_type)
1382                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1383                                 if (real_target_type == TypeManager.byte_type)
1384                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1385                                 if (real_target_type == TypeManager.short_type)
1386                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1387                                 if (real_target_type == TypeManager.ushort_type)
1388                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1389                                 if (real_target_type == TypeManager.int32_type)
1390                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1391                                 if (real_target_type == TypeManager.char_type)
1392                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1393                         } else if (expr_type == TypeManager.int64_type){
1394                                 //
1395                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1396                                 //
1397                                 if (real_target_type == TypeManager.sbyte_type)
1398                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1399                                 if (real_target_type == TypeManager.byte_type)
1400                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1401                                 if (real_target_type == TypeManager.short_type)
1402                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1403                                 if (real_target_type == TypeManager.ushort_type)
1404                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1405                                 if (real_target_type == TypeManager.int32_type)
1406                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1407                                 if (real_target_type == TypeManager.uint32_type)
1408                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1409                                 if (real_target_type == TypeManager.uint64_type)
1410                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1411                                 if (real_target_type == TypeManager.char_type)
1412                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1413                         } else if (expr_type == TypeManager.uint64_type){
1414                                 //
1415                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1416                                 //
1417                                 if (real_target_type == TypeManager.sbyte_type)
1418                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1419                                 if (real_target_type == TypeManager.byte_type)
1420                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1421                                 if (real_target_type == TypeManager.short_type)
1422                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1423                                 if (real_target_type == TypeManager.ushort_type)
1424                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1425                                 if (real_target_type == TypeManager.int32_type)
1426                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1427                                 if (real_target_type == TypeManager.uint32_type)
1428                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1429                                 if (real_target_type == TypeManager.int64_type)
1430                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1431                                 if (real_target_type == TypeManager.char_type)
1432                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1433                         } else if (expr_type == TypeManager.char_type){
1434                                 //
1435                                 // From char to sbyte, byte, short
1436                                 //
1437                                 if (real_target_type == TypeManager.sbyte_type)
1438                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1439                                 if (real_target_type == TypeManager.byte_type)
1440                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1441                                 if (real_target_type == TypeManager.short_type)
1442                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1443                         } else if (expr_type == TypeManager.float_type){
1444                                 //
1445                                 // From float to sbyte, byte, short,
1446                                 // ushort, int, uint, long, ulong, char
1447                                 // or decimal
1448                                 //
1449                                 if (real_target_type == TypeManager.sbyte_type)
1450                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1451                                 if (real_target_type == TypeManager.byte_type)
1452                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1453                                 if (real_target_type == TypeManager.short_type)
1454                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1455                                 if (real_target_type == TypeManager.ushort_type)
1456                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1457                                 if (real_target_type == TypeManager.int32_type)
1458                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1459                                 if (real_target_type == TypeManager.uint32_type)
1460                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1461                                 if (real_target_type == TypeManager.int64_type)
1462                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1463                                 if (real_target_type == TypeManager.uint64_type)
1464                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1465                                 if (real_target_type == TypeManager.char_type)
1466                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1467                         } else if (expr_type == TypeManager.double_type){
1468                                 //
1469                                 // From double to byte, byte, short,
1470                                 // ushort, int, uint, long, ulong,
1471                                 // char, float or decimal
1472                                 //
1473                                 if (real_target_type == TypeManager.sbyte_type)
1474                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1475                                 if (real_target_type == TypeManager.byte_type)
1476                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1477                                 if (real_target_type == TypeManager.short_type)
1478                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1479                                 if (real_target_type == TypeManager.ushort_type)
1480                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1481                                 if (real_target_type == TypeManager.int32_type)
1482                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1483                                 if (real_target_type == TypeManager.uint32_type)
1484                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1485                                 if (real_target_type == TypeManager.int64_type)
1486                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1487                                 if (real_target_type == TypeManager.uint64_type)
1488                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1489                                 if (real_target_type == TypeManager.char_type)
1490                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1491                                 if (real_target_type == TypeManager.float_type)
1492                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1493                         } 
1494
1495                         // decimal is taken care of by the op_Explicit methods.
1496
1497                         return null;
1498                 }
1499
1500                 /// <summary>
1501                 ///  Returns whether an explicit reference conversion can be performed
1502                 ///  from source_type to target_type
1503                 /// </summary>
1504                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1505                 {
1506                         bool target_is_value_type = target_type.IsValueType;
1507                         
1508                         if (source_type == target_type)
1509                                 return true;
1510                         
1511                         //
1512                         // From object to any reference type
1513                         //
1514                         if (source_type == TypeManager.object_type && !target_is_value_type)
1515                                 return true;
1516                                         
1517                         //
1518                         // From any class S to any class-type T, provided S is a base class of T
1519                         //
1520                         if (target_type.IsSubclassOf (source_type))
1521                                 return true;
1522
1523                         //
1524                         // From any interface type S to any interface T provided S is not derived from T
1525                         //
1526                         if (source_type.IsInterface && target_type.IsInterface){
1527                                 if (!target_type.IsSubclassOf (source_type))
1528                                         return true;
1529                         }
1530                             
1531                         //
1532                         // From any class type S to any interface T, provided S is not sealed
1533                         // and provided S does not implement T.
1534                         //
1535                         if (target_type.IsInterface && !source_type.IsSealed &&
1536                             !TypeManager.ImplementsInterface (source_type, target_type))
1537                                 return true;
1538
1539                         //
1540                         // From any interface-type S to to any class type T, provided T is not
1541                         // sealed, or provided T implements S.
1542                         //
1543                         if (source_type.IsInterface &&
1544                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1545                                 return true;
1546                         
1547                         
1548                         // From an array type S with an element type Se to an array type T with an 
1549                         // element type Te provided all the following are true:
1550                         //     * S and T differe only in element type, in other words, S and T
1551                         //       have the same number of dimensions.
1552                         //     * Both Se and Te are reference types
1553                         //     * An explicit referenc conversions exist from Se to Te
1554                         //
1555                         if (source_type.IsArray && target_type.IsArray) {
1556                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1557                                         
1558                                         Type source_element_type = TypeManager.GetElementType (source_type);
1559                                         Type target_element_type = TypeManager.GetElementType (target_type);
1560                                         
1561                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1562                                                 if (ExplicitReferenceConversionExists (source_element_type,
1563                                                                                        target_element_type))
1564                                                         return true;
1565                                 }
1566                         }
1567                         
1568
1569                         // From System.Array to any array-type
1570                         if (source_type == TypeManager.array_type &&
1571                             target_type.IsArray){
1572                                 return true;
1573                         }
1574
1575                         //
1576                         // From System delegate to any delegate-type
1577                         //
1578                         if (source_type == TypeManager.delegate_type &&
1579                             target_type.IsSubclassOf (TypeManager.delegate_type))
1580                                 return true;
1581
1582                         //
1583                         // From ICloneable to Array or Delegate types
1584                         //
1585                         if (source_type == TypeManager.icloneable_type &&
1586                             (target_type == TypeManager.array_type ||
1587                              target_type == TypeManager.delegate_type))
1588                                 return true;
1589                         
1590                         return false;
1591                 }
1592
1593                 /// <summary>
1594                 ///   Implements Explicit Reference conversions
1595                 /// </summary>
1596                 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1597                 {
1598                         Type source_type = source.Type;
1599                         bool target_is_value_type = target_type.IsValueType;
1600
1601                         //
1602                         // From object to any reference type
1603                         //
1604                         if (source_type == TypeManager.object_type && !target_is_value_type)
1605                                 return new ClassCast (source, target_type);
1606
1607                         //
1608                         // Unboxing conversion.
1609                         //
1610                         if (((source_type == TypeManager.enum_type &&
1611                                 !(source is EmptyCast)) ||
1612                                 source_type == TypeManager.value_type) && target_is_value_type)
1613                                 return new UnboxCast (source, target_type);
1614
1615                         //
1616                         // From any class S to any class-type T, provided S is a base class of T
1617                         //
1618                         if (target_type.IsSubclassOf (source_type))
1619                                 return new ClassCast (source, target_type);
1620
1621                         //
1622                         // From any interface type S to any interface T provided S is not derived from T
1623                         //
1624                         if (source_type.IsInterface && target_type.IsInterface){
1625                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1626                                         return null;
1627                                 else
1628                                         return new ClassCast (source, target_type);
1629                         }
1630                             
1631                         //
1632                         // From any class type S to any interface T, provides S is not sealed
1633                         // and provided S does not implement T.
1634                         //
1635                         if (target_type.IsInterface && !source_type.IsSealed) {
1636                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1637                                         return null;
1638                                 else
1639                                         return new ClassCast (source, target_type);
1640                                 
1641                         }
1642
1643                         //
1644                         // From any interface-type S to to any class type T, provided T is not
1645                         // sealed, or provided T implements S.
1646                         //
1647                         if (source_type.IsInterface) {
1648                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1649                                         if (target_type.IsClass)
1650                                                 return new ClassCast (source, target_type);
1651                                         else
1652                                                 return new UnboxCast (source, target_type);
1653                                 }
1654
1655                                 return null;
1656                         }
1657                         
1658                         // From an array type S with an element type Se to an array type T with an 
1659                         // element type Te provided all the following are true:
1660                         //     * S and T differe only in element type, in other words, S and T
1661                         //       have the same number of dimensions.
1662                         //     * Both Se and Te are reference types
1663                         //     * An explicit referenc conversions exist from Se to Te
1664                         //
1665                         if (source_type.IsArray && target_type.IsArray) {
1666                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1667                                         
1668                                         Type source_element_type = TypeManager.GetElementType (source_type);
1669                                         Type target_element_type = TypeManager.GetElementType (target_type);
1670                                         
1671                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1672                                                 if (ExplicitReferenceConversionExists (source_element_type,
1673                                                                                        target_element_type))
1674                                                         return new ClassCast (source, target_type);
1675                                 }
1676                         }
1677                         
1678
1679                         // From System.Array to any array-type
1680                         if (source_type == TypeManager.array_type &&
1681                             target_type.IsArray) {
1682                                 return new ClassCast (source, target_type);
1683                         }
1684
1685                         //
1686                         // From System delegate to any delegate-type
1687                         //
1688                         if (source_type == TypeManager.delegate_type &&
1689                             target_type.IsSubclassOf (TypeManager.delegate_type))
1690                                 return new ClassCast (source, target_type);
1691
1692                         //
1693                         // From ICloneable to Array or Delegate types
1694                         //
1695                         if (source_type == TypeManager.icloneable_type &&
1696                             (target_type == TypeManager.array_type ||
1697                              target_type == TypeManager.delegate_type))
1698                                 return new ClassCast (source, target_type);
1699                         
1700                         return null;
1701                 }
1702                 
1703                 /// <summary>
1704                 ///   Performs an explicit conversion of the expression `expr' whose
1705                 ///   type is expr.Type to `target_type'.
1706                 /// </summary>
1707                 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1708                                                              Type target_type, Location loc)
1709                 {
1710                         Type expr_type = expr.Type;
1711                         Type original_expr_type = expr_type;
1712
1713                         if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1714                                 if (target_type == TypeManager.enum_type ||
1715                                     target_type == TypeManager.object_type) {
1716                                         if (expr is EnumConstant)
1717                                                 expr = ((EnumConstant) expr).Child;
1718                                         // We really need all these casts here .... :-(
1719                                         expr = new BoxedCast (new EmptyCast (expr, expr_type));
1720                                         return new EmptyCast (expr, target_type);
1721                                 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1722                                            target_type.IsSubclassOf (TypeManager.enum_type))
1723                                         return new UnboxCast (expr, target_type);
1724
1725                                 //
1726                                 // Notice that we have kept the expr_type unmodified, which is only
1727                                 // used later on to 
1728                                 if (expr is EnumConstant)
1729                                         expr = ((EnumConstant) expr).Child;
1730                                 else
1731                                         expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1732                                 expr_type = expr.Type;
1733                         }
1734
1735                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1736
1737                         if (ne != null)
1738                                 return ne;
1739
1740                         ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1741                         if (ne != null)
1742                                 return ne;
1743
1744                         //
1745                         // Unboxing conversion.
1746                         //
1747                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
1748                                 return new UnboxCast (expr, target_type);
1749
1750                         //
1751                         // Skip the ExplicitReferenceConversion because we can not convert
1752                         // from Null to a ValueType, and ExplicitReference wont check against
1753                         // null literal explicitly
1754                         //
1755                         if (expr_type != TypeManager.null_type){
1756                                 ne = ExplicitReferenceConversion (expr, target_type);
1757                                 if (ne != null)
1758                                         return ne;
1759                         }
1760
1761                 skip_explicit:
1762                         if (ec.InUnsafe){
1763                                 if (target_type.IsPointer){
1764                                         if (expr_type.IsPointer)
1765                                                 return new EmptyCast (expr, target_type);
1766                                         
1767                                         if (expr_type == TypeManager.sbyte_type ||
1768                                             expr_type == TypeManager.byte_type ||
1769                                             expr_type == TypeManager.short_type ||
1770                                             expr_type == TypeManager.ushort_type ||
1771                                             expr_type == TypeManager.int32_type ||
1772                                             expr_type == TypeManager.uint32_type ||
1773                                             expr_type == TypeManager.uint64_type ||
1774                                             expr_type == TypeManager.int64_type)
1775                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1776                                 }
1777                                 if (expr_type.IsPointer){
1778                                         Expression e = null;
1779                                         
1780                                         if (target_type == TypeManager.sbyte_type)
1781                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1782                                         else if (target_type == TypeManager.byte_type)
1783                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1784                                         else if (target_type == TypeManager.short_type)
1785                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1786                                         else if (target_type == TypeManager.ushort_type)
1787                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1788                                         else if (target_type == TypeManager.int32_type)
1789                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1790                                         else if (target_type == TypeManager.uint32_type)
1791                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1792                                         else if (target_type == TypeManager.uint64_type)
1793                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1794                                         else if (target_type == TypeManager.int64_type){
1795                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1796                                         }
1797
1798                                         if (e != null){
1799                                                 Expression ci, ce;
1800
1801                                                 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1802
1803                                                 if (ci != null)
1804                                                         return ci;
1805
1806                                                 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1807                                                 if (ce != null)
1808                                                         return ce;
1809                                                 //
1810                                                 // We should always be able to go from an uint32
1811                                                 // implicitly or explicitly to the other integral
1812                                                 // types
1813                                                 //
1814                                                 throw new Exception ("Internal compiler error");
1815                                         }
1816                                 }
1817                         }
1818
1819                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1820                         if (ne != null)
1821                                 return ne;
1822
1823                         if (expr is NullLiteral){
1824                                 Report.Error (37, loc, "Cannot convert null to value type `" +
1825                                               TypeManager.CSharpName (target_type) + "'");
1826                                 return null;
1827                         }
1828                                 
1829                         Error_CannotConvertType (loc, original_expr_type, target_type);
1830                         return null;
1831                 }
1832
1833                 /// <summary>
1834                 ///   Same as ExplicitConversion, only it doesn't include user defined conversions
1835                 /// </summary>
1836                 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1837                                                                   Type target_type, Location l)
1838                 {
1839                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1840
1841                         if (ne != null)
1842                                 return ne;
1843
1844                         ne = ExplicitNumericConversion (ec, expr, target_type, l);
1845                         if (ne != null)
1846                                 return ne;
1847
1848                         ne = ExplicitReferenceConversion (expr, target_type);
1849                         if (ne != null)
1850                                 return ne;
1851
1852                         Error_CannotConvertType (l, expr.Type, target_type);
1853                         return null;
1854                 }
1855         }
1856 }