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