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