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