2002-07-22 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / mcs / ecore.cs
1 //
2 // ecore.cs: Core of the Expression representation for the intermediate tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
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         using System.Text;
18
19         /// <remarks>
20         ///   The ExprClass class contains the is used to pass the 
21         ///   classification of an expression (value, variable, namespace,
22         ///   type, method group, property access, event access, indexer access,
23         ///   nothing).
24         /// </remarks>
25         public enum ExprClass : byte {
26                 Invalid,
27                 
28                 Value,
29                 Variable,
30                 Namespace,
31                 Type,
32                 MethodGroup,
33                 PropertyAccess,
34                 EventAccess,
35                 IndexerAccess,
36                 Nothing, 
37         }
38
39         //
40         // This is just as a hint to AddressOf of what will be done with the
41         // address.
42         [Flags]
43         public enum AddressOp {
44                 Store = 1,
45                 Load  = 2,
46                 LoadStore = 3
47         };
48         
49         /// <summary>
50         ///   This interface is implemented by variables
51         /// </summary>
52         public interface IMemoryLocation {
53                 /// <summary>
54                 ///   The AddressOf method should generate code that loads
55                 ///   the address of the object and leaves it on the stack.
56                 ///
57                 ///   The `mode' argument is used to notify the expression
58                 ///   of whether this will be used to read from the address or
59                 ///   write to the address.
60                 ///
61                 ///   This is just a hint that can be used to provide good error
62                 ///   reporting, and should have no other side effects. 
63                 /// </summary>
64                 void AddressOf (EmitContext ec, AddressOp mode);
65         }
66
67         /// <remarks>
68         ///   Base class for expressions
69         /// </remarks>
70         public abstract class Expression {
71                 public ExprClass eclass;
72                 protected Type      type;
73                 
74                 public Type Type {
75                         get {
76                                 return type;
77                         }
78
79                         set {
80                                 type = value;
81                         }
82                 }
83
84                 /// <summary>
85                 ///   Utility wrapper routine for Error, just to beautify the code
86                 /// </summary>
87                 static protected void Error (int error, string s)
88                 {
89                         Report.Error (error, s);
90                 }
91
92                 static protected void Error (int error, Location loc, string s)
93                 {
94                         Report.Error (error, loc, s);
95                 }
96                 
97                 /// <summary>
98                 ///   Utility wrapper routine for Warning, just to beautify the code
99                 /// </summary>
100                 static protected void Warning (int warning, string s)
101                 {
102                         Report.Warning (warning, s);
103                 }
104
105                 static public void Error_CannotConvertType (Location loc, Type source, Type target)
106                 {
107                         Report.Error (30, loc, "Cannot convert type '" +
108                                       TypeManager.CSharpName (source) + "' to '" +
109                                       TypeManager.CSharpName (target) + "'");
110                 }
111
112                 /// <summary>
113                 ///   Performs semantic analysis on the Expression
114                 /// </summary>
115                 ///
116                 /// <remarks>
117                 ///   The Resolve method is invoked to perform the semantic analysis
118                 ///   on the node.
119                 ///
120                 ///   The return value is an expression (it can be the
121                 ///   same expression in some cases) or a new
122                 ///   expression that better represents this node.
123                 ///   
124                 ///   For example, optimizations of Unary (LiteralInt)
125                 ///   would return a new LiteralInt with a negated
126                 ///   value.
127                 ///   
128                 ///   If there is an error during semantic analysis,
129                 ///   then an error should be reported (using Report)
130                 ///   and a null value should be returned.
131                 ///   
132                 ///   There are two side effects expected from calling
133                 ///   Resolve(): the the field variable "eclass" should
134                 ///   be set to any value of the enumeration
135                 ///   `ExprClass' and the type variable should be set
136                 ///   to a valid type (this is the type of the
137                 ///   expression).
138                 /// </remarks>
139                 public abstract Expression DoResolve (EmitContext ec);
140
141                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
142                 {
143                         return DoResolve (ec);
144                 }
145                 
146                 /// <summary>
147                 ///   Resolves an expression and performs semantic analysis on it.
148                 /// </summary>
149                 ///
150                 /// <remarks>
151                 ///   Currently Resolve wraps DoResolve to perform sanity
152                 ///   checking and assertion checking on what we expect from Resolve.
153                 /// </remarks>
154                 public Expression Resolve (EmitContext ec)
155                 {
156                         Expression e = DoResolve (ec);
157
158                         if (e != null){
159
160                                 if (e is SimpleName){
161                                         SimpleName s = (SimpleName) e;
162
163                                         Report.Error (
164                                                       103, s.Location,
165                                                       "The name `" + s.Name + "' could not be found in `" +
166                                                       ec.DeclSpace.Name + "'");
167                                         return null;
168                                 }
169                                 
170                                 if (e.eclass == ExprClass.Invalid)
171                                         throw new Exception ("Expression " + e.GetType () +
172                                                              " ExprClass is Invalid after resolve");
173
174                                 if (e.eclass != ExprClass.MethodGroup)
175                                         if (e.type == null)
176                                                 throw new Exception (
177                                                         "Expression " + e.GetType () +
178                                                         " did not set its type after Resolve\n" +
179                                                         "called from: " + this.GetType ());
180                         }
181
182                         return e;
183                 }
184
185                 /// <summary>
186                 ///   Performs expression resolution and semantic analysis, but
187                 ///   allows SimpleNames to be returned.
188                 /// </summary>
189                 ///
190                 /// <remarks>
191                 ///   This is used by MemberAccess to construct long names that can not be
192                 ///   partially resolved (namespace-qualified names for example).
193                 /// </remarks>
194                 public Expression ResolveWithSimpleName (EmitContext ec)
195                 {
196                         Expression e;
197
198                         if (this is SimpleName)
199                                 e = ((SimpleName) this).DoResolveAllowStatic (ec);
200                         else 
201                                 e = DoResolve (ec);
202
203                         if (e != null){
204                                 if (e is SimpleName)
205                                         return e;
206
207                                 if (e.eclass == ExprClass.Invalid)
208                                         throw new Exception ("Expression " + e +
209                                                              " ExprClass is Invalid after resolve");
210
211                                 if (e.eclass != ExprClass.MethodGroup)
212                                         if (e.type == null)
213                                                 throw new Exception ("Expression " + e +
214                                                                      " did not set its type after Resolve");
215                         }
216
217                         return e;
218                 }
219                 
220                 /// <summary>
221                 ///   Resolves an expression for LValue assignment
222                 /// </summary>
223                 ///
224                 /// <remarks>
225                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
226                 ///   checking and assertion checking on what we expect from Resolve
227                 /// </remarks>
228                 public Expression ResolveLValue (EmitContext ec, Expression right_side)
229                 {
230                         Expression e = DoResolveLValue (ec, right_side);
231
232                         if (e != null){
233                                 if (e is SimpleName){
234                                         SimpleName s = (SimpleName) e;
235
236                                         Report.Error (
237                                                 103, s.Location,
238                                                 "The name `" + s.Name + "' could not be found in `" +
239                                                 ec.DeclSpace.Name + "'");
240                                         return null;
241                                 }
242
243                                 if (e.eclass == ExprClass.Invalid)
244                                         throw new Exception ("Expression " + e +
245                                                              " ExprClass is Invalid after resolve");
246
247                                 if (e.eclass != ExprClass.MethodGroup)
248                                         if (e.type == null)
249                                                 throw new Exception ("Expression " + e +
250                                                                      " did not set its type after Resolve");
251                         }
252
253                         return e;
254                 }
255                 
256                 /// <summary>
257                 ///   Emits the code for the expression
258                 /// </summary>
259                 ///
260                 /// <remarks>
261                 ///   The Emit method is invoked to generate the code
262                 ///   for the expression.  
263                 /// </remarks>
264                 public abstract void Emit (EmitContext ec);
265
266                 /// <summary>
267                 ///   Protected constructor.  Only derivate types should
268                 ///   be able to be created
269                 /// </summary>
270
271                 protected Expression ()
272                 {
273                         eclass = ExprClass.Invalid;
274                         type = null;
275                 }
276
277                 /// <summary>
278                 ///   Returns a literalized version of a literal FieldInfo
279                 /// </summary>
280                 ///
281                 /// <remarks>
282                 ///   The possible return values are:
283                 ///      IntConstant, UIntConstant
284                 ///      LongLiteral, ULongConstant
285                 ///      FloatConstant, DoubleConstant
286                 ///      StringConstant
287                 ///
288                 ///   The value returned is already resolved.
289                 /// </remarks>
290                 public static Constant Constantify (object v, Type t)
291                 {
292                         if (t == TypeManager.int32_type)
293                                 return new IntConstant ((int) v);
294                         else if (t == TypeManager.uint32_type)
295                                 return new UIntConstant ((uint) v);
296                         else if (t == TypeManager.int64_type)
297                                 return new LongConstant ((long) v);
298                         else if (t == TypeManager.uint64_type)
299                                 return new ULongConstant ((ulong) v);
300                         else if (t == TypeManager.float_type)
301                                 return new FloatConstant ((float) v);
302                         else if (t == TypeManager.double_type)
303                                 return new DoubleConstant ((double) v);
304                         else if (t == TypeManager.string_type)
305                                 return new StringConstant ((string) v);
306                         else if (t == TypeManager.short_type)
307                                 return new ShortConstant ((short)v);
308                         else if (t == TypeManager.ushort_type)
309                                 return new UShortConstant ((ushort)v);
310                         else if (t == TypeManager.sbyte_type)
311                                 return new SByteConstant (((sbyte)v));
312                         else if (t == TypeManager.byte_type)
313                                 return new ByteConstant ((byte)v);
314                         else if (t == TypeManager.char_type)
315                                 return new CharConstant ((char)v);
316                         else if (t == TypeManager.bool_type)
317                                 return new BoolConstant ((bool) v);
318                         else if (TypeManager.IsEnumType (t)){
319                                 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
320
321                                 return new EnumConstant (e, t);
322                         } else
323                                 throw new Exception ("Unknown type for constant (" + t +
324                                                      "), details: " + v);
325                 }
326
327                 /// <summary>
328                 ///   Returns a fully formed expression after a MemberLookup
329                 /// </summary>
330                 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
331                 {
332                         if (mi is EventInfo)
333                                 return new EventExpr ((EventInfo) mi, loc);
334                         else if (mi is FieldInfo)
335                                 return new FieldExpr ((FieldInfo) mi, loc);
336                         else if (mi is PropertyInfo)
337                                 return new PropertyExpr ((PropertyInfo) mi, loc);
338                         else if (mi is Type){
339                                 return new TypeExpr ((System.Type) mi);
340                         }
341
342                         return null;
343                 }
344
345                 //
346                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
347                 //
348                 // This code could use some optimizations, but we need to do some
349                 // measurements.  For example, we could use a delegate to `flag' when
350                 // something can not any longer be a method-group (because it is something
351                 // else).
352                 //
353                 // Return values:
354                 //     If the return value is an Array, then it is an array of
355                 //     MethodBases
356                 //   
357                 //     If the return value is an MemberInfo, it is anything, but a Method
358                 //
359                 //     null on error.
360                 //
361                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
362                 // the arguments here and have MemberLookup return only the methods that
363                 // match the argument count/type, unlike we are doing now (we delay this
364                 // decision).
365                 //
366                 // This is so we can catch correctly attempts to invoke instance methods
367                 // from a static body (scan for error 120 in ResolveSimpleName).
368                 //
369                 //
370                 // FIXME: Potential optimization, have a static ArrayList
371                 //
372
373                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
374                                                        MemberTypes mt, BindingFlags bf, Location loc)
375                 {
376                         MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
377
378                         if (mi == null)
379                                 return null;
380
381                         int count = mi.Length;
382
383                         if (count > 1)
384                                 return new MethodGroupExpr (mi, loc);
385
386                         if (mi [0] is MethodBase)
387                                 return new MethodGroupExpr (mi, loc);
388
389                         return ExprClassFromMemberInfo (ec, mi [0], loc);
390                 }
391
392                 public const MemberTypes AllMemberTypes =
393                         MemberTypes.Constructor |
394                         MemberTypes.Event       |
395                         MemberTypes.Field       |
396                         MemberTypes.Method      |
397                         MemberTypes.NestedType  |
398                         MemberTypes.Property;
399                 
400                 public const BindingFlags AllBindingFlags =
401                         BindingFlags.Public |
402                         BindingFlags.Static |
403                         BindingFlags.Instance;
404
405                 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
406                 {
407                         return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
408                 }
409
410                 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
411                 {
412                         return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
413                 }
414
415                 /// <summary>
416                 ///   This is a wrapper for MemberLookup that is not used to "probe", but
417                 ///   to find a final definition.  If the final definition is not found, we
418                 ///   look for private members and display a useful debugging message if we
419                 ///   find it.
420                 /// </summary>
421                 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name, 
422                                                             Location loc)
423                 {
424                         return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
425                 }
426
427                 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
428                                                             MemberTypes mt, BindingFlags bf, Location loc)
429                 {
430                         Expression e;
431
432                         e = MemberLookup (ec, t, name, mt, bf, loc);
433
434                         if (e != null)
435                                 return e;
436                         
437                         e = MemberLookup (ec, t, name, AllMemberTypes,
438                                           AllBindingFlags | BindingFlags.NonPublic, loc);
439                         if (e == null){
440                                 Report.Error (
441                                         117, loc, "`" + t + "' does not contain a definition " +
442                                         "for `" + name + "'");
443                         } else {
444                                 Report.Error (
445                                         122, loc, "`" + t + "." + name +
446                                         "' is inaccessible due to its protection level");
447                         }
448                         
449                         return null;
450                 }
451
452                 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
453                 {
454                         EventInfo ei = event_expr.EventInfo;
455
456                         return TypeManager.GetPrivateFieldOfEvent (ei);
457                 }
458                 
459                 static EmptyExpression MyEmptyExpr;
460                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
461                 {
462                         Type expr_type = expr.Type;
463
464                         if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
465                                 // if we are a method group, emit a warning
466
467                                 expr.Emit (null);
468                         }
469                         
470                         if (target_type == TypeManager.object_type) {
471                                 //
472                                 // A pointer type cannot be converted to object
473                                 // 
474                                 if (expr_type.IsPointer)
475                                         return null;
476
477                                 if (expr_type.IsValueType)
478                                         return new BoxedCast (expr);
479                                 if (expr_type.IsClass || expr_type.IsInterface)
480                                         return new EmptyCast (expr, target_type);
481                         } else if (expr_type.IsSubclassOf (target_type)) {
482                                 return new EmptyCast (expr, target_type);
483                         } else {
484
485                                 // This code is kind of mirrored inside StandardConversionExists
486                                 // with the small distinction that we only probe there
487                                 //
488                                 // Always ensure that the code here and there is in sync
489                                 
490                                 // from the null type to any reference-type.
491                                 if (expr is NullLiteral && !target_type.IsValueType)
492                                         return new EmptyCast (expr, target_type);
493
494                                 // from any class-type S to any interface-type T.
495                                 if (expr_type.IsClass && target_type.IsInterface) {
496                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
497                                                 return new EmptyCast (expr, target_type);
498                                         else
499                                                 return null;
500                                 }
501
502                                 // from any interface type S to interface-type T.
503                                 if (expr_type.IsInterface && target_type.IsInterface) {
504
505                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
506                                                 return new EmptyCast (expr, target_type);
507                                         else
508                                                 return null;
509                                 }
510                                 
511                                 // from an array-type S to an array-type of type T
512                                 if (expr_type.IsArray && target_type.IsArray) {
513                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
514
515                                                 Type expr_element_type = expr_type.GetElementType ();
516
517                                                 if (MyEmptyExpr == null)
518                                                         MyEmptyExpr = new EmptyExpression ();
519                                                 
520                                                 MyEmptyExpr.SetType (expr_element_type);
521                                                 Type target_element_type = target_type.GetElementType ();
522
523                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
524                                                         if (StandardConversionExists (MyEmptyExpr,
525                                                                                       target_element_type))
526                                                                 return new EmptyCast (expr, target_type);
527                                         }
528                                 }
529                                 
530                                 
531                                 // from an array-type to System.Array
532                                 if (expr_type.IsArray && target_type == TypeManager.array_type)
533                                         return new EmptyCast (expr, target_type);
534                                 
535                                 // from any delegate type to System.Delegate
536                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
537                                     target_type == TypeManager.delegate_type)
538                                         return new EmptyCast (expr, target_type);
539                                         
540                                 // from any array-type or delegate type into System.ICloneable.
541                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
542                                         if (target_type == TypeManager.icloneable_type)
543                                                 return new EmptyCast (expr, target_type);
544                                 
545                                 return null;
546
547                         }
548                         
549                         return null;
550                 }
551
552                 /// <summary>
553                 ///   Handles expressions like this: decimal d; d = 1;
554                 ///   and changes them into: decimal d; d = new System.Decimal (1);
555                 /// </summary>
556                 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
557                 {
558                         ArrayList args = new ArrayList ();
559
560                         args.Add (new Argument (expr, Argument.AType.Expression));
561
562                         Expression ne = new New (new TypeExpr (target), args, new Location (-1));
563
564                         return ne.Resolve (ec);
565                 }
566
567                 /// <summary>
568                 ///   Implicit Numeric Conversions.
569                 ///
570                 ///   expr is the expression to convert, returns a new expression of type
571                 ///   target_type or null if an implicit conversion is not possible.
572                 /// </summary>
573                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
574                                                                     Type target_type, Location loc)
575                 {
576                         Type expr_type = expr.Type;
577                         
578                         //
579                         // Attempt to do the implicit constant expression conversions
580
581                         if (expr is IntConstant){
582                                 Expression e;
583                                 
584                                 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
585
586                                 if (e != null)
587                                         return e;
588                         } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
589                                 //
590                                 // Try the implicit constant expression conversion
591                                 // from long to ulong, instead of a nice routine,
592                                 // we just inline it
593                                 //
594                                 long v = ((LongConstant) expr).Value;
595                                 if (v > 0)
596                                         return new ULongConstant ((ulong) v);
597                         }
598
599                         //
600                         // If we have an enumeration, extract the underlying type,
601                         // use this during the comparission, but wrap around the original
602                         // target_type
603                         //
604                         Type real_target_type = target_type;
605
606                         if (TypeManager.IsEnumType (real_target_type))
607                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
608
609                         if (expr_type == real_target_type)
610                                 return new EmptyCast (expr, target_type);
611                         
612                         if (expr_type == TypeManager.sbyte_type){
613                                 //
614                                 // From sbyte to short, int, long, float, double.
615                                 //
616                                 if (real_target_type == TypeManager.int32_type)
617                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
618                                 if (real_target_type == TypeManager.int64_type)
619                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
620                                 if (real_target_type == TypeManager.double_type)
621                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
622                                 if (real_target_type == TypeManager.float_type)
623                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
624                                 if (real_target_type == TypeManager.short_type)
625                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
626                                 if (real_target_type == TypeManager.decimal_type)
627                                         return InternalTypeConstructor (ec, expr, target_type);
628                         } else if (expr_type == TypeManager.byte_type){
629                                 //
630                                 // From byte to short, ushort, int, uint, long, ulong, float, double
631                                 // 
632                                 if ((real_target_type == TypeManager.short_type) ||
633                                     (real_target_type == TypeManager.ushort_type) ||
634                                     (real_target_type == TypeManager.int32_type) ||
635                                     (real_target_type == TypeManager.uint32_type))
636                                         return new EmptyCast (expr, target_type);
637
638                                 if (real_target_type == TypeManager.uint64_type)
639                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
640                                 if (real_target_type == TypeManager.int64_type)
641                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
642                                 if (real_target_type == TypeManager.float_type)
643                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
644                                 if (real_target_type == TypeManager.double_type)
645                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
646                                 if (real_target_type == TypeManager.decimal_type)
647                                         return InternalTypeConstructor (ec, expr, target_type);
648                         } else if (expr_type == TypeManager.short_type){
649                                 //
650                                 // From short to int, long, float, double
651                                 // 
652                                 if (real_target_type == TypeManager.int32_type)
653                                         return new EmptyCast (expr, target_type);
654                                 if (real_target_type == TypeManager.int64_type)
655                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
656                                 if (real_target_type == TypeManager.double_type)
657                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
658                                 if (real_target_type == TypeManager.float_type)
659                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
660                                 if (real_target_type == TypeManager.decimal_type)
661                                         return InternalTypeConstructor (ec, expr, target_type);
662                         } else if (expr_type == TypeManager.ushort_type){
663                                 //
664                                 // From ushort to int, uint, long, ulong, float, double
665                                 //
666                                 if (real_target_type == TypeManager.uint32_type)
667                                         return new EmptyCast (expr, target_type);
668
669                                 if (real_target_type == TypeManager.uint64_type)
670                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
671                                 if (real_target_type == TypeManager.int32_type)
672                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
673                                 if (real_target_type == TypeManager.int64_type)
674                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
675                                 if (real_target_type == TypeManager.double_type)
676                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
677                                 if (real_target_type == TypeManager.float_type)
678                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
679                                 if (real_target_type == TypeManager.decimal_type)
680                                         return InternalTypeConstructor (ec, expr, target_type);
681                         } else if (expr_type == TypeManager.int32_type){
682                                 //
683                                 // From int to long, float, double
684                                 //
685                                 if (real_target_type == TypeManager.int64_type)
686                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
687                                 if (real_target_type == TypeManager.double_type)
688                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
689                                 if (real_target_type == TypeManager.float_type)
690                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
691                                 if (real_target_type == TypeManager.decimal_type)
692                                         return InternalTypeConstructor (ec, expr, target_type);
693                         } else if (expr_type == TypeManager.uint32_type){
694                                 //
695                                 // From uint to long, ulong, float, double
696                                 //
697                                 if (real_target_type == TypeManager.int64_type)
698                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
699                                 if (real_target_type == TypeManager.uint64_type)
700                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
701                                 if (real_target_type == TypeManager.double_type)
702                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
703                                                                OpCodes.Conv_R8);
704                                 if (real_target_type == TypeManager.float_type)
705                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
706                                                                OpCodes.Conv_R4);
707                                 if (real_target_type == TypeManager.decimal_type)
708                                         return InternalTypeConstructor (ec, expr, target_type);
709                         } else if (expr_type == TypeManager.int64_type){
710                                 //
711                                 // From long/ulong to float, double
712                                 //
713                                 if (real_target_type == TypeManager.double_type)
714                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
715                                 if (real_target_type == TypeManager.float_type)
716                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
717                                 if (real_target_type == TypeManager.decimal_type)
718                                         return InternalTypeConstructor (ec, expr, target_type);
719                         } else if (expr_type == TypeManager.uint64_type){
720                                 //
721                                 // From ulong to float, double
722                                 //
723                                 if (real_target_type == TypeManager.double_type)
724                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
725                                                                OpCodes.Conv_R8);
726                                 if (real_target_type == TypeManager.float_type)
727                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
728                                                                OpCodes.Conv_R4);        
729                                 if (real_target_type == TypeManager.decimal_type)
730                                         return InternalTypeConstructor (ec, expr, target_type);
731                         } else if (expr_type == TypeManager.char_type){
732                                 //
733                                 // From char to ushort, int, uint, long, ulong, float, double
734                                 // 
735                                 if ((real_target_type == TypeManager.ushort_type) ||
736                                     (real_target_type == TypeManager.int32_type) ||
737                                     (real_target_type == TypeManager.uint32_type))
738                                         return new EmptyCast (expr, target_type);
739                                 if (real_target_type == TypeManager.uint64_type)
740                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
741                                 if (real_target_type == TypeManager.int64_type)
742                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
743                                 if (real_target_type == TypeManager.float_type)
744                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
745                                 if (real_target_type == TypeManager.double_type)
746                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
747                                 if (real_target_type == TypeManager.decimal_type)
748                                         return InternalTypeConstructor (ec, expr, target_type);
749                         } else if (expr_type == TypeManager.float_type){
750                                 //
751                                 // float to double
752                                 //
753                                 if (real_target_type == TypeManager.double_type)
754                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
755                         }
756
757                         return null;
758                 }
759
760                 //
761                 // Tests whether an implicit reference conversion exists between expr_type
762                 // and target_type
763                 //
764                 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
765                 {
766                         Type expr_type = expr.Type;
767                         
768                         //
769                         // This is the boxed case.
770                         //
771                         if (target_type == TypeManager.object_type) {
772                                 if ((expr_type.IsClass) ||
773                                     (expr_type.IsValueType) ||
774                                     (expr_type.IsInterface))
775                                         return true;
776                                 
777                         } else if (expr_type.IsSubclassOf (target_type)) {
778                                 return true;
779                                 
780                         } else {
781                                 // Please remember that all code below actually comes
782                                 // from ImplicitReferenceConversion so make sure code remains in sync
783                                 
784                                 // from any class-type S to any interface-type T.
785                                 if (expr_type.IsClass && target_type.IsInterface) {
786                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
787                                                 return true;
788                                 }
789                                 
790                                 // from any interface type S to interface-type T.
791                                 if (expr_type.IsInterface && target_type.IsInterface)
792                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
793                                                 return true;
794                                 
795                                 // from an array-type S to an array-type of type T
796                                 if (expr_type.IsArray && target_type.IsArray) {
797                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
798                                                 
799                                                 Type expr_element_type = expr_type.GetElementType ();
800
801                                                 if (MyEmptyExpr == null)
802                                                         MyEmptyExpr = new EmptyExpression ();
803                                                 
804                                                 MyEmptyExpr.SetType (expr_element_type);
805                                                 Type target_element_type = target_type.GetElementType ();
806                                                 
807                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
808                                                         if (StandardConversionExists (MyEmptyExpr,
809                                                                                       target_element_type))
810                                                                 return true;
811                                         }
812                                 }
813                                 
814                                 // from an array-type to System.Array
815                                 if (expr_type.IsArray && (target_type == TypeManager.array_type))
816                                         return true;
817                                 
818                                 // from any delegate type to System.Delegate
819                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
820                                     target_type == TypeManager.delegate_type)
821                                         if (target_type.IsAssignableFrom (expr_type))
822                                                 return true;
823                                         
824                                 // from any array-type or delegate type into System.ICloneable.
825                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
826                                         if (target_type == TypeManager.icloneable_type)
827                                                 return true;
828                                 
829                                 // from the null type to any reference-type.
830                                 if (expr is NullLiteral && !target_type.IsValueType &&
831                                     !TypeManager.IsEnumType (target_type))
832                                         return true;
833                                 
834                         }
835
836                         return false;
837                 }
838
839                 /// <summary>
840                 ///  Same as StandardConversionExists except that it also looks at
841                 ///  implicit user defined conversions - needed for overload resolution
842                 /// </summary>
843                 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
844                 {
845                         if (StandardConversionExists (expr, target_type) == true)
846                                 return true;
847
848                         Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
849
850                         if (dummy != null)
851                                 return true;
852
853                         return false;
854                 }
855                 
856                 /// <summary>
857                 ///  Determines if a standard implicit conversion exists from
858                 ///  expr_type to target_type
859                 /// </summary>
860                 public static bool StandardConversionExists (Expression expr, Type target_type)
861                 {
862                         Type expr_type = expr.Type;
863                         
864                         if (expr_type == target_type)
865                                 return true;
866
867                         // First numeric conversions 
868
869                         if (expr_type == TypeManager.sbyte_type){
870                                 //
871                                 // From sbyte to short, int, long, float, double.
872                                 //
873                                 if ((target_type == TypeManager.int32_type) || 
874                                     (target_type == TypeManager.int64_type) ||
875                                     (target_type == TypeManager.double_type) ||
876                                     (target_type == TypeManager.float_type)  ||
877                                     (target_type == TypeManager.short_type) ||
878                                     (target_type == TypeManager.decimal_type))
879                                         return true;
880                                 
881                         } else if (expr_type == TypeManager.byte_type){
882                                 //
883                                 // From byte to short, ushort, int, uint, long, ulong, float, double
884                                 // 
885                                 if ((target_type == TypeManager.short_type) ||
886                                     (target_type == TypeManager.ushort_type) ||
887                                     (target_type == TypeManager.int32_type) ||
888                                     (target_type == TypeManager.uint32_type) ||
889                                     (target_type == TypeManager.uint64_type) ||
890                                     (target_type == TypeManager.int64_type) ||
891                                     (target_type == TypeManager.float_type) ||
892                                     (target_type == TypeManager.double_type) ||
893                                     (target_type == TypeManager.decimal_type))
894                                         return true;
895         
896                         } else if (expr_type == TypeManager.short_type){
897                                 //
898                                 // From short to int, long, float, double
899                                 // 
900                                 if ((target_type == TypeManager.int32_type) ||
901                                     (target_type == TypeManager.int64_type) ||
902                                     (target_type == TypeManager.double_type) ||
903                                     (target_type == TypeManager.float_type) ||
904                                     (target_type == TypeManager.decimal_type))
905                                         return true;
906                                         
907                         } else if (expr_type == TypeManager.ushort_type){
908                                 //
909                                 // From ushort to int, uint, long, ulong, float, double
910                                 //
911                                 if ((target_type == TypeManager.uint32_type) ||
912                                     (target_type == TypeManager.uint64_type) ||
913                                     (target_type == TypeManager.int32_type) ||
914                                     (target_type == TypeManager.int64_type) ||
915                                     (target_type == TypeManager.double_type) ||
916                                     (target_type == TypeManager.float_type) ||
917                                     (target_type == TypeManager.decimal_type))
918                                         return true;
919                                     
920                         } else if (expr_type == TypeManager.int32_type){
921                                 //
922                                 // From int to long, float, double
923                                 //
924                                 if ((target_type == TypeManager.int64_type) ||
925                                     (target_type == TypeManager.double_type) ||
926                                     (target_type == TypeManager.float_type) ||
927                                     (target_type == TypeManager.decimal_type))
928                                         return true;
929                                         
930                         } else if (expr_type == TypeManager.uint32_type){
931                                 //
932                                 // From uint to long, ulong, float, double
933                                 //
934                                 if ((target_type == TypeManager.int64_type) ||
935                                     (target_type == TypeManager.uint64_type) ||
936                                     (target_type == TypeManager.double_type) ||
937                                     (target_type == TypeManager.float_type) ||
938                                     (target_type == TypeManager.decimal_type))
939                                         return true;
940                                         
941                         } else if ((expr_type == TypeManager.uint64_type) ||
942                                    (expr_type == TypeManager.int64_type)) {
943                                 //
944                                 // From long/ulong to float, double
945                                 //
946                                 if ((target_type == TypeManager.double_type) ||
947                                     (target_type == TypeManager.float_type) ||
948                                     (target_type == TypeManager.decimal_type))
949                                         return true;
950                                     
951                         } else if (expr_type == TypeManager.char_type){
952                                 //
953                                 // From char to ushort, int, uint, long, ulong, float, double
954                                 // 
955                                 if ((target_type == TypeManager.ushort_type) ||
956                                     (target_type == TypeManager.int32_type) ||
957                                     (target_type == TypeManager.uint32_type) ||
958                                     (target_type == TypeManager.uint64_type) ||
959                                     (target_type == TypeManager.int64_type) ||
960                                     (target_type == TypeManager.float_type) ||
961                                     (target_type == TypeManager.double_type) ||
962                                     (target_type == TypeManager.decimal_type))
963                                         return true;
964
965                         } else if (expr_type == TypeManager.float_type){
966                                 //
967                                 // float to double
968                                 //
969                                 if (target_type == TypeManager.double_type)
970                                         return true;
971                         }       
972                         
973                         if (ImplicitReferenceConversionExists (expr, target_type))
974                                 return true;
975                         
976                         if (expr is IntConstant){
977                                 int value = ((IntConstant) expr).Value;
978
979                                 if (target_type == TypeManager.sbyte_type){
980                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
981                                                 return true;
982                                 } else if (target_type == TypeManager.byte_type){
983                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
984                                                 return true;
985                                 } else if (target_type == TypeManager.short_type){
986                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
987                                                 return true;
988                                 } else if (target_type == TypeManager.ushort_type){
989                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
990                                                 return true;
991                                 } else if (target_type == TypeManager.uint32_type){
992                                         if (value >= 0)
993                                                 return true;
994                                 } else if (target_type == TypeManager.uint64_type){
995                                          //
996                                          // we can optimize this case: a positive int32
997                                          // always fits on a uint64.  But we need an opcode
998                                          // to do it.
999                                          //
1000                                         if (value >= 0)
1001                                                 return true;
1002                                 }
1003                                 
1004                                 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1005                                         return true;
1006                         }
1007
1008                         if (expr is LongConstant && target_type == TypeManager.uint64_type){
1009                                 //
1010                                 // Try the implicit constant expression conversion
1011                                 // from long to ulong, instead of a nice routine,
1012                                 // we just inline it
1013                                 //
1014                                 long v = ((LongConstant) expr).Value;
1015                                 if (v > 0)
1016                                         return true;
1017                         }
1018                         
1019                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1020                                 IntLiteral i = (IntLiteral) expr;
1021
1022                                 if (i.Value == 0)
1023                                         return true;
1024                         }
1025                         return false;
1026                 }
1027
1028                 //
1029                 // Used internally by FindMostEncompassedType, this is used
1030                 // to avoid creating lots of objects in the tight loop inside
1031                 // FindMostEncompassedType
1032                 //
1033                 static EmptyExpression priv_fmet_param;
1034                 
1035                 /// <summary>
1036                 ///  Finds "most encompassed type" according to the spec (13.4.2)
1037                 ///  amongst the methods in the MethodGroupExpr
1038                 /// </summary>
1039                 static Type FindMostEncompassedType (ArrayList types)
1040                 {
1041                         Type best = null;
1042
1043                         if (priv_fmet_param == null)
1044                                 priv_fmet_param = new EmptyExpression ();
1045
1046                         foreach (Type t in types){
1047                                 priv_fmet_param.SetType (t);
1048                                 
1049                                 if (best == null) {
1050                                         best = t;
1051                                         continue;
1052                                 }
1053                                 
1054                                 if (StandardConversionExists (priv_fmet_param, best))
1055                                         best = t;
1056                         }
1057
1058                         return best;
1059                 }
1060
1061                 //
1062                 // Used internally by FindMostEncompassingType, this is used
1063                 // to avoid creating lots of objects in the tight loop inside
1064                 // FindMostEncompassingType
1065                 //
1066                 static EmptyExpression priv_fmee_ret;
1067                 
1068                 /// <summary>
1069                 ///  Finds "most encompassing type" according to the spec (13.4.2)
1070                 ///  amongst the types in the given set
1071                 /// </summary>
1072                 static Type FindMostEncompassingType (ArrayList types)
1073                 {
1074                         Type best = null;
1075
1076                         if (priv_fmee_ret == null)
1077                                 priv_fmee_ret = new EmptyExpression ();
1078
1079                         foreach (Type t in types){
1080                                 priv_fmee_ret.SetType (best);
1081
1082                                 if (best == null) {
1083                                         best = t;
1084                                         continue;
1085                                 }
1086
1087                                 if (StandardConversionExists (priv_fmee_ret, t))
1088                                         best = t;
1089                         }
1090                         
1091                         return best;
1092                 }
1093
1094                 //
1095                 // Used to avoid creating too many objects
1096                 //
1097                 static EmptyExpression priv_fms_expr;
1098                 
1099                 /// <summary>
1100                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
1101                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
1102                 ///   for explicit and implicit conversion operators.
1103                 /// </summary>
1104                 static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
1105                                                            bool apply_explicit_conv_rules,
1106                                                            Location loc)
1107                 {
1108                         ArrayList src_types_set = new ArrayList ();
1109                         
1110                         if (priv_fms_expr == null)
1111                                 priv_fms_expr = new EmptyExpression ();
1112                         
1113                         //
1114                         // If any operator converts from S then Sx = S
1115                         //
1116                         foreach (MethodBase mb in me.Methods){
1117                                 ParameterData pd = Invocation.GetParameterData (mb);
1118                                 Type param_type = pd.ParameterType (0);
1119
1120                                 if (param_type == source_type)
1121                                         return param_type;
1122
1123                                 if (apply_explicit_conv_rules) {
1124                                         //
1125                                         // From the spec :
1126                                         // Find the set of applicable user-defined conversion operators, U.  This set
1127                                         // consists of the
1128                                         // user-defined implicit or explicit conversion operators declared by
1129                                         // the classes or structs in D that convert from a type encompassing
1130                                         // or encompassed by S to a type encompassing or encompassed by T
1131                                         //
1132                                         priv_fms_expr.SetType (param_type);
1133                                         if (StandardConversionExists (priv_fms_expr, source_type))
1134                                                 src_types_set.Add (param_type);
1135                                         else {
1136                                                 priv_fms_expr.SetType (source_type);
1137                                                 if (StandardConversionExists (priv_fms_expr, param_type))
1138                                                         src_types_set.Add (param_type);
1139                                         }
1140                                 } else {
1141                                         //
1142                                         // Only if S is encompassed by param_type
1143                                         //
1144                                         priv_fms_expr.SetType (source_type);
1145                                         if (StandardConversionExists (priv_fms_expr, param_type))
1146                                                 src_types_set.Add (param_type);
1147                                 }
1148                         }
1149                         
1150                         //
1151                         // Explicit Conv rules
1152                         //
1153                         if (apply_explicit_conv_rules) {
1154                                 ArrayList candidate_set = new ArrayList ();
1155
1156                                 foreach (Type param_type in src_types_set){
1157                                         priv_fms_expr.SetType (source_type);
1158                                         
1159                                         if (StandardConversionExists (priv_fms_expr, param_type))
1160                                                 candidate_set.Add (param_type);
1161                                 }
1162
1163                                 if (candidate_set.Count != 0)
1164                                         return FindMostEncompassedType (candidate_set);
1165                         }
1166
1167                         //
1168                         // Final case
1169                         //
1170                         if (apply_explicit_conv_rules)
1171                                 return FindMostEncompassingType (src_types_set);
1172                         else
1173                                 return FindMostEncompassedType (src_types_set);
1174                 }
1175
1176                 //
1177                 // Useful in avoiding proliferation of objects
1178                 //
1179                 static EmptyExpression priv_fmt_expr;
1180                 
1181                 /// <summary>
1182                 ///  Finds the most specific target Tx according to section 13.4.4
1183                 /// </summary>
1184                 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1185                                                            bool apply_explicit_conv_rules,
1186                                                            Location loc)
1187                 {
1188                         ArrayList tgt_types_set = new ArrayList ();
1189                         
1190                         if (priv_fmt_expr == null)
1191                                 priv_fmt_expr = new EmptyExpression ();
1192                         
1193                         //
1194                         // If any operator converts to T then Tx = T
1195                         //
1196                         foreach (MethodInfo mi in me.Methods){
1197                                 Type ret_type = mi.ReturnType;
1198
1199                                 if (ret_type == target)
1200                                         return ret_type;
1201
1202                                 if (apply_explicit_conv_rules) {
1203                                         //
1204                                         // From the spec :
1205                                         // Find the set of applicable user-defined conversion operators, U.
1206                                         //
1207                                         // This set consists of the
1208                                         // user-defined implicit or explicit conversion operators declared by
1209                                         // the classes or structs in D that convert from a type encompassing
1210                                         // or encompassed by S to a type encompassing or encompassed by T
1211                                         //
1212                                         priv_fms_expr.SetType (ret_type);
1213                                         if (StandardConversionExists (priv_fms_expr, target))
1214                                                 tgt_types_set.Add (ret_type);
1215                                         else {
1216                                                 priv_fms_expr.SetType (target);
1217                                                 if (StandardConversionExists (priv_fms_expr, ret_type))
1218                                                         tgt_types_set.Add (ret_type);
1219                                         }
1220                                 } else {
1221                                         //
1222                                         // Only if T is encompassed by param_type
1223                                         //
1224                                         priv_fms_expr.SetType (ret_type);
1225                                         if (StandardConversionExists (priv_fms_expr, target))
1226                                                 tgt_types_set.Add (ret_type);
1227                                 }
1228                         }
1229
1230                         //
1231                         // Explicit conv rules
1232                         //
1233                         if (apply_explicit_conv_rules) {
1234                                 ArrayList candidate_set = new ArrayList ();
1235
1236                                 foreach (Type ret_type in tgt_types_set){
1237                                         priv_fmt_expr.SetType (ret_type);
1238                                         
1239                                         if (StandardConversionExists (priv_fmt_expr, target))
1240                                                 candidate_set.Add (ret_type);
1241                                 }
1242
1243                                 if (candidate_set.Count != 0)
1244                                         return FindMostEncompassingType (candidate_set);
1245                         }
1246                         
1247                         //
1248                         // Okay, final case !
1249                         //
1250                         if (apply_explicit_conv_rules)
1251                                 return FindMostEncompassedType (tgt_types_set);
1252                         else
1253                                 return FindMostEncompassingType (tgt_types_set);
1254                 }
1255                 
1256                 /// <summary>
1257                 ///  User-defined Implicit conversions
1258                 /// </summary>
1259                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1260                                                                  Type target, Location loc)
1261                 {
1262                         return UserDefinedConversion (ec, source, target, loc, false);
1263                 }
1264
1265                 /// <summary>
1266                 ///  User-defined Explicit conversions
1267                 /// </summary>
1268                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1269                                                                  Type target, Location loc)
1270                 {
1271                         return UserDefinedConversion (ec, source, target, loc, true);
1272                 }
1273
1274                 /// <summary>
1275                 ///   Computes the MethodGroup for the user-defined conversion
1276                 ///   operators from source_type to target_type.  `look_for_explicit'
1277                 ///   controls whether we should also include the list of explicit
1278                 ///   operators
1279                 /// </summary>
1280                 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1281                                                                Type source_type, Type target_type,
1282                                                                Location loc, bool look_for_explicit)
1283                 {
1284                         Expression mg1 = null, mg2 = null;
1285                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1286                         string op_name;
1287
1288                         //
1289                         // FIXME : How does the False operator come into the picture ?
1290                         // This doesn't look complete and very correct !
1291                         //
1292                         if (target_type == TypeManager.bool_type && !look_for_explicit)
1293                                 op_name = "op_True";
1294                         else
1295                                 op_name = "op_Implicit";
1296
1297                         MethodGroupExpr union3;
1298                         
1299                         mg1 = MethodLookup (ec, source_type, op_name, loc);
1300                         if (source_type.BaseType != null)
1301                                 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1302
1303                         if (mg1 == null)
1304                                 union3 = (MethodGroupExpr) mg2;
1305                         else if (mg2 == null)
1306                                 union3 = (MethodGroupExpr) mg1;
1307                         else
1308                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1309
1310                         mg1 = MethodLookup (ec, target_type, op_name, loc);
1311                         if (mg1 != null){
1312                                 if (union3 != null)
1313                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1314                                 else
1315                                         union3 = (MethodGroupExpr) mg1;
1316                         }
1317
1318                         if (target_type.BaseType != null)
1319                                 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1320                         
1321                         if (mg1 != null){
1322                                 if (union3 != null)
1323                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1324                                 else
1325                                         union3 = (MethodGroupExpr) mg1;
1326                         }
1327
1328                         MethodGroupExpr union4 = null;
1329
1330                         if (look_for_explicit) {
1331                                 op_name = "op_Explicit";
1332
1333                                 mg5 = MemberLookup (ec, source_type, op_name, loc);
1334                                 if (source_type.BaseType != null)
1335                                         mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1336                                 
1337                                 mg7 = MemberLookup (ec, target_type, op_name, loc);
1338                                 if (target_type.BaseType != null)
1339                                         mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1340                                 
1341                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1342                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1343
1344                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1345                         }
1346                         
1347                         return Invocation.MakeUnionSet (union3, union4, loc);
1348                 }
1349                 
1350                 /// <summary>
1351                 ///   User-defined conversions
1352                 /// </summary>
1353                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1354                                                                 Type target, Location loc,
1355                                                                 bool look_for_explicit)
1356                 {
1357                         MethodGroupExpr union;
1358                         Type source_type = source.Type;
1359                         MethodBase method = null;
1360                         
1361                         union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1362                         if (union == null)
1363                                 return null;
1364                         
1365                         Type most_specific_source, most_specific_target;
1366
1367 #if BLAH
1368                         foreach (MethodBase m in union.Methods){
1369                                 Console.WriteLine ("Name: " + m.Name);
1370                                 Console.WriteLine ("    : " + ((MethodInfo)m).ReturnType);
1371                         }
1372 #endif
1373                         
1374                         most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
1375                         if (most_specific_source == null)
1376                                 return null;
1377
1378                         most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1379                         if (most_specific_target == null) 
1380                                 return null;
1381                         
1382                         int count = 0;
1383
1384                         foreach (MethodBase mb in union.Methods){
1385                                 ParameterData pd = Invocation.GetParameterData (mb);
1386                                 MethodInfo mi = (MethodInfo) mb;
1387                                 
1388                                 if (pd.ParameterType (0) == most_specific_source &&
1389                                     mi.ReturnType == most_specific_target) {
1390                                         method = mb;
1391                                         count++;
1392                                 }
1393                         }
1394                         
1395                         if (method == null || count > 1) {
1396                                 Report.Error (-11, loc, "Ambiguous user defined conversion");
1397                                 return null;
1398                         }
1399                         
1400                         //
1401                         // This will do the conversion to the best match that we
1402                         // found.  Now we need to perform an implict standard conversion
1403                         // if the best match was not the type that we were requested
1404                         // by target.
1405                         //
1406                         if (look_for_explicit)
1407                                 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1408                         else
1409                                 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1410
1411                         if (source == null)
1412                                 return null;
1413
1414                         Expression e;
1415                         e =  new UserCast ((MethodInfo) method, source);
1416                         if (e.Type != target){
1417                                 if (!look_for_explicit)
1418                                         e = ConvertImplicitStandard (ec, e, target, loc);
1419                                 else
1420                                         e = ConvertExplicitStandard (ec, e, target, loc);
1421                         } 
1422                         return e;
1423                 }
1424                 
1425                 /// <summary>
1426                 ///   Converts implicitly the resolved expression `expr' into the
1427                 ///   `target_type'.  It returns a new expression that can be used
1428                 ///   in a context that expects a `target_type'. 
1429                 /// </summary>
1430                 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1431                                                           Type target_type, Location loc)
1432                 {
1433                         Type expr_type = expr.Type;
1434                         Expression e;
1435
1436                         if (expr_type == target_type)
1437                                 return expr;
1438
1439                         if (target_type == null)
1440                                 throw new Exception ("Target type is null");
1441
1442                         e = ConvertImplicitStandard (ec, expr, target_type, loc);
1443                         if (e != null)
1444                                 return e;
1445
1446                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1447                         if (e != null)
1448                                 return e;
1449
1450                         return null;
1451                 }
1452
1453                 
1454                 /// <summary>
1455                 ///   Attempts to apply the `Standard Implicit
1456                 ///   Conversion' rules to the expression `expr' into
1457                 ///   the `target_type'.  It returns a new expression
1458                 ///   that can be used in a context that expects a
1459                 ///   `target_type'.
1460                 ///
1461                 ///   This is different from `ConvertImplicit' in that the
1462                 ///   user defined implicit conversions are excluded. 
1463                 /// </summary>
1464                 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1465                                                                   Type target_type, Location loc)
1466                 {
1467                         Type expr_type = expr.Type;
1468                         Expression e;
1469
1470                         if (expr_type == target_type)
1471                                 return expr;
1472
1473                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1474                         if (e != null)
1475                                 return e;
1476
1477                         e = ImplicitReferenceConversion (expr, target_type);
1478                         if (e != null)
1479                                 return e;
1480
1481                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1482                                 IntLiteral i = (IntLiteral) expr;
1483
1484                                 if (i.Value == 0)
1485                                         return new EmptyCast (expr, target_type);
1486                         }
1487
1488                         if (ec.InUnsafe) {
1489                                 if (expr_type.IsPointer){
1490                                         if (target_type == TypeManager.void_ptr_type)
1491                                                 return new EmptyCast (expr, target_type);
1492
1493                                         //
1494                                         // yep, comparing pointer types cant be done with
1495                                         // t1 == t2, we have to compare their element types.
1496                                         //
1497                                         if (target_type.IsPointer){
1498                                                 if (target_type.GetElementType()==expr_type.GetElementType())
1499                                                         return expr;
1500                                         }
1501                                 }
1502                                 
1503                                 if (target_type.IsPointer){
1504                                         if (expr is NullLiteral)
1505                                                 return new EmptyCast (expr, target_type);
1506                                 }
1507                         }
1508
1509                         return null;
1510                 }
1511
1512                 /// <summary>
1513                 ///   Attemps to perform an implict constant conversion of the IntConstant
1514                 ///   into a different data type using casts (See Implicit Constant
1515                 ///   Expression Conversions)
1516                 /// </summary>
1517                 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1518                 {
1519                         int value = ic.Value;
1520
1521                         //
1522                         // FIXME: This could return constants instead of EmptyCasts
1523                         //
1524                         if (target_type == TypeManager.sbyte_type){
1525                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1526                                         return new SByteConstant ((sbyte) value);
1527                         } else if (target_type == TypeManager.byte_type){
1528                                 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1529                                         return new ByteConstant ((byte) value);
1530                         } else if (target_type == TypeManager.short_type){
1531                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1532                                         return new ShortConstant ((short) value);
1533                         } else if (target_type == TypeManager.ushort_type){
1534                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1535                                         return new UShortConstant ((ushort) value);
1536                         } else if (target_type == TypeManager.uint32_type){
1537                                 if (value >= 0)
1538                                         return new UIntConstant ((uint) value);
1539                         } else if (target_type == TypeManager.uint64_type){
1540                                 //
1541                                 // we can optimize this case: a positive int32
1542                                 // always fits on a uint64.  But we need an opcode
1543                                 // to do it.
1544                                 //
1545                                 if (value >= 0)
1546                                         return new ULongConstant ((ulong) value);
1547                         }
1548                         
1549                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1550                                 return new EnumConstant (ic, target_type);
1551
1552                         return null;
1553                 }
1554
1555                 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1556                 {
1557                         string msg = "Cannot convert implicitly from `"+
1558                                 TypeManager.CSharpName (source) + "' to `" +
1559                                 TypeManager.CSharpName (target) + "'";
1560
1561                         Error (29, loc, msg);
1562                 }
1563
1564                 /// <summary>
1565                 ///   Attemptes to implicityly convert `target' into `type', using
1566                 ///   ConvertImplicit.  If there is no implicit conversion, then
1567                 ///   an error is signaled
1568                 /// </summary>
1569                 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1570                                                                   Type target_type, Location loc)
1571                 {
1572                         Expression e;
1573                         
1574                         e = ConvertImplicit (ec, source, target_type, loc);
1575                         if (e != null)
1576                                 return e;
1577
1578                         if (source is DoubleLiteral && target_type == TypeManager.float_type){
1579                                 Error (664, loc,
1580                                        "Double literal cannot be implicitly converted to " +
1581                                        "float type, use F suffix to create a float literal");
1582                         }
1583                         
1584                         Error_CannotConvertImplicit (loc, source.Type, target_type);
1585
1586                         return null;
1587                 }
1588
1589                 /// <summary>
1590                 ///   Performs the explicit numeric conversions
1591                 /// </summary>
1592                 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
1593                 {
1594                         Type expr_type = expr.Type;
1595
1596                         //
1597                         // If we have an enumeration, extract the underlying type,
1598                         // use this during the comparission, but wrap around the original
1599                         // target_type
1600                         //
1601                         Type real_target_type = target_type;
1602
1603                         if (TypeManager.IsEnumType (real_target_type))
1604                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1605
1606                         if (expr_type == TypeManager.sbyte_type){
1607                                 //
1608                                 // From sbyte to byte, ushort, uint, ulong, char
1609                                 //
1610                                 if (real_target_type == TypeManager.byte_type)
1611                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1612                                 if (real_target_type == TypeManager.ushort_type)
1613                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1614                                 if (real_target_type == TypeManager.uint32_type)
1615                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1616                                 if (real_target_type == TypeManager.uint64_type)
1617                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1618                                 if (real_target_type == TypeManager.char_type)
1619                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1620                         } else if (expr_type == TypeManager.byte_type){
1621                                 //
1622                                 // From byte to sbyte and char
1623                                 //
1624                                 if (real_target_type == TypeManager.sbyte_type)
1625                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1626                                 if (real_target_type == TypeManager.char_type)
1627                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1628                         } else if (expr_type == TypeManager.short_type){
1629                                 //
1630                                 // From short to sbyte, byte, ushort, uint, ulong, char
1631                                 //
1632                                 if (real_target_type == TypeManager.sbyte_type)
1633                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1634                                 if (real_target_type == TypeManager.byte_type)
1635                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1636                                 if (real_target_type == TypeManager.ushort_type)
1637                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1638                                 if (real_target_type == TypeManager.uint32_type)
1639                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1640                                 if (real_target_type == TypeManager.uint64_type)
1641                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1642                                 if (real_target_type == TypeManager.char_type)
1643                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1644                         } else if (expr_type == TypeManager.ushort_type){
1645                                 //
1646                                 // From ushort to sbyte, byte, short, char
1647                                 //
1648                                 if (real_target_type == TypeManager.sbyte_type)
1649                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1650                                 if (real_target_type == TypeManager.byte_type)
1651                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1652                                 if (real_target_type == TypeManager.short_type)
1653                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1654                                 if (real_target_type == TypeManager.char_type)
1655                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1656                         } else if (expr_type == TypeManager.int32_type){
1657                                 //
1658                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1659                                 //
1660                                 if (real_target_type == TypeManager.sbyte_type)
1661                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1662                                 if (real_target_type == TypeManager.byte_type)
1663                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1664                                 if (real_target_type == TypeManager.short_type)
1665                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1666                                 if (real_target_type == TypeManager.ushort_type)
1667                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1668                                 if (real_target_type == TypeManager.uint32_type)
1669                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1670                                 if (real_target_type == TypeManager.uint64_type)
1671                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1672                                 if (real_target_type == TypeManager.char_type)
1673                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1674                         } else if (expr_type == TypeManager.uint32_type){
1675                                 //
1676                                 // From uint to sbyte, byte, short, ushort, int, char
1677                                 //
1678                                 if (real_target_type == TypeManager.sbyte_type)
1679                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1680                                 if (real_target_type == TypeManager.byte_type)
1681                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1682                                 if (real_target_type == TypeManager.short_type)
1683                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1684                                 if (real_target_type == TypeManager.ushort_type)
1685                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1686                                 if (real_target_type == TypeManager.int32_type)
1687                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1688                                 if (real_target_type == TypeManager.char_type)
1689                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1690                         } else if (expr_type == TypeManager.int64_type){
1691                                 //
1692                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1693                                 //
1694                                 if (real_target_type == TypeManager.sbyte_type)
1695                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1696                                 if (real_target_type == TypeManager.byte_type)
1697                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1698                                 if (real_target_type == TypeManager.short_type)
1699                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1700                                 if (real_target_type == TypeManager.ushort_type)
1701                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1702                                 if (real_target_type == TypeManager.int32_type)
1703                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1704                                 if (real_target_type == TypeManager.uint32_type)
1705                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1706                                 if (real_target_type == TypeManager.uint64_type)
1707                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1708                                 if (real_target_type == TypeManager.char_type)
1709                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1710                         } else if (expr_type == TypeManager.uint64_type){
1711                                 //
1712                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1713                                 //
1714                                 if (real_target_type == TypeManager.sbyte_type)
1715                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1716                                 if (real_target_type == TypeManager.byte_type)
1717                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1718                                 if (real_target_type == TypeManager.short_type)
1719                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1720                                 if (real_target_type == TypeManager.ushort_type)
1721                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1722                                 if (real_target_type == TypeManager.int32_type)
1723                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1724                                 if (real_target_type == TypeManager.uint32_type)
1725                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1726                                 if (real_target_type == TypeManager.int64_type)
1727                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1728                                 if (real_target_type == TypeManager.char_type)
1729                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1730                         } else if (expr_type == TypeManager.char_type){
1731                                 //
1732                                 // From char to sbyte, byte, short
1733                                 //
1734                                 if (real_target_type == TypeManager.sbyte_type)
1735                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1736                                 if (real_target_type == TypeManager.byte_type)
1737                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1738                                 if (real_target_type == TypeManager.short_type)
1739                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1740                         } else if (expr_type == TypeManager.float_type){
1741                                 //
1742                                 // From float to sbyte, byte, short,
1743                                 // ushort, int, uint, long, ulong, char
1744                                 // or decimal
1745                                 //
1746                                 if (real_target_type == TypeManager.sbyte_type)
1747                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1748                                 if (real_target_type == TypeManager.byte_type)
1749                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1750                                 if (real_target_type == TypeManager.short_type)
1751                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1752                                 if (real_target_type == TypeManager.ushort_type)
1753                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1754                                 if (real_target_type == TypeManager.int32_type)
1755                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1756                                 if (real_target_type == TypeManager.uint32_type)
1757                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1758                                 if (real_target_type == TypeManager.int64_type)
1759                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1760                                 if (real_target_type == TypeManager.uint64_type)
1761                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1762                                 if (real_target_type == TypeManager.char_type)
1763                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1764                                 if (real_target_type == TypeManager.decimal_type)
1765                                         return InternalTypeConstructor (ec, expr, target_type);
1766                         } else if (expr_type == TypeManager.double_type){
1767                                 //
1768                                 // From double to byte, byte, short,
1769                                 // ushort, int, uint, long, ulong,
1770                                 // char, float or decimal
1771                                 //
1772                                 if (real_target_type == TypeManager.sbyte_type)
1773                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1774                                 if (real_target_type == TypeManager.byte_type)
1775                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1776                                 if (real_target_type == TypeManager.short_type)
1777                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1778                                 if (real_target_type == TypeManager.ushort_type)
1779                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1780                                 if (real_target_type == TypeManager.int32_type)
1781                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1782                                 if (real_target_type == TypeManager.uint32_type)
1783                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1784                                 if (real_target_type == TypeManager.int64_type)
1785                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1786                                 if (real_target_type == TypeManager.uint64_type)
1787                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1788                                 if (real_target_type == TypeManager.char_type)
1789                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1790                                 if (real_target_type == TypeManager.float_type)
1791                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1792                                 if (real_target_type == TypeManager.decimal_type)
1793                                         return InternalTypeConstructor (ec, expr, target_type);
1794                         } 
1795
1796                         // decimal is taken care of by the op_Explicit methods.
1797
1798                         return null;
1799                 }
1800
1801                 /// <summary>
1802                 ///  Returns whether an explicit reference conversion can be performed
1803                 ///  from source_type to target_type
1804                 /// </summary>
1805                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1806                 {
1807                         bool target_is_value_type = target_type.IsValueType;
1808                         
1809                         if (source_type == target_type)
1810                                 return true;
1811                         
1812                         //
1813                         // From object to any reference type
1814                         //
1815                         if (source_type == TypeManager.object_type && !target_is_value_type)
1816                                 return true;
1817                                         
1818                         //
1819                         // From any class S to any class-type T, provided S is a base class of T
1820                         //
1821                         if (target_type.IsSubclassOf (source_type))
1822                                 return true;
1823
1824                         //
1825                         // From any interface type S to any interface T provided S is not derived from T
1826                         //
1827                         if (source_type.IsInterface && target_type.IsInterface){
1828                                 if (!target_type.IsSubclassOf (source_type))
1829                                         return true;
1830                         }
1831                             
1832                         //
1833                         // From any class type S to any interface T, provided S is not sealed
1834                         // and provided S does not implement T.
1835                         //
1836                         if (target_type.IsInterface && !source_type.IsSealed &&
1837                             !TypeManager.ImplementsInterface (source_type, target_type))
1838                                 return true;
1839
1840                         //
1841                         // From any interface-type S to to any class type T, provided T is not
1842                         // sealed, or provided T implements S.
1843                         //
1844                         if (source_type.IsInterface &&
1845                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1846                                 return true;
1847                         
1848                         
1849                         // From an array type S with an element type Se to an array type T with an 
1850                         // element type Te provided all the following are true:
1851                         //     * S and T differe only in element type, in other words, S and T
1852                         //       have the same number of dimensions.
1853                         //     * Both Se and Te are reference types
1854                         //     * An explicit referenc conversions exist from Se to Te
1855                         //
1856                         if (source_type.IsArray && target_type.IsArray) {
1857                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1858                                         
1859                                         Type source_element_type = source_type.GetElementType ();
1860                                         Type target_element_type = target_type.GetElementType ();
1861                                         
1862                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1863                                                 if (ExplicitReferenceConversionExists (source_element_type,
1864                                                                                        target_element_type))
1865                                                         return true;
1866                                 }
1867                         }
1868                         
1869
1870                         // From System.Array to any array-type
1871                         if (source_type == TypeManager.array_type &&
1872                             target_type.IsArray){
1873                                 return true;
1874                         }
1875
1876                         //
1877                         // From System delegate to any delegate-type
1878                         //
1879                         if (source_type == TypeManager.delegate_type &&
1880                             target_type.IsSubclassOf (TypeManager.delegate_type))
1881                                 return true;
1882
1883                         //
1884                         // From ICloneable to Array or Delegate types
1885                         //
1886                         if (source_type == TypeManager.icloneable_type &&
1887                             (target_type == TypeManager.array_type ||
1888                              target_type == TypeManager.delegate_type))
1889                                 return true;
1890                         
1891                         return false;
1892                 }
1893
1894                 /// <summary>
1895                 ///   Implements Explicit Reference conversions
1896                 /// </summary>
1897                 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1898                 {
1899                         Type source_type = source.Type;
1900                         bool target_is_value_type = target_type.IsValueType;
1901
1902                         //
1903                         // From object to any reference type
1904                         //
1905                         if (source_type == TypeManager.object_type && !target_is_value_type)
1906                                 return new ClassCast (source, target_type);
1907
1908
1909                         //
1910                         // From any class S to any class-type T, provided S is a base class of T
1911                         //
1912                         if (target_type.IsSubclassOf (source_type))
1913                                 return new ClassCast (source, target_type);
1914
1915                         //
1916                         // From any interface type S to any interface T provided S is not derived from T
1917                         //
1918                         if (source_type.IsInterface && target_type.IsInterface){
1919                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1920                                         return null;
1921                                 else
1922                                         return new ClassCast (source, target_type);
1923                         }
1924                             
1925                         //
1926                         // From any class type S to any interface T, provides S is not sealed
1927                         // and provided S does not implement T.
1928                         //
1929                         if (target_type.IsInterface && !source_type.IsSealed) {
1930                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1931                                         return null;
1932                                 else
1933                                         return new ClassCast (source, target_type);
1934                                 
1935                         }
1936
1937                         //
1938                         // From any interface-type S to to any class type T, provided T is not
1939                         // sealed, or provided T implements S.
1940                         //
1941                         if (source_type.IsInterface) {
1942                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
1943                                         return new ClassCast (source, target_type);
1944                                 else
1945                                         return null;
1946                         }
1947                         
1948                         // From an array type S with an element type Se to an array type T with an 
1949                         // element type Te provided all the following are true:
1950                         //     * S and T differe only in element type, in other words, S and T
1951                         //       have the same number of dimensions.
1952                         //     * Both Se and Te are reference types
1953                         //     * An explicit referenc conversions exist from Se to Te
1954                         //
1955                         if (source_type.IsArray && target_type.IsArray) {
1956                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1957                                         
1958                                         Type source_element_type = source_type.GetElementType ();
1959                                         Type target_element_type = target_type.GetElementType ();
1960                                         
1961                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1962                                                 if (ExplicitReferenceConversionExists (source_element_type,
1963                                                                                        target_element_type))
1964                                                         return new ClassCast (source, target_type);
1965                                 }
1966                         }
1967                         
1968
1969                         // From System.Array to any array-type
1970                         if (source_type == TypeManager.array_type &&
1971                             target_type.IsArray) {
1972                                 return new ClassCast (source, target_type);
1973                         }
1974
1975                         //
1976                         // From System delegate to any delegate-type
1977                         //
1978                         if (source_type == TypeManager.delegate_type &&
1979                             target_type.IsSubclassOf (TypeManager.delegate_type))
1980                                 return new ClassCast (source, target_type);
1981
1982                         //
1983                         // From ICloneable to Array or Delegate types
1984                         //
1985                         if (source_type == TypeManager.icloneable_type &&
1986                             (target_type == TypeManager.array_type ||
1987                              target_type == TypeManager.delegate_type))
1988                                 return new ClassCast (source, target_type);
1989                         
1990                         return null;
1991                 }
1992                 
1993                 /// <summary>
1994                 ///   Performs an explicit conversion of the expression `expr' whose
1995                 ///   type is expr.Type to `target_type'.
1996                 /// </summary>
1997                 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1998                                                           Type target_type, Location loc)
1999                 {
2000                         Type expr_type = expr.Type;
2001                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2002
2003                         if (ne != null)
2004                                 return ne;
2005
2006                         ne = ConvertNumericExplicit (ec, expr, target_type);
2007                         if (ne != null)
2008                                 return ne;
2009
2010                         //
2011                         // Unboxing conversion.
2012                         //
2013                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
2014                                 return new UnboxCast (expr, target_type);
2015
2016                         //
2017                         // Enum types
2018                         //
2019                         if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2020                                 Expression e;
2021
2022                                 //
2023                                 // FIXME: Is there any reason we should have EnumConstant
2024                                 // dealt with here instead of just using always the
2025                                 // UnderlyingSystemType to wrap the type?
2026                                 //
2027                                 if (expr is EnumConstant)
2028                                         e = ((EnumConstant) expr).Child;
2029                                 else {
2030                                         e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2031                                 }
2032                                 
2033                                 Expression t = ConvertImplicit (ec, e, target_type, loc);
2034                                 if (t != null)
2035                                         return t;
2036                                 
2037                                 return ConvertNumericExplicit (ec, e, target_type);
2038                         }
2039                         
2040                         ne = ConvertReferenceExplicit (expr, target_type);
2041                         if (ne != null)
2042                                 return ne;
2043
2044                         if (ec.InUnsafe){
2045                                 if (target_type.IsPointer){
2046                                         if (expr_type.IsPointer)
2047                                                 return new EmptyCast (expr, target_type);
2048                                         
2049                                         if (expr_type == TypeManager.sbyte_type ||
2050                                             expr_type == TypeManager.byte_type ||
2051                                             expr_type == TypeManager.short_type ||
2052                                             expr_type == TypeManager.ushort_type ||
2053                                             expr_type == TypeManager.int32_type ||
2054                                             expr_type == TypeManager.uint32_type ||
2055                                             expr_type == TypeManager.uint64_type ||
2056                                             expr_type == TypeManager.int64_type)
2057                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2058                                 }
2059                                 if (expr_type.IsPointer){
2060                                         if (target_type == TypeManager.sbyte_type ||
2061                                             target_type == TypeManager.byte_type ||
2062                                             target_type == TypeManager.short_type ||
2063                                             target_type == TypeManager.ushort_type ||
2064                                             target_type == TypeManager.int32_type ||
2065                                             target_type == TypeManager.uint32_type ||
2066                                             target_type == TypeManager.uint64_type ||
2067                                             target_type == TypeManager.int64_type){
2068                                                 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2069                                                 Expression ci, ce;
2070
2071                                                 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2072
2073                                                 if (ci != null)
2074                                                         return ci;
2075
2076                                                 ce = ConvertNumericExplicit (ec, e, target_type);
2077                                                 if (ce != null)
2078                                                         return ce;
2079                                                 //
2080                                                 // We should always be able to go from an uint32
2081                                                 // implicitly or explicitly to the other integral
2082                                                 // types
2083                                                 //
2084                                                 throw new Exception ("Internal compiler error");
2085                                         }
2086                                 }
2087                         }
2088                         
2089                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
2090                         if (ne != null)
2091                                 return ne;
2092
2093                         Error_CannotConvertType (loc, expr_type, target_type);
2094                         return null;
2095                 }
2096
2097                 /// <summary>
2098                 ///   Same as ConvertExplicit, only it doesn't include user defined conversions
2099                 /// </summary>
2100                 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2101                                                                   Type target_type, Location l)
2102                 {
2103                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2104
2105                         if (ne != null)
2106                                 return ne;
2107
2108                         ne = ConvertNumericExplicit (ec, expr, target_type);
2109                         if (ne != null)
2110                                 return ne;
2111
2112                         ne = ConvertReferenceExplicit (expr, target_type);
2113                         if (ne != null)
2114                                 return ne;
2115
2116                         Error_CannotConvertType (l, expr.Type, target_type);
2117                         return null;
2118                 }
2119
2120                 static string ExprClassName (ExprClass c)
2121                 {
2122                         switch (c){
2123                         case ExprClass.Invalid:
2124                                 return "Invalid";
2125                         case ExprClass.Value:
2126                                 return "value";
2127                         case ExprClass.Variable:
2128                                 return "variable";
2129                         case ExprClass.Namespace:
2130                                 return "namespace";
2131                         case ExprClass.Type:
2132                                 return "type";
2133                         case ExprClass.MethodGroup:
2134                                 return "method group";
2135                         case ExprClass.PropertyAccess:
2136                                 return "property access";
2137                         case ExprClass.EventAccess:
2138                                 return "event access";
2139                         case ExprClass.IndexerAccess:
2140                                 return "indexer access";
2141                         case ExprClass.Nothing:
2142                                 return "null";
2143                         }
2144                         throw new Exception ("Should not happen");
2145                 }
2146                 
2147                 /// <summary>
2148                 ///   Reports that we were expecting `expr' to be of class `expected'
2149                 /// </summary>
2150                 protected void report118 (Location loc, Expression expr, string expected)
2151                 {
2152                         string kind = "Unknown";
2153                         
2154                         if (expr != null)
2155                                 kind = ExprClassName (expr.eclass);
2156
2157                         Error (118, loc, "Expression denotes a `" + kind +
2158                                "' where a `" + expected + "' was expected");
2159                 }
2160
2161                 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2162                 {
2163                         Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2164                                       TypeManager.CSharpName (t));
2165                 }
2166
2167                 public static void UnsafeError (Location loc)
2168                 {
2169                         Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2170                 }
2171                 
2172                 /// <summary>
2173                 ///   Converts the IntConstant, UIntConstant, LongConstant or
2174                 ///   ULongConstant into the integral target_type.   Notice
2175                 ///   that we do not return an `Expression' we do return
2176                 ///   a boxed integral type.
2177                 ///
2178                 ///   FIXME: Since I added the new constants, we need to
2179                 ///   also support conversions from CharConstant, ByteConstant,
2180                 ///   SByteConstant, UShortConstant, ShortConstant
2181                 ///
2182                 ///   This is used by the switch statement, so the domain
2183                 ///   of work is restricted to the literals above, and the
2184                 ///   targets are int32, uint32, char, byte, sbyte, ushort,
2185                 ///   short, uint64 and int64
2186                 /// </summary>
2187                 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2188                 {
2189                         string s = "";
2190
2191                         if (c.Type == target_type)
2192                                 return ((Constant) c).GetValue ();
2193
2194                         //
2195                         // Make into one of the literals we handle, we dont really care
2196                         // about this value as we will just return a few limited types
2197                         // 
2198                         if (c is EnumConstant)
2199                                 c = ((EnumConstant)c).WidenToCompilerConstant ();
2200
2201                         if (c is IntConstant){
2202                                 int v = ((IntConstant) c).Value;
2203                                 
2204                                 if (target_type == TypeManager.uint32_type){
2205                                         if (v >= 0)
2206                                                 return (uint) v;
2207                                 } else if (target_type == TypeManager.char_type){
2208                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2209                                                 return (char) v;
2210                                 } else if (target_type == TypeManager.byte_type){
2211                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2212                                                 return (byte) v;
2213                                 } else if (target_type == TypeManager.sbyte_type){
2214                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2215                                                 return (sbyte) v;
2216                                 } else if (target_type == TypeManager.short_type){
2217                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2218                                                 return (short) v;
2219                                 } else if (target_type == TypeManager.ushort_type){
2220                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2221                                                 return (ushort) v;
2222                                 } else if (target_type == TypeManager.int64_type)
2223                                         return (long) v;
2224                                 else if (target_type == TypeManager.uint64_type){
2225                                         if (v > 0)
2226                                                 return (ulong) v;
2227                                 }
2228
2229                                 s = v.ToString ();
2230                         } else if (c is UIntConstant){
2231                                 uint v = ((UIntConstant) c).Value;
2232
2233                                 if (target_type == TypeManager.int32_type){
2234                                         if (v <= Int32.MaxValue)
2235                                                 return (int) v;
2236                                 } else if (target_type == TypeManager.char_type){
2237                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2238                                                 return (char) v;
2239                                 } else if (target_type == TypeManager.byte_type){
2240                                         if (v <= Byte.MaxValue)
2241                                                 return (byte) v;
2242                                 } else if (target_type == TypeManager.sbyte_type){
2243                                         if (v <= SByte.MaxValue)
2244                                                 return (sbyte) v;
2245                                 } else if (target_type == TypeManager.short_type){
2246                                         if (v <= UInt16.MaxValue)
2247                                                 return (short) v;
2248                                 } else if (target_type == TypeManager.ushort_type){
2249                                         if (v <= UInt16.MaxValue)
2250                                                 return (ushort) v;
2251                                 } else if (target_type == TypeManager.int64_type)
2252                                         return (long) v;
2253                                 else if (target_type == TypeManager.uint64_type)
2254                                         return (ulong) v;
2255                                 s = v.ToString ();
2256                         } else if (c is LongConstant){ 
2257                                 long v = ((LongConstant) c).Value;
2258
2259                                 if (target_type == TypeManager.int32_type){
2260                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2261                                                 return (int) v;
2262                                 } else if (target_type == TypeManager.uint32_type){
2263                                         if (v >= 0 && v <= UInt32.MaxValue)
2264                                                 return (uint) v;
2265                                 } else if (target_type == TypeManager.char_type){
2266                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2267                                                 return (char) v;
2268                                 } else if (target_type == TypeManager.byte_type){
2269                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2270                                                 return (byte) v;
2271                                 } else if (target_type == TypeManager.sbyte_type){
2272                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2273                                                 return (sbyte) v;
2274                                 } else if (target_type == TypeManager.short_type){
2275                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2276                                                 return (short) v;
2277                                 } else if (target_type == TypeManager.ushort_type){
2278                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2279                                                 return (ushort) v;
2280                                 } else if (target_type == TypeManager.uint64_type){
2281                                         if (v > 0)
2282                                                 return (ulong) v;
2283                                 }
2284                                 s = v.ToString ();
2285                         } else if (c is ULongConstant){
2286                                 ulong v = ((ULongConstant) c).Value;
2287
2288                                 if (target_type == TypeManager.int32_type){
2289                                         if (v <= Int32.MaxValue)
2290                                                 return (int) v;
2291                                 } else if (target_type == TypeManager.uint32_type){
2292                                         if (v <= UInt32.MaxValue)
2293                                                 return (uint) v;
2294                                 } else if (target_type == TypeManager.char_type){
2295                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2296                                                 return (char) v;
2297                                 } else if (target_type == TypeManager.byte_type){
2298                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2299                                                 return (byte) v;
2300                                 } else if (target_type == TypeManager.sbyte_type){
2301                                         if (v <= (int) SByte.MaxValue)
2302                                                 return (sbyte) v;
2303                                 } else if (target_type == TypeManager.short_type){
2304                                         if (v <= UInt16.MaxValue)
2305                                                 return (short) v;
2306                                 } else if (target_type == TypeManager.ushort_type){
2307                                         if (v <= UInt16.MaxValue)
2308                                                 return (ushort) v;
2309                                 } else if (target_type == TypeManager.int64_type){
2310                                         if (v <= Int64.MaxValue)
2311                                                 return (long) v;
2312                                 }
2313                                 s = v.ToString ();
2314                         } else if (c is ByteConstant){
2315                                 byte v = ((ByteConstant) c).Value;
2316                                 
2317                                 if (target_type == TypeManager.int32_type)
2318                                         return (int) v;
2319                                 else if (target_type == TypeManager.uint32_type)
2320                                         return (uint) v;
2321                                 else if (target_type == TypeManager.char_type)
2322                                         return (char) v;
2323                                 else if (target_type == TypeManager.sbyte_type){
2324                                         if (v <= SByte.MaxValue)
2325                                                 return (sbyte) v;
2326                                 } else if (target_type == TypeManager.short_type)
2327                                         return (short) v;
2328                                 else if (target_type == TypeManager.ushort_type)
2329                                         return (ushort) v;
2330                                 else if (target_type == TypeManager.int64_type)
2331                                         return (long) v;
2332                                 else if (target_type == TypeManager.uint64_type)
2333                                         return (ulong) v;
2334                                 s = v.ToString ();
2335                         } else if (c is SByteConstant){
2336                                 sbyte v = ((SByteConstant) c).Value;
2337                                 
2338                                 if (target_type == TypeManager.int32_type)
2339                                         return (int) v;
2340                                 else if (target_type == TypeManager.uint32_type){
2341                                         if (v >= 0)
2342                                                 return (uint) v;
2343                                 } else if (target_type == TypeManager.char_type){
2344                                         if (v >= 0)
2345                                                 return (char) v;
2346                                 } else if (target_type == TypeManager.byte_type){
2347                                         if (v >= 0)
2348                                                 return (byte) v;
2349                                 } else if (target_type == TypeManager.short_type)
2350                                         return (short) v;
2351                                 else if (target_type == TypeManager.ushort_type){
2352                                         if (v >= 0)
2353                                                 return (ushort) v;
2354                                 } else if (target_type == TypeManager.int64_type)
2355                                         return (long) v;
2356                                 else if (target_type == TypeManager.uint64_type){
2357                                         if (v >= 0)
2358                                                 return (ulong) v;
2359                                 }
2360                                 s = v.ToString ();
2361                         } else if (c is ShortConstant){
2362                                 short v = ((ShortConstant) c).Value;
2363                                 
2364                                 if (target_type == TypeManager.int32_type){
2365                                         return (int) v;
2366                                 } else if (target_type == TypeManager.uint32_type){
2367                                         if (v >= 0)
2368                                                 return (uint) v;
2369                                 } else if (target_type == TypeManager.char_type){
2370                                         if (v >= 0)
2371                                                 return (char) v;
2372                                 } else if (target_type == TypeManager.byte_type){
2373                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2374                                                 return (byte) v;
2375                                 } else if (target_type == TypeManager.sbyte_type){
2376                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2377                                                 return (sbyte) v;
2378                                 } else if (target_type == TypeManager.ushort_type){
2379                                         if (v >= 0)
2380                                                 return (ushort) v;
2381                                 } else if (target_type == TypeManager.int64_type)
2382                                         return (long) v;
2383                                 else if (target_type == TypeManager.uint64_type)
2384                                         return (ulong) v;
2385
2386                                 s = v.ToString ();
2387                         } else if (c is UShortConstant){
2388                                 ushort v = ((UShortConstant) c).Value;
2389                                 
2390                                 if (target_type == TypeManager.int32_type)
2391                                         return (int) v;
2392                                 else if (target_type == TypeManager.uint32_type)
2393                                         return (uint) v;
2394                                 else if (target_type == TypeManager.char_type){
2395                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2396                                                 return (char) v;
2397                                 } else if (target_type == TypeManager.byte_type){
2398                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2399                                                 return (byte) v;
2400                                 } else if (target_type == TypeManager.sbyte_type){
2401                                         if (v <= SByte.MaxValue)
2402                                                 return (byte) v;
2403                                 } else if (target_type == TypeManager.short_type){
2404                                         if (v <= Int16.MaxValue)
2405                                                 return (short) v;
2406                                 } else if (target_type == TypeManager.int64_type)
2407                                         return (long) v;
2408                                 else if (target_type == TypeManager.uint64_type)
2409                                         return (ulong) v;
2410
2411                                 s = v.ToString ();
2412                         } else if (c is CharConstant){
2413                                 char v = ((CharConstant) c).Value;
2414                                 
2415                                 if (target_type == TypeManager.int32_type)
2416                                         return (int) v;
2417                                 else if (target_type == TypeManager.uint32_type)
2418                                         return (uint) v;
2419                                 else if (target_type == TypeManager.byte_type){
2420                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2421                                                 return (byte) v;
2422                                 } else if (target_type == TypeManager.sbyte_type){
2423                                         if (v <= SByte.MaxValue)
2424                                                 return (sbyte) v;
2425                                 } else if (target_type == TypeManager.short_type){
2426                                         if (v <= Int16.MaxValue)
2427                                                 return (short) v;
2428                                 } else if (target_type == TypeManager.ushort_type)
2429                                         return (short) v;
2430                                 else if (target_type == TypeManager.int64_type)
2431                                         return (long) v;
2432                                 else if (target_type == TypeManager.uint64_type)
2433                                         return (ulong) v;
2434
2435                                 s = v.ToString ();
2436                         }
2437                         Error_ConstantValueCannotBeConverted (loc, s, target_type);
2438                         return null;
2439                 }
2440
2441                 //
2442                 // Load the object from the pointer.  
2443                 //
2444                 public static void LoadFromPtr (ILGenerator ig, Type t)
2445                 {
2446                         if (t == TypeManager.int32_type)
2447                                 ig.Emit (OpCodes.Ldind_I4);
2448                         else if (t == TypeManager.uint32_type)
2449                                 ig.Emit (OpCodes.Ldind_U4);
2450                         else if (t == TypeManager.short_type)
2451                                 ig.Emit (OpCodes.Ldind_I2);
2452                         else if (t == TypeManager.ushort_type)
2453                                 ig.Emit (OpCodes.Ldind_U2);
2454                         else if (t == TypeManager.char_type)
2455                                 ig.Emit (OpCodes.Ldind_U2);
2456                         else if (t == TypeManager.byte_type)
2457                                 ig.Emit (OpCodes.Ldind_U1);
2458                         else if (t == TypeManager.sbyte_type)
2459                                 ig.Emit (OpCodes.Ldind_I1);
2460                         else if (t == TypeManager.uint64_type)
2461                                 ig.Emit (OpCodes.Ldind_I8);
2462                         else if (t == TypeManager.int64_type)
2463                                 ig.Emit (OpCodes.Ldind_I8);
2464                         else if (t == TypeManager.float_type)
2465                                 ig.Emit (OpCodes.Ldind_R4);
2466                         else if (t == TypeManager.double_type)
2467                                 ig.Emit (OpCodes.Ldind_R8);
2468                         else if (t == TypeManager.bool_type)
2469                                 ig.Emit (OpCodes.Ldind_I1);
2470                         else if (t == TypeManager.intptr_type)
2471                                 ig.Emit (OpCodes.Ldind_I);
2472                         else if (TypeManager.IsEnumType (t)) {
2473                                 if (t == TypeManager.enum_type)
2474                                         ig.Emit (OpCodes.Ldind_Ref);
2475                                 else
2476                                         LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2477                         } else if (t.IsValueType)
2478                                 ig.Emit (OpCodes.Ldobj, t);
2479                         else
2480                                 ig.Emit (OpCodes.Ldind_Ref);
2481                 }
2482
2483                 //
2484                 // The stack contains the pointer and the value of type `type'
2485                 //
2486                 public static void StoreFromPtr (ILGenerator ig, Type type)
2487                 {
2488                         if (type.IsEnum)
2489                                 type = TypeManager.EnumToUnderlying (type);
2490                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2491                                 ig.Emit (OpCodes.Stind_I4);
2492                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2493                                 ig.Emit (OpCodes.Stind_I8);
2494                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2495                                  type == TypeManager.ushort_type)
2496                                 ig.Emit (OpCodes.Stind_I2);
2497                         else if (type == TypeManager.float_type)
2498                                 ig.Emit (OpCodes.Stind_R4);
2499                         else if (type == TypeManager.double_type)
2500                                 ig.Emit (OpCodes.Stind_R8);
2501                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2502                                  type == TypeManager.bool_type)
2503                                 ig.Emit (OpCodes.Stind_I1);
2504                         else if (type == TypeManager.intptr_type)
2505                                 ig.Emit (OpCodes.Stind_I);
2506                         else if (type.IsValueType)
2507                                 ig.Emit (OpCodes.Stobj, type);
2508                         else
2509                                 ig.Emit (OpCodes.Stind_Ref);
2510                 }
2511                 
2512                 //
2513                 // Returns the size of type `t' if known, otherwise, 0
2514                 //
2515                 public static int GetTypeSize (Type t)
2516                 {
2517                         t = TypeManager.TypeToCoreType (t);
2518                         if (t == TypeManager.int32_type ||
2519                             t == TypeManager.uint32_type ||
2520                             t == TypeManager.float_type)
2521                                 return 4;
2522                         else if (t == TypeManager.int64_type ||
2523                                  t == TypeManager.uint64_type ||
2524                                  t == TypeManager.double_type)
2525                                 return 8;
2526                         else if (t == TypeManager.byte_type ||
2527                                  t == TypeManager.sbyte_type ||
2528                                  t == TypeManager.bool_type)    
2529                                 return 1;
2530                         else if (t == TypeManager.short_type ||
2531                                  t == TypeManager.char_type ||
2532                                  t == TypeManager.ushort_type)
2533                                 return 2;
2534                         else
2535                                 return 0;
2536                 }
2537
2538                 //
2539                 // Default implementation of IAssignMethod.CacheTemporaries
2540                 //
2541                 public void CacheTemporaries (EmitContext ec)
2542                 {
2543                 }
2544
2545                 static void Error_NegativeArrayIndex (Location loc)
2546                 {
2547                         Report.Error (284, loc, "Can not create array with a negative size");
2548                 }
2549                 
2550                 //
2551                 // Converts `source' to an int, uint, long or ulong.
2552                 //
2553                 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2554                 {
2555                         Expression target;
2556                         
2557                         bool old_checked = ec.CheckState;
2558                         ec.CheckState = true;
2559                         
2560                         target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2561                         if (target == null){
2562                                 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2563                                 if (target == null){
2564                                         target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2565                                         if (target == null){
2566                                                 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2567                                                 if (target == null)
2568                                                         Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2569                                         }
2570                                 }
2571                         } 
2572                         ec.CheckState = old_checked;
2573
2574                         //
2575                         // Only positive constants are allowed at compile time
2576                         //
2577                         if (target is Constant){
2578                                 if (target is IntConstant){
2579                                         if (((IntConstant) target).Value < 0){
2580                                                 Error_NegativeArrayIndex (loc);
2581                                                 return null;
2582                                         }
2583                                 }
2584
2585                                 if (target is LongConstant){
2586                                         if (((LongConstant) target).Value < 0){
2587                                                 Error_NegativeArrayIndex (loc);
2588                                                 return null;
2589                                         }
2590                                 }
2591                                 
2592                         }
2593
2594                         return target;
2595                 }
2596                 
2597         }
2598
2599         /// <summary>
2600         ///   This is just a base class for expressions that can
2601         ///   appear on statements (invocations, object creation,
2602         ///   assignments, post/pre increment and decrement).  The idea
2603         ///   being that they would support an extra Emition interface that
2604         ///   does not leave a result on the stack.
2605         /// </summary>
2606         public abstract class ExpressionStatement : Expression {
2607
2608                 /// <summary>
2609                 ///   Requests the expression to be emitted in a `statement'
2610                 ///   context.  This means that no new value is left on the
2611                 ///   stack after invoking this method (constrasted with
2612                 ///   Emit that will always leave a value on the stack).
2613                 /// </summary>
2614                 public abstract void EmitStatement (EmitContext ec);
2615         }
2616
2617         /// <summary>
2618         ///   This kind of cast is used to encapsulate the child
2619         ///   whose type is child.Type into an expression that is
2620         ///   reported to return "return_type".  This is used to encapsulate
2621         ///   expressions which have compatible types, but need to be dealt
2622         ///   at higher levels with.
2623         ///
2624         ///   For example, a "byte" expression could be encapsulated in one
2625         ///   of these as an "unsigned int".  The type for the expression
2626         ///   would be "unsigned int".
2627         ///
2628         /// </summary>
2629         public class EmptyCast : Expression {
2630                 protected Expression child;
2631
2632                 public EmptyCast (Expression child, Type return_type)
2633                 {
2634                         eclass = child.eclass;
2635                         type = return_type;
2636                         this.child = child;
2637                 }
2638
2639                 public override Expression DoResolve (EmitContext ec)
2640                 {
2641                         // This should never be invoked, we are born in fully
2642                         // initialized state.
2643
2644                         return this;
2645                 }
2646
2647                 public override void Emit (EmitContext ec)
2648                 {
2649                         child.Emit (ec);
2650                 }
2651         }
2652
2653         /// <summary>
2654         ///  This class is used to wrap literals which belong inside Enums
2655         /// </summary>
2656         public class EnumConstant : Constant {
2657                 public Constant Child;
2658
2659                 public EnumConstant (Constant child, Type enum_type)
2660                 {
2661                         eclass = child.eclass;
2662                         this.Child = child;
2663                         type = enum_type;
2664                 }
2665                 
2666                 public override Expression DoResolve (EmitContext ec)
2667                 {
2668                         // This should never be invoked, we are born in fully
2669                         // initialized state.
2670
2671                         return this;
2672                 }
2673
2674                 public override void Emit (EmitContext ec)
2675                 {
2676                         Child.Emit (ec);
2677                 }
2678
2679                 public override object GetValue ()
2680                 {
2681                         return Child.GetValue ();
2682                 }
2683
2684                 //
2685                 // Converts from one of the valid underlying types for an enumeration
2686                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2687                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2688                 //
2689                 public Constant WidenToCompilerConstant ()
2690                 {
2691                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2692                         object v = ((Constant) Child).GetValue ();;
2693                         
2694                         if (t == TypeManager.int32_type)
2695                                 return new IntConstant ((int) v);
2696                         if (t == TypeManager.uint32_type)
2697                                 return new UIntConstant ((uint) v);
2698                         if (t == TypeManager.int64_type)
2699                                 return new LongConstant ((long) v);
2700                         if (t == TypeManager.uint64_type)
2701                                 return new ULongConstant ((ulong) v);
2702                         if (t == TypeManager.short_type)
2703                                 return new ShortConstant ((short) v);
2704                         if (t == TypeManager.ushort_type)
2705                                 return new UShortConstant ((ushort) v);
2706                         if (t == TypeManager.byte_type)
2707                                 return new ByteConstant ((byte) v);
2708                         if (t == TypeManager.sbyte_type)
2709                                 return new SByteConstant ((sbyte) v);
2710
2711                         throw new Exception ("Invalid enumeration underlying type: " + t);
2712                 }
2713
2714                 //
2715                 // Extracts the value in the enumeration on its native representation
2716                 //
2717                 public object GetPlainValue ()
2718                 {
2719                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2720                         object v = ((Constant) Child).GetValue ();;
2721                         
2722                         if (t == TypeManager.int32_type)
2723                                 return (int) v;
2724                         if (t == TypeManager.uint32_type)
2725                                 return (uint) v;
2726                         if (t == TypeManager.int64_type)
2727                                 return (long) v;
2728                         if (t == TypeManager.uint64_type)
2729                                 return (ulong) v;
2730                         if (t == TypeManager.short_type)
2731                                 return (short) v;
2732                         if (t == TypeManager.ushort_type)
2733                                 return (ushort) v;
2734                         if (t == TypeManager.byte_type)
2735                                 return (byte) v;
2736                         if (t == TypeManager.sbyte_type)
2737                                 return (sbyte) v;
2738
2739                         return null;
2740                 }
2741                 
2742                 public override string AsString ()
2743                 {
2744                         return Child.AsString ();
2745                 }
2746
2747                 public override DoubleConstant ConvertToDouble ()
2748                 {
2749                         return Child.ConvertToDouble ();
2750                 }
2751
2752                 public override FloatConstant ConvertToFloat ()
2753                 {
2754                         return Child.ConvertToFloat ();
2755                 }
2756
2757                 public override ULongConstant ConvertToULong ()
2758                 {
2759                         return Child.ConvertToULong ();
2760                 }
2761
2762                 public override LongConstant ConvertToLong ()
2763                 {
2764                         return Child.ConvertToLong ();
2765                 }
2766
2767                 public override UIntConstant ConvertToUInt ()
2768                 {
2769                         return Child.ConvertToUInt ();
2770                 }
2771
2772                 public override IntConstant ConvertToInt ()
2773                 {
2774                         return Child.ConvertToInt ();
2775                 }
2776         }
2777
2778         /// <summary>
2779         ///   This kind of cast is used to encapsulate Value Types in objects.
2780         ///
2781         ///   The effect of it is to box the value type emitted by the previous
2782         ///   operation.
2783         /// </summary>
2784         public class BoxedCast : EmptyCast {
2785
2786                 public BoxedCast (Expression expr)
2787                         : base (expr, TypeManager.object_type)
2788                 {
2789                 }
2790
2791                 public override Expression DoResolve (EmitContext ec)
2792                 {
2793                         // This should never be invoked, we are born in fully
2794                         // initialized state.
2795
2796                         return this;
2797                 }
2798
2799                 public override void Emit (EmitContext ec)
2800                 {
2801                         base.Emit (ec);
2802                         
2803                         ec.ig.Emit (OpCodes.Box, child.Type);
2804                 }
2805         }
2806
2807         public class UnboxCast : EmptyCast {
2808                 public UnboxCast (Expression expr, Type return_type)
2809                         : base (expr, return_type)
2810                 {
2811                 }
2812
2813                 public override Expression DoResolve (EmitContext ec)
2814                 {
2815                         // This should never be invoked, we are born in fully
2816                         // initialized state.
2817
2818                         return this;
2819                 }
2820
2821                 public override void Emit (EmitContext ec)
2822                 {
2823                         Type t = type;
2824                         ILGenerator ig = ec.ig;
2825                         
2826                         base.Emit (ec);
2827                         ig.Emit (OpCodes.Unbox, t);
2828
2829                         LoadFromPtr (ig, t);
2830                 }
2831         }
2832         
2833         /// <summary>
2834         ///   This is used to perform explicit numeric conversions.
2835         ///
2836         ///   Explicit numeric conversions might trigger exceptions in a checked
2837         ///   context, so they should generate the conv.ovf opcodes instead of
2838         ///   conv opcodes.
2839         /// </summary>
2840         public class ConvCast : EmptyCast {
2841                 public enum Mode : byte {
2842                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2843                         U1_I1, U1_CH,
2844                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2845                         U2_I1, U2_U1, U2_I2, U2_CH,
2846                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2847                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2848                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2849                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2850                         CH_I1, CH_U1, CH_I2,
2851                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2852                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2853                 }
2854
2855                 Mode mode;
2856                 bool checked_state;
2857                 
2858                 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2859                         : base (child, return_type)
2860                 {
2861                         checked_state = ec.CheckState;
2862                         mode = m;
2863                 }
2864
2865                 public override Expression DoResolve (EmitContext ec)
2866                 {
2867                         // This should never be invoked, we are born in fully
2868                         // initialized state.
2869
2870                         return this;
2871                 }
2872
2873                 public override void Emit (EmitContext ec)
2874                 {
2875                         ILGenerator ig = ec.ig;
2876                         
2877                         base.Emit (ec);
2878
2879                         if (checked_state){
2880                                 switch (mode){
2881                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2882                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2883                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2884                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2885                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2886
2887                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2888                                 case Mode.U1_CH: /* nothing */ break;
2889
2890                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2891                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2892                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2893                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2894                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2895                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2896
2897                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2898                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2899                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2900                                 case Mode.U2_CH: /* nothing */ break;
2901
2902                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2903                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2904                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2905                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2906                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2907                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2908                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2909
2910                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2911                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2912                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2913                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2914                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2915                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2916
2917                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2918                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2919                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2920                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2921                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2922                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2923                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2924                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2925
2926                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2927                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2928                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2929                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2930                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2931                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2932                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2933                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2934
2935                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2936                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2937                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2938
2939                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2940                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2941                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2942                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2943                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2944                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2945                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2946                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2947                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2948
2949                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2950                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2951                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2952                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2953                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2954                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2955                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2956                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2957                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2958                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2959                                 }
2960                         } else {
2961                                 switch (mode){
2962                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2963                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2964                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2965                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2966                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2967
2968                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2969                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2970
2971                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2972                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2973                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2974                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2975                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2976                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2977
2978                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2979                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2980                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2981                                 case Mode.U2_CH: /* nothing */ break;
2982
2983                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2984                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2985                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2986                                 case Mode.I4_U4: /* nothing */ break;
2987                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2988                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2989                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2990
2991                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2992                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2993                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2994                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2995                                 case Mode.U4_I4: /* nothing */ break;
2996                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2997
2998                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2999                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3000                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3001                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3002                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3003                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3004                                 case Mode.I8_U8: /* nothing */ break;
3005                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3006
3007                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3008                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3009                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3010                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3011                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3012                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3013                                 case Mode.U8_I8: /* nothing */ break;
3014                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3015
3016                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3017                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3018                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3019
3020                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3021                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3022                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3023                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3024                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3025                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3026                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3027                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3028                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3029
3030                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3031                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3032                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3033                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3034                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3035                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3036                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3037                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3038                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3039                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3040                                 }
3041                         }
3042                 }
3043         }
3044         
3045         public class OpcodeCast : EmptyCast {
3046                 OpCode op, op2;
3047                 bool second_valid;
3048                 
3049                 public OpcodeCast (Expression child, Type return_type, OpCode op)
3050                         : base (child, return_type)
3051                         
3052                 {
3053                         this.op = op;
3054                         second_valid = false;
3055                 }
3056
3057                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3058                         : base (child, return_type)
3059                         
3060                 {
3061                         this.op = op;
3062                         this.op2 = op2;
3063                         second_valid = true;
3064                 }
3065
3066                 public override Expression DoResolve (EmitContext ec)
3067                 {
3068                         // This should never be invoked, we are born in fully
3069                         // initialized state.
3070
3071                         return this;
3072                 }
3073
3074                 public override void Emit (EmitContext ec)
3075                 {
3076                         base.Emit (ec);
3077                         ec.ig.Emit (op);
3078
3079                         if (second_valid)
3080                                 ec.ig.Emit (op2);
3081                 }                       
3082         }
3083
3084         /// <summary>
3085         ///   This kind of cast is used to encapsulate a child and cast it
3086         ///   to the class requested
3087         /// </summary>
3088         public class ClassCast : EmptyCast {
3089                 public ClassCast (Expression child, Type return_type)
3090                         : base (child, return_type)
3091                         
3092                 {
3093                 }
3094
3095                 public override Expression DoResolve (EmitContext ec)
3096                 {
3097                         // This should never be invoked, we are born in fully
3098                         // initialized state.
3099
3100                         return this;
3101                 }
3102
3103                 public override void Emit (EmitContext ec)
3104                 {
3105                         base.Emit (ec);
3106
3107                         ec.ig.Emit (OpCodes.Castclass, type);
3108                 }                       
3109                 
3110         }
3111         
3112         /// <summary>
3113         ///   SimpleName expressions are initially formed of a single
3114         ///   word and it only happens at the beginning of the expression.
3115         /// </summary>
3116         ///
3117         /// <remarks>
3118         ///   The expression will try to be bound to a Field, a Method
3119         ///   group or a Property.  If those fail we pass the name to our
3120         ///   caller and the SimpleName is compounded to perform a type
3121         ///   lookup.  The idea behind this process is that we want to avoid
3122         ///   creating a namespace map from the assemblies, as that requires
3123         ///   the GetExportedTypes function to be called and a hashtable to
3124         ///   be constructed which reduces startup time.  If later we find
3125         ///   that this is slower, we should create a `NamespaceExpr' expression
3126         ///   that fully participates in the resolution process. 
3127         ///   
3128         ///   For example `System.Console.WriteLine' is decomposed into
3129         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3130         ///   
3131         ///   The first SimpleName wont produce a match on its own, so it will
3132         ///   be turned into:
3133         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
3134         ///   
3135         ///   System.Console will produce a TypeExpr match.
3136         ///   
3137         ///   The downside of this is that we might be hitting `LookupType' too many
3138         ///   times with this scheme.
3139         /// </remarks>
3140         public class SimpleName : Expression {
3141                 public readonly string Name;
3142                 public readonly Location Location;
3143                 
3144                 public SimpleName (string name, Location l)
3145                 {
3146                         Name = name;
3147                         Location = l;
3148                 }
3149
3150                 public static void Error_ObjectRefRequired (Location l, string name)
3151                 {
3152                         Report.Error (
3153                                 120, l,
3154                                 "An object reference is required " +
3155                                 "for the non-static field `"+name+"'");
3156                 }
3157                 
3158                 //
3159                 // Checks whether we are trying to access an instance
3160                 // property, method or field from a static body.
3161                 //
3162                 Expression MemberStaticCheck (Expression e)
3163                 {
3164                         if (e is FieldExpr){
3165                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3166                                 
3167                                 if (!fi.IsStatic){
3168                                         Error_ObjectRefRequired (Location, Name);
3169                                         return null;
3170                                 }
3171                         } else if (e is MethodGroupExpr){
3172                                 MethodGroupExpr mg = (MethodGroupExpr) e;
3173
3174                                 if (!mg.RemoveInstanceMethods ()){
3175                                         Error_ObjectRefRequired (Location, mg.Methods [0].Name);
3176                                         return null;
3177                                 }
3178                                 return e;
3179                         } else if (e is PropertyExpr){
3180                                 if (!((PropertyExpr) e).IsStatic){
3181                                         Error_ObjectRefRequired (Location, Name);
3182                                         return null;
3183                                 }
3184                         } else if (e is EventExpr) {
3185                                 if (!((EventExpr) e).IsStatic) {
3186                                         Error_ObjectRefRequired (Location, Name);
3187                                         return null;
3188                                 }
3189                         }
3190
3191                         return e;
3192                 }
3193                 
3194                 public override Expression DoResolve (EmitContext ec)
3195                 {
3196                         return SimpleNameResolve (ec, null, false);
3197                 }
3198
3199                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3200                 {
3201                         return SimpleNameResolve (ec, right_side, false);
3202                 }
3203                 
3204
3205                 public Expression DoResolveAllowStatic (EmitContext ec)
3206                 {
3207                         return SimpleNameResolve (ec, null, true);
3208                 }
3209
3210                 /// <remarks>
3211                 ///   7.5.2: Simple Names. 
3212                 ///
3213                 ///   Local Variables and Parameters are handled at
3214                 ///   parse time, so they never occur as SimpleNames.
3215                 ///
3216                 ///   The `allow_static' flag is used by MemberAccess only
3217                 ///   and it is used to inform us that it is ok for us to 
3218                 ///   avoid the static check, because MemberAccess might end
3219                 ///   up resolving the Name as a Type name and the access as
3220                 ///   a static type access.
3221                 ///
3222                 ///   ie: Type Type; .... { Type.GetType (""); }
3223                 ///
3224                 ///   Type is both an instance variable and a Type;  Type.GetType
3225                 ///   is the static method not an instance method of type.
3226                 /// </remarks>
3227                 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3228                 {
3229                         Expression e = null;
3230
3231                         //
3232                         // Stage 1: Performed by the parser (binding to locals or parameters).
3233                         //
3234                         if (!ec.OnlyLookupTypes){
3235                                 Block current_block = ec.CurrentBlock;
3236                                 if (current_block != null && current_block.IsVariableDefined (Name)){
3237                                         LocalVariableReference var;
3238                                         
3239                                         var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
3240
3241                                         if (right_side != null)
3242                                                 return var.ResolveLValue (ec, right_side);
3243                                         else
3244                                                 return var.Resolve (ec);
3245                                 }
3246                         
3247                                 //
3248                                 // Stage 2: Lookup members 
3249                                 //
3250
3251                                 //
3252                                 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3253                                 // Hence we have two different cases
3254                                 //
3255
3256                                 DeclSpace lookup_ds = ec.DeclSpace;
3257                                 do {
3258                                         if (lookup_ds.TypeBuilder == null)
3259                                                 break;
3260
3261                                         e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
3262                                         if (e != null)
3263                                                 break;
3264
3265                                         //
3266                                         // Classes/structs keep looking, enums break
3267                                         //
3268                                         if (lookup_ds is TypeContainer)
3269                                                 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3270                                         else
3271                                                 break;
3272                                 } while (lookup_ds != null);
3273                                 
3274                                 if (e == null && ec.ContainerType != null)
3275                                         e = MemberLookup (ec, ec.ContainerType, Name, Location);
3276                         }
3277
3278                         // Continuation of stage 2
3279                         if (e == null){
3280                                 //
3281                                 // Stage 3: Lookup symbol in the various namespaces. 
3282                                 //
3283                                 DeclSpace ds = ec.DeclSpace;
3284                                 Type t;
3285                                 string alias_value;
3286
3287                                 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3288                                         return new TypeExpr (t);
3289                                 
3290                                 //
3291                                 // Stage 2 part b: Lookup up if we are an alias to a type
3292                                 // or a namespace.
3293                                 //
3294                                 // Since we are cheating: we only do the Alias lookup for
3295                                 // namespaces if the name does not include any dots in it
3296                                 //
3297                                 
3298                                 alias_value = ec.DeclSpace.LookupAlias (Name);
3299                                 
3300                                 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3301                                         if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3302                                             != null)
3303                                                 return new TypeExpr (t);
3304                                         
3305                                 // we have alias value, but it isn't Type, so try if it's namespace
3306                                         return new SimpleName (alias_value, Location);
3307                                 }
3308                                 
3309                                 if (ec.ResolvingTypeTree){
3310                                         Type dt = ec.DeclSpace.FindType (Name);
3311                                         if (dt != null)
3312                                                 return new TypeExpr (dt);
3313                                 }
3314                                 
3315                                 // No match, maybe our parent can compose us
3316                                 // into something meaningful.
3317                                 return this;
3318                         }
3319                         
3320                         //
3321                         // Stage 2 continues here. 
3322                         // 
3323                         if (e is TypeExpr)
3324                                 return e;
3325
3326                         if (ec.OnlyLookupTypes)
3327                                 return null;
3328                         
3329                         if (e is FieldExpr){
3330                                 FieldExpr fe = (FieldExpr) e;
3331                                 FieldInfo fi = fe.FieldInfo;
3332
3333                                 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3334                                         UnsafeError (Location);
3335                                 }
3336                                 
3337                                 if (ec.IsStatic){
3338                                         if (!allow_static && !fi.IsStatic){
3339                                                 Error_ObjectRefRequired (Location, Name);
3340                                                 return null;
3341                                         }
3342                                 } else {
3343                                         // If we are not in static code and this
3344                                         // field is not static, set the instance to `this'.
3345
3346                                         if (!fi.IsStatic)
3347                                                 fe.InstanceExpression = ec.This;
3348                                 }
3349
3350                                 
3351                                 if (fi is FieldBuilder) {
3352                                         Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3353                                         
3354                                         if (c != null) {
3355                                                 object o = c.LookupConstantValue (ec);
3356                                                 object real_value = ((Constant)c.Expr).GetValue ();
3357                                                 return Constantify (real_value, fi.FieldType);
3358                                         }
3359                                 }
3360
3361                                 if (fi.IsLiteral) {
3362                                         Type t = fi.FieldType;
3363                                         Type decl_type = fi.DeclaringType;
3364                                         object o;
3365
3366                                         if (fi is FieldBuilder)
3367                                                 o = TypeManager.GetValue ((FieldBuilder) fi);
3368                                         else
3369                                                 o = fi.GetValue (fi);
3370                                         
3371                                         if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3372                                                 Expression enum_member = MemberLookup (
3373                                                         ec, decl_type, "value__", MemberTypes.Field,
3374                                                         AllBindingFlags, Location); 
3375
3376                                                 Enum en = TypeManager.LookupEnum (decl_type);
3377
3378                                                 Constant c;
3379                                                 if (en != null)
3380                                                         c = Constantify (o, en.UnderlyingType);
3381                                                 else 
3382                                                         c = Constantify (o, enum_member.Type);
3383                                                 
3384                                                 return new EnumConstant (c, decl_type);
3385                                         }
3386                                         
3387                                         Expression exp = Constantify (o, t);
3388                                 }
3389                                         
3390                                 return e;
3391                         }
3392
3393                         if (e is PropertyExpr) {
3394                                 PropertyExpr pe = (PropertyExpr) e;
3395
3396                                 if (ec.IsStatic){
3397                                         if (allow_static)
3398                                                 return e;
3399
3400                                         return MemberStaticCheck (e);
3401                                 } else {
3402                                         // If we are not in static code and this
3403                                         // field is not static, set the instance to `this'.
3404
3405                                         if (!pe.IsStatic)
3406                                                 pe.InstanceExpression = ec.This;
3407                                 }
3408
3409                                 return e;
3410                         }
3411
3412                         if (e is EventExpr) {
3413                                 //
3414                                 // If the event is local to this class, we transform ourselves into
3415                                 // a FieldExpr
3416                                 //
3417                                 EventExpr ee = (EventExpr) e;
3418
3419                                 Expression ml = MemberLookup (
3420                                         ec, ec.ContainerType, ee.EventInfo.Name,
3421                                         MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, Location);
3422
3423                                 if (ml != null) {
3424                                         MemberInfo mi = GetFieldFromEvent ((EventExpr) ml);
3425
3426                                         if (mi == null) {
3427                                                 //
3428                                                 // If this happens, then we have an event with its own
3429                                                 // accessors and private field etc so there's no need
3430                                                 // to transform ourselves : we should instead flag an error
3431                                                 //
3432                                                 Assign.error70 (ee.EventInfo, Location);
3433                                                 return null;
3434                                         }
3435
3436                                         ml = ExprClassFromMemberInfo (ec, mi, Location);
3437                                         
3438                                         if (ml == null) {
3439                                                 Report.Error (-200, Location, "Internal error!!");
3440                                                 return null;
3441                                         }
3442
3443                                         Expression instance_expr;
3444                                         
3445                                         FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3446
3447                                         if (fi.IsStatic)
3448                                                 instance_expr = null;
3449                                         else {
3450                                                 instance_expr = ec.This;
3451                                                 instance_expr = instance_expr.Resolve (ec);
3452                                         } 
3453                                         
3454                                         return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3455                                 }
3456                         }
3457                                 
3458                         
3459                         if (ec.IsStatic){
3460                                 if (allow_static)
3461                                         return e;
3462
3463                                 return MemberStaticCheck (e);
3464                         } else
3465                                 return e;
3466                 }
3467
3468                 
3469                 
3470                 public override void Emit (EmitContext ec)
3471                 {
3472                         //
3473                         // If this is ever reached, then we failed to
3474                         // find the name as a namespace
3475                         //
3476
3477                         Error (103, Location, "The name `" + Name +
3478                                "' does not exist in the class `" +
3479                                ec.DeclSpace.Name + "'");
3480                 }
3481
3482                 public override string ToString ()
3483                 {
3484                         return Name;
3485                 }
3486         }
3487         
3488         /// <summary>
3489         ///   Fully resolved expression that evaluates to a type
3490         /// </summary>
3491         public class TypeExpr : Expression {
3492                 public TypeExpr (Type t)
3493                 {
3494                         Type = t;
3495                         eclass = ExprClass.Type;
3496                 }
3497
3498                 override public Expression DoResolve (EmitContext ec)
3499                 {
3500                         return this;
3501                 }
3502
3503                 override public void Emit (EmitContext ec)
3504                 {
3505                         throw new Exception ("Should never be called");
3506                 }
3507         }
3508
3509         /// <summary>
3510         ///   Used to create types from a fully qualified name.  These are just used
3511         ///   by the parser to setup the core types.  A TypeExpression is always
3512         ///   classified as a type.
3513         /// </summary>
3514         public class TypeExpression : TypeExpr {
3515                 string name;
3516                 
3517                 public TypeExpression (string name) : base (null)
3518                 {
3519                         this.name = name;
3520                 }
3521
3522                 public override Expression DoResolve (EmitContext ec)
3523                 {
3524                         if (type == null)
3525                                 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3526                         return this;
3527                 }
3528
3529                 public override void Emit (EmitContext ec)
3530                 {
3531                         throw new Exception ("Should never be called");                 
3532                 }
3533
3534                 public override string ToString ()
3535                 {
3536                         return name;
3537                 }
3538         }
3539
3540         /// <summary>
3541         ///   MethodGroup Expression.
3542         ///  
3543         ///   This is a fully resolved expression that evaluates to a type
3544         /// </summary>
3545         public class MethodGroupExpr : Expression {
3546                 public MethodBase [] Methods;
3547                 Location loc;
3548                 Expression instance_expression = null;
3549                 
3550                 public MethodGroupExpr (MemberInfo [] mi, Location l)
3551                 {
3552                         Methods = new MethodBase [mi.Length];
3553                         mi.CopyTo (Methods, 0);
3554                         eclass = ExprClass.MethodGroup;
3555                         type = TypeManager.object_type;
3556                         loc = l;
3557                 }
3558
3559                 public MethodGroupExpr (ArrayList list, Location l)
3560                 {
3561                         Methods = new MethodBase [list.Count];
3562
3563                         try {
3564                                 list.CopyTo (Methods, 0);
3565                         } catch {
3566                                 foreach (MemberInfo m in list){
3567                                         if (!(m is MethodBase)){
3568                                                 Console.WriteLine ("Name " + m.Name);
3569                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3570                                         }
3571                                 }
3572                                 throw;
3573                         }
3574                         loc = l;
3575                         eclass = ExprClass.MethodGroup;
3576                         type = TypeManager.object_type;
3577                 }
3578                 
3579                 //
3580                 // `A method group may have associated an instance expression' 
3581                 // 
3582                 public Expression InstanceExpression {
3583                         get {
3584                                 return instance_expression;
3585                         }
3586
3587                         set {
3588                                 instance_expression = value;
3589                         }
3590                 }
3591                 
3592                 override public Expression DoResolve (EmitContext ec)
3593                 {
3594                         return this;
3595                 }
3596
3597                 public void ReportUsageError ()
3598                 {
3599                         Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3600                                       Methods [0].Name + "()' is referenced without parentheses");
3601                 }
3602
3603                 override public void Emit (EmitContext ec)
3604                 {
3605                         ReportUsageError ();
3606                 }
3607
3608                 bool RemoveMethods (bool keep_static)
3609                 {
3610                         ArrayList smethods = new ArrayList ();
3611
3612                         foreach (MethodBase mb in Methods){
3613                                 if (mb.IsStatic == keep_static)
3614                                         smethods.Add (mb);
3615                         }
3616
3617                         if (smethods.Count == 0)
3618                                 return false;
3619
3620                         Methods = new MethodBase [smethods.Count];
3621                         smethods.CopyTo (Methods, 0);
3622
3623                         return true;
3624                 }
3625                 
3626                 /// <summary>
3627                 ///   Removes any instance methods from the MethodGroup, returns
3628                 ///   false if the resulting set is empty.
3629                 /// </summary>
3630                 public bool RemoveInstanceMethods ()
3631                 {
3632                         return RemoveMethods (true);
3633                 }
3634
3635                 /// <summary>
3636                 ///   Removes any static methods from the MethodGroup, returns
3637                 ///   false if the resulting set is empty.
3638                 /// </summary>
3639                 public bool RemoveStaticMethods ()
3640                 {
3641                         return RemoveMethods (false);
3642                 }
3643         }
3644
3645         /// <summary>
3646         ///   Fully resolved expression that evaluates to a Field
3647         /// </summary>
3648         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3649                 public readonly FieldInfo FieldInfo;
3650                 public Expression InstanceExpression;
3651                 Location loc;
3652                 
3653                 public FieldExpr (FieldInfo fi, Location l)
3654                 {
3655                         FieldInfo = fi;
3656                         eclass = ExprClass.Variable;
3657                         type = fi.FieldType;
3658                         loc = l;
3659                 }
3660
3661                 override public Expression DoResolve (EmitContext ec)
3662                 {
3663                         if (!FieldInfo.IsStatic){
3664                                 if (InstanceExpression == null){
3665                                         throw new Exception ("non-static FieldExpr without instance var\n" +
3666                                                              "You have to assign the Instance variable\n" +
3667                                                              "Of the FieldExpr to set this\n");
3668                                 }
3669
3670                                 InstanceExpression = InstanceExpression.Resolve (ec);
3671                                 if (InstanceExpression == null)
3672                                         return null;
3673                         }
3674
3675                         return this;
3676                 }
3677
3678                 void Report_AssignToReadonly (bool is_instance)
3679                 {
3680                         string msg;
3681                         
3682                         if (is_instance)
3683                                 msg = "Readonly field can not be assigned outside " +
3684                                 "of constructor or variable initializer";
3685                         else
3686                                 msg = "A static readonly field can only be assigned in " +
3687                                 "a static constructor";
3688
3689                         Report.Error (is_instance ? 191 : 198, loc, msg);
3690                 }
3691                 
3692                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3693                 {
3694                         Expression e = DoResolve (ec);
3695
3696                         if (e == null)
3697                                 return null;
3698                         
3699                         if (!FieldInfo.IsInitOnly)
3700                                 return this;
3701
3702                         //
3703                         // InitOnly fields can only be assigned in constructors
3704                         //
3705
3706                         if (ec.IsConstructor)
3707                                 return this;
3708
3709                         Report_AssignToReadonly (true);
3710                         
3711                         return null;
3712                 }
3713
3714                 override public void Emit (EmitContext ec)
3715                 {
3716                         ILGenerator ig = ec.ig;
3717                         bool is_volatile = false;
3718                                 
3719                         if (FieldInfo is FieldBuilder){
3720                                 FieldBase f = TypeManager.GetField (FieldInfo);
3721
3722                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3723                                         is_volatile = true;
3724                                 
3725                                 f.status |= Field.Status.USED;
3726                         }
3727                         
3728                         if (FieldInfo.IsStatic){
3729                                 if (is_volatile)
3730                                         ig.Emit (OpCodes.Volatile);
3731                                 
3732                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3733                         } else {
3734                                 if (InstanceExpression.Type.IsValueType){
3735                                         IMemoryLocation ml;
3736                                         LocalTemporary tempo = null;
3737                                         
3738                                         if (!(InstanceExpression is IMemoryLocation)){
3739                                                 tempo = new LocalTemporary (
3740                                                         ec, InstanceExpression.Type);
3741
3742                                                 InstanceExpression.Emit (ec);
3743                                                 tempo.Store (ec);
3744                                                 ml = tempo;
3745                                         } else
3746                                                 ml = (IMemoryLocation) InstanceExpression;
3747
3748                                         ml.AddressOf (ec, AddressOp.Load);
3749                                 } else 
3750                                         InstanceExpression.Emit (ec);
3751
3752                                 if (is_volatile)
3753                                         ig.Emit (OpCodes.Volatile);
3754                                 
3755                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
3756                         }
3757                 }
3758
3759                 public void EmitAssign (EmitContext ec, Expression source)
3760                 {
3761                         FieldAttributes fa = FieldInfo.Attributes;
3762                         bool is_static = (fa & FieldAttributes.Static) != 0;
3763                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3764                         ILGenerator ig = ec.ig;
3765
3766                         if (is_readonly && !ec.IsConstructor){
3767                                 Report_AssignToReadonly (!is_static);
3768                                 return;
3769                         }
3770                         
3771                         if (!is_static){
3772                                 Expression instance = InstanceExpression;
3773
3774                                 if (instance.Type.IsValueType){
3775                                         if (instance is IMemoryLocation){
3776                                                 IMemoryLocation ml = (IMemoryLocation) instance;
3777
3778                                                 ml.AddressOf (ec, AddressOp.Store);
3779                                         } else
3780                                                 throw new Exception ("The " + instance + " of type " +
3781                                                                      instance.Type +
3782                                                                      " represents a ValueType and does " +
3783                                                                      "not implement IMemoryLocation");
3784                                 } else
3785                                         instance.Emit (ec);
3786                         }
3787                         source.Emit (ec);
3788
3789                         if (FieldInfo is FieldBuilder){
3790                                 FieldBase f = TypeManager.GetField (FieldInfo);
3791                                 
3792                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3793                                         ig.Emit (OpCodes.Volatile);
3794                         }
3795                         
3796                         if (is_static)
3797                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3798                         else 
3799                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3800
3801                         if (FieldInfo is FieldBuilder){
3802                                 FieldBase f = TypeManager.GetField (FieldInfo);
3803
3804                                 f.status |= Field.Status.ASSIGNED;
3805                         }
3806                 }
3807                 
3808                 public void AddressOf (EmitContext ec, AddressOp mode)
3809                 {
3810                         ILGenerator ig = ec.ig;
3811                         
3812                         if (FieldInfo is FieldBuilder){
3813                                 FieldBase f = TypeManager.GetField (FieldInfo);
3814                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3815                                         ig.Emit (OpCodes.Volatile);
3816                         }
3817
3818                         if (FieldInfo is FieldBuilder){
3819                                 FieldBase f = TypeManager.GetField (FieldInfo);
3820
3821                                 if ((mode & AddressOp.Store) != 0)
3822                                         f.status |= Field.Status.ASSIGNED;
3823                                 if ((mode & AddressOp.Load) != 0)
3824                                         f.status |= Field.Status.USED;
3825                         }
3826
3827                         //
3828                         // Handle initonly fields specially: make a copy and then
3829                         // get the address of the copy.
3830                         //
3831                         if (FieldInfo.IsInitOnly){
3832                                 if (ec.IsConstructor) {
3833                                         ig.Emit (OpCodes.Ldsflda, FieldInfo);
3834                                 } else {
3835                                         LocalBuilder local;
3836                                 
3837                                         Emit (ec);
3838                                         local = ig.DeclareLocal (type);
3839                                         ig.Emit (OpCodes.Stloc, local);
3840                                         ig.Emit (OpCodes.Ldloca, local);
3841                                 }
3842                                 return;
3843                         }
3844
3845                         if (FieldInfo.IsStatic)
3846                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3847                         else {
3848                                 if (InstanceExpression is IMemoryLocation)
3849                                         ((IMemoryLocation)InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
3850                                 else
3851                                         InstanceExpression.Emit (ec);
3852                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
3853                         }
3854                 }
3855         }
3856         
3857         /// <summary>
3858         ///   Expression that evaluates to a Property.  The Assign class
3859         ///   might set the `Value' expression if we are in an assignment.
3860         ///
3861         ///   This is not an LValue because we need to re-write the expression, we
3862         ///   can not take data from the stack and store it.  
3863         /// </summary>
3864         public class PropertyExpr : ExpressionStatement, IAssignMethod {
3865                 public readonly PropertyInfo PropertyInfo;
3866                 public readonly bool IsStatic;
3867                 public bool IsBase;
3868                 MethodInfo [] Accessors;
3869                 Location loc;
3870                 
3871                 Expression instance_expr;
3872                 
3873                 public PropertyExpr (PropertyInfo pi, Location l)
3874                 {
3875                         PropertyInfo = pi;
3876                         eclass = ExprClass.PropertyAccess;
3877                         IsStatic = false;
3878                         loc = l;
3879                         Accessors = TypeManager.GetAccessors (pi);
3880
3881                         if (Accessors != null)
3882                                 foreach (MethodInfo mi in Accessors){
3883                                         if (mi != null)
3884                                                 if (mi.IsStatic)
3885                                                         IsStatic = true;
3886                                 }
3887                         else
3888                                 Accessors = new MethodInfo [2];
3889                         
3890                         type = TypeManager.TypeToCoreType (pi.PropertyType);
3891                 }
3892
3893                 //
3894                 // The instance expression associated with this expression
3895                 //
3896                 public Expression InstanceExpression {
3897                         set {
3898                                 instance_expr = value;
3899                         }
3900
3901                         get {
3902                                 return instance_expr;
3903                         }
3904                 }
3905
3906                 public bool VerifyAssignable ()
3907                 {
3908                         if (!PropertyInfo.CanWrite){
3909                                 Report.Error (200, loc, 
3910                                               "The property `" + PropertyInfo.Name +
3911                                               "' can not be assigned to, as it has not set accessor");
3912                                 return false;
3913                         }
3914
3915                         return true;
3916                 }
3917
3918                 override public Expression DoResolve (EmitContext ec)
3919                 {
3920                         if (!PropertyInfo.CanRead){
3921                                 Report.Error (154, loc, 
3922                                               "The property `" + PropertyInfo.Name +
3923                                               "' can not be used in " +
3924                                               "this context because it lacks a get accessor");
3925                                 return null;
3926                         }
3927
3928                         type = PropertyInfo.PropertyType;
3929
3930                         return this;
3931                 }
3932
3933                 override public void Emit (EmitContext ec)
3934                 {
3935                         MethodInfo method = Accessors [0];
3936
3937                         //
3938                         // Special case: length of single dimension array is turned into ldlen
3939                         //
3940                         if (method == TypeManager.int_array_get_length){
3941                                 Type iet = instance_expr.Type;
3942
3943                                 //
3944                                 // System.Array.Length can be called, but the Type does not
3945                                 // support invoking GetArrayRank, so test for that case first
3946                                 //
3947                                 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
3948                                         instance_expr.Emit (ec);
3949                                         ec.ig.Emit (OpCodes.Ldlen);
3950                                         return;
3951                                 }
3952                         }
3953
3954                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
3955                         
3956                 }
3957
3958                 //
3959                 // Implements the IAssignMethod interface for assignments
3960                 //
3961                 public void EmitAssign (EmitContext ec, Expression source)
3962                 {
3963                         Argument arg = new Argument (source, Argument.AType.Expression);
3964                         ArrayList args = new ArrayList ();
3965
3966                         args.Add (arg);
3967                         Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
3968                 }
3969
3970                 override public void EmitStatement (EmitContext ec)
3971                 {
3972                         Emit (ec);
3973                         ec.ig.Emit (OpCodes.Pop);
3974                 }
3975         }
3976
3977         /// <summary>
3978         ///   Fully resolved expression that evaluates to an Event
3979         /// </summary>
3980         public class EventExpr : Expression {
3981                 public readonly EventInfo EventInfo;
3982                 Location loc;
3983                 public Expression InstanceExpression;
3984
3985                 public readonly bool IsStatic;
3986
3987                 MethodInfo add_accessor, remove_accessor;
3988                 
3989                 public EventExpr (EventInfo ei, Location loc)
3990                 {
3991                         EventInfo = ei;
3992                         this.loc = loc;
3993                         eclass = ExprClass.EventAccess;
3994
3995                         add_accessor = TypeManager.GetAddMethod (ei);
3996                         remove_accessor = TypeManager.GetRemoveMethod (ei);
3997                         
3998                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
3999                                         IsStatic = true;
4000
4001                         if (EventInfo is MyEventBuilder)
4002                                 type = ((MyEventBuilder) EventInfo).EventType;
4003                         else
4004                                 type = EventInfo.EventHandlerType;
4005                 }
4006
4007                 public override Expression DoResolve (EmitContext ec)
4008                 {
4009                         // We are born fully resolved
4010                         return this;
4011                 }
4012
4013                 public override void Emit (EmitContext ec)
4014                 {
4015                         throw new Exception ("Should not happen I think");
4016                 }
4017
4018                 public void EmitAddOrRemove (EmitContext ec, Expression source)
4019                 {
4020                         Expression handler = ((Binary) source).Right;
4021                         
4022                         Argument arg = new Argument (handler, Argument.AType.Expression);
4023                         ArrayList args = new ArrayList ();
4024                                 
4025                         args.Add (arg);
4026                         
4027                         if (((Binary) source).Oper == Binary.Operator.Addition)
4028                                 Invocation.EmitCall (
4029                                         ec, false, IsStatic, InstanceExpression, add_accessor, args, loc);
4030                         else
4031                                 Invocation.EmitCall (
4032                                         ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);
4033                 }
4034         }
4035 }