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