Small bug fix
[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 || expr_type.IsInterface)
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                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1722                                         return null;
1723                                 else
1724                                         return new ClassCast (source, target_type);
1725                         }
1726                             
1727                         //
1728                         // From any class type S to any interface T, provides S is not sealed
1729                         // and provided S does not implement T.
1730                         //
1731                         if (target_type.IsInterface && !source_type.IsSealed) {
1732                                 
1733                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1734                                         return null;
1735                                 else
1736                                         return new ClassCast (source, target_type);
1737                                 
1738                         }
1739
1740                         //
1741                         // From any interface-type S to to any class type T, provided T is not
1742                         // sealed, or provided T implements S.
1743                         //
1744                         if (source_type.IsInterface) {
1745
1746                                 if (target_type.IsSealed)
1747                                         return null;
1748                                 
1749                                 if (TypeManager.ImplementsInterface (target_type, source_type))
1750                                         return new ClassCast (source, target_type);
1751                                 else
1752                                         return null;
1753                         }
1754                         
1755                         // From an array type S with an element type Se to an array type T with an 
1756                         // element type Te provided all the following are true:
1757                         //     * S and T differe only in element type, in other words, S and T
1758                         //       have the same number of dimensions.
1759                         //     * Both Se and Te are reference types
1760                         //     * An explicit referenc conversions exist from Se to Te
1761                         //
1762                         if (source_type.IsArray && target_type.IsArray) {
1763                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1764                                         
1765                                         Type source_element_type = source_type.GetElementType ();
1766                                         Type target_element_type = target_type.GetElementType ();
1767                                         
1768                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1769                                                 if (ExplicitReferenceConversionExists (source_element_type,
1770                                                                                        target_element_type))
1771                                                         return new ClassCast (source, target_type);
1772                                 }
1773                         }
1774                         
1775
1776                         // From System.Array to any array-type
1777                         if (source_type == TypeManager.array_type &&
1778                             target_type.IsSubclassOf (TypeManager.array_type)){
1779                                 return new ClassCast (source, target_type);
1780                         }
1781
1782                         //
1783                         // From System delegate to any delegate-type
1784                         //
1785                         if (source_type == TypeManager.delegate_type &&
1786                             target_type.IsSubclassOf (TypeManager.delegate_type))
1787                                 return new ClassCast (source, target_type);
1788
1789                         //
1790                         // From ICloneable to Array or Delegate types
1791                         //
1792                         if (source_type == TypeManager.icloneable_type &&
1793                             (target_type == TypeManager.array_type ||
1794                              target_type == TypeManager.delegate_type))
1795                                 return new ClassCast (source, target_type);
1796                         
1797                         return null;
1798                 }
1799                 
1800                 /// <summary>
1801                 ///   Performs an explicit conversion of the expression `expr' whose
1802                 ///   type is expr.Type to `target_type'.
1803                 /// </summary>
1804                 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1805                                                           Type target_type, Location loc)
1806                 {
1807                         Type expr_type = expr.Type;
1808                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1809
1810                         if (ne != null)
1811                                 return ne;
1812
1813                         ne = ConvertNumericExplicit (ec, expr, target_type);
1814                         if (ne != null)
1815                                 return ne;
1816
1817                         //
1818                         // Unboxing conversion.
1819                         //
1820                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
1821                                 return new UnboxCast (expr, target_type);
1822
1823                         //
1824                         // Enum types
1825                         //
1826                         if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1827                                 Expression e;
1828
1829                                 //
1830                                 // FIXME: Is there any reason we should have EnumConstant
1831                                 // dealt with here instead of just using always the
1832                                 // UnderlyingSystemType to wrap the type?
1833                                 //
1834                                 if (expr is EnumConstant)
1835                                         e = ((EnumConstant) expr).Child;
1836                                 else {
1837                                         e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1838                                 }
1839                                 
1840                                 Expression t = ConvertImplicit (ec, e, target_type, loc);
1841                                 if (t != null)
1842                                         return t;
1843                                 
1844                                 return ConvertNumericExplicit (ec, e, target_type);
1845                         }
1846                         
1847                         ne = ConvertReferenceExplicit (expr, target_type);
1848                         if (ne != null)
1849                                 return ne;
1850
1851                         if (ec.InUnsafe){
1852                                 if (target_type.IsPointer){
1853                                         if (expr_type.IsPointer)
1854                                                 return new EmptyCast (expr, target_type);
1855                                         
1856                                         if (expr_type == TypeManager.sbyte_type ||
1857                                             expr_type == TypeManager.byte_type ||
1858                                             expr_type == TypeManager.short_type ||
1859                                             expr_type == TypeManager.ushort_type ||
1860                                             expr_type == TypeManager.int32_type ||
1861                                             expr_type == TypeManager.uint32_type ||
1862                                             expr_type == TypeManager.uint64_type ||
1863                                             expr_type == TypeManager.int64_type)
1864                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1865                                 }
1866                                 if (expr_type.IsPointer){
1867                                         if (target_type == TypeManager.sbyte_type ||
1868                                             target_type == TypeManager.byte_type ||
1869                                             target_type == TypeManager.short_type ||
1870                                             target_type == TypeManager.ushort_type ||
1871                                             target_type == TypeManager.int32_type ||
1872                                             target_type == TypeManager.uint32_type ||
1873                                             target_type == TypeManager.uint64_type ||
1874                                             target_type == TypeManager.int64_type){
1875                                                 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1876                                                 Expression ci, ce;
1877
1878                                                 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1879
1880                                                 if (ci != null)
1881                                                         return ci;
1882
1883                                                 ce = ConvertNumericExplicit (ec, e, target_type);
1884                                                 if (ce != null)
1885                                                         return ce;
1886                                                 //
1887                                                 // We should always be able to go from an uint32
1888                                                 // implicitly or explicitly to the other integral
1889                                                 // types
1890                                                 //
1891                                                 throw new Exception ("Internal compiler error");
1892                                         }
1893                                 }
1894                         }
1895                         
1896                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1897                         if (ne != null)
1898                                 return ne;
1899
1900                         Error_CannotConvertType (loc, expr_type, target_type);
1901                         return null;
1902                 }
1903
1904                 /// <summary>
1905                 ///   Same as ConverExplicit, only it doesn't include user defined conversions
1906                 /// </summary>
1907                 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1908                                                                   Type target_type, Location l)
1909                 {
1910                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1911
1912                         if (ne != null)
1913                                 return ne;
1914
1915                         ne = ConvertNumericExplicit (ec, expr, target_type);
1916                         if (ne != null)
1917                                 return ne;
1918
1919                         ne = ConvertReferenceExplicit (expr, target_type);
1920                         if (ne != null)
1921                                 return ne;
1922
1923                         Error_CannotConvertType (l, expr.Type, target_type);
1924                         return null;
1925                 }
1926
1927                 static string ExprClassName (ExprClass c)
1928                 {
1929                         switch (c){
1930                         case ExprClass.Invalid:
1931                                 return "Invalid";
1932                         case ExprClass.Value:
1933                                 return "value";
1934                         case ExprClass.Variable:
1935                                 return "variable";
1936                         case ExprClass.Namespace:
1937                                 return "namespace";
1938                         case ExprClass.Type:
1939                                 return "type";
1940                         case ExprClass.MethodGroup:
1941                                 return "method group";
1942                         case ExprClass.PropertyAccess:
1943                                 return "property access";
1944                         case ExprClass.EventAccess:
1945                                 return "event access";
1946                         case ExprClass.IndexerAccess:
1947                                 return "indexer access";
1948                         case ExprClass.Nothing:
1949                                 return "null";
1950                         }
1951                         throw new Exception ("Should not happen");
1952                 }
1953                 
1954                 /// <summary>
1955                 ///   Reports that we were expecting `expr' to be of class `expected'
1956                 /// </summary>
1957                 protected void report118 (Location loc, Expression expr, string expected)
1958                 {
1959                         string kind = "Unknown";
1960                         
1961                         if (expr != null)
1962                                 kind = ExprClassName (expr.eclass);
1963
1964                         Error (118, loc, "Expression denotes a `" + kind +
1965                                "' where a `" + expected + "' was expected");
1966                 }
1967
1968                 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
1969                 {
1970                         Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1971                                       TypeManager.CSharpName (t));
1972                 }
1973
1974                 public static void UnsafeError (Location loc)
1975                 {
1976                         Report.Error (214, loc, "Pointers may only be used in an unsafe context");
1977                 }
1978                 
1979                 /// <summary>
1980                 ///   Converts the IntConstant, UIntConstant, LongConstant or
1981                 ///   ULongConstant into the integral target_type.   Notice
1982                 ///   that we do not return an `Expression' we do return
1983                 ///   a boxed integral type.
1984                 ///
1985                 ///   FIXME: Since I added the new constants, we need to
1986                 ///   also support conversions from CharConstant, ByteConstant,
1987                 ///   SByteConstant, UShortConstant, ShortConstant
1988                 ///
1989                 ///   This is used by the switch statement, so the domain
1990                 ///   of work is restricted to the literals above, and the
1991                 ///   targets are int32, uint32, char, byte, sbyte, ushort,
1992                 ///   short, uint64 and int64
1993                 /// </summary>
1994                 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
1995                 {
1996                         string s = "";
1997
1998                         if (c.Type == target_type)
1999                                 return ((Constant) c).GetValue ();
2000
2001                         //
2002                         // Make into one of the literals we handle, we dont really care
2003                         // about this value as we will just return a few limited types
2004                         // 
2005                         if (c is EnumConstant)
2006                                 c = ((EnumConstant)c).WidenToCompilerConstant ();
2007
2008                         if (c is IntConstant){
2009                                 int v = ((IntConstant) c).Value;
2010                                 
2011                                 if (target_type == TypeManager.uint32_type){
2012                                         if (v >= 0)
2013                                                 return (uint) v;
2014                                 } else if (target_type == TypeManager.char_type){
2015                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2016                                                 return (char) v;
2017                                 } else if (target_type == TypeManager.byte_type){
2018                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2019                                                 return (byte) v;
2020                                 } else if (target_type == TypeManager.sbyte_type){
2021                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2022                                                 return (sbyte) v;
2023                                 } else if (target_type == TypeManager.short_type){
2024                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2025                                                 return (short) v;
2026                                 } else if (target_type == TypeManager.ushort_type){
2027                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2028                                                 return (ushort) v;
2029                                 } else if (target_type == TypeManager.int64_type)
2030                                         return (long) v;
2031                                 else if (target_type == TypeManager.uint64_type){
2032                                         if (v > 0)
2033                                                 return (ulong) v;
2034                                 }
2035
2036                                 s = v.ToString ();
2037                         } else if (c is UIntConstant){
2038                                 uint v = ((UIntConstant) c).Value;
2039
2040                                 if (target_type == TypeManager.int32_type){
2041                                         if (v <= Int32.MaxValue)
2042                                                 return (int) v;
2043                                 } else if (target_type == TypeManager.char_type){
2044                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2045                                                 return (char) v;
2046                                 } else if (target_type == TypeManager.byte_type){
2047                                         if (v <= Byte.MaxValue)
2048                                                 return (byte) v;
2049                                 } else if (target_type == TypeManager.sbyte_type){
2050                                         if (v <= SByte.MaxValue)
2051                                                 return (sbyte) v;
2052                                 } else if (target_type == TypeManager.short_type){
2053                                         if (v <= UInt16.MaxValue)
2054                                                 return (short) v;
2055                                 } else if (target_type == TypeManager.ushort_type){
2056                                         if (v <= UInt16.MaxValue)
2057                                                 return (ushort) v;
2058                                 } else if (target_type == TypeManager.int64_type)
2059                                         return (long) v;
2060                                 else if (target_type == TypeManager.uint64_type)
2061                                         return (ulong) v;
2062                                 s = v.ToString ();
2063                         } else if (c is LongConstant){ 
2064                                 long v = ((LongConstant) c).Value;
2065
2066                                 if (target_type == TypeManager.int32_type){
2067                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2068                                                 return (int) v;
2069                                 } else if (target_type == TypeManager.uint32_type){
2070                                         if (v >= 0 && v <= UInt32.MaxValue)
2071                                                 return (uint) v;
2072                                 } else if (target_type == TypeManager.char_type){
2073                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2074                                                 return (char) v;
2075                                 } else if (target_type == TypeManager.byte_type){
2076                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2077                                                 return (byte) v;
2078                                 } else if (target_type == TypeManager.sbyte_type){
2079                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2080                                                 return (sbyte) v;
2081                                 } else if (target_type == TypeManager.short_type){
2082                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2083                                                 return (short) v;
2084                                 } else if (target_type == TypeManager.ushort_type){
2085                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2086                                                 return (ushort) v;
2087                                 } else if (target_type == TypeManager.uint64_type){
2088                                         if (v > 0)
2089                                                 return (ulong) v;
2090                                 }
2091                                 s = v.ToString ();
2092                         } else if (c is ULongConstant){
2093                                 ulong v = ((ULongConstant) c).Value;
2094
2095                                 if (target_type == TypeManager.int32_type){
2096                                         if (v <= Int32.MaxValue)
2097                                                 return (int) v;
2098                                 } else if (target_type == TypeManager.uint32_type){
2099                                         if (v <= UInt32.MaxValue)
2100                                                 return (uint) v;
2101                                 } else if (target_type == TypeManager.char_type){
2102                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2103                                                 return (char) v;
2104                                 } else if (target_type == TypeManager.byte_type){
2105                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2106                                                 return (byte) v;
2107                                 } else if (target_type == TypeManager.sbyte_type){
2108                                         if (v <= (int) SByte.MaxValue)
2109                                                 return (sbyte) v;
2110                                 } else if (target_type == TypeManager.short_type){
2111                                         if (v <= UInt16.MaxValue)
2112                                                 return (short) v;
2113                                 } else if (target_type == TypeManager.ushort_type){
2114                                         if (v <= UInt16.MaxValue)
2115                                                 return (ushort) v;
2116                                 } else if (target_type == TypeManager.int64_type){
2117                                         if (v <= Int64.MaxValue)
2118                                                 return (long) v;
2119                                 }
2120                                 s = v.ToString ();
2121                         } else if (c is ByteConstant){
2122                                 byte v = ((ByteConstant) c).Value;
2123                                 
2124                                 if (target_type == TypeManager.int32_type)
2125                                         return (int) v;
2126                                 else if (target_type == TypeManager.uint32_type)
2127                                         return (uint) v;
2128                                 else if (target_type == TypeManager.char_type)
2129                                         return (char) v;
2130                                 else if (target_type == TypeManager.sbyte_type){
2131                                         if (v <= SByte.MaxValue)
2132                                                 return (sbyte) v;
2133                                 } else if (target_type == TypeManager.short_type)
2134                                         return (short) v;
2135                                 else if (target_type == TypeManager.ushort_type)
2136                                         return (ushort) v;
2137                                 else if (target_type == TypeManager.int64_type)
2138                                         return (long) v;
2139                                 else if (target_type == TypeManager.uint64_type)
2140                                         return (ulong) v;
2141                                 s = v.ToString ();
2142                         } else if (c is SByteConstant){
2143                                 sbyte v = ((SByteConstant) c).Value;
2144                                 
2145                                 if (target_type == TypeManager.int32_type)
2146                                         return (int) v;
2147                                 else if (target_type == TypeManager.uint32_type){
2148                                         if (v >= 0)
2149                                                 return (uint) v;
2150                                 } else if (target_type == TypeManager.char_type){
2151                                         if (v >= 0)
2152                                                 return (char) v;
2153                                 } else if (target_type == TypeManager.byte_type){
2154                                         if (v >= 0)
2155                                                 return (byte) v;
2156                                 } else if (target_type == TypeManager.short_type)
2157                                         return (short) v;
2158                                 else if (target_type == TypeManager.ushort_type){
2159                                         if (v >= 0)
2160                                                 return (ushort) v;
2161                                 } else if (target_type == TypeManager.int64_type)
2162                                         return (long) v;
2163                                 else if (target_type == TypeManager.uint64_type){
2164                                         if (v >= 0)
2165                                                 return (ulong) v;
2166                                 }
2167                                 s = v.ToString ();
2168                         } else if (c is ShortConstant){
2169                                 short v = ((ShortConstant) c).Value;
2170                                 
2171                                 if (target_type == TypeManager.int32_type){
2172                                         return (int) v;
2173                                 } else if (target_type == TypeManager.uint32_type){
2174                                         if (v >= 0)
2175                                                 return (uint) v;
2176                                 } else if (target_type == TypeManager.char_type){
2177                                         if (v >= 0)
2178                                                 return (char) v;
2179                                 } else if (target_type == TypeManager.byte_type){
2180                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2181                                                 return (byte) v;
2182                                 } else if (target_type == TypeManager.sbyte_type){
2183                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2184                                                 return (sbyte) v;
2185                                 } else if (target_type == TypeManager.ushort_type){
2186                                         if (v >= 0)
2187                                                 return (ushort) v;
2188                                 } else if (target_type == TypeManager.int64_type)
2189                                         return (long) v;
2190                                 else if (target_type == TypeManager.uint64_type)
2191                                         return (ulong) v;
2192
2193                                 s = v.ToString ();
2194                         } else if (c is UShortConstant){
2195                                 ushort v = ((UShortConstant) c).Value;
2196                                 
2197                                 if (target_type == TypeManager.int32_type)
2198                                         return (int) v;
2199                                 else if (target_type == TypeManager.uint32_type)
2200                                         return (uint) v;
2201                                 else if (target_type == TypeManager.char_type){
2202                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2203                                                 return (char) v;
2204                                 } else if (target_type == TypeManager.byte_type){
2205                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2206                                                 return (byte) v;
2207                                 } else if (target_type == TypeManager.sbyte_type){
2208                                         if (v <= SByte.MaxValue)
2209                                                 return (byte) v;
2210                                 } else if (target_type == TypeManager.short_type){
2211                                         if (v <= Int16.MaxValue)
2212                                                 return (short) v;
2213                                 } else if (target_type == TypeManager.int64_type)
2214                                         return (long) v;
2215                                 else if (target_type == TypeManager.uint64_type)
2216                                         return (ulong) v;
2217
2218                                 s = v.ToString ();
2219                         } else if (c is CharConstant){
2220                                 char v = ((CharConstant) c).Value;
2221                                 
2222                                 if (target_type == TypeManager.int32_type)
2223                                         return (int) v;
2224                                 else if (target_type == TypeManager.uint32_type)
2225                                         return (uint) v;
2226                                 else if (target_type == TypeManager.byte_type){
2227                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2228                                                 return (byte) v;
2229                                 } else if (target_type == TypeManager.sbyte_type){
2230                                         if (v <= SByte.MaxValue)
2231                                                 return (sbyte) v;
2232                                 } else if (target_type == TypeManager.short_type){
2233                                         if (v <= Int16.MaxValue)
2234                                                 return (short) v;
2235                                 } else if (target_type == TypeManager.ushort_type)
2236                                         return (short) v;
2237                                 else if (target_type == TypeManager.int64_type)
2238                                         return (long) v;
2239                                 else if (target_type == TypeManager.uint64_type)
2240                                         return (ulong) v;
2241
2242                                 s = v.ToString ();
2243                         }
2244                         Error_ConstantValueCannotBeConverted (loc, s, target_type);
2245                         return null;
2246                 }
2247
2248                 //
2249                 // Load the object from the pointer.  The `IsReference' is used
2250                 // to control whether we should use Ldind_Ref or LdObj if the
2251                 // value is not a `core' type.
2252                 //
2253                 // Maybe we should try to extract this infromation form the type?
2254                 // TODO: Maybe this is a bug.  The reason we have this flag is because
2255                 // I had almost identical code in ParameterReference (for handling
2256                 // references) and in UnboxCast.
2257                 //
2258                 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2259                 {
2260                         if (t == TypeManager.int32_type)
2261                                 ig.Emit (OpCodes.Ldind_I4);
2262                         else if (t == TypeManager.uint32_type)
2263                                 ig.Emit (OpCodes.Ldind_U4);
2264                         else if (t == TypeManager.short_type)
2265                                 ig.Emit (OpCodes.Ldind_I2);
2266                         else if (t == TypeManager.ushort_type)
2267                                 ig.Emit (OpCodes.Ldind_U2);
2268                         else if (t == TypeManager.char_type)
2269                                 ig.Emit (OpCodes.Ldind_U2);
2270                         else if (t == TypeManager.byte_type)
2271                                 ig.Emit (OpCodes.Ldind_U1);
2272                         else if (t == TypeManager.sbyte_type)
2273                                 ig.Emit (OpCodes.Ldind_I1);
2274                         else if (t == TypeManager.uint64_type)
2275                                 ig.Emit (OpCodes.Ldind_I8);
2276                         else if (t == TypeManager.int64_type)
2277                                 ig.Emit (OpCodes.Ldind_I8);
2278                         else if (t == TypeManager.float_type)
2279                                 ig.Emit (OpCodes.Ldind_R4);
2280                         else if (t == TypeManager.double_type)
2281                                 ig.Emit (OpCodes.Ldind_R8);
2282                         else if (t == TypeManager.bool_type)
2283                                 ig.Emit (OpCodes.Ldind_I1);
2284                         else if (t == TypeManager.intptr_type)
2285                                 ig.Emit (OpCodes.Ldind_I);
2286                         else if (TypeManager.IsEnumType (t)){
2287                                 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2288                         } else {
2289                                 if (IsReference)
2290                                         ig.Emit (OpCodes.Ldind_Ref);
2291                                 else 
2292                                         ig.Emit (OpCodes.Ldobj, t);
2293                         }
2294                 }
2295
2296                 //
2297                 // The stack contains the pointer and the value of type `type'
2298                 //
2299                 public static void StoreFromPtr (ILGenerator ig, Type type)
2300                 {
2301                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2302                                 ig.Emit (OpCodes.Stind_I4);
2303                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2304                                 ig.Emit (OpCodes.Stind_I8);
2305                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2306                                  type == TypeManager.ushort_type)
2307                                 ig.Emit (OpCodes.Stind_I2);
2308                         else if (type == TypeManager.float_type)
2309                                 ig.Emit (OpCodes.Stind_R4);
2310                         else if (type == TypeManager.double_type)
2311                                 ig.Emit (OpCodes.Stind_R8);
2312                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2313                                  type == TypeManager.bool_type)
2314                                 ig.Emit (OpCodes.Stind_I1);
2315                         else if (type == TypeManager.intptr_type)
2316                                 ig.Emit (OpCodes.Stind_I);
2317                         else
2318                                 ig.Emit (OpCodes.Stind_Ref);
2319                 }
2320                 
2321                 //
2322                 // Returns the size of type `t' if known, otherwise, 0
2323                 //
2324                 public static int GetTypeSize (Type t)
2325                 {
2326                         if (t == TypeManager.int32_type ||
2327                             t == TypeManager.uint32_type ||
2328                             t == TypeManager.float_type)
2329                                 return 4;
2330                         else if (t == TypeManager.int64_type ||
2331                                  t == TypeManager.uint64_type ||
2332                                  t == TypeManager.double_type)
2333                                 return 8;
2334                         else if (t == TypeManager.byte_type ||
2335                                  t == TypeManager.sbyte_type ||
2336                                  t == TypeManager.bool_type)    
2337                                 return 1;
2338                         else if (t == TypeManager.short_type ||
2339                                  t == TypeManager.char_type ||
2340                                  t == TypeManager.ushort_type)
2341                                 return 2;
2342                         else
2343                                 return 0;
2344                 }
2345         }
2346
2347         /// <summary>
2348         ///   This is just a base class for expressions that can
2349         ///   appear on statements (invocations, object creation,
2350         ///   assignments, post/pre increment and decrement).  The idea
2351         ///   being that they would support an extra Emition interface that
2352         ///   does not leave a result on the stack.
2353         /// </summary>
2354         public abstract class ExpressionStatement : Expression {
2355
2356                 /// <summary>
2357                 ///   Requests the expression to be emitted in a `statement'
2358                 ///   context.  This means that no new value is left on the
2359                 ///   stack after invoking this method (constrasted with
2360                 ///   Emit that will always leave a value on the stack).
2361                 /// </summary>
2362                 public abstract void EmitStatement (EmitContext ec);
2363         }
2364
2365         /// <summary>
2366         ///   This kind of cast is used to encapsulate the child
2367         ///   whose type is child.Type into an expression that is
2368         ///   reported to return "return_type".  This is used to encapsulate
2369         ///   expressions which have compatible types, but need to be dealt
2370         ///   at higher levels with.
2371         ///
2372         ///   For example, a "byte" expression could be encapsulated in one
2373         ///   of these as an "unsigned int".  The type for the expression
2374         ///   would be "unsigned int".
2375         ///
2376         /// </summary>
2377         public class EmptyCast : Expression {
2378                 protected Expression child;
2379
2380                 public EmptyCast (Expression child, Type return_type)
2381                 {
2382                         eclass = child.eclass;
2383                         type = return_type;
2384                         this.child = child;
2385                 }
2386
2387                 public override Expression DoResolve (EmitContext ec)
2388                 {
2389                         // This should never be invoked, we are born in fully
2390                         // initialized state.
2391
2392                         return this;
2393                 }
2394
2395                 public override void Emit (EmitContext ec)
2396                 {
2397                         child.Emit (ec);
2398                 }
2399         }
2400
2401         /// <summary>
2402         ///  This class is used to wrap literals which belong inside Enums
2403         /// </summary>
2404         public class EnumConstant : Constant {
2405                 public Constant Child;
2406
2407                 public EnumConstant (Constant child, Type enum_type)
2408                 {
2409                         eclass = child.eclass;
2410                         this.Child = child;
2411                         type = enum_type;
2412                 }
2413                 
2414                 public override Expression DoResolve (EmitContext ec)
2415                 {
2416                         // This should never be invoked, we are born in fully
2417                         // initialized state.
2418
2419                         return this;
2420                 }
2421
2422                 public override void Emit (EmitContext ec)
2423                 {
2424                         Child.Emit (ec);
2425                 }
2426
2427                 public override object GetValue ()
2428                 {
2429                         return Child.GetValue ();
2430                 }
2431
2432                 //
2433                 // Converts from one of the valid underlying types for an enumeration
2434                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2435                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2436                 //
2437                 public Constant WidenToCompilerConstant ()
2438                 {
2439                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2440                         object v = ((Constant) Child).GetValue ();;
2441                         
2442                         if (t == TypeManager.int32_type)
2443                                 return new IntConstant ((int) v);
2444                         if (t == TypeManager.uint32_type)
2445                                 return new UIntConstant ((uint) v);
2446                         if (t == TypeManager.int64_type)
2447                                 return new LongConstant ((long) v);
2448                         if (t == TypeManager.uint64_type)
2449                                 return new ULongConstant ((ulong) v);
2450                         if (t == TypeManager.short_type)
2451                                 return new ShortConstant ((short) v);
2452                         if (t == TypeManager.ushort_type)
2453                                 return new UShortConstant ((ushort) v);
2454                         if (t == TypeManager.byte_type)
2455                                 return new ByteConstant ((byte) v);
2456                         if (t == TypeManager.sbyte_type)
2457                                 return new SByteConstant ((sbyte) v);
2458
2459                         throw new Exception ("Invalid enumeration underlying type: " + t);
2460                 }
2461
2462                 //
2463                 // Extracts the value in the enumeration on its native representation
2464                 //
2465                 public object GetPlainValue ()
2466                 {
2467                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2468                         object v = ((Constant) Child).GetValue ();;
2469                         
2470                         if (t == TypeManager.int32_type)
2471                                 return (int) v;
2472                         if (t == TypeManager.uint32_type)
2473                                 return (uint) v;
2474                         if (t == TypeManager.int64_type)
2475                                 return (long) v;
2476                         if (t == TypeManager.uint64_type)
2477                                 return (ulong) v;
2478                         if (t == TypeManager.short_type)
2479                                 return (short) v;
2480                         if (t == TypeManager.ushort_type)
2481                                 return (ushort) v;
2482                         if (t == TypeManager.byte_type)
2483                                 return (byte) v;
2484                         if (t == TypeManager.sbyte_type)
2485                                 return (sbyte) v;
2486
2487                         return null;
2488                 }
2489                 
2490                 public override string AsString ()
2491                 {
2492                         return Child.AsString ();
2493                 }
2494
2495                 public override DoubleConstant ConvertToDouble ()
2496                 {
2497                         return Child.ConvertToDouble ();
2498                 }
2499
2500                 public override FloatConstant ConvertToFloat ()
2501                 {
2502                         return Child.ConvertToFloat ();
2503                 }
2504
2505                 public override ULongConstant ConvertToULong ()
2506                 {
2507                         return Child.ConvertToULong ();
2508                 }
2509
2510                 public override LongConstant ConvertToLong ()
2511                 {
2512                         return Child.ConvertToLong ();
2513                 }
2514
2515                 public override UIntConstant ConvertToUInt ()
2516                 {
2517                         return Child.ConvertToUInt ();
2518                 }
2519
2520                 public override IntConstant ConvertToInt ()
2521                 {
2522                         return Child.ConvertToInt ();
2523                 }
2524         }
2525
2526         /// <summary>
2527         ///   This kind of cast is used to encapsulate Value Types in objects.
2528         ///
2529         ///   The effect of it is to box the value type emitted by the previous
2530         ///   operation.
2531         /// </summary>
2532         public class BoxedCast : EmptyCast {
2533
2534                 public BoxedCast (Expression expr)
2535                         : base (expr, TypeManager.object_type)
2536                 {
2537                 }
2538
2539                 public override Expression DoResolve (EmitContext ec)
2540                 {
2541                         // This should never be invoked, we are born in fully
2542                         // initialized state.
2543
2544                         return this;
2545                 }
2546
2547                 public override void Emit (EmitContext ec)
2548                 {
2549                         base.Emit (ec);
2550                         
2551                         ec.ig.Emit (OpCodes.Box, child.Type);
2552                 }
2553         }
2554
2555         public class UnboxCast : EmptyCast {
2556                 public UnboxCast (Expression expr, Type return_type)
2557                         : base (expr, return_type)
2558                 {
2559                 }
2560
2561                 public override Expression DoResolve (EmitContext ec)
2562                 {
2563                         // This should never be invoked, we are born in fully
2564                         // initialized state.
2565
2566                         return this;
2567                 }
2568
2569                 public override void Emit (EmitContext ec)
2570                 {
2571                         Type t = type;
2572                         ILGenerator ig = ec.ig;
2573                         
2574                         base.Emit (ec);
2575                         ig.Emit (OpCodes.Unbox, t);
2576
2577                         LoadFromPtr (ig, t, false);
2578                 }
2579         }
2580         
2581         /// <summary>
2582         ///   This is used to perform explicit numeric conversions.
2583         ///
2584         ///   Explicit numeric conversions might trigger exceptions in a checked
2585         ///   context, so they should generate the conv.ovf opcodes instead of
2586         ///   conv opcodes.
2587         /// </summary>
2588         public class ConvCast : EmptyCast {
2589                 public enum Mode : byte {
2590                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2591                         U1_I1, U1_CH,
2592                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2593                         U2_I1, U2_U1, U2_I2, U2_CH,
2594                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2595                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2596                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2597                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2598                         CH_I1, CH_U1, CH_I2,
2599                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2600                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2601                 }
2602
2603                 Mode mode;
2604                 
2605                 public ConvCast (Expression child, Type return_type, Mode m)
2606                         : base (child, return_type)
2607                 {
2608                         mode = m;
2609                 }
2610
2611                 public override Expression DoResolve (EmitContext ec)
2612                 {
2613                         // This should never be invoked, we are born in fully
2614                         // initialized state.
2615
2616                         return this;
2617                 }
2618
2619                 public override void Emit (EmitContext ec)
2620                 {
2621                         ILGenerator ig = ec.ig;
2622                         
2623                         base.Emit (ec);
2624
2625                         if (ec.CheckState){
2626                                 switch (mode){
2627                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2628                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2629                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2630                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2631                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2632
2633                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2634                                 case Mode.U1_CH: /* nothing */ break;
2635
2636                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2637                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2638                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2639                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2640                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2641                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2642
2643                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2644                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2645                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2646                                 case Mode.U2_CH: /* nothing */ break;
2647
2648                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2649                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2650                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2651                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2652                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2653                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2654                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2655
2656                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2657                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2658                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2659                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2660                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2661                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2662
2663                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2664                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2665                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2666                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2667                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2668                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2669                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2670                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2671
2672                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2673                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2674                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2675                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2676                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2677                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2678                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2679                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2680
2681                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2682                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2683                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2684
2685                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2686                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2687                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2688                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2689                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2690                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2691                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2692                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2693                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2694
2695                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2696                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2697                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2698                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2699                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2700                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2701                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2702                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2703                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2704                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2705                                 }
2706                         } else {
2707                                 switch (mode){
2708                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2709                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2710                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2711                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2712                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2713
2714                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2715                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2716
2717                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2718                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2719                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2720                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2721                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2722                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2723
2724                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2725                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2726                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2727                                 case Mode.U2_CH: /* nothing */ break;
2728
2729                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2730                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2731                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2732                                 case Mode.I4_U4: /* nothing */ break;
2733                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2734                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2735                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2736
2737                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2738                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2739                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2740                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2741                                 case Mode.U4_I4: /* nothing */ break;
2742                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2743
2744                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2745                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2746                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2747                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2748                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2749                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2750                                 case Mode.I8_U8: /* nothing */ break;
2751                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2752
2753                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2754                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2755                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2756                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2757                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2758                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2759                                 case Mode.U8_I8: /* nothing */ break;
2760                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2761
2762                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2763                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2764                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2765
2766                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2767                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2768                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2769                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2770                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2771                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2772                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2773                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2774                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2775
2776                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2777                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2778                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2779                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2780                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2781                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2782                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2783                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2784                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2785                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2786                                 }
2787                         }
2788                 }
2789         }
2790         
2791         public class OpcodeCast : EmptyCast {
2792                 OpCode op, op2;
2793                 bool second_valid;
2794                 
2795                 public OpcodeCast (Expression child, Type return_type, OpCode op)
2796                         : base (child, return_type)
2797                         
2798                 {
2799                         this.op = op;
2800                         second_valid = false;
2801                 }
2802
2803                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2804                         : base (child, return_type)
2805                         
2806                 {
2807                         this.op = op;
2808                         this.op2 = op2;
2809                         second_valid = true;
2810                 }
2811
2812                 public override Expression DoResolve (EmitContext ec)
2813                 {
2814                         // This should never be invoked, we are born in fully
2815                         // initialized state.
2816
2817                         return this;
2818                 }
2819
2820                 public override void Emit (EmitContext ec)
2821                 {
2822                         base.Emit (ec);
2823                         ec.ig.Emit (op);
2824
2825                         if (second_valid)
2826                                 ec.ig.Emit (op2);
2827                 }                       
2828         }
2829
2830         /// <summary>
2831         ///   This kind of cast is used to encapsulate a child and cast it
2832         ///   to the class requested
2833         /// </summary>
2834         public class ClassCast : EmptyCast {
2835                 public ClassCast (Expression child, Type return_type)
2836                         : base (child, return_type)
2837                         
2838                 {
2839                 }
2840
2841                 public override Expression DoResolve (EmitContext ec)
2842                 {
2843                         // This should never be invoked, we are born in fully
2844                         // initialized state.
2845
2846                         return this;
2847                 }
2848
2849                 public override void Emit (EmitContext ec)
2850                 {
2851                         base.Emit (ec);
2852
2853                         ec.ig.Emit (OpCodes.Castclass, type);
2854                 }                       
2855                 
2856         }
2857         
2858         /// <summary>
2859         ///   SimpleName expressions are initially formed of a single
2860         ///   word and it only happens at the beginning of the expression.
2861         /// </summary>
2862         ///
2863         /// <remarks>
2864         ///   The expression will try to be bound to a Field, a Method
2865         ///   group or a Property.  If those fail we pass the name to our
2866         ///   caller and the SimpleName is compounded to perform a type
2867         ///   lookup.  The idea behind this process is that we want to avoid
2868         ///   creating a namespace map from the assemblies, as that requires
2869         ///   the GetExportedTypes function to be called and a hashtable to
2870         ///   be constructed which reduces startup time.  If later we find
2871         ///   that this is slower, we should create a `NamespaceExpr' expression
2872         ///   that fully participates in the resolution process. 
2873         ///   
2874         ///   For example `System.Console.WriteLine' is decomposed into
2875         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2876         ///   
2877         ///   The first SimpleName wont produce a match on its own, so it will
2878         ///   be turned into:
2879         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
2880         ///   
2881         ///   System.Console will produce a TypeExpr match.
2882         ///   
2883         ///   The downside of this is that we might be hitting `LookupType' too many
2884         ///   times with this scheme.
2885         /// </remarks>
2886         public class SimpleName : Expression {
2887                 public readonly string Name;
2888                 public readonly Location Location;
2889                 
2890                 public SimpleName (string name, Location l)
2891                 {
2892                         Name = name;
2893                         Location = l;
2894                 }
2895
2896                 public static void Error120 (Location l, string name)
2897                 {
2898                         Report.Error (
2899                                 120, l,
2900                                 "An object reference is required " +
2901                                 "for the non-static field `"+name+"'");
2902                 }
2903                 
2904                 //
2905                 // Checks whether we are trying to access an instance
2906                 // property, method or field from a static body.
2907                 //
2908                 Expression MemberStaticCheck (Expression e)
2909                 {
2910                         if (e is FieldExpr){
2911                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2912                                 
2913                                 if (!fi.IsStatic){
2914                                         Error120 (Location, Name);
2915                                         return null;
2916                                 }
2917                         } else if (e is MethodGroupExpr){
2918                                 MethodGroupExpr mg = (MethodGroupExpr) e;
2919
2920                                 if (!mg.RemoveInstanceMethods ()){
2921                                         Error120 (Location, mg.Methods [0].Name);
2922                                         return null;
2923                                 }
2924                                 return e;
2925                         } else if (e is PropertyExpr){
2926                                 if (!((PropertyExpr) e).IsStatic){
2927                                         Error120 (Location, Name);
2928                                         return null;
2929                                 }
2930                         } else if (e is EventExpr) {
2931                                 if (!((EventExpr) e).IsStatic) {
2932                                         Error120 (Location, Name);
2933                                         return null;
2934                                 }
2935                         }
2936
2937                         return e;
2938                 }
2939                 
2940                 public override Expression DoResolve (EmitContext ec)
2941                 {
2942                         return SimpleNameResolve (ec, false);
2943                 }
2944
2945                 public Expression DoResolveAllowStatic (EmitContext ec)
2946                 {
2947                         return SimpleNameResolve (ec, true);
2948                 }
2949
2950                 /// <remarks>
2951                 ///   7.5.2: Simple Names. 
2952                 ///
2953                 ///   Local Variables and Parameters are handled at
2954                 ///   parse time, so they never occur as SimpleNames.
2955                 ///
2956                 ///   The `allow_static' flag is used by MemberAccess only
2957                 ///   and it is used to inform us that it is ok for us to 
2958                 ///   avoid the static check, because MemberAccess might end
2959                 ///   up resolving the Name as a Type name and the access as
2960                 ///   a static type access.
2961                 ///
2962                 ///   ie: Type Type; .... { Type.GetType (""); }
2963                 ///
2964                 ///   Type is both an instance variable and a Type;  Type.GetType
2965                 ///   is the static method not an instance method of type.
2966                 /// </remarks>
2967                 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
2968                 {
2969                         Expression e = null;
2970
2971                         //
2972                         // Stage 1: Performed by the parser (binding to locals or parameters).
2973                         //
2974
2975                         //
2976                         // Stage 2: Lookup members 
2977                         //
2978
2979                         //
2980                         // For enums, the TypeBuilder is not ec.TypeContainer.TypeBuilder
2981                         // Hence we have two different cases
2982                         //
2983                         e = MemberLookup (ec, ec.DeclSpace.TypeBuilder, Name, Location);
2984
2985                         if (e == null && ec.TypeContainer.TypeBuilder != null)
2986                                 e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
2987
2988                         if (e == null){
2989                                 //
2990                                 // Stage 3: Lookup symbol in the various namespaces. 
2991                                 //
2992                                 DeclSpace ds = ec.DeclSpace;
2993                                 Type t;
2994                                 string alias_value;
2995                                 
2996                                 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2997                                         return new TypeExpr (t);
2998                                 
2999                                 //
3000                                 // Stage 2 part b: Lookup up if we are an alias to a type
3001                                 // or a namespace.
3002                                 //
3003                                 // Since we are cheating: we only do the Alias lookup for
3004                                 // namespaces if the name does not include any dots in it
3005                                 //
3006                                 
3007                                 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
3008                                 // System.Console.WriteLine (Name + " --> " + alias_value);
3009                                         if ((t = RootContext.LookupType (ds, alias_value, true, Location))
3010                                             != null)
3011                                                 return new TypeExpr (t);
3012                                         
3013                                 // we have alias value, but it isn't Type, so try if it's namespace
3014                                         return new SimpleName (alias_value, Location);
3015                                 }
3016                                 
3017                                 // No match, maybe our parent can compose us
3018                                 // into something meaningful.
3019                                 return this;
3020                         }
3021                         
3022                         //
3023                         // Stage 2 continues here. 
3024                         // 
3025                         if (e is TypeExpr)
3026                                 return e;
3027
3028                         if (e is FieldExpr){
3029                                 FieldExpr fe = (FieldExpr) e;
3030                                 FieldInfo fi = fe.FieldInfo;
3031
3032                                 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3033                                         UnsafeError (Location);
3034                                 }
3035                                 
3036                                 if (ec.IsStatic){
3037                                         if (!allow_static && !fi.IsStatic){
3038                                                 Error120 (Location, Name);
3039                                                 return null;
3040                                         }
3041                                 } else {
3042                                         // If we are not in static code and this
3043                                         // field is not static, set the instance to `this'.
3044
3045                                         if (!fi.IsStatic)
3046                                                 fe.InstanceExpression = ec.This;
3047                                 }
3048
3049                                 
3050                                 if (fi is FieldBuilder) {
3051                                         Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3052                                         
3053                                         if (c != null) {
3054                                                 object o = c.LookupConstantValue (ec);
3055                                                 object real_value = ((Constant)c.Expr).GetValue ();
3056                                                 return Constantify (real_value, fi.FieldType);
3057                                         }
3058                                 }
3059
3060                                 return e;
3061                         }                               
3062
3063                         if (e is EventExpr) {
3064                                 //
3065                                 // If the event is local to this class, we transform ourselves into
3066                                 // a FieldExpr
3067                                 //
3068                                 EventExpr ee = (EventExpr) e;
3069
3070                                 Expression ml = MemberLookup (
3071                                         ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3072                                         MemberTypes.Event, AllBindingFlags, Location);
3073
3074                                 if (ml != null) {
3075                                         MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3076
3077                                         if (mi == null) {
3078                                                 //
3079                                                 // If this happens, then we have an event with its own
3080                                                 // accessors and private field etc so there's no need
3081                                                 // to transform ourselves : we should instead flag an error
3082                                                 //
3083                                                 Assign.error70 (ee.EventInfo, Location);
3084                                                 return null;
3085                                         }
3086
3087                                         ml = ExprClassFromMemberInfo (ec, mi, Location);
3088                                         
3089                                         if (ml == null) {
3090                                                 Report.Error (-200, Location, "Internal error!!");
3091                                                 return null;
3092                                         }
3093
3094                                         Expression instance_expr;
3095                                         
3096                                         FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3097
3098                                         if (fi.IsStatic)
3099                                                 instance_expr = null;
3100                                         else
3101                                                 instance_expr = ec.This;
3102
3103                                         instance_expr = instance_expr.Resolve (ec);
3104
3105                                         if (instance_expr != null)
3106                                                 instance_expr = instance_expr.Resolve (ec);
3107                                         
3108                                         return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3109                                 }
3110                         }
3111                                 
3112                         
3113                         if (ec.IsStatic){
3114                                 if (allow_static)
3115                                         return e;
3116
3117                                 return MemberStaticCheck (e);
3118                         } else
3119                                 return e;
3120                 }
3121
3122                 public override void Emit (EmitContext ec)
3123                 {
3124                         //
3125                         // If this is ever reached, then we failed to
3126                         // find the name as a namespace
3127                         //
3128
3129                         Error (103, Location, "The name `" + Name +
3130                                "' does not exist in the class `" +
3131                                ec.DeclSpace.Name + "'");
3132                 }
3133         }
3134         
3135         /// <summary>
3136         ///   Fully resolved expression that evaluates to a type
3137         /// </summary>
3138         public class TypeExpr : Expression {
3139                 public TypeExpr (Type t)
3140                 {
3141                         Type = t;
3142                         eclass = ExprClass.Type;
3143                 }
3144
3145                 override public Expression DoResolve (EmitContext ec)
3146                 {
3147                         return this;
3148                 }
3149
3150                 override public void Emit (EmitContext ec)
3151                 {
3152                         throw new Exception ("Implement me");
3153                 }
3154         }
3155
3156         /// <summary>
3157         ///   MethodGroup Expression.
3158         ///  
3159         ///   This is a fully resolved expression that evaluates to a type
3160         /// </summary>
3161         public class MethodGroupExpr : Expression {
3162                 public MethodBase [] Methods;
3163                 Expression instance_expression = null;
3164                 
3165                 public MethodGroupExpr (MemberInfo [] mi)
3166                 {
3167                         Methods = new MethodBase [mi.Length];
3168                         mi.CopyTo (Methods, 0);
3169                         eclass = ExprClass.MethodGroup;
3170                 }
3171
3172                 public MethodGroupExpr (ArrayList l)
3173                 {
3174                         Methods = new MethodBase [l.Count];
3175
3176                         l.CopyTo (Methods, 0);
3177                         eclass = ExprClass.MethodGroup;
3178                 }
3179                 
3180                 //
3181                 // `A method group may have associated an instance expression' 
3182                 // 
3183                 public Expression InstanceExpression {
3184                         get {
3185                                 return instance_expression;
3186                         }
3187
3188                         set {
3189                                 instance_expression = value;
3190                         }
3191                 }
3192                 
3193                 override public Expression DoResolve (EmitContext ec)
3194                 {
3195                         return this;
3196                 }
3197
3198                 override public void Emit (EmitContext ec)
3199                 {
3200                         throw new Exception ("This should never be reached");
3201                 }
3202
3203                 bool RemoveMethods (bool keep_static)
3204                 {
3205                         ArrayList smethods = new ArrayList ();
3206                         int top = Methods.Length;
3207                         int i;
3208                         
3209                         for (i = 0; i < top; i++){
3210                                 MethodBase mb = Methods [i];
3211
3212                                 if (mb.IsStatic == keep_static)
3213                                         smethods.Add (mb);
3214                         }
3215
3216                         if (smethods.Count == 0)
3217                                 return false;
3218
3219                         Methods = new MethodBase [smethods.Count];
3220                         smethods.CopyTo (Methods, 0);
3221
3222                         return true;
3223                 }
3224                 
3225                 /// <summary>
3226                 ///   Removes any instance methods from the MethodGroup, returns
3227                 ///   false if the resulting set is empty.
3228                 /// </summary>
3229                 public bool RemoveInstanceMethods ()
3230                 {
3231                         return RemoveMethods (true);
3232                 }
3233
3234                 /// <summary>
3235                 ///   Removes any static methods from the MethodGroup, returns
3236                 ///   false if the resulting set is empty.
3237                 /// </summary>
3238                 public bool RemoveStaticMethods ()
3239                 {
3240                         return RemoveMethods (false);
3241                 }
3242         }
3243
3244         /// <summary>
3245         ///   Fully resolved expression that evaluates to a Field
3246         /// </summary>
3247         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3248                 public readonly FieldInfo FieldInfo;
3249                 public Expression InstanceExpression;
3250                 Location loc;
3251                 
3252                 public FieldExpr (FieldInfo fi, Location l)
3253                 {
3254                         FieldInfo = fi;
3255                         eclass = ExprClass.Variable;
3256                         type = fi.FieldType;
3257                         loc = l;
3258                 }
3259
3260                 override public Expression DoResolve (EmitContext ec)
3261                 {
3262                         if (!FieldInfo.IsStatic){
3263                                 if (InstanceExpression == null){
3264                                         throw new Exception ("non-static FieldExpr without instance var\n" +
3265                                                              "You have to assign the Instance variable\n" +
3266                                                              "Of the FieldExpr to set this\n");
3267                                 }
3268
3269                                 InstanceExpression = InstanceExpression.Resolve (ec);
3270                                 if (InstanceExpression == null)
3271                                         return null;
3272                         }
3273
3274                         return this;
3275                 }
3276
3277                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3278                 {
3279                         Expression e = DoResolve (ec);
3280
3281                         if (e == null)
3282                                 return null;
3283                         
3284                         if (!FieldInfo.IsInitOnly)
3285                                 return this;
3286
3287                         //
3288                         // InitOnly fields can only be assigned in constructors
3289                         //
3290
3291                         if (ec.IsConstructor)
3292                                 return this;
3293
3294                         Report.Error (191, loc,
3295                                       "Readonly field can not be assigned outside " +
3296                                       "of constructor or variable initializer");
3297                         
3298                         return null;
3299                 }
3300
3301                 override public void Emit (EmitContext ec)
3302                 {
3303                         ILGenerator ig = ec.ig;
3304                         bool is_volatile = false;
3305                                 
3306                         if (FieldInfo is FieldBuilder){
3307                                 Field f = TypeManager.GetField (FieldInfo);
3308                                 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3309                                         is_volatile = true;
3310                                 
3311                                 f.status |= Field.Status.USED;
3312                         }
3313                         
3314                         if (FieldInfo.IsStatic){
3315                                 if (is_volatile)
3316                                         ig.Emit (OpCodes.Volatile);
3317                                 
3318                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3319                         } else {
3320                                 if (InstanceExpression.Type.IsValueType){
3321                                         IMemoryLocation ml;
3322                                         LocalTemporary tempo = null;
3323                                         
3324                                         if (!(InstanceExpression is IMemoryLocation)){
3325                                                 tempo = new LocalTemporary (
3326                                                         ec, InstanceExpression.Type);
3327
3328                                                 InstanceExpression.Emit (ec);
3329                                                 tempo.Store (ec);
3330                                                 ml = tempo;
3331                                         } else
3332                                                 ml = (IMemoryLocation) InstanceExpression;
3333
3334                                         ml.AddressOf (ec, AddressOp.Load);
3335                                 } else 
3336                                         InstanceExpression.Emit (ec);
3337
3338                                 if (is_volatile)
3339                                         ig.Emit (OpCodes.Volatile);
3340                                 
3341                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
3342                         }
3343                 }
3344
3345                 public void EmitAssign (EmitContext ec, Expression source)
3346                 {
3347                         bool is_static = FieldInfo.IsStatic;
3348                         ILGenerator ig = ec.ig;
3349                         
3350                         if (!is_static){
3351                                 Expression instance = InstanceExpression;
3352
3353                                 if (instance.Type.IsValueType){
3354                                         if (instance is IMemoryLocation){
3355                                                 IMemoryLocation ml = (IMemoryLocation) instance;
3356
3357                                                 ml.AddressOf (ec, AddressOp.Store);
3358                                         } else
3359                                                 throw new Exception ("The " + instance + " of type " +
3360                                                                      instance.Type +
3361                                                                      " represents a ValueType and does " +
3362                                                                      "not implement IMemoryLocation");
3363                                 } else
3364                                         instance.Emit (ec);
3365                         }
3366                         source.Emit (ec);
3367
3368                         if (FieldInfo is FieldBuilder){
3369                                 Field f = TypeManager.GetField (FieldInfo);
3370                                 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3371                                         ig.Emit (OpCodes.Volatile);
3372                         }
3373                         
3374                         if (is_static)
3375                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3376                         else 
3377                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3378
3379                         if (FieldInfo is FieldBuilder){
3380                                 Field f = TypeManager.GetField (FieldInfo);
3381
3382                                 f.status |= Field.Status.ASSIGNED;
3383                         }
3384                 }
3385                 
3386                 public void AddressOf (EmitContext ec, AddressOp mode)
3387                 {
3388                         ILGenerator ig = ec.ig;
3389                         
3390                         if (FieldInfo is FieldBuilder){
3391                                 Field f = TypeManager.GetField (FieldInfo);
3392                                 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3393                                         ig.Emit (OpCodes.Volatile);
3394                         }
3395
3396                         if (FieldInfo is FieldBuilder){
3397                                 Field f = TypeManager.GetField (FieldInfo);
3398
3399                                 if ((mode & AddressOp.Store) != 0)
3400                                         f.status |= Field.Status.ASSIGNED;
3401                                 if ((mode & AddressOp.Load) != 0)
3402                                         f.status |= Field.Status.USED;
3403                         }
3404
3405                         //
3406                         // Handle initonly fields specially: make a copy and then
3407                         // get the address of the copy.
3408                         //
3409                         if (FieldInfo.IsInitOnly){
3410                                 LocalBuilder local;
3411                                 
3412                                 Emit (ec);
3413                                 local = ig.DeclareLocal (type);
3414                                 ig.Emit (OpCodes.Stloc, local);
3415                                 ig.Emit (OpCodes.Ldloca, local);
3416                                 return;
3417                         } 
3418
3419                         if (FieldInfo.IsStatic)
3420                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3421                         else {
3422                                 InstanceExpression.Emit (ec);
3423                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
3424                         }
3425                 }
3426         }
3427         
3428         /// <summary>
3429         ///   Expression that evaluates to a Property.  The Assign class
3430         ///   might set the `Value' expression if we are in an assignment.
3431         ///
3432         ///   This is not an LValue because we need to re-write the expression, we
3433         ///   can not take data from the stack and store it.  
3434         /// </summary>
3435         public class PropertyExpr : ExpressionStatement, IAssignMethod {
3436                 public readonly PropertyInfo PropertyInfo;
3437                 public readonly bool IsStatic;
3438                 public bool IsBase;
3439                 MethodInfo [] Accessors;
3440                 Location loc;
3441                 
3442                 Expression instance_expr;
3443                 
3444                 public PropertyExpr (PropertyInfo pi, Location l)
3445                 {
3446                         PropertyInfo = pi;
3447                         eclass = ExprClass.PropertyAccess;
3448                         IsStatic = false;
3449                         loc = l;
3450                         Accessors = TypeManager.GetAccessors (pi);
3451
3452                         if (Accessors != null)
3453                                 for (int i = 0; i < Accessors.Length; i++){
3454                                         if (Accessors [i] != null)
3455                                                 if (Accessors [i].IsStatic)
3456                                                         IsStatic = true;
3457                                 }
3458                         else
3459                                 Accessors = new MethodInfo [2];
3460                         
3461                         type = pi.PropertyType;
3462                 }
3463
3464                 //
3465                 // The instance expression associated with this expression
3466                 //
3467                 public Expression InstanceExpression {
3468                         set {
3469                                 instance_expr = value;
3470                         }
3471
3472                         get {
3473                                 return instance_expr;
3474                         }
3475                 }
3476
3477                 public bool VerifyAssignable ()
3478                 {
3479                         if (!PropertyInfo.CanWrite){
3480                                 Report.Error (200, loc, 
3481                                               "The property `" + PropertyInfo.Name +
3482                                               "' can not be assigned to, as it has not set accessor");
3483                                 return false;
3484                         }
3485
3486                         return true;
3487                 }
3488
3489                 override public Expression DoResolve (EmitContext ec)
3490                 {
3491                         if (!PropertyInfo.CanRead){
3492                                 Report.Error (154, loc, 
3493                                               "The property `" + PropertyInfo.Name +
3494                                               "' can not be used in " +
3495                                               "this context because it lacks a get accessor");
3496                                 return null;
3497                         }
3498
3499                         type = PropertyInfo.PropertyType;
3500
3501                         return this;
3502                 }
3503
3504                 override public void Emit (EmitContext ec)
3505                 {
3506                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
3507                         
3508                 }
3509
3510                 //
3511                 // Implements the IAssignMethod interface for assignments
3512                 //
3513                 public void EmitAssign (EmitContext ec, Expression source)
3514                 {
3515                         Argument arg = new Argument (source, Argument.AType.Expression);
3516                         ArrayList args = new ArrayList ();
3517
3518                         args.Add (arg);
3519                         Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3520                 }
3521
3522                 override public void EmitStatement (EmitContext ec)
3523                 {
3524                         Emit (ec);
3525                         ec.ig.Emit (OpCodes.Pop);
3526                 }
3527         }
3528
3529         /// <summary>
3530         ///   Fully resolved expression that evaluates to an Event
3531         /// </summary>
3532         public class EventExpr : Expression {
3533                 public readonly EventInfo EventInfo;
3534                 Location loc;
3535                 public Expression InstanceExpression;
3536
3537                 public readonly bool IsStatic;
3538
3539                 MethodInfo add_accessor, remove_accessor;
3540                 
3541                 public EventExpr (EventInfo ei, Location loc)
3542                 {
3543                         EventInfo = ei;
3544                         this.loc = loc;
3545                         eclass = ExprClass.EventAccess;
3546
3547                         add_accessor = TypeManager.GetAddMethod (ei);
3548                         remove_accessor = TypeManager.GetRemoveMethod (ei);
3549                         
3550                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
3551                                         IsStatic = true;
3552
3553                         if (EventInfo is MyEventBuilder)
3554                                 type = ((MyEventBuilder) EventInfo).EventType;
3555                         else
3556                                 type = EventInfo.EventHandlerType;
3557                 }
3558
3559                 override public Expression DoResolve (EmitContext ec)
3560                 {
3561                         // We are born fully resolved
3562                         return this;
3563                 }
3564
3565                 override public void Emit (EmitContext ec)
3566                 {
3567                         throw new Exception ("Should not happen I think");
3568                 }
3569
3570                 public void EmitAddOrRemove (EmitContext ec, Expression source)
3571                 {
3572                         Expression handler = ((Binary) source).Right;
3573                         
3574                         Argument arg = new Argument (handler, Argument.AType.Expression);
3575                         ArrayList args = new ArrayList ();
3576                                 
3577                         args.Add (arg);
3578                         
3579                         if (((Binary) source).Oper == Binary.Operator.Addition)
3580                                 Invocation.EmitCall (
3581                                         ec, false, IsStatic, InstanceExpression, add_accessor, args);
3582                         else
3583                                 Invocation.EmitCall (
3584                                         ec, false, IsStatic, InstanceExpression, remove_accessor, args);
3585                 }
3586         }
3587 }