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