2002-07-10 Ravi Pratap <ravi@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
2537         /// <summary>
2538         ///   This is just a base class for expressions that can
2539         ///   appear on statements (invocations, object creation,
2540         ///   assignments, post/pre increment and decrement).  The idea
2541         ///   being that they would support an extra Emition interface that
2542         ///   does not leave a result on the stack.
2543         /// </summary>
2544         public abstract class ExpressionStatement : Expression {
2545
2546                 /// <summary>
2547                 ///   Requests the expression to be emitted in a `statement'
2548                 ///   context.  This means that no new value is left on the
2549                 ///   stack after invoking this method (constrasted with
2550                 ///   Emit that will always leave a value on the stack).
2551                 /// </summary>
2552                 public abstract void EmitStatement (EmitContext ec);
2553         }
2554
2555         /// <summary>
2556         ///   This kind of cast is used to encapsulate the child
2557         ///   whose type is child.Type into an expression that is
2558         ///   reported to return "return_type".  This is used to encapsulate
2559         ///   expressions which have compatible types, but need to be dealt
2560         ///   at higher levels with.
2561         ///
2562         ///   For example, a "byte" expression could be encapsulated in one
2563         ///   of these as an "unsigned int".  The type for the expression
2564         ///   would be "unsigned int".
2565         ///
2566         /// </summary>
2567         public class EmptyCast : Expression {
2568                 protected Expression child;
2569
2570                 public EmptyCast (Expression child, Type return_type)
2571                 {
2572                         eclass = child.eclass;
2573                         type = return_type;
2574                         this.child = child;
2575                 }
2576
2577                 public override Expression DoResolve (EmitContext ec)
2578                 {
2579                         // This should never be invoked, we are born in fully
2580                         // initialized state.
2581
2582                         return this;
2583                 }
2584
2585                 public override void Emit (EmitContext ec)
2586                 {
2587                         child.Emit (ec);
2588                 }
2589         }
2590
2591         /// <summary>
2592         ///  This class is used to wrap literals which belong inside Enums
2593         /// </summary>
2594         public class EnumConstant : Constant {
2595                 public Constant Child;
2596
2597                 public EnumConstant (Constant child, Type enum_type)
2598                 {
2599                         eclass = child.eclass;
2600                         this.Child = child;
2601                         type = enum_type;
2602                 }
2603                 
2604                 public override Expression DoResolve (EmitContext ec)
2605                 {
2606                         // This should never be invoked, we are born in fully
2607                         // initialized state.
2608
2609                         return this;
2610                 }
2611
2612                 public override void Emit (EmitContext ec)
2613                 {
2614                         Child.Emit (ec);
2615                 }
2616
2617                 public override object GetValue ()
2618                 {
2619                         return Child.GetValue ();
2620                 }
2621
2622                 //
2623                 // Converts from one of the valid underlying types for an enumeration
2624                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2625                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2626                 //
2627                 public Constant WidenToCompilerConstant ()
2628                 {
2629                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2630                         object v = ((Constant) Child).GetValue ();;
2631                         
2632                         if (t == TypeManager.int32_type)
2633                                 return new IntConstant ((int) v);
2634                         if (t == TypeManager.uint32_type)
2635                                 return new UIntConstant ((uint) v);
2636                         if (t == TypeManager.int64_type)
2637                                 return new LongConstant ((long) v);
2638                         if (t == TypeManager.uint64_type)
2639                                 return new ULongConstant ((ulong) v);
2640                         if (t == TypeManager.short_type)
2641                                 return new ShortConstant ((short) v);
2642                         if (t == TypeManager.ushort_type)
2643                                 return new UShortConstant ((ushort) v);
2644                         if (t == TypeManager.byte_type)
2645                                 return new ByteConstant ((byte) v);
2646                         if (t == TypeManager.sbyte_type)
2647                                 return new SByteConstant ((sbyte) v);
2648
2649                         throw new Exception ("Invalid enumeration underlying type: " + t);
2650                 }
2651
2652                 //
2653                 // Extracts the value in the enumeration on its native representation
2654                 //
2655                 public object GetPlainValue ()
2656                 {
2657                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2658                         object v = ((Constant) Child).GetValue ();;
2659                         
2660                         if (t == TypeManager.int32_type)
2661                                 return (int) v;
2662                         if (t == TypeManager.uint32_type)
2663                                 return (uint) v;
2664                         if (t == TypeManager.int64_type)
2665                                 return (long) v;
2666                         if (t == TypeManager.uint64_type)
2667                                 return (ulong) v;
2668                         if (t == TypeManager.short_type)
2669                                 return (short) v;
2670                         if (t == TypeManager.ushort_type)
2671                                 return (ushort) v;
2672                         if (t == TypeManager.byte_type)
2673                                 return (byte) v;
2674                         if (t == TypeManager.sbyte_type)
2675                                 return (sbyte) v;
2676
2677                         return null;
2678                 }
2679                 
2680                 public override string AsString ()
2681                 {
2682                         return Child.AsString ();
2683                 }
2684
2685                 public override DoubleConstant ConvertToDouble ()
2686                 {
2687                         return Child.ConvertToDouble ();
2688                 }
2689
2690                 public override FloatConstant ConvertToFloat ()
2691                 {
2692                         return Child.ConvertToFloat ();
2693                 }
2694
2695                 public override ULongConstant ConvertToULong ()
2696                 {
2697                         return Child.ConvertToULong ();
2698                 }
2699
2700                 public override LongConstant ConvertToLong ()
2701                 {
2702                         return Child.ConvertToLong ();
2703                 }
2704
2705                 public override UIntConstant ConvertToUInt ()
2706                 {
2707                         return Child.ConvertToUInt ();
2708                 }
2709
2710                 public override IntConstant ConvertToInt ()
2711                 {
2712                         return Child.ConvertToInt ();
2713                 }
2714         }
2715
2716         /// <summary>
2717         ///   This kind of cast is used to encapsulate Value Types in objects.
2718         ///
2719         ///   The effect of it is to box the value type emitted by the previous
2720         ///   operation.
2721         /// </summary>
2722         public class BoxedCast : EmptyCast {
2723
2724                 public BoxedCast (Expression expr)
2725                         : base (expr, TypeManager.object_type)
2726                 {
2727                 }
2728
2729                 public override Expression DoResolve (EmitContext ec)
2730                 {
2731                         // This should never be invoked, we are born in fully
2732                         // initialized state.
2733
2734                         return this;
2735                 }
2736
2737                 public override void Emit (EmitContext ec)
2738                 {
2739                         base.Emit (ec);
2740                         
2741                         ec.ig.Emit (OpCodes.Box, child.Type);
2742                 }
2743         }
2744
2745         public class UnboxCast : EmptyCast {
2746                 public UnboxCast (Expression expr, Type return_type)
2747                         : base (expr, return_type)
2748                 {
2749                 }
2750
2751                 public override Expression DoResolve (EmitContext ec)
2752                 {
2753                         // This should never be invoked, we are born in fully
2754                         // initialized state.
2755
2756                         return this;
2757                 }
2758
2759                 public override void Emit (EmitContext ec)
2760                 {
2761                         Type t = type;
2762                         ILGenerator ig = ec.ig;
2763                         
2764                         base.Emit (ec);
2765                         ig.Emit (OpCodes.Unbox, t);
2766
2767                         LoadFromPtr (ig, t);
2768                 }
2769         }
2770         
2771         /// <summary>
2772         ///   This is used to perform explicit numeric conversions.
2773         ///
2774         ///   Explicit numeric conversions might trigger exceptions in a checked
2775         ///   context, so they should generate the conv.ovf opcodes instead of
2776         ///   conv opcodes.
2777         /// </summary>
2778         public class ConvCast : EmptyCast {
2779                 public enum Mode : byte {
2780                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2781                         U1_I1, U1_CH,
2782                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2783                         U2_I1, U2_U1, U2_I2, U2_CH,
2784                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2785                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2786                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2787                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2788                         CH_I1, CH_U1, CH_I2,
2789                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2790                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2791                 }
2792
2793                 Mode mode;
2794                 bool checked_state;
2795                 
2796                 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2797                         : base (child, return_type)
2798                 {
2799                         checked_state = ec.CheckState;
2800                         mode = m;
2801                 }
2802
2803                 public override Expression DoResolve (EmitContext ec)
2804                 {
2805                         // This should never be invoked, we are born in fully
2806                         // initialized state.
2807
2808                         return this;
2809                 }
2810
2811                 public override void Emit (EmitContext ec)
2812                 {
2813                         ILGenerator ig = ec.ig;
2814                         
2815                         base.Emit (ec);
2816
2817                         if (checked_state){
2818                                 switch (mode){
2819                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2820                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2821                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2822                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2823                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2824
2825                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2826                                 case Mode.U1_CH: /* nothing */ break;
2827
2828                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2829                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2830                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2831                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2832                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2833                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2834
2835                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2836                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2837                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2838                                 case Mode.U2_CH: /* nothing */ break;
2839
2840                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2841                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2842                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2843                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2844                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2845                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2846                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2847
2848                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2849                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2850                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2851                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2852                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2853                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2854
2855                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2856                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2857                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2858                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2859                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2860                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2861                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2862                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2863
2864                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2865                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2866                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2867                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2868                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2869                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2870                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2871                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2872
2873                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2874                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2875                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2876
2877                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2878                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2879                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2880                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2881                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2882                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2883                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2884                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2885                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2886
2887                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2888                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2889                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2890                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2891                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2892                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2893                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2894                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2895                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2896                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2897                                 }
2898                         } else {
2899                                 switch (mode){
2900                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2901                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2902                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2903                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2904                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2905
2906                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2907                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2908
2909                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2910                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2911                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2912                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2913                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2914                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2915
2916                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2917                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2918                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2919                                 case Mode.U2_CH: /* nothing */ break;
2920
2921                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2922                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2923                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2924                                 case Mode.I4_U4: /* nothing */ break;
2925                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2926                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2927                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2928
2929                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2930                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2931                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2932                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2933                                 case Mode.U4_I4: /* nothing */ break;
2934                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2935
2936                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2937                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2938                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2939                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2940                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2941                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2942                                 case Mode.I8_U8: /* nothing */ break;
2943                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2944
2945                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2946                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2947                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2948                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2949                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2950                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2951                                 case Mode.U8_I8: /* nothing */ break;
2952                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2953
2954                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2955                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2956                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2957
2958                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2959                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2960                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2961                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2962                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2963                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2964                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2965                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2966                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2967
2968                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2969                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2970                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2971                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2972                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2973                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2974                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2975                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2976                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2977                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2978                                 }
2979                         }
2980                 }
2981         }
2982         
2983         public class OpcodeCast : EmptyCast {
2984                 OpCode op, op2;
2985                 bool second_valid;
2986                 
2987                 public OpcodeCast (Expression child, Type return_type, OpCode op)
2988                         : base (child, return_type)
2989                         
2990                 {
2991                         this.op = op;
2992                         second_valid = false;
2993                 }
2994
2995                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2996                         : base (child, return_type)
2997                         
2998                 {
2999                         this.op = op;
3000                         this.op2 = op2;
3001                         second_valid = true;
3002                 }
3003
3004                 public override Expression DoResolve (EmitContext ec)
3005                 {
3006                         // This should never be invoked, we are born in fully
3007                         // initialized state.
3008
3009                         return this;
3010                 }
3011
3012                 public override void Emit (EmitContext ec)
3013                 {
3014                         base.Emit (ec);
3015                         ec.ig.Emit (op);
3016
3017                         if (second_valid)
3018                                 ec.ig.Emit (op2);
3019                 }                       
3020         }
3021
3022         /// <summary>
3023         ///   This kind of cast is used to encapsulate a child and cast it
3024         ///   to the class requested
3025         /// </summary>
3026         public class ClassCast : EmptyCast {
3027                 public ClassCast (Expression child, Type return_type)
3028                         : base (child, return_type)
3029                         
3030                 {
3031                 }
3032
3033                 public override Expression DoResolve (EmitContext ec)
3034                 {
3035                         // This should never be invoked, we are born in fully
3036                         // initialized state.
3037
3038                         return this;
3039                 }
3040
3041                 public override void Emit (EmitContext ec)
3042                 {
3043                         base.Emit (ec);
3044
3045                         ec.ig.Emit (OpCodes.Castclass, type);
3046                 }                       
3047                 
3048         }
3049         
3050         /// <summary>
3051         ///   SimpleName expressions are initially formed of a single
3052         ///   word and it only happens at the beginning of the expression.
3053         /// </summary>
3054         ///
3055         /// <remarks>
3056         ///   The expression will try to be bound to a Field, a Method
3057         ///   group or a Property.  If those fail we pass the name to our
3058         ///   caller and the SimpleName is compounded to perform a type
3059         ///   lookup.  The idea behind this process is that we want to avoid
3060         ///   creating a namespace map from the assemblies, as that requires
3061         ///   the GetExportedTypes function to be called and a hashtable to
3062         ///   be constructed which reduces startup time.  If later we find
3063         ///   that this is slower, we should create a `NamespaceExpr' expression
3064         ///   that fully participates in the resolution process. 
3065         ///   
3066         ///   For example `System.Console.WriteLine' is decomposed into
3067         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3068         ///   
3069         ///   The first SimpleName wont produce a match on its own, so it will
3070         ///   be turned into:
3071         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
3072         ///   
3073         ///   System.Console will produce a TypeExpr match.
3074         ///   
3075         ///   The downside of this is that we might be hitting `LookupType' too many
3076         ///   times with this scheme.
3077         /// </remarks>
3078         public class SimpleName : Expression {
3079                 public readonly string Name;
3080                 public readonly Location Location;
3081                 
3082                 public SimpleName (string name, Location l)
3083                 {
3084                         Name = name;
3085                         Location = l;
3086                 }
3087
3088                 public static void Error120 (Location l, string name)
3089                 {
3090                         Report.Error (
3091                                 120, l,
3092                                 "An object reference is required " +
3093                                 "for the non-static field `"+name+"'");
3094                 }
3095                 
3096                 //
3097                 // Checks whether we are trying to access an instance
3098                 // property, method or field from a static body.
3099                 //
3100                 Expression MemberStaticCheck (Expression e)
3101                 {
3102                         if (e is FieldExpr){
3103                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3104                                 
3105                                 if (!fi.IsStatic){
3106                                         Error120 (Location, Name);
3107                                         return null;
3108                                 }
3109                         } else if (e is MethodGroupExpr){
3110                                 MethodGroupExpr mg = (MethodGroupExpr) e;
3111
3112                                 if (!mg.RemoveInstanceMethods ()){
3113                                         Error120 (Location, mg.Methods [0].Name);
3114                                         return null;
3115                                 }
3116                                 return e;
3117                         } else if (e is PropertyExpr){
3118                                 if (!((PropertyExpr) e).IsStatic){
3119                                         Error120 (Location, Name);
3120                                         return null;
3121                                 }
3122                         } else if (e is EventExpr) {
3123                                 if (!((EventExpr) e).IsStatic) {
3124                                         Error120 (Location, Name);
3125                                         return null;
3126                                 }
3127                         }
3128
3129                         return e;
3130                 }
3131                 
3132                 public override Expression DoResolve (EmitContext ec)
3133                 {
3134                         return SimpleNameResolve (ec, null, false);
3135                 }
3136
3137                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3138                 {
3139                         return SimpleNameResolve (ec, right_side, false);
3140                 }
3141                 
3142
3143                 public Expression DoResolveAllowStatic (EmitContext ec)
3144                 {
3145                         return SimpleNameResolve (ec, null, true);
3146                 }
3147
3148                 /// <remarks>
3149                 ///   7.5.2: Simple Names. 
3150                 ///
3151                 ///   Local Variables and Parameters are handled at
3152                 ///   parse time, so they never occur as SimpleNames.
3153                 ///
3154                 ///   The `allow_static' flag is used by MemberAccess only
3155                 ///   and it is used to inform us that it is ok for us to 
3156                 ///   avoid the static check, because MemberAccess might end
3157                 ///   up resolving the Name as a Type name and the access as
3158                 ///   a static type access.
3159                 ///
3160                 ///   ie: Type Type; .... { Type.GetType (""); }
3161                 ///
3162                 ///   Type is both an instance variable and a Type;  Type.GetType
3163                 ///   is the static method not an instance method of type.
3164                 /// </remarks>
3165                 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3166                 {
3167                         Expression e = null;
3168
3169                         //
3170                         // Stage 1: Performed by the parser (binding to locals or parameters).
3171                         //
3172                         if (!ec.OnlyLookupTypes){
3173                                 Block current_block = ec.CurrentBlock;
3174                                 if (current_block != null && current_block.IsVariableDefined (Name)){
3175                                         LocalVariableReference var;
3176                                         
3177                                         var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
3178
3179                                         if (right_side != null)
3180                                                 return var.ResolveLValue (ec, right_side);
3181                                         else
3182                                                 return var.Resolve (ec);
3183                                 }
3184                         
3185                                 //
3186                                 // Stage 2: Lookup members 
3187                                 //
3188
3189                                 //
3190                                 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3191                                 // Hence we have two different cases
3192                                 //
3193
3194                                 DeclSpace lookup_ds = ec.DeclSpace;
3195                                 do {
3196                                         if (lookup_ds.TypeBuilder == null)
3197                                                 break;
3198
3199                                         e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
3200                                         if (e != null)
3201                                                 break;
3202
3203                                         //
3204                                         // Classes/structs keep looking, enums break
3205                                         //
3206                                         if (lookup_ds is TypeContainer)
3207                                                 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3208                                         else
3209                                                 break;
3210                                 } while (lookup_ds != null);
3211                                 
3212                                 if (e == null && ec.ContainerType != null)
3213                                         e = MemberLookup (ec, ec.ContainerType, Name, Location);
3214                         }
3215
3216                         // Continuation of stage 2
3217                         if (e == null){
3218                                 //
3219                                 // Stage 3: Lookup symbol in the various namespaces. 
3220                                 //
3221                                 DeclSpace ds = ec.DeclSpace;
3222                                 Type t;
3223                                 string alias_value;
3224
3225                                 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
3226                                         return new TypeExpr (t);
3227                                 
3228                                 //
3229                                 // Stage 2 part b: Lookup up if we are an alias to a type
3230                                 // or a namespace.
3231                                 //
3232                                 // Since we are cheating: we only do the Alias lookup for
3233                                 // namespaces if the name does not include any dots in it
3234                                 //
3235                                 
3236                                 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3237                                 // System.Console.WriteLine (Name + " --> " + alias_value);
3238                                         if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3239                                             != null)
3240                                                 return new TypeExpr (t);
3241                                         
3242                                 // we have alias value, but it isn't Type, so try if it's namespace
3243                                         return new SimpleName (alias_value, Location);
3244                                 }
3245                                 
3246                                 // No match, maybe our parent can compose us
3247                                 // into something meaningful.
3248                                 return this;
3249                         }
3250                         
3251                         //
3252                         // Stage 2 continues here. 
3253                         // 
3254                         if (e is TypeExpr)
3255                                 return e;
3256
3257                         if (ec.OnlyLookupTypes)
3258                                 return null;
3259                         
3260                         if (e is FieldExpr){
3261                                 FieldExpr fe = (FieldExpr) e;
3262                                 FieldInfo fi = fe.FieldInfo;
3263
3264                                 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3265                                         UnsafeError (Location);
3266                                 }
3267                                 
3268                                 if (ec.IsStatic){
3269                                         if (!allow_static && !fi.IsStatic){
3270                                                 Error120 (Location, Name);
3271                                                 return null;
3272                                         }
3273                                 } else {
3274                                         // If we are not in static code and this
3275                                         // field is not static, set the instance to `this'.
3276
3277                                         if (!fi.IsStatic)
3278                                                 fe.InstanceExpression = ec.This;
3279                                 }
3280
3281                                 
3282                                 if (fi is FieldBuilder) {
3283                                         Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3284                                         
3285                                         if (c != null) {
3286                                                 object o = c.LookupConstantValue (ec);
3287                                                 object real_value = ((Constant)c.Expr).GetValue ();
3288                                                 return Constantify (real_value, fi.FieldType);
3289                                         }
3290                                 }
3291
3292                                 if (fi.IsLiteral) {
3293                                         Type t = fi.FieldType;
3294                                         Type decl_type = fi.DeclaringType;
3295                                         object o;
3296
3297                                         if (fi is FieldBuilder)
3298                                                 o = TypeManager.GetValue ((FieldBuilder) fi);
3299                                         else
3300                                                 o = fi.GetValue (fi);
3301                                         
3302                                         if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3303                                                 Expression enum_member = MemberLookup (
3304                                                         ec, decl_type, "value__", MemberTypes.Field,
3305                                                         AllBindingFlags, Location); 
3306
3307                                                 Enum en = TypeManager.LookupEnum (decl_type);
3308
3309                                                 Constant c;
3310                                                 if (en != null)
3311                                                         c = Constantify (o, en.UnderlyingType);
3312                                                 else 
3313                                                         c = Constantify (o, enum_member.Type);
3314                                                 
3315                                                 return new EnumConstant (c, decl_type);
3316                                         }
3317                                         
3318                                         Expression exp = Constantify (o, t);
3319                                 }
3320                                         
3321                                 return e;
3322                         }
3323
3324                         if (e is PropertyExpr) {
3325                                 PropertyExpr pe = (PropertyExpr) e;
3326
3327                                 if (ec.IsStatic){
3328                                         if (allow_static)
3329                                                 return e;
3330
3331                                         return MemberStaticCheck (e);
3332                                 } else {
3333                                         // If we are not in static code and this
3334                                         // field is not static, set the instance to `this'.
3335
3336                                         if (!pe.IsStatic)
3337                                                 pe.InstanceExpression = ec.This;
3338                                 }
3339
3340                                 return e;
3341                         }
3342
3343                         if (e is EventExpr) {
3344                                 //
3345                                 // If the event is local to this class, we transform ourselves into
3346                                 // a FieldExpr
3347                                 //
3348                                 EventExpr ee = (EventExpr) e;
3349
3350                                 Expression ml = MemberLookup (
3351                                         ec, ec.ContainerType, ee.EventInfo.Name,
3352                                         MemberTypes.Event, AllBindingFlags, Location);
3353
3354                                 if (ml != null) {
3355                                         MemberInfo mi = GetFieldFromEvent ((EventExpr) ml);
3356
3357                                         if (mi == null) {
3358                                                 //
3359                                                 // If this happens, then we have an event with its own
3360                                                 // accessors and private field etc so there's no need
3361                                                 // to transform ourselves : we should instead flag an error
3362                                                 //
3363                                                 Assign.error70 (ee.EventInfo, Location);
3364                                                 return null;
3365                                         }
3366
3367                                         ml = ExprClassFromMemberInfo (ec, mi, Location);
3368                                         
3369                                         if (ml == null) {
3370                                                 Report.Error (-200, Location, "Internal error!!");
3371                                                 return null;
3372                                         }
3373
3374                                         Expression instance_expr;
3375                                         
3376                                         FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3377
3378                                         if (fi.IsStatic)
3379                                                 instance_expr = null;
3380                                         else {
3381                                                 instance_expr = ec.This;
3382                                                 instance_expr = instance_expr.Resolve (ec);
3383                                         } 
3384                                         
3385                                         return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3386                                 }
3387                         }
3388                                 
3389                         
3390                         if (ec.IsStatic){
3391                                 if (allow_static)
3392                                         return e;
3393
3394                                 return MemberStaticCheck (e);
3395                         } else
3396                                 return e;
3397                 }
3398
3399                 
3400                 
3401                 public override void Emit (EmitContext ec)
3402                 {
3403                         //
3404                         // If this is ever reached, then we failed to
3405                         // find the name as a namespace
3406                         //
3407
3408                         Error (103, Location, "The name `" + Name +
3409                                "' does not exist in the class `" +
3410                                ec.DeclSpace.Name + "'");
3411                 }
3412         }
3413         
3414         /// <summary>
3415         ///   Fully resolved expression that evaluates to a type
3416         /// </summary>
3417         public class TypeExpr : Expression {
3418                 public TypeExpr (Type t)
3419                 {
3420                         Type = t;
3421                         eclass = ExprClass.Type;
3422                 }
3423
3424                 override public Expression DoResolve (EmitContext ec)
3425                 {
3426                         return this;
3427                 }
3428
3429                 override public void Emit (EmitContext ec)
3430                 {
3431                         throw new Exception ("Implement me");
3432                 }
3433         }
3434
3435         /// <summary>
3436         ///   MethodGroup Expression.
3437         ///  
3438         ///   This is a fully resolved expression that evaluates to a type
3439         /// </summary>
3440         public class MethodGroupExpr : Expression {
3441                 public MethodBase [] Methods;
3442                 Location loc;
3443                 Expression instance_expression = null;
3444                 
3445                 public MethodGroupExpr (MemberInfo [] mi, Location l)
3446                 {
3447                         Methods = new MethodBase [mi.Length];
3448                         mi.CopyTo (Methods, 0);
3449                         eclass = ExprClass.MethodGroup;
3450                         type = TypeManager.object_type;
3451                         loc = l;
3452                 }
3453
3454                 public MethodGroupExpr (ArrayList list, Location l)
3455                 {
3456                         Methods = new MethodBase [list.Count];
3457
3458                         try {
3459                                 list.CopyTo (Methods, 0);
3460                         } catch {
3461                                 foreach (MemberInfo m in list){
3462                                         if (!(m is MethodBase)){
3463                                                 Console.WriteLine ("Name " + m.Name);
3464                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3465                                         }
3466                                 }
3467                                 throw;
3468                         }
3469                         loc = l;
3470                         eclass = ExprClass.MethodGroup;
3471                         type = TypeManager.object_type;
3472                 }
3473                 
3474                 //
3475                 // `A method group may have associated an instance expression' 
3476                 // 
3477                 public Expression InstanceExpression {
3478                         get {
3479                                 return instance_expression;
3480                         }
3481
3482                         set {
3483                                 instance_expression = value;
3484                         }
3485                 }
3486                 
3487                 override public Expression DoResolve (EmitContext ec)
3488                 {
3489                         return this;
3490                 }
3491
3492                 public void ReportUsageError ()
3493                 {
3494                         Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3495                                       Methods [0].Name + "()' is referenced without parentheses");
3496                 }
3497
3498                 override public void Emit (EmitContext ec)
3499                 {
3500                         ReportUsageError ();
3501                 }
3502
3503                 bool RemoveMethods (bool keep_static)
3504                 {
3505                         ArrayList smethods = new ArrayList ();
3506
3507                         foreach (MethodBase mb in Methods){
3508                                 if (mb.IsStatic == keep_static)
3509                                         smethods.Add (mb);
3510                         }
3511
3512                         if (smethods.Count == 0)
3513                                 return false;
3514
3515                         Methods = new MethodBase [smethods.Count];
3516                         smethods.CopyTo (Methods, 0);
3517
3518                         return true;
3519                 }
3520                 
3521                 /// <summary>
3522                 ///   Removes any instance methods from the MethodGroup, returns
3523                 ///   false if the resulting set is empty.
3524                 /// </summary>
3525                 public bool RemoveInstanceMethods ()
3526                 {
3527                         return RemoveMethods (true);
3528                 }
3529
3530                 /// <summary>
3531                 ///   Removes any static methods from the MethodGroup, returns
3532                 ///   false if the resulting set is empty.
3533                 /// </summary>
3534                 public bool RemoveStaticMethods ()
3535                 {
3536                         return RemoveMethods (false);
3537                 }
3538         }
3539
3540         /// <summary>
3541         ///   Fully resolved expression that evaluates to a Field
3542         /// </summary>
3543         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3544                 public readonly FieldInfo FieldInfo;
3545                 public Expression InstanceExpression;
3546                 Location loc;
3547                 
3548                 public FieldExpr (FieldInfo fi, Location l)
3549                 {
3550                         FieldInfo = fi;
3551                         eclass = ExprClass.Variable;
3552                         type = fi.FieldType;
3553                         loc = l;
3554                 }
3555
3556                 override public Expression DoResolve (EmitContext ec)
3557                 {
3558                         if (!FieldInfo.IsStatic){
3559                                 if (InstanceExpression == null){
3560                                         throw new Exception ("non-static FieldExpr without instance var\n" +
3561                                                              "You have to assign the Instance variable\n" +
3562                                                              "Of the FieldExpr to set this\n");
3563                                 }
3564
3565                                 InstanceExpression = InstanceExpression.Resolve (ec);
3566                                 if (InstanceExpression == null)
3567                                         return null;
3568                         }
3569
3570                         return this;
3571                 }
3572
3573                 void Report_AssignToReadonly (bool is_instance)
3574                 {
3575                         string msg;
3576                         
3577                         if (is_instance)
3578                                 msg = "Readonly field can not be assigned outside " +
3579                                 "of constructor or variable initializer";
3580                         else
3581                                 msg = "A static readonly field can only be assigned in " +
3582                                 "a static constructor";
3583
3584                         Report.Error (is_instance ? 191 : 198, loc, msg);
3585                 }
3586                 
3587                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3588                 {
3589                         Expression e = DoResolve (ec);
3590
3591                         if (e == null)
3592                                 return null;
3593                         
3594                         if (!FieldInfo.IsInitOnly)
3595                                 return this;
3596
3597                         //
3598                         // InitOnly fields can only be assigned in constructors
3599                         //
3600
3601                         if (ec.IsConstructor)
3602                                 return this;
3603
3604                         Report_AssignToReadonly (true);
3605                         
3606                         return null;
3607                 }
3608
3609                 override public void Emit (EmitContext ec)
3610                 {
3611                         ILGenerator ig = ec.ig;
3612                         bool is_volatile = false;
3613                                 
3614                         if (FieldInfo is FieldBuilder){
3615                                 FieldBase f = TypeManager.GetField (FieldInfo);
3616
3617                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3618                                         is_volatile = true;
3619                                 
3620                                 f.status |= Field.Status.USED;
3621                         }
3622                         
3623                         if (FieldInfo.IsStatic){
3624                                 if (is_volatile)
3625                                         ig.Emit (OpCodes.Volatile);
3626                                 
3627                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3628                         } else {
3629                                 if (InstanceExpression.Type.IsValueType){
3630                                         IMemoryLocation ml;
3631                                         LocalTemporary tempo = null;
3632                                         
3633                                         if (!(InstanceExpression is IMemoryLocation)){
3634                                                 tempo = new LocalTemporary (
3635                                                         ec, InstanceExpression.Type);
3636
3637                                                 InstanceExpression.Emit (ec);
3638                                                 tempo.Store (ec);
3639                                                 ml = tempo;
3640                                         } else
3641                                                 ml = (IMemoryLocation) InstanceExpression;
3642
3643                                         ml.AddressOf (ec, AddressOp.Load);
3644                                 } else 
3645                                         InstanceExpression.Emit (ec);
3646
3647                                 if (is_volatile)
3648                                         ig.Emit (OpCodes.Volatile);
3649                                 
3650                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
3651                         }
3652                 }
3653
3654                 public void EmitAssign (EmitContext ec, Expression source)
3655                 {
3656                         FieldAttributes fa = FieldInfo.Attributes;
3657                         bool is_static = (fa & FieldAttributes.Static) != 0;
3658                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3659                         ILGenerator ig = ec.ig;
3660
3661                         if (is_readonly && !ec.IsConstructor){
3662                                 Report_AssignToReadonly (!is_static);
3663                                 return;
3664                         }
3665                         
3666                         if (!is_static){
3667                                 Expression instance = InstanceExpression;
3668
3669                                 if (instance.Type.IsValueType){
3670                                         if (instance is IMemoryLocation){
3671                                                 IMemoryLocation ml = (IMemoryLocation) instance;
3672
3673                                                 ml.AddressOf (ec, AddressOp.Store);
3674                                         } else
3675                                                 throw new Exception ("The " + instance + " of type " +
3676                                                                      instance.Type +
3677                                                                      " represents a ValueType and does " +
3678                                                                      "not implement IMemoryLocation");
3679                                 } else
3680                                         instance.Emit (ec);
3681                         }
3682                         source.Emit (ec);
3683
3684                         if (FieldInfo is FieldBuilder){
3685                                 FieldBase f = TypeManager.GetField (FieldInfo);
3686                                 
3687                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3688                                         ig.Emit (OpCodes.Volatile);
3689                         }
3690                         
3691                         if (is_static)
3692                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3693                         else 
3694                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3695
3696                         if (FieldInfo is FieldBuilder){
3697                                 FieldBase f = TypeManager.GetField (FieldInfo);
3698
3699                                 f.status |= Field.Status.ASSIGNED;
3700                         }
3701                 }
3702                 
3703                 public void AddressOf (EmitContext ec, AddressOp mode)
3704                 {
3705                         ILGenerator ig = ec.ig;
3706                         
3707                         if (FieldInfo is FieldBuilder){
3708                                 FieldBase f = TypeManager.GetField (FieldInfo);
3709                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3710                                         ig.Emit (OpCodes.Volatile);
3711                         }
3712
3713                         if (FieldInfo is FieldBuilder){
3714                                 FieldBase f = TypeManager.GetField (FieldInfo);
3715
3716                                 if ((mode & AddressOp.Store) != 0)
3717                                         f.status |= Field.Status.ASSIGNED;
3718                                 if ((mode & AddressOp.Load) != 0)
3719                                         f.status |= Field.Status.USED;
3720                         }
3721
3722                         //
3723                         // Handle initonly fields specially: make a copy and then
3724                         // get the address of the copy.
3725                         //
3726                         if (FieldInfo.IsInitOnly){
3727                                 LocalBuilder local;
3728                                 
3729                                 Emit (ec);
3730                                 local = ig.DeclareLocal (type);
3731                                 ig.Emit (OpCodes.Stloc, local);
3732                                 ig.Emit (OpCodes.Ldloca, local);
3733                                 return;
3734                         } 
3735
3736                         if (FieldInfo.IsStatic)
3737                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3738                         else {
3739                                 if (InstanceExpression is IMemoryLocation)
3740                                         ((IMemoryLocation)InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
3741                                 else
3742                                         InstanceExpression.Emit (ec);
3743                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
3744                         }
3745                 }
3746         }
3747         
3748         /// <summary>
3749         ///   Expression that evaluates to a Property.  The Assign class
3750         ///   might set the `Value' expression if we are in an assignment.
3751         ///
3752         ///   This is not an LValue because we need to re-write the expression, we
3753         ///   can not take data from the stack and store it.  
3754         /// </summary>
3755         public class PropertyExpr : ExpressionStatement, IAssignMethod {
3756                 public readonly PropertyInfo PropertyInfo;
3757                 public readonly bool IsStatic;
3758                 public bool IsBase;
3759                 MethodInfo [] Accessors;
3760                 Location loc;
3761                 
3762                 Expression instance_expr;
3763                 
3764                 public PropertyExpr (PropertyInfo pi, Location l)
3765                 {
3766                         PropertyInfo = pi;
3767                         eclass = ExprClass.PropertyAccess;
3768                         IsStatic = false;
3769                         loc = l;
3770                         Accessors = TypeManager.GetAccessors (pi);
3771
3772                         if (Accessors != null)
3773                                 foreach (MethodInfo mi in Accessors){
3774                                         if (mi != null)
3775                                                 if (mi.IsStatic)
3776                                                         IsStatic = true;
3777                                 }
3778                         else
3779                                 Accessors = new MethodInfo [2];
3780                         
3781                         type = TypeManager.TypeToCoreType (pi.PropertyType);
3782                 }
3783
3784                 //
3785                 // The instance expression associated with this expression
3786                 //
3787                 public Expression InstanceExpression {
3788                         set {
3789                                 instance_expr = value;
3790                         }
3791
3792                         get {
3793                                 return instance_expr;
3794                         }
3795                 }
3796
3797                 public bool VerifyAssignable ()
3798                 {
3799                         if (!PropertyInfo.CanWrite){
3800                                 Report.Error (200, loc, 
3801                                               "The property `" + PropertyInfo.Name +
3802                                               "' can not be assigned to, as it has not set accessor");
3803                                 return false;
3804                         }
3805
3806                         return true;
3807                 }
3808
3809                 override public Expression DoResolve (EmitContext ec)
3810                 {
3811                         if (!PropertyInfo.CanRead){
3812                                 Report.Error (154, loc, 
3813                                               "The property `" + PropertyInfo.Name +
3814                                               "' can not be used in " +
3815                                               "this context because it lacks a get accessor");
3816                                 return null;
3817                         }
3818
3819                         type = PropertyInfo.PropertyType;
3820
3821                         return this;
3822                 }
3823
3824                 override public void Emit (EmitContext ec)
3825                 {
3826                         MethodInfo method = Accessors [0];
3827
3828                         //
3829                         // Special case: length of single dimension array is turned into ldlen
3830                         //
3831                         if (method == TypeManager.int_array_get_length){
3832                                 Type iet = instance_expr.Type;
3833
3834                                 //
3835                                 // System.Array.Length can be called, but the Type does not
3836                                 // support invoking GetArrayRank, so test for that case first
3837                                 //
3838                                 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
3839                                         instance_expr.Emit (ec);
3840                                         ec.ig.Emit (OpCodes.Ldlen);
3841                                         return;
3842                                 }
3843                         }
3844
3845                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
3846                         
3847                 }
3848
3849                 //
3850                 // Implements the IAssignMethod interface for assignments
3851                 //
3852                 public void EmitAssign (EmitContext ec, Expression source)
3853                 {
3854                         Argument arg = new Argument (source, Argument.AType.Expression);
3855                         ArrayList args = new ArrayList ();
3856
3857                         args.Add (arg);
3858                         Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
3859                 }
3860
3861                 override public void EmitStatement (EmitContext ec)
3862                 {
3863                         Emit (ec);
3864                         ec.ig.Emit (OpCodes.Pop);
3865                 }
3866         }
3867
3868         /// <summary>
3869         ///   Fully resolved expression that evaluates to an Event
3870         /// </summary>
3871         public class EventExpr : Expression {
3872                 public readonly EventInfo EventInfo;
3873                 Location loc;
3874                 public Expression InstanceExpression;
3875
3876                 public readonly bool IsStatic;
3877
3878                 MethodInfo add_accessor, remove_accessor;
3879                 
3880                 public EventExpr (EventInfo ei, Location loc)
3881                 {
3882                         EventInfo = ei;
3883                         this.loc = loc;
3884                         eclass = ExprClass.EventAccess;
3885
3886                         add_accessor = TypeManager.GetAddMethod (ei);
3887                         remove_accessor = TypeManager.GetRemoveMethod (ei);
3888                         
3889                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
3890                                         IsStatic = true;
3891
3892                         if (EventInfo is MyEventBuilder)
3893                                 type = ((MyEventBuilder) EventInfo).EventType;
3894                         else
3895                                 type = EventInfo.EventHandlerType;
3896                 }
3897
3898                 override public Expression DoResolve (EmitContext ec)
3899                 {
3900                         // We are born fully resolved
3901                         return this;
3902                 }
3903
3904                 override public void Emit (EmitContext ec)
3905                 {
3906                         throw new Exception ("Should not happen I think");
3907                 }
3908
3909                 public void EmitAddOrRemove (EmitContext ec, Expression source)
3910                 {
3911                         Expression handler = ((Binary) source).Right;
3912                         
3913                         Argument arg = new Argument (handler, Argument.AType.Expression);
3914                         ArrayList args = new ArrayList ();
3915                                 
3916                         args.Add (arg);
3917                         
3918                         if (((Binary) source).Oper == Binary.Operator.Addition)
3919                                 Invocation.EmitCall (
3920                                         ec, false, IsStatic, InstanceExpression, add_accessor, args, loc);
3921                         else
3922                                 Invocation.EmitCall (
3923                                         ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);
3924                 }
3925         }
3926 }