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