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