2001-10-26 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / expression.cs
1 //
2 // expression.cs: Expression representation for the IL tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 //
10 // Ideas:
11 //   Maybe we should make Resolve be an instance method that just calls
12 //   the virtual DoResolve function and checks conditions like the eclass
13 //   and type being set if a non-null value is returned.  For robustness
14 //   purposes.
15 //
16
17 namespace CIR {
18         using System;
19         using System.Collections;
20         using System.Diagnostics;
21         using System.Reflection;
22         using System.Reflection.Emit;
23         using System.Text;
24         
25         // <remarks>
26         //   The ExprClass class contains the is used to pass the 
27         //   classification of an expression (value, variable, namespace,
28         //   type, method group, property access, event access, indexer access,
29         //   nothing).
30         // </remarks>
31         public enum ExprClass {
32                 Invalid,
33                 
34                 Value,
35                 Variable,
36                 Namespace,
37                 Type,
38                 MethodGroup,
39                 PropertyAccess,
40                 EventAccess,
41                 IndexerAccess,
42                 Nothing, 
43         }
44
45         // <summary>
46         //   An interface provided by expressions that can be used as
47         //   LValues and can store the value on the top of the stack on
48         //   their storage
49         // </summary>
50         public interface IStackStore {
51
52                 // <summary>
53                 //   The Store method should store the contents of the top
54                 //   of the stack into the storage that is implemented by
55                 //   the particular implementation of LValue
56                 // </summary>
57                 void Store     (EmitContext ec);
58         }
59
60         // <summary>
61         //   This interface is implemented by variables
62         // </summary>
63         public interface IMemoryLocation {
64                 // <summary>
65                 //   The AddressOf method should generate code that loads
66                 //   the address of the object and leaves it on the stack
67                 // </summary>
68                 void AddressOf (EmitContext ec);
69         }
70
71         // <remarks>
72         //   Base class for expressions
73         // </remarks>
74         public abstract class Expression {
75                 protected ExprClass eclass;
76                 protected Type      type;
77                 
78                 public Type Type {
79                         get {
80                                 return type;
81                         }
82
83                         set {
84                                 type = value;
85                         }
86                 }
87
88                 public ExprClass ExprClass {
89                         get {
90                                 return eclass;
91                         }
92
93                         set {
94                                 eclass = value;
95                         }
96                 }
97
98                 // <summary>
99                 //   Utility wrapper routine for Error, just to beautify the code
100                 // </summary>
101                 static protected void Error (int error, string s)
102                 {
103                         Report.Error (error, s);
104                 }
105
106                 static protected void Error (int error, Location loc, string s)
107                 {
108                         Report.Error (error, loc, s);
109                 }
110                 
111                 // <summary>
112                 //   Utility wrapper routine for Warning, just to beautify the code
113                 // </summary>
114                 static protected void Warning (int warning, string s)
115                 {
116                         Report.Warning (warning, s);
117                 }
118
119                 // <summary>
120                 //   Performs semantic analysis on the Expression
121                 // </summary>
122                 //
123                 // <remarks>
124                 //   The Resolve method is invoked to perform the semantic analysis
125                 //   on the node.
126                 //
127                 //   The return value is an expression (it can be the
128                 //   same expression in some cases) or a new
129                 //   expression that better represents this node.
130                 //   
131                 //   For example, optimizations of Unary (LiteralInt)
132                 //   would return a new LiteralInt with a negated
133                 //   value.
134                 //   
135                 //   If there is an error during semantic analysis,
136                 //   then an error should be reported (using Report)
137                 //   and a null value should be returned.
138                 //   
139                 //   There are two side effects expected from calling
140                 //   Resolve(): the the field variable "eclass" should
141                 //   be set to any value of the enumeration
142                 //   `ExprClass' and the type variable should be set
143                 //   to a valid type (this is the type of the
144                 //   expression).
145                 // </remarks>
146                 
147                 public abstract Expression DoResolve (EmitContext ec);
148
149                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
150                 {
151                         return DoResolve (ec);
152                 }
153                 
154                 //
155                 // Currently Resolve wraps DoResolve to perform sanity
156                 // checking and assertion checking on what we expect from Resolve
157                 //
158                 public Expression Resolve (EmitContext ec)
159                 {
160                         Expression e = DoResolve (ec);
161
162                         if (e != null){
163                                 if (e is SimpleName)
164                                         return e;
165
166                                 if (e.ExprClass == ExprClass.Invalid)
167                                         throw new Exception ("Expression " + e +
168                                                              " ExprClass is Invalid after resolve");
169
170                                 if (e.ExprClass != ExprClass.MethodGroup)
171                                         if (e.type == null)
172                                                 throw new Exception ("Expression " + e +
173                                                                      " did not set its type after Resolve");
174                         }
175
176                         return e;
177                 }
178
179                 //
180                 // Currently ResolveLValue wraps DoResolveLValue to perform sanity
181                 // checking and assertion checking on what we expect from Resolve
182                 //
183                 public Expression ResolveLValue (EmitContext ec, Expression right_side)
184                 {
185                         Expression e = DoResolveLValue (ec, right_side);
186
187                         if (e != null){
188                                 if (e is SimpleName)
189                                         return e;
190
191                                 if (e.ExprClass == ExprClass.Invalid)
192                                         throw new Exception ("Expression " + e +
193                                                              " ExprClass is Invalid after resolve");
194
195                                 if (e.ExprClass != ExprClass.MethodGroup)
196                                         if (e.type == null)
197                                                 throw new Exception ("Expression " + e +
198                                                                      " did not set its type after Resolve");
199                         }
200
201                         return e;
202                 }
203                 
204                 // <summary>
205                 //   Emits the code for the expression
206                 // </summary>
207                 //
208                 // <remarks>
209                 // 
210                 //   The Emit method is invoked to generate the code
211                 //   for the expression.  
212                 //
213                 // </remarks>
214                 public abstract void Emit (EmitContext ec);
215                 
216                 // <summary>
217                 //   Protected constructor.  Only derivate types should
218                 //   be able to be created
219                 // </summary>
220
221                 protected Expression ()
222                 {
223                         eclass = ExprClass.Invalid;
224                         type = null;
225                 }
226
227                 // <summary>
228                 //   Returns a literalized version of a literal FieldInfo
229                 // </summary>
230                 static Expression Literalize (FieldInfo fi)
231                 {
232                         Type t = fi.FieldType;
233                         object v = fi.GetValue (fi);
234
235                         if (t == TypeManager.int32_type)
236                                 return new IntLiteral ((int) v);
237                         else if (t == TypeManager.uint32_type)
238                                 return new UIntLiteral ((uint) v);
239                         else if (t == TypeManager.int64_type)
240                                 return new LongLiteral ((long) v);
241                         else if (t == TypeManager.uint64_type)
242                                 return new ULongLiteral ((ulong) v);
243                         else if (t == TypeManager.float_type)
244                                 return new FloatLiteral ((float) v);
245                         else if (t == TypeManager.double_type)
246                                 return new DoubleLiteral ((double) v);
247                         else if (t == TypeManager.string_type)
248                                 return new StringLiteral ((string) v);
249                         else if (t == TypeManager.short_type)
250                                 return new IntLiteral ((int) ((short)v));
251                         else if (t == TypeManager.ushort_type)
252                                 return new IntLiteral ((int) ((ushort)v));
253                         else if (t == TypeManager.sbyte_type)
254                                 return new IntLiteral ((int) ((sbyte)v));
255                         else if (t == TypeManager.byte_type)
256                                 return new IntLiteral ((int) ((byte)v));
257                         else if (t == TypeManager.char_type)
258                                 return new IntLiteral ((int) ((char)v));
259                         else
260                                 throw new Exception ("Unknown type for literal (" + v.GetType () +
261                                                      "), details: " + fi);
262                 }
263
264                 // 
265                 // Returns a fully formed expression after a MemberLookup
266                 //
267                 static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
268                 {
269                         if (mi is EventInfo){
270                                 return new EventExpr ((EventInfo) mi, loc);
271                         } else if (mi is FieldInfo){
272                                 FieldInfo fi = (FieldInfo) mi;
273
274                                 if (fi.IsLiteral){
275                                         Expression e = Literalize (fi);
276                                         e.Resolve (ec);
277
278                                         return e;
279                                 } else
280                                         return new FieldExpr (fi, loc);
281                         } else if (mi is PropertyInfo){
282                                 return new PropertyExpr ((PropertyInfo) mi, loc);
283                         } else if (mi is Type)
284                                 return new TypeExpr ((Type) mi);
285
286                         return null;
287                 }
288                 
289                 //
290                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
291                 //
292                 // FIXME: We need to cope with access permissions here, or this wont
293                 // work!
294                 //
295                 // This code could use some optimizations, but we need to do some
296                 // measurements.  For example, we could use a delegate to `flag' when
297                 // something can not any longer be a method-group (because it is something
298                 // else).
299                 //
300                 // Return values:
301                 //     If the return value is an Array, then it is an array of
302                 //     MethodBases
303                 //   
304                 //     If the return value is an MemberInfo, it is anything, but a Method
305                 //
306                 //     null on error.
307                 //
308                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
309                 // the arguments here and have MemberLookup return only the methods that
310                 // match the argument count/type, unlike we are doing now (we delay this
311                 // decision).
312                 //
313                 // This is so we can catch correctly attempts to invoke instance methods
314                 // from a static body (scan for error 120 in ResolveSimpleName).
315                 //
316                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
317                                                        bool same_type, MemberTypes mt,
318                                                        BindingFlags bf, Location loc)
319                 {
320                         if (same_type)
321                                 bf |= BindingFlags.NonPublic;
322
323                         MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
324                                 t, mt, bf, Type.FilterName, name);
325
326                         if (mi == null)
327                                 return null;
328
329                         // FIXME : How does this wierd case arise ?
330                         if (mi.Length == 0)
331                                 return null;
332                         
333                         if (mi.Length == 1 && !(mi [0] is MethodBase))
334                                 return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
335                         
336                         for (int i = 0; i < mi.Length; i++)
337                                 if (!(mi [i] is MethodBase)){
338                                         Error (-5, "Do not know how to reproduce this case: " + 
339                                                "Methods and non-Method with the same name, " +
340                                                "report this please");
341
342                                         for (i = 0; i < mi.Length; i++){
343                                                 Type tt = mi [i].GetType ();
344
345                                                 Console.WriteLine (i + ": " + mi [i]);
346                                                 while (tt != TypeManager.object_type){
347                                                         Console.WriteLine (tt);
348                                                         tt = tt.BaseType;
349                                                 }
350                                         }
351                                 }
352
353                         return new MethodGroupExpr (mi);
354                 }
355
356                 public const MemberTypes AllMemberTypes =
357                         MemberTypes.Constructor |
358                         MemberTypes.Event       |
359                         MemberTypes.Field       |
360                         MemberTypes.Method      |
361                         MemberTypes.NestedType  |
362                         MemberTypes.Property;
363                 
364                 public const BindingFlags AllBindingsFlags =
365                         BindingFlags.Public |
366                         BindingFlags.Static |
367                         BindingFlags.Instance;
368
369                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
370                                                        bool same_type, Location loc)
371                 {
372                         return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
373                 }
374
375                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
376                 {
377                         Type expr_type = expr.Type;
378
379                         if (target_type == TypeManager.object_type) {
380                                 if (expr_type.IsClass)
381                                         return new EmptyCast (expr, target_type);
382                                 if (expr_type.IsValueType)
383                                         return new BoxedCast (expr);
384                         } else if (expr_type.IsSubclassOf (target_type)) {
385                                 return new EmptyCast (expr, target_type);
386                         } else {
387                                 // from any class-type S to any interface-type T.
388                                 if (expr_type.IsClass && target_type.IsInterface) {
389
390                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
391                                                 return new EmptyCast (expr, target_type);
392                                         else
393                                                 return null;
394                                 }
395
396                                 // from any interface type S to interface-type T.
397                                 if (expr_type.IsInterface && target_type.IsInterface) {
398
399                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
400                                                 return new EmptyCast (expr, target_type);
401                                         else
402                                                 return null;
403                                 }
404                                 
405                                 // from an array-type S to an array-type of type T
406                                 if (expr_type.IsArray && target_type.IsArray) {
407                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
408
409                                                 Type expr_element_type = expr_type.GetElementType ();
410                                                 Type target_element_type = target_type.GetElementType ();
411
412                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
413                                                         if (StandardConversionExists (expr_element_type,
414                                                                                       target_element_type))
415                                                                 return new EmptyCast (expr, target_type);
416                                         }
417                                 }
418                                 
419                                 
420                                 // from an array-type to System.Array
421                                 if (expr_type.IsArray && target_type == TypeManager.array_type)
422                                         return new EmptyCast (expr, target_type);
423                                 
424                                 // from any delegate type to System.Delegate
425                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
426                                     target_type == TypeManager.delegate_type)
427                                         return new EmptyCast (expr, target_type);
428                                         
429                                 // from any array-type or delegate type into System.ICloneable.
430                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
431                                         if (target_type == TypeManager.icloneable_type)
432                                                 return new EmptyCast (expr, target_type);
433                                 
434                                 // from the null type to any reference-type.
435                                 if (expr is NullLiteral)
436                                         return new EmptyCast (expr, target_type);
437
438                                 return null;
439
440                         }
441                         
442                         return null;
443                 }
444
445                 // <summary>
446                 //   Handles expressions like this: decimal d; d = 1;
447                 //   and changes them into: decimal d; d = new System.Decimal (1);
448                 // </summary>
449                 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
450                 {
451                         ArrayList args = new ArrayList ();
452
453                         args.Add (new Argument (expr, Argument.AType.Expression));
454
455                         Expression ne = new New (target.FullName, args,
456                                                  new Location (-1));
457
458                         return ne.Resolve (ec);
459                 }
460
461                 // <summary>
462                 //   Implicit Numeric Conversions.
463                 //
464                 //   expr is the expression to convert, returns a new expression of type
465                 //   target_type or null if an implicit conversion is not possible.
466                 //
467                 // </summary>
468                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
469                                                                     Type target_type, Location loc)
470                 {
471                         Type expr_type = expr.Type;
472                         
473                         //
474                         // Attempt to do the implicit constant expression conversions
475
476                         if (expr is IntLiteral){
477                                 Expression e;
478                                 
479                                 e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
480                                 if (e != null)
481                                         return e;
482                         } else if (expr is LongLiteral){
483                                 //
484                                 // Try the implicit constant expression conversion
485                                 // from long to ulong, instead of a nice routine,
486                                 // we just inline it
487                                 //
488                                 if (((LongLiteral) expr).Value > 0)
489                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
490                         }
491                         
492                         if (expr_type == TypeManager.sbyte_type){
493                                 //
494                                 // From sbyte to short, int, long, float, double.
495                                 //
496                                 if (target_type == TypeManager.int32_type)
497                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
498                                 if (target_type == TypeManager.int64_type)
499                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
500                                 if (target_type == TypeManager.double_type)
501                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
502                                 if (target_type == TypeManager.float_type)
503                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
504                                 if (target_type == TypeManager.short_type)
505                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
506                                 if (target_type == TypeManager.decimal_type)
507                                         return InternalTypeConstructor (ec, expr, target_type);
508                         } else if (expr_type == TypeManager.byte_type){
509                                 //
510                                 // From byte to short, ushort, int, uint, long, ulong, float, double
511                                 // 
512                                 if ((target_type == TypeManager.short_type) ||
513                                     (target_type == TypeManager.ushort_type) ||
514                                     (target_type == TypeManager.int32_type) ||
515                                     (target_type == TypeManager.uint32_type))
516                                         return new EmptyCast (expr, target_type);
517
518                                 if (target_type == TypeManager.uint64_type)
519                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
520                                 if (target_type == TypeManager.int64_type)
521                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
522                                 
523                                 if (target_type == TypeManager.float_type)
524                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
525                                 if (target_type == TypeManager.double_type)
526                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
527                                 if (target_type == TypeManager.decimal_type)
528                                         return InternalTypeConstructor (ec, expr, target_type);
529                         } else if (expr_type == TypeManager.short_type){
530                                 //
531                                 // From short to int, long, float, double
532                                 // 
533                                 if (target_type == TypeManager.int32_type)
534                                         return new EmptyCast (expr, target_type);
535                                 if (target_type == TypeManager.int64_type)
536                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
537                                 if (target_type == TypeManager.double_type)
538                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
539                                 if (target_type == TypeManager.float_type)
540                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
541                                 if (target_type == TypeManager.decimal_type)
542                                         return InternalTypeConstructor (ec, expr, target_type);
543                         } else if (expr_type == TypeManager.ushort_type){
544                                 //
545                                 // From ushort to int, uint, long, ulong, float, double
546                                 //
547                                 if (target_type == TypeManager.uint32_type)
548                                         return new EmptyCast (expr, target_type);
549
550                                 if (target_type == TypeManager.uint64_type)
551                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
552                                 if (target_type == TypeManager.int32_type)
553                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
554                                 if (target_type == TypeManager.int64_type)
555                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
556                                 if (target_type == TypeManager.double_type)
557                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
558                                 if (target_type == TypeManager.float_type)
559                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
560                                 if (target_type == TypeManager.decimal_type)
561                                         return InternalTypeConstructor (ec, expr, target_type);
562                         } else if (expr_type == TypeManager.int32_type){
563                                 //
564                                 // From int to long, float, double
565                                 //
566                                 if (target_type == TypeManager.int64_type)
567                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
568                                 if (target_type == TypeManager.double_type)
569                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
570                                 if (target_type == TypeManager.float_type)
571                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
572                                 if (target_type == TypeManager.decimal_type)
573                                         return InternalTypeConstructor (ec, expr, target_type);
574                         } else if (expr_type == TypeManager.uint32_type){
575                                 //
576                                 // From uint to long, ulong, float, double
577                                 //
578                                 if (target_type == TypeManager.int64_type)
579                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
580                                 if (target_type == TypeManager.uint64_type)
581                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
582                                 if (target_type == TypeManager.double_type)
583                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
584                                                                OpCodes.Conv_R8);
585                                 if (target_type == TypeManager.float_type)
586                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
587                                                                OpCodes.Conv_R4);
588                                 if (target_type == TypeManager.decimal_type)
589                                         return InternalTypeConstructor (ec, expr, target_type);
590                         } else if ((expr_type == TypeManager.uint64_type) ||
591                                    (expr_type == TypeManager.int64_type)){
592                                 //
593                                 // From long/ulong to float, double
594                                 //
595                                 if (target_type == TypeManager.double_type)
596                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
597                                                                OpCodes.Conv_R8);
598                                 if (target_type == TypeManager.float_type)
599                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
600                                                                OpCodes.Conv_R4);        
601                                 if (target_type == TypeManager.decimal_type)
602                                         return InternalTypeConstructor (ec, expr, target_type);
603                         } else if (expr_type == TypeManager.char_type){
604                                 //
605                                 // From char to ushort, int, uint, long, ulong, float, double
606                                 // 
607                                 if ((target_type == TypeManager.ushort_type) ||
608                                     (target_type == TypeManager.int32_type) ||
609                                     (target_type == TypeManager.uint32_type))
610                                         return new EmptyCast (expr, target_type);
611                                 if (target_type == TypeManager.uint64_type)
612                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
613                                 if (target_type == TypeManager.int64_type)
614                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
615                                 if (target_type == TypeManager.float_type)
616                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
617                                 if (target_type == TypeManager.double_type)
618                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
619                                 if (target_type == TypeManager.decimal_type)
620                                         return InternalTypeConstructor (ec, expr, target_type);
621                         } else if (expr_type == TypeManager.float_type){
622                                 //
623                                 // float to double
624                                 //
625                                 if (target_type == TypeManager.double_type)
626                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
627                         }
628
629                         return null;
630                 }
631
632                 // <summary>
633                 //  Determines if a standard implicit conversion exists from
634                 //  expr_type to target_type
635                 // </summary>
636                 public static bool StandardConversionExists (Type expr_type, Type target_type)
637                 {
638                         if (expr_type == target_type)
639                                 return true;
640
641                         // First numeric conversions 
642                         
643                         if (expr_type == TypeManager.sbyte_type){
644                                 //
645                                 // From sbyte to short, int, long, float, double.
646                                 //
647                                 if ((target_type == TypeManager.int32_type) || 
648                                     (target_type == TypeManager.int64_type) ||
649                                     (target_type == TypeManager.double_type) ||
650                                     (target_type == TypeManager.float_type)  ||
651                                     (target_type == TypeManager.short_type) ||
652                                     (target_type == TypeManager.decimal_type))
653                                         return true;
654                                 
655                         } else if (expr_type == TypeManager.byte_type){
656                                 //
657                                 // From byte to short, ushort, int, uint, long, ulong, float, double
658                                 // 
659                                 if ((target_type == TypeManager.short_type) ||
660                                     (target_type == TypeManager.ushort_type) ||
661                                     (target_type == TypeManager.int32_type) ||
662                                     (target_type == TypeManager.uint32_type) ||
663                                     (target_type == TypeManager.uint64_type) ||
664                                     (target_type == TypeManager.int64_type) ||
665                                     (target_type == TypeManager.float_type) ||
666                                     (target_type == TypeManager.double_type) ||
667                                     (target_type == TypeManager.decimal_type))
668                                         return true;
669         
670                         } else if (expr_type == TypeManager.short_type){
671                                 //
672                                 // From short to int, long, float, double
673                                 // 
674                                 if ((target_type == TypeManager.int32_type) ||
675                                     (target_type == TypeManager.int64_type) ||
676                                     (target_type == TypeManager.double_type) ||
677                                     (target_type == TypeManager.float_type) ||
678                                     (target_type == TypeManager.decimal_type))
679                                         return true;
680                                         
681                         } else if (expr_type == TypeManager.ushort_type){
682                                 //
683                                 // From ushort to int, uint, long, ulong, float, double
684                                 //
685                                 if ((target_type == TypeManager.uint32_type) ||
686                                     (target_type == TypeManager.uint64_type) ||
687                                     (target_type == TypeManager.int32_type) ||
688                                     (target_type == TypeManager.int64_type) ||
689                                     (target_type == TypeManager.double_type) ||
690                                     (target_type == TypeManager.float_type) ||
691                                     (target_type == TypeManager.decimal_type))
692                                         return true;
693                                     
694                         } else if (expr_type == TypeManager.int32_type){
695                                 //
696                                 // From int to long, float, double
697                                 //
698                                 if ((target_type == TypeManager.int64_type) ||
699                                     (target_type == TypeManager.double_type) ||
700                                     (target_type == TypeManager.float_type) ||
701                                     (target_type == TypeManager.decimal_type))
702                                         return true;
703                                         
704                         } else if (expr_type == TypeManager.uint32_type){
705                                 //
706                                 // From uint to long, ulong, float, double
707                                 //
708                                 if ((target_type == TypeManager.int64_type) ||
709                                     (target_type == TypeManager.uint64_type) ||
710                                     (target_type == TypeManager.double_type) ||
711                                     (target_type == TypeManager.float_type) ||
712                                     (target_type == TypeManager.decimal_type))
713                                         return true;
714                                         
715                         } else if ((expr_type == TypeManager.uint64_type) ||
716                                    (expr_type == TypeManager.int64_type)) {
717                                 //
718                                 // From long/ulong to float, double
719                                 //
720                                 if ((target_type == TypeManager.double_type) ||
721                                     (target_type == TypeManager.float_type) ||
722                                     (target_type == TypeManager.decimal_type))
723                                         return true;
724                                     
725                         } else if (expr_type == TypeManager.char_type){
726                                 //
727                                 // From char to ushort, int, uint, long, ulong, float, double
728                                 // 
729                                 if ((target_type == TypeManager.ushort_type) ||
730                                     (target_type == TypeManager.int32_type) ||
731                                     (target_type == TypeManager.uint32_type) ||
732                                     (target_type == TypeManager.uint64_type) ||
733                                     (target_type == TypeManager.int64_type) ||
734                                     (target_type == TypeManager.float_type) ||
735                                     (target_type == TypeManager.double_type) ||
736                                     (target_type == TypeManager.decimal_type))
737                                         return true;
738
739                         } else if (expr_type == TypeManager.float_type){
740                                 //
741                                 // float to double
742                                 //
743                                 if (target_type == TypeManager.double_type)
744                                         return true;
745                         }       
746                         
747                         // Next reference conversions
748
749                         if (target_type == TypeManager.object_type) {
750                                 if ((expr_type.IsClass) ||
751                                     (expr_type.IsValueType))
752                                         return true;
753                                 
754                         } else if (expr_type.IsSubclassOf (target_type)) {
755                                 return true;
756                                 
757                         } else {
758                                 // from any class-type S to any interface-type T.
759                                 if (expr_type.IsClass && target_type.IsInterface)
760                                         return true;
761                                 
762                                 // from any interface type S to interface-type T.
763                                 // FIXME : Is it right to use IsAssignableFrom ?
764                                 if (expr_type.IsInterface && target_type.IsInterface)
765                                         if (target_type.IsAssignableFrom (expr_type))
766                                                 return true;
767                                 
768                                 // from an array-type S to an array-type of type T
769                                 if (expr_type.IsArray && target_type.IsArray) {
770                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
771                                                 
772                                                 Type expr_element_type = expr_type.GetElementType ();
773                                                 Type target_element_type = target_type.GetElementType ();
774                                                 
775                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
776                                                         if (StandardConversionExists (expr_element_type,
777                                                                                       target_element_type))
778                                                                 return true;
779                                         }
780                                 }
781                                 
782                                 // from an array-type to System.Array
783                                 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
784                                         return true;
785                                 
786                                 // from any delegate type to System.Delegate
787                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
788                                     target_type == TypeManager.delegate_type)
789                                         if (target_type.IsAssignableFrom (expr_type))
790                                                 return true;
791                                         
792                                 // from any array-type or delegate type into System.ICloneable.
793                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
794                                         if (target_type == TypeManager.icloneable_type)
795                                                 return true;
796                                 
797                                 // from the null type to any reference-type.
798                                 // FIXME : How do we do this ?
799
800                         }
801
802                         return false;
803                 }
804                 
805                 // <summary>
806                 //  Finds "most encompassed type" according to the spec (13.4.2)
807                 //  amongst the methods in the MethodGroupExpr which convert from a
808                 //  type encompassing source_type
809                 // </summary>
810                 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
811                 {
812                         Type best = null;
813                         
814                         for (int i = me.Methods.Length; i > 0; ) {
815                                 i--;
816
817                                 MethodBase mb = me.Methods [i];
818                                 ParameterData pd = Invocation.GetParameterData (mb);
819                                 Type param_type = pd.ParameterType (0);
820
821                                 if (StandardConversionExists (source_type, param_type)) {
822                                         if (best == null)
823                                                 best = param_type;
824                                         
825                                         if (StandardConversionExists (param_type, best))
826                                                 best = param_type;
827                                 }
828                         }
829
830                         return best;
831                 }
832                 
833                 // <summary>
834                 //  Finds "most encompassing type" according to the spec (13.4.2)
835                 //  amongst the methods in the MethodGroupExpr which convert to a
836                 //  type encompassed by target_type
837                 // </summary>
838                 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
839                 {
840                         Type best = null;
841                         
842                         for (int i = me.Methods.Length; i > 0; ) {
843                                 i--;
844                                 
845                                 MethodInfo mi = (MethodInfo) me.Methods [i];
846                                 Type ret_type = mi.ReturnType;
847                                 
848                                 if (StandardConversionExists (ret_type, target)) {
849                                         if (best == null)
850                                                 best = ret_type;
851
852                                         if (!StandardConversionExists (ret_type, best))
853                                                 best = ret_type;
854                                 }
855                                 
856                         }
857                         
858                         return best;
859
860                 }
861                 
862
863                 // <summary>
864                 //  User-defined Implicit conversions
865                 // </summary>
866                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
867                                                                  Type target, Location loc)
868                 {
869                         return UserDefinedConversion (ec, source, target, loc, false);
870                 }
871
872                 // <summary>
873                 //  User-defined Explicit conversions
874                 // </summary>
875                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
876                                                                  Type target, Location loc)
877                 {
878                         return UserDefinedConversion (ec, source, target, loc, true);
879                 }
880                 
881                 // <summary>
882                 //   User-defined conversions
883                 // </summary>
884                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
885                                                                 Type target, Location loc,
886                                                                 bool look_for_explicit)
887                 {
888                         Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
889                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
890                         Expression e;
891                         MethodBase method = null;
892                         Type source_type = source.Type;
893
894                         string op_name;
895                         
896                         // If we have a boolean type, we need to check for the True operator
897
898                         // FIXME : How does the False operator come into the picture ?
899                         // FIXME : This doesn't look complete and very correct !
900                         if (target == TypeManager.bool_type)
901                                 op_name = "op_True";
902                         else
903                                 op_name = "op_Implicit";
904                         
905                         mg1 = MemberLookup (ec, source_type, op_name, false, loc);
906
907                         if (source_type.BaseType != null)
908                                 mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
909                         
910                         mg3 = MemberLookup (ec, target, op_name, false, loc);
911
912                         if (target.BaseType != null)
913                                 mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
914
915                         MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
916                         MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
917
918                         MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
919
920                         MethodGroupExpr union4 = null;
921
922                         if (look_for_explicit) {
923
924                                 op_name = "op_Explicit";
925                                 
926                                 mg5 = MemberLookup (ec, source_type, op_name, false, loc);
927
928                                 if (source_type.BaseType != null)
929                                         mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
930                                 
931                                 mg7 = MemberLookup (ec, target, op_name, false, loc);
932                                 
933                                 if (target.BaseType != null)
934                                         mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
935                                 
936                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
937                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
938
939                                 union4 = Invocation.MakeUnionSet (union5, union6);
940                         }
941                         
942                         MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
943
944                         if (union != null) {
945
946                                 Type most_specific_source, most_specific_target;
947
948                                 most_specific_source = FindMostEncompassedType (union, source_type);
949                                 if (most_specific_source == null)
950                                         return null;
951
952                                 most_specific_target = FindMostEncompassingType (union, target);
953                                 if (most_specific_target == null) 
954                                         return null;
955                                 
956                                 int count = 0;
957                                 
958                                 for (int i = union.Methods.Length; i > 0;) {
959                                         i--;
960
961                                         MethodBase mb = union.Methods [i];
962                                         ParameterData pd = Invocation.GetParameterData (mb);
963                                         MethodInfo mi = (MethodInfo) union.Methods [i];
964
965                                         if (pd.ParameterType (0) == most_specific_source &&
966                                             mi.ReturnType == most_specific_target) {
967                                                 method = mb;
968                                                 count++;
969                                         }
970                                 }
971
972                                 if (method == null || count > 1) {
973                                         Report.Error (-11, loc, "Ambiguous user defined conversion");
974                                         return null;
975                                 }
976                                 
977                                 //
978                                 // This will do the conversion to the best match that we
979                                 // found.  Now we need to perform an implict standard conversion
980                                 // if the best match was not the type that we were requested
981                                 // by target.
982                                 //
983                                 if (look_for_explicit)
984                                         source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
985                                 else
986                                         source = ConvertImplicitStandard (ec, source,
987                                                                           most_specific_source, loc);
988
989                                 if (source == null)
990                                         return null;
991                                 
992                                 e =  new UserCast ((MethodInfo) method, source);
993                                 
994                                 if (e.Type != target){
995                                         if (!look_for_explicit)
996                                                 e = ConvertImplicitStandard (ec, e, target, loc);
997                                         else
998                                                 e = ConvertExplicitStandard (ec, e, target, loc);
999
1000                                         return e;
1001                                 } else
1002                                         return e;
1003                         }
1004                         
1005                         return null;
1006                 }
1007                 
1008                 // <summary>
1009                 //   Converts implicitly the resolved expression `expr' into the
1010                 //   `target_type'.  It returns a new expression that can be used
1011                 //   in a context that expects a `target_type'. 
1012                 // </summary>
1013                 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1014                                                           Type target_type, Location loc)
1015                 {
1016                         Type expr_type = expr.Type;
1017                         Expression e;
1018
1019                         if (expr_type == target_type)
1020                                 return expr;
1021
1022                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1023                         if (e != null)
1024                                 return e;
1025
1026                         e = ImplicitReferenceConversion (expr, target_type);
1027                         if (e != null)
1028                                 return e;
1029
1030                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1031                         if (e != null)
1032                                 return e;
1033
1034                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1035                                 IntLiteral i = (IntLiteral) expr;
1036
1037                                 if (i.Value == 0)
1038                                         return new EmptyCast (expr, target_type);
1039                         }
1040
1041                         return null;
1042                 }
1043
1044                 
1045                 // <summary>
1046                 //   Attempts to apply the `Standard Implicit
1047                 //   Conversion' rules to the expression `expr' into
1048                 //   the `target_type'.  It returns a new expression
1049                 //   that can be used in a context that expects a
1050                 //   `target_type'.
1051                 //
1052                 //   This is different from `ConvertImplicit' in that the
1053                 //   user defined implicit conversions are excluded. 
1054                 // </summary>
1055                 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1056                                                                   Type target_type, Location loc)
1057                 {
1058                         Type expr_type = expr.Type;
1059                         Expression e;
1060
1061                         if (expr_type == target_type)
1062                                 return expr;
1063
1064                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1065                         if (e != null)
1066                                 return e;
1067
1068                         e = ImplicitReferenceConversion (expr, target_type);
1069                         if (e != null)
1070                                 return e;
1071
1072                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1073                                 IntLiteral i = (IntLiteral) expr;
1074
1075                                 if (i.Value == 0)
1076                                         return new EmptyCast (expr, target_type);
1077                         }
1078                         return null;
1079                 }
1080                 // <summary>
1081                 //   Attemps to perform an implict constant conversion of the IntLiteral
1082                 //   into a different data type using casts (See Implicit Constant
1083                 //   Expression Conversions)
1084                 // </summary>
1085                 static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
1086                 {
1087                         int value = il.Value;
1088                         
1089                         if (target_type == TypeManager.sbyte_type){
1090                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1091                                         return il;
1092                         } else if (target_type == TypeManager.byte_type){
1093                                 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1094                                         return il;
1095                         } else if (target_type == TypeManager.short_type){
1096                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1097                                         return il;
1098                         } else if (target_type == TypeManager.ushort_type){
1099                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1100                                         return il;
1101                         } else if (target_type == TypeManager.uint32_type){
1102                                 //
1103                                 // we can optimize this case: a positive int32
1104                                 // always fits on a uint32
1105                                 //
1106                                 if (value >= 0)
1107                                         return il;
1108                         } else if (target_type == TypeManager.uint64_type){
1109                                 //
1110                                 // we can optimize this case: a positive int32
1111                                 // always fits on a uint64.  But we need an opcode
1112                                 // to do it.
1113                                 //
1114                                 if (value >= 0)
1115                                         return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
1116                         }
1117
1118                         return null;
1119                 }
1120
1121                 // <summary>
1122                 //   Attemptes to implicityly convert `target' into `type', using
1123                 //   ConvertImplicit.  If there is no implicit conversion, then
1124                 //   an error is signaled
1125                 // </summary>
1126                 static public Expression ConvertImplicitRequired (EmitContext ec, Expression target,
1127                                                                   Type type, Location loc)
1128                 {
1129                         Expression e;
1130                         
1131                         e = ConvertImplicit (ec, target, type, loc);
1132                         if (e != null)
1133                                 return e;
1134                         
1135                         string msg = "Can not convert implicitly from `"+
1136                                 TypeManager.CSharpName (target.Type) + "' to `" +
1137                                 TypeManager.CSharpName (type) + "'";
1138
1139                         Error (29, loc, msg);
1140
1141                         return null;
1142                 }
1143
1144                 // <summary>
1145                 //   Performs the explicit numeric conversions
1146                 // </summary>
1147                 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1148                                                           Type target_type)
1149                 {
1150                         Type expr_type = expr.Type;
1151                         
1152                         if (expr_type == TypeManager.sbyte_type){
1153                                 //
1154                                 // From sbyte to byte, ushort, uint, ulong, char
1155                                 //
1156                                 if (target_type == TypeManager.byte_type)
1157                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1158                                 if (target_type == TypeManager.ushort_type)
1159                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1160                                 if (target_type == TypeManager.uint32_type)
1161                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1162                                 if (target_type == TypeManager.uint64_type)
1163                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1164                                 if (target_type == TypeManager.char_type)
1165                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1166                         } else if (expr_type == TypeManager.byte_type){
1167                                 //
1168                                 // From byte to sbyte and char
1169                                 //
1170                                 if (target_type == TypeManager.sbyte_type)
1171                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1172                                 if (target_type == TypeManager.char_type)
1173                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1174                         } else if (expr_type == TypeManager.short_type){
1175                                 //
1176                                 // From short to sbyte, byte, ushort, uint, ulong, char
1177                                 //
1178                                 if (target_type == TypeManager.sbyte_type)
1179                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1180                                 if (target_type == TypeManager.byte_type)
1181                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1182                                 if (target_type == TypeManager.ushort_type)
1183                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1184                                 if (target_type == TypeManager.uint32_type)
1185                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1186                                 if (target_type == TypeManager.uint64_type)
1187                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1188                                 if (target_type == TypeManager.char_type)
1189                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1190                         } else if (expr_type == TypeManager.ushort_type){
1191                                 //
1192                                 // From ushort to sbyte, byte, short, char
1193                                 //
1194                                 if (target_type == TypeManager.sbyte_type)
1195                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1196                                 if (target_type == TypeManager.byte_type)
1197                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1198                                 if (target_type == TypeManager.short_type)
1199                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1200                                 if (target_type == TypeManager.char_type)
1201                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1202                         } else if (expr_type == TypeManager.int32_type){
1203                                 //
1204                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1205                                 //
1206                                 if (target_type == TypeManager.sbyte_type)
1207                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1208                                 if (target_type == TypeManager.byte_type)
1209                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1210                                 if (target_type == TypeManager.short_type)
1211                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1212                                 if (target_type == TypeManager.ushort_type)
1213                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1214                                 if (target_type == TypeManager.uint32_type)
1215                                         return new EmptyCast (expr, target_type);
1216                                 if (target_type == TypeManager.uint64_type)
1217                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1218                                 if (target_type == TypeManager.char_type)
1219                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1220                         } else if (expr_type == TypeManager.uint32_type){
1221                                 //
1222                                 // From uint to sbyte, byte, short, ushort, int, char
1223                                 //
1224                                 if (target_type == TypeManager.sbyte_type)
1225                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1226                                 if (target_type == TypeManager.byte_type)
1227                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1228                                 if (target_type == TypeManager.short_type)
1229                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1230                                 if (target_type == TypeManager.ushort_type)
1231                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1232                                 if (target_type == TypeManager.int32_type)
1233                                         return new EmptyCast (expr, target_type);
1234                                 if (target_type == TypeManager.char_type)
1235                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1236                         } else if (expr_type == TypeManager.int64_type){
1237                                 //
1238                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1239                                 //
1240                                 if (target_type == TypeManager.sbyte_type)
1241                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1242                                 if (target_type == TypeManager.byte_type)
1243                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1244                                 if (target_type == TypeManager.short_type)
1245                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1246                                 if (target_type == TypeManager.ushort_type)
1247                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1248                                 if (target_type == TypeManager.int32_type)
1249                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1250                                 if (target_type == TypeManager.uint32_type)
1251                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1252                                 if (target_type == TypeManager.uint64_type)
1253                                         return new EmptyCast (expr, target_type);
1254                                 if (target_type == TypeManager.char_type)
1255                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1256                         } else if (expr_type == TypeManager.uint64_type){
1257                                 //
1258                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1259                                 //
1260                                 if (target_type == TypeManager.sbyte_type)
1261                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1262                                 if (target_type == TypeManager.byte_type)
1263                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1264                                 if (target_type == TypeManager.short_type)
1265                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1266                                 if (target_type == TypeManager.ushort_type)
1267                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1268                                 if (target_type == TypeManager.int32_type)
1269                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1270                                 if (target_type == TypeManager.uint32_type)
1271                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1272                                 if (target_type == TypeManager.int64_type)
1273                                         return new EmptyCast (expr, target_type);
1274                                 if (target_type == TypeManager.char_type)
1275                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1276                         } else if (expr_type == TypeManager.char_type){
1277                                 //
1278                                 // From char to sbyte, byte, short
1279                                 //
1280                                 if (target_type == TypeManager.sbyte_type)
1281                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1282                                 if (target_type == TypeManager.byte_type)
1283                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1284                                 if (target_type == TypeManager.short_type)
1285                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1286                         } else if (expr_type == TypeManager.float_type){
1287                                 //
1288                                 // From float to sbyte, byte, short,
1289                                 // ushort, int, uint, long, ulong, char
1290                                 // or decimal
1291                                 //
1292                                 if (target_type == TypeManager.sbyte_type)
1293                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1294                                 if (target_type == TypeManager.byte_type)
1295                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1296                                 if (target_type == TypeManager.short_type)
1297                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1298                                 if (target_type == TypeManager.ushort_type)
1299                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1300                                 if (target_type == TypeManager.int32_type)
1301                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1302                                 if (target_type == TypeManager.uint32_type)
1303                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1304                                 if (target_type == TypeManager.int64_type)
1305                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1306                                 if (target_type == TypeManager.uint64_type)
1307                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1308                                 if (target_type == TypeManager.char_type)
1309                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1310                                 if (target_type == TypeManager.decimal_type)
1311                                         return InternalTypeConstructor (ec, expr, target_type);
1312                         } else if (expr_type == TypeManager.double_type){
1313                                 //
1314                                 // From double to byte, byte, short,
1315                                 // ushort, int, uint, long, ulong,
1316                                 // char, float or decimal
1317                                 //
1318                                 if (target_type == TypeManager.sbyte_type)
1319                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1320                                 if (target_type == TypeManager.byte_type)
1321                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1322                                 if (target_type == TypeManager.short_type)
1323                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1324                                 if (target_type == TypeManager.ushort_type)
1325                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1326                                 if (target_type == TypeManager.int32_type)
1327                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1328                                 if (target_type == TypeManager.uint32_type)
1329                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1330                                 if (target_type == TypeManager.int64_type)
1331                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1332                                 if (target_type == TypeManager.uint64_type)
1333                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1334                                 if (target_type == TypeManager.char_type)
1335                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1336                                 if (target_type == TypeManager.float_type)
1337                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1338                                 if (target_type == TypeManager.decimal_type)
1339                                         return InternalTypeConstructor (ec, expr, target_type);
1340                         } 
1341
1342                         // decimal is taken care of by the op_Explicit methods.
1343
1344                         return null;
1345                 }
1346
1347                 // <summary>
1348                 //  Returns whether an explicit reference conversion can be performed
1349                 //  from source_type to target_type
1350                 // </summary>
1351                 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1352                 {
1353                         bool target_is_value_type = target_type.IsValueType;
1354                         
1355                         if (source_type == target_type)
1356                                 return true;
1357                         
1358                         //
1359                         // From object to any reference type
1360                         //
1361                         if (source_type == TypeManager.object_type && !target_is_value_type)
1362                                 return true;
1363                                         
1364                         //
1365                         // From any class S to any class-type T, provided S is a base class of T
1366                         //
1367                         if (target_type.IsSubclassOf (source_type))
1368                                 return true;
1369
1370                         //
1371                         // From any interface type S to any interface T provided S is not derived from T
1372                         //
1373                         if (source_type.IsInterface && target_type.IsInterface){
1374                                 if (!target_type.IsSubclassOf (source_type))
1375                                         return true;
1376                         }
1377                             
1378                         //
1379                         // From any class type S to any interface T, provides S is not sealed
1380                         // and provided S does not implement T.
1381                         //
1382                         if (target_type.IsInterface && !source_type.IsSealed &&
1383                             !target_type.IsAssignableFrom (source_type))
1384                                 return true;
1385
1386                         //
1387                         // From any interface-type S to to any class type T, provided T is not
1388                         // sealed, or provided T implements S.
1389                         //
1390                         if (source_type.IsInterface &&
1391                             (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1392                                 return true;
1393
1394                         // From an array type S with an element type Se to an array type T with an 
1395                         // element type Te provided all the following are true:
1396                         //     * S and T differe only in element type, in other words, S and T
1397                         //       have the same number of dimensions.
1398                         //     * Both Se and Te are reference types
1399                         //     * An explicit referenc conversions exist from Se to Te
1400                         //
1401                         if (source_type.IsArray && target_type.IsArray) {
1402                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1403                                         
1404                                         Type source_element_type = source_type.GetElementType ();
1405                                         Type target_element_type = target_type.GetElementType ();
1406                                         
1407                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1408                                                 if (ExplicitReferenceConversionExists (source_element_type,
1409                                                                                        target_element_type))
1410                                                         return true;
1411                                 }
1412                         }
1413                         
1414
1415                         // From System.Array to any array-type
1416                         if (source_type == TypeManager.array_type &&
1417                             target_type.IsSubclassOf (TypeManager.array_type)){
1418                                 return true;
1419                         }
1420
1421                         //
1422                         // From System delegate to any delegate-type
1423                         //
1424                         if (source_type == TypeManager.delegate_type &&
1425                             target_type.IsSubclassOf (TypeManager.delegate_type))
1426                                 return true;
1427
1428                         //
1429                         // From ICloneable to Array or Delegate types
1430                         //
1431                         if (source_type == TypeManager.icloneable_type &&
1432                             (target_type == TypeManager.array_type ||
1433                              target_type == TypeManager.delegate_type))
1434                                 return true;
1435                         
1436                         return false;
1437                 }
1438
1439                 // <summary>
1440                 //   Implements Explicit Reference conversions
1441                 // </summary>
1442                 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1443                 {
1444                         Type source_type = source.Type;
1445                         bool target_is_value_type = target_type.IsValueType;
1446                         
1447                         //
1448                         // From object to any reference type
1449                         //
1450                         if (source_type == TypeManager.object_type && !target_is_value_type)
1451                                 return new ClassCast (source, target_type);
1452
1453
1454                         //
1455                         // From any class S to any class-type T, provided S is a base class of T
1456                         //
1457                         if (target_type.IsSubclassOf (source_type))
1458                                 return new ClassCast (source, target_type);
1459
1460                         //
1461                         // From any interface type S to any interface T provided S is not derived from T
1462                         //
1463                         if (source_type.IsInterface && target_type.IsInterface){
1464
1465                                 Type [] ifaces = source_type.GetInterfaces ();
1466
1467                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1468                                         return null;
1469                                 else
1470                                         return new ClassCast (source, target_type);
1471                         }
1472                             
1473                         //
1474                         // From any class type S to any interface T, provides S is not sealed
1475                         // and provided S does not implement T.
1476                         //
1477                         if (target_type.IsInterface && !source_type.IsSealed) {
1478                                 
1479                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1480                                         return null;
1481                                 else
1482                                         return new ClassCast (source, target_type);
1483                                 
1484                         }
1485
1486                         //
1487                         // From any interface-type S to to any class type T, provided T is not
1488                         // sealed, or provided T implements S.
1489                         //
1490                         if (source_type.IsInterface) {
1491
1492                                 if (target_type.IsSealed)
1493                                         return null;
1494                                 
1495                                 if (TypeManager.ImplementsInterface (target_type, source_type))
1496                                         return new ClassCast (source, target_type);
1497                                 else
1498                                         return null;
1499                         }
1500                         
1501                         // From an array type S with an element type Se to an array type T with an 
1502                         // element type Te provided all the following are true:
1503                         //     * S and T differe only in element type, in other words, S and T
1504                         //       have the same number of dimensions.
1505                         //     * Both Se and Te are reference types
1506                         //     * An explicit referenc conversions exist from Se to Te
1507                         //
1508                         if (source_type.IsArray && target_type.IsArray) {
1509                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1510                                         
1511                                         Type source_element_type = source_type.GetElementType ();
1512                                         Type target_element_type = target_type.GetElementType ();
1513                                         
1514                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1515                                                 if (ExplicitReferenceConversionExists (source_element_type,
1516                                                                                        target_element_type))
1517                                                         return new ClassCast (source, target_type);
1518                                 }
1519                         }
1520                         
1521
1522                         // From System.Array to any array-type
1523                         if (source_type == TypeManager.array_type &&
1524                             target_type.IsSubclassOf (TypeManager.array_type)){
1525                                 return new ClassCast (source, target_type);
1526                         }
1527
1528                         //
1529                         // From System delegate to any delegate-type
1530                         //
1531                         if (source_type == TypeManager.delegate_type &&
1532                             target_type.IsSubclassOf (TypeManager.delegate_type))
1533                                 return new ClassCast (source, target_type);
1534
1535                         //
1536                         // From ICloneable to Array or Delegate types
1537                         //
1538                         if (source_type == TypeManager.icloneable_type &&
1539                             (target_type == TypeManager.array_type ||
1540                              target_type == TypeManager.delegate_type))
1541                                 return new ClassCast (source, target_type);
1542                         
1543                         return null;
1544                 }
1545                 
1546                 // <summary>
1547                 //   Performs an explicit conversion of the expression `expr' whose
1548                 //   type is expr.Type to `target_type'.
1549                 // </summary>
1550                 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1551                                                           Type target_type, Location loc)
1552                 {
1553                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1554
1555                         if (ne != null)
1556                                 return ne;
1557
1558                         ne = ConvertNumericExplicit (ec, expr, target_type);
1559                         if (ne != null)
1560                                 return ne;
1561
1562                         ne = ConvertReferenceExplicit (expr, target_type);
1563                         if (ne != null)
1564                                 return ne;
1565
1566                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1567                         if (ne != null)
1568                                 return ne;
1569
1570                         Report.Error (30, loc, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
1571                                       + TypeManager.CSharpName (target_type) + "'");
1572                         return null;
1573                 }
1574
1575                 // <summary>
1576                 //   Same as ConverExplicit, only it doesn't include user defined conversions
1577                 // </summary>
1578                 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1579                                                                   Type target_type, Location l)
1580                 {
1581                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1582
1583                         if (ne != null)
1584                                 return ne;
1585
1586                         ne = ConvertNumericExplicit (ec, expr, target_type);
1587                         if (ne != null)
1588                                 return ne;
1589
1590                         ne = ConvertReferenceExplicit (expr, target_type);
1591                         if (ne != null)
1592                                 return ne;
1593
1594                         Report.Error (30, l, "Cannot convert type '" +
1595                                       TypeManager.CSharpName (expr.Type) + "' to '" + 
1596                                       TypeManager.CSharpName (target_type) + "'");
1597                         return null;
1598                 }
1599
1600                 static string ExprClassName (ExprClass c)
1601                 {
1602                         switch (c){
1603                         case ExprClass.Invalid:
1604                                 return "Invalid";
1605                         case ExprClass.Value:
1606                                 return "value";
1607                         case ExprClass.Variable:
1608                                 return "variable";
1609                         case ExprClass.Namespace:
1610                                 return "namespace";
1611                         case ExprClass.Type:
1612                                 return "type";
1613                         case ExprClass.MethodGroup:
1614                                 return "method group";
1615                         case ExprClass.PropertyAccess:
1616                                 return "property access";
1617                         case ExprClass.EventAccess:
1618                                 return "event access";
1619                         case ExprClass.IndexerAccess:
1620                                 return "indexer access";
1621                         case ExprClass.Nothing:
1622                                 return "null";
1623                         }
1624                         throw new Exception ("Should not happen");
1625                 }
1626                 
1627                 // <summary>
1628                 //   Reports that we were expecting `expr' to be of class `expected'
1629                 // </summary>
1630                 protected void report118 (Location loc, Expression expr, string expected)
1631                 {
1632                         string kind = "Unknown";
1633                         
1634                         if (expr != null)
1635                                 kind = ExprClassName (expr.ExprClass);
1636
1637                         Error (118, loc, "Expression denotes a '" + kind +
1638                                "' where an " + expected + " was expected");
1639                 }
1640         }
1641
1642         // <summary>
1643         //   This is just a base class for expressions that can
1644         //   appear on statements (invocations, object creation,
1645         //   assignments, post/pre increment and decrement).  The idea
1646         //   being that they would support an extra Emition interface that
1647         //   does not leave a result on the stack.
1648         // </summary>
1649
1650         public abstract class ExpressionStatement : Expression {
1651
1652                 // <summary>
1653                 //   Requests the expression to be emitted in a `statement'
1654                 //   context.  This means that no new value is left on the
1655                 //   stack after invoking this method (constrasted with
1656                 //   Emit that will always leave a value on the stack).
1657                 // </summary>
1658                 public abstract void EmitStatement (EmitContext ec);
1659         }
1660
1661         // <summary>
1662         //   This kind of cast is used to encapsulate the child
1663         //   whose type is child.Type into an expression that is
1664         //   reported to return "return_type".  This is used to encapsulate
1665         //   expressions which have compatible types, but need to be dealt
1666         //   at higher levels with.
1667         //
1668         //   For example, a "byte" expression could be encapsulated in one
1669         //   of these as an "unsigned int".  The type for the expression
1670         //   would be "unsigned int".
1671         //
1672         // </summary>
1673         
1674         public class EmptyCast : Expression {
1675                 protected Expression child;
1676
1677                 public EmptyCast (Expression child, Type return_type)
1678                 {
1679                         ExprClass = child.ExprClass;
1680                         type = return_type;
1681                         this.child = child;
1682                 }
1683
1684                 public override Expression DoResolve (EmitContext ec)
1685                 {
1686                         // This should never be invoked, we are born in fully
1687                         // initialized state.
1688
1689                         return this;
1690                 }
1691
1692                 public override void Emit (EmitContext ec)
1693                 {
1694                         child.Emit (ec);
1695                 }                       
1696         }
1697
1698         // <summary>
1699         //   This kind of cast is used to encapsulate Value Types in objects.
1700         //
1701         //   The effect of it is to box the value type emitted by the previous
1702         //   operation.
1703         // </summary>
1704         public class BoxedCast : EmptyCast {
1705
1706                 public BoxedCast (Expression expr)
1707                         : base (expr, TypeManager.object_type)
1708                 {
1709                 }
1710
1711                 public override Expression DoResolve (EmitContext ec)
1712                 {
1713                         // This should never be invoked, we are born in fully
1714                         // initialized state.
1715
1716                         return this;
1717                 }
1718
1719                 public override void Emit (EmitContext ec)
1720                 {
1721                         base.Emit (ec);
1722                         ec.ig.Emit (OpCodes.Box, child.Type);
1723                 }
1724         }
1725
1726         // <summary>
1727         //   This kind of cast is used to encapsulate a child expression
1728         //   that can be trivially converted to a target type using one or 
1729         //   two opcodes.  The opcodes are passed as arguments.
1730         // </summary>
1731         public class OpcodeCast : EmptyCast {
1732                 OpCode op, op2;
1733                 bool second_valid;
1734                 
1735                 public OpcodeCast (Expression child, Type return_type, OpCode op)
1736                         : base (child, return_type)
1737                         
1738                 {
1739                         this.op = op;
1740                         second_valid = false;
1741                 }
1742
1743                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1744                         : base (child, return_type)
1745                         
1746                 {
1747                         this.op = op;
1748                         this.op2 = op2;
1749                         second_valid = true;
1750                 }
1751
1752                 public override Expression DoResolve (EmitContext ec)
1753                 {
1754                         // This should never be invoked, we are born in fully
1755                         // initialized state.
1756
1757                         return this;
1758                 }
1759
1760                 public override void Emit (EmitContext ec)
1761                 {
1762                         base.Emit (ec);
1763                         ec.ig.Emit (op);
1764
1765                         if (second_valid)
1766                                 ec.ig.Emit (op2);
1767                 }                       
1768                 
1769         }
1770
1771         // <summary>
1772         //   This kind of cast is used to encapsulate a child and cast it
1773         //   to the class requested
1774         // </summary>
1775         public class ClassCast : EmptyCast {
1776                 public ClassCast (Expression child, Type return_type)
1777                         : base (child, return_type)
1778                         
1779                 {
1780                 }
1781
1782                 public override Expression DoResolve (EmitContext ec)
1783                 {
1784                         // This should never be invoked, we are born in fully
1785                         // initialized state.
1786
1787                         return this;
1788                 }
1789
1790                 public override void Emit (EmitContext ec)
1791                 {
1792                         base.Emit (ec);
1793
1794                         ec.ig.Emit (OpCodes.Castclass, type);
1795                 }                       
1796                 
1797         }
1798         
1799         // <summary>
1800         //   Unary expressions.  
1801         // </summary>
1802         //
1803         // <remarks>
1804         //   Unary implements unary expressions.   It derives from
1805         //   ExpressionStatement becuase the pre/post increment/decrement
1806         //   operators can be used in a statement context.
1807         // </remarks>
1808         public class Unary : ExpressionStatement {
1809                 public enum Operator {
1810                         UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
1811                         Indirection, AddressOf, PreIncrement,
1812                         PreDecrement, PostIncrement, PostDecrement 
1813                 }
1814
1815                 Operator   oper;
1816                 Expression expr;
1817                 ArrayList  Arguments;
1818                 MethodBase method;
1819                 Location   loc;
1820                 
1821                 public Unary (Operator op, Expression expr, Location loc)
1822                 {
1823                         this.oper = op;
1824                         this.expr = expr;
1825                         this.loc = loc;
1826                 }
1827
1828                 public Expression Expr {
1829                         get {
1830                                 return expr;
1831                         }
1832
1833                         set {
1834                                 expr = value;
1835                         }
1836                 }
1837
1838                 public Operator Oper {
1839                         get {
1840                                 return oper;
1841                         }
1842
1843                         set {
1844                                 oper = value;
1845                         }
1846                 }
1847
1848                 // <summary>
1849                 //   Returns a stringified representation of the Operator
1850                 // </summary>
1851                 string OperName ()
1852                 {
1853                         switch (oper){
1854                         case Operator.UnaryPlus:
1855                                 return "+";
1856                         case Operator.UnaryNegation:
1857                                 return "-";
1858                         case Operator.LogicalNot:
1859                                 return "!";
1860                         case Operator.OnesComplement:
1861                                 return "~";
1862                         case Operator.AddressOf:
1863                                 return "&";
1864                         case Operator.Indirection:
1865                                 return "*";
1866                         case Operator.PreIncrement : case Operator.PostIncrement :
1867                                 return "++";
1868                         case Operator.PreDecrement : case Operator.PostDecrement :
1869                                 return "--";
1870                         }
1871
1872                         return oper.ToString ();
1873                 }
1874
1875                 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1876                 {
1877                         if (expr.Type == target_type)
1878                                 return expr;
1879
1880                         return ConvertImplicit (ec, expr, target_type, new Location (-1));
1881                 }
1882
1883                 void error23 (Type t)
1884                 {
1885                         Report.Error (
1886                                 23, loc, "Operator " + OperName () +
1887                                 " cannot be applied to operand of type `" +
1888                                 TypeManager.CSharpName (t) + "'");
1889                 }
1890
1891                 // <summary>
1892                 //   Returns whether an object of type `t' can be incremented
1893                 //   or decremented with add/sub (ie, basically whether we can
1894                 //   use pre-post incr-decr operations on it, but it is not a
1895                 //   System.Decimal, which we test elsewhere)
1896                 // </summary>
1897                 static bool IsIncrementableNumber (Type t)
1898                 {
1899                         return (t == TypeManager.sbyte_type) ||
1900                                 (t == TypeManager.byte_type) ||
1901                                 (t == TypeManager.short_type) ||
1902                                 (t == TypeManager.ushort_type) ||
1903                                 (t == TypeManager.int32_type) ||
1904                                 (t == TypeManager.uint32_type) ||
1905                                 (t == TypeManager.int64_type) ||
1906                                 (t == TypeManager.uint64_type) ||
1907                                 (t == TypeManager.char_type) ||
1908                                 (t.IsSubclassOf (TypeManager.enum_type)) ||
1909                                 (t == TypeManager.float_type) ||
1910                                 (t == TypeManager.double_type);
1911                 }
1912                         
1913                 Expression ResolveOperator (EmitContext ec)
1914                 {
1915                         Type expr_type = expr.Type;
1916
1917                         //
1918                         // Step 1: Perform Operator Overload location
1919                         //
1920                         Expression mg;
1921                         string op_name;
1922                         
1923                         if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
1924                                 op_name = "op_Increment";
1925                         else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
1926                                 op_name = "op_Decrement";
1927                         else
1928                                 op_name = "op_" + oper;
1929
1930                         mg = MemberLookup (ec, expr_type, op_name, false, loc);
1931                         
1932                         if (mg == null && expr_type.BaseType != null)
1933                                 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
1934                         
1935                         if (mg != null) {
1936                                 Arguments = new ArrayList ();
1937                                 Arguments.Add (new Argument (expr, Argument.AType.Expression));
1938                                 
1939                                 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg,
1940                                                                      Arguments, loc);
1941                                 if (method != null) {
1942                                         MethodInfo mi = (MethodInfo) method;
1943                                         type = mi.ReturnType;
1944                                         return this;
1945                                 } else {
1946                                         error23 (expr_type);
1947                                         return null;
1948                                 }
1949                                         
1950                         }
1951
1952                         //
1953                         // Step 2: Default operations on CLI native types.
1954                         //
1955
1956                         // Only perform numeric promotions on:
1957                         // +, -, ++, --
1958
1959                         if (expr_type == null)
1960                                 return null;
1961                         
1962                         if (oper == Operator.LogicalNot){
1963                                 if (expr_type != TypeManager.bool_type) {
1964                                         error23 (expr.Type);
1965                                         return null;
1966                                 }
1967                                 
1968                                 type = TypeManager.bool_type;
1969                                 return this;
1970                         }
1971
1972                         if (oper == Operator.OnesComplement) {
1973                                 if (!((expr_type == TypeManager.int32_type) ||
1974                                       (expr_type == TypeManager.uint32_type) ||
1975                                       (expr_type == TypeManager.int64_type) ||
1976                                       (expr_type == TypeManager.uint64_type) ||
1977                                       (expr_type.IsSubclassOf (TypeManager.enum_type)))){
1978                                         error23 (expr.Type);
1979                                         return null;
1980                                 }
1981                                 type = expr_type;
1982                                 return this;
1983                         }
1984
1985                         if (oper == Operator.UnaryPlus) {
1986                                 //
1987                                 // A plus in front of something is just a no-op, so return the child.
1988                                 //
1989                                 return expr;
1990                         }
1991
1992                         //
1993                         // Deals with -literals
1994                         // int     operator- (int x)
1995                         // long    operator- (long x)
1996                         // float   operator- (float f)
1997                         // double  operator- (double d)
1998                         // decimal operator- (decimal d)
1999                         //
2000                         if (oper == Operator.UnaryNegation){
2001                                 //
2002                                 // Fold a "- Constant" into a negative constant
2003                                 //
2004                         
2005                                 Expression e = null;
2006
2007                                 //
2008                                 // Is this a constant? 
2009                                 //
2010                                 if (expr is IntLiteral)
2011                                         e = new IntLiteral (-((IntLiteral) expr).Value);
2012                                 else if (expr is LongLiteral)
2013                                         e = new LongLiteral (-((LongLiteral) expr).Value);
2014                                 else if (expr is FloatLiteral)
2015                                         e = new FloatLiteral (-((FloatLiteral) expr).Value);
2016                                 else if (expr is DoubleLiteral)
2017                                         e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
2018                                 else if (expr is DecimalLiteral)
2019                                         e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
2020                                 
2021                                 if (e != null){
2022                                         e = e.Resolve (ec);
2023                                         return e;
2024                                 }
2025
2026                                 //
2027                                 // Not a constant we can optimize, perform numeric 
2028                                 // promotions to int, long, double.
2029                                 //
2030                                 //
2031                                 // The following is inneficient, because we call
2032                                 // ConvertImplicit too many times.
2033                                 //
2034                                 // It is also not clear if we should convert to Float
2035                                 // or Double initially.
2036                                 //
2037                                 if (expr_type == TypeManager.uint32_type){
2038                                         //
2039                                         // FIXME: handle exception to this rule that
2040                                         // permits the int value -2147483648 (-2^31) to
2041                                         // bt written as a decimal interger literal
2042                                         //
2043                                         type = TypeManager.int64_type;
2044                                         expr = ConvertImplicit (ec, expr, type, loc);
2045                                         return this;
2046                                 }
2047
2048                                 if (expr_type == TypeManager.uint64_type){
2049                                         //
2050                                         // FIXME: Handle exception of `long value'
2051                                         // -92233720368547758087 (-2^63) to be written as
2052                                         // decimal integer literal.
2053                                         //
2054                                         error23 (expr_type);
2055                                         return null;
2056                                 }
2057
2058                                 e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
2059                                 if (e != null){
2060                                         expr = e;
2061                                         type = e.Type;
2062                                         return this;
2063                                 } 
2064
2065                                 e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
2066                                 if (e != null){
2067                                         expr = e;
2068                                         type = e.Type;
2069                                         return this;
2070                                 }
2071
2072                                 e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
2073                                 if (e != null){
2074                                         expr = e;
2075                                         type = e.Type;
2076                                         return this;
2077                                 }
2078
2079                                 error23 (expr_type);
2080                                 return null;
2081                         }
2082
2083                         //
2084                         // The operand of the prefix/postfix increment decrement operators
2085                         // should be an expression that is classified as a variable,
2086                         // a property access or an indexer access
2087                         //
2088                         if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
2089                             oper == Operator.PostDecrement || oper == Operator.PostIncrement){
2090                                 if (expr.ExprClass == ExprClass.Variable){
2091                                         if (IsIncrementableNumber (expr_type) ||
2092                                             expr_type == TypeManager.decimal_type){
2093                                                 type = expr_type;
2094                                                 return this;
2095                                         }
2096                                 } else if (expr.ExprClass == ExprClass.IndexerAccess){
2097                                         //
2098                                         // FIXME: Verify that we have both get and set methods
2099                                         //
2100                                         throw new Exception ("Implement me");
2101                                 } else if (expr.ExprClass == ExprClass.PropertyAccess){
2102                                         PropertyExpr pe = (PropertyExpr) expr;
2103                                         
2104                                         if (pe.VerifyAssignable ())
2105                                                 return this;
2106                                         return null;
2107                                 } else {
2108                                         report118 (loc, expr, "variable, indexer or property access");
2109                                 }
2110                         }
2111
2112                         if (oper == Operator.AddressOf){
2113                                 if (expr.ExprClass != ExprClass.Variable){
2114                                         Error (211, "Cannot take the address of non-variables");
2115                                         return null;
2116                                 }
2117                                 type = Type.GetType (expr.Type.ToString () + "*");
2118                         }
2119                         
2120                         Error (187, "No such operator '" + OperName () + "' defined for type '" +
2121                                TypeManager.CSharpName (expr_type) + "'");
2122                         return null;
2123
2124                 }
2125
2126                 public override Expression DoResolve (EmitContext ec)
2127                 {
2128                         expr = expr.Resolve (ec);
2129                         
2130                         if (expr == null)
2131                                 return null;
2132
2133                         eclass = ExprClass.Value;
2134                         return ResolveOperator (ec);
2135                 }
2136
2137                 public override void Emit (EmitContext ec)
2138                 {
2139                         ILGenerator ig = ec.ig;
2140                         Type expr_type = expr.Type;
2141                         ExprClass eclass;
2142                         
2143                         if (method != null) {
2144
2145                                 // Note that operators are static anyway
2146                                 
2147                                 if (Arguments != null) 
2148                                         Invocation.EmitArguments (ec, method, Arguments);
2149
2150                                 //
2151                                 // Post increment/decrement operations need a copy at this
2152                                 // point.
2153                                 //
2154                                 if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
2155                                         ig.Emit (OpCodes.Dup);
2156                                 
2157
2158                                 ig.Emit (OpCodes.Call, (MethodInfo) method);
2159
2160                                 //
2161                                 // Pre Increment and Decrement operators
2162                                 //
2163                                 if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
2164                                         ig.Emit (OpCodes.Dup);
2165                                 }
2166                                 
2167                                 //
2168                                 // Increment and Decrement should store the result
2169                                 //
2170                                 if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
2171                                     oper == Operator.PostDecrement || oper == Operator.PostIncrement){
2172                                         ((IStackStore) expr).Store (ec);
2173                                 }
2174                                 return;
2175                         }
2176                         
2177                         switch (oper) {
2178                         case Operator.UnaryPlus:
2179                                 throw new Exception ("This should be caught by Resolve");
2180                                 
2181                         case Operator.UnaryNegation:
2182                                 expr.Emit (ec);
2183                                 ig.Emit (OpCodes.Neg);
2184                                 break;
2185                                 
2186                         case Operator.LogicalNot:
2187                                 expr.Emit (ec);
2188                                 ig.Emit (OpCodes.Ldc_I4_0);
2189                                 ig.Emit (OpCodes.Ceq);
2190                                 break;
2191                                 
2192                         case Operator.OnesComplement:
2193                                 expr.Emit (ec);
2194                                 ig.Emit (OpCodes.Not);
2195                                 break;
2196                                 
2197                         case Operator.AddressOf:
2198                                 ((IMemoryLocation)expr).AddressOf (ec);
2199                                 break;
2200                                 
2201                         case Operator.Indirection:
2202                                 throw new Exception ("Not implemented yet");
2203                                 
2204                         case Operator.PreIncrement:
2205                         case Operator.PreDecrement:
2206                                 if (expr.ExprClass == ExprClass.Variable){
2207                                         //
2208                                         // Resolve already verified that it is an "incrementable"
2209                                         // 
2210                                         expr.Emit (ec);
2211                                         ig.Emit (OpCodes.Ldc_I4_1);
2212                                         
2213                                         if (oper == Operator.PreDecrement)
2214                                                 ig.Emit (OpCodes.Sub);
2215                                         else
2216                                                 ig.Emit (OpCodes.Add);
2217                                         ig.Emit (OpCodes.Dup);
2218                                         ((IStackStore) expr).Store (ec);
2219                                 } else {
2220                                         throw new Exception ("Handle Indexers and Properties here");
2221                                 }
2222                                 break;
2223                                 
2224                         case Operator.PostIncrement:
2225                         case Operator.PostDecrement:
2226                                 eclass = expr.ExprClass;
2227                                 if (eclass == ExprClass.Variable){
2228                                         //
2229                                         // Resolve already verified that it is an "incrementable"
2230                                         // 
2231                                         expr.Emit (ec);
2232                                         ig.Emit (OpCodes.Dup);
2233                                         ig.Emit (OpCodes.Ldc_I4_1);
2234                                         
2235                                         if (oper == Operator.PostDecrement)
2236                                                 ig.Emit (OpCodes.Sub);
2237                                         else
2238                                                 ig.Emit (OpCodes.Add);
2239                                         ((IStackStore) expr).Store (ec);
2240                                 } else if (eclass == ExprClass.PropertyAccess){
2241                                         throw new Exception ("Handle Properties here");
2242                                 } else if (eclass == ExprClass.IndexerAccess) {
2243                                         throw new Exception ("Handle Indexers here");
2244                                 } else {
2245                                         Console.WriteLine ("Unknown exprclass: " + eclass);
2246                                 }
2247                                 break;
2248                                 
2249                         default:
2250                                 throw new Exception ("This should not happen: Operator = "
2251                                                      + oper.ToString ());
2252                         }
2253                 }
2254                 
2255
2256                 public override void EmitStatement (EmitContext ec)
2257                 {
2258                         //
2259                         // FIXME: we should rewrite this code to generate
2260                         // better code for ++ and -- as we know we wont need
2261                         // the values on the stack
2262                         //
2263                         Emit (ec);
2264                         ec.ig.Emit (OpCodes.Pop);
2265                 }
2266         }
2267         
2268         public class Probe : Expression {
2269                 public readonly string ProbeType;
2270                 public readonly Operator Oper;
2271                 Expression expr;
2272                 Type probe_type;
2273                 
2274                 public enum Operator {
2275                         Is, As
2276                 }
2277                 
2278                 public Probe (Operator oper, Expression expr, string probe_type)
2279                 {
2280                         Oper = oper;
2281                         ProbeType = probe_type;
2282                         this.expr = expr;
2283                 }
2284
2285                 public Expression Expr {
2286                         get {
2287                                 return expr;
2288                         }
2289                 }
2290                 
2291                 public override Expression DoResolve (EmitContext ec)
2292                 {
2293                         probe_type = ec.TypeContainer.LookupType (ProbeType, false);
2294
2295                         if (probe_type == null)
2296                                 return null;
2297
2298                         expr = expr.Resolve (ec);
2299                         
2300                         type = TypeManager.bool_type;
2301                         eclass = ExprClass.Value;
2302
2303                         return this;
2304                 }
2305
2306                 public override void Emit (EmitContext ec)
2307                 {
2308                         ILGenerator ig = ec.ig;
2309                         
2310                         expr.Emit (ec);
2311                         
2312                         if (Oper == Operator.Is){
2313                                 ig.Emit (OpCodes.Isinst, probe_type);
2314                                 ig.Emit (OpCodes.Ldnull);
2315                                 ig.Emit (OpCodes.Cgt_Un);
2316                         } else {
2317                                 ig.Emit (OpCodes.Isinst, probe_type);
2318                         }
2319                 }
2320         }
2321
2322         // <summary>
2323         //   This represents a typecast in the source language.
2324         //
2325         //   FIXME: Cast expressions have an unusual set of parsing
2326         //   rules, we need to figure those out.
2327         // </summary>
2328         public class Cast : Expression {
2329                 string target_type;
2330                 Expression expr;
2331                 Location   loc;
2332                         
2333                 public Cast (string cast_type, Expression expr, Location loc)
2334                 {
2335                         this.target_type = cast_type;
2336                         this.expr = expr;
2337                         this.loc = loc;
2338                 }
2339
2340                 public string TargetType {
2341                         get {
2342                                 return target_type;
2343                         }
2344                 }
2345
2346                 public Expression Expr {
2347                         get {
2348                                 return expr;
2349                         }
2350                         set {
2351                                 expr = value;
2352                         }
2353                 }
2354                 
2355                 public override Expression DoResolve (EmitContext ec)
2356                 {
2357                         expr = expr.Resolve (ec);
2358                         if (expr == null)
2359                                 return null;
2360                         
2361                         type = ec.TypeContainer.LookupType (target_type, false);
2362                         eclass = ExprClass.Value;
2363                         
2364                         if (type == null)
2365                                 return null;
2366
2367                         expr = ConvertExplicit (ec, expr, type, loc);
2368
2369                         return expr;
2370                 }
2371
2372                 public override void Emit (EmitContext ec)
2373                 {
2374                         //
2375                         // This one will never happen
2376                         //
2377                         throw new Exception ("Should not happen");
2378                 }
2379         }
2380
2381         public class Binary : Expression {
2382                 public enum Operator {
2383                         Multiply, Division, Modulus,
2384                         Addition, Subtraction,
2385                         LeftShift, RightShift,
2386                         LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual, 
2387                         Equality, Inequality,
2388                         BitwiseAnd,
2389                         ExclusiveOr,
2390                         BitwiseOr,
2391                         LogicalAnd,
2392                         LogicalOr
2393                 }
2394
2395                 Operator oper;
2396                 Expression left, right;
2397                 MethodBase method;
2398                 ArrayList  Arguments;
2399                 Location   loc;
2400                 
2401
2402                 public Binary (Operator oper, Expression left, Expression right, Location loc)
2403                 {
2404                         this.oper = oper;
2405                         this.left = left;
2406                         this.right = right;
2407                         this.loc = loc;
2408                 }
2409
2410                 public Operator Oper {
2411                         get {
2412                                 return oper;
2413                         }
2414                         set {
2415                                 oper = value;
2416                         }
2417                 }
2418                 
2419                 public Expression Left {
2420                         get {
2421                                 return left;
2422                         }
2423                         set {
2424                                 left = value;
2425                         }
2426                 }
2427
2428                 public Expression Right {
2429                         get {
2430                                 return right;
2431                         }
2432                         set {
2433                                 right = value;
2434                         }
2435                 }
2436
2437
2438                 // <summary>
2439                 //   Returns a stringified representation of the Operator
2440                 // </summary>
2441                 string OperName ()
2442                 {
2443                         switch (oper){
2444                         case Operator.Multiply:
2445                                 return "*";
2446                         case Operator.Division:
2447                                 return "/";
2448                         case Operator.Modulus:
2449                                 return "%";
2450                         case Operator.Addition:
2451                                 return "+";
2452                         case Operator.Subtraction:
2453                                 return "-";
2454                         case Operator.LeftShift:
2455                                 return "<<";
2456                         case Operator.RightShift:
2457                                 return ">>";
2458                         case Operator.LessThan:
2459                                 return "<";
2460                         case Operator.GreaterThan:
2461                                 return ">";
2462                         case Operator.LessThanOrEqual:
2463                                 return "<=";
2464                         case Operator.GreaterThanOrEqual:
2465                                 return ">=";
2466                         case Operator.Equality:
2467                                 return "==";
2468                         case Operator.Inequality:
2469                                 return "!=";
2470                         case Operator.BitwiseAnd:
2471                                 return "&";
2472                         case Operator.BitwiseOr:
2473                                 return "|";
2474                         case Operator.ExclusiveOr:
2475                                 return "^";
2476                         case Operator.LogicalOr:
2477                                 return "||";
2478                         case Operator.LogicalAnd:
2479                                 return "&&";
2480                         }
2481
2482                         return oper.ToString ();
2483                 }
2484
2485                 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
2486                 {
2487                         if (expr.Type == target_type)
2488                                 return expr;
2489
2490                         return ConvertImplicit (ec, expr, target_type, new Location (-1));
2491                 }
2492                 
2493                 //
2494                 // Note that handling the case l == Decimal || r == Decimal
2495                 // is taken care of by the Step 1 Operator Overload resolution.
2496                 //
2497                 void DoNumericPromotions (EmitContext ec, Type l, Type r)
2498                 {
2499                         if (l == TypeManager.double_type || r == TypeManager.double_type){
2500                                 //
2501                                 // If either operand is of type double, the other operand is
2502                                 // conveted to type double.
2503                                 //
2504                                 if (r != TypeManager.double_type)
2505                                         right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
2506                                 if (l != TypeManager.double_type)
2507                                         left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
2508                                 
2509                                 type = TypeManager.double_type;
2510                         } else if (l == TypeManager.float_type || r == TypeManager.float_type){
2511                                 //
2512                                 // if either operand is of type float, th eother operand is
2513                                 // converd to type float.
2514                                 //
2515                                 if (r != TypeManager.double_type)
2516                                         right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
2517                                 if (l != TypeManager.double_type)
2518                                         left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
2519                                 type = TypeManager.float_type;
2520                         } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
2521                                 Expression e;
2522                                 Type other;
2523                                 //
2524                                 // If either operand is of type ulong, the other operand is
2525                                 // converted to type ulong.  or an error ocurrs if the other
2526                                 // operand is of type sbyte, short, int or long
2527                                 //
2528                                 
2529                                 if (l == TypeManager.uint64_type){
2530                                         if (r != TypeManager.uint64_type && right is IntLiteral){
2531                                                 e = TryImplicitIntConversion (l, (IntLiteral) right);
2532                                                 if (e != null)
2533                                                         right = e;
2534                                         }
2535                                         other = right.Type;
2536                                 } else {
2537                                         if (left is IntLiteral){
2538                                                 e = TryImplicitIntConversion (r, (IntLiteral) left);
2539                                                 if (e != null)
2540                                                         left = e;
2541                                         }
2542                                         other = left.Type;
2543                                 }
2544
2545                                 if ((other == TypeManager.sbyte_type) ||
2546                                     (other == TypeManager.short_type) ||
2547                                     (other == TypeManager.int32_type) ||
2548                                     (other == TypeManager.int64_type)){
2549                                         string oper = OperName ();
2550                                         
2551                                         Error (34, loc, "Operator `" + OperName ()
2552                                                + "' is ambiguous on operands of type `"
2553                                                + TypeManager.CSharpName (l) + "' "
2554                                                + "and `" + TypeManager.CSharpName (r)
2555                                                + "'");
2556                                 }
2557                                 type = TypeManager.uint64_type;
2558                         } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
2559                                 //
2560                                 // If either operand is of type long, the other operand is converted
2561                                 // to type long.
2562                                 //
2563                                 if (l != TypeManager.int64_type)
2564                                         left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
2565                                 if (r != TypeManager.int64_type)
2566                                         right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
2567
2568                                 type = TypeManager.int64_type;
2569                         } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
2570                                 //
2571                                 // If either operand is of type uint, and the other
2572                                 // operand is of type sbyte, short or int, othe operands are
2573                                 // converted to type long.
2574                                 //
2575                                 Type other = null;
2576                                 
2577                                 if (l == TypeManager.uint32_type)
2578                                         other = r;
2579                                 else if (r == TypeManager.uint32_type)
2580                                         other = l;
2581
2582                                 if ((other == TypeManager.sbyte_type) ||
2583                                     (other == TypeManager.short_type) ||
2584                                     (other == TypeManager.int32_type)){
2585                                         left = ForceConversion (ec, left, TypeManager.int64_type);
2586                                         right = ForceConversion (ec, right, TypeManager.int64_type);
2587                                         type = TypeManager.int64_type;
2588                                 } else {
2589                                         //
2590                                         // if either operand is of type uint, the other
2591                                         // operand is converd to type uint
2592                                         //
2593                                         left = ForceConversion (ec, left, TypeManager.uint32_type);
2594                                         right = ForceConversion (ec, right, TypeManager.uint32_type);
2595                                         type = TypeManager.uint32_type;
2596                                 } 
2597                         } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2598                                 if (l != TypeManager.decimal_type)
2599                                         left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
2600                                 if (r != TypeManager.decimal_type)
2601                                         right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
2602
2603                                 type = TypeManager.decimal_type;
2604                         } else {
2605                                 Expression l_tmp, r_tmp;
2606
2607                                 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
2608                                 if (l_tmp == null) {
2609                                         error19 ();
2610                                         left = l_tmp;
2611                                         return;
2612                                 }
2613                                 
2614                                 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
2615                                 if (r_tmp == null) {
2616                                         error19 ();
2617                                         right = r_tmp;
2618                                         return;
2619                                 }
2620                                 
2621                                 type = TypeManager.int32_type;
2622                         }
2623                 }
2624
2625                 void error19 ()
2626                 {
2627                         Error (19, loc,
2628                                "Operator " + OperName () + " cannot be applied to operands of type `" +
2629                                TypeManager.CSharpName (left.Type) + "' and `" +
2630                                TypeManager.CSharpName (right.Type) + "'");
2631                                                      
2632                 }
2633                 
2634                 Expression CheckShiftArguments (EmitContext ec)
2635                 {
2636                         Expression e;
2637                         Type l = left.Type;
2638                         Type r = right.Type;
2639
2640                         e = ForceConversion (ec, right, TypeManager.int32_type);
2641                         if (e == null){
2642                                 error19 ();
2643                                 return null;
2644                         }
2645                         right = e;
2646
2647                         if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
2648                             ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
2649                             ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
2650                             ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
2651                                 left = e;
2652                                 type = e.Type;
2653
2654                                 return this;
2655                         }
2656                         error19 ();
2657                         return null;
2658                 }
2659                 
2660                 Expression ResolveOperator (EmitContext ec)
2661                 {
2662                         Type l = left.Type;
2663                         Type r = right.Type;
2664
2665                         //
2666                         // Step 1: Perform Operator Overload location
2667                         //
2668                         Expression left_expr, right_expr;
2669                         
2670                         string op = "op_" + oper;
2671
2672                         left_expr = MemberLookup (ec, l, op, false, loc);
2673                         if (left_expr == null && l.BaseType != null)
2674                                 left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
2675                         
2676                         right_expr = MemberLookup (ec, r, op, false, loc);
2677                         if (right_expr == null && r.BaseType != null)
2678                                 right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
2679                         
2680                         MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
2681                         
2682                         if (union != null) {
2683                                 Arguments = new ArrayList ();
2684                                 Arguments.Add (new Argument (left, Argument.AType.Expression));
2685                                 Arguments.Add (new Argument (right, Argument.AType.Expression));
2686                                 
2687                                 method = Invocation.OverloadResolve (ec, union, Arguments, loc);
2688                                 if (method != null) {
2689                                         MethodInfo mi = (MethodInfo) method;
2690                                         type = mi.ReturnType;
2691                                         return this;
2692                                 } else {
2693                                         error19 ();
2694                                         return null;
2695                                 }
2696                         }       
2697
2698                         //
2699                         // Step 2: Default operations on CLI native types.
2700                         //
2701                         
2702                         // Only perform numeric promotions on:
2703                         // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2704                         //
2705                         if (oper == Operator.Addition){
2706                                 //
2707                                 // If any of the arguments is a string, cast to string
2708                                 //
2709                                 if (l == TypeManager.string_type){
2710                                         if (r == TypeManager.string_type){
2711                                                 // string + string
2712                                                 method = TypeManager.string_concat_string_string;
2713                                         } else {
2714                                                 // string + object
2715                                                 method = TypeManager.string_concat_object_object;
2716                                                 right = ConvertImplicit (ec, right,
2717                                                                          TypeManager.object_type, loc);
2718                                         }
2719                                         type = TypeManager.string_type;
2720
2721                                         Arguments = new ArrayList ();
2722                                         Arguments.Add (new Argument (left, Argument.AType.Expression));
2723                                         Arguments.Add (new Argument (right, Argument.AType.Expression));
2724
2725                                         return this;
2726                                         
2727                                 } else if (r == TypeManager.string_type){
2728                                         // object + string
2729                                         method = TypeManager.string_concat_object_object;
2730                                         Arguments = new ArrayList ();
2731                                         Arguments.Add (new Argument (left, Argument.AType.Expression));
2732                                         Arguments.Add (new Argument (right, Argument.AType.Expression));
2733
2734                                         left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
2735                                         type = TypeManager.string_type;
2736
2737                                         return this;
2738                                 }
2739
2740                                 //
2741                                 // FIXME: is Delegate operator + (D x, D y) handled?
2742                                 //
2743                         }
2744                         
2745                         if (oper == Operator.LeftShift || oper == Operator.RightShift)
2746                                 return CheckShiftArguments (ec);
2747
2748                         if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2749                                 if (l != TypeManager.bool_type || r != TypeManager.bool_type)
2750                                         error19 ();
2751
2752                                 type = TypeManager.bool_type;
2753                                 return this;
2754                         } 
2755
2756                         //
2757                         // We are dealing with numbers
2758                         //
2759
2760                         DoNumericPromotions (ec, l, r);
2761
2762                         if (left == null || right == null)
2763                                 return null;
2764
2765                         
2766                         if (oper == Operator.BitwiseAnd ||
2767                             oper == Operator.BitwiseOr ||
2768                             oper == Operator.ExclusiveOr){
2769                                 if (!((l == TypeManager.int32_type) ||
2770                                       (l == TypeManager.uint32_type) ||
2771                                       (l == TypeManager.int64_type) ||
2772                                       (l == TypeManager.uint64_type))){
2773                                         error19 ();
2774                                         return null;
2775                                 }
2776                                 type = l;
2777                         }
2778
2779                         if (oper == Operator.Equality ||
2780                             oper == Operator.Inequality ||
2781                             oper == Operator.LessThanOrEqual ||
2782                             oper == Operator.LessThan ||
2783                             oper == Operator.GreaterThanOrEqual ||
2784                             oper == Operator.GreaterThan){
2785                                 type = TypeManager.bool_type;
2786                         }
2787
2788                         return this;
2789                 }
2790                 
2791                 public override Expression DoResolve (EmitContext ec)
2792                 {
2793                         left = left.Resolve (ec);
2794                         right = right.Resolve (ec);
2795
2796                         if (left == null || right == null)
2797                                 return null;
2798
2799                         if (left.Type == null)
2800                                 throw new Exception (
2801                                         "Resolve returned non null, but did not set the type! (" +
2802                                         left + ") at Line: " + loc.Row);
2803                         if (right.Type == null)
2804                                 throw new Exception (
2805                                         "Resolve returned non null, but did not set the type! (" +
2806                                         right + ") at Line: "+ loc.Row);
2807
2808                         eclass = ExprClass.Value;
2809
2810                         return ResolveOperator (ec);
2811                 }
2812
2813                 public bool IsBranchable ()
2814                 {
2815                         if (oper == Operator.Equality ||
2816                             oper == Operator.Inequality ||
2817                             oper == Operator.LessThan ||
2818                             oper == Operator.GreaterThan ||
2819                             oper == Operator.LessThanOrEqual ||
2820                             oper == Operator.GreaterThanOrEqual){
2821                                 return true;
2822                         } else
2823                                 return false;
2824                 }
2825
2826                 // <summary>
2827                 //   This entry point is used by routines that might want
2828                 //   to emit a brfalse/brtrue after an expression, and instead
2829                 //   they could use a more compact notation.
2830                 //
2831                 //   Typically the code would generate l.emit/r.emit, followed
2832                 //   by the comparission and then a brtrue/brfalse.  The comparissions
2833                 //   are sometimes inneficient (there are not as complete as the branches
2834                 //   look for the hacks in Emit using double ceqs).
2835                 //
2836                 //   So for those cases we provide EmitBranchable that can emit the
2837                 //   branch with the test
2838                 // </summary>
2839                 public void EmitBranchable (EmitContext ec, int target)
2840                 {
2841                         OpCode opcode;
2842                         bool close_target = false;
2843                         
2844                         left.Emit (ec);
2845                         right.Emit (ec);
2846                         
2847                         switch (oper){
2848                         case Operator.Equality:
2849                                 if (close_target)
2850                                         opcode = OpCodes.Beq_S;
2851                                 else
2852                                         opcode = OpCodes.Beq;
2853                                 break;
2854
2855                         case Operator.Inequality:
2856                                 if (close_target)
2857                                         opcode = OpCodes.Bne_Un_S;
2858                                 else
2859                                         opcode = OpCodes.Bne_Un;
2860                                 break;
2861
2862                         case Operator.LessThan:
2863                                 if (close_target)
2864                                         opcode = OpCodes.Blt_S;
2865                                 else
2866                                         opcode = OpCodes.Blt;
2867                                 break;
2868
2869                         case Operator.GreaterThan:
2870                                 if (close_target)
2871                                         opcode = OpCodes.Bgt_S;
2872                                 else
2873                                         opcode = OpCodes.Bgt;
2874                                 break;
2875
2876                         case Operator.LessThanOrEqual:
2877                                 if (close_target)
2878                                         opcode = OpCodes.Ble_S;
2879                                 else
2880                                         opcode = OpCodes.Ble;
2881                                 break;
2882
2883                         case Operator.GreaterThanOrEqual:
2884                                 if (close_target)
2885                                         opcode = OpCodes.Bge_S;
2886                                 else
2887                                         opcode = OpCodes.Ble;
2888                                 break;
2889
2890                         default:
2891                                 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
2892                                                      + oper.ToString ());
2893                         }
2894
2895                         ec.ig.Emit (opcode, target);
2896                 }
2897                 
2898                 public override void Emit (EmitContext ec)
2899                 {
2900                         ILGenerator ig = ec.ig;
2901                         Type l = left.Type;
2902                         Type r = right.Type;
2903                         OpCode opcode;
2904
2905                         if (method != null) {
2906
2907                                 // Note that operators are static anyway
2908                                 
2909                                 if (Arguments != null) 
2910                                         Invocation.EmitArguments (ec, method, Arguments);
2911                                 
2912                                 if (method is MethodInfo)
2913                                         ig.Emit (OpCodes.Call, (MethodInfo) method);
2914                                 else
2915                                         ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2916
2917                                 return;
2918                         }
2919                         
2920                         left.Emit (ec);
2921                         right.Emit (ec);
2922
2923                         switch (oper){
2924                         case Operator.Multiply:
2925                                 if (ec.CheckState){
2926                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2927                                                 opcode = OpCodes.Mul_Ovf;
2928                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2929                                                 opcode = OpCodes.Mul_Ovf_Un;
2930                                         else
2931                                                 opcode = OpCodes.Mul;
2932                                 } else
2933                                         opcode = OpCodes.Mul;
2934
2935                                 break;
2936
2937                         case Operator.Division:
2938                                 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2939                                         opcode = OpCodes.Div_Un;
2940                                 else
2941                                         opcode = OpCodes.Div;
2942                                 break;
2943
2944                         case Operator.Modulus:
2945                                 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
2946                                         opcode = OpCodes.Rem_Un;
2947                                 else
2948                                         opcode = OpCodes.Rem;
2949                                 break;
2950
2951                         case Operator.Addition:
2952                                 if (ec.CheckState){
2953                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2954                                                 opcode = OpCodes.Add_Ovf;
2955                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2956                                                 opcode = OpCodes.Add_Ovf_Un;
2957                                         else
2958                                                 opcode = OpCodes.Mul;
2959                                 } else
2960                                         opcode = OpCodes.Add;
2961                                 break;
2962
2963                         case Operator.Subtraction:
2964                                 if (ec.CheckState){
2965                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2966                                                 opcode = OpCodes.Sub_Ovf;
2967                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
2968                                                 opcode = OpCodes.Sub_Ovf_Un;
2969                                         else
2970                                                 opcode = OpCodes.Sub;
2971                                 } else
2972                                         opcode = OpCodes.Sub;
2973                                 break;
2974
2975                         case Operator.RightShift:
2976                                 opcode = OpCodes.Shr;
2977                                 break;
2978                                 
2979                         case Operator.LeftShift:
2980                                 opcode = OpCodes.Shl;
2981                                 break;
2982
2983                         case Operator.Equality:
2984                                 opcode = OpCodes.Ceq;
2985                                 break;
2986
2987                         case Operator.Inequality:
2988                                 ec.ig.Emit (OpCodes.Ceq);
2989                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
2990                                 
2991                                 opcode = OpCodes.Ceq;
2992                                 break;
2993
2994                         case Operator.LessThan:
2995                                 opcode = OpCodes.Clt;
2996                                 break;
2997
2998                         case Operator.GreaterThan:
2999                                 opcode = OpCodes.Cgt;
3000                                 break;
3001
3002                         case Operator.LessThanOrEqual:
3003                                 ec.ig.Emit (OpCodes.Cgt);
3004                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
3005                                 
3006                                 opcode = OpCodes.Ceq;
3007                                 break;
3008
3009                         case Operator.GreaterThanOrEqual:
3010                                 ec.ig.Emit (OpCodes.Clt);
3011                                 ec.ig.Emit (OpCodes.Ldc_I4_1);
3012                                 
3013                                 opcode = OpCodes.Sub;
3014                                 break;
3015
3016                         case Operator.LogicalOr:
3017                         case Operator.BitwiseOr:
3018                                 opcode = OpCodes.Or;
3019                                 break;
3020
3021                         case Operator.LogicalAnd:
3022                         case Operator.BitwiseAnd:
3023                                 opcode = OpCodes.And;
3024                                 break;
3025
3026                         case Operator.ExclusiveOr:
3027                                 opcode = OpCodes.Xor;
3028                                 break;
3029
3030                         default:
3031                                 throw new Exception ("This should not happen: Operator = "
3032                                                      + oper.ToString ());
3033                         }
3034
3035                         ig.Emit (opcode);
3036                 }
3037         }
3038
3039         public class Conditional : Expression {
3040                 Expression expr, trueExpr, falseExpr;
3041                 Location loc;
3042                 
3043                 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
3044                 {
3045                         this.expr = expr;
3046                         this.trueExpr = trueExpr;
3047                         this.falseExpr = falseExpr;
3048                         this.loc = l;
3049                 }
3050
3051                 public Expression Expr {
3052                         get {
3053                                 return expr;
3054                         }
3055                 }
3056
3057                 public Expression TrueExpr {
3058                         get {
3059                                 return trueExpr;
3060                         }
3061                 }
3062
3063                 public Expression FalseExpr {
3064                         get {
3065                                 return falseExpr;
3066                         }
3067                 }
3068
3069                 public override Expression DoResolve (EmitContext ec)
3070                 {
3071                         expr = expr.Resolve (ec);
3072
3073                         if (expr.Type != TypeManager.bool_type)
3074                                 expr = Expression.ConvertImplicitRequired (
3075                                         ec, expr, TypeManager.bool_type, loc);
3076                         
3077                         trueExpr = trueExpr.Resolve (ec);
3078                         falseExpr = falseExpr.Resolve (ec);
3079
3080                         if (expr == null || trueExpr == null || falseExpr == null)
3081                                 return null;
3082                         
3083                         if (trueExpr.Type == falseExpr.Type)
3084                                 type = trueExpr.Type;
3085                         else {
3086                                 Expression conv;
3087
3088                                 //
3089                                 // First, if an implicit conversion exists from trueExpr
3090                                 // to falseExpr, then the result type is of type falseExpr.Type
3091                                 //
3092                                 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
3093                                 if (conv != null){
3094                                         type = falseExpr.Type;
3095                                         trueExpr = conv;
3096                                 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
3097                                         type = trueExpr.Type;
3098                                         falseExpr = conv;
3099                                 } else {
3100                                         Error (173, loc, "The type of the conditional expression can " +
3101                                                "not be computed because there is no implicit conversion" +
3102                                                " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
3103                                                " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
3104                                         return null;
3105                                 }
3106                         }
3107
3108                         eclass = ExprClass.Value;
3109                         return this;
3110                 }
3111
3112                 public override void Emit (EmitContext ec)
3113                 {
3114                         ILGenerator ig = ec.ig;
3115                         Label false_target = ig.DefineLabel ();
3116                         Label end_target = ig.DefineLabel ();
3117
3118                         expr.Emit (ec);
3119                         ig.Emit (OpCodes.Brfalse, false_target);
3120                         trueExpr.Emit (ec);
3121                         ig.Emit (OpCodes.Br, end_target);
3122                         ig.MarkLabel (false_target);
3123                         falseExpr.Emit (ec);
3124                         ig.MarkLabel (end_target);
3125                 }
3126         }
3127
3128         //
3129         // SimpleName expressions are initially formed of a single
3130         // word and it only happens at the beginning of the expression.
3131         //
3132         // The expression will try to be bound to a Field, a Method
3133         // group or a Property.  If those fail we pass the name to our
3134         // caller and the SimpleName is compounded to perform a type
3135         // lookup.  The idea behind this process is that we want to avoid
3136         // creating a namespace map from the assemblies, as that requires
3137         // the GetExportedTypes function to be called and a hashtable to
3138         // be constructed which reduces startup time.  If later we find
3139         // that this is slower, we should create a `NamespaceExpr' expression
3140         // that fully participates in the resolution process. 
3141         //
3142         // For example `System.Console.WriteLine' is decomposed into
3143         // MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3144         //
3145         // The first SimpleName wont produce a match on its own, so it will
3146         // be turned into:
3147         // MemberAccess (SimpleName ("System.Console"), "WriteLine").
3148         //
3149         // System.Console will produce a TypeExpr match.
3150         //
3151         // The downside of this is that we might be hitting `LookupType' too many
3152         // times with this scheme.
3153         //
3154         public class SimpleName : Expression {
3155                 public readonly string Name;
3156                 public readonly Location Location;
3157                 
3158                 public SimpleName (string name, Location l)
3159                 {
3160                         Name = name;
3161                         Location = l;
3162                 }
3163
3164                 public static void Error120 (Location l, string name)
3165                 {
3166                         Report.Error (
3167                                 120, l,
3168                                 "An object reference is required " +
3169                                 "for the non-static field `"+name+"'");
3170                 }
3171                 
3172                 //
3173                 // Checks whether we are trying to access an instance
3174                 // property, method or field from a static body.
3175                 //
3176                 Expression MemberStaticCheck (Expression e)
3177                 {
3178                         if (e is FieldExpr){
3179                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
3180                                 
3181                                 if (!fi.IsStatic){
3182                                         Error120 (Location, Name);
3183                                         return null;
3184                                 }
3185                         } else if (e is MethodGroupExpr){
3186                                 MethodGroupExpr mg = (MethodGroupExpr) e;
3187
3188                                 if (!mg.RemoveInstanceMethods ()){
3189                                         Error120 (Location, mg.Methods [0].Name);
3190                                         return null;
3191                                 }
3192                                 return e;
3193                         } else if (e is PropertyExpr){
3194                                 if (!((PropertyExpr) e).IsStatic){
3195                                         Error120 (Location, Name);
3196                                         return null;
3197                                 }
3198                         }
3199
3200                         return e;
3201                 }
3202                 
3203                 //
3204                 // 7.5.2: Simple Names. 
3205                 //
3206                 // Local Variables and Parameters are handled at
3207                 // parse time, so they never occur as SimpleNames.
3208                 //
3209                 public override Expression DoResolve (EmitContext ec)
3210                 {
3211                         Expression e;
3212
3213                         //
3214                         // Stage 1: Performed by the parser (binding to local or parameters).
3215                         //
3216
3217                         //
3218                         // Stage 2: Lookup members
3219                         //
3220                         e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
3221                         if (e == null){
3222                                 //
3223                                 // Stage 3: Lookup symbol in the various namespaces. 
3224                                 // 
3225                                 Type t;
3226                                 
3227                                 if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
3228                                         return new TypeExpr (t);
3229
3230                                 //
3231                                 // Stage 3 part b: Lookup up if we are an alias to a type
3232                                 // or a namespace.
3233                                 //
3234                                 // Since we are cheating: we only do the Alias lookup for
3235                                 // namespaces if the name does not include any dots in it
3236                                 //
3237                                 
3238                                 // IMPLEMENT ME.  Read mcs/mcs/TODO for ideas, or rewrite
3239                                 // using NamespaceExprs (dunno how that fixes the alias
3240                                 // per-file though).
3241                                 
3242                                 // No match, maybe our parent can compose us
3243                                 // into something meaningful.
3244                                 //
3245                                 return this;
3246                         }
3247
3248                         // Step 2, continues here.
3249                         if (e is TypeExpr)
3250                                 return e;
3251
3252                         if (e is FieldExpr){
3253                                 FieldExpr fe = (FieldExpr) e;
3254                                 
3255                                 if (!fe.FieldInfo.IsStatic)
3256                                         fe.InstanceExpression = new This (Location.Null);
3257                         }                               
3258
3259                         if (ec.IsStatic)
3260                                 return MemberStaticCheck (e);
3261                         else
3262                                 return e;
3263                 }
3264
3265                 public override void Emit (EmitContext ec)
3266                 {
3267                         //
3268                         // If this is ever reached, then we failed to
3269                         // find the name as a namespace
3270                         //
3271
3272                         Error (103, Location, "The name `" + Name +
3273                                "' does not exist in the class `" +
3274                                ec.TypeContainer.Name + "'");
3275                 }
3276         }
3277         
3278         public class LocalVariableReference : Expression, IStackStore, IMemoryLocation {
3279                 public readonly string Name;
3280                 public readonly Block Block;
3281
3282                 VariableInfo variable_info;
3283                 
3284                 public LocalVariableReference (Block block, string name)
3285                 {
3286                         Block = block;
3287                         Name = name;
3288                         eclass = ExprClass.Variable;
3289                 }
3290
3291                 public VariableInfo VariableInfo {
3292                         get {
3293                                 if (variable_info == null)
3294                                         variable_info = Block.GetVariableInfo (Name);
3295                                 return variable_info;
3296                         }
3297                 }
3298                 
3299                 public override Expression DoResolve (EmitContext ec)
3300                 {
3301                         VariableInfo vi = VariableInfo;
3302
3303                         type = vi.VariableType;
3304                         return this;
3305                 }
3306
3307                 public override void Emit (EmitContext ec)
3308                 {
3309                         VariableInfo vi = VariableInfo;
3310                         ILGenerator ig = ec.ig;
3311                         int idx = vi.Idx;
3312
3313                         vi.Used = true;
3314                         
3315                         switch (idx){
3316                         case 0:
3317                                 ig.Emit (OpCodes.Ldloc_0);
3318                                 break;
3319                                 
3320                         case 1:
3321                                 ig.Emit (OpCodes.Ldloc_1);
3322                                 break;
3323
3324                         case 2:
3325                                 ig.Emit (OpCodes.Ldloc_2);
3326                                 break;
3327
3328                         case 3:
3329                                 ig.Emit (OpCodes.Ldloc_3);
3330                                 break;
3331
3332                         default:
3333                                 if (idx <= 255)
3334                                         ig.Emit (OpCodes.Ldloc_S, (byte) idx);
3335                                 else
3336                                         ig.Emit (OpCodes.Ldloc, idx);
3337                                 break;
3338                         }
3339                 }
3340
3341                 public static void Store (ILGenerator ig, int idx)
3342                 {
3343                         switch (idx){
3344                         case 0:
3345                                 ig.Emit (OpCodes.Stloc_0);
3346                                 break;
3347                                 
3348                         case 1:
3349                                 ig.Emit (OpCodes.Stloc_1);
3350                                 break;
3351                                 
3352                         case 2:
3353                                 ig.Emit (OpCodes.Stloc_2);
3354                                 break;
3355                                 
3356                         case 3:
3357                                 ig.Emit (OpCodes.Stloc_3);
3358                                 break;
3359                                 
3360                         default:
3361                                 if (idx <= 255)
3362                                         ig.Emit (OpCodes.Stloc_S, (byte) idx);
3363                                 else
3364                                         ig.Emit (OpCodes.Stloc, idx);
3365                                 break;
3366                         }
3367                 }
3368                 
3369                 public void Store (EmitContext ec)
3370                 {
3371                         ILGenerator ig = ec.ig;
3372                         VariableInfo vi = VariableInfo;
3373
3374                         vi.Assigned = true;
3375
3376                         // Funny seems the above generates optimal code for us, but
3377                         // seems to take too long to generate what we need.
3378                         // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
3379
3380                         Store (ig, vi.Idx);
3381                 }
3382
3383                 public void AddressOf (EmitContext ec)
3384                 {
3385                         VariableInfo vi = VariableInfo;
3386                         int idx = vi.Idx;
3387
3388                         vi.Used = true;
3389                         vi.Assigned = true;
3390                         
3391                         if (idx <= 255)
3392                                 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
3393                         else
3394                                 ec.ig.Emit (OpCodes.Ldloca, idx);
3395                 }
3396         }
3397
3398         public class ParameterReference : Expression, IStackStore, IMemoryLocation {
3399                 public readonly Parameters Pars;
3400                 public readonly String Name;
3401                 public readonly int Idx;
3402                 int arg_idx;
3403                 
3404                 public ParameterReference (Parameters pars, int idx, string name)
3405                 {
3406                         Pars = pars;
3407                         Idx  = idx;
3408                         Name = name;
3409                         eclass = ExprClass.Variable;
3410                 }
3411
3412                 public override Expression DoResolve (EmitContext ec)
3413                 {
3414                         Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
3415
3416                         type = types [Idx];
3417
3418                         arg_idx = Idx;
3419                         if (!ec.IsStatic)
3420                                 arg_idx++;
3421                         
3422                         return this;
3423                 }
3424
3425                 public override void Emit (EmitContext ec)
3426                 {
3427                         if (arg_idx <= 255)
3428                                 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3429                         else
3430                                 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3431                 }
3432
3433                 public void Store (EmitContext ec)
3434                 {
3435                         if (arg_idx <= 255)
3436                                 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3437                         else
3438                                 ec.ig.Emit (OpCodes.Starg, arg_idx);
3439                         
3440                 }
3441
3442                 public void AddressOf (EmitContext ec)
3443                 {
3444                         if (arg_idx <= 255)
3445                                 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3446                         else
3447                                 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3448                 }
3449         }
3450         
3451         // <summary>
3452         //   Used for arguments to New(), Invocation()
3453         // </summary>
3454         public class Argument {
3455                 public enum AType {
3456                         Expression,
3457                         Ref,
3458                         Out
3459                 };
3460
3461                 public readonly AType Type;
3462                 public Expression expr;
3463
3464                 public Argument (Expression expr, AType type)
3465                 {
3466                         this.expr = expr;
3467                         this.Type = type;
3468                 }
3469
3470                 public Expression Expr {
3471                         get {
3472                                 return expr;
3473                         }
3474
3475                         set {
3476                                 expr = value;
3477                         }
3478                 }
3479
3480                 public bool Resolve (EmitContext ec)
3481                 {
3482                         expr = expr.Resolve (ec);
3483
3484                         return expr != null;
3485                 }
3486
3487                 public void Emit (EmitContext ec)
3488                 {
3489                         expr.Emit (ec);
3490                 }
3491         }
3492
3493         // <summary>
3494         //   Invocation of methods or delegates.
3495         // </summary>
3496         public class Invocation : ExpressionStatement {
3497                 public readonly ArrayList Arguments;
3498                 public readonly Location Location;
3499
3500                 Expression expr;
3501                 MethodBase method = null;
3502                         
3503                 static Hashtable method_parameter_cache;
3504
3505                 static Invocation ()
3506                 {
3507                         method_parameter_cache = new Hashtable ();
3508                 }
3509                         
3510                 //
3511                 // arguments is an ArrayList, but we do not want to typecast,
3512                 // as it might be null.
3513                 //
3514                 // FIXME: only allow expr to be a method invocation or a
3515                 // delegate invocation (7.5.5)
3516                 //
3517                 public Invocation (Expression expr, ArrayList arguments, Location l)
3518                 {
3519                         this.expr = expr;
3520                         Arguments = arguments;
3521                         Location = l;
3522                 }
3523
3524                 public Expression Expr {
3525                         get {
3526                                 return expr;
3527                         }
3528                 }
3529
3530                 // <summary>
3531                 //   Returns the Parameters (a ParameterData interface) for the
3532                 //   Method `mb'
3533                 // </summary>
3534                 public static ParameterData GetParameterData (MethodBase mb)
3535                 {
3536                         object pd = method_parameter_cache [mb];
3537                         object ip;
3538                         
3539                         if (pd != null)
3540                                 return (ParameterData) pd;
3541
3542                         
3543                         ip = TypeContainer.LookupParametersByBuilder (mb);
3544                         if (ip != null){
3545                                 method_parameter_cache [mb] = ip;
3546
3547                                 return (ParameterData) ip;
3548                         } else {
3549                                 ParameterInfo [] pi = mb.GetParameters ();
3550                                 ReflectionParameters rp = new ReflectionParameters (pi);
3551                                 method_parameter_cache [mb] = rp;
3552
3553                                 return (ParameterData) rp;
3554                         }
3555                 }
3556
3557                 // <summary>
3558                 //   Tells whether a user defined conversion from Type `from' to
3559                 //   Type `to' exists.
3560                 //
3561                 //   FIXME: we could implement a cache here. 
3562                 // </summary>
3563                 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
3564                 {
3565                         // Locate user-defined implicit operators
3566
3567                         Expression mg;
3568                         
3569                         mg = MemberLookup (ec, to, "op_Implicit", false, loc);
3570
3571                         if (mg != null) {
3572                                 MethodGroupExpr me = (MethodGroupExpr) mg;
3573                                 
3574                                 for (int i = me.Methods.Length; i > 0;) {
3575                                         i--;
3576                                         MethodBase mb = me.Methods [i];
3577                                         ParameterData pd = GetParameterData (mb);
3578                                         
3579                                         if (from == pd.ParameterType (0))
3580                                                 return true;
3581                                 }
3582                         }
3583
3584                         mg = MemberLookup (ec, from, "op_Implicit", false, loc);
3585
3586                         if (mg != null) {
3587                                 MethodGroupExpr me = (MethodGroupExpr) mg;
3588
3589                                 for (int i = me.Methods.Length; i > 0;) {
3590                                         i--;
3591                                         MethodBase mb = me.Methods [i];
3592                                         MethodInfo mi = (MethodInfo) mb;
3593                                         
3594                                         if (mi.ReturnType == to)
3595                                                 return true;
3596                                 }
3597                         }
3598                         
3599                         return false;
3600                 }
3601                 
3602                 // <summary>
3603                 //  Determines "better conversion" as specified in 7.4.2.3
3604                 //  Returns : 1 if a->p is better
3605                 //            0 if a->q or neither is better 
3606                 // </summary>
3607                 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
3608                                              Location loc)
3609                 {
3610                         Type argument_type = a.Expr.Type;
3611                         Expression argument_expr = a.Expr;
3612
3613                         if (argument_type == null)
3614                                 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
3615
3616                         if (p == q)
3617                                 return 0;
3618                         
3619                         if (argument_type == p)
3620                                 return 1;
3621
3622                         if (argument_type == q)
3623                                 return 0;
3624
3625                         //
3626                         // Now probe whether an implicit constant expression conversion
3627                         // can be used.
3628                         //
3629                         // An implicit constant expression conversion permits the following
3630                         // conversions:
3631                         //
3632                         //    * A constant-expression of type `int' can be converted to type
3633                         //      sbyte, byute, short, ushort, uint, ulong provided the value of
3634                         //      of the expression is withing the range of the destination type.
3635                         //
3636                         //    * A constant-expression of type long can be converted to type
3637                         //      ulong, provided the value of the constant expression is not negative
3638                         //
3639                         // FIXME: Note that this assumes that constant folding has
3640                         // taken place.  We dont do constant folding yet.
3641                         //
3642
3643                         if (argument_expr is IntLiteral){
3644                                 IntLiteral ei = (IntLiteral) argument_expr;
3645                                 int value = ei.Value;
3646                                 
3647                                 if (p == TypeManager.sbyte_type){
3648                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
3649                                                 return 1;
3650                                 } else if (p == TypeManager.byte_type){
3651                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
3652                                                 return 1;
3653                                 } else if (p == TypeManager.short_type){
3654                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
3655                                                 return 1;
3656                                 } else if (p == TypeManager.ushort_type){
3657                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
3658                                                 return 1;
3659                                 } else if (p == TypeManager.uint32_type){
3660                                         //
3661                                         // we can optimize this case: a positive int32
3662                                         // always fits on a uint32
3663                                         //
3664                                         if (value >= 0)
3665                                                 return 1;
3666                                 } else if (p == TypeManager.uint64_type){
3667                                         //
3668                                         // we can optimize this case: a positive int32
3669                                         // always fits on a uint64
3670                                         //
3671                                         if (value >= 0)
3672                                                 return 1;
3673                                 }
3674                         } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
3675                                 LongLiteral ll = (LongLiteral) argument_expr;
3676                                 
3677                                 if (p == TypeManager.uint64_type){
3678                                         if (ll.Value > 0)
3679                                                 return 1;
3680                                 }
3681                         }
3682
3683                         if (q == null) {
3684
3685                                 Expression tmp;
3686
3687                                 if (use_standard)
3688                                         tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
3689                                 else
3690                                         tmp = ConvertImplicit (ec, argument_expr, p, loc);
3691
3692                                 if (tmp != null)
3693                                         return 1;
3694                                 else
3695                                         return 0;
3696
3697                         }
3698
3699                         if (ConversionExists (ec, p, q, loc) == true &&
3700                             ConversionExists (ec, q, p, loc) == false)
3701                                 return 1;
3702
3703                         if (p == TypeManager.sbyte_type)
3704                                 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
3705                                     q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3706                                         return 1;
3707
3708                         if (p == TypeManager.short_type)
3709                                 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
3710                                     q == TypeManager.uint64_type)
3711                                         return 1;
3712
3713                         if (p == TypeManager.int32_type)
3714                                 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
3715                                         return 1;
3716
3717                         if (p == TypeManager.int64_type)
3718                                 if (q == TypeManager.uint64_type)
3719                                         return 1;
3720
3721                         return 0;
3722                 }
3723                 
3724                 // <summary>
3725                 //  Determines "Better function" and returns an integer indicating :
3726                 //  0 if candidate ain't better
3727                 //  1 if candidate is better than the current best match
3728                 // </summary>
3729                 static int BetterFunction (EmitContext ec, ArrayList args,
3730                                            MethodBase candidate, MethodBase best,
3731                                            bool use_standard, Location loc)
3732                 {
3733                         ParameterData candidate_pd = GetParameterData (candidate);
3734                         ParameterData best_pd;
3735                         int argument_count;
3736
3737                         if (args == null)
3738                                 argument_count = 0;
3739                         else
3740                                 argument_count = args.Count;
3741
3742                         if (candidate_pd.Count == 0 && argument_count == 0)
3743                                 return 1;
3744
3745                         if (best == null) {
3746                                 if (candidate_pd.Count == argument_count) {
3747                                         int x = 0;
3748                                         for (int j = argument_count; j > 0;) {
3749                                                 j--;
3750                                                 
3751                                                 Argument a = (Argument) args [j];
3752                                                 
3753                                                 x = BetterConversion (
3754                                                         ec, a, candidate_pd.ParameterType (j), null,
3755                                                         use_standard, loc);
3756                                                 
3757                                                 if (x <= 0)
3758                                                         break;
3759                                         }
3760                                         
3761                                         if (x > 0)
3762                                                 return 1;
3763                                         else
3764                                                 return 0;
3765                                         
3766                                 } else
3767                                         return 0;
3768                         }
3769
3770                         best_pd = GetParameterData (best);
3771
3772                         if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
3773                                 int rating1 = 0, rating2 = 0;
3774                                 
3775                                 for (int j = argument_count; j > 0;) {
3776                                         j--;
3777                                         int x, y;
3778                                         
3779                                         Argument a = (Argument) args [j];
3780
3781                                         x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
3782                                                               best_pd.ParameterType (j), use_standard, loc);
3783                                         y = BetterConversion (ec, a, best_pd.ParameterType (j),
3784                                                               candidate_pd.ParameterType (j), use_standard,
3785                                                               loc);
3786                                         
3787                                         rating1 += x;
3788                                         rating2 += y;
3789                                 }
3790
3791                                 if (rating1 > rating2)
3792                                         return 1;
3793                                 else
3794                                         return 0;
3795                         } else
3796                                 return 0;
3797                         
3798                 }
3799
3800                 public static string FullMethodDesc (MethodBase mb)
3801                 {
3802                         StringBuilder sb = new StringBuilder (mb.Name);
3803                         ParameterData pd = GetParameterData (mb);
3804
3805                         int count = pd.Count;
3806                         sb.Append (" (");
3807                         
3808                         for (int i = count; i > 0; ) {
3809                                 i--;
3810                                 
3811                                 sb.Append (TypeManager.CSharpName (pd.ParameterType (count - i - 1)));
3812                                 if (i != 0)
3813                                         sb.Append (", ");
3814                         }
3815                         
3816                         sb.Append (")");
3817                         return sb.ToString ();
3818                 }
3819
3820                 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
3821                 {
3822                         MemberInfo [] miset;
3823                         MethodGroupExpr union;
3824                         
3825                         if (mg1 != null && mg2 != null) {
3826                                 
3827                                 MethodGroupExpr left_set = null, right_set = null;
3828                                 int length1 = 0, length2 = 0;
3829                                 
3830                                 left_set = (MethodGroupExpr) mg1;
3831                                 length1 = left_set.Methods.Length;
3832                                 
3833                                 right_set = (MethodGroupExpr) mg2;
3834                                 length2 = right_set.Methods.Length;
3835
3836                                 ArrayList common = new ArrayList ();
3837                                 
3838                                 for (int i = 0; i < left_set.Methods.Length; i++) {
3839                                         for (int j = 0; j < right_set.Methods.Length; j++) {
3840                                                 if (left_set.Methods [i] == right_set.Methods [j]) 
3841                                                         common.Add (left_set.Methods [i]);
3842                                         }
3843                                 }
3844                                 
3845                                 miset = new MemberInfo [length1 + length2 - common.Count];
3846
3847                                 left_set.Methods.CopyTo (miset, 0);
3848
3849                                 int k = 0;
3850                                 
3851                                 for (int j = 0; j < right_set.Methods.Length; j++)
3852                                         if (!common.Contains (right_set.Methods [j]))
3853                                                 miset [length1 + k++] = right_set.Methods [j];
3854                                 
3855                                 union = new MethodGroupExpr (miset);
3856
3857                                 return union;
3858
3859                         } else if (mg1 == null && mg2 != null) {
3860                                 
3861                                 MethodGroupExpr me = (MethodGroupExpr) mg2; 
3862                                 
3863                                 miset = new MemberInfo [me.Methods.Length];
3864                                 me.Methods.CopyTo (miset, 0);
3865
3866                                 union = new MethodGroupExpr (miset);
3867                                 
3868                                 return union;
3869
3870                         } else if (mg2 == null && mg1 != null) {
3871                                 
3872                                 MethodGroupExpr me = (MethodGroupExpr) mg1; 
3873                                 
3874                                 miset = new MemberInfo [me.Methods.Length];
3875                                 me.Methods.CopyTo (miset, 0);
3876
3877                                 union = new MethodGroupExpr (miset);
3878                                 
3879                                 return union;
3880                         }
3881                         
3882                         return null;
3883                 }
3884
3885                 // <summary>
3886                 //   Find the Applicable Function Members (7.4.2.1)
3887                 //
3888                 //   me: Method Group expression with the members to select.
3889                 //       it might contain constructors or methods (or anything
3890                 //       that maps to a method).
3891                 //
3892                 //   Arguments: ArrayList containing resolved Argument objects.
3893                 //
3894                 //   loc: The location if we want an error to be reported, or a Null
3895                 //        location for "probing" purposes.
3896                 //
3897                 //   inside_user_defined: controls whether OverloadResolve should use the 
3898                 //   ConvertImplicit or ConvertImplicitStandard during overload resolution.
3899                 //
3900                 //   Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
3901                 //            that is the best match of me on Arguments.
3902                 //
3903                 // </summary>
3904                 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
3905                                                           ArrayList Arguments, Location loc,
3906                                                           bool use_standard)
3907                 {
3908                         ArrayList afm = new ArrayList ();
3909                         int best_match_idx = -1;
3910                         MethodBase method = null;
3911                         int argument_count;
3912                         
3913                         for (int i = me.Methods.Length; i > 0; ){
3914                                 i--;
3915                                 MethodBase candidate  = me.Methods [i];
3916                                 int x;
3917
3918                                 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
3919                                 
3920                                 if (x == 0)
3921                                         continue;
3922                                 else {
3923                                         best_match_idx = i;
3924                                         method = me.Methods [best_match_idx];
3925                                 }
3926                         }
3927
3928                         if (Arguments == null)
3929                                 argument_count = 0;
3930                         else
3931                                 argument_count = Arguments.Count;
3932                         
3933                         ParameterData pd;
3934                         
3935                         // Now we see if we can at least find a method with the same number of arguments
3936                         // and then try doing implicit conversion on the arguments
3937                         if (best_match_idx == -1) {
3938                                 
3939                                 for (int i = me.Methods.Length; i > 0;) {
3940                                         i--;
3941                                         MethodBase mb = me.Methods [i];
3942                                         pd = GetParameterData (mb);
3943                                         
3944                                         if (pd.Count == argument_count) {
3945                                                 best_match_idx = i;
3946                                                 method = me.Methods [best_match_idx];
3947                                                 break;
3948                                         } else
3949                                                 continue;
3950                                 }
3951
3952                         }
3953
3954                         if (method == null)
3955                                 return null;
3956
3957                         // And now convert implicitly, each argument to the required type
3958                         
3959                         pd = GetParameterData (method);
3960
3961                         for (int j = argument_count; j > 0;) {
3962                                 j--;
3963                                 Argument a = (Argument) Arguments [j];
3964                                 Expression a_expr = a.Expr;
3965                                 Type parameter_type = pd.ParameterType (j);
3966                                 
3967                                 if (a_expr.Type != parameter_type){
3968                                         Expression conv;
3969
3970                                         if (use_standard)
3971                                                 conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
3972                                                                                 Location.Null);
3973                                         else
3974                                                 conv = ConvertImplicit (ec, a_expr, parameter_type,
3975                                                                         Location.Null);
3976
3977                                         if (conv == null){
3978                                                 if (!Location.IsNull (loc)) {
3979                                                         Error (1502, loc,
3980                                                                "The best overloaded match for method '" + FullMethodDesc (method) +
3981                                                                "' has some invalid arguments");
3982                                                         Error (1503, loc,
3983                                                                "Argument " + (j+1) +
3984                                                                ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
3985                                                                + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
3986                                                 }
3987                                                 return null;
3988                                         }
3989                                         //
3990                                         // Update the argument with the implicit conversion
3991                                         //
3992                                         if (a_expr != conv)
3993                                                 a.Expr = conv;
3994                                 }
3995                         }
3996                         
3997                         return method;
3998                 }
3999
4000                 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4001                                                           ArrayList Arguments, Location loc)
4002                 {
4003                         return OverloadResolve (ec, me, Arguments, loc, false);
4004                 }
4005                         
4006                 public override Expression DoResolve (EmitContext ec)
4007                 {
4008                         bool IsDelegate = false;
4009                         //
4010                         // First, resolve the expression that is used to
4011                         // trigger the invocation
4012                         //
4013                         expr = expr.Resolve (ec);
4014                         if (expr == null)
4015                                 return null;
4016
4017                         //
4018                         // If SimpleNames make it here, it is an error
4019                         //
4020                         if (expr is SimpleName){
4021                                 SimpleName s = (SimpleName) expr;
4022                                 
4023                                 Report.Error (246, s.Location,
4024                                               "The type or name `" + s.Name + "' could not be found, " +
4025                                               "maybe you are missing a using directive");
4026                                 return null;
4027                         }
4028                         
4029                         if (!(expr is MethodGroupExpr)) {
4030                                 Type expr_type = expr.Type;
4031
4032                                 if (expr_type == null){
4033                                         IsDelegate = TypeManager.IsDelegateType (expr_type);
4034                                         if (IsDelegate)
4035                                                 return (new DelegateInvocation (
4036                                                         this.expr, Arguments, Location)).Resolve (ec);
4037                                 }
4038                         }
4039                         
4040                         if (!(this.expr is MethodGroupExpr)){
4041                                 report118 (Location, this.expr, "method group");
4042                                 return null;
4043                         }
4044
4045                         //
4046                         // Next, evaluate all the expressions in the argument list
4047                         //
4048                         if (Arguments != null){
4049                                 for (int i = Arguments.Count; i > 0;){
4050                                         --i;
4051                                         Argument a = (Argument) Arguments [i];
4052
4053                                         if (!a.Resolve (ec))
4054                                                 return null;
4055                                 }
4056                         }
4057
4058                         method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
4059                                                   Location);
4060
4061                         if (method == null){
4062                                 Error (-6, Location,
4063                                        "Could not find any applicable function for this argument list");
4064                                 return null;
4065                         }
4066
4067                         if (method is MethodInfo)
4068                                 type = ((MethodInfo)method).ReturnType;
4069
4070                         eclass = ExprClass.Value;
4071                         return this;
4072                 }
4073
4074                 public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
4075                 {
4076                         int top;
4077
4078                         if (Arguments != null)
4079                                 top = Arguments.Count;
4080                         else
4081                                 top = 0;
4082
4083                         for (int i = 0; i < top; i++){
4084                                 Argument a = (Argument) Arguments [i];
4085
4086                                 a.Emit (ec);
4087                         }
4088                 }
4089
4090                 public static void EmitCall (EmitContext ec,
4091                                              bool is_static, Expression instance_expr,
4092                                              MethodBase method, ArrayList Arguments)
4093                 {
4094                         ILGenerator ig = ec.ig;
4095                         bool struct_call = false;
4096                                 
4097                         if (!is_static){
4098                                 //
4099                                 // If this is ourselves, push "this"
4100                                 //
4101                                 if (instance_expr == null){
4102                                         ig.Emit (OpCodes.Ldarg_0);
4103                                 } else {
4104                                         //
4105                                         // Push the instance expression
4106                                         //
4107                                         if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
4108
4109                                                 struct_call = true;
4110
4111                                                 //
4112                                                 // If the expression implements IMemoryLocation, then
4113                                                 // we can optimize and use AddressOf on the
4114                                                 // return.
4115                                                 //
4116                                                 // If not we have to use some temporary storage for
4117                                                 // it.
4118                                                 if (instance_expr is IMemoryLocation)
4119                                                         ((IMemoryLocation) instance_expr).AddressOf (ec);
4120                                                 else {
4121                                                         Type t = instance_expr.Type;
4122                                                         
4123                                                         instance_expr.Emit (ec);
4124                                                         LocalBuilder temp = ec.GetTemporaryStorage (t);
4125                                                         ig.Emit (OpCodes.Stloc, temp);
4126                                                         ig.Emit (OpCodes.Ldloca, temp);
4127                                                 }
4128                                         } else 
4129                                                 instance_expr.Emit (ec);
4130                                 }
4131                         }
4132
4133                         if (Arguments != null)
4134                                 EmitArguments (ec, method, Arguments);
4135
4136                         if (is_static || struct_call){
4137                                 if (method is MethodInfo)
4138                                         ig.Emit (OpCodes.Call, (MethodInfo) method);
4139                                 else
4140                                         ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4141                         } else {
4142                                 if (method is MethodInfo)
4143                                         ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
4144                                 else
4145                                         ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
4146                         }
4147                 }
4148                 
4149                 public override void Emit (EmitContext ec)
4150                 {
4151                         MethodGroupExpr mg = (MethodGroupExpr) this.expr;
4152                         EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
4153                 }
4154                 
4155                 public override void EmitStatement (EmitContext ec)
4156                 {
4157                         Emit (ec);
4158
4159                         // 
4160                         // Pop the return value if there is one
4161                         //
4162                         if (method is MethodInfo){
4163                                 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
4164                                         ec.ig.Emit (OpCodes.Pop);
4165                         }
4166                 }
4167         }
4168
4169         public class New : ExpressionStatement {
4170
4171                 public enum NType {
4172                         Object,
4173                         Array
4174                 };
4175
4176                 public readonly NType     NewType;
4177                 public readonly ArrayList Arguments;
4178                 public readonly string    RequestedType;
4179
4180                 // These are for the case when we have an array
4181                 public readonly string    Rank;
4182                 public readonly ArrayList Initializers;
4183
4184                 Location Location;
4185                 MethodBase method = null;
4186
4187                 //
4188                 // If set, the new expression is for a value_target, and
4189                 // we will not leave anything on the stack.
4190                 //
4191                 Expression value_target;
4192                 
4193                 public New (string requested_type, ArrayList arguments, Location loc)
4194                 {
4195                         RequestedType = requested_type;
4196                         Arguments = arguments;
4197                         NewType = NType.Object;
4198                         Location = loc;
4199                 }
4200
4201                 public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
4202                 {
4203                         RequestedType = requested_type;
4204                         Rank          = rank;
4205                         Initializers  = initializers;
4206                         NewType       = NType.Array;
4207                         Location      = loc;
4208
4209                         Arguments = new ArrayList ();
4210
4211                         foreach (Expression e in exprs)
4212                                 Arguments.Add (new Argument (e, Argument.AType.Expression));
4213                         
4214                 }
4215
4216                 public static string FormLookupType (string base_type, int idx_count, string rank)
4217                 {
4218                         StringBuilder sb = new StringBuilder (base_type);
4219
4220                         sb.Append (rank);
4221
4222                         sb.Append ("[");
4223                         for (int i = 1; i < idx_count; i++)
4224                                 sb.Append (",");
4225                         sb.Append ("]");
4226                         
4227                         return sb.ToString ();
4228                 }
4229                 
4230                 public Expression ValueTypeVariable {
4231                         get {
4232                                 return value_target;
4233                         }
4234
4235                         set {
4236                                 value_target = value;
4237                         }
4238                 }
4239                 
4240                 public override Expression DoResolve (EmitContext ec)
4241                 {
4242                         if (NewType == NType.Object) {
4243
4244                                 type = ec.TypeContainer.LookupType (RequestedType, false);
4245                                 
4246                                 if (type == null)
4247                                         return null;
4248
4249                                 bool IsDelegate = TypeManager.IsDelegateType (type);
4250                                 
4251                                 if (IsDelegate)
4252                                         return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
4253                                 
4254                                 Expression ml;
4255                                 
4256                                 ml = MemberLookup (ec, type, ".ctor", false,
4257                                                    MemberTypes.Constructor, AllBindingsFlags, Location);
4258                                 
4259                                 bool is_struct = false;
4260                                 is_struct = type.IsSubclassOf (TypeManager.value_type);
4261                                 
4262                                 if (! (ml is MethodGroupExpr)){
4263                                         if (!is_struct){
4264                                                 report118 (Location, ml, "method group");
4265                                                 return null;
4266                                         }
4267                                 }
4268                                 
4269                                 if (ml != null) {
4270                                         if (Arguments != null){
4271                                                 for (int i = Arguments.Count; i > 0;){
4272                                                         --i;
4273                                                         Argument a = (Argument) Arguments [i];
4274                                                         
4275                                                         if (!a.Resolve (ec))
4276                                                                 return null;
4277                                                 }
4278                                         }
4279                                         
4280                                         method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
4281                                                                              Arguments, Location);
4282                                 }
4283                                 
4284                                 
4285                                 if (method == null && !is_struct) {
4286                                         Error (-6, Location,
4287                                                "New invocation: Can not find a constructor for " +
4288                                                "this argument list");
4289                                         return null;
4290                                 }
4291                                 
4292                                 eclass = ExprClass.Value;
4293                                 return this;
4294                                 
4295                         }
4296                         
4297                         if (NewType == NType.Array) {
4298                                 throw new Exception ("Finish array creation");
4299                         }
4300                         
4301                         return null;
4302                 }
4303                 
4304                 //
4305                 // This DoEmit can be invoked in two contexts:
4306                 //    * As a mechanism that will leave a value on the stack (new object)
4307                 //    * As one that wont (init struct)
4308                 //
4309                 // You can control whether a value is required on the stack by passing
4310                 // need_value_on_stack.  The code *might* leave a value on the stack
4311                 // so it must be popped manually
4312                 //
4313                 // Returns whether a value is left on the stack
4314                 //
4315                 bool DoEmit (EmitContext ec, bool need_value_on_stack)
4316                 {
4317                         if (method == null){
4318                                 IMemoryLocation ml = (IMemoryLocation) value_target;
4319
4320                                 ml.AddressOf (ec);
4321                         } else {
4322                                 Invocation.EmitArguments (ec, method, Arguments);
4323                                 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
4324                                 return true;
4325                         }
4326
4327                         //
4328                         // It must be a value type, sanity check
4329                         //
4330                         if (value_target != null){
4331                                 ec.ig.Emit (OpCodes.Initobj, type);
4332
4333                                 if (need_value_on_stack){
4334                                         value_target.Emit (ec);
4335                                         return true;
4336                                 }
4337                                 return false;
4338                         }
4339
4340                         throw new Exception ("No method and no value type");
4341                 }
4342
4343                 public override void Emit (EmitContext ec)
4344                 {
4345                         DoEmit (ec, true);
4346                 }
4347                 
4348                 public override void EmitStatement (EmitContext ec)
4349                 {
4350                         if (DoEmit (ec, false))
4351                                 ec.ig.Emit (OpCodes.Pop);
4352                 }
4353         }
4354
4355         //
4356         // Represents the `this' construct
4357         //
4358         public class This : Expression, IStackStore, IMemoryLocation {
4359                 Location loc;
4360                 
4361                 public This (Location loc)
4362                 {
4363                         this.loc = loc;
4364                 }
4365
4366                 public override Expression DoResolve (EmitContext ec)
4367                 {
4368                         eclass = ExprClass.Variable;
4369                         type = ec.TypeContainer.TypeBuilder;
4370
4371                         if (ec.IsStatic){
4372                                 Report.Error (26, loc,
4373                                               "Keyword this not valid in static code");
4374                                 return null;
4375                         }
4376                         
4377                         return this;
4378                 }
4379
4380                 public Expression DoResolveLValue (EmitContext ec)
4381                 {
4382                         DoResolve (ec);
4383                         
4384                         if (ec.TypeContainer is Class){
4385                                 Report.Error (1604, loc, "Cannot assign to `this'");
4386                                 return null;
4387                         }
4388
4389                         return this;
4390                 }
4391
4392                 public override void Emit (EmitContext ec)
4393                 {
4394                         ec.ig.Emit (OpCodes.Ldarg_0);
4395                 }
4396
4397                 public void Store (EmitContext ec)
4398                 {
4399                         ec.ig.Emit (OpCodes.Starg, 0);
4400                 }
4401
4402                 public void AddressOf (EmitContext ec)
4403                 {
4404                         ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
4405                 }
4406         }
4407
4408         // <summary>
4409         //   Implements the typeof operator
4410         // </summary>
4411         public class TypeOf : Expression {
4412                 public readonly string QueriedType;
4413                 Type typearg;
4414                 
4415                 public TypeOf (string queried_type)
4416                 {
4417                         QueriedType = queried_type;
4418                 }
4419
4420                 public override Expression DoResolve (EmitContext ec)
4421                 {
4422                         typearg = ec.TypeContainer.LookupType (QueriedType, false);
4423
4424                         if (typearg == null)
4425                                 return null;
4426
4427                         type = TypeManager.type_type;
4428                         eclass = ExprClass.Type;
4429                         return this;
4430                 }
4431
4432                 public override void Emit (EmitContext ec)
4433                 {
4434                         ec.ig.Emit (OpCodes.Ldtoken, typearg);
4435                         ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
4436                 }
4437         }
4438
4439         public class SizeOf : Expression {
4440                 public readonly string QueriedType;
4441                 
4442                 public SizeOf (string queried_type)
4443                 {
4444                         this.QueriedType = queried_type;
4445                 }
4446
4447                 public override Expression DoResolve (EmitContext ec)
4448                 {
4449                         // FIXME: Implement;
4450                         throw new Exception ("Unimplemented");
4451                         // return this;
4452                 }
4453
4454                 public override void Emit (EmitContext ec)
4455                 {
4456                         throw new Exception ("Implement me");
4457                 }
4458         }
4459
4460         public class MemberAccess : Expression {
4461                 public readonly string Identifier;
4462                 Expression expr;
4463                 Expression member_lookup;
4464                 Location loc;
4465                 
4466                 public MemberAccess (Expression expr, string id, Location l)
4467                 {
4468                         this.expr = expr;
4469                         Identifier = id;
4470                         loc = l;
4471                 }
4472
4473                 public Expression Expr {
4474                         get {
4475                                 return expr;
4476                         }
4477                 }
4478
4479                 void error176 (Location loc, string name)
4480                 {
4481                         Report.Error (176, loc, "Static member `" +
4482                                       name + "' cannot be accessed " +
4483                                       "with an instance reference, qualify with a " +
4484                                       "type name instead");
4485                 }
4486                 
4487                 public override Expression DoResolve (EmitContext ec)
4488                 {
4489                         expr = expr.Resolve (ec);
4490
4491                         if (expr == null)
4492                                 return null;
4493
4494                         if (expr is SimpleName){
4495                                 SimpleName child_expr = (SimpleName) expr;
4496                                 
4497                                 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
4498
4499                                 return expr.Resolve (ec);
4500                         }
4501                                         
4502                         member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
4503
4504                         //
4505                         // Method Groups
4506                         //
4507                         if (member_lookup is MethodGroupExpr){
4508                                 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
4509                                 
4510                                 //
4511                                 // Type.MethodGroup
4512                                 //
4513                                 if (expr is TypeExpr){
4514                                         if (!mg.RemoveInstanceMethods ()){
4515                                                 SimpleName.Error120 (loc, mg.Methods [0].Name); 
4516                                                 return null;
4517                                         }
4518
4519                                         return member_lookup;
4520                                 }
4521
4522                                 //
4523                                 // Instance.MethodGroup
4524                                 //
4525                                 if (!mg.RemoveStaticMethods ()){
4526                                         error176 (loc, mg.Methods [0].Name);
4527                                         return null;
4528                                 }
4529                                 
4530                                 mg.InstanceExpression = expr;
4531                                         
4532                                 return member_lookup;
4533                         }
4534
4535                         if (member_lookup is FieldExpr){
4536                                 FieldExpr fe = (FieldExpr) member_lookup;
4537
4538                                 if (expr is TypeExpr){
4539                                         if (!fe.FieldInfo.IsStatic){
4540                                                 error176 (loc, fe.FieldInfo.Name);
4541                                                 return null;
4542                                         }
4543                                         return member_lookup;
4544                                 } else {
4545                                         if (fe.FieldInfo.IsStatic){
4546                                                 error176 (loc, fe.FieldInfo.Name);
4547                                                 return null;
4548                                         }
4549                                         fe.InstanceExpression = expr;
4550
4551                                         return fe;
4552                                 }
4553                         }
4554
4555                         if (member_lookup is PropertyExpr){
4556                                 PropertyExpr pe = (PropertyExpr) member_lookup;
4557
4558                                 if (expr is TypeExpr){
4559                                         if (!pe.IsStatic){
4560                                                 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
4561                                                 return null;
4562                                         }
4563                                         return pe;
4564                                 } else {
4565                                         if (pe.IsStatic){
4566                                                 error176 (loc, pe.PropertyInfo.Name);
4567                                                 return null;
4568                                         }
4569                                         pe.InstanceExpression = expr;
4570
4571                                         return pe;
4572                                 }
4573                         }
4574                         
4575                         Console.WriteLine ("Support for " + member_lookup + " is not present yet");
4576                         Environment.Exit (0);
4577                         return null;
4578                 }
4579
4580                 public override void Emit (EmitContext ec)
4581                 {
4582                         throw new Exception ("Should not happen I think");
4583                 }
4584
4585         }
4586
4587         // <summary>
4588         //   Fully resolved expression that evaluates to a type
4589         // </summary>
4590         public class TypeExpr : Expression {
4591                 public TypeExpr (Type t)
4592                 {
4593                         Type = t;
4594                         eclass = ExprClass.Type;
4595                 }
4596
4597                 override public Expression DoResolve (EmitContext ec)
4598                 {
4599                         return this;
4600                 }
4601
4602                 override public void Emit (EmitContext ec)
4603                 {
4604                         throw new Exception ("Implement me");
4605                 }
4606         }
4607
4608         // <summary>
4609         //   MethodGroup Expression.
4610         //  
4611         //   This is a fully resolved expression that evaluates to a type
4612         // </summary>
4613         public class MethodGroupExpr : Expression {
4614                 public MethodBase [] Methods;
4615                 Expression instance_expression = null;
4616                 
4617                 public MethodGroupExpr (MemberInfo [] mi)
4618                 {
4619                         Methods = new MethodBase [mi.Length];
4620                         mi.CopyTo (Methods, 0);
4621                         eclass = ExprClass.MethodGroup;
4622                 }
4623
4624                 public MethodGroupExpr (ArrayList l)
4625                 {
4626                         Methods = new MethodBase [l.Count];
4627
4628                         l.CopyTo (Methods, 0);
4629                         eclass = ExprClass.MethodGroup;
4630                 }
4631                 
4632                 //
4633                 // `A method group may have associated an instance expression' 
4634                 // 
4635                 public Expression InstanceExpression {
4636                         get {
4637                                 return instance_expression;
4638                         }
4639
4640                         set {
4641                                 instance_expression = value;
4642                         }
4643                 }
4644                 
4645                 override public Expression DoResolve (EmitContext ec)
4646                 {
4647                         return this;
4648                 }
4649
4650                 override public void Emit (EmitContext ec)
4651                 {
4652                         throw new Exception ("This should never be reached");
4653                 }
4654
4655                 bool RemoveMethods (bool keep_static)
4656                 {
4657                         ArrayList smethods = new ArrayList ();
4658                         int top = Methods.Length;
4659                         int i;
4660                         
4661                         for (i = 0; i < top; i++){
4662                                 MethodBase mb = Methods [i];
4663
4664                                 if (mb.IsStatic == keep_static)
4665                                         smethods.Add (mb);
4666                         }
4667
4668                         if (smethods.Count == 0)
4669                                 return false;
4670
4671                         Methods = new MethodBase [smethods.Count];
4672                         smethods.CopyTo (Methods, 0);
4673
4674                         return true;
4675                 }
4676                 
4677                 // <summary>
4678                 //   Removes any instance methods from the MethodGroup, returns
4679                 //   false if the resulting set is empty.
4680                 // </summary>
4681                 public bool RemoveInstanceMethods ()
4682                 {
4683                         return RemoveMethods (true);
4684                 }
4685
4686                 // <summary>
4687                 //   Removes any static methods from the MethodGroup, returns
4688                 //   false if the resulting set is empty.
4689                 // </summary>
4690                 public bool RemoveStaticMethods ()
4691                 {
4692                         return RemoveMethods (false);
4693                 }
4694         }
4695
4696         // <summary>
4697         //   Fully resolved expression that evaluates to a Field
4698         // </summary>
4699         public class FieldExpr : Expression, IStackStore, IMemoryLocation {
4700                 public readonly FieldInfo FieldInfo;
4701                 public Expression InstanceExpression;
4702                 Location loc;
4703                 
4704                 public FieldExpr (FieldInfo fi, Location l)
4705                 {
4706                         FieldInfo = fi;
4707                         eclass = ExprClass.Variable;
4708                         type = fi.FieldType;
4709                         loc = l;
4710                 }
4711
4712                 override public Expression DoResolve (EmitContext ec)
4713                 {
4714                         if (!FieldInfo.IsStatic){
4715                                 if (InstanceExpression == null){
4716                                         throw new Exception ("non-static FieldExpr without instance var\n" +
4717                                                              "You have to assign the Instance variable\n" +
4718                                                              "Of the FieldExpr to set this\n");
4719                                 }
4720
4721                                 InstanceExpression = InstanceExpression.Resolve (ec);
4722                                 if (InstanceExpression == null)
4723                                         return null;
4724                                 
4725                         }
4726                         return this;
4727                 }
4728
4729                 public Expression DoResolveLValue (EmitContext ec)
4730                 {
4731                         if (!FieldInfo.IsInitOnly)
4732                                 return this;
4733
4734                         //
4735                         // InitOnly fields can only be assigned in constructors
4736                         //
4737
4738                         if (ec.IsConstructor)
4739                                 return this;
4740
4741                         Report.Error (191, loc,
4742                                       "Readonly field can not be assigned outside " +
4743                                       "of constructor or variable initializer");
4744                         
4745                         return null;
4746                 }
4747
4748                 override public void Emit (EmitContext ec)
4749                 {
4750                         ILGenerator ig = ec.ig;
4751
4752                         if (FieldInfo.IsStatic)
4753                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4754                         else {
4755                                 InstanceExpression.Emit (ec);
4756                                 
4757                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
4758                         }
4759                 }
4760
4761                 public void Store (EmitContext ec)
4762                 {
4763                         if (FieldInfo.IsStatic)
4764                                 ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
4765                         else
4766                                 ec.ig.Emit (OpCodes.Stfld, FieldInfo);
4767                 }
4768
4769                 public void AddressOf (EmitContext ec)
4770                 {
4771                         if (FieldInfo.IsStatic)
4772                                 ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
4773                         else {
4774                                 InstanceExpression.Emit (ec);
4775                                 ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
4776                         }
4777                 }
4778         }
4779         
4780         // <summary>
4781         //   Expression that evaluates to a Property.  The Assign class
4782         //   might set the `Value' expression if we are in an assignment.
4783         //
4784         //   This is not an LValue because we need to re-write the expression, we
4785         //   can not take data from the stack and store it.  
4786         // </summary>
4787         public class PropertyExpr : ExpressionStatement, IAssignMethod {
4788                 public readonly PropertyInfo PropertyInfo;
4789                 public readonly bool IsStatic;
4790                 MethodInfo [] Accessors;
4791                 Location loc;
4792                 
4793                 Expression instance_expr;
4794                 
4795                 public PropertyExpr (PropertyInfo pi, Location l)
4796                 {
4797                         PropertyInfo = pi;
4798                         eclass = ExprClass.PropertyAccess;
4799                         IsStatic = false;
4800                         loc = l;
4801                         Accessors = TypeManager.GetAccessors (pi);
4802
4803                         if (Accessors != null)
4804                                 for (int i = 0; i < Accessors.Length; i++){
4805                                         if (Accessors [i] != null)
4806                                                 if (Accessors [i].IsStatic)
4807                                                         IsStatic = true;
4808                                 }
4809                         else
4810                                 Accessors = new MethodInfo [2];
4811                         
4812                         type = pi.PropertyType;
4813                 }
4814
4815                 //
4816                 // The instance expression associated with this expression
4817                 //
4818                 public Expression InstanceExpression {
4819                         set {
4820                                 instance_expr = value;
4821                         }
4822
4823                         get {
4824                                 return instance_expr;
4825                         }
4826                 }
4827
4828                 public bool VerifyAssignable ()
4829                 {
4830                         if (!PropertyInfo.CanWrite){
4831                                 Report.Error (200, loc, 
4832                                               "The property `" + PropertyInfo.Name +
4833                                               "' can not be assigned to, as it has not set accessor");
4834                                 return false;
4835                         }
4836
4837                         return true;
4838                 }
4839
4840                 override public Expression DoResolve (EmitContext ec)
4841                 {
4842                         if (!PropertyInfo.CanRead){
4843                                 Report.Error (154, loc, 
4844                                               "The property `" + PropertyInfo.Name +
4845                                               "' can not be used in " +
4846                                               "this context because it lacks a get accessor");
4847                                 return null;
4848                         }
4849
4850                         return this;
4851                 }
4852
4853                 override public void Emit (EmitContext ec)
4854                 {
4855                         Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
4856                         
4857                 }
4858
4859                 //
4860                 // Implements the IAssignMethod interface for assignments
4861                 //
4862                 public void EmitAssign (EmitContext ec, Expression source)
4863                 {
4864                         Argument arg = new Argument (source, Argument.AType.Expression);
4865                         ArrayList args = new ArrayList ();
4866
4867                         args.Add (arg);
4868                         Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
4869                 }
4870
4871                 override public void EmitStatement (EmitContext ec)
4872                 {
4873                         Emit (ec);
4874                         ec.ig.Emit (OpCodes.Pop);
4875                 }
4876         }
4877
4878         // <summary>
4879         //   Fully resolved expression that evaluates to a Expression
4880         // </summary>
4881         public class EventExpr : Expression {
4882                 public readonly EventInfo EventInfo;
4883                 Location loc;
4884                 
4885                 public EventExpr (EventInfo ei, Location loc)
4886                 {
4887                         EventInfo = ei;
4888                         this.loc = loc;
4889                         eclass = ExprClass.EventAccess;
4890                 }
4891
4892                 override public Expression DoResolve (EmitContext ec)
4893                 {
4894                         // We are born in resolved state. 
4895                         return this;
4896                 }
4897
4898                 override public void Emit (EmitContext ec)
4899                 {
4900                         throw new Exception ("Implement me");
4901                         // FIXME: Implement.
4902                 }
4903         }
4904         
4905         public class CheckedExpr : Expression {
4906
4907                 public Expression Expr;
4908
4909                 public CheckedExpr (Expression e)
4910                 {
4911                         Expr = e;
4912                 }
4913
4914                 public override Expression DoResolve (EmitContext ec)
4915                 {
4916                         Expr = Expr.Resolve (ec);
4917
4918                         if (Expr == null)
4919                                 return null;
4920
4921                         eclass = Expr.ExprClass;
4922                         type = Expr.Type;
4923                         return this;
4924                 }
4925
4926                 public override void Emit (EmitContext ec)
4927                 {
4928                         bool last_check = ec.CheckState;
4929                         
4930                         ec.CheckState = true;
4931                         Expr.Emit (ec);
4932                         ec.CheckState = last_check;
4933                 }
4934                 
4935         }
4936
4937         public class UnCheckedExpr : Expression {
4938
4939                 public Expression Expr;
4940
4941                 public UnCheckedExpr (Expression e)
4942                 {
4943                         Expr = e;
4944                 }
4945
4946                 public override Expression DoResolve (EmitContext ec)
4947                 {
4948                         Expr = Expr.Resolve (ec);
4949
4950                         if (Expr == null)
4951                                 return null;
4952
4953                         eclass = Expr.ExprClass;
4954                         type = Expr.Type;
4955                         return this;
4956                 }
4957
4958                 public override void Emit (EmitContext ec)
4959                 {
4960                         bool last_check = ec.CheckState;
4961                         
4962                         ec.CheckState = false;
4963                         Expr.Emit (ec);
4964                         ec.CheckState = last_check;
4965                 }
4966                 
4967         }
4968
4969         public class ElementAccess : Expression {
4970                 
4971                 public ArrayList  Arguments;
4972                 public Expression Expr;
4973                 public Location   loc;
4974                 
4975                 public ElementAccess (Expression e, ArrayList e_list, Location l)
4976                 {
4977                         Expr = e;
4978
4979                         Arguments = new ArrayList ();
4980                         foreach (Expression tmp in e_list)
4981                                 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
4982                         
4983                         loc  = l;
4984                 }
4985
4986                 bool CommonResolve (EmitContext ec)
4987                 {
4988                         Expr = Expr.Resolve (ec);
4989
4990                         if (Expr == null) 
4991                                 return false;
4992
4993                         if (Arguments == null)
4994                                 return false;
4995
4996                         for (int i = Arguments.Count; i > 0;){
4997                                 --i;
4998                                 Argument a = (Argument) Arguments [i];
4999                                 
5000                                 if (!a.Resolve (ec))
5001                                         return false;
5002                         }
5003
5004                         return true;
5005                 }
5006                                 
5007                 public override Expression DoResolve (EmitContext ec)
5008                 {
5009                         if (!CommonResolve (ec))
5010                                 return null;
5011
5012                         //
5013                         // We perform some simple tests, and then to "split" the emit and store
5014                         // code we create an instance of a different class, and return that.
5015                         //
5016                         // I am experimenting with this pattern.
5017                         //
5018                         if (Expr.Type == TypeManager.array_type)
5019                                 return (new ArrayAccess (this)).Resolve (ec);
5020                         else
5021                                 return (new IndexerAccess (this)).Resolve (ec);
5022                 }
5023
5024                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5025                 {
5026                         if (!CommonResolve (ec))
5027                                 return null;
5028
5029                         if (Expr.Type == TypeManager.array_type)
5030                                 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
5031                         else
5032                                 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
5033                 }
5034                 
5035                 public override void Emit (EmitContext ec)
5036                 {
5037                         throw new Exception ("Should never be reached");
5038                 }
5039         }
5040
5041         public class ArrayAccess : Expression, IStackStore {
5042                 //
5043                 // Points to our "data" repository
5044                 //
5045                 ElementAccess ea;
5046                 
5047                 public ArrayAccess (ElementAccess ea_data)
5048                 {
5049                         ea = ea_data;
5050                         eclass = ExprClass.Variable;
5051
5052                         //
5053                         // FIXME: Figure out the type here
5054                         //
5055                 }
5056
5057                 Expression CommonResolve (EmitContext ec)
5058                 {
5059                         return this;
5060                 }
5061                 
5062                 public override Expression DoResolve (EmitContext ec)
5063                 {
5064                         if (ea.Expr.ExprClass != ExprClass.Variable) {
5065                                 report118 (ea.loc, ea.Expr, "variable");
5066                                 return null;
5067                         }
5068                         
5069                         throw new Exception ("Implement me");
5070                 }
5071
5072                 public void Store (EmitContext ec)
5073                 {
5074                         throw new Exception ("Implement me !");
5075                 }
5076
5077                 public override void Emit (EmitContext ec)
5078                 {
5079                         throw new Exception ("Implement me !");
5080                 }
5081         }
5082
5083         class Indexers {
5084                 public ArrayList getters, setters;
5085                 static Hashtable map;
5086
5087                 static Indexers ()
5088                 {
5089                         map = new Hashtable ();
5090                 }
5091
5092                 Indexers (MemberInfo [] mi)
5093                 {
5094                         foreach (PropertyInfo property in mi){
5095                                 MethodInfo get, set;
5096                                 
5097                                 get = property.GetGetMethod (true);
5098                                 if (get != null){
5099                                         if (getters == null)
5100                                                 getters = new ArrayList ();
5101
5102                                         getters.Add (get);
5103                                 }
5104                                 
5105                                 set = property.GetSetMethod (true);
5106                                 if (set != null){
5107                                         if (setters == null)
5108                                                 setters = new ArrayList ();
5109                                         setters.Add (set);
5110                                 }
5111                         }
5112                 }
5113                 
5114                 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc) 
5115                 {
5116                         Indexers ix = (Indexers) map [t];
5117                         string p_name = TypeManager.IndexerPropertyName (t);
5118                         
5119                         if (ix != null)
5120                                 return ix;
5121
5122                         MemberInfo [] mi = tm.FindMembers (
5123                                 t, MemberTypes.Property,
5124                                 BindingFlags.Public | BindingFlags.Instance,
5125                                 Type.FilterName, p_name);
5126
5127                         if (mi == null || mi.Length == 0){
5128                                 Report.Error (21, loc,
5129                                               "Type `" + TypeManager.CSharpName (t) + "' does not have " +
5130                                               "any indexers defined");
5131                                 return null;
5132                         }
5133                         
5134                         ix = new Indexers (mi);
5135                         map [t] = ix;
5136
5137                         return ix;
5138                 }
5139         }
5140         
5141         public class IndexerAccess : Expression, IAssignMethod {
5142                 //
5143                 // Points to our "data" repository
5144                 //
5145                 ElementAccess ea;
5146                 MethodInfo get, set;
5147                 Indexers ilist;
5148                 ArrayList set_arguments;
5149                 
5150                 public IndexerAccess (ElementAccess ea_data)
5151                 {
5152                         ea = ea_data;
5153                         eclass = ExprClass.Value;
5154                 }
5155
5156                 public bool VerifyAssignable (Expression source)
5157                 {
5158                         throw new Exception ("Implement me!");
5159                 }
5160
5161                 public override Expression DoResolve (EmitContext ec)
5162                 {
5163                         Type indexer_type = ea.Expr.Type;
5164                         
5165                         //
5166                         // Step 1: Query for all `Item' *properties*.  Notice
5167                         // that the actual methods are pointed from here.
5168                         //
5169                         // This is a group of properties, piles of them.  
5170
5171                         if (ilist == null)
5172                                 ilist = Indexers.GetIndexersForType (
5173                                         indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5174                         
5175                         if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
5176                                 get = (MethodInfo) Invocation.OverloadResolve (
5177                                         ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
5178
5179                         if (get == null){
5180                                 Report.Error (154, ea.loc,
5181                                               "indexer can not be used in this context, because " +
5182                                               "it lacks a `get' accessor");
5183                                         return null;
5184                         }
5185                                 
5186                         type = get.ReturnType;
5187                         eclass = ExprClass.Value;
5188                         return this;
5189                 }
5190
5191                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
5192                 {
5193                         Type indexer_type = ea.Expr.Type;
5194                         Type right_type = right_side.Type;
5195
5196                         if (ilist == null)
5197                                 ilist = Indexers.GetIndexersForType (
5198                                         indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
5199
5200                         if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
5201                                 set_arguments = (ArrayList) ea.Arguments.Clone ();
5202                                 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
5203
5204                                 set = (MethodInfo) Invocation.OverloadResolve (
5205                                         ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
5206                         }
5207                         
5208                         if (set == null){
5209                                 Report.Error (200, ea.loc,
5210                                               "indexer X.this [" + TypeManager.CSharpName (right_type) +
5211                                               "] lacks a `set' accessor");
5212                                         return null;
5213                         }
5214
5215                         type = TypeManager.void_type;
5216                         eclass = ExprClass.IndexerAccess;
5217                         return this;
5218                 }
5219                 
5220                 public override void Emit (EmitContext ec)
5221                 {
5222                         Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
5223                 }
5224
5225                 //
5226                 // source is ignored, because we already have a copy of it from the
5227                 // LValue resolution and we have already constructed a pre-cached
5228                 // version of the arguments (ea.set_arguments);
5229                 //
5230                 public void EmitAssign (EmitContext ec, Expression source)
5231                 {
5232                         Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
5233                 }
5234         }
5235         
5236         public class BaseAccess : Expression {
5237
5238                 public enum BaseAccessType {
5239                         Member,
5240                         Indexer
5241                 };
5242                 
5243                 public readonly BaseAccessType BAType;
5244                 public readonly string         Member;
5245                 public readonly ArrayList      Arguments;
5246
5247                 public BaseAccess (BaseAccessType t, string member, ArrayList args)
5248                 {
5249                         BAType = t;
5250                         Member = member;
5251                         Arguments = args;
5252                         
5253                 }
5254
5255                 public override Expression DoResolve (EmitContext ec)
5256                 {
5257                         // FIXME: Implement;
5258                         throw new Exception ("Unimplemented");
5259                         // return this;
5260                 }
5261
5262                 public override void Emit (EmitContext ec)
5263                 {
5264                         throw new Exception ("Unimplemented");
5265                 }
5266         }
5267
5268         // <summary>
5269         //   This class exists solely to pass the Type around and to be a dummy
5270         //   that can be passed to the conversion functions (this is used by
5271         //   foreach implementation to typecast the object return value from
5272         //   get_Current into the proper type.  All code has been generated and
5273         //   we only care about the side effect conversions to be performed
5274         // </summary>
5275         
5276         public class EmptyExpression : Expression {
5277                 public EmptyExpression ()
5278                 {
5279                         type = TypeManager.object_type;
5280                         eclass = ExprClass.Value;
5281                 }
5282
5283                 public override Expression DoResolve (EmitContext ec)
5284                 {
5285                         return this;
5286                 }
5287
5288                 public override void Emit (EmitContext ec)
5289                 {
5290                         // nothing, as we only exist to not do anything.
5291                 }
5292         }
5293
5294         public class UserCast : Expression {
5295                 MethodBase method;
5296                 Expression source;
5297                 
5298                 public UserCast (MethodInfo method, Expression source)
5299                 {
5300                         this.method = method;
5301                         this.source = source;
5302                         type = method.ReturnType;
5303                         eclass = ExprClass.Value;
5304                 }
5305
5306                 public override Expression DoResolve (EmitContext ec)
5307                 {
5308                         //
5309                         // We are born fully resolved
5310                         //
5311                         return this;
5312                 }
5313
5314                 public override void Emit (EmitContext ec)
5315                 {
5316                         ILGenerator ig = ec.ig;
5317
5318                         source.Emit (ec);
5319                         
5320                         if (method is MethodInfo)
5321                                 ig.Emit (OpCodes.Call, (MethodInfo) method);
5322                         else
5323                                 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5324
5325                 }
5326
5327         }
5328 }