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