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