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