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