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