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