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