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