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