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