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