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