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