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