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